Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [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. |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +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> |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 12 | |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 13 | #include <tensorflow/lite/version.h> |
| 14 | |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 15 | namespace |
| 16 | { |
| 17 | |
| 18 | std::vector<char> CreateUnpackTfLiteModel(tflite::BuiltinOperator unpackOperatorCode, |
| 19 | tflite::TensorType tensorType, |
| 20 | std::vector<int32_t>& inputTensorShape, |
| 21 | const std::vector <int32_t>& outputTensorShape, |
| 22 | const int32_t outputTensorNum, |
| 23 | unsigned int axis = 0, |
| 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; |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 31 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 32 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 33 | |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [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 | const std::vector<int32_t> operatorInputs{ 0 }; |
| 43 | std::vector<int32_t> operatorOutputs{}; |
| 44 | const std::vector<int> subgraphInputs{ 0 }; |
| 45 | std::vector<int> subgraphOutputs{}; |
| 46 | |
| 47 | std::vector<flatbuffers::Offset<Tensor>> tensors(outputTensorNum + 1); |
| 48 | |
| 49 | // Create input tensor |
| 50 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 51 | flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(), |
| 52 | inputTensorShape.size()), |
| 53 | tensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 54 | 1, |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 55 | flatBufferBuilder.CreateString("input"), |
| 56 | quantizationParameters); |
| 57 | |
| 58 | for (int i = 0; i < outputTensorNum; ++i) |
| 59 | { |
| 60 | tensors[i + 1] = CreateTensor(flatBufferBuilder, |
| 61 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 62 | outputTensorShape.size()), |
| 63 | tensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 64 | (i + 2), |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 65 | flatBufferBuilder.CreateString("output" + std::to_string(i)), |
| 66 | quantizationParameters); |
| 67 | |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 68 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 69 | operatorOutputs.push_back(i + 1); |
| 70 | subgraphOutputs.push_back(i + 1); |
| 71 | } |
| 72 | |
| 73 | // create operator |
| 74 | tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_UnpackOptions; |
| 75 | flatbuffers::Offset<void> operatorBuiltinOptions = |
| 76 | CreateUnpackOptions(flatBufferBuilder, outputTensorNum, axis).Union(); |
| 77 | |
| 78 | flatbuffers::Offset <Operator> unpackOperator = |
| 79 | CreateOperator(flatBufferBuilder, |
| 80 | 0, |
| 81 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 82 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 83 | operatorBuiltinOptionsType, |
| 84 | operatorBuiltinOptions); |
| 85 | |
| 86 | flatbuffers::Offset <SubGraph> subgraph = |
| 87 | CreateSubGraph(flatBufferBuilder, |
| 88 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 89 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 90 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
| 91 | flatBufferBuilder.CreateVector(&unpackOperator, 1)); |
| 92 | |
| 93 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 94 | flatBufferBuilder.CreateString("ArmnnDelegate: Unpack Operator Model"); |
| 95 | flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, unpackOperatorCode); |
| 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, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 103 | flatBufferBuilder.CreateVector(buffers)); |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 104 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 105 | flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER); |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 106 | |
| 107 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 108 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 109 | } |
| 110 | |
| 111 | template <typename T> |
| 112 | void UnpackTest(tflite::BuiltinOperator unpackOperatorCode, |
| 113 | tflite::TensorType tensorType, |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 114 | std::vector<int32_t>& inputShape, |
| 115 | std::vector<int32_t>& expectedOutputShape, |
| 116 | std::vector<T>& inputValues, |
| 117 | std::vector<std::vector<T>>& expectedOutputValues, |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame] | 118 | const std::vector<armnn::BackendId>& backends = {}, |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 119 | unsigned int axis = 0, |
| 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; |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 124 | std::vector<char> modelBuffer = CreateUnpackTfLiteModel(unpackOperatorCode, |
| 125 | tensorType, |
| 126 | inputShape, |
| 127 | expectedOutputShape, |
| 128 | expectedOutputValues.size(), |
| 129 | axis, |
| 130 | quantScale, |
| 131 | quantOffset); |
| 132 | |
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>(inputValues, 0) == kTfLiteOk); |
| 137 | CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk); |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +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>(inputValues, 0) == kTfLiteOk); |
| 143 | CHECK(armnnInterpreter.Invoke() == kTfLiteOk); |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 144 | |
| 145 | // Compare output data |
| 146 | for (unsigned int i = 0; i < expectedOutputValues.size(); ++i) |
| 147 | { |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 148 | std::vector<T> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<T>(i); |
| 149 | std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(i); |
| 150 | |
| 151 | std::vector<T> armnnOutputValues = armnnInterpreter.GetOutputResult<T>(i); |
| 152 | std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(i); |
| 153 | |
| 154 | armnnDelegate::CompareOutputData<T>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues[i]); |
| 155 | armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, expectedOutputShape); |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 158 | tfLiteInterpreter.Cleanup(); |
| 159 | armnnInterpreter.Cleanup(); |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | } // anonymous namespace |