blob: b165920762d346abf06167594c63f0e425aa98b6 [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
8#include <tensorflow/lite/interpreter.h>
9
Matthew Sloyan91c41712020-11-13 09:47:35 +000010#include <doctest/doctest.h>
11
Jan Eilerse339bf62020-11-10 18:43:23 +000012namespace armnnDelegate
13{
14
15/// Can be used to assign input data from a vector to a model input.
16/// Example usage can be found in ResizeTesthelper.hpp
17template <typename T>
18void FillInput(std::unique_ptr<tflite::Interpreter>& interpreter, int inputIndex, std::vector<T>& inputValues)
19{
20 auto tfLiteDelegateInputId = interpreter->inputs()[inputIndex];
21 auto tfLiteDelageInputData = interpreter->typed_tensor<T>(tfLiteDelegateInputId);
22 for (unsigned int i = 0; i < inputValues.size(); ++i)
23 {
24 tfLiteDelageInputData[i] = inputValues[i];
25 }
26}
27
Jan Eilersfe73b042020-11-18 10:36:46 +000028/// Can be used to compare bool data coming from a tflite interpreter
29/// Boolean types get converted to a bit representation in a vector. vector.data() returns a void pointer
30/// instead of a pointer to bool. Therefore a special function to compare to vector of bool is required
31void CompareData(std::vector<bool>& tensor1, bool tensor2[], size_t tensorSize);
32void CompareData(bool tensor1[], bool tensor2[], size_t tensorSize);
33
34/// 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 +000035void CompareData(float tensor1[], float tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000036
37/// 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 +000038void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000039
40/// 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 +000041void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize);
Jan Eilersfe73b042020-11-18 10:36:46 +000042
43/// 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 +000044void CompareData(int16_t tensor1[], int16_t tensor2[], size_t tensorSize);
45
46
Jan Eilersfe73b042020-11-18 10:36:46 +000047/// Can be used to compare the output tensor shape and values
48/// from armnnDelegateInterpreter and tfLiteInterpreter.
49/// Example usage can be found in ControlTestHelper.hpp
Matthew Sloyan91c41712020-11-13 09:47:35 +000050template <typename T>
51void CompareOutputData(std::unique_ptr<tflite::Interpreter>& tfLiteInterpreter,
52 std::unique_ptr<tflite::Interpreter>& armnnDelegateInterpreter,
53 std::vector<int32_t>& expectedOutputShape,
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000054 std::vector<T>& expectedOutputValues,
55 unsigned int outputIndex = 0)
Matthew Sloyan91c41712020-11-13 09:47:35 +000056{
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000057 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex];
Matthew Sloyan91c41712020-11-13 09:47:35 +000058 auto tfLiteDelegateOutputTensor = tfLiteInterpreter->tensor(tfLiteDelegateOutputId);
Jan Eilers3812fbc2020-11-17 19:06:35 +000059 auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateOutputId);
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000060 auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[outputIndex];
Matthew Sloyan91c41712020-11-13 09:47:35 +000061 auto armnnDelegateOutputTensor = armnnDelegateInterpreter->tensor(armnnDelegateOutputId);
62 auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateOutputId);
63
Narumol Prangnawarat958024b2020-12-17 12:17:58 +000064 CHECK(expectedOutputShape.size() == tfLiteDelegateOutputTensor->dims->size);
65 CHECK(expectedOutputShape.size() == armnnDelegateOutputTensor->dims->size);
66
Matthew Sloyan91c41712020-11-13 09:47:35 +000067 for (size_t i = 0; i < expectedOutputShape.size(); i++)
68 {
69 CHECK(expectedOutputShape[i] == armnnDelegateOutputTensor->dims->data[i]);
70 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]);
71 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == armnnDelegateOutputTensor->dims->data[i]);
72 }
73
Jan Eilers3812fbc2020-11-17 19:06:35 +000074 armnnDelegate::CompareData(expectedOutputValues.data(), armnnDelegateOutputData , expectedOutputValues.size());
75 armnnDelegate::CompareData(tfLiteDelegateOutputData , expectedOutputValues.data(), expectedOutputValues.size());
76 armnnDelegate::CompareData(tfLiteDelegateOutputData , armnnDelegateOutputData , expectedOutputValues.size());
Matthew Sloyan91c41712020-11-13 09:47:35 +000077}
78
Jan Eilerse339bf62020-11-10 18:43:23 +000079} // namespace armnnDelegate