blob: 808e3ffabdfd2ef7f3327e5795fce6d3e4d9bfe1 [file] [log] [blame]
morgolock37722d92020-04-09 14:17:48 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2020-2021, 2023 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:
morgolock37722d92020-04-09 14:17:48 +010049 void setup(TensorShape shape, PoolingLayerInfo pool_info, DataType data_type, DataLayout data_layout)
50 {
51 std::mt19937 gen(library->seed());
52 std::uniform_int_distribution<> offset_dis(0, 20);
53 const float scale = data_type == DataType::QASYMM8_SIGNED ? 1.f / 127.f : 1.f / 255.f;
54 const int scale_in = data_type == DataType::QASYMM8_SIGNED ? -offset_dis(gen) : offset_dis(gen);
55 const int scale_out = data_type == DataType::QASYMM8_SIGNED ? -offset_dis(gen) : offset_dis(gen);
56 const QuantizationInfo input_qinfo(scale, scale_in);
57 const QuantizationInfo output_qinfo(scale, scale_out);
58 _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);
61 }
62
63protected:
64 template <typename U>
65 void fill(U &&tensor)
66 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000067 if(tensor.data_type() == DataType::F32)
morgolock37722d92020-04-09 14:17:48 +010068 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000069 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
70 library->fill(tensor, distribution, 0);
71 }
72 else if(tensor.data_type() == DataType::F16)
73 {
Giorgio Arena33b103b2021-01-08 10:37:15 +000074 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
morgolock37722d92020-04-09 14:17:48 +010075 library->fill(tensor, distribution, 0);
76 }
77 else // data type is quantized_asymmetric
78 {
79 library->fill_tensor_uniform(tensor, 0);
80 }
81 }
82
83 TensorType compute_target(TensorShape input_shape, PoolingLayerInfo pool_info,
84 DataType data_type, DataLayout data_layout,
85 QuantizationInfo input_qinfo, QuantizationInfo output_qinfo)
86 {
87 // Change shape in case of NHWC.
88 if(data_layout == DataLayout::NHWC)
89 {
90 permute(input_shape, PermutationVector(2U, 0U, 1U));
91 }
92
93 // Create tensors
94 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, input_qinfo, data_layout);
95 const TensorShape dst_shape = misc::shape_calculator::compute_pool_shape(*(src.info()), pool_info);
96 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, output_qinfo, data_layout);
97 TensorType unpooled = create_tensor<TensorType>(input_shape, data_type, 1, output_qinfo, data_layout);
98 TensorType indices = create_tensor<TensorType>(dst_shape, DataType::U32, 1, output_qinfo, data_layout);
99
100 // Create and configure function
101 PoolingFunctionType pool_layer;
102 pool_layer.configure(&src, &dst, pool_info, &indices);
103 // Create and configure function
104
105 MaxUnpoolingFunctionType unpool_layer;
106 unpool_layer.configure(&dst, &indices, &unpooled, pool_info);
107
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100108 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
109 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
110 ARM_COMPUTE_ASSERT(indices.info()->is_resizable());
morgolock37722d92020-04-09 14:17:48 +0100111
112 // Allocate tensors
113 src.allocator()->allocate();
114 dst.allocator()->allocate();
115 indices.allocator()->allocate();
116 unpooled.allocator()->allocate();
117
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100118 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
119 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
120 ARM_COMPUTE_ASSERT(!indices.info()->is_resizable());
121 ARM_COMPUTE_ASSERT(!unpooled.info()->is_resizable());
morgolock37722d92020-04-09 14:17:48 +0100122
123 // Fill tensors
124 fill(AccessorType(src));
125
126 // Compute function
127 pool_layer.run();
128 unpool_layer.run();
129 return unpooled;
130 }
131
132 SimpleTensor<T> compute_reference(TensorShape input_shape, PoolingLayerInfo info, DataType data_type,
133 QuantizationInfo input_qinfo, QuantizationInfo output_qinfo)
134 {
135 SimpleTensor<T> src(input_shape, data_type, 1, input_qinfo);
136 SimpleTensor<uint32_t> indices{};
137 // Fill reference
138 fill(src);
139 auto pooled_tensor = reference::pooling_layer<T>(src, info, output_qinfo, &indices);
140 return reference::max_unpooling_layer<T>(pooled_tensor, info, output_qinfo, indices, input_shape);
141 }
142
143 TensorType _target{};
144 SimpleTensor<T> _reference{};
145 PoolingLayerInfo _pool_info{};
146};
147
148template <typename TensorType, typename AccessorType, typename F1, typename F2, typename T>
149class MaxUnpoolingLayerValidationFixture : public MaxUnpoolingLayerValidationGenericFixture<TensorType, AccessorType, F1, F2, T>
150{
151public:
morgolock37722d92020-04-09 14:17:48 +0100152 void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, DataType data_type, DataLayout data_layout)
153 {
154 MaxUnpoolingLayerValidationGenericFixture<TensorType, AccessorType, F1, F2, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, true),
155 data_type, data_layout);
156 }
157};
158
159} // namespace validation
160} // namespace test
161} // namespace arm_compute
162#endif /* ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE */