blob: 5d4a0ed7d4c451933543cff4c256d989da7c6707 [file] [log] [blame]
Jan Eilerse339bf62020-11-10 18:43:23 +00001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +00008#include <tensorflow/lite/c/common.h>
Jan Eilerse339bf62020-11-10 18:43:23 +00009#include <tensorflow/lite/interpreter.h>
10
Matthew Sloyan91c41712020-11-13 09:47:35 +000011#include <doctest/doctest.h>
12
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000013#include <half/half.hpp>
14
15using Half = half_float::half;
16
Jan Eilerse339bf62020-11-10 18:43:23 +000017namespace armnnDelegate
18{
19
20/// Can be used to assign input data from a vector to a model input.
21/// Example usage can be found in ResizeTesthelper.hpp
22template <typename T>
23void FillInput(std::unique_ptr<tflite::Interpreter>& interpreter, int inputIndex, std::vector<T>& inputValues)
24{
25 auto tfLiteDelegateInputId = interpreter->inputs()[inputIndex];
26 auto tfLiteDelageInputData = interpreter->typed_tensor<T>(tfLiteDelegateInputId);
27 for (unsigned int i = 0; i < inputValues.size(); ++i)
28 {
29 tfLiteDelageInputData[i] = inputValues[i];
30 }
31}
32
Finn Williams63e6ace2021-02-17 18:12:39 +000033template <>
34void FillInput(std::unique_ptr<tflite::Interpreter>& interpreter, int inputIndex, std::vector<Half>& inputValues);
35
Jan Eilersfe73b042020-11-18 10:36:46 +000036/// Can be used to compare bool data coming from a tflite interpreter
37/// Boolean types get converted to a bit representation in a vector. vector.data() returns a void pointer
38/// instead of a pointer to bool. Therefore a special function to compare to vector of bool is required
39void CompareData(std::vector<bool>& tensor1, bool tensor2[], size_t tensorSize);
40void CompareData(bool tensor1[], bool tensor2[], size_t tensorSize);
41
42/// 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 +000043void CompareData(float tensor1[], float tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000044
Narumol Prangnawarat7684b182021-08-12 14:48:15 +010045/// Can be used to compare float data coming from a tflite interpreter with a given percentage tolerance
46void CompareData(float tensor1[], float tensor2[], size_t tensorSize, float percentTolerance);
47
Jan Eilersfe73b042020-11-18 10:36:46 +000048/// 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 +000049void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000050
51/// 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 +000052void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000053
54/// 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 +000055void CompareData(int16_t tensor1[], int16_t tensor2[], size_t tensorSize);
56
Sadik Armagan29b49cf2021-02-22 18:09:07 +000057/// Can be used to compare int32_t data coming from a tflite interpreter with a tolerance of 1
58void CompareData(int32_t tensor1[], int32_t tensor2[], size_t tensorSize);
59
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000060/// Can be used to compare Half (Float16) data with a tolerance of limit_of_float*100
61void CompareData(Half tensor1[], Half tensor2[], size_t tensorSize);
62
63/// Can be used to compare TfLiteFloat16 data coming from a tflite interpreter
64void CompareData(TfLiteFloat16 tensor1[], TfLiteFloat16 tensor2[], size_t tensorSize);
65
66/// Can be used to compare Half (Float16) data and TfLiteFloat16 data coming from a tflite interpreter
67void CompareData(TfLiteFloat16 tensor1[], Half tensor2[], size_t tensorSize);
Jan Eilers3812fbc2020-11-17 19:06:35 +000068
Jan Eilersfe73b042020-11-18 10:36:46 +000069/// Can be used to compare the output tensor shape and values
70/// from armnnDelegateInterpreter and tfLiteInterpreter.
71/// Example usage can be found in ControlTestHelper.hpp
Matthew Sloyan91c41712020-11-13 09:47:35 +000072template <typename T>
73void CompareOutputData(std::unique_ptr<tflite::Interpreter>& tfLiteInterpreter,
74 std::unique_ptr<tflite::Interpreter>& armnnDelegateInterpreter,
75 std::vector<int32_t>& expectedOutputShape,
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000076 std::vector<T>& expectedOutputValues,
77 unsigned int outputIndex = 0)
Matthew Sloyan91c41712020-11-13 09:47:35 +000078{
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000079 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex];
Matthew Sloyan91c41712020-11-13 09:47:35 +000080 auto tfLiteDelegateOutputTensor = tfLiteInterpreter->tensor(tfLiteDelegateOutputId);
Jan Eilers3812fbc2020-11-17 19:06:35 +000081 auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateOutputId);
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000082 auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[outputIndex];
Matthew Sloyan91c41712020-11-13 09:47:35 +000083 auto armnnDelegateOutputTensor = armnnDelegateInterpreter->tensor(armnnDelegateOutputId);
84 auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateOutputId);
85
Narumol Prangnawarat958024b2020-12-17 12:17:58 +000086 CHECK(expectedOutputShape.size() == tfLiteDelegateOutputTensor->dims->size);
87 CHECK(expectedOutputShape.size() == armnnDelegateOutputTensor->dims->size);
88
Matthew Sloyan91c41712020-11-13 09:47:35 +000089 for (size_t i = 0; i < expectedOutputShape.size(); i++)
90 {
91 CHECK(expectedOutputShape[i] == armnnDelegateOutputTensor->dims->data[i]);
92 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]);
93 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == armnnDelegateOutputTensor->dims->data[i]);
94 }
95
Jan Eilers3812fbc2020-11-17 19:06:35 +000096 armnnDelegate::CompareData(expectedOutputValues.data(), armnnDelegateOutputData , expectedOutputValues.size());
97 armnnDelegate::CompareData(tfLiteDelegateOutputData , expectedOutputValues.data(), expectedOutputValues.size());
98 armnnDelegate::CompareData(tfLiteDelegateOutputData , armnnDelegateOutputData , expectedOutputValues.size());
Matthew Sloyan91c41712020-11-13 09:47:35 +000099}
100
Jan Eilerse339bf62020-11-10 18:43:23 +0000101} // namespace armnnDelegate