blob: 1813ef4c842db756c17d4051e8d56e8073fb844f [file] [log] [blame]
Georgios Pinitasdc460f12017-08-24 19:02:44 +01001/*
Pablo Telloa52e4cf2019-04-01 14:55:18 +01002 * Copyright (c) 2017-2019 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);
55 const QuantizationInfo input_qinfo(1.f / 255.f, offset_dis(gen));
56 const QuantizationInfo output_qinfo(1.f / 255.f, offset_dis(gen));
Georgios Pinitasdc460f12017-08-24 19:02:44 +010057
Pablo Telloa52e4cf2019-04-01 14:55:18 +010058 _pool_info = pool_info;
59 _target = compute_target(shape, pool_info, data_type, data_layout, input_qinfo, output_qinfo);
60 _reference = compute_reference(shape, pool_info, data_type, input_qinfo, output_qinfo);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010061 }
62
63protected:
64 template <typename U>
65 void fill(U &&tensor)
66 {
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000067 if(!is_data_type_quantized(tensor.data_type()))
Georgios Pinitasdc460f12017-08-24 19:02:44 +010068 {
69 std::uniform_real_distribution<> distribution(-1.f, 1.f);
70 library->fill(tensor, distribution, 0);
71 }
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010072 else // data type is quantized_asymmetric
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000073 {
74 library->fill_tensor_uniform(tensor, 0);
75 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +010076 }
77
Giorgio Arena563494c2018-04-30 17:29:41 +010078 TensorType compute_target(TensorShape shape, PoolingLayerInfo info,
Pablo Telloa52e4cf2019-04-01 14:55:18 +010079 DataType data_type, DataLayout data_layout, QuantizationInfo input_qinfo, QuantizationInfo output_qinfo)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010080 {
Giorgio Arena563494c2018-04-30 17:29:41 +010081 // Change shape in case of NHWC.
82 if(data_layout == DataLayout::NHWC)
83 {
84 permute(shape, PermutationVector(2U, 0U, 1U));
85 }
86
Georgios Pinitasdc460f12017-08-24 19:02:44 +010087 // Create tensors
Pablo Telloa52e4cf2019-04-01 14:55:18 +010088 TensorType src = create_tensor<TensorType>(shape, data_type, 1, input_qinfo, data_layout);
89 const TensorShape dst_shape = misc::shape_calculator::compute_pool_shape(*(src.info()), info);
90 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, output_qinfo, data_layout);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010091
92 // Create and configure function
93 FunctionType pool_layer;
94 pool_layer.configure(&src, &dst, info);
95
96 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
97 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
98
99 // Allocate tensors
100 src.allocator()->allocate();
101 dst.allocator()->allocate();
102
103 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
104 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
105
106 // Fill tensors
107 fill(AccessorType(src));
108
109 // Compute function
110 pool_layer.run();
111
112 return dst;
113 }
114
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100115 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 +0100116 {
117 // Create reference
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100118 SimpleTensor<T> src{ shape, data_type, 1, input_qinfo };
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100119
120 // Fill reference
121 fill(src);
122
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100123 return reference::pooling_layer<T>(src, info, output_qinfo);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100124 }
125
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000126 TensorType _target{};
127 SimpleTensor<T> _reference{};
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000128 PoolingLayerInfo _pool_info{};
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100129};
130
131template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000132class PoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100133{
134public:
135 template <typename...>
Michalis Spyrou57dac842018-03-01 16:03:50 +0000136 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 +0100137 {
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000138 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, pad_stride_info, exclude_padding),
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100139 data_type, data_layout);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100140 }
141};
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100142
143template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000144class PoolingLayerValidationQuantizedFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
145{
146public:
147 template <typename...>
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100148 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 +0000149 {
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000150 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, pad_stride_info, exclude_padding),
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100151 data_type, data_layout);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000152 }
153};
154
155template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Georgios Pinitas15997872018-02-19 13:58:22 +0000156class SpecialPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
157{
158public:
159 template <typename...>
Giorgio Arena3c520c52018-05-01 11:47:24 +0100160 void setup(TensorShape src_shape, PoolingLayerInfo pool_info, DataType data_type)
Georgios Pinitas15997872018-02-19 13:58:22 +0000161 {
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100162 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, pool_info, data_type, DataLayout::NCHW);
Georgios Pinitas15997872018-02-19 13:58:22 +0000163 }
164};
165
166template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000167class GlobalPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100168{
169public:
170 template <typename...>
Michalis Spyrou57dac842018-03-01 16:03:50 +0000171 void setup(TensorShape shape, PoolingType pool_type, DataType data_type, DataLayout data_layout = DataLayout::NCHW)
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100172 {
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100173 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type), data_type, DataLayout::NCHW);
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100174 }
175};
Georgios Pinitas15997872018-02-19 13:58:22 +0000176
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100177} // namespace validation
178} // namespace test
179} // namespace arm_compute
180#endif /* ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE */