• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
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 
10 #include <doctest/doctest.h>
11 
12 namespace 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
17 template <typename T>
FillInput(std::unique_ptr<tflite::Interpreter> & interpreter,int inputIndex,std::vector<T> & inputValues)18 void 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 
28 /// 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
31 void CompareData(std::vector<bool>& tensor1, bool tensor2[], size_t tensorSize);
32 void 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
35 void CompareData(float tensor1[], float tensor2[], size_t tensorSize);
36 
37 /// Can be used to compare int8_t data coming from a tflite interpreter with a tolerance of 1
38 void CompareData(int8_t tensor1[], int8_t tensor2[], size_t tensorSize);
39 
40 /// Can be used to compare uint8_t data coming from a tflite interpreter with a tolerance of 1
41 void CompareData(uint8_t tensor1[], uint8_t tensor2[], size_t tensorSize);
42 
43 /// Can be used to compare int16_t data coming from a tflite interpreter with a tolerance of 1
44 void CompareData(int16_t tensor1[], int16_t tensor2[], size_t tensorSize);
45 
46 
47 /// 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
50 template <typename T>
CompareOutputData(std::unique_ptr<tflite::Interpreter> & tfLiteInterpreter,std::unique_ptr<tflite::Interpreter> & armnnDelegateInterpreter,std::vector<int32_t> & expectedOutputShape,std::vector<T> & expectedOutputValues)51 void 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);
58     auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateOutputId);
59     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 
70     armnnDelegate::CompareData(expectedOutputValues.data(), armnnDelegateOutputData    , expectedOutputValues.size());
71     armnnDelegate::CompareData(tfLiteDelegateOutputData   , expectedOutputValues.data(), expectedOutputValues.size());
72     armnnDelegate::CompareData(tfLiteDelegateOutputData   , armnnDelegateOutputData    , expectedOutputValues.size());
73 }
74 
75 } // namespace armnnDelegate
76