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