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