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 | |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 8 | #include <tensorflow/lite/c/common.h> |
Jan Eilers | e339bf6 | 2020-11-10 18:43:23 +0000 | [diff] [blame] | 9 | #include <tensorflow/lite/interpreter.h> |
| 10 | |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 11 | #include <doctest/doctest.h> |
| 12 | |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 13 | #include <half/half.hpp> |
| 14 | |
| 15 | using Half = half_float::half; |
| 16 | |
Jan Eilers | e339bf6 | 2020-11-10 18:43:23 +0000 | [diff] [blame] | 17 | namespace armnnDelegate |
| 18 | { |
| 19 | |
| 20 | /// Can be used to assign input data from a vector to a model input. |
| 21 | /// Example usage can be found in ResizeTesthelper.hpp |
| 22 | template <typename T> |
| 23 | void FillInput(std::unique_ptr<tflite::Interpreter>& interpreter, int inputIndex, std::vector<T>& inputValues) |
| 24 | { |
| 25 | auto tfLiteDelegateInputId = interpreter->inputs()[inputIndex]; |
| 26 | auto tfLiteDelageInputData = interpreter->typed_tensor<T>(tfLiteDelegateInputId); |
| 27 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 28 | { |
| 29 | tfLiteDelageInputData[i] = inputValues[i]; |
| 30 | } |
| 31 | } |
| 32 | |
Finn Williams | 63e6ace | 2021-02-17 18:12:39 +0000 | [diff] [blame] | 33 | template <> |
| 34 | void FillInput(std::unique_ptr<tflite::Interpreter>& interpreter, int inputIndex, std::vector<Half>& inputValues); |
| 35 | |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 36 | /// Can be used to compare bool data coming from a tflite interpreter |
| 37 | /// Boolean types get converted to a bit representation in a vector. vector.data() returns a void pointer |
| 38 | /// instead of a pointer to bool. Therefore a special function to compare to vector of bool is required |
| 39 | void CompareData(std::vector<bool>& tensor1, bool tensor2[], size_t tensorSize); |
| 40 | void CompareData(bool tensor1[], bool tensor2[], size_t tensorSize); |
| 41 | |
| 42 | /// 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] | 43 | void CompareData(float tensor1[], float tensor2[], size_t tensorSize); |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 44 | |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 45 | /// Can be used to compare float data coming from a tflite interpreter with a given percentage tolerance |
| 46 | void CompareData(float tensor1[], float tensor2[], size_t tensorSize, float percentTolerance); |
| 47 | |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 48 | /// 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] | 49 | void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize); |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 50 | |
| 51 | /// 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] | 52 | void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize); |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 53 | |
| 54 | /// 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] | 55 | void CompareData(int16_t tensor1[], int16_t tensor2[], size_t tensorSize); |
| 56 | |
Sadik Armagan | 29b49cf | 2021-02-22 18:09:07 +0000 | [diff] [blame] | 57 | /// Can be used to compare int32_t data coming from a tflite interpreter with a tolerance of 1 |
| 58 | void CompareData(int32_t tensor1[], int32_t tensor2[], size_t tensorSize); |
| 59 | |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 60 | /// Can be used to compare Half (Float16) data with a tolerance of limit_of_float*100 |
| 61 | void CompareData(Half tensor1[], Half tensor2[], size_t tensorSize); |
| 62 | |
| 63 | /// Can be used to compare TfLiteFloat16 data coming from a tflite interpreter |
| 64 | void CompareData(TfLiteFloat16 tensor1[], TfLiteFloat16 tensor2[], size_t tensorSize); |
| 65 | |
| 66 | /// Can be used to compare Half (Float16) data and TfLiteFloat16 data coming from a tflite interpreter |
| 67 | void CompareData(TfLiteFloat16 tensor1[], Half tensor2[], size_t tensorSize); |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 68 | |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 69 | /// Can be used to compare the output tensor shape and values |
| 70 | /// from armnnDelegateInterpreter and tfLiteInterpreter. |
| 71 | /// Example usage can be found in ControlTestHelper.hpp |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 72 | template <typename T> |
| 73 | void CompareOutputData(std::unique_ptr<tflite::Interpreter>& tfLiteInterpreter, |
| 74 | std::unique_ptr<tflite::Interpreter>& armnnDelegateInterpreter, |
| 75 | std::vector<int32_t>& expectedOutputShape, |
Sadik Armagan | 34fa1bd | 2020-11-27 12:40:52 +0000 | [diff] [blame] | 76 | std::vector<T>& expectedOutputValues, |
| 77 | unsigned int outputIndex = 0) |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 78 | { |
Sadik Armagan | 34fa1bd | 2020-11-27 12:40:52 +0000 | [diff] [blame] | 79 | auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex]; |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 80 | auto tfLiteDelegateOutputTensor = tfLiteInterpreter->tensor(tfLiteDelegateOutputId); |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 81 | auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateOutputId); |
Sadik Armagan | 34fa1bd | 2020-11-27 12:40:52 +0000 | [diff] [blame] | 82 | auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[outputIndex]; |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 83 | auto armnnDelegateOutputTensor = armnnDelegateInterpreter->tensor(armnnDelegateOutputId); |
| 84 | auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateOutputId); |
| 85 | |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 86 | CHECK(expectedOutputShape.size() == tfLiteDelegateOutputTensor->dims->size); |
| 87 | CHECK(expectedOutputShape.size() == armnnDelegateOutputTensor->dims->size); |
| 88 | |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 89 | for (size_t i = 0; i < expectedOutputShape.size(); i++) |
| 90 | { |
| 91 | CHECK(expectedOutputShape[i] == armnnDelegateOutputTensor->dims->data[i]); |
| 92 | CHECK(tfLiteDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]); |
| 93 | CHECK(tfLiteDelegateOutputTensor->dims->data[i] == armnnDelegateOutputTensor->dims->data[i]); |
| 94 | } |
| 95 | |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 96 | armnnDelegate::CompareData(expectedOutputValues.data(), armnnDelegateOutputData , expectedOutputValues.size()); |
| 97 | armnnDelegate::CompareData(tfLiteDelegateOutputData , expectedOutputValues.data(), expectedOutputValues.size()); |
| 98 | armnnDelegate::CompareData(tfLiteDelegateOutputData , armnnDelegateOutputData , expectedOutputValues.size()); |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Jan Eilers | e339bf6 | 2020-11-10 18:43:23 +0000 | [diff] [blame] | 101 | } // namespace armnnDelegate |