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