Kevin May | 93bbf00 | 2024-03-11 09:31:10 +0000 | [diff] [blame^] | 1 | // |
| 2 | // Copyright © 2024 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 <tensorflow/lite/version.h> |
| 14 | |
| 15 | namespace |
| 16 | { |
| 17 | |
| 18 | std::vector<char> CreateScatterNdTfLiteModel(tflite::TensorType tensorType, |
| 19 | const std::vector<int32_t>& indicesShape, |
| 20 | const std::vector<int32_t>& updatesShape, |
| 21 | const std::vector<int32_t>& shapeShape, |
| 22 | const std::vector<int32_t>& outputShape, |
| 23 | const std::vector<int32_t>& shapeData, |
| 24 | float quantScale = 1.0f, |
| 25 | int quantOffset = 0) |
| 26 | { |
| 27 | using namespace tflite; |
| 28 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 29 | |
| 30 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
| 31 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 32 | buffers.push_back(CreateBuffer(flatBufferBuilder)); // indices |
| 33 | buffers.push_back(CreateBuffer(flatBufferBuilder)); // updates |
| 34 | buffers.push_back(CreateBuffer(flatBufferBuilder, |
| 35 | flatBufferBuilder.CreateVector(reinterpret_cast<const uint8_t*>(shapeData.data()), |
| 36 | sizeof(int32_t) * shapeData.size()))); |
| 37 | buffers.push_back(CreateBuffer(flatBufferBuilder)); // output |
| 38 | |
| 39 | auto quantizationParameters = |
| 40 | CreateQuantizationParameters(flatBufferBuilder, |
| 41 | 0, |
| 42 | 0, |
| 43 | flatBufferBuilder.CreateVector<float>({ quantScale }), |
| 44 | flatBufferBuilder.CreateVector<int64_t>({ quantOffset })); |
| 45 | |
| 46 | std::array<flatbuffers::Offset<Tensor>, 4> tensors; |
| 47 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 48 | flatBufferBuilder.CreateVector<int32_t>(indicesShape.data(), |
| 49 | indicesShape.size()), |
| 50 | TensorType_INT32, |
| 51 | 1, |
| 52 | flatBufferBuilder.CreateString("indices_tensor"), |
| 53 | quantizationParameters); |
| 54 | |
| 55 | tensors[1] = CreateTensor(flatBufferBuilder, |
| 56 | flatBufferBuilder.CreateVector<int32_t>(updatesShape.data(), |
| 57 | updatesShape.size()), |
| 58 | tensorType, |
| 59 | 2, |
| 60 | flatBufferBuilder.CreateString("updates_tensor"), |
| 61 | quantizationParameters); |
| 62 | |
| 63 | tensors[2] = CreateTensor(flatBufferBuilder, |
| 64 | flatBufferBuilder.CreateVector<int32_t>(shapeShape.data(), |
| 65 | shapeShape.size()), |
| 66 | TensorType_INT32, |
| 67 | 3, |
| 68 | flatBufferBuilder.CreateString("shape_tensor"), |
| 69 | quantizationParameters); |
| 70 | |
| 71 | tensors[3] = CreateTensor(flatBufferBuilder, |
| 72 | flatBufferBuilder.CreateVector<int32_t>(outputShape.data(), |
| 73 | outputShape.size()), |
| 74 | tensorType, |
| 75 | 4, |
| 76 | flatBufferBuilder.CreateString("output_tensor"), |
| 77 | quantizationParameters); |
| 78 | |
| 79 | // Create Operator |
| 80 | tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_ScatterNdOptions; |
| 81 | flatbuffers::Offset<void> operatorBuiltinOptions = CreateScatterNdOptions(flatBufferBuilder).Union(); |
| 82 | |
| 83 | const std::vector<int> operatorInputs { 0, 1, 2 }; |
| 84 | const std::vector<int> operatorOutputs { 3 }; |
| 85 | |
| 86 | flatbuffers::Offset<Operator> scatterNdOperator = |
| 87 | CreateOperator(flatBufferBuilder, |
| 88 | 0, |
| 89 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 90 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 91 | operatorBuiltinOptionsType, |
| 92 | operatorBuiltinOptions); |
| 93 | |
| 94 | const std::vector<int> subgraphInputs{ 0, 1, 2 }; |
| 95 | const std::vector<int> subgraphOutputs{ 3 }; |
| 96 | flatbuffers::Offset <SubGraph> subgraph = |
| 97 | CreateSubGraph(flatBufferBuilder, |
| 98 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 99 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 100 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
| 101 | flatBufferBuilder.CreateVector(&scatterNdOperator, 1)); |
| 102 | |
| 103 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 104 | flatBufferBuilder.CreateString("ArmnnDelegate: ScatterNd Operator Model"); |
| 105 | flatbuffers::Offset <OperatorCode> opCode = CreateOperatorCode(flatBufferBuilder, |
| 106 | tflite::BuiltinOperator_SCATTER_ND); |
| 107 | |
| 108 | flatbuffers::Offset <Model> flatbufferModel = |
| 109 | CreateModel(flatBufferBuilder, |
| 110 | TFLITE_SCHEMA_VERSION, |
| 111 | flatBufferBuilder.CreateVector(&opCode, 1), |
| 112 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 113 | modelDescription, |
| 114 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 115 | |
| 116 | flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER); |
| 117 | |
| 118 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 119 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 120 | } |
| 121 | |
| 122 | template<typename T> |
| 123 | void ScatterNdTestImpl(tflite::TensorType tensorType, |
| 124 | std::vector<int32_t>& indicesShape, |
| 125 | std::vector<int32_t>& indicesValues, |
| 126 | std::vector<int32_t>& updatesShape, |
| 127 | std::vector<T>& updatesValues, |
| 128 | std::vector<int32_t>& shapeShape, |
| 129 | std::vector<int32_t>& shapeValue, |
| 130 | std::vector<int32_t>& expectedOutputShape, |
| 131 | std::vector<T>& expectedOutputValues, |
| 132 | const std::vector<armnn::BackendId>& backends = {}, |
| 133 | float quantScale = 1.0f, |
| 134 | int quantOffset = 0) |
| 135 | { |
| 136 | using namespace delegateTestInterpreter; |
| 137 | |
| 138 | std::vector<char> modelBuffer = CreateScatterNdTfLiteModel(tensorType, |
| 139 | indicesShape, |
| 140 | updatesShape, |
| 141 | shapeShape, |
| 142 | expectedOutputShape, |
| 143 | shapeValue, |
| 144 | quantScale, |
| 145 | quantOffset); |
| 146 | |
| 147 | // Setup interpreter with just TFLite Runtime. |
| 148 | auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer); |
| 149 | CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk); |
| 150 | CHECK(tfLiteInterpreter.FillInputTensor<int32_t>(indicesValues, 0) == kTfLiteOk); |
| 151 | CHECK(tfLiteInterpreter.FillInputTensor<T>(updatesValues, 1) == kTfLiteOk); |
| 152 | CHECK(tfLiteInterpreter.FillInputTensor<int32_t>(shapeValue, 2) == kTfLiteOk); |
| 153 | CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk); |
| 154 | std::vector<T> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<T>(0); |
| 155 | std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0); |
| 156 | |
| 157 | // Setup interpreter with Arm NN Delegate applied. |
| 158 | auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, CaptureAvailableBackends(backends)); |
| 159 | CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk); |
| 160 | CHECK(armnnInterpreter.FillInputTensor<int32_t>(indicesValues, 0) == kTfLiteOk); |
| 161 | CHECK(armnnInterpreter.FillInputTensor<T>(updatesValues, 1) == kTfLiteOk); |
| 162 | CHECK(armnnInterpreter.FillInputTensor<int32_t>(shapeValue, 2) == kTfLiteOk); |
| 163 | CHECK(armnnInterpreter.Invoke() == kTfLiteOk); |
| 164 | std::vector<T> armnnOutputValues = armnnInterpreter.GetOutputResult<T>(0); |
| 165 | std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(0); |
| 166 | |
| 167 | armnnDelegate::CompareOutputData<T>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues); |
| 168 | armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, expectedOutputShape); |
| 169 | |
| 170 | tfLiteInterpreter.Cleanup(); |
| 171 | armnnInterpreter.Cleanup(); |
| 172 | } |
| 173 | |
| 174 | } // anonymous namespace |