blob: 7beb53ba3d4e131a4a3fc903a6a7049dce9895a3 [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
Matthew Sloyanebe392d2023-03-30 10:12:08 +010017#include <schema_generated.h>
18
David Monahan0cf84422020-11-16 15:53:03 +000019#include <doctest/doctest.h>
20
21namespace
22{
23
24std::vector<char> CreateActivationTfLiteModel(tflite::BuiltinOperator activationOperatorCode,
25 tflite::TensorType tensorType,
Tianle Chengae931732023-07-28 11:53:04 +010026 const std::vector <int32_t>& tensorShape,
27 float alpha = 0)
David Monahan0cf84422020-11-16 15:53:03 +000028{
29 using namespace tflite;
30 flatbuffers::FlatBufferBuilder flatBufferBuilder;
31
32 std::array<flatbuffers::Offset<tflite::Buffer>, 1> buffers;
Ryan OShea238ecd92023-03-07 11:44:23 +000033 buffers[0] = CreateBuffer(flatBufferBuilder);
David Monahan0cf84422020-11-16 15:53:03 +000034
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 Davis892fafe2020-11-26 17:40:35 +000044 const std::vector<int> operatorInputs{0};
45 const std::vector<int> operatorOutputs{1};
Tianle Chengae931732023-07-28 11:53:04 +010046
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 Monahan0cf84422020-11-16 15:53:03 +000057 flatbuffers::Offset <Operator> unaryOperator =
58 CreateOperator(flatBufferBuilder,
59 0,
60 flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()),
Tianle Chengae931732023-07-28 11:53:04 +010061 flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()),
62 operatorBuiltinOptionsType,
63 operatorBuiltinOption);
David Monahan0cf84422020-11-16 15:53:03 +000064
Keith Davis892fafe2020-11-26 17:40:35 +000065 const std::vector<int> subgraphInputs{0};
66 const std::vector<int> subgraphOutputs{1};
David Monahan0cf84422020-11-16 15:53:03 +000067 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");
Teresa Charlin077cddb2023-09-15 15:19:21 +010076 flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder,
77 activationOperatorCode,
78 0,
79 1,
80 activationOperatorCode);
David Monahan0cf84422020-11-16 15:53:03 +000081
82 flatbuffers::Offset <Model> flatbufferModel =
83 CreateModel(flatBufferBuilder,
84 TFLITE_SCHEMA_VERSION,
85 flatBufferBuilder.CreateVector(&operatorCode, 1),
86 flatBufferBuilder.CreateVector(&subgraph, 1),
87 modelDescription,
88 flatBufferBuilder.CreateVector(buffers.data(), buffers.size()));
89
Matthew Sloyanebe392d2023-03-30 10:12:08 +010090 flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER);
David Monahan0cf84422020-11-16 15:53:03 +000091
92 return std::vector<char>(flatBufferBuilder.GetBufferPointer(),
93 flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize());
94}
95
96void ActivationTest(tflite::BuiltinOperator activationOperatorCode,
97 std::vector<armnn::BackendId>& backends,
98 std::vector<float>& inputValues,
Tianle Chengae931732023-07-28 11:53:04 +010099 std::vector<float>& expectedOutputValues,
100 float alpha = 0)
David Monahan0cf84422020-11-16 15:53:03 +0000101{
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100102 using namespace delegateTestInterpreter;
Matthew Sloyan7515d072020-12-16 12:50:01 +0000103 std::vector<int32_t> inputShape { { 4, 1, 4} };
David Monahan0cf84422020-11-16 15:53:03 +0000104 std::vector<char> modelBuffer = CreateActivationTfLiteModel(activationOperatorCode,
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100105 ::tflite::TensorType_FLOAT32,
Tianle Chengae931732023-07-28 11:53:04 +0100106 inputShape,
107 alpha);
David Monahan0cf84422020-11-16 15:53:03 +0000108
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100109 // Setup interpreter with just TFLite Runtime.
110 auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer);
111 CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk);
112 CHECK(tfLiteInterpreter.FillInputTensor<float>(inputValues, 0) == kTfLiteOk);
113 CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk);
114 std::vector<float> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<float>(0);
115 std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0);
David Monahan0cf84422020-11-16 15:53:03 +0000116
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100117 // Setup interpreter with Arm NN Delegate applied.
118 auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, backends);
119 CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk);
120 CHECK(armnnInterpreter.FillInputTensor<float>(inputValues, 0) == kTfLiteOk);
121 CHECK(armnnInterpreter.Invoke() == kTfLiteOk);
122 std::vector<float> armnnOutputValues = armnnInterpreter.GetOutputResult<float>(0);
123 std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(0);
David Monahan0cf84422020-11-16 15:53:03 +0000124
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100125 armnnDelegate::CompareOutputData<float>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues);
126 armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, inputShape);
David Monahan0cf84422020-11-16 15:53:03 +0000127
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100128 tfLiteInterpreter.Cleanup();
129 armnnInterpreter.Cleanup();
David Monahan0cf84422020-11-16 15:53:03 +0000130}
131
132} // anonymous namespace