blob: 0d53d9492beb2f33a932b60a7e4057830e76cd45 [file] [log] [blame]
Jan Eilers3812fbc2020-11-17 19:06:35 +00001//
Teresa Charlinad1b3d72023-03-14 12:10:28 +00002// Copyright © 2020, 2023 Arm Ltd and Contributors. All rights reserved.
Jan Eilers3812fbc2020-11-17 19:06:35 +00003// 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
Matthew Sloyanebe392d2023-03-30 10:12:08 +010020void CompareData(std::vector<bool>& tensor1, std::vector<bool>& tensor2, size_t tensorSize)
Jan Eilersfe73b042020-11-18 10:36:46 +000021{
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
Narumol Prangnawarat7684b182021-08-12 14:48:15 +010037void CompareData(float tensor1[], float tensor2[], size_t tensorSize, float percentTolerance)
38{
39 for (size_t i = 0; i < tensorSize; i++)
40 {
41 CHECK(std::max(tensor1[i], tensor2[i]) - std::min(tensor1[i], tensor2[i]) <=
42 std::abs(tensor1[i]*percentTolerance/100));
43 }
44}
45
Jan Eilers3812fbc2020-11-17 19:06:35 +000046void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize)
47{
48 uint8_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(int16_t tensor1[], int16_t tensor2[], size_t tensorSize)
56{
57 int16_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
Sadik Armagan29b49cf2021-02-22 18:09:07 +000064void CompareData(int32_t tensor1[], int32_t tensor2[], size_t tensorSize)
65{
66 int32_t tolerance = 1;
67 for (size_t i = 0; i < tensorSize; i++)
68 {
69 CHECK(std::max(tensor1[i], tensor2[i]) - std::min(tensor1[i], tensor2[i]) <= tolerance);
70 }
71}
72
Jan Eilers3812fbc2020-11-17 19:06:35 +000073void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize)
74{
75 int8_t tolerance = 1;
76 for (size_t i = 0; i < tensorSize; i++)
77 {
78 CHECK(std::max(tensor1[i], tensor2[i]) - std::min(tensor1[i], tensor2[i]) <= tolerance);
79 }
80}
81
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000082void CompareData(Half tensor1[], Half tensor2[], size_t tensorSize)
83{
84 for (size_t i = 0; i < tensorSize; i++)
85 {
86 CHECK(tensor1[i] == doctest::Approx( tensor2[i] ));
87 }
88}
89
90void CompareData(TfLiteFloat16 tensor1[], TfLiteFloat16 tensor2[], size_t tensorSize)
91{
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +000092 uint16_t tolerance = 1;
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000093 for (size_t i = 0; i < tensorSize; i++)
94 {
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +000095 uint16_t tensor1Data = tensor1[i].data;
96 uint16_t tensor2Data = tensor2[i].data;
97 CHECK(std::max(tensor1Data, tensor2Data) - std::min(tensor1Data, tensor2Data) <= tolerance);
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000098 }
99}
100
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +0000101void CompareData(TfLiteFloat16 tensor1[], Half tensor2[], size_t tensorSize) {
102 uint16_t tolerance = 1;
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +0000103 for (size_t i = 0; i < tensorSize; i++)
104 {
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +0000105 uint16_t tensor1Data = tensor1[i].data;
106 uint16_t tensor2Data = half_float::detail::float2half<std::round_indeterminate, float>(tensor2[i]);
107 CHECK(std::max(tensor1Data, tensor2Data) - std::min(tensor1Data, tensor2Data) <= tolerance);
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +0000108 }
109}
110
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100111void CompareOutputShape(const std::vector<int32_t>& tfLiteDelegateShape,
112 const std::vector<int32_t>& armnnDelegateShape,
113 const std::vector<int32_t>& expectedOutputShape)
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +0000114{
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100115 CHECK(expectedOutputShape.size() == tfLiteDelegateShape.size());
116 CHECK(expectedOutputShape.size() == armnnDelegateShape.size());
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +0000117
118 for (size_t i = 0; i < expectedOutputShape.size(); i++)
119 {
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100120 CHECK(expectedOutputShape[i] == armnnDelegateShape[i]);
121 CHECK(tfLiteDelegateShape[i] == expectedOutputShape[i]);
122 CHECK(tfLiteDelegateShape[i] == armnnDelegateShape[i]);
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +0000123 }
124}
125
Jan Eilers3812fbc2020-11-17 19:06:35 +0000126} // namespace armnnDelegate