Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 1 | // |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame] | 2 | // Copyright © 2020, 2023-2024 Arm Ltd and Contributors. All rights reserved. |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "TestUtils.hpp" |
| 9 | |
| 10 | #include <armnn_delegate.hpp> |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 11 | #include <DelegateTestInterpreter.hpp> |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 12 | |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 13 | #include <tensorflow/lite/version.h> |
| 14 | |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 15 | namespace |
| 16 | { |
| 17 | |
| 18 | template <typename T> |
| 19 | std::vector<char> CreatePadTfLiteModel( |
| 20 | tflite::BuiltinOperator padOperatorCode, |
| 21 | tflite::TensorType tensorType, |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 22 | tflite::MirrorPadMode paddingMode, |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 23 | const std::vector<int32_t>& inputTensorShape, |
| 24 | const std::vector<int32_t>& paddingTensorShape, |
| 25 | const std::vector<int32_t>& outputTensorShape, |
| 26 | const std::vector<int32_t>& paddingDim, |
| 27 | const std::vector<T> paddingValue, |
| 28 | float quantScale = 1.0f, |
| 29 | int quantOffset = 0) |
| 30 | { |
| 31 | using namespace tflite; |
| 32 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 33 | |
| 34 | auto quantizationParameters = |
| 35 | CreateQuantizationParameters(flatBufferBuilder, |
| 36 | 0, |
| 37 | 0, |
| 38 | flatBufferBuilder.CreateVector<float>({ quantScale }), |
| 39 | flatBufferBuilder.CreateVector<int64_t>({ quantOffset })); |
| 40 | |
| 41 | auto inputTensor = CreateTensor(flatBufferBuilder, |
| 42 | flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(), |
| 43 | inputTensorShape.size()), |
| 44 | tensorType, |
| 45 | 0, |
| 46 | flatBufferBuilder.CreateString("input"), |
| 47 | quantizationParameters); |
| 48 | |
| 49 | auto paddingTensor = CreateTensor(flatBufferBuilder, |
| 50 | flatBufferBuilder.CreateVector<int32_t>(paddingTensorShape.data(), |
| 51 | paddingTensorShape.size()), |
| 52 | tflite::TensorType_INT32, |
| 53 | 1, |
| 54 | flatBufferBuilder.CreateString("padding")); |
| 55 | |
| 56 | auto outputTensor = CreateTensor(flatBufferBuilder, |
| 57 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 58 | outputTensorShape.size()), |
| 59 | tensorType, |
| 60 | 2, |
| 61 | flatBufferBuilder.CreateString("output"), |
| 62 | quantizationParameters); |
| 63 | |
| 64 | std::vector<flatbuffers::Offset<Tensor>> tensors = { inputTensor, paddingTensor, outputTensor}; |
| 65 | |
| 66 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 67 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 68 | buffers.push_back( |
| 69 | CreateBuffer(flatBufferBuilder, |
| 70 | flatBufferBuilder.CreateVector(reinterpret_cast<const uint8_t*>(paddingDim.data()), |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 71 | sizeof(int32_t) * paddingDim.size()))); |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 72 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 73 | |
| 74 | std::vector<int32_t> operatorInputs; |
| 75 | std::vector<int> subgraphInputs; |
| 76 | |
| 77 | tflite::BuiltinOptions operatorBuiltinOptionsType = BuiltinOptions_PadOptions; |
| 78 | flatbuffers::Offset<void> operatorBuiltinOptions; |
| 79 | |
| 80 | if (padOperatorCode == tflite::BuiltinOperator_PAD) |
| 81 | { |
| 82 | operatorInputs = {{ 0, 1 }}; |
| 83 | subgraphInputs = {{ 0, 1 }}; |
| 84 | operatorBuiltinOptions = CreatePadOptions(flatBufferBuilder).Union(); |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 85 | } |
| 86 | else if(padOperatorCode == tflite::BuiltinOperator_MIRROR_PAD) |
| 87 | { |
| 88 | operatorInputs = {{ 0, 1 }}; |
| 89 | subgraphInputs = {{ 0, 1 }}; |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 90 | |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 91 | operatorBuiltinOptionsType = BuiltinOptions_MirrorPadOptions; |
| 92 | operatorBuiltinOptions = CreateMirrorPadOptions(flatBufferBuilder, paddingMode).Union(); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 93 | } |
| 94 | else if (padOperatorCode == tflite::BuiltinOperator_PADV2) |
| 95 | { |
| 96 | buffers.push_back( |
| 97 | CreateBuffer(flatBufferBuilder, |
| 98 | flatBufferBuilder.CreateVector(reinterpret_cast<const uint8_t*>(paddingValue.data()), |
| 99 | sizeof(T)))); |
| 100 | |
| 101 | const std::vector<int32_t> shape = { 1 }; |
| 102 | auto padValueTensor = CreateTensor(flatBufferBuilder, |
| 103 | flatBufferBuilder.CreateVector<int32_t>(shape.data(), |
| 104 | shape.size()), |
| 105 | tensorType, |
| 106 | 3, |
| 107 | flatBufferBuilder.CreateString("paddingValue"), |
| 108 | quantizationParameters); |
| 109 | |
| 110 | tensors.push_back(padValueTensor); |
| 111 | |
| 112 | operatorInputs = {{ 0, 1, 3 }}; |
| 113 | subgraphInputs = {{ 0, 1, 3 }}; |
| 114 | |
| 115 | operatorBuiltinOptionsType = BuiltinOptions_PadV2Options; |
| 116 | operatorBuiltinOptions = CreatePadV2Options(flatBufferBuilder).Union(); |
| 117 | } |
| 118 | |
| 119 | // create operator |
Keith Davis | bbc876c | 2021-01-27 13:12:03 +0000 | [diff] [blame] | 120 | const std::vector<int32_t> operatorOutputs{ 2 }; |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 121 | flatbuffers::Offset <Operator> paddingOperator = |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 122 | CreateOperator(flatBufferBuilder, |
| 123 | 0, |
| 124 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 125 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 126 | operatorBuiltinOptionsType, |
| 127 | operatorBuiltinOptions); |
| 128 | |
Keith Davis | bbc876c | 2021-01-27 13:12:03 +0000 | [diff] [blame] | 129 | const std::vector<int> subgraphOutputs{ 2 }; |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 130 | flatbuffers::Offset <SubGraph> subgraph = |
| 131 | CreateSubGraph(flatBufferBuilder, |
| 132 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 133 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 134 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 135 | flatBufferBuilder.CreateVector(&paddingOperator, 1)); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 136 | |
| 137 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 138 | flatBufferBuilder.CreateString("ArmnnDelegate: Pad Operator Model"); |
| 139 | flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, |
| 140 | padOperatorCode); |
| 141 | |
| 142 | flatbuffers::Offset <Model> flatbufferModel = |
| 143 | CreateModel(flatBufferBuilder, |
| 144 | TFLITE_SCHEMA_VERSION, |
| 145 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 146 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 147 | modelDescription, |
| 148 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 149 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 150 | flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 151 | |
| 152 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 153 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 154 | } |
| 155 | |
| 156 | template <typename T> |
| 157 | void PadTest(tflite::BuiltinOperator padOperatorCode, |
| 158 | tflite::TensorType tensorType, |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 159 | const std::vector<int32_t>& inputShape, |
| 160 | const std::vector<int32_t>& paddingShape, |
| 161 | std::vector<int32_t>& outputShape, |
| 162 | std::vector<T>& inputValues, |
| 163 | std::vector<int32_t>& paddingDim, |
| 164 | std::vector<T>& expectedOutputValues, |
| 165 | T paddingValue, |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame] | 166 | const std::vector<armnn::BackendId>& backends = {}, |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 167 | float quantScale = 1.0f, |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 168 | int quantOffset = 0, |
| 169 | tflite::MirrorPadMode paddingMode = tflite::MirrorPadMode_SYMMETRIC) |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 170 | { |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 171 | using namespace delegateTestInterpreter; |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 172 | std::vector<char> modelBuffer = CreatePadTfLiteModel<T>(padOperatorCode, |
| 173 | tensorType, |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 174 | paddingMode, |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 175 | inputShape, |
| 176 | paddingShape, |
| 177 | outputShape, |
| 178 | paddingDim, |
| 179 | {paddingValue}, |
| 180 | quantScale, |
| 181 | quantOffset); |
| 182 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 183 | // Setup interpreter with just TFLite Runtime. |
| 184 | auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer); |
| 185 | CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk); |
| 186 | CHECK(tfLiteInterpreter.FillInputTensor<T>(inputValues, 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); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 190 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 191 | // Setup interpreter with Arm NN Delegate applied. |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame] | 192 | auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, CaptureAvailableBackends(backends)); |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 193 | CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk); |
| 194 | CHECK(armnnInterpreter.FillInputTensor<T>(inputValues, 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); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 198 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 199 | armnnDelegate::CompareOutputData<T>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues); |
| 200 | armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, outputShape); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 201 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 202 | tfLiteInterpreter.Cleanup(); |
| 203 | armnnInterpreter.Cleanup(); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Keith Davis | bbc876c | 2021-01-27 13:12:03 +0000 | [diff] [blame] | 206 | } // anonymous namespace |