blob: a0732c3eb381783d19d4fb22525e2f068172053e [file] [log] [blame]
Giorgio Arena156fcf32018-03-09 15:30:43 +00001/*
Giorgio Arena4f8ce132021-01-07 16:35:05 +00002 * Copyright (c) 2017-2021 Arm Limited.
Giorgio Arena156fcf32018-03-09 15:30:43 +00003 *
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_IM2COL_FIXTURE
25#define ARM_COMPUTE_TEST_IM2COL_FIXTURE
26
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/TensorShape.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.h"
31#include "arm_compute/runtime/Tensor.h"
32#include "tests/AssetsLibrary.h"
33#include "tests/Globals.h"
34#include "tests/IAccessor.h"
35#include "tests/framework/Asserts.h"
36#include "tests/framework/Fixture.h"
37#include "tests/validation/reference/Im2Col.h"
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45using namespace arm_compute::misc::shape_calculator;
46
Gian Marco Iodice597a8562018-08-01 15:06:06 +010047template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool batch_size_on_z>
Manuel Bottini90028992021-06-30 18:29:18 +010048class Im2ColOpValidationFixture : public framework::Fixture
49{
50public:
51 template <typename...>
52 void setup(TensorShape input_shape, DataType data_type, const Size2D &kernel_dims, const PadStrideInfo &conv_info, const QuantizationInfo &quant_info, const DataLayout &data_layout,
53 unsigned int num_groups)
54 {
55 _kernel_dims = kernel_dims;
56 _conv_info = conv_info;
57 _quant_info = quant_info;
58 _data_layout = data_layout;
59 _has_bias = data_type != DataType::QASYMM8;
60 _num_groups = num_groups;
61
62 if(_data_layout == DataLayout::NHWC)
63 {
64 permute(input_shape, PermutationVector(2U, 0U, 1U));
65 }
66
67 TensorInfo input_info(input_shape, 1, data_type);
68 input_info.set_data_layout(_data_layout);
69
70 const TensorShape output_shape = compute_im2col_conv_shape(&input_info, _kernel_dims, _conv_info, _has_bias, Size2D(1U, 1U), batch_size_on_z && _num_groups == 1, _num_groups);
71 _target = compute_target(input_shape, output_shape, data_type);
72
73 compute_reference(input_shape, output_shape, data_type);
74 }
75
76protected:
77 template <typename U>
78 void fill(U &&tensor)
79 {
80 library->fill_tensor_uniform(tensor, 0);
81 }
82
83 TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, DataType data_type)
84 {
85 // Create tensors
86 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, _quant_info, _data_layout);
87 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, _quant_info);
88
89 // Create and configure function
90 FunctionType im2col_func;
91 im2col_func.configure(src.info(), dst.info(), _kernel_dims, _conv_info, _has_bias, Size2D(1U, 1U), _num_groups);
92
93 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
94 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
95
96 // Allocate tensors
97 src.allocator()->allocate();
98 dst.allocator()->allocate();
99
100 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
101 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
102
103 // Fill tensors
104 fill(AccessorType(src));
105
106 arm_compute::ITensorPack pack =
107 {
108 { arm_compute::TensorType::ACL_SRC, &src },
109 { arm_compute::TensorType::ACL_DST, &dst }
110 };
111 // Compute function
112 im2col_func.run(pack);
113
114 return dst;
115 }
116
117 void compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, DataType data_type)
118 {
119 // Create reference
120 SimpleTensor<T> src{ input_shape, data_type, 1, _quant_info, _data_layout };
121 _reference = SimpleTensor<T>(output_shape, data_type, 1, _quant_info, DataLayout::NCHW);
122
123 // Fill reference
124 fill(src);
125
126 reference::im2col<T>(src, _reference, _kernel_dims, _conv_info, _has_bias, _num_groups);
127 }
128 TensorType _target{};
129 SimpleTensor<T> _reference{};
130 Size2D _kernel_dims{};
131 PadStrideInfo _conv_info{};
132 DataLayout _data_layout{};
133 QuantizationInfo _quant_info{};
134 bool _has_bias{};
135 unsigned int _num_groups{};
136};
Giorgio Arena156fcf32018-03-09 15:30:43 +0000137} // namespace validation
138} // namespace test
139} // namespace arm_compute
140#endif /* ARM_COMPUTE_TEST_IM2COL_FIXTURE */