blob: ad7600d27eeeae9d524ba905c1ada0c3c7b75490 [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
Jan Eilersfe73b042020-11-18 10:36:46 +000033/// Can be used to compare bool data coming from a tflite interpreter
34/// Boolean types get converted to a bit representation in a vector. vector.data() returns a void pointer
35/// instead of a pointer to bool. Therefore a special function to compare to vector of bool is required
36void CompareData(std::vector<bool>& tensor1, bool tensor2[], size_t tensorSize);
37void CompareData(bool tensor1[], bool tensor2[], size_t tensorSize);
38
39/// 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 +000040void CompareData(float tensor1[], float tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000041
42/// 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 +000043void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000044
45/// 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 +000046void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000047
48/// 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 +000049void CompareData(int16_t tensor1[], int16_t tensor2[], size_t tensorSize);
50
Narumol Prangnawarat4cf0fe32020-12-18 16:13:06 +000051/// Can be used to compare Half (Float16) data with a tolerance of limit_of_float*100
52void CompareData(Half tensor1[], Half tensor2[], size_t tensorSize);
53
54/// Can be used to compare TfLiteFloat16 data coming from a tflite interpreter
55void CompareData(TfLiteFloat16 tensor1[], TfLiteFloat16 tensor2[], size_t tensorSize);
56
57/// Can be used to compare Half (Float16) data and TfLiteFloat16 data coming from a tflite interpreter
58void CompareData(TfLiteFloat16 tensor1[], Half tensor2[], size_t tensorSize);
Jan Eilers3812fbc2020-11-17 19:06:35 +000059
Jan Eilersfe73b042020-11-18 10:36:46 +000060/// Can be used to compare the output tensor shape and values
61/// from armnnDelegateInterpreter and tfLiteInterpreter.
62/// Example usage can be found in ControlTestHelper.hpp
Matthew Sloyan91c41712020-11-13 09:47:35 +000063template <typename T>
64void CompareOutputData(std::unique_ptr<tflite::Interpreter>& tfLiteInterpreter,
65 std::unique_ptr<tflite::Interpreter>& armnnDelegateInterpreter,
66 std::vector<int32_t>& expectedOutputShape,
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000067 std::vector<T>& expectedOutputValues,
68 unsigned int outputIndex = 0)
Matthew Sloyan91c41712020-11-13 09:47:35 +000069{
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000070 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex];
Matthew Sloyan91c41712020-11-13 09:47:35 +000071 auto tfLiteDelegateOutputTensor = tfLiteInterpreter->tensor(tfLiteDelegateOutputId);
Jan Eilers3812fbc2020-11-17 19:06:35 +000072 auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateOutputId);
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000073 auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[outputIndex];
Matthew Sloyan91c41712020-11-13 09:47:35 +000074 auto armnnDelegateOutputTensor = armnnDelegateInterpreter->tensor(armnnDelegateOutputId);
75 auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateOutputId);
76
Narumol Prangnawarat958024b2020-12-17 12:17:58 +000077 CHECK(expectedOutputShape.size() == tfLiteDelegateOutputTensor->dims->size);
78 CHECK(expectedOutputShape.size() == armnnDelegateOutputTensor->dims->size);
79
Matthew Sloyan91c41712020-11-13 09:47:35 +000080 for (size_t i = 0; i < expectedOutputShape.size(); i++)
81 {
82 CHECK(expectedOutputShape[i] == armnnDelegateOutputTensor->dims->data[i]);
83 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]);
84 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == armnnDelegateOutputTensor->dims->data[i]);
85 }
86
Jan Eilers3812fbc2020-11-17 19:06:35 +000087 armnnDelegate::CompareData(expectedOutputValues.data(), armnnDelegateOutputData , expectedOutputValues.size());
88 armnnDelegate::CompareData(tfLiteDelegateOutputData , expectedOutputValues.data(), expectedOutputValues.size());
89 armnnDelegate::CompareData(tfLiteDelegateOutputData , armnnDelegateOutputData , expectedOutputValues.size());
Matthew Sloyan91c41712020-11-13 09:47:35 +000090}
91
Jan Eilerse339bf62020-11-10 18:43:23 +000092} // namespace armnnDelegate