Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "TestUtils.hpp" |
| 7 | |
| 8 | namespace armnnDelegate |
| 9 | { |
| 10 | |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 11 | void 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 | |
| 20 | void 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 Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 29 | void 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 | |
| 37 | void 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 | |
| 46 | void 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 | |
Sadik Armagan | 29b49cf | 2021-02-22 18:09:07 +0000 | [diff] [blame] | 55 | void CompareData(int32_t tensor1[], int32_t tensor2[], size_t tensorSize) |
| 56 | { |
| 57 | int32_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 | |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 64 | void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize) |
| 65 | { |
| 66 | int8_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 | |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 73 | void CompareData(Half tensor1[], Half tensor2[], size_t tensorSize) |
| 74 | { |
| 75 | for (size_t i = 0; i < tensorSize; i++) |
| 76 | { |
| 77 | CHECK(tensor1[i] == doctest::Approx( tensor2[i] )); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void CompareData(TfLiteFloat16 tensor1[], TfLiteFloat16 tensor2[], size_t tensorSize) |
| 82 | { |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 83 | uint16_t tolerance = 1; |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 84 | for (size_t i = 0; i < tensorSize; i++) |
| 85 | { |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 86 | uint16_t tensor1Data = tensor1[i].data; |
| 87 | uint16_t tensor2Data = tensor2[i].data; |
| 88 | CHECK(std::max(tensor1Data, tensor2Data) - std::min(tensor1Data, tensor2Data) <= tolerance); |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 92 | void CompareData(TfLiteFloat16 tensor1[], Half tensor2[], size_t tensorSize) { |
| 93 | uint16_t tolerance = 1; |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 94 | for (size_t i = 0; i < tensorSize; i++) |
| 95 | { |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 96 | uint16_t tensor1Data = tensor1[i].data; |
| 97 | uint16_t tensor2Data = half_float::detail::float2half<std::round_indeterminate, float>(tensor2[i]); |
| 98 | CHECK(std::max(tensor1Data, tensor2Data) - std::min(tensor1Data, tensor2Data) <= tolerance); |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | template <> |
| 103 | void CompareOutputData(std::unique_ptr<tflite::Interpreter>& tfLiteInterpreter, |
| 104 | std::unique_ptr<tflite::Interpreter>& armnnDelegateInterpreter, |
| 105 | std::vector<int32_t>& expectedOutputShape, |
| 106 | std::vector<Half>& expectedOutputValues, |
| 107 | unsigned int outputIndex) |
| 108 | { |
| 109 | auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex]; |
| 110 | auto tfLiteDelegateOutputTensor = tfLiteInterpreter->tensor(tfLiteDelegateOutputId); |
| 111 | auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<TfLiteFloat16>(tfLiteDelegateOutputId); |
| 112 | auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[outputIndex]; |
| 113 | auto armnnDelegateOutputTensor = armnnDelegateInterpreter->tensor(armnnDelegateOutputId); |
| 114 | auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<TfLiteFloat16>(armnnDelegateOutputId); |
| 115 | |
| 116 | CHECK(expectedOutputShape.size() == tfLiteDelegateOutputTensor->dims->size); |
| 117 | CHECK(expectedOutputShape.size() == armnnDelegateOutputTensor->dims->size); |
| 118 | |
| 119 | for (size_t i = 0; i < expectedOutputShape.size(); i++) |
| 120 | { |
| 121 | CHECK(armnnDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]); |
| 122 | CHECK(tfLiteDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]); |
| 123 | CHECK(tfLiteDelegateOutputTensor->dims->data[i] == armnnDelegateOutputTensor->dims->data[i]); |
| 124 | } |
| 125 | |
| 126 | armnnDelegate::CompareData(armnnDelegateOutputData, expectedOutputValues.data(), expectedOutputValues.size()); |
| 127 | armnnDelegate::CompareData(tfLiteDelegateOutputData, expectedOutputValues.data(), expectedOutputValues.size()); |
| 128 | armnnDelegate::CompareData(tfLiteDelegateOutputData, armnnDelegateOutputData, expectedOutputValues.size()); |
| 129 | } |
| 130 | |
| 131 | template <> |
| 132 | void FillInput<Half>(std::unique_ptr<tflite::Interpreter>& interpreter, int inputIndex, std::vector<Half>& inputValues) |
| 133 | { |
| 134 | auto tfLiteDelegateInputId = interpreter->inputs()[inputIndex]; |
| 135 | auto tfLiteDelageInputData = interpreter->typed_tensor<TfLiteFloat16>(tfLiteDelegateInputId); |
| 136 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 137 | { |
| 138 | tfLiteDelageInputData[i].data = half_float::detail::float2half<std::round_indeterminate, float>(inputValues[i]); |
| 139 | |
| 140 | } |
| 141 | } |
| 142 | |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 143 | } // namespace armnnDelegate |