• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2021, 2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "TestUtils.hpp"
9 
10 #include <armnn_delegate.hpp>
11 #include <DelegateTestInterpreter.hpp>
12 
13 #include <flatbuffers/flatbuffers.h>
14 #include <tensorflow/lite/kernels/register.h>
15 #include <tensorflow/lite/version.h>
16 
17 #include <schema_generated.h>
18 
19 #include <doctest/doctest.h>
20 
21 namespace
22 {
23 
CreateReduceTfLiteModel(tflite::BuiltinOperator reduceOperatorCode,tflite::TensorType tensorType,std::vector<int32_t> & input0TensorShape,std::vector<int32_t> & input1TensorShape,const std::vector<int32_t> & outputTensorShape,std::vector<int32_t> & axisData,const bool keepDims,float quantScale=1.0f,int quantOffset=0,bool kTfLiteNoQuantizationForQuantized=false)24 std::vector<char> CreateReduceTfLiteModel(tflite::BuiltinOperator reduceOperatorCode,
25                                           tflite::TensorType tensorType,
26                                           std::vector<int32_t>& input0TensorShape,
27                                           std::vector<int32_t>& input1TensorShape,
28                                           const std::vector <int32_t>& outputTensorShape,
29                                           std::vector<int32_t>& axisData,
30                                           const bool keepDims,
31                                           float quantScale = 1.0f,
32                                           int quantOffset  = 0,
33                                           bool kTfLiteNoQuantizationForQuantized = false)
34 {
35     using namespace tflite;
36     flatbuffers::FlatBufferBuilder flatBufferBuilder;
37 
38     flatbuffers::Offset<tflite::Buffer> buffers[4] = {
39             CreateBuffer(flatBufferBuilder),
40             CreateBuffer(flatBufferBuilder),
41             CreateBuffer(flatBufferBuilder,
42                          flatBufferBuilder.CreateVector(reinterpret_cast<const uint8_t*>(axisData.data()),
43                                                         sizeof(int32_t) * axisData.size())),
44             CreateBuffer(flatBufferBuilder)
45     };
46 
47     flatbuffers::Offset<tflite::QuantizationParameters> quantizationParametersAxis
48             = CreateQuantizationParameters(flatBufferBuilder);
49 
50     flatbuffers::Offset<tflite::QuantizationParameters> quantizationParameters;
51 
52     if (kTfLiteNoQuantizationForQuantized)
53     {
54         if ((quantScale == 1 || quantScale == 0) && quantOffset == 0)
55         {
56             // Creates quantization parameter with quantization.type = kTfLiteNoQuantization
57             quantizationParameters = CreateQuantizationParameters(flatBufferBuilder);
58         }
59         else
60         {
61             // Creates quantization parameter with quantization.type != kTfLiteNoQuantization
62             quantizationParameters = CreateQuantizationParameters(
63                     flatBufferBuilder,
64                     0,
65                     0,
66                     flatBufferBuilder.CreateVector<float>({quantScale}),
67                     flatBufferBuilder.CreateVector<int64_t>({quantOffset}));
68         }
69     }
70     else
71     {
72         quantizationParameters = CreateQuantizationParameters(
73                 flatBufferBuilder,
74                 0,
75                 0,
76                 flatBufferBuilder.CreateVector<float>({quantScale}),
77                 flatBufferBuilder.CreateVector<int64_t>({quantOffset}));
78     }
79 
80     std::array<flatbuffers::Offset<Tensor>, 3> tensors;
81     tensors[0] = CreateTensor(flatBufferBuilder,
82                               flatBufferBuilder.CreateVector<int32_t>(input0TensorShape.data(),
83                                                                       input0TensorShape.size()),
84                               tensorType,
85                               1,
86                               flatBufferBuilder.CreateString("input"),
87                               quantizationParameters);
88 
89     tensors[1] = CreateTensor(flatBufferBuilder,
90                               flatBufferBuilder.CreateVector<int32_t>(input1TensorShape.data(),
91                                                                       input1TensorShape.size()),
92                               ::tflite::TensorType_INT32,
93                               2,
94                               flatBufferBuilder.CreateString("axis"),
95                               quantizationParametersAxis);
96 
97     // Create output tensor
98     tensors[2] = CreateTensor(flatBufferBuilder,
99                               flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(),
100                                                                       outputTensorShape.size()),
101                               tensorType,
102                               3,
103                               flatBufferBuilder.CreateString("output"),
104                               quantizationParameters);
105 
106     // Create operator. Reduce operations MIN, MAX, SUM, MEAN, PROD uses ReducerOptions.
107     tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_ReducerOptions;
108     flatbuffers::Offset<void> operatorBuiltinOptions = CreateReducerOptions(flatBufferBuilder, keepDims).Union();
109 
110     const std::vector<int> operatorInputs{ {0, 1} };
111     const std::vector<int> operatorOutputs{ 2 };
112     flatbuffers::Offset <Operator> reduceOperator =
113             CreateOperator(flatBufferBuilder,
114                            0,
115                            flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()),
116                            flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()),
117                            operatorBuiltinOptionsType,
118                            operatorBuiltinOptions);
119 
120     const std::vector<int> subgraphInputs{ {0, 1} };
121     const std::vector<int> subgraphOutputs{ 2 };
122     flatbuffers::Offset <SubGraph> subgraph =
123             CreateSubGraph(flatBufferBuilder,
124                            flatBufferBuilder.CreateVector(tensors.data(), tensors.size()),
125                            flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()),
126                            flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()),
127                            flatBufferBuilder.CreateVector(&reduceOperator, 1));
128 
129     flatbuffers::Offset <flatbuffers::String> modelDescription =
130             flatBufferBuilder.CreateString("ArmnnDelegate: Reduce Operator Model");
131     flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, reduceOperatorCode);
132 
133     flatbuffers::Offset <Model> flatbufferModel =
134             CreateModel(flatBufferBuilder,
135                         TFLITE_SCHEMA_VERSION,
136                         flatBufferBuilder.CreateVector(&operatorCode, 1),
137                         flatBufferBuilder.CreateVector(&subgraph, 1),
138                         modelDescription,
139                         flatBufferBuilder.CreateVector(buffers, 4));
140 
141     flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER);
142 
143     return std::vector<char>(flatBufferBuilder.GetBufferPointer(),
144                              flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize());
145 }
146 
147 template <typename T>
ReduceTest(tflite::BuiltinOperator reduceOperatorCode,tflite::TensorType tensorType,std::vector<armnn::BackendId> & backends,std::vector<int32_t> & input0Shape,std::vector<int32_t> & input1Shape,std::vector<int32_t> & expectedOutputShape,std::vector<T> & input0Values,std::vector<int32_t> & input1Values,std::vector<T> & expectedOutputValues,const bool keepDims,float quantScale=1.0f,int quantOffset=0)148 void ReduceTest(tflite::BuiltinOperator reduceOperatorCode,
149                 tflite::TensorType tensorType,
150                 std::vector<armnn::BackendId>& backends,
151                 std::vector<int32_t>& input0Shape,
152                 std::vector<int32_t>& input1Shape,
153                 std::vector<int32_t>& expectedOutputShape,
154                 std::vector<T>& input0Values,
155                 std::vector<int32_t>& input1Values,
156                 std::vector<T>& expectedOutputValues,
157                 const bool keepDims,
158                 float quantScale = 1.0f,
159                 int quantOffset  = 0)
160 {
161     using namespace delegateTestInterpreter;
162     std::vector<char> modelBufferArmNN = CreateReduceTfLiteModel(reduceOperatorCode,
163                                                                  tensorType,
164                                                                  input0Shape,
165                                                                  input1Shape,
166                                                                  expectedOutputShape,
167                                                                  input1Values,
168                                                                  keepDims,
169                                                                  quantScale,
170                                                                  quantOffset,
171                                                                  false);
172     std::vector<char> modelBufferTFLite = CreateReduceTfLiteModel(reduceOperatorCode,
173                                                                   tensorType,
174                                                                   input0Shape,
175                                                                   input1Shape,
176                                                                   expectedOutputShape,
177                                                                   input1Values,
178                                                                   keepDims,
179                                                                   quantScale,
180                                                                   quantOffset,
181                                                                   true);
182 
183     // Setup interpreter with just TFLite Runtime.
184     auto tfLiteInterpreter = DelegateTestInterpreter(modelBufferTFLite);
185     CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk);
186     CHECK(tfLiteInterpreter.FillInputTensor<T>(input0Values, 0) == kTfLiteOk);
187     CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk);
188     std::vector<T>       tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<T>(0);
189     std::vector<int32_t> tfLiteOutputShape  = tfLiteInterpreter.GetOutputShape(0);
190 
191     // Setup interpreter with Arm NN Delegate applied.
192     auto armnnInterpreter = DelegateTestInterpreter(modelBufferArmNN, backends);
193     CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk);
194     CHECK(armnnInterpreter.FillInputTensor<T>(input0Values, 0) == kTfLiteOk);
195     CHECK(armnnInterpreter.Invoke() == kTfLiteOk);
196     std::vector<T>       armnnOutputValues = armnnInterpreter.GetOutputResult<T>(0);
197     std::vector<int32_t> armnnOutputShape  = armnnInterpreter.GetOutputShape(0);
198 
199     armnnDelegate::CompareOutputData<T>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues);
200     armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, expectedOutputShape);
201 
202     tfLiteInterpreter.Cleanup();
203     armnnInterpreter.Cleanup();
204 }
205 
206 } // anonymous namespace