blob: f8a6f8b4ea46fdbde7b75e7294ce210ba1b3b28c [file] [log] [blame]
Matthew Sloyan0d35a932020-11-09 12:25:05 +00001//
Colm Donelan7bcae3c2024-01-22 10:07:14 +00002// Copyright © 2020, 2023-2024 Arm Ltd and Contributors. All rights reserved.
Matthew Sloyan0d35a932020-11-09 12:25:05 +00003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Matthew Sloyanebe392d2023-03-30 10:12:08 +01008#include "TestUtils.hpp"
9
Matthew Sloyan0d35a932020-11-09 12:25:05 +000010#include <armnn_delegate.hpp>
Matthew Sloyanebe392d2023-03-30 10:12:08 +010011#include <DelegateTestInterpreter.hpp>
Matthew Sloyan0d35a932020-11-09 12:25:05 +000012
Matthew Sloyan0d35a932020-11-09 12:25:05 +000013#include <tensorflow/lite/version.h>
14
Matthew Sloyan0d35a932020-11-09 12:25:05 +000015namespace
16{
17
18std::vector<char> CreateQuantizationTfLiteModel(tflite::BuiltinOperator quantizationOperatorCode,
19 tflite::TensorType inputTensorType,
20 tflite::TensorType outputTensorType,
21 const std::vector <int32_t>& inputTensorShape,
22 const std::vector <int32_t>& outputTensorShape,
23 float quantScale = 1.0f,
24 int quantOffset = 0)
25{
26 using namespace tflite;
27 flatbuffers::FlatBufferBuilder flatBufferBuilder;
28
29 std::vector<flatbuffers::Offset<tflite::Buffer>> buffers;
Ryan OShea238ecd92023-03-07 11:44:23 +000030 buffers.push_back(CreateBuffer(flatBufferBuilder));
31 buffers.push_back(CreateBuffer(flatBufferBuilder));
32 buffers.push_back(CreateBuffer(flatBufferBuilder));
33
Matthew Sloyan0d35a932020-11-09 12:25:05 +000034
35 auto quantizationParameters =
36 CreateQuantizationParameters(flatBufferBuilder,
37 0,
38 0,
39 flatBufferBuilder.CreateVector<float>({ quantScale }),
40 flatBufferBuilder.CreateVector<int64_t>({ quantOffset }),
41 QuantizationDetails_CustomQuantization);
42
43 std::array<flatbuffers::Offset<Tensor>, 2> tensors;
44 tensors[0] = CreateTensor(flatBufferBuilder,
45 flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(),
46 inputTensorShape.size()),
47 inputTensorType,
Ryan OShea238ecd92023-03-07 11:44:23 +000048 1,
Matthew Sloyan0d35a932020-11-09 12:25:05 +000049 flatBufferBuilder.CreateString("input"),
50 quantizationParameters);
51 tensors[1] = CreateTensor(flatBufferBuilder,
52 flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(),
53 outputTensorShape.size()),
54 outputTensorType,
Ryan OShea238ecd92023-03-07 11:44:23 +000055 2,
Matthew Sloyan0d35a932020-11-09 12:25:05 +000056 flatBufferBuilder.CreateString("output"),
57 quantizationParameters);
58
59 // create operator
60 tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_NONE;
61 flatbuffers::Offset<void> operatorBuiltinOptions = 0;
62 switch (quantizationOperatorCode)
63 {
64 case BuiltinOperator_QUANTIZE:
65 {
66 operatorBuiltinOptionsType = BuiltinOptions_QuantizeOptions;
67 operatorBuiltinOptions = CreateQuantizeOptions(flatBufferBuilder).Union();
68 break;
69 }
70 case BuiltinOperator_DEQUANTIZE:
71 {
72 operatorBuiltinOptionsType = BuiltinOptions_DequantizeOptions;
73 operatorBuiltinOptions = CreateDequantizeOptions(flatBufferBuilder).Union();
74 break;
75 }
76 default:
77 break;
78 }
79
Keith Davis892fafe2020-11-26 17:40:35 +000080 const std::vector<int32_t> operatorInputs{0};
81 const std::vector<int32_t> operatorOutputs{1};
Matthew Sloyan0d35a932020-11-09 12:25:05 +000082 flatbuffers::Offset <Operator> quantizationOperator =
83 CreateOperator(flatBufferBuilder,
84 0,
85 flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()),
86 flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()),
87 operatorBuiltinOptionsType,
88 operatorBuiltinOptions);
89
Keith Davis892fafe2020-11-26 17:40:35 +000090 const std::vector<int> subgraphInputs{0};
91 const std::vector<int> subgraphOutputs{1};
Matthew Sloyan0d35a932020-11-09 12:25:05 +000092 flatbuffers::Offset <SubGraph> subgraph =
93 CreateSubGraph(flatBufferBuilder,
94 flatBufferBuilder.CreateVector(tensors.data(), tensors.size()),
95 flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()),
96 flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()),
97 flatBufferBuilder.CreateVector(&quantizationOperator, 1));
98
99 flatbuffers::Offset <flatbuffers::String> modelDescription =
100 flatBufferBuilder.CreateString("ArmnnDelegate: Quantization Operator Model");
101 flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, quantizationOperatorCode);
102
103 flatbuffers::Offset <Model> flatbufferModel =
104 CreateModel(flatBufferBuilder,
105 TFLITE_SCHEMA_VERSION,
106 flatBufferBuilder.CreateVector(&operatorCode, 1),
107 flatBufferBuilder.CreateVector(&subgraph, 1),
108 modelDescription,
109 flatBufferBuilder.CreateVector(buffers.data(), buffers.size()));
110
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100111 flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER);
Matthew Sloyan0d35a932020-11-09 12:25:05 +0000112
113 return std::vector<char>(flatBufferBuilder.GetBufferPointer(),
114 flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize());
115}
116
117template <typename InputT, typename OutputT>
118void QuantizationTest(tflite::BuiltinOperator quantizeOperatorCode,
119 tflite::TensorType inputTensorType,
120 tflite::TensorType outputTensorType,
Matthew Sloyan0d35a932020-11-09 12:25:05 +0000121 std::vector<int32_t>& inputShape,
122 std::vector<int32_t>& outputShape,
123 std::vector<InputT>& inputValues,
124 std::vector<OutputT>& expectedOutputValues,
Colm Donelan7bcae3c2024-01-22 10:07:14 +0000125 const std::vector<armnn::BackendId>& backends = {},
Matthew Sloyan0d35a932020-11-09 12:25:05 +0000126 float quantScale = 1.0f,
127 int quantOffset = 0)
128{
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100129 using namespace delegateTestInterpreter;
Matthew Sloyan0d35a932020-11-09 12:25:05 +0000130 std::vector<char> modelBuffer = CreateQuantizationTfLiteModel(quantizeOperatorCode,
131 inputTensorType,
132 outputTensorType,
133 inputShape,
134 outputShape,
135 quantScale,
136 quantOffset);
137
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100138 // Setup interpreter with just TFLite Runtime.
139 auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer);
140 CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk);
141 CHECK(tfLiteInterpreter.FillInputTensor(inputValues, 0) == kTfLiteOk);
142 CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk);
143 std::vector<OutputT> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<OutputT>(0);
144 std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0);
Matthew Sloyan0d35a932020-11-09 12:25:05 +0000145
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100146 // Setup interpreter with Arm NN Delegate applied.
Colm Donelan7bcae3c2024-01-22 10:07:14 +0000147 auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, CaptureAvailableBackends(backends));
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100148 CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk);
149 CHECK(armnnInterpreter.FillInputTensor(inputValues, 0) == kTfLiteOk);
150 CHECK(armnnInterpreter.Invoke() == kTfLiteOk);
151 std::vector<OutputT> armnnOutputValues = armnnInterpreter.GetOutputResult<OutputT>(0);
152 std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(0);
Matthew Sloyan0d35a932020-11-09 12:25:05 +0000153
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100154 armnnDelegate::CompareOutputData<OutputT>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues);
155 armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, outputShape);
Matthew Sloyan0d35a932020-11-09 12:25:05 +0000156
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100157 tfLiteInterpreter.Cleanup();
158 armnnInterpreter.Cleanup();
Matthew Sloyan0d35a932020-11-09 12:25:05 +0000159}
160
161} // anonymous namespace