blob: f403aa9d217e3c86c7474c492fd74cc55e56d76c [file] [log] [blame]
Giorgio Arena156fcf32018-03-09 15:30:43 +00001/*
2 * Copyright (c) 2017-2018 ARM Limited.
3 *
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
47template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
48class Im2ColValidationFixture : public framework::Fixture
49{
50public:
51 template <typename...>
52 void setup(TensorShape shape, DataType data_type, const Size2D &kernel_dims, const PadStrideInfo &conv_info, const QuantizationInfo &quant_info, const DataLayout &data_layout)
53 {
54 _kernel_dims = kernel_dims;
55 _conv_info = conv_info;
56 _quant_info = quant_info;
57 _data_layout = data_layout;
58 _has_bias = data_type != DataType::QASYMM8;
59
60 if(_data_layout == DataLayout::NHWC)
61 {
62 permute(shape, PermutationVector(2U, 0U, 1U));
63 }
64
65 TensorShape output_shape;
66 TensorInfo input_info(shape, 1, data_type);
67 input_info.set_data_layout(_data_layout);
68 output_shape = compute_im2col_conv_shape(&input_info, _kernel_dims, _conv_info, _has_bias, Size2D(1U, 1U));
69
70 _target = compute_target(shape, output_shape, data_type);
71 _reference = compute_reference(shape, output_shape, data_type);
72 }
73
74protected:
75 template <typename U>
76 void fill(U &&tensor)
77 {
78 library->fill_tensor_uniform(tensor, 0);
79 }
80
81 TensorType compute_target(const TensorShape &shape, const TensorShape &output_shape, DataType data_type)
82 {
83 // Create tensors
84 TensorType src = create_tensor<TensorType>(shape, data_type, 1, 0, _quant_info, _data_layout);
85 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, 0, _quant_info, _data_layout);
86
87 // Create and configure function
88 FunctionType im2col_func;
89 im2col_func.configure(&src, &dst, _kernel_dims, _conv_info, _has_bias);
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 im2col_func.run();
106
107 return dst;
108 }
109
110 SimpleTensor<T> compute_reference(const TensorShape &shape, const TensorShape &output_shape, DataType data_type)
111 {
112 // Create reference
113 SimpleTensor<T> src{ shape, data_type, 1, 0, _quant_info, _data_layout };
114
115 // Fill reference
116 fill(src);
117
118 return reference::im2col<T>(src, output_shape, _kernel_dims, _conv_info, _has_bias);
119 }
120
121 TensorType _target{};
122 SimpleTensor<T> _reference{};
123 Size2D _kernel_dims{};
124 PadStrideInfo _conv_info{};
125 DataLayout _data_layout{};
126 QuantizationInfo _quant_info{};
127 bool _has_bias{};
128};
129} // namespace validation
130} // namespace test
131} // namespace arm_compute
132#endif /* ARM_COMPUTE_TEST_IM2COL_FIXTURE */