blob: ec186564b722f80b43a6cf8dbbb16dd7055315a5 [file] [log] [blame]
Georgios Pinitasdc460f12017-08-24 19:02:44 +01001/*
Manuel Bottinib4bb8272019-12-18 18:01:27 +00002 * Copyright (c) 2017-2020 ARM Limited.
Georgios Pinitasdc460f12017-08-24 19:02:44 +01003 *
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_POOLING_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Pablo Telloa52e4cf2019-04-01 14:55:18 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010030#include "arm_compute/runtime/Tensor.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010031#include "tests/AssetsLibrary.h"
32#include "tests/Globals.h"
33#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010034#include "tests/framework/Asserts.h"
35#include "tests/framework/Fixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000036#include "tests/validation/reference/PoolingLayer.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010037
38#include <random>
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
46template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000047class PoolingLayerValidationGenericFixture : public framework::Fixture
Georgios Pinitasdc460f12017-08-24 19:02:44 +010048{
49public:
50 template <typename...>
Pablo Telloa52e4cf2019-04-01 14:55:18 +010051 void setup(TensorShape shape, PoolingLayerInfo pool_info, DataType data_type, DataLayout data_layout)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010052 {
Pablo Telloa52e4cf2019-04-01 14:55:18 +010053 std::mt19937 gen(library->seed());
54 std::uniform_int_distribution<> offset_dis(0, 20);
Manuel Bottinib4bb8272019-12-18 18:01:27 +000055 const float scale = data_type == DataType::QASYMM8_SIGNED ? 1.f / 127.f : 1.f / 255.f;
56 const int scale_in = data_type == DataType::QASYMM8_SIGNED ? -offset_dis(gen) : offset_dis(gen);
57 const int scale_out = data_type == DataType::QASYMM8_SIGNED ? -offset_dis(gen) : offset_dis(gen);
58 const QuantizationInfo input_qinfo(scale, scale_in);
59 const QuantizationInfo output_qinfo(scale, scale_out);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010060
Pablo Telloa52e4cf2019-04-01 14:55:18 +010061 _pool_info = pool_info;
62 _target = compute_target(shape, pool_info, data_type, data_layout, input_qinfo, output_qinfo);
63 _reference = compute_reference(shape, pool_info, data_type, input_qinfo, output_qinfo);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010064 }
65
66protected:
67 template <typename U>
68 void fill(U &&tensor)
69 {
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000070 if(!is_data_type_quantized(tensor.data_type()))
Georgios Pinitasdc460f12017-08-24 19:02:44 +010071 {
72 std::uniform_real_distribution<> distribution(-1.f, 1.f);
73 library->fill(tensor, distribution, 0);
74 }
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010075 else // data type is quantized_asymmetric
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000076 {
77 library->fill_tensor_uniform(tensor, 0);
78 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +010079 }
80
Giorgio Arena563494c2018-04-30 17:29:41 +010081 TensorType compute_target(TensorShape shape, PoolingLayerInfo info,
Pablo Telloa52e4cf2019-04-01 14:55:18 +010082 DataType data_type, DataLayout data_layout, QuantizationInfo input_qinfo, QuantizationInfo output_qinfo)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010083 {
Giorgio Arena563494c2018-04-30 17:29:41 +010084 // Change shape in case of NHWC.
85 if(data_layout == DataLayout::NHWC)
86 {
87 permute(shape, PermutationVector(2U, 0U, 1U));
88 }
89
Georgios Pinitasdc460f12017-08-24 19:02:44 +010090 // Create tensors
Pablo Telloa52e4cf2019-04-01 14:55:18 +010091 TensorType src = create_tensor<TensorType>(shape, data_type, 1, input_qinfo, data_layout);
92 const TensorShape dst_shape = misc::shape_calculator::compute_pool_shape(*(src.info()), info);
93 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, output_qinfo, data_layout);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010094
95 // Create and configure function
96 FunctionType pool_layer;
97 pool_layer.configure(&src, &dst, info);
98
99 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
100 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
101
102 // Allocate tensors
103 src.allocator()->allocate();
104 dst.allocator()->allocate();
105
106 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
107 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
108
109 // Fill tensors
110 fill(AccessorType(src));
111
112 // Compute function
113 pool_layer.run();
114
115 return dst;
116 }
117
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100118 SimpleTensor<T> compute_reference(const TensorShape &shape, PoolingLayerInfo info, DataType data_type, QuantizationInfo input_qinfo, QuantizationInfo output_qinfo)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100119 {
120 // Create reference
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100121 SimpleTensor<T> src{ shape, data_type, 1, input_qinfo };
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100122
123 // Fill reference
124 fill(src);
125
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100126 return reference::pooling_layer<T>(src, info, output_qinfo);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100127 }
128
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000129 TensorType _target{};
130 SimpleTensor<T> _reference{};
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000131 PoolingLayerInfo _pool_info{};
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100132};
133
134template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000135class PoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100136{
137public:
138 template <typename...>
Michalis Spyrou57dac842018-03-01 16:03:50 +0000139 void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, DataLayout data_layout)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100140 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000141 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding),
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100142 data_type, data_layout);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100143 }
144};
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100145
146template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100147class PoolingLayerValidationMixedPrecisionFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
148{
149public:
150 template <typename...>
151 void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, DataLayout data_layout, bool fp_mixed_precision = false)
152 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000153 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding, fp_mixed_precision),
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100154 data_type, data_layout);
155 }
156};
157
158template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000159class PoolingLayerValidationQuantizedFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
160{
161public:
162 template <typename...>
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100163 void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, DataLayout data_layout = DataLayout::NCHW)
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000164 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000165 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding),
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100166 data_type, data_layout);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000167 }
168};
169
170template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Georgios Pinitas15997872018-02-19 13:58:22 +0000171class SpecialPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
172{
173public:
174 template <typename...>
Giorgio Arena3c520c52018-05-01 11:47:24 +0100175 void setup(TensorShape src_shape, PoolingLayerInfo pool_info, DataType data_type)
Georgios Pinitas15997872018-02-19 13:58:22 +0000176 {
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100177 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, pool_info, data_type, DataLayout::NCHW);
Georgios Pinitas15997872018-02-19 13:58:22 +0000178 }
179};
180
181template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000182class GlobalPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100183{
184public:
185 template <typename...>
Michalis Spyrou57dac842018-03-01 16:03:50 +0000186 void setup(TensorShape shape, PoolingType pool_type, DataType data_type, DataLayout data_layout = DataLayout::NCHW)
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100187 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000188 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, data_layout), data_type, data_layout);
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100189 }
190};
Georgios Pinitas15997872018-02-19 13:58:22 +0000191
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100192} // namespace validation
193} // namespace test
194} // namespace arm_compute
195#endif /* ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE */