blob: 8a2756f4c5efdd71bb8c73d81ee9d135e52c0602 [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
45/// 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 +000046void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000047
48/// 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 +000049void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000050
51/// 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 +000052void CompareData(int16_t tensor1[], int16_t tensor2[], size_t tensorSize);
53
Sadik Armagan29b49cf2021-02-22 18:09:07 +000054/// Can be used to compare int32_t data coming from a tflite interpreter with a tolerance of 1
55void CompareData(int32_t tensor1[], int32_t tensor2[], size_t tensorSize);
56
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000057/// Can be used to compare Half (Float16) data with a tolerance of limit_of_float*100
58void CompareData(Half tensor1[], Half tensor2[], size_t tensorSize);
59
60/// Can be used to compare TfLiteFloat16 data coming from a tflite interpreter
61void CompareData(TfLiteFloat16 tensor1[], TfLiteFloat16 tensor2[], size_t tensorSize);
62
63/// Can be used to compare Half (Float16) data and TfLiteFloat16 data coming from a tflite interpreter
64void CompareData(TfLiteFloat16 tensor1[], Half tensor2[], size_t tensorSize);
Jan Eilers3812fbc2020-11-17 19:06:35 +000065
Jan Eilersfe73b042020-11-18 10:36:46 +000066/// Can be used to compare the output tensor shape and values
67/// from armnnDelegateInterpreter and tfLiteInterpreter.
68/// Example usage can be found in ControlTestHelper.hpp
Matthew Sloyan91c41712020-11-13 09:47:35 +000069template <typename T>
70void CompareOutputData(std::unique_ptr<tflite::Interpreter>& tfLiteInterpreter,
71 std::unique_ptr<tflite::Interpreter>& armnnDelegateInterpreter,
72 std::vector<int32_t>& expectedOutputShape,
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000073 std::vector<T>& expectedOutputValues,
74 unsigned int outputIndex = 0)
Matthew Sloyan91c41712020-11-13 09:47:35 +000075{
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000076 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex];
Matthew Sloyan91c41712020-11-13 09:47:35 +000077 auto tfLiteDelegateOutputTensor = tfLiteInterpreter->tensor(tfLiteDelegateOutputId);
Jan Eilers3812fbc2020-11-17 19:06:35 +000078 auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateOutputId);
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000079 auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[outputIndex];
Matthew Sloyan91c41712020-11-13 09:47:35 +000080 auto armnnDelegateOutputTensor = armnnDelegateInterpreter->tensor(armnnDelegateOutputId);
81 auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateOutputId);
82
Narumol Prangnawarat958024b2020-12-17 12:17:58 +000083 CHECK(expectedOutputShape.size() == tfLiteDelegateOutputTensor->dims->size);
84 CHECK(expectedOutputShape.size() == armnnDelegateOutputTensor->dims->size);
85
Matthew Sloyan91c41712020-11-13 09:47:35 +000086 for (size_t i = 0; i < expectedOutputShape.size(); i++)
87 {
88 CHECK(expectedOutputShape[i] == armnnDelegateOutputTensor->dims->data[i]);
89 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]);
90 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == armnnDelegateOutputTensor->dims->data[i]);
91 }
92
Jan Eilers3812fbc2020-11-17 19:06:35 +000093 armnnDelegate::CompareData(expectedOutputValues.data(), armnnDelegateOutputData , expectedOutputValues.size());
94 armnnDelegate::CompareData(tfLiteDelegateOutputData , expectedOutputValues.data(), expectedOutputValues.size());
95 armnnDelegate::CompareData(tfLiteDelegateOutputData , armnnDelegateOutputData , expectedOutputValues.size());
Matthew Sloyan91c41712020-11-13 09:47:35 +000096}
97
Jan Eilerse339bf62020-11-10 18:43:23 +000098} // namespace armnnDelegate