blob: 17229cac2579c8a8f7dbe9b14e400b435f0f1050 [file] [log] [blame]
Pablo Tello89519332017-11-17 11:52:36 +00001/*
Pablo Tellod6ca4782018-01-23 09:36:04 +00002 * Copyright (c) 2017-2018 ARM Limited.
Pablo Tello89519332017-11-17 11:52:36 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ARM_COMPUTE_TEST_WINOGRAD_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_WINOGRAD_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Pablo Tello89519332017-11-17 11:52:36 +000030#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
33#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
Pablo Tello89519332017-11-17 11:52:36 +000035#include "tests/validation/Helpers.h"
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000036#include "tests/validation/reference/ActivationLayer.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000037#include "tests/validation/reference/ConvolutionLayer.h"
38#include "tests/validation/reference/Utils.h"
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000039#include "tests/validation/reference/Winograd.h"
Pablo Tello89519332017-11-17 11:52:36 +000040
41#include <random>
42
43namespace arm_compute
44{
Pablo Tello89519332017-11-17 11:52:36 +000045namespace test
46{
47namespace validation
48{
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000049using namespace arm_compute::misc::shape_calculator;
50
Andrew Mundy4d9379a2018-03-15 16:47:03 +000051template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool use_bias = true>
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000052class WinogradConvolutionLayerValidationFixture : public framework::Fixture
Pablo Tello89519332017-11-17 11:52:36 +000053{
54public:
55 template <typename...>
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000056 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, DataType data_type, ActivationLayerInfo act_info)
Pablo Tello89519332017-11-17 11:52:36 +000057 {
Alex Gilday7da29b62018-03-23 14:16:00 +000058 ARM_COMPUTE_UNUSED(dilation);
59
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000060 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, act_info);
61 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, act_info);
Pablo Tello89519332017-11-17 11:52:36 +000062 }
63
64protected:
65 template <typename U>
66 void fill(U &&tensor, int i, float min, float max)
67 {
68 switch(tensor.data_type())
69 {
70 case DataType::F32:
71 {
72 std::uniform_real_distribution<> distribution(min, max);
73 library->fill(tensor, distribution, i);
74 break;
75 }
76 default:
77 {
78 ARM_COMPUTE_ERROR("Not supported");
79 library->fill_tensor_uniform(tensor, i);
80 break;
81 }
82 }
83 }
84
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000085 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000086 DataType data_type, ActivationLayerInfo act_info)
Pablo Tello89519332017-11-17 11:52:36 +000087 {
88 // Create tensors
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000089 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1);
90 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1);
91 TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1);
92 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1);
Pablo Tello89519332017-11-17 11:52:36 +000093
94 // Create and configure function
95 FunctionType conv;
Andrew Mundy4d9379a2018-03-15 16:47:03 +000096 conv.configure(&src, &weights, (use_bias) ? &bias : nullptr, &dst, info, act_info);
Pablo Tello89519332017-11-17 11:52:36 +000097
98 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
99 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
100 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
101 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
102
103 // Allocate tensors
104 src.allocator()->allocate();
105 weights.allocator()->allocate();
Pablo Tello89519332017-11-17 11:52:36 +0000106 dst.allocator()->allocate();
Pablo Tellod6ca4782018-01-23 09:36:04 +0000107 bias.allocator()->allocate();
Pablo Tello89519332017-11-17 11:52:36 +0000108
109 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
110 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
111 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
112 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
113
114 // Fill tensors
115 fill(AccessorType(src), 0, -1.f, 1.f);
116 fill(AccessorType(weights), 1, -1.f, 1.f);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000117 fill(AccessorType(bias), 2, -1.f, 1.f);
Pablo Tello89519332017-11-17 11:52:36 +0000118
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000119 // Compute Winograd Convolution function
Pablo Tello89519332017-11-17 11:52:36 +0000120 conv.run();
121
122 return dst;
123 }
124
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000125 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000126 DataType data_type, ActivationLayerInfo act_info)
Pablo Tello89519332017-11-17 11:52:36 +0000127 {
128 // Create reference
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000129 SimpleTensor<T> src{ input_shape, data_type, 1 };
130 SimpleTensor<T> weights{ weights_shape, data_type, 1 };
131 SimpleTensor<T> bias{ bias_shape, data_type, 1 };
Pablo Tello89519332017-11-17 11:52:36 +0000132
133 // Fill reference
134 fill(src, 0, -1.f, 1.f);
135 fill(weights, 1, -1.f, 1.f);
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000136 if(use_bias)
137 {
138 fill(bias, 2, -1.f, 1.f);
139 }
140 else
141 {
142 fill(bias, 2, 0.f, 0.f);
143 }
Pablo Tello89519332017-11-17 11:52:36 +0000144
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000145 SimpleTensor<T> conv_out = reference::convolution_layer<T>(src, weights, bias, output_shape, info);
146
147 return (act_info.enabled()) ? reference::activation_layer<T>(conv_out, act_info) : conv_out;
Pablo Tello89519332017-11-17 11:52:36 +0000148 }
149
150 TensorType _target{};
151 SimpleTensor<T> _reference{};
Pablo Tello89519332017-11-17 11:52:36 +0000152};
153
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000154template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
155class WinogradInputTransformValidationFixture : public framework::Fixture
156{
157public:
158 template <typename...>
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000159 void setup(TensorShape input_shape, WinogradInfo winograd_info, DataLayout data_layout, DataType data_type)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000160 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000161 TensorShape output_shape = compute_winograd_input_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000162
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000163 _target = compute_target(input_shape, output_shape, winograd_info, data_layout, data_type);
164 _reference = compute_reference(input_shape, output_shape, winograd_info, data_layout, data_type);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000165 }
166
167protected:
168 template <typename U>
169 void fill(U &&tensor, int i, float min, float max)
170 {
171 switch(tensor.data_type())
172 {
173 case DataType::F32:
174 {
175 std::uniform_real_distribution<> distribution(min, max);
176 library->fill(tensor, distribution, i);
177 break;
178 }
179 default:
180 {
181 ARM_COMPUTE_ERROR("Not supported");
182 library->fill_tensor_uniform(tensor, i);
183 break;
184 }
185 }
186 }
187
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000188 TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataLayout data_layout, DataType data_type)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000189 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000190 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, 0, QuantizationInfo(), data_layout);
191 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, 0, QuantizationInfo(), data_layout);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000192
193 // Create and configure function
194 FunctionType transf;
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000195 transf.configure(&src, &dst, winograd_info);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000196
197 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
198 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
199
200 // Allocate tensors
201 src.allocator()->allocate();
202 dst.allocator()->allocate();
203
204 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
205 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
206
207 // Fill tensors
208 fill(AccessorType(src), 0, -1.f, 1.f);
209
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000210 // Compute Winograd input transform function
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000211 transf.run();
212
213 return dst;
214 }
215
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000216 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataLayout data_layout, DataType data_type)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000217 {
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000218 // Create reference
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000219 SimpleTensor<T> src{ input_shape, data_type, 1, 0, QuantizationInfo(), data_layout };
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000220
221 // Fill reference
222 fill(src, 0, -1.f, 1.f);
223
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000224 return reference::winograd_input_transform<T>(src, output_shape, winograd_info);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000225 }
226
227 TensorType _target{};
228 SimpleTensor<T> _reference{};
229};
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000230
231template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
232class WinogradFilterTransformValidationFixture : public framework::Fixture
233{
234public:
235 template <typename...>
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000236 void setup(TensorShape input_shape, Size2D output_tile, DataLayout data_layout, DataType data_type)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000237 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000238 WinogradInfo winograd_info(output_tile, Size2D(input_shape[0], input_shape[1]), Size2D() /* Not needed */, PadStrideInfo() /* Not needed */, DataLayout::NCHW /* Not needed */);
239 TensorShape output_shape = compute_winograd_filter_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info);
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000240
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000241 _target = compute_target(input_shape, output_shape, winograd_info, data_layout, data_type);
242 _reference = compute_reference(input_shape, output_shape, winograd_info, data_layout, data_type);
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000243 }
244
245protected:
246 template <typename U>
247 void fill(U &&tensor, int i, float min, float max)
248 {
249 switch(tensor.data_type())
250 {
251 case DataType::F32:
252 {
253 std::uniform_real_distribution<> distribution(min, max);
254 library->fill(tensor, distribution, i);
255 break;
256 }
257 default:
258 {
259 ARM_COMPUTE_ERROR("Not supported");
260 library->fill_tensor_uniform(tensor, i);
261 break;
262 }
263 }
264 }
265
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000266 TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataLayout data_layout, DataType data_type)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000267 {
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000268 // Create tensors
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000269 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, 0, QuantizationInfo(), data_layout);
270 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, 0, QuantizationInfo(), data_layout);
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000271
272 // Create and configure function
273 FunctionType filter_transform;
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000274 filter_transform.configure(&src, &dst, winograd_info);
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000275
276 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
277 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
278
279 // Allocate tensors
280 src.allocator()->allocate();
281 dst.allocator()->allocate();
282
283 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
284 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
285
286 // Fill tensors
287 fill(AccessorType(src), 0, -1.f, 1.f);
288
289 filter_transform.run();
290
291 return dst;
292 }
293
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000294 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataLayout data_layout, DataType data_type)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000295 {
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000296 // Create reference
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000297 SimpleTensor<T> src{ input_shape, data_type, 1, 0, QuantizationInfo(), data_layout };
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000298
299 // Fill reference
300 fill(src, 0, -1.f, 1.f);
301
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000302 return reference::winograd_filter_transform<T>(src, output_shape, winograd_info);
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000303 }
304
305 TensorType _target{};
306 SimpleTensor<T> _reference{};
307};
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000308
309template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
310class WinogradOutputTransformValidationFixture : public framework::Fixture
311{
312public:
313 template <typename...>
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000314 void setup(TensorShape input_shape, WinogradInfo winograd_info, DataType data_type)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000315 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000316 TensorShape output_shape = compute_winograd_output_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000317
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000318 _target = compute_target(input_shape, output_shape, winograd_info, data_type);
319 _reference = compute_reference(input_shape, output_shape, winograd_info, data_type);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000320 }
321
322protected:
323 template <typename U>
324 void fill(U &&tensor, int i, float min, float max)
325 {
326 switch(tensor.data_type())
327 {
328 case DataType::F32:
329 {
330 std::uniform_real_distribution<> distribution(min, max);
331 library->fill(tensor, distribution, i);
332 break;
333 }
334 default:
335 {
336 ARM_COMPUTE_ERROR("Not supported");
337 library->fill_tensor_uniform(tensor, i);
338 break;
339 }
340 }
341 }
342
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000343 TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataType data_type)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000344 {
345 // Create tensors
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000346 TensorType src = create_tensor<TensorType>(input_shape, data_type);
347 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, 0, QuantizationInfo(), winograd_info.output_data_layout);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000348
349 // Create and configure function
350 FunctionType output_transform;
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000351 output_transform.configure(&src, nullptr, &dst, winograd_info);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000352
353 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
354 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
355
356 // Allocate tensors
357 src.allocator()->allocate();
358 dst.allocator()->allocate();
359
360 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
361 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
362
363 // Fill tensors
364 fill(AccessorType(src), 0, -1.f, 1.f);
365
366 output_transform.run();
367
368 return dst;
369 }
370
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000371 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataType data_type)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000372 {
373 // Create reference
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000374 SimpleTensor<T> src{ input_shape, data_type };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000375
376 // Fill reference
377 fill(src, 0, -1.f, 1.f);
378
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000379 return reference::winograd_output_transform<T>(src, output_shape, winograd_info);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000380 }
381
382 TensorType _target{};
383 SimpleTensor<T> _reference{};
384};
Pablo Tello89519332017-11-17 11:52:36 +0000385} // namespace validation
386} // namespace test
387} // namespace arm_compute
388#endif /* ARM_COMPUTE_TEST_WINOGRAD_LAYER_FIXTURE */