David Monahan | 0cf8442 | 2020-11-16 15:53:03 +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. |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 8 | #include "TestUtils.hpp" |
| 9 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 10 | #include <armnn_delegate.hpp> |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 11 | #include <DelegateTestInterpreter.hpp> |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 12 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 13 | #include <tensorflow/lite/version.h> |
| 14 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 15 | namespace |
| 16 | { |
| 17 | |
| 18 | std::vector<char> CreateActivationTfLiteModel(tflite::BuiltinOperator activationOperatorCode, |
| 19 | tflite::TensorType tensorType, |
Tianle Cheng | ae93173 | 2023-07-28 11:53:04 +0100 | [diff] [blame] | 20 | const std::vector <int32_t>& tensorShape, |
| 21 | float alpha = 0) |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 22 | { |
| 23 | using namespace tflite; |
| 24 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 25 | |
| 26 | std::array<flatbuffers::Offset<tflite::Buffer>, 1> buffers; |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 27 | buffers[0] = CreateBuffer(flatBufferBuilder); |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 28 | |
| 29 | std::array<flatbuffers::Offset<Tensor>, 2> tensors; |
| 30 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 31 | flatBufferBuilder.CreateVector<int32_t>(tensorShape.data(), tensorShape.size()), |
| 32 | tensorType); |
| 33 | tensors[1] = CreateTensor(flatBufferBuilder, |
| 34 | flatBufferBuilder.CreateVector<int32_t>(tensorShape.data(), tensorShape.size()), |
| 35 | tensorType); |
| 36 | |
| 37 | // create operator |
Keith Davis | 892fafe | 2020-11-26 17:40:35 +0000 | [diff] [blame] | 38 | const std::vector<int> operatorInputs{0}; |
| 39 | const std::vector<int> operatorOutputs{1}; |
Tianle Cheng | ae93173 | 2023-07-28 11:53:04 +0100 | [diff] [blame] | 40 | |
| 41 | // builtin options |
| 42 | tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_NONE; |
| 43 | flatbuffers::Offset<void> operatorBuiltinOption = 0; |
| 44 | |
| 45 | if (activationOperatorCode == tflite::BuiltinOperator_LEAKY_RELU) |
| 46 | { |
| 47 | operatorBuiltinOptionsType = tflite::BuiltinOptions_LeakyReluOptions; |
| 48 | operatorBuiltinOption = CreateLeakyReluOptions(flatBufferBuilder, alpha).Union(); |
| 49 | } |
| 50 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 51 | flatbuffers::Offset <Operator> unaryOperator = |
| 52 | CreateOperator(flatBufferBuilder, |
| 53 | 0, |
| 54 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
Tianle Cheng | ae93173 | 2023-07-28 11:53:04 +0100 | [diff] [blame] | 55 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 56 | operatorBuiltinOptionsType, |
| 57 | operatorBuiltinOption); |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 58 | |
Keith Davis | 892fafe | 2020-11-26 17:40:35 +0000 | [diff] [blame] | 59 | const std::vector<int> subgraphInputs{0}; |
| 60 | const std::vector<int> subgraphOutputs{1}; |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 61 | flatbuffers::Offset <SubGraph> subgraph = |
| 62 | CreateSubGraph(flatBufferBuilder, |
| 63 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 64 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 65 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
| 66 | flatBufferBuilder.CreateVector(&unaryOperator, 1)); |
| 67 | |
| 68 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 69 | flatBufferBuilder.CreateString("ArmnnDelegate: Activation Operator Model"); |
Teresa Charlin | 077cddb | 2023-09-15 15:19:21 +0100 | [diff] [blame] | 70 | flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, |
| 71 | activationOperatorCode, |
| 72 | 0, |
| 73 | 1, |
| 74 | activationOperatorCode); |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 75 | |
| 76 | flatbuffers::Offset <Model> flatbufferModel = |
| 77 | CreateModel(flatBufferBuilder, |
| 78 | TFLITE_SCHEMA_VERSION, |
| 79 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 80 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 81 | modelDescription, |
| 82 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 83 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 84 | flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER); |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 85 | |
| 86 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 87 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 88 | } |
| 89 | |
| 90 | void ActivationTest(tflite::BuiltinOperator activationOperatorCode, |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 91 | std::vector<float>& inputValues, |
Tianle Cheng | ae93173 | 2023-07-28 11:53:04 +0100 | [diff] [blame] | 92 | std::vector<float>& expectedOutputValues, |
Colm Donelan | eff204a | 2023-11-28 15:46:09 +0000 | [diff] [blame] | 93 | float alpha = 0, |
| 94 | const std::vector<armnn::BackendId>& backends = {}) |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 95 | { |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 96 | using namespace delegateTestInterpreter; |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 97 | std::vector<int32_t> inputShape { { 4, 1, 4} }; |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 98 | std::vector<char> modelBuffer = CreateActivationTfLiteModel(activationOperatorCode, |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 99 | ::tflite::TensorType_FLOAT32, |
Tianle Cheng | ae93173 | 2023-07-28 11:53:04 +0100 | [diff] [blame] | 100 | inputShape, |
| 101 | alpha); |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 102 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 103 | // Setup interpreter with just TFLite Runtime. |
| 104 | auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer); |
| 105 | CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk); |
| 106 | CHECK(tfLiteInterpreter.FillInputTensor<float>(inputValues, 0) == kTfLiteOk); |
| 107 | CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk); |
| 108 | std::vector<float> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<float>(0); |
| 109 | std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0); |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 110 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 111 | // Setup interpreter with Arm NN Delegate applied. |
Colm Donelan | eff204a | 2023-11-28 15:46:09 +0000 | [diff] [blame] | 112 | auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, CaptureAvailableBackends(backends)); |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 113 | CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk); |
| 114 | CHECK(armnnInterpreter.FillInputTensor<float>(inputValues, 0) == kTfLiteOk); |
| 115 | CHECK(armnnInterpreter.Invoke() == kTfLiteOk); |
| 116 | std::vector<float> armnnOutputValues = armnnInterpreter.GetOutputResult<float>(0); |
| 117 | std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(0); |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 118 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 119 | armnnDelegate::CompareOutputData<float>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues); |
| 120 | armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, inputShape); |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 121 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 122 | tfLiteInterpreter.Cleanup(); |
| 123 | armnnInterpreter.Cleanup(); |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | } // anonymous namespace |