blob: 57ae3ce6fea68b6237ce5ba22441bbf98dd8bc1e [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 Eilersb33f28b2020-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 Eilersb33f28b2020-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 Eilersb33f28b2020-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 Eilersb33f28b2020-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 Eilersb33f28b2020-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,
54 std::vector<T>& expectedOutputValues)
55{
56 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[0];
57 auto tfLiteDelegateOutputTensor = tfLiteInterpreter->tensor(tfLiteDelegateOutputId);
Jan Eilers3812fbc2020-11-17 19:06:35 +000058 auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateOutputId);
Matthew Sloyan91c41712020-11-13 09:47:35 +000059 auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[0];
60 auto armnnDelegateOutputTensor = armnnDelegateInterpreter->tensor(armnnDelegateOutputId);
61 auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateOutputId);
62
63 for (size_t i = 0; i < expectedOutputShape.size(); i++)
64 {
65 CHECK(expectedOutputShape[i] == armnnDelegateOutputTensor->dims->data[i]);
66 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == expectedOutputShape[i]);
67 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == armnnDelegateOutputTensor->dims->data[i]);
68 }
69
Jan Eilers3812fbc2020-11-17 19:06:35 +000070 armnnDelegate::CompareData(expectedOutputValues.data(), armnnDelegateOutputData , expectedOutputValues.size());
71 armnnDelegate::CompareData(tfLiteDelegateOutputData , expectedOutputValues.data(), expectedOutputValues.size());
72 armnnDelegate::CompareData(tfLiteDelegateOutputData , armnnDelegateOutputData , expectedOutputValues.size());
Matthew Sloyan91c41712020-11-13 09:47:35 +000073}
74
Jan Eilerse339bf62020-11-10 18:43:23 +000075} // namespace armnnDelegate