blob: c023696bab0f0c8074a97d2f7283700a66c5eee5 [file] [log] [blame]
David Monahan0cf84422020-11-16 15:53:03 +00001//
Ryan OShea238ecd92023-03-07 11:44:23 +00002// Copyright © 2020, 2023 Arm Ltd and Contributors. All rights reserved.
David Monahan0cf84422020-11-16 15:53:03 +00003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Matthew Sloyan7515d072020-12-16 12:50:01 +00008#include "TestUtils.hpp"
9
David Monahan0cf84422020-11-16 15:53:03 +000010#include <armnn_delegate.hpp>
Matthew Sloyanebe392d2023-03-30 10:12:08 +010011#include <DelegateTestInterpreter.hpp>
David Monahan0cf84422020-11-16 15:53:03 +000012
13#include <flatbuffers/flatbuffers.h>
David Monahan0cf84422020-11-16 15:53:03 +000014#include <tensorflow/lite/kernels/register.h>
David Monahan0cf84422020-11-16 15:53:03 +000015#include <tensorflow/lite/version.h>
16
17#include <doctest/doctest.h>
18
19namespace
20{
21
22std::vector<char> CreateActivationTfLiteModel(tflite::BuiltinOperator activationOperatorCode,
23 tflite::TensorType tensorType,
Tianle Chengae931732023-07-28 11:53:04 +010024 const std::vector <int32_t>& tensorShape,
25 float alpha = 0)
David Monahan0cf84422020-11-16 15:53:03 +000026{
27 using namespace tflite;
28 flatbuffers::FlatBufferBuilder flatBufferBuilder;
29
30 std::array<flatbuffers::Offset<tflite::Buffer>, 1> buffers;
Ryan OShea238ecd92023-03-07 11:44:23 +000031 buffers[0] = CreateBuffer(flatBufferBuilder);
David Monahan0cf84422020-11-16 15:53:03 +000032
33 std::array<flatbuffers::Offset<Tensor>, 2> tensors;
34 tensors[0] = CreateTensor(flatBufferBuilder,
35 flatBufferBuilder.CreateVector<int32_t>(tensorShape.data(), tensorShape.size()),
36 tensorType);
37 tensors[1] = CreateTensor(flatBufferBuilder,
38 flatBufferBuilder.CreateVector<int32_t>(tensorShape.data(), tensorShape.size()),
39 tensorType);
40
41 // create operator
Keith Davis892fafe2020-11-26 17:40:35 +000042 const std::vector<int> operatorInputs{0};
43 const std::vector<int> operatorOutputs{1};
Tianle Chengae931732023-07-28 11:53:04 +010044
45 // builtin options
46 tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_NONE;
47 flatbuffers::Offset<void> operatorBuiltinOption = 0;
48
49 if (activationOperatorCode == tflite::BuiltinOperator_LEAKY_RELU)
50 {
51 operatorBuiltinOptionsType = tflite::BuiltinOptions_LeakyReluOptions;
52 operatorBuiltinOption = CreateLeakyReluOptions(flatBufferBuilder, alpha).Union();
53 }
54
David Monahan0cf84422020-11-16 15:53:03 +000055 flatbuffers::Offset <Operator> unaryOperator =
56 CreateOperator(flatBufferBuilder,
57 0,
58 flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()),
Tianle Chengae931732023-07-28 11:53:04 +010059 flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()),
60 operatorBuiltinOptionsType,
61 operatorBuiltinOption);
David Monahan0cf84422020-11-16 15:53:03 +000062
Keith Davis892fafe2020-11-26 17:40:35 +000063 const std::vector<int> subgraphInputs{0};
64 const std::vector<int> subgraphOutputs{1};
David Monahan0cf84422020-11-16 15:53:03 +000065 flatbuffers::Offset <SubGraph> subgraph =
66 CreateSubGraph(flatBufferBuilder,
67 flatBufferBuilder.CreateVector(tensors.data(), tensors.size()),
68 flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()),
69 flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()),
70 flatBufferBuilder.CreateVector(&unaryOperator, 1));
71
72 flatbuffers::Offset <flatbuffers::String> modelDescription =
73 flatBufferBuilder.CreateString("ArmnnDelegate: Activation Operator Model");
Teresa Charlin077cddb2023-09-15 15:19:21 +010074 flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder,
75 activationOperatorCode,
76 0,
77 1,
78 activationOperatorCode);
David Monahan0cf84422020-11-16 15:53:03 +000079
80 flatbuffers::Offset <Model> flatbufferModel =
81 CreateModel(flatBufferBuilder,
82 TFLITE_SCHEMA_VERSION,
83 flatBufferBuilder.CreateVector(&operatorCode, 1),
84 flatBufferBuilder.CreateVector(&subgraph, 1),
85 modelDescription,
86 flatBufferBuilder.CreateVector(buffers.data(), buffers.size()));
87
Matthew Sloyanebe392d2023-03-30 10:12:08 +010088 flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER);
David Monahan0cf84422020-11-16 15:53:03 +000089
90 return std::vector<char>(flatBufferBuilder.GetBufferPointer(),
91 flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize());
92}
93
94void ActivationTest(tflite::BuiltinOperator activationOperatorCode,
David Monahan0cf84422020-11-16 15:53:03 +000095 std::vector<float>& inputValues,
Tianle Chengae931732023-07-28 11:53:04 +010096 std::vector<float>& expectedOutputValues,
Colm Donelaneff204a2023-11-28 15:46:09 +000097 float alpha = 0,
98 const std::vector<armnn::BackendId>& backends = {})
David Monahan0cf84422020-11-16 15:53:03 +000099{
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100100 using namespace delegateTestInterpreter;
Matthew Sloyan7515d072020-12-16 12:50:01 +0000101 std::vector<int32_t> inputShape { { 4, 1, 4} };
David Monahan0cf84422020-11-16 15:53:03 +0000102 std::vector<char> modelBuffer = CreateActivationTfLiteModel(activationOperatorCode,
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100103 ::tflite::TensorType_FLOAT32,
Tianle Chengae931732023-07-28 11:53:04 +0100104 inputShape,
105 alpha);
David Monahan0cf84422020-11-16 15:53:03 +0000106
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100107 // Setup interpreter with just TFLite Runtime.
108 auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer);
109 CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk);
110 CHECK(tfLiteInterpreter.FillInputTensor<float>(inputValues, 0) == kTfLiteOk);
111 CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk);
112 std::vector<float> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<float>(0);
113 std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0);
David Monahan0cf84422020-11-16 15:53:03 +0000114
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100115 // Setup interpreter with Arm NN Delegate applied.
Colm Donelaneff204a2023-11-28 15:46:09 +0000116 auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, CaptureAvailableBackends(backends));
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100117 CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk);
118 CHECK(armnnInterpreter.FillInputTensor<float>(inputValues, 0) == kTfLiteOk);
119 CHECK(armnnInterpreter.Invoke() == kTfLiteOk);
120 std::vector<float> armnnOutputValues = armnnInterpreter.GetOutputResult<float>(0);
121 std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(0);
David Monahan0cf84422020-11-16 15:53:03 +0000122
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100123 armnnDelegate::CompareOutputData<float>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues);
124 armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, inputShape);
David Monahan0cf84422020-11-16 15:53:03 +0000125
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100126 tfLiteInterpreter.Cleanup();
127 armnnInterpreter.Cleanup();
David Monahan0cf84422020-11-16 15:53:03 +0000128}
129
130} // anonymous namespace