James Ward | a857810 | 2020-11-13 18:05:04 +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. |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 8 | #include "TestUtils.hpp" |
| 9 | |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 10 | #include <armnn_delegate.hpp> |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 11 | #include <DelegateTestInterpreter.hpp> |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 12 | |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 13 | #include <tensorflow/lite/version.h> |
| 14 | |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 15 | namespace |
| 16 | { |
| 17 | std::vector<char> CreateSoftmaxTfLiteModel(tflite::BuiltinOperator softmaxOperatorCode, |
| 18 | tflite::TensorType tensorType, |
| 19 | const std::vector <int32_t>& tensorShape, |
| 20 | float beta) |
| 21 | { |
| 22 | using namespace tflite; |
| 23 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 24 | |
| 25 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 26 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 27 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 28 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 29 | |
| 30 | std::array<flatbuffers::Offset<Tensor>, 2> tensors; |
| 31 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 32 | flatBufferBuilder.CreateVector<int32_t>(tensorShape.data(), |
| 33 | tensorShape.size()), |
| 34 | tensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 35 | 1); |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 36 | tensors[1] = CreateTensor(flatBufferBuilder, |
| 37 | flatBufferBuilder.CreateVector<int32_t>(tensorShape.data(), |
| 38 | tensorShape.size()), |
| 39 | tensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 40 | 2); |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 41 | |
| 42 | const std::vector<int32_t> operatorInputs({0}); |
| 43 | const std::vector<int32_t> operatorOutputs({1}); |
| 44 | |
| 45 | flatbuffers::Offset<Operator> softmaxOperator; |
| 46 | flatbuffers::Offset<flatbuffers::String> modelDescription; |
| 47 | flatbuffers::Offset<OperatorCode> operatorCode; |
| 48 | |
| 49 | switch (softmaxOperatorCode) |
| 50 | { |
| 51 | case tflite::BuiltinOperator_SOFTMAX: |
| 52 | softmaxOperator = |
| 53 | CreateOperator(flatBufferBuilder, |
| 54 | 0, |
| 55 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 56 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 57 | BuiltinOptions_SoftmaxOptions, |
| 58 | CreateSoftmaxOptions(flatBufferBuilder, beta).Union()); |
| 59 | modelDescription = flatBufferBuilder.CreateString("ArmnnDelegate: Softmax Operator Model"); |
| 60 | operatorCode = CreateOperatorCode(flatBufferBuilder, |
| 61 | tflite::BuiltinOperator_SOFTMAX); |
| 62 | break; |
| 63 | case tflite::BuiltinOperator_LOG_SOFTMAX: |
| 64 | softmaxOperator = |
| 65 | CreateOperator(flatBufferBuilder, |
| 66 | 0, |
| 67 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 68 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 69 | BuiltinOptions_LogSoftmaxOptions, |
| 70 | CreateLogSoftmaxOptions(flatBufferBuilder).Union()); |
| 71 | flatBufferBuilder.CreateString("ArmnnDelegate: Log-Softmax Operator Model"); |
| 72 | operatorCode = CreateOperatorCode(flatBufferBuilder, |
| 73 | tflite::BuiltinOperator_LOG_SOFTMAX); |
| 74 | break; |
| 75 | default: |
| 76 | break; |
| 77 | } |
| 78 | const std::vector<int32_t> subgraphInputs({0}); |
| 79 | const std::vector<int32_t> subgraphOutputs({1}); |
| 80 | flatbuffers::Offset<SubGraph> subgraph = |
| 81 | CreateSubGraph(flatBufferBuilder, |
| 82 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 83 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 84 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
| 85 | flatBufferBuilder.CreateVector(&softmaxOperator, 1)); |
| 86 | flatbuffers::Offset<Model> flatbufferModel = |
| 87 | CreateModel(flatBufferBuilder, |
| 88 | TFLITE_SCHEMA_VERSION, |
| 89 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 90 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 91 | modelDescription, |
| 92 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 93 | flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER); |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 94 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 95 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 96 | } |
| 97 | |
| 98 | void SoftmaxTest(tflite::BuiltinOperator softmaxOperatorCode, |
| 99 | tflite::TensorType tensorType, |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 100 | std::vector<int32_t>& shape, |
| 101 | std::vector<float>& inputValues, |
| 102 | std::vector<float>& expectedOutputValues, |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame] | 103 | const std::vector<armnn::BackendId>& backends = {}, |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 104 | float beta = 0) |
| 105 | { |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 106 | using namespace delegateTestInterpreter; |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 107 | std::vector<char> modelBuffer = CreateSoftmaxTfLiteModel(softmaxOperatorCode, |
| 108 | tensorType, |
| 109 | shape, |
| 110 | beta); |
| 111 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 112 | // Setup interpreter with just TFLite Runtime. |
| 113 | auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer); |
| 114 | CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk); |
| 115 | CHECK(tfLiteInterpreter.FillInputTensor<float>(inputValues, 0) == kTfLiteOk); |
| 116 | CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk); |
| 117 | std::vector<float> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<float>(0); |
| 118 | std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0); |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 119 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 120 | // Setup interpreter with Arm NN Delegate applied. |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame] | 121 | auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, CaptureAvailableBackends(backends)); |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 122 | CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk); |
| 123 | CHECK(armnnInterpreter.FillInputTensor<float>(inputValues, 0) == kTfLiteOk); |
| 124 | CHECK(armnnInterpreter.Invoke() == kTfLiteOk); |
| 125 | std::vector<float> armnnOutputValues = armnnInterpreter.GetOutputResult<float>(0); |
| 126 | std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(0); |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 127 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 128 | armnnDelegate::CompareOutputData<float>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues); |
| 129 | armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, shape); |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 130 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 131 | tfLiteInterpreter.Cleanup(); |
| 132 | armnnInterpreter.Cleanup(); |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Keith Davis | 7c67fab | 2021-04-08 11:47:23 +0100 | [diff] [blame] | 135 | |
| 136 | /// Convenience function to run softmax and log-softmax test cases |
| 137 | /// \param operatorCode tflite::BuiltinOperator_SOFTMAX or tflite::BuiltinOperator_LOG_SOFTMAX |
| 138 | /// \param backends armnn backends to target |
| 139 | /// \param beta multiplicative parameter to the softmax function |
| 140 | /// \param expectedOutput to be checked against transformed input |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame] | 141 | void SoftmaxTestCase(tflite::BuiltinOperator operatorCode, float beta, |
| 142 | std::vector<float> expectedOutput, const std::vector<armnn::BackendId> backends = {}) |
| 143 | { |
Keith Davis | 7c67fab | 2021-04-08 11:47:23 +0100 | [diff] [blame] | 144 | std::vector<float> input = { |
| 145 | 1.0, 2.5, 3.0, 4.5, 5.0, |
| 146 | -1.0, -2.5, -3.0, -4.5, -5.0}; |
| 147 | std::vector<int32_t> shape = {2, 5}; |
| 148 | |
| 149 | SoftmaxTest(operatorCode, |
| 150 | tflite::TensorType_FLOAT32, |
Keith Davis | 7c67fab | 2021-04-08 11:47:23 +0100 | [diff] [blame] | 151 | shape, |
| 152 | input, |
| 153 | expectedOutput, |
Colm Donelan | 7bcae3c | 2024-01-22 10:07:14 +0000 | [diff] [blame] | 154 | backends, |
Keith Davis | 7c67fab | 2021-04-08 11:47:23 +0100 | [diff] [blame] | 155 | beta); |
| 156 | } |
| 157 | |
James Ward | a857810 | 2020-11-13 18:05:04 +0000 | [diff] [blame] | 158 | } // anonymous namespace |