blob: ff03127d93369f2ab12a6750bd65b2ab954d42f6 [file] [log] [blame]
David Monahan0cf84422020-11-16 15:53:03 +00001//
Colm Donelan7bcae3c2024-01-22 10:07:14 +00002// Copyright © 2020, 2023-2024 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
David Monahan0cf84422020-11-16 15:53:03 +000013#include <tensorflow/lite/version.h>
14
David Monahan0cf84422020-11-16 15:53:03 +000015namespace
16{
17
18std::vector<char> CreateActivationTfLiteModel(tflite::BuiltinOperator activationOperatorCode,
19 tflite::TensorType tensorType,
Tianle Chengae931732023-07-28 11:53:04 +010020 const std::vector <int32_t>& tensorShape,
21 float alpha = 0)
David Monahan0cf84422020-11-16 15:53:03 +000022{
23 using namespace tflite;
24 flatbuffers::FlatBufferBuilder flatBufferBuilder;
25
26 std::array<flatbuffers::Offset<tflite::Buffer>, 1> buffers;
Ryan OShea238ecd92023-03-07 11:44:23 +000027 buffers[0] = CreateBuffer(flatBufferBuilder);
David Monahan0cf84422020-11-16 15:53:03 +000028
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 Davis892fafe2020-11-26 17:40:35 +000038 const std::vector<int> operatorInputs{0};
39 const std::vector<int> operatorOutputs{1};
Tianle Chengae931732023-07-28 11:53:04 +010040
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 Monahan0cf84422020-11-16 15:53:03 +000051 flatbuffers::Offset <Operator> unaryOperator =
52 CreateOperator(flatBufferBuilder,
53 0,
54 flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()),
Tianle Chengae931732023-07-28 11:53:04 +010055 flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()),
56 operatorBuiltinOptionsType,
57 operatorBuiltinOption);
David Monahan0cf84422020-11-16 15:53:03 +000058
Keith Davis892fafe2020-11-26 17:40:35 +000059 const std::vector<int> subgraphInputs{0};
60 const std::vector<int> subgraphOutputs{1};
David Monahan0cf84422020-11-16 15:53:03 +000061 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 Charlin077cddb2023-09-15 15:19:21 +010070 flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder,
71 activationOperatorCode,
72 0,
73 1,
74 activationOperatorCode);
David Monahan0cf84422020-11-16 15:53:03 +000075
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 Sloyanebe392d2023-03-30 10:12:08 +010084 flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER);
David Monahan0cf84422020-11-16 15:53:03 +000085
86 return std::vector<char>(flatBufferBuilder.GetBufferPointer(),
87 flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize());
88}
89
90void ActivationTest(tflite::BuiltinOperator activationOperatorCode,
David Monahan0cf84422020-11-16 15:53:03 +000091 std::vector<float>& inputValues,
Tianle Chengae931732023-07-28 11:53:04 +010092 std::vector<float>& expectedOutputValues,
Colm Donelaneff204a2023-11-28 15:46:09 +000093 float alpha = 0,
94 const std::vector<armnn::BackendId>& backends = {})
David Monahan0cf84422020-11-16 15:53:03 +000095{
Matthew Sloyanebe392d2023-03-30 10:12:08 +010096 using namespace delegateTestInterpreter;
Matthew Sloyan7515d072020-12-16 12:50:01 +000097 std::vector<int32_t> inputShape { { 4, 1, 4} };
David Monahan0cf84422020-11-16 15:53:03 +000098 std::vector<char> modelBuffer = CreateActivationTfLiteModel(activationOperatorCode,
Matthew Sloyanebe392d2023-03-30 10:12:08 +010099 ::tflite::TensorType_FLOAT32,
Tianle Chengae931732023-07-28 11:53:04 +0100100 inputShape,
101 alpha);
David Monahan0cf84422020-11-16 15:53:03 +0000102
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100103 // 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 Monahan0cf84422020-11-16 15:53:03 +0000110
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100111 // Setup interpreter with Arm NN Delegate applied.
Colm Donelaneff204a2023-11-28 15:46:09 +0000112 auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, CaptureAvailableBackends(backends));
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100113 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 Monahan0cf84422020-11-16 15:53:03 +0000118
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100119 armnnDelegate::CompareOutputData<float>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues);
120 armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, inputShape);
David Monahan0cf84422020-11-16 15:53:03 +0000121
Matthew Sloyanebe392d2023-03-30 10:12:08 +0100122 tfLiteInterpreter.Cleanup();
123 armnnInterpreter.Cleanup();
David Monahan0cf84422020-11-16 15:53:03 +0000124}
125
126} // anonymous namespace