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 | |
| 55 | void 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 Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 64 | void 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 | |
| 72 | void CompareData(TfLiteFloat16 tensor1[], TfLiteFloat16 tensor2[], size_t tensorSize) |
| 73 | { |
| 74 | for (size_t i = 0; i < tensorSize; i++) |
| 75 | { |
| 76 | CHECK(tensor1[i].data == tensor2[i].data); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void CompareData(TfLiteFloat16 tensor1[], Half tensor2[], size_t tensorSize) |
| 81 | { |
| 82 | for (size_t i = 0; i < tensorSize; i++) |
| 83 | { |
| 84 | CHECK(tensor1[i].data == half_float::detail::float2half<std::round_indeterminate, float>(tensor2[i])); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | template <> |
| 89 | void CompareOutputData(std::unique_ptr<tflite::Interpreter>& tfLiteInterpreter, |
| 90 | std::unique_ptr<tflite::Interpreter>& armnnDelegateInterpreter, |
| 91 | std::vector<int32_t>& expectedOutputShape, |
| 92 | std::vector<Half>& expectedOutputValues, |
| 93 | unsigned int outputIndex) |
| 94 | { |
| 95 | auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex]; |
| 96 | auto tfLiteDelegateOutputTensor = tfLiteInterpreter->tensor(tfLiteDelegateOutputId); |
| 97 | auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<TfLiteFloat16>(tfLiteDelegateOutputId); |
| 98 | auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[outputIndex]; |
| 99 | auto armnnDelegateOutputTensor = armnnDelegateInterpreter->tensor(armnnDelegateOutputId); |
| 100 | auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<TfLiteFloat16>(armnnDelegateOutputId); |
| 101 | |
| 102 | CHECK(expectedOutputShape.size() == tfLiteDelegateOutputTensor->dims->size); |
| 103 | CHECK(expectedOutputShape.size() == armnnDelegateOutputTensor->dims->size); |
| 104 | |
| 105 | for (size_t i = 0; i < expectedOutputShape.size(); i++) |
| 106 | { |
| 107 | CHECK(armnnDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]); |
| 108 | CHECK(tfLiteDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]); |
| 109 | CHECK(tfLiteDelegateOutputTensor->dims->data[i] == armnnDelegateOutputTensor->dims->data[i]); |
| 110 | } |
| 111 | |
| 112 | armnnDelegate::CompareData(armnnDelegateOutputData, expectedOutputValues.data(), expectedOutputValues.size()); |
| 113 | armnnDelegate::CompareData(tfLiteDelegateOutputData, expectedOutputValues.data(), expectedOutputValues.size()); |
| 114 | armnnDelegate::CompareData(tfLiteDelegateOutputData, armnnDelegateOutputData, expectedOutputValues.size()); |
| 115 | } |
| 116 | |
| 117 | template <> |
| 118 | void FillInput<Half>(std::unique_ptr<tflite::Interpreter>& interpreter, int inputIndex, std::vector<Half>& inputValues) |
| 119 | { |
| 120 | auto tfLiteDelegateInputId = interpreter->inputs()[inputIndex]; |
| 121 | auto tfLiteDelageInputData = interpreter->typed_tensor<TfLiteFloat16>(tfLiteDelegateInputId); |
| 122 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 123 | { |
| 124 | tfLiteDelageInputData[i].data = half_float::detail::float2half<std::round_indeterminate, float>(inputValues[i]); |
| 125 | |
| 126 | } |
| 127 | } |
| 128 | |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 129 | } // namespace armnnDelegate |