1 //
2 // Copyright © 2020, 2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #pragma once
7
8 #include <tensorflow/lite/c/common.h>
9 #include <tensorflow/lite/interpreter.h>
10
11 #include <doctest/doctest.h>
12
13 #include <half/half.hpp>
14
15 using Half = half_float::half;
16
17 namespace armnnDelegate
18 {
19
20 constexpr const char* FILE_IDENTIFIER = "TFL3";
21
22 /// Can be used to compare bool data coming from a tflite interpreter
23 /// Boolean types get converted to a bit representation in a vector. vector.data() returns a void pointer
24 /// instead of a pointer to bool. Therefore a special function to compare to vector of bool is required
25 void CompareData(std::vector<bool>& tensor1, std::vector<bool>& tensor2, size_t tensorSize);
26 void CompareData(bool tensor1[], bool tensor2[], size_t tensorSize);
27
28 /// Can be used to compare float data coming from a tflite interpreter with a tolerance of limit_of_float*100
29 void CompareData(float tensor1[], float tensor2[], size_t tensorSize);
30
31 /// Can be used to compare float data coming from a tflite interpreter with a given percentage tolerance
32 void CompareData(float tensor1[], float tensor2[], size_t tensorSize, float percentTolerance);
33
34 /// Can be used to compare int8_t data coming from a tflite interpreter with a tolerance of 1
35 void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize);
36
37 /// Can be used to compare uint8_t data coming from a tflite interpreter with a tolerance of 1
38 void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize);
39
40 /// Can be used to compare int16_t data coming from a tflite interpreter with a tolerance of 1
41 void CompareData(int16_t tensor1[], int16_t tensor2[], size_t tensorSize);
42
43 /// Can be used to compare int32_t data coming from a tflite interpreter with a tolerance of 1
44 void CompareData(int32_t tensor1[], int32_t tensor2[], size_t tensorSize);
45
46 /// Can be used to compare Half (Float16) data with a tolerance of limit_of_float*100
47 void CompareData(Half tensor1[], Half tensor2[], size_t tensorSize);
48
49 /// Can be used to compare TfLiteFloat16 data coming from a tflite interpreter
50 void CompareData(TfLiteFloat16 tensor1[], TfLiteFloat16 tensor2[], size_t tensorSize);
51
52 /// Can be used to compare Half (Float16) data and TfLiteFloat16 data coming from a tflite interpreter
53 void CompareData(TfLiteFloat16 tensor1[], Half tensor2[], size_t tensorSize);
54
55 /// Can be used to compare the output tensor shape
56 /// Example usage can be found in ControlTestHelper.hpp
57 void CompareOutputShape(const std::vector<int32_t>& tfLiteDelegateShape,
58 const std::vector<int32_t>& armnnDelegateShape,
59 const std::vector<int32_t>& expectedOutputShape);
60
61 /// Can be used to compare the output tensor values
62 /// Example usage can be found in ControlTestHelper.hpp
63 template <typename T>
CompareOutputData(std::vector<T> & tfLiteDelegateOutputs,std::vector<T> & armnnDelegateOutputs,std::vector<T> & expectedOutputValues)64 void CompareOutputData(std::vector<T>& tfLiteDelegateOutputs,
65 std::vector<T>& armnnDelegateOutputs,
66 std::vector<T>& expectedOutputValues)
67 {
68 armnnDelegate::CompareData(expectedOutputValues.data(), armnnDelegateOutputs.data(), expectedOutputValues.size());
69 armnnDelegate::CompareData(tfLiteDelegateOutputs.data(), expectedOutputValues.data(), expectedOutputValues.size());
70 armnnDelegate::CompareData(tfLiteDelegateOutputs.data(), armnnDelegateOutputs.data(), expectedOutputValues.size());
71 }
72
73 } // namespace armnnDelegate
74