Jan Eilers | e339bf6 | 2020-11-10 18:43:23 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <tensorflow/lite/interpreter.h> |
| 9 | |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 10 | #include <doctest/doctest.h> |
| 11 | |
Jan Eilers | e339bf6 | 2020-11-10 18:43:23 +0000 | [diff] [blame] | 12 | namespace armnnDelegate |
| 13 | { |
| 14 | |
| 15 | /// Can be used to assign input data from a vector to a model input. |
| 16 | /// Example usage can be found in ResizeTesthelper.hpp |
| 17 | template <typename T> |
| 18 | void FillInput(std::unique_ptr<tflite::Interpreter>& interpreter, int inputIndex, std::vector<T>& inputValues) |
| 19 | { |
| 20 | auto tfLiteDelegateInputId = interpreter->inputs()[inputIndex]; |
| 21 | auto tfLiteDelageInputData = interpreter->typed_tensor<T>(tfLiteDelegateInputId); |
| 22 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 23 | { |
| 24 | tfLiteDelageInputData[i] = inputValues[i]; |
| 25 | } |
| 26 | } |
| 27 | |
Jan Eilers | b33f28b | 2020-11-18 10:36:46 +0000 | [diff] [blame^] | 28 | /// Can be used to compare bool data coming from a tflite interpreter |
| 29 | /// Boolean types get converted to a bit representation in a vector. vector.data() returns a void pointer |
| 30 | /// instead of a pointer to bool. Therefore a special function to compare to vector of bool is required |
| 31 | void CompareData(std::vector<bool>& tensor1, bool tensor2[], size_t tensorSize); |
| 32 | void CompareData(bool tensor1[], bool tensor2[], size_t tensorSize); |
| 33 | |
| 34 | /// Can be used to compare float data coming from a tflite interpreter with a tolerance of limit_of_float*100 |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 35 | void CompareData(float tensor1[], float tensor2[], size_t tensorSize); |
Jan Eilers | b33f28b | 2020-11-18 10:36:46 +0000 | [diff] [blame^] | 36 | |
| 37 | /// Can be used to compare int8_t data coming from a tflite interpreter with a tolerance of 1 |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 38 | void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize); |
Jan Eilers | b33f28b | 2020-11-18 10:36:46 +0000 | [diff] [blame^] | 39 | |
| 40 | /// Can be used to compare uint8_t data coming from a tflite interpreter with a tolerance of 1 |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 41 | void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize); |
Jan Eilers | b33f28b | 2020-11-18 10:36:46 +0000 | [diff] [blame^] | 42 | |
| 43 | /// Can be used to compare int16_t data coming from a tflite interpreter with a tolerance of 1 |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 44 | void CompareData(int16_t tensor1[], int16_t tensor2[], size_t tensorSize); |
| 45 | |
| 46 | |
Jan Eilers | b33f28b | 2020-11-18 10:36:46 +0000 | [diff] [blame^] | 47 | /// Can be used to compare the output tensor shape and values |
| 48 | /// from armnnDelegateInterpreter and tfLiteInterpreter. |
| 49 | /// Example usage can be found in ControlTestHelper.hpp |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 50 | template <typename T> |
| 51 | void CompareOutputData(std::unique_ptr<tflite::Interpreter>& tfLiteInterpreter, |
| 52 | std::unique_ptr<tflite::Interpreter>& armnnDelegateInterpreter, |
| 53 | std::vector<int32_t>& expectedOutputShape, |
| 54 | std::vector<T>& expectedOutputValues) |
| 55 | { |
| 56 | auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[0]; |
| 57 | auto tfLiteDelegateOutputTensor = tfLiteInterpreter->tensor(tfLiteDelegateOutputId); |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 58 | auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateOutputId); |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 59 | auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[0]; |
| 60 | auto armnnDelegateOutputTensor = armnnDelegateInterpreter->tensor(armnnDelegateOutputId); |
| 61 | auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateOutputId); |
| 62 | |
| 63 | for (size_t i = 0; i < expectedOutputShape.size(); i++) |
| 64 | { |
| 65 | CHECK(expectedOutputShape[i] == armnnDelegateOutputTensor->dims->data[i]); |
| 66 | CHECK(tfLiteDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]); |
| 67 | CHECK(tfLiteDelegateOutputTensor->dims->data[i] == armnnDelegateOutputTensor->dims->data[i]); |
| 68 | } |
| 69 | |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 70 | armnnDelegate::CompareData(expectedOutputValues.data(), armnnDelegateOutputData , expectedOutputValues.size()); |
| 71 | armnnDelegate::CompareData(tfLiteDelegateOutputData , expectedOutputValues.data(), expectedOutputValues.size()); |
| 72 | armnnDelegate::CompareData(tfLiteDelegateOutputData , armnnDelegateOutputData , expectedOutputValues.size()); |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Jan Eilers | e339bf6 | 2020-11-10 18:43:23 +0000 | [diff] [blame] | 75 | } // namespace armnnDelegate |