Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +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. |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +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> |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 12 | |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 13 | #include <tensorflow/lite/version.h> |
| 14 | |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 15 | namespace |
| 16 | { |
| 17 | |
| 18 | std::vector<char> CreateGatherTfLiteModel(tflite::TensorType tensorType, |
| 19 | std::vector<int32_t>& paramsShape, |
| 20 | std::vector<int32_t>& indicesShape, |
| 21 | const std::vector<int32_t>& expectedOutputShape, |
| 22 | int32_t axis, |
| 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 OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 30 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 31 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 32 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 33 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 34 | |
| 35 | auto quantizationParameters = |
| 36 | CreateQuantizationParameters(flatBufferBuilder, |
| 37 | 0, |
| 38 | 0, |
| 39 | flatBufferBuilder.CreateVector<float>({quantScale}), |
| 40 | flatBufferBuilder.CreateVector<int64_t>({quantOffset})); |
| 41 | |
| 42 | std::array<flatbuffers::Offset<Tensor>, 3> tensors; |
| 43 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 44 | flatBufferBuilder.CreateVector<int32_t>(paramsShape.data(), |
| 45 | paramsShape.size()), |
| 46 | tensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 47 | 1, |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 48 | flatBufferBuilder.CreateString("params"), |
| 49 | quantizationParameters); |
| 50 | tensors[1] = CreateTensor(flatBufferBuilder, |
| 51 | flatBufferBuilder.CreateVector<int32_t>(indicesShape.data(), |
| 52 | indicesShape.size()), |
| 53 | ::tflite::TensorType_INT32, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 54 | 2, |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 55 | flatBufferBuilder.CreateString("indices"), |
| 56 | quantizationParameters); |
| 57 | tensors[2] = CreateTensor(flatBufferBuilder, |
| 58 | flatBufferBuilder.CreateVector<int32_t>(expectedOutputShape.data(), |
| 59 | expectedOutputShape.size()), |
| 60 | tensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 61 | 3, |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 62 | flatBufferBuilder.CreateString("output"), |
| 63 | quantizationParameters); |
| 64 | |
| 65 | |
| 66 | // create operator |
| 67 | tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_GatherOptions; |
| 68 | flatbuffers::Offset<void> operatorBuiltinOptions = CreateGatherOptions(flatBufferBuilder).Union(); |
| 69 | |
| 70 | const std::vector<int> operatorInputs{{0, 1}}; |
Finn Williams | 019840d | 2020-11-30 17:43:28 +0000 | [diff] [blame] | 71 | const std::vector<int> operatorOutputs{2}; |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 72 | flatbuffers::Offset<Operator> controlOperator = |
| 73 | CreateOperator(flatBufferBuilder, |
| 74 | 0, |
| 75 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), |
| 76 | operatorInputs.size()), |
| 77 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), |
| 78 | operatorOutputs.size()), |
| 79 | operatorBuiltinOptionsType, |
| 80 | operatorBuiltinOptions); |
| 81 | |
| 82 | const std::vector<int> subgraphInputs{{0, 1}}; |
Finn Williams | 019840d | 2020-11-30 17:43:28 +0000 | [diff] [blame] | 83 | const std::vector<int> subgraphOutputs{2}; |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 84 | flatbuffers::Offset<SubGraph> subgraph = |
| 85 | CreateSubGraph(flatBufferBuilder, |
| 86 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 87 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), |
| 88 | subgraphInputs.size()), |
| 89 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), |
| 90 | subgraphOutputs.size()), |
| 91 | flatBufferBuilder.CreateVector(&controlOperator, 1)); |
| 92 | |
| 93 | flatbuffers::Offset<flatbuffers::String> modelDescription = |
| 94 | flatBufferBuilder.CreateString("ArmnnDelegate: GATHER Operator Model"); |
| 95 | flatbuffers::Offset<OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, |
| 96 | BuiltinOperator_GATHER); |
| 97 | |
| 98 | flatbuffers::Offset<Model> flatbufferModel = |
| 99 | CreateModel(flatBufferBuilder, |
| 100 | TFLITE_SCHEMA_VERSION, |
| 101 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 102 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 103 | modelDescription, |
| 104 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 105 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 106 | flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER); |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 107 | |
| 108 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 109 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 110 | } |
| 111 | |
| 112 | template<typename T> |
| 113 | void GatherTest(tflite::TensorType tensorType, |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 114 | std::vector<int32_t>& paramsShape, |
| 115 | std::vector<int32_t>& indicesShape, |
| 116 | std::vector<int32_t>& expectedOutputShape, |
| 117 | int32_t axis, |
| 118 | std::vector<T>& paramsValues, |
| 119 | std::vector<int32_t>& indicesValues, |
| 120 | std::vector<T>& expectedOutputValues, |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame^] | 121 | const std::vector<armnn::BackendId>& backends = {}, |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 122 | float quantScale = 1.0f, |
| 123 | int quantOffset = 0) |
| 124 | { |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 125 | using namespace delegateTestInterpreter; |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 126 | std::vector<char> modelBuffer = CreateGatherTfLiteModel(tensorType, |
| 127 | paramsShape, |
| 128 | indicesShape, |
| 129 | expectedOutputShape, |
| 130 | axis, |
| 131 | quantScale, |
| 132 | quantOffset); |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 133 | // Setup interpreter with just TFLite Runtime. |
| 134 | auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer); |
| 135 | CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk); |
| 136 | CHECK(tfLiteInterpreter.FillInputTensor<T>(paramsValues, 0) == kTfLiteOk); |
| 137 | CHECK(tfLiteInterpreter.FillInputTensor<int32_t>(indicesValues, 1) == kTfLiteOk); |
| 138 | CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk); |
| 139 | std::vector<T> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<T>(0); |
| 140 | std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0); |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 141 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 142 | // Setup interpreter with Arm NN Delegate applied. |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame^] | 143 | auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, CaptureAvailableBackends(backends)); |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 144 | CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk); |
| 145 | CHECK(armnnInterpreter.FillInputTensor<T>(paramsValues, 0) == kTfLiteOk); |
| 146 | CHECK(armnnInterpreter.FillInputTensor<int32_t>(indicesValues, 1) == kTfLiteOk); |
| 147 | CHECK(armnnInterpreter.Invoke() == kTfLiteOk); |
| 148 | std::vector<T> armnnOutputValues = armnnInterpreter.GetOutputResult<T>(0); |
| 149 | std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(0); |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 150 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 151 | armnnDelegate::CompareOutputData<T>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues); |
| 152 | armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, expectedOutputShape); |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 153 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 154 | tfLiteInterpreter.Cleanup(); |
| 155 | armnnInterpreter.Cleanup(); |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 156 | } |
| 157 | } // anonymous namespace |