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