blob: 3e34f98271669a5f16869d1172808aecd0408862 [file] [log] [blame]
Georgios Pinitasdc460f12017-08-24 19:02:44 +01001/*
Isabella Gottardi6e464c32018-01-26 12:32:45 +00002 * Copyright (c) 2017-2018 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"
29#include "arm_compute/runtime/Tensor.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010030#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/PoolingLayer.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010036
37#include <random>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000046class PoolingLayerValidationGenericFixture : public framework::Fixture
Georgios Pinitasdc460f12017-08-24 19:02:44 +010047{
48public:
49 template <typename...>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010050 void setup(TensorShape shape, PoolingLayerInfo pool_info, DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010051 {
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000052 _quantization_info = quantization_info;
Georgios Pinitas4c2dd542017-11-13 12:58:41 +000053 _pool_info = pool_info;
Georgios Pinitasdc460f12017-08-24 19:02:44 +010054
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010055 _target = compute_target(shape, pool_info, data_type, data_layout, quantization_info);
56 _reference = compute_reference(shape, pool_info, data_type, quantization_info);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010057 }
58
59protected:
60 template <typename U>
61 void fill(U &&tensor)
62 {
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000063 if(!is_data_type_quantized(tensor.data_type()))
Georgios Pinitasdc460f12017-08-24 19:02:44 +010064 {
65 std::uniform_real_distribution<> distribution(-1.f, 1.f);
66 library->fill(tensor, distribution, 0);
67 }
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010068 else // data type is quantized_asymmetric
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000069 {
70 library->fill_tensor_uniform(tensor, 0);
71 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +010072 }
73
Giorgio Arena563494c2018-04-30 17:29:41 +010074 TensorType compute_target(TensorShape shape, PoolingLayerInfo info,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010075 DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010076 {
Giorgio Arena563494c2018-04-30 17:29:41 +010077 // Change shape in case of NHWC.
78 if(data_layout == DataLayout::NHWC)
79 {
80 permute(shape, PermutationVector(2U, 0U, 1U));
81 }
82
Georgios Pinitasdc460f12017-08-24 19:02:44 +010083 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010084 TensorType src = create_tensor<TensorType>(shape, data_type, 1, quantization_info, data_layout);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010085 TensorType dst;
86
87 // Create and configure function
88 FunctionType pool_layer;
89 pool_layer.configure(&src, &dst, info);
90
91 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
92 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
93
94 // Allocate tensors
95 src.allocator()->allocate();
96 dst.allocator()->allocate();
97
98 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
99 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
100
101 // Fill tensors
102 fill(AccessorType(src));
103
104 // Compute function
105 pool_layer.run();
106
107 return dst;
108 }
109
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000110 SimpleTensor<T> compute_reference(const TensorShape &shape, PoolingLayerInfo info,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100111 DataType data_type, QuantizationInfo quantization_info)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100112 {
113 // Create reference
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100114 SimpleTensor<T> src{ shape, data_type, 1, quantization_info };
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100115
116 // Fill reference
117 fill(src);
118
119 return reference::pooling_layer<T>(src, info);
120 }
121
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000122 TensorType _target{};
123 SimpleTensor<T> _reference{};
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000124 QuantizationInfo _quantization_info{};
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000125 PoolingLayerInfo _pool_info{};
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100126};
127
128template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000129class PoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100130{
131public:
132 template <typename...>
Michalis Spyrou57dac842018-03-01 16:03:50 +0000133 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 +0100134 {
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000135 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, pad_stride_info, exclude_padding),
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100136 data_type, data_layout, QuantizationInfo());
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100137 }
138};
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100139
140template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000141class PoolingLayerValidationQuantizedFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
142{
143public:
144 template <typename...>
Michalis Spyrou57dac842018-03-01 16:03:50 +0000145 void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type,
146 QuantizationInfo quantization_info, DataLayout data_layout = DataLayout::NCHW)
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000147 {
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000148 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, pad_stride_info, exclude_padding),
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100149 data_type, data_layout, quantization_info);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000150 }
151};
152
153template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Georgios Pinitas15997872018-02-19 13:58:22 +0000154class SpecialPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
155{
156public:
157 template <typename...>
Giorgio Arena3c520c52018-05-01 11:47:24 +0100158 void setup(TensorShape src_shape, PoolingLayerInfo pool_info, DataType data_type)
Georgios Pinitas15997872018-02-19 13:58:22 +0000159 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100160 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, pool_info, data_type, DataLayout::NCHW, QuantizationInfo());
Georgios Pinitas15997872018-02-19 13:58:22 +0000161 }
162};
163
164template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000165class GlobalPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100166{
167public:
168 template <typename...>
Michalis Spyrou57dac842018-03-01 16:03:50 +0000169 void setup(TensorShape shape, PoolingType pool_type, DataType data_type, DataLayout data_layout = DataLayout::NCHW)
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100170 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100171 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type), data_type, DataLayout::NCHW, QuantizationInfo());
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100172 }
173};
Georgios Pinitas15997872018-02-19 13:58:22 +0000174
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100175} // namespace validation
176} // namespace test
177} // namespace arm_compute
178#endif /* ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE */