Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 1 | // |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame] | 2 | // Copyright © 2021, 2023-2024 Arm Ltd and Contributors. All rights reserved. |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +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> |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 12 | |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 13 | #include <tensorflow/lite/version.h> |
| 14 | |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 15 | namespace |
| 16 | { |
| 17 | |
| 18 | std::vector<char> CreateBatchSpaceTfLiteModel(tflite::BuiltinOperator batchSpaceOperatorCode, |
| 19 | tflite::TensorType tensorType, |
| 20 | std::vector<int32_t>& inputTensorShape, |
| 21 | std::vector <int32_t>& outputTensorShape, |
| 22 | std::vector<unsigned int>& blockData, |
| 23 | std::vector<std::pair<unsigned int, unsigned int>>& cropsPadData, |
| 24 | float quantScale = 1.0f, |
| 25 | int quantOffset = 0) |
| 26 | { |
| 27 | using namespace tflite; |
| 28 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 29 | |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 30 | std::array<flatbuffers::Offset<tflite::Buffer>, 5> buffers; |
| 31 | buffers[0] = CreateBuffer(flatBufferBuilder); |
| 32 | buffers[1] = CreateBuffer(flatBufferBuilder); |
| 33 | buffers[2] = CreateBuffer(flatBufferBuilder, |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 34 | flatBufferBuilder.CreateVector(reinterpret_cast<const uint8_t*>(blockData.data()), |
| 35 | sizeof(int32_t) * blockData.size())); |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 36 | buffers[3] = CreateBuffer(flatBufferBuilder, |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 37 | flatBufferBuilder.CreateVector(reinterpret_cast<const uint8_t*>(cropsPadData.data()), |
| 38 | sizeof(int64_t) * cropsPadData.size())); |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 39 | buffers[4] = CreateBuffer(flatBufferBuilder); |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 40 | |
| 41 | auto quantizationParameters = |
| 42 | CreateQuantizationParameters(flatBufferBuilder, |
| 43 | 0, |
| 44 | 0, |
| 45 | flatBufferBuilder.CreateVector<float>({ quantScale }), |
| 46 | flatBufferBuilder.CreateVector<int64_t>({ quantOffset })); |
| 47 | |
| 48 | std::string cropsOrPadding = |
| 49 | batchSpaceOperatorCode == tflite::BuiltinOperator_BATCH_TO_SPACE_ND ? "crops" : "padding"; |
| 50 | |
| 51 | std::vector<int32_t> blockShape { 2 }; |
| 52 | std::vector<int32_t> cropsOrPaddingShape { 2, 2 }; |
| 53 | |
| 54 | std::array<flatbuffers::Offset<Tensor>, 4> tensors; |
| 55 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 56 | flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(), |
| 57 | inputTensorShape.size()), |
| 58 | tensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 59 | 1, |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 60 | flatBufferBuilder.CreateString("input"), |
| 61 | quantizationParameters); |
| 62 | |
| 63 | tensors[1] = CreateTensor(flatBufferBuilder, |
| 64 | flatBufferBuilder.CreateVector<int32_t>(blockShape.data(), |
| 65 | blockShape.size()), |
| 66 | ::tflite::TensorType_INT32, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 67 | 2, |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 68 | flatBufferBuilder.CreateString("block"), |
| 69 | quantizationParameters); |
| 70 | |
| 71 | tensors[2] = CreateTensor(flatBufferBuilder, |
| 72 | flatBufferBuilder.CreateVector<int32_t>(cropsOrPaddingShape.data(), |
| 73 | cropsOrPaddingShape.size()), |
| 74 | ::tflite::TensorType_INT32, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 75 | 3, |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 76 | flatBufferBuilder.CreateString(cropsOrPadding), |
| 77 | quantizationParameters); |
| 78 | |
| 79 | // Create output tensor |
| 80 | tensors[3] = CreateTensor(flatBufferBuilder, |
| 81 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 82 | outputTensorShape.size()), |
| 83 | tensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 84 | 4, |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 85 | flatBufferBuilder.CreateString("output"), |
| 86 | quantizationParameters); |
| 87 | |
| 88 | // Create operator |
| 89 | tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_NONE; |
| 90 | flatbuffers::Offset<void> operatorBuiltinOptions = 0; |
| 91 | switch (batchSpaceOperatorCode) |
| 92 | { |
| 93 | case tflite::BuiltinOperator_BATCH_TO_SPACE_ND: |
| 94 | { |
| 95 | operatorBuiltinOptionsType = tflite::BuiltinOptions_BatchToSpaceNDOptions; |
| 96 | operatorBuiltinOptions = CreateBatchToSpaceNDOptions(flatBufferBuilder).Union(); |
| 97 | break; |
| 98 | } |
| 99 | case tflite::BuiltinOperator_SPACE_TO_BATCH_ND: |
| 100 | { |
| 101 | operatorBuiltinOptionsType = tflite::BuiltinOptions_SpaceToBatchNDOptions; |
| 102 | operatorBuiltinOptions = CreateSpaceToBatchNDOptions(flatBufferBuilder).Union(); |
| 103 | break; |
| 104 | } |
| 105 | default: |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | const std::vector<int> operatorInputs{ {0, 1, 2} }; |
| 110 | const std::vector<int> operatorOutputs{ 3 }; |
| 111 | flatbuffers::Offset <Operator> batchSpaceOperator = |
| 112 | CreateOperator(flatBufferBuilder, |
| 113 | 0, |
| 114 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 115 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 116 | operatorBuiltinOptionsType, |
| 117 | operatorBuiltinOptions); |
| 118 | |
| 119 | const std::vector<int> subgraphInputs{ {0, 1, 2} }; |
| 120 | const std::vector<int> subgraphOutputs{ 3 }; |
| 121 | flatbuffers::Offset <SubGraph> subgraph = |
| 122 | CreateSubGraph(flatBufferBuilder, |
| 123 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 124 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 125 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
| 126 | flatBufferBuilder.CreateVector(&batchSpaceOperator, 1)); |
| 127 | |
| 128 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 129 | flatBufferBuilder.CreateString("ArmnnDelegate: BatchSpace Operator Model"); |
| 130 | flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, batchSpaceOperatorCode); |
| 131 | |
| 132 | flatbuffers::Offset <Model> flatbufferModel = |
| 133 | CreateModel(flatBufferBuilder, |
| 134 | TFLITE_SCHEMA_VERSION, |
| 135 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 136 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 137 | modelDescription, |
| 138 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 139 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 140 | flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER); |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 141 | |
| 142 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 143 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 144 | } |
| 145 | |
| 146 | template <typename T> |
| 147 | void BatchSpaceTest(tflite::BuiltinOperator controlOperatorCode, |
| 148 | tflite::TensorType tensorType, |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 149 | std::vector<int32_t>& inputShape, |
| 150 | std::vector<int32_t>& expectedOutputShape, |
| 151 | std::vector<T>& inputValues, |
| 152 | std::vector<unsigned int>& blockShapeValues, |
| 153 | std::vector<std::pair<unsigned int, unsigned int>>& cropsPaddingValues, |
| 154 | std::vector<T>& expectedOutputValues, |
| 155 | float quantScale = 1.0f, |
Colm Donelan | eff204a | 2023-11-28 15:46:09 +0000 | [diff] [blame] | 156 | int quantOffset = 0, |
| 157 | const std::vector<armnn::BackendId>& backends = {}) |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 158 | { |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 159 | using namespace delegateTestInterpreter; |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 160 | std::vector<char> modelBuffer = CreateBatchSpaceTfLiteModel(controlOperatorCode, |
| 161 | tensorType, |
| 162 | inputShape, |
| 163 | expectedOutputShape, |
| 164 | blockShapeValues, |
| 165 | cropsPaddingValues, |
| 166 | quantScale, |
| 167 | quantOffset); |
| 168 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 169 | // Setup interpreter with just TFLite Runtime. |
| 170 | auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer); |
| 171 | CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk); |
| 172 | CHECK(tfLiteInterpreter.FillInputTensor(inputValues, 0) == kTfLiteOk); |
| 173 | CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk); |
| 174 | std::vector<T> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<T>(0); |
| 175 | std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0); |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 176 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 177 | // Setup interpreter with Arm NN Delegate applied. |
Colm Donelan | eff204a | 2023-11-28 15:46:09 +0000 | [diff] [blame] | 178 | auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, CaptureAvailableBackends(backends)); |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 179 | CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk); |
| 180 | CHECK(armnnInterpreter.FillInputTensor(inputValues, 0) == kTfLiteOk); |
| 181 | CHECK(armnnInterpreter.Invoke() == kTfLiteOk); |
| 182 | std::vector<T> armnnOutputValues = armnnInterpreter.GetOutputResult<T>(0); |
| 183 | std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(0); |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 184 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 185 | armnnDelegate::CompareOutputData<T>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues); |
| 186 | armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, expectedOutputShape); |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 187 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 188 | tfLiteInterpreter.Cleanup(); |
| 189 | armnnInterpreter.Cleanup(); |
Matthew Sloyan | a35b40b | 2021-02-05 17:22:28 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } // anonymous namespace |