blob: b0a4d6785d88e13e69e09bdf326fbe41708fe186 [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");
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 Sloyanebe392d2023-03-30 10:12:08 +010086 flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER);
David Monahan0cf84422020-11-16 15:53:03 +000087
88 return std::vector<char>(flatBufferBuilder.GetBufferPointer(),
89 flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize());
90}
91
92void ActivationTest(tflite::BuiltinOperator activationOperatorCode,
93 std::vector<armnn::BackendId>& backends,
94 std::vector<float>& inputValues,
Tianle Chengae931732023-07-28 11:53:04 +010095 std::vector<float>& expectedOutputValues,
96 float alpha = 0)
David Monahan0cf84422020-11-16 15:53:03 +000097{
Matthew Sloyanebe392d2023-03-30 10:12:08 +010098 using namespace delegateTestInterpreter;
Matthew Sloyan7515d072020-12-16 12:50:01 +000099 std::vector<int32_t> inputShape { { 4, 1, 4} };
David Monahan0cf84422020-11-16 15:53:03 +0000100 std::vector<char> modelBuffer = CreateActivationTfLiteModel(activationOperatorCode,
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100101 ::tflite::TensorType_FLOAT32,
Tianle Chengae931732023-07-28 11:53:04 +0100102 inputShape,
103 alpha);
David Monahan0cf84422020-11-16 15:53:03 +0000104
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100105 // 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 Monahan0cf84422020-11-16 15:53:03 +0000112
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100113 // 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 Monahan0cf84422020-11-16 15:53:03 +0000120
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100121 armnnDelegate::CompareOutputData<float>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues);
122 armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, inputShape);
David Monahan0cf84422020-11-16 15:53:03 +0000123
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100124 tfLiteInterpreter.Cleanup();
125 armnnInterpreter.Cleanup();
David Monahan0cf84422020-11-16 15:53:03 +0000126}
127
128} // anonymous namespace