blob: f6acdbc84f2915843c362a908ecc5bab7b84eb19 [file] [log] [blame]
Sadik Armagana2747482021-02-09 10:28:54 +00001//
Ryan OShea238ecd92023-03-07 11:44:23 +00002// Copyright © 2021, 2023 Arm Ltd and Contributors. All rights reserved.
Sadik Armagana2747482021-02-09 10:28:54 +00003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "TestUtils.hpp"
9
10#include <armnn_delegate.hpp>
Matthew Sloyanebe392d2023-03-30 10:12:08 +010011#include <DelegateTestInterpreter.hpp>
Sadik Armagana2747482021-02-09 10:28:54 +000012
13#include <flatbuffers/flatbuffers.h>
Sadik Armagana2747482021-02-09 10:28:54 +000014#include <tensorflow/lite/kernels/register.h>
Sadik Armagana2747482021-02-09 10:28:54 +000015#include <tensorflow/lite/version.h>
16
Matthew Sloyanebe392d2023-03-30 10:12:08 +010017#include <doctest/doctest.h>
Sadik Armagana2747482021-02-09 10:28:54 +000018
19namespace
20{
21
22std::vector<char> CreateReduceTfLiteModel(tflite::BuiltinOperator reduceOperatorCode,
Teresa Charlin4d85adf2022-10-27 11:37:29 +010023 tflite::TensorType tensorType,
24 std::vector<int32_t>& input0TensorShape,
25 std::vector<int32_t>& input1TensorShape,
26 const std::vector <int32_t>& outputTensorShape,
27 std::vector<int32_t>& axisData,
28 const bool keepDims,
29 float quantScale = 1.0f,
30 int quantOffset = 0,
31 bool kTfLiteNoQuantizationForQuantized = false)
Sadik Armagana2747482021-02-09 10:28:54 +000032{
33 using namespace tflite;
34 flatbuffers::FlatBufferBuilder flatBufferBuilder;
35
Ryan OShea238ecd92023-03-07 11:44:23 +000036 flatbuffers::Offset<tflite::Buffer> buffers[4] = {
37 CreateBuffer(flatBufferBuilder),
38 CreateBuffer(flatBufferBuilder),
39 CreateBuffer(flatBufferBuilder,
40 flatBufferBuilder.CreateVector(reinterpret_cast<const uint8_t*>(axisData.data()),
41 sizeof(int32_t) * axisData.size())),
42 CreateBuffer(flatBufferBuilder)
43 };
Sadik Armagana2747482021-02-09 10:28:54 +000044
Teresa Charlin4d85adf2022-10-27 11:37:29 +010045 flatbuffers::Offset<tflite::QuantizationParameters> quantizationParametersAxis
Ryan OShea238ecd92023-03-07 11:44:23 +000046 = CreateQuantizationParameters(flatBufferBuilder);
Teresa Charlin4d85adf2022-10-27 11:37:29 +010047
48 flatbuffers::Offset<tflite::QuantizationParameters> quantizationParameters;
49
Idriss Chaouch08ee1aa2023-09-13 13:53:37 +010050 if (kTfLiteNoQuantizationForQuantized && reduceOperatorCode == BuiltinOperator_REDUCE_PROD)
Teresa Charlin4d85adf2022-10-27 11:37:29 +010051 {
52 if ((quantScale == 1 || quantScale == 0) && quantOffset == 0)
53 {
54 // Creates quantization parameter with quantization.type = kTfLiteNoQuantization
55 quantizationParameters = CreateQuantizationParameters(flatBufferBuilder);
56 }
57 else
58 {
59 // Creates quantization parameter with quantization.type != kTfLiteNoQuantization
60 quantizationParameters = CreateQuantizationParameters(
61 flatBufferBuilder,
62 0,
63 0,
64 flatBufferBuilder.CreateVector<float>({quantScale}),
65 flatBufferBuilder.CreateVector<int64_t>({quantOffset}));
66 }
67 }
68 else
69 {
70 quantizationParameters = CreateQuantizationParameters(
71 flatBufferBuilder,
72 0,
73 0,
74 flatBufferBuilder.CreateVector<float>({quantScale}),
75 flatBufferBuilder.CreateVector<int64_t>({quantOffset}));
76 }
Sadik Armagana2747482021-02-09 10:28:54 +000077
78 std::array<flatbuffers::Offset<Tensor>, 3> tensors;
79 tensors[0] = CreateTensor(flatBufferBuilder,
80 flatBufferBuilder.CreateVector<int32_t>(input0TensorShape.data(),
81 input0TensorShape.size()),
82 tensorType,
Ryan OShea238ecd92023-03-07 11:44:23 +000083 1,
Sadik Armagana2747482021-02-09 10:28:54 +000084 flatBufferBuilder.CreateString("input"),
85 quantizationParameters);
86
87 tensors[1] = CreateTensor(flatBufferBuilder,
88 flatBufferBuilder.CreateVector<int32_t>(input1TensorShape.data(),
89 input1TensorShape.size()),
90 ::tflite::TensorType_INT32,
Ryan OShea238ecd92023-03-07 11:44:23 +000091 2,
Sadik Armagana2747482021-02-09 10:28:54 +000092 flatBufferBuilder.CreateString("axis"),
Teresa Charlin4d85adf2022-10-27 11:37:29 +010093 quantizationParametersAxis);
Sadik Armagana2747482021-02-09 10:28:54 +000094
95 // Create output tensor
96 tensors[2] = CreateTensor(flatBufferBuilder,
97 flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(),
98 outputTensorShape.size()),
99 tensorType,
Ryan OShea238ecd92023-03-07 11:44:23 +0000100 3,
Sadik Armagana2747482021-02-09 10:28:54 +0000101 flatBufferBuilder.CreateString("output"),
102 quantizationParameters);
103
Teresa Charlin4d85adf2022-10-27 11:37:29 +0100104 // Create operator. Reduce operations MIN, MAX, SUM, MEAN, PROD uses ReducerOptions.
Sadik Armagana2747482021-02-09 10:28:54 +0000105 tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_ReducerOptions;
106 flatbuffers::Offset<void> operatorBuiltinOptions = CreateReducerOptions(flatBufferBuilder, keepDims).Union();
107
108 const std::vector<int> operatorInputs{ {0, 1} };
109 const std::vector<int> operatorOutputs{ 2 };
110 flatbuffers::Offset <Operator> reduceOperator =
111 CreateOperator(flatBufferBuilder,
112 0,
113 flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()),
114 flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()),
115 operatorBuiltinOptionsType,
116 operatorBuiltinOptions);
117
118 const std::vector<int> subgraphInputs{ {0, 1} };
119 const std::vector<int> subgraphOutputs{ 2 };
120 flatbuffers::Offset <SubGraph> subgraph =
121 CreateSubGraph(flatBufferBuilder,
122 flatBufferBuilder.CreateVector(tensors.data(), tensors.size()),
123 flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()),
124 flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()),
125 flatBufferBuilder.CreateVector(&reduceOperator, 1));
126
127 flatbuffers::Offset <flatbuffers::String> modelDescription =
128 flatBufferBuilder.CreateString("ArmnnDelegate: Reduce Operator Model");
129 flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, reduceOperatorCode);
130
131 flatbuffers::Offset <Model> flatbufferModel =
132 CreateModel(flatBufferBuilder,
133 TFLITE_SCHEMA_VERSION,
134 flatBufferBuilder.CreateVector(&operatorCode, 1),
135 flatBufferBuilder.CreateVector(&subgraph, 1),
136 modelDescription,
Ryan OShea238ecd92023-03-07 11:44:23 +0000137 flatBufferBuilder.CreateVector(buffers, 4));
Sadik Armagana2747482021-02-09 10:28:54 +0000138
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100139 flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER);
Sadik Armagana2747482021-02-09 10:28:54 +0000140
141 return std::vector<char>(flatBufferBuilder.GetBufferPointer(),
142 flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize());
143}
144
145template <typename T>
146void ReduceTest(tflite::BuiltinOperator reduceOperatorCode,
147 tflite::TensorType tensorType,
148 std::vector<armnn::BackendId>& backends,
149 std::vector<int32_t>& input0Shape,
150 std::vector<int32_t>& input1Shape,
151 std::vector<int32_t>& expectedOutputShape,
152 std::vector<T>& input0Values,
153 std::vector<int32_t>& input1Values,
154 std::vector<T>& expectedOutputValues,
155 const bool keepDims,
156 float quantScale = 1.0f,
157 int quantOffset = 0)
158{
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100159 using namespace delegateTestInterpreter;
Teresa Charlin4d85adf2022-10-27 11:37:29 +0100160 std::vector<char> modelBufferArmNN = CreateReduceTfLiteModel(reduceOperatorCode,
161 tensorType,
162 input0Shape,
163 input1Shape,
164 expectedOutputShape,
165 input1Values,
166 keepDims,
167 quantScale,
168 quantOffset,
169 false);
170 std::vector<char> modelBufferTFLite = CreateReduceTfLiteModel(reduceOperatorCode,
171 tensorType,
172 input0Shape,
173 input1Shape,
174 expectedOutputShape,
175 input1Values,
176 keepDims,
177 quantScale,
178 quantOffset,
179 true);
Sadik Armagana2747482021-02-09 10:28:54 +0000180
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100181 // Setup interpreter with just TFLite Runtime.
182 auto tfLiteInterpreter = DelegateTestInterpreter(modelBufferTFLite);
183 CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk);
184 CHECK(tfLiteInterpreter.FillInputTensor<T>(input0Values, 0) == kTfLiteOk);
185 CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk);
186 std::vector<T> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<T>(0);
187 std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0);
Sadik Armagana2747482021-02-09 10:28:54 +0000188
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100189 // Setup interpreter with Arm NN Delegate applied.
190 auto armnnInterpreter = DelegateTestInterpreter(modelBufferArmNN, backends);
191 CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk);
192 CHECK(armnnInterpreter.FillInputTensor<T>(input0Values, 0) == kTfLiteOk);
193 CHECK(armnnInterpreter.Invoke() == kTfLiteOk);
194 std::vector<T> armnnOutputValues = armnnInterpreter.GetOutputResult<T>(0);
195 std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(0);
Sadik Armagana2747482021-02-09 10:28:54 +0000196
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100197 armnnDelegate::CompareOutputData<T>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues);
198 armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, expectedOutputShape);
Sadik Armagana2747482021-02-09 10:28:54 +0000199
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100200 tfLiteInterpreter.Cleanup();
201 armnnInterpreter.Cleanup();
Sadik Armagana2747482021-02-09 10:28:54 +0000202}
203
204} // anonymous namespace