blob: e1189367f0f3091cf80aaf904beedd71736fc6fb [file] [log] [blame]
Matthew Sloyan81ec9942021-10-12 10:26:30 +01001//
Teresa Charlinad1b3d72023-03-14 12:10:28 +00002// Copyright © 2021, 2023 Arm Ltd and Contributors. All rights reserved.
Matthew Sloyan81ec9942021-10-12 10:26:30 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "ConvolutionTestHelper.hpp"
7
8#include <armnn_delegate.hpp>
9
10#include <flatbuffers/flatbuffers.h>
11#include <tensorflow/lite/interpreter.h>
12#include <tensorflow/lite/kernels/register.h>
13#include <tensorflow/lite/model.h>
Matthew Sloyan81ec9942021-10-12 10:26:30 +010014
15#include <doctest/doctest.h>
16
17namespace armnnDelegate
18{
19
20// Conv3d is currently only supports Float32 inputs, filter, bias and outputs in TFLite.
21// Conv3d is only correctly supported for external delegates from TF Lite v2.6, as there was a breaking bug in v2.5.
22#if defined(ARMNN_POST_TFLITE_2_5)
23
24// Create a vector from 0 to size divided to create smaller floating point values.
25template <typename T>
26std::vector<T> CreateFloatData(int32_t size, float divisor)
27{
28 std::vector<float> data;
29 for (int32_t i = 0; i < size; ++i)
30 {
31 float value = static_cast<float>(i);
32 data.push_back(value/divisor);
33 }
34 return data;
35}
36
Colm Donelaneff204a2023-11-28 15:46:09 +000037void Conv3DWithBiasesSimpleWithPaddingFp32Test()
Matthew Sloyan81ec9942021-10-12 10:26:30 +010038{
39 // Set input data
40 std::vector<int32_t> inputShape { 1, 2, 2, 2, 1 };
41 std::vector<int32_t> filterShape { 2, 2, 2, 1, 1 };
42 std::vector<int32_t> biasShape { 1 };
43 std::vector<int32_t> outputShape { 1, 2, 2, 2, 1 };
44
45 static std::vector<float> inputValues =
46 {
47 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f
48 };
49
50 std::vector<float> filterValues =
51 {
52 2.f,1.f, 1.f,0.f, 0.f,1.f, 1.f,1.f
53 };
54
55 std::vector<float> biasValues = { 5.f };
56
57 std::vector<float> expectedOutputValues =
58 {
59 33.f, 21.f, 23.f, 13.f, 28.f, 25.f, 27.f, 21.f
60 };
61
62 Convolution3dTest<float>(tflite::BuiltinOperator_CONV_3D,
63 ::tflite::TensorType_FLOAT32,
64 { 1, 1, 1 }, // strideX, strideY, strideZ
65 { 1, 1, 1 }, // dilationX, dilationY, dilationZ
66 tflite::Padding_SAME,
67 tflite::ActivationFunctionType_NONE,
Matthew Sloyan81ec9942021-10-12 10:26:30 +010068 inputShape,
69 filterShape,
70 outputShape,
71 inputValues,
72 filterValues,
73 expectedOutputValues,
74 biasShape,
75 biasValues);
76}
77
Colm Donelaneff204a2023-11-28 15:46:09 +000078void Conv3DWithBiasesStridesFp32Test()
Matthew Sloyan81ec9942021-10-12 10:26:30 +010079{
80 std::vector<int32_t> inputShape { 1, 3, 10, 10, 1 };
81 std::vector<int32_t> filterShape { 3, 5, 5, 1, 1 };
82 std::vector<int32_t> biasShape { 1 };
83 std::vector<int32_t> outputShape { 1, 1, 3, 3, 1 };
84
85 std::vector<float> inputValues = CreateFloatData<float>(300, 1.0f);
86
87 std::vector<float> filterValues =
88 {
89 1.f, 1.f, 1.f, 1.f, 1.f,
90 1.f, 1.f, 1.f, 1.f, 1.f,
91 1.f, 1.f, 1.f, 1.f, 1.f,
92 1.f, 1.f, 1.f, 1.f, 1.f,
93 1.f, 1.f, 1.f, 1.f, 1.f,
94
95 0.f, 0.f, 0.f, 0.f, 0.f,
96 0.f, 0.f, 0.f, 0.f, 0.f,
97 0.f, 0.f, 0.f, 0.f, 0.f,
98 0.f, 0.f, 0.f, 0.f, 0.f,
99 0.f, 0.f, 0.f, 0.f, 0.f,
100
101 2.f, 2.f, 2.f, 2.f, 2.f,
102 2.f, 2.f, 2.f, 2.f, 2.f,
103 2.f, 2.f, 2.f, 2.f, 2.f,
104 2.f, 2.f, 2.f, 2.f, 2.f,
105 2.f, 2.f, 2.f, 2.f, 2.f
106 };
107
108 std::vector<float> biasValues = { 10.f };
109
110 std::vector<float> expectedOutputValues =
111 {
112 11660.f, 11810.f, 11960.f,
113
114 13160.f, 13310.f, 13460.f,
115
116 14660.f, 14810.f, 14960.f
117 };
118
119 Convolution3dTest<float>(tflite::BuiltinOperator_CONV_3D,
120 ::tflite::TensorType_FLOAT32,
121 { 2, 2, 2 }, // strideX, strideY, strideZ
122 { 1, 1, 1 }, // dilationX, dilationY, dilationZ
123 tflite::Padding_VALID,
124 tflite::ActivationFunctionType_NONE,
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100125 inputShape,
126 filterShape,
127 outputShape,
128 inputValues,
129 filterValues,
130 expectedOutputValues,
131 biasShape,
132 biasValues);
133}
134
135
136void Conv3DWithBiasesDilationFp32Test(std::vector<armnn::BackendId>& backends)
137{
138 std::vector<int32_t> inputShape { 1, 5, 5, 5, 2 };
139 std::vector<int32_t> filterShape { 2, 2, 2, 2, 2 };
140 std::vector<int32_t> biasShape { 2 };
141 std::vector<int32_t> outputShape { 1, 2, 2, 2, 2 };
142
143 std::vector<float> inputValues = CreateFloatData<float>(250, 1.0f);
144
145 std::vector<float> filterValues =
146 {
147 -1.f, -1.f, -1.f, -1.f, -1.f, -1.f, -1.f, -1.f, -1.f, -1.f, -1.f, 1.f, 1.f, 1.f, -1.f, -1.f,
148 1.f, 1.f, -1.f, 1.f, -1.f, 1.f, -1.f, 1.f, -1.f, -1.f, -1.f, 1.f, -1.f, 1.f, -1.f, 1.f,
149 };
150
151 std::vector<float> biasValues = { 0.f, 2.f };
152
153 // Since the dilation rate is 3 this will dilate the kernel to be 4x4,
154 // therefore the output will be 2x2
155 std::vector<float> expectedOutputValues =
156 {
157 -1124.f, 976.f,
158 -1148.f, 980.f,
159
160 -1244.f, 996.f,
161 -1268.f, 1000.f,
162
163 -1724.f, 1076.f,
164 -1748.f, 1080.f,
165
166 -1844.f, 1096.f,
167 -1868.f, 1100.f
168 };
169
170 Convolution3dTest<float>(tflite::BuiltinOperator_CONV_3D,
171 ::tflite::TensorType_FLOAT32,
172 { 1, 1, 1 }, // strideX, strideY, strideZ
173 { 3, 3, 3 }, // dilationX, dilationY, dilationZ
174 tflite::Padding_VALID,
175 tflite::ActivationFunctionType_NONE,
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100176 inputShape,
177 filterShape,
178 outputShape,
179 inputValues,
180 filterValues,
181 expectedOutputValues,
182 biasShape,
Colm Donelaneff204a2023-11-28 15:46:09 +0000183 biasValues,
184 {1.0f},
185 {0},
186 {1.0f},
187 {0},
188 2.0f,
189 0,
190 1.0f,
191 0,
192 1,
193 3,
194 backends);
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100195}
196
Colm Donelaneff204a2023-11-28 15:46:09 +0000197void Conv3DFp32SmallTest()
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100198{
199 std::vector<int32_t> inputShape { 1, 3, 10, 10, 1 };
200 std::vector<int32_t> filterShape { 3, 3, 3, 1, 1 };
201 std::vector<int32_t> biasShape { 1 };
202 std::vector<int32_t> outputShape { 1, 1, 4, 4, 1 };
203
204 std::vector<float> inputValues = CreateFloatData<float>(300, 100.0f);
205
206 std::vector<float> filterValues =
207 {
208 0.125977f, 0.150391f, 0.101562f,
209 0.0585938f, 0.0864258f, 0.043457f,
210 0.034668f, 0.0322266f, 0.0385742f,
211
212 0.125977f, 0.150391f, -0.101562f,
213 -0.0585938f,-0.0864258f,-0.043457f,
214 -0.0104630f, 0.0154114f, 0.0013768f,
215
216 0.0344238f, 0.035644f, 0.0495605f,
217 0.0683594f, 0.099121f, -0.0461426f,
218 -0.0996094f,-0.126953f, -0.043457f,
219 };
220
221 std::vector<float> biasValues = { 0 };
222
223 std::vector<float> expectedOutputValues =
224 {
225 -0.08156067f, -0.06891209f, -0.05589598f, -0.04310101f,
226 0.04584253f, 0.05855697f, 0.07129729f, 0.08325434f,
227 0.17304349f, 0.18521416f, 0.19818866f, 0.21096253f,
228 0.29965734f, 0.312698f, 0.32547557f, 0.33818722f
229 };
230
231 Convolution3dTest<float>(tflite::BuiltinOperator_CONV_3D,
232 ::tflite::TensorType_FLOAT32,
233 { 2, 2, 2 }, // strideX, strideY, strideZ
234 { 1, 1, 1 }, // dilationX, dilationY, dilationZ
235 tflite::Padding_VALID,
236 tflite::ActivationFunctionType_NONE,
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100237 inputShape,
238 filterShape,
239 outputShape,
240 inputValues,
241 filterValues,
242 expectedOutputValues,
243 biasShape,
244 biasValues);
245}
246
247TEST_SUITE("Convolution3dTest_CpuRefTests")
248{
249
Colm Donelaneff204a2023-11-28 15:46:09 +0000250TEST_CASE ("Conv3DWithBiasesSimpleWithPadding_Fp32_Test")
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100251{
Colm Donelaneff204a2023-11-28 15:46:09 +0000252 Conv3DWithBiasesSimpleWithPaddingFp32Test();
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100253}
254
Colm Donelaneff204a2023-11-28 15:46:09 +0000255TEST_CASE ("Conv3DWithBiasesStrides_Fp32_Test")
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100256{
Colm Donelaneff204a2023-11-28 15:46:09 +0000257 Conv3DWithBiasesStridesFp32Test();
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100258}
259
260TEST_CASE ("Conv3DWithBiasesDilation_Fp32_CpuRef_Test")
261{
Colm Donelaneff204a2023-11-28 15:46:09 +0000262 // Known to only work on CpuRef.
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100263 std::vector <armnn::BackendId> backends = {armnn::Compute::CpuRef};
264 Conv3DWithBiasesDilationFp32Test(backends);
265}
266
Colm Donelaneff204a2023-11-28 15:46:09 +0000267TEST_CASE ("Conv3DFp32Small_Fp32_Test")
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100268{
Colm Donelaneff204a2023-11-28 15:46:09 +0000269 Conv3DFp32SmallTest();
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100270}
271
Colm Donelaneff204a2023-11-28 15:46:09 +0000272} //End of TEST_SUITE("Convolution3dTest_Tests")
Matthew Sloyan7afc92c2021-11-04 16:52:34 +0000273
Matthew Sloyan81ec9942021-10-12 10:26:30 +0100274#endif
275
276} // namespace armnnDelegate