blob: 086bd6cd5831b5e236ca2e28eabe05a046ecaa80 [file] [log] [blame]
morgolock37722d92020-04-09 14:17:48 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2020 Arm Limited.
morgolock37722d92020-04-09 14:17:48 +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/core/utils/misc/ShapeCalculator.h"
30#include "arm_compute/runtime/Tensor.h"
31#include "tests/AssetsLibrary.h"
32#include "tests/Globals.h"
33#include "tests/IAccessor.h"
34#include "tests/framework/Asserts.h"
35#include "tests/framework/Fixture.h"
36#include "tests/validation/reference/MaxUnpoolingLayer.h"
37#include "tests/validation/reference/PoolingLayer.h"
38#include <random>
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename PoolingFunctionType, typename MaxUnpoolingFunctionType, typename T>
46class MaxUnpoolingLayerValidationGenericFixture : public framework::Fixture
47{
48public:
49 template <typename...>
50 void setup(TensorShape shape, PoolingLayerInfo pool_info, DataType data_type, DataLayout data_layout)
51 {
52 std::mt19937 gen(library->seed());
53 std::uniform_int_distribution<> offset_dis(0, 20);
54 const float scale = data_type == DataType::QASYMM8_SIGNED ? 1.f / 127.f : 1.f / 255.f;
55 const int scale_in = data_type == DataType::QASYMM8_SIGNED ? -offset_dis(gen) : offset_dis(gen);
56 const int scale_out = data_type == DataType::QASYMM8_SIGNED ? -offset_dis(gen) : offset_dis(gen);
57 const QuantizationInfo input_qinfo(scale, scale_in);
58 const QuantizationInfo output_qinfo(scale, scale_out);
59 _pool_info = pool_info;
60 _target = compute_target(shape, pool_info, data_type, data_layout, input_qinfo, output_qinfo);
61 _reference = compute_reference(shape, pool_info, data_type, input_qinfo, output_qinfo);
62 }
63
64protected:
65 template <typename U>
66 void fill(U &&tensor)
67 {
68 if(!is_data_type_quantized(tensor.data_type()))
69 {
70 std::uniform_real_distribution<> distribution(-1.f, 1.f);
71 library->fill(tensor, distribution, 0);
72 }
73 else // data type is quantized_asymmetric
74 {
75 library->fill_tensor_uniform(tensor, 0);
76 }
77 }
78
79 TensorType compute_target(TensorShape input_shape, PoolingLayerInfo pool_info,
80 DataType data_type, DataLayout data_layout,
81 QuantizationInfo input_qinfo, QuantizationInfo output_qinfo)
82 {
83 // Change shape in case of NHWC.
84 if(data_layout == DataLayout::NHWC)
85 {
86 permute(input_shape, PermutationVector(2U, 0U, 1U));
87 }
88
89 // Create tensors
90 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, input_qinfo, data_layout);
91 const TensorShape dst_shape = misc::shape_calculator::compute_pool_shape(*(src.info()), pool_info);
92 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, output_qinfo, data_layout);
93 TensorType unpooled = create_tensor<TensorType>(input_shape, data_type, 1, output_qinfo, data_layout);
94 TensorType indices = create_tensor<TensorType>(dst_shape, DataType::U32, 1, output_qinfo, data_layout);
95
96 // Create and configure function
97 PoolingFunctionType pool_layer;
98 pool_layer.configure(&src, &dst, pool_info, &indices);
99 // Create and configure function
100
101 MaxUnpoolingFunctionType unpool_layer;
102 unpool_layer.configure(&dst, &indices, &unpooled, pool_info);
103
104 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
105 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
106 ARM_COMPUTE_EXPECT(indices.info()->is_resizable(), framework::LogLevel::ERRORS);
107
108 // Allocate tensors
109 src.allocator()->allocate();
110 dst.allocator()->allocate();
111 indices.allocator()->allocate();
112 unpooled.allocator()->allocate();
113
114 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
115 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
116 ARM_COMPUTE_EXPECT(!indices.info()->is_resizable(), framework::LogLevel::ERRORS);
117 ARM_COMPUTE_EXPECT(!unpooled.info()->is_resizable(), framework::LogLevel::ERRORS);
118
119 // Fill tensors
120 fill(AccessorType(src));
121
122 // Compute function
123 pool_layer.run();
124 unpool_layer.run();
125 return unpooled;
126 }
127
128 SimpleTensor<T> compute_reference(TensorShape input_shape, PoolingLayerInfo info, DataType data_type,
129 QuantizationInfo input_qinfo, QuantizationInfo output_qinfo)
130 {
131 SimpleTensor<T> src(input_shape, data_type, 1, input_qinfo);
132 SimpleTensor<uint32_t> indices{};
133 // Fill reference
134 fill(src);
135 auto pooled_tensor = reference::pooling_layer<T>(src, info, output_qinfo, &indices);
136 return reference::max_unpooling_layer<T>(pooled_tensor, info, output_qinfo, indices, input_shape);
137 }
138
139 TensorType _target{};
140 SimpleTensor<T> _reference{};
141 PoolingLayerInfo _pool_info{};
142};
143
144template <typename TensorType, typename AccessorType, typename F1, typename F2, typename T>
145class MaxUnpoolingLayerValidationFixture : public MaxUnpoolingLayerValidationGenericFixture<TensorType, AccessorType, F1, F2, T>
146{
147public:
148 template <typename...>
149 void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, DataType data_type, DataLayout data_layout)
150 {
151 MaxUnpoolingLayerValidationGenericFixture<TensorType, AccessorType, F1, F2, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, true),
152 data_type, data_layout);
153 }
154};
155
156} // namespace validation
157} // namespace test
158} // namespace arm_compute
159#endif /* ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE */