blob: 05db4899b66a7a5d758f4eb0891069f84aaec59a [file] [log] [blame]
Jan Eilerse339bf62020-11-10 18:43:23 +00001//
Colm Donelan7bcae3c2024-01-22 10:07:14 +00002// Copyright © 2020, 2023-2024 Arm Ltd and Contributors. All rights reserved.
Jan Eilerse339bf62020-11-10 18:43:23 +00003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +00008#include <tensorflow/lite/c/common.h>
Matthew Sloyan91c41712020-11-13 09:47:35 +00009
Colm Donelaneff204a2023-11-28 15:46:09 +000010#include <armnn/BackendId.hpp>
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000011#include <half/half.hpp>
12
Colm Donelan7bcae3c2024-01-22 10:07:14 +000013#include <doctest/doctest.h>
14
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000015using Half = half_float::half;
16
Colm Donelaneff204a2023-11-28 15:46:09 +000017namespace
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 */
26std::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 Eilerse339bf62020-11-10 18:43:23 +000066namespace armnnDelegate
67{
68
Matthew Sloyanebe392d2023-03-30 10:12:08 +010069constexpr const char* FILE_IDENTIFIER = "TFL3";
Finn Williams63e6ace2021-02-17 18:12:39 +000070
Jan Eilersfe73b042020-11-18 10:36:46 +000071/// 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 Sloyanebe392d2023-03-30 10:12:08 +010074void CompareData(std::vector<bool>& tensor1, std::vector<bool>& tensor2, size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000075void 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 Eilers3812fbc2020-11-17 19:06:35 +000078void CompareData(float tensor1[], float tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000079
Narumol Prangnawarat7684b182021-08-12 14:48:15 +010080/// Can be used to compare float data coming from a tflite interpreter with a given percentage tolerance
81void CompareData(float tensor1[], float tensor2[], size_t tensorSize, float percentTolerance);
82
Jan Eilersfe73b042020-11-18 10:36:46 +000083/// Can be used to compare int8_t data coming from a tflite interpreter with a tolerance of 1
Jan Eilers3812fbc2020-11-17 19:06:35 +000084void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000085
86/// Can be used to compare uint8_t data coming from a tflite interpreter with a tolerance of 1
Jan Eilers3812fbc2020-11-17 19:06:35 +000087void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000088
89/// Can be used to compare int16_t data coming from a tflite interpreter with a tolerance of 1
Jan Eilers3812fbc2020-11-17 19:06:35 +000090void CompareData(int16_t tensor1[], int16_t tensor2[], size_t tensorSize);
91
Sadik Armagan29b49cf2021-02-22 18:09:07 +000092/// Can be used to compare int32_t data coming from a tflite interpreter with a tolerance of 1
93void CompareData(int32_t tensor1[], int32_t tensor2[], size_t tensorSize);
94
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000095/// Can be used to compare Half (Float16) data with a tolerance of limit_of_float*100
96void CompareData(Half tensor1[], Half tensor2[], size_t tensorSize);
97
98/// Can be used to compare TfLiteFloat16 data coming from a tflite interpreter
99void 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
102void CompareData(TfLiteFloat16 tensor1[], Half tensor2[], size_t tensorSize);
Jan Eilers3812fbc2020-11-17 19:06:35 +0000103
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100104/// Can be used to compare the output tensor shape
105/// Example usage can be found in ControlTestHelper.hpp
106void 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 Eilersfe73b042020-11-18 10:36:46 +0000111/// Example usage can be found in ControlTestHelper.hpp
Matthew Sloyan91c41712020-11-13 09:47:35 +0000112template <typename T>
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100113void CompareOutputData(std::vector<T>& tfLiteDelegateOutputs,
114 std::vector<T>& armnnDelegateOutputs,
115 std::vector<T>& expectedOutputValues)
Matthew Sloyan91c41712020-11-13 09:47:35 +0000116{
Colm Donelaneff204a2023-11-28 15:46:09 +0000117 armnnDelegate::CompareData(expectedOutputValues.data(), armnnDelegateOutputs.data(), expectedOutputValues.size());
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100118 armnnDelegate::CompareData(tfLiteDelegateOutputs.data(), expectedOutputValues.data(), expectedOutputValues.size());
119 armnnDelegate::CompareData(tfLiteDelegateOutputs.data(), armnnDelegateOutputs.data(), expectedOutputValues.size());
Matthew Sloyan91c41712020-11-13 09:47:35 +0000120}
121
Colm Donelaneff204a2023-11-28 15:46:09 +0000122} // namespace armnnDelegate