Jan Eilers | e339bf6 | 2020-11-10 18:43:23 +0000 | [diff] [blame] | 1 | // |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame] | 2 | // Copyright © 2020, 2023-2024 Arm Ltd and Contributors. All rights reserved. |
Jan Eilers | e339bf6 | 2020-11-10 18:43:23 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 8 | #include <tensorflow/lite/c/common.h> |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 9 | |
Colm Donelan | eff204a | 2023-11-28 15:46:09 +0000 | [diff] [blame] | 10 | #include <armnn/BackendId.hpp> |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 11 | #include <half/half.hpp> |
| 12 | |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame] | 13 | #include <doctest/doctest.h> |
| 14 | |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 15 | using Half = half_float::half; |
| 16 | |
Colm Donelan | eff204a | 2023-11-28 15:46:09 +0000 | [diff] [blame] | 17 | namespace |
| 18 | { |
| 19 | /** |
| 20 | * Based on the compilation options capture subcases for the available backends. If "onlyTheseBackends" is NOT empty |
| 21 | * then we'll ignore any backend NOT listed in it. |
| 22 | * |
| 23 | * @param onlyTheseBackends limit the number of backends considered for sub casing. If empty all are considered. |
| 24 | * @return vector of backends that have been captured for sub casing. |
| 25 | */ |
| 26 | std::vector<armnn::BackendId> CaptureAvailableBackends(const std::vector<armnn::BackendId>& onlyTheseBackends) |
| 27 | { |
| 28 | std::vector<armnn::BackendId> availableBackends; |
| 29 | #if defined(ARMNNREF_ENABLED) |
| 30 | // Careful logic here. An empty onlyTheseBackends means we always evaluate. |
| 31 | if (onlyTheseBackends.empty() || (std::find(onlyTheseBackends.begin(), onlyTheseBackends.end(), |
| 32 | armnn::Compute::CpuRef) != onlyTheseBackends.end())) |
| 33 | { |
| 34 | SUBCASE("CpuRef") |
| 35 | { |
| 36 | availableBackends.push_back({ armnn::Compute::CpuRef }); |
| 37 | } |
| 38 | } |
| 39 | #endif |
| 40 | #if defined(ARMCOMPUTENEON_ENABLED) |
| 41 | // Careful logic here. An empty onlyTheseBackends means we always evaluate. |
| 42 | if (onlyTheseBackends.empty() || (std::find(onlyTheseBackends.begin(), onlyTheseBackends.end(), |
| 43 | armnn::Compute::CpuAcc) != onlyTheseBackends.end())) |
| 44 | { |
| 45 | SUBCASE("CpuAcc") |
| 46 | { |
| 47 | availableBackends.push_back({ armnn::Compute::CpuAcc }); |
| 48 | } |
| 49 | } |
| 50 | #endif |
| 51 | #if defined(ARMCOMPUTECL_ENABLED) |
| 52 | if (onlyTheseBackends.empty() || (std::find(onlyTheseBackends.begin(), onlyTheseBackends.end(), |
| 53 | armnn::Compute::GpuAcc) != onlyTheseBackends.end())) |
| 54 | { |
| 55 | SUBCASE("GpuAcc") |
| 56 | { |
| 57 | availableBackends.push_back({ armnn::Compute::GpuAcc }); |
| 58 | } |
| 59 | } |
| 60 | #endif |
| 61 | CAPTURE(availableBackends); |
| 62 | return availableBackends; |
| 63 | } |
| 64 | |
| 65 | } // namespace |
Jan Eilers | e339bf6 | 2020-11-10 18:43:23 +0000 | [diff] [blame] | 66 | namespace armnnDelegate |
| 67 | { |
| 68 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 69 | constexpr const char* FILE_IDENTIFIER = "TFL3"; |
Finn Williams | 63e6ace | 2021-02-17 18:12:39 +0000 | [diff] [blame] | 70 | |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 71 | /// Can be used to compare bool data coming from a tflite interpreter |
| 72 | /// Boolean types get converted to a bit representation in a vector. vector.data() returns a void pointer |
| 73 | /// instead of a pointer to bool. Therefore a special function to compare to vector of bool is required |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 74 | void CompareData(std::vector<bool>& tensor1, std::vector<bool>& tensor2, size_t tensorSize); |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 75 | void CompareData(bool tensor1[], bool tensor2[], size_t tensorSize); |
| 76 | |
| 77 | /// Can be used to compare float data coming from a tflite interpreter with a tolerance of limit_of_float*100 |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 78 | void CompareData(float tensor1[], float tensor2[], size_t tensorSize); |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 79 | |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 80 | /// Can be used to compare float data coming from a tflite interpreter with a given percentage tolerance |
| 81 | void CompareData(float tensor1[], float tensor2[], size_t tensorSize, float percentTolerance); |
| 82 | |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 83 | /// Can be used to compare int8_t data coming from a tflite interpreter with a tolerance of 1 |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 84 | void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize); |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 85 | |
| 86 | /// Can be used to compare uint8_t data coming from a tflite interpreter with a tolerance of 1 |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 87 | void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize); |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 88 | |
| 89 | /// Can be used to compare int16_t data coming from a tflite interpreter with a tolerance of 1 |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 90 | void CompareData(int16_t tensor1[], int16_t tensor2[], size_t tensorSize); |
| 91 | |
Sadik Armagan | 29b49cf | 2021-02-22 18:09:07 +0000 | [diff] [blame] | 92 | /// Can be used to compare int32_t data coming from a tflite interpreter with a tolerance of 1 |
| 93 | void CompareData(int32_t tensor1[], int32_t tensor2[], size_t tensorSize); |
| 94 | |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 95 | /// Can be used to compare Half (Float16) data with a tolerance of limit_of_float*100 |
| 96 | void CompareData(Half tensor1[], Half tensor2[], size_t tensorSize); |
| 97 | |
| 98 | /// Can be used to compare TfLiteFloat16 data coming from a tflite interpreter |
| 99 | void CompareData(TfLiteFloat16 tensor1[], TfLiteFloat16 tensor2[], size_t tensorSize); |
| 100 | |
| 101 | /// Can be used to compare Half (Float16) data and TfLiteFloat16 data coming from a tflite interpreter |
| 102 | void CompareData(TfLiteFloat16 tensor1[], Half tensor2[], size_t tensorSize); |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 103 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 104 | /// Can be used to compare the output tensor shape |
| 105 | /// Example usage can be found in ControlTestHelper.hpp |
| 106 | void CompareOutputShape(const std::vector<int32_t>& tfLiteDelegateShape, |
| 107 | const std::vector<int32_t>& armnnDelegateShape, |
| 108 | const std::vector<int32_t>& expectedOutputShape); |
| 109 | |
| 110 | /// Can be used to compare the output tensor values |
Jan Eilers | fe73b04 | 2020-11-18 10:36:46 +0000 | [diff] [blame] | 111 | /// Example usage can be found in ControlTestHelper.hpp |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 112 | template <typename T> |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 113 | void CompareOutputData(std::vector<T>& tfLiteDelegateOutputs, |
| 114 | std::vector<T>& armnnDelegateOutputs, |
| 115 | std::vector<T>& expectedOutputValues) |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 116 | { |
Colm Donelan | eff204a | 2023-11-28 15:46:09 +0000 | [diff] [blame] | 117 | armnnDelegate::CompareData(expectedOutputValues.data(), armnnDelegateOutputs.data(), expectedOutputValues.size()); |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 118 | armnnDelegate::CompareData(tfLiteDelegateOutputs.data(), expectedOutputValues.data(), expectedOutputValues.size()); |
| 119 | armnnDelegate::CompareData(tfLiteDelegateOutputs.data(), armnnDelegateOutputs.data(), expectedOutputValues.size()); |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Colm Donelan | eff204a | 2023-11-28 15:46:09 +0000 | [diff] [blame] | 122 | } // namespace armnnDelegate |