blob: 1bc5786112206d1a9d96ccd911d31dba6058d80b [file] [log] [blame]
Jan Eilers3812fbc2020-11-17 19:06:35 +00001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "TestUtils.hpp"
7
8namespace armnnDelegate
9{
10
Jan Eilersfe73b042020-11-18 10:36:46 +000011void CompareData(bool tensor1[], bool tensor2[], size_t tensorSize)
12{
13 auto compareBool = [](auto a, auto b) {return (((a == 0) && (b == 0)) || ((a != 0) && (b != 0)));};
14 for (size_t i = 0; i < tensorSize; i++)
15 {
16 CHECK(compareBool(tensor1[i], tensor2[i]));
17 }
18}
19
20void CompareData(std::vector<bool>& tensor1, bool tensor2[], size_t tensorSize)
21{
22 auto compareBool = [](auto a, auto b) {return (((a == 0) && (b == 0)) || ((a != 0) && (b != 0)));};
23 for (size_t i = 0; i < tensorSize; i++)
24 {
25 CHECK(compareBool(tensor1[i], tensor2[i]));
26 }
27}
28
Jan Eilers3812fbc2020-11-17 19:06:35 +000029void CompareData(float tensor1[], float tensor2[], size_t tensorSize)
30{
31 for (size_t i = 0; i < tensorSize; i++)
32 {
33 CHECK(tensor1[i] == doctest::Approx( tensor2[i] ));
34 }
35}
36
37void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize)
38{
39 uint8_t tolerance = 1;
40 for (size_t i = 0; i < tensorSize; i++)
41 {
42 CHECK(std::max(tensor1[i], tensor2[i]) - std::min(tensor1[i], tensor2[i]) <= tolerance);
43 }
44}
45
46void CompareData(int16_t tensor1[], int16_t tensor2[], size_t tensorSize)
47{
48 int16_t tolerance = 1;
49 for (size_t i = 0; i < tensorSize; i++)
50 {
51 CHECK(std::max(tensor1[i], tensor2[i]) - std::min(tensor1[i], tensor2[i]) <= tolerance);
52 }
53}
54
55void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize)
56{
57 int8_t tolerance = 1;
58 for (size_t i = 0; i < tensorSize; i++)
59 {
60 CHECK(std::max(tensor1[i], tensor2[i]) - std::min(tensor1[i], tensor2[i]) <= tolerance);
61 }
62}
63
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000064void CompareData(Half tensor1[], Half tensor2[], size_t tensorSize)
65{
66 for (size_t i = 0; i < tensorSize; i++)
67 {
68 CHECK(tensor1[i] == doctest::Approx( tensor2[i] ));
69 }
70}
71
72void CompareData(TfLiteFloat16 tensor1[], TfLiteFloat16 tensor2[], size_t tensorSize)
73{
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +000074 uint16_t tolerance = 1;
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000075 for (size_t i = 0; i < tensorSize; i++)
76 {
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +000077 uint16_t tensor1Data = tensor1[i].data;
78 uint16_t tensor2Data = tensor2[i].data;
79 CHECK(std::max(tensor1Data, tensor2Data) - std::min(tensor1Data, tensor2Data) <= tolerance);
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000080 }
81}
82
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +000083void CompareData(TfLiteFloat16 tensor1[], Half tensor2[], size_t tensorSize) {
84 uint16_t tolerance = 1;
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000085 for (size_t i = 0; i < tensorSize; i++)
86 {
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +000087 uint16_t tensor1Data = tensor1[i].data;
88 uint16_t tensor2Data = half_float::detail::float2half<std::round_indeterminate, float>(tensor2[i]);
89 CHECK(std::max(tensor1Data, tensor2Data) - std::min(tensor1Data, tensor2Data) <= tolerance);
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000090 }
91}
92
93template <>
94void CompareOutputData(std::unique_ptr<tflite::Interpreter>& tfLiteInterpreter,
95 std::unique_ptr<tflite::Interpreter>& armnnDelegateInterpreter,
96 std::vector<int32_t>& expectedOutputShape,
97 std::vector<Half>& expectedOutputValues,
98 unsigned int outputIndex)
99{
100 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex];
101 auto tfLiteDelegateOutputTensor = tfLiteInterpreter->tensor(tfLiteDelegateOutputId);
102 auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<TfLiteFloat16>(tfLiteDelegateOutputId);
103 auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[outputIndex];
104 auto armnnDelegateOutputTensor = armnnDelegateInterpreter->tensor(armnnDelegateOutputId);
105 auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<TfLiteFloat16>(armnnDelegateOutputId);
106
107 CHECK(expectedOutputShape.size() == tfLiteDelegateOutputTensor->dims->size);
108 CHECK(expectedOutputShape.size() == armnnDelegateOutputTensor->dims->size);
109
110 for (size_t i = 0; i < expectedOutputShape.size(); i++)
111 {
112 CHECK(armnnDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]);
113 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]);
114 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == armnnDelegateOutputTensor->dims->data[i]);
115 }
116
117 armnnDelegate::CompareData(armnnDelegateOutputData, expectedOutputValues.data(), expectedOutputValues.size());
118 armnnDelegate::CompareData(tfLiteDelegateOutputData, expectedOutputValues.data(), expectedOutputValues.size());
119 armnnDelegate::CompareData(tfLiteDelegateOutputData, armnnDelegateOutputData, expectedOutputValues.size());
120}
121
122template <>
123void FillInput<Half>(std::unique_ptr<tflite::Interpreter>& interpreter, int inputIndex, std::vector<Half>& inputValues)
124{
125 auto tfLiteDelegateInputId = interpreter->inputs()[inputIndex];
126 auto tfLiteDelageInputData = interpreter->typed_tensor<TfLiteFloat16>(tfLiteDelegateInputId);
127 for (unsigned int i = 0; i < inputValues.size(); ++i)
128 {
129 tfLiteDelageInputData[i].data = half_float::detail::float2half<std::round_indeterminate, float>(inputValues[i]);
130
131 }
132}
133
Jan Eilers3812fbc2020-11-17 19:06:35 +0000134} // namespace armnnDelegate