1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "arm_compute/core/Types.h"
25*c217d954SCole Faust #include "src/cpu/kernels/CpuIm2ColKernel.h"
26*c217d954SCole Faust #include "tests/NEON/Accessor.h"
27*c217d954SCole Faust #include "tests/NEON/Helper.h"
28*c217d954SCole Faust #include "tests/datasets/ShapeDatasets.h"
29*c217d954SCole Faust #include "tests/framework/Asserts.h"
30*c217d954SCole Faust #include "tests/framework/Macros.h"
31*c217d954SCole Faust #include "tests/framework/datasets/Datasets.h"
32*c217d954SCole Faust #include "tests/validation/Validation.h"
33*c217d954SCole Faust #include "tests/validation/fixtures/Im2ColFixture.h"
34*c217d954SCole Faust
35*c217d954SCole Faust namespace arm_compute
36*c217d954SCole Faust {
37*c217d954SCole Faust namespace test
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace validation
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace
42*c217d954SCole Faust {
43*c217d954SCole Faust const auto im2col_shapes = framework::dataset::make("Shape", { TensorShape{ 11U, 11U, 11U }, TensorShape{ 16U, 16U, 16U }, TensorShape{ 27U, 13U, 7U }, TensorShape{ 31U, 27U, 17U, 2U }, TensorShape{ 27U, 13U, 5U, 4U }, TensorShape{ 11U, 11U, 5U, 5U } });
44*c217d954SCole Faust
45*c217d954SCole Faust const auto conv_filter_sizes = framework::dataset::make("KernelDims", { Size2D(3U, 3U), Size2D(3U, 1U), Size2D(1U, 5U), Size2D(5U, 5U), Size2D(7U, 7U) });
46*c217d954SCole Faust const auto conv_args = combine(combine(combine(combine(conv_filter_sizes, framework::dataset::make("PadStride", { PadStrideInfo(1U, 1U, 0U, 0U), PadStrideInfo(1U, 1U, 1U, 1U), PadStrideInfo(2U, 2U, 0U, 2U) })),
47*c217d954SCole Faust framework::dataset::make("QuantizationInfo", QuantizationInfo(0.5f, 10))),
48*c217d954SCole Faust framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
49*c217d954SCole Faust framework::dataset::make("NumGroups", { 1 }));
50*c217d954SCole Faust
51*c217d954SCole Faust const auto conv_filter_sizes_small = framework::dataset::make("KernelDims", { Size2D(3U, 3U), Size2D(3U, 1U), Size2D(1U, 5U) });
52*c217d954SCole Faust const auto conv_args_small = combine(combine(combine(combine(conv_filter_sizes_small, framework::dataset::make("PadStride", { PadStrideInfo(1U, 1U, 0U, 0U), PadStrideInfo(1U, 1U, 1U, 1U) })),
53*c217d954SCole Faust framework::dataset::make("QuantizationInfo", QuantizationInfo(0.5f, 10))),
54*c217d954SCole Faust framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
55*c217d954SCole Faust framework::dataset::make("NumGroups", { 1 }));
56*c217d954SCole Faust } // namespace
57*c217d954SCole Faust TEST_SUITE(NEON)
58*c217d954SCole Faust TEST_SUITE(Im2Col)
59*c217d954SCole Faust
60*c217d954SCole Faust using CpuIm2Col = NESynthetizeFunctionWithZeroConstantKernelBorder<cpu::kernels::CpuIm2ColKernel>;
61*c217d954SCole Faust
62*c217d954SCole Faust // *INDENT-OFF*
63*c217d954SCole Faust // clang-format off
64*c217d954SCole Faust DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
65*c217d954SCole Faust framework::dataset::make("InputInfo", { TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::U8), // Unsupported data type
66*c217d954SCole Faust TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::F32), // Mismatching data type
67*c217d954SCole Faust TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::QASYMM8), // Bias not supported with QASYMM8
68*c217d954SCole Faust TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::QASYMM8), // Mismatching shapes
69*c217d954SCole Faust TensorInfo(TensorShape(10U, 12U, 2U, 2U), 1, DataType::QASYMM8),
70*c217d954SCole Faust }),
71*c217d954SCole Faust framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(3U, 4U, 10U, 2U), 1, DataType::F16),
72*c217d954SCole Faust TensorInfo(TensorShape(3U, 4U, 10U, 2U), 1, DataType::F16),
73*c217d954SCole Faust TensorInfo(TensorShape(3U, 3U, 10U, 2U), 1, DataType::QASYMM8),
74*c217d954SCole Faust TensorInfo(TensorShape(3U, 4U, 10U, 2U), 1, DataType::QASYMM8),
75*c217d954SCole Faust TensorInfo(TensorShape(18U, 80U, 1U, 2U), 1, DataType::QASYMM8),
76*c217d954SCole Faust })),
77*c217d954SCole Faust framework::dataset::make("HasBias", { true, true, true, false, false })),
78*c217d954SCole Faust framework::dataset::make("Expected", { false, false, false, false, true })),
79*c217d954SCole Faust input_info, output_info, has_bias, expected)
80*c217d954SCole Faust {
81*c217d954SCole Faust bool status = bool(cpu::kernels::CpuIm2ColKernel::validate(&input_info, &output_info, Size2D(3U, 3U), PadStrideInfo(), has_bias));
82*c217d954SCole Faust ARM_COMPUTE_EXPECT(status == expected, framework::LogLevel::ERRORS);
83*c217d954SCole Faust }
84*c217d954SCole Faust // clang-format on
85*c217d954SCole Faust // *INDENT-ON*
86*c217d954SCole Faust
87*c217d954SCole Faust template <typename T>
88*c217d954SCole Faust using CpuIm2ColFixture = Im2ColOpValidationFixture<Tensor, Accessor, CpuIm2Col, T, false>;
89*c217d954SCole Faust
90*c217d954SCole Faust TEST_SUITE(Float)
TEST_SUITE(FP32)91*c217d954SCole Faust TEST_SUITE(FP32)
92*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CpuIm2ColFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(im2col_shapes, framework::dataset::make("DataType", DataType::F32)),
93*c217d954SCole Faust conv_args_small))
94*c217d954SCole Faust {
95*c217d954SCole Faust // Validate output
96*c217d954SCole Faust validate(Accessor(_target), _reference);
97*c217d954SCole Faust }
98*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunLarge, CpuIm2ColFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(concat(im2col_shapes, datasets::LargeShapes()), framework::dataset::make("DataType",
99*c217d954SCole Faust DataType::F32)),
100*c217d954SCole Faust conv_args))
101*c217d954SCole Faust {
102*c217d954SCole Faust // Validate output
103*c217d954SCole Faust validate(Accessor(_target), _reference);
104*c217d954SCole Faust }
105*c217d954SCole Faust TEST_SUITE_END() // FP32
106*c217d954SCole Faust
107*c217d954SCole Faust #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
108*c217d954SCole Faust
TEST_SUITE(FP16)109*c217d954SCole Faust TEST_SUITE(FP16)
110*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CpuIm2ColFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(im2col_shapes, framework::dataset::make("DataType", DataType::F16)),
111*c217d954SCole Faust conv_args_small))
112*c217d954SCole Faust {
113*c217d954SCole Faust // Validate output
114*c217d954SCole Faust validate(Accessor(_target), _reference);
115*c217d954SCole Faust }
116*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunLarge, CpuIm2ColFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(concat(im2col_shapes, datasets::LargeShapes()), framework::dataset::make("DataType",
117*c217d954SCole Faust DataType::F16)),
118*c217d954SCole Faust conv_args))
119*c217d954SCole Faust {
120*c217d954SCole Faust // Validate output
121*c217d954SCole Faust validate(Accessor(_target), _reference);
122*c217d954SCole Faust }
123*c217d954SCole Faust TEST_SUITE_END() // FP16
124*c217d954SCole Faust
125*c217d954SCole Faust #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
126*c217d954SCole Faust
TEST_SUITE_END()127*c217d954SCole Faust TEST_SUITE_END() // Float
128*c217d954SCole Faust
129*c217d954SCole Faust TEST_SUITE(QASYMM8)
130*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CpuIm2ColFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(im2col_shapes, framework::dataset::make("DataType", DataType::QASYMM8)),
131*c217d954SCole Faust conv_args_small))
132*c217d954SCole Faust {
133*c217d954SCole Faust // Validate output
134*c217d954SCole Faust validate(Accessor(_target), _reference);
135*c217d954SCole Faust }
136*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunLarge, CpuIm2ColFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(concat(im2col_shapes, datasets::LargeShapes()),
137*c217d954SCole Faust framework::dataset::make("DataType", DataType::QASYMM8)),
138*c217d954SCole Faust conv_args))
139*c217d954SCole Faust {
140*c217d954SCole Faust // Validate output
141*c217d954SCole Faust validate(Accessor(_target), _reference);
142*c217d954SCole Faust }
143*c217d954SCole Faust TEST_SUITE_END() // QASYMM8
144*c217d954SCole Faust
TEST_SUITE(SpecialCases)145*c217d954SCole Faust TEST_SUITE(SpecialCases)
146*c217d954SCole Faust TEST_CASE(PaddedChannelNHWC, framework::DatasetMode::PRECOMMIT)
147*c217d954SCole Faust {
148*c217d954SCole Faust // Const data
149*c217d954SCole Faust const TensorShape src_shape = TensorShape(7U, 27U, 13U);
150*c217d954SCole Faust const DataType data_type = DataType::F32;
151*c217d954SCole Faust const DataLayout data_layout = DataLayout::NHWC;
152*c217d954SCole Faust const bool has_bias = false;
153*c217d954SCole Faust const unsigned int num_groups = 1;
154*c217d954SCole Faust const Size2D spatial_kernel(3, 3);
155*c217d954SCole Faust const QuantizationInfo qinfo{};
156*c217d954SCole Faust const PadStrideInfo conv_info(1U, 1U, 0U, 0U);
157*c217d954SCole Faust
158*c217d954SCole Faust // Calculate destination shape
159*c217d954SCole Faust TensorInfo src_info(src_shape, 1, data_type);
160*c217d954SCole Faust src_info.set_data_layout(data_layout);
161*c217d954SCole Faust const TensorShape dst_shape = compute_im2col_conv_shape(&src_info, spatial_kernel, conv_info, has_bias, Size2D(1U, 1U), false, num_groups);
162*c217d954SCole Faust
163*c217d954SCole Faust // Compute target
164*c217d954SCole Faust Tensor src_target = create_tensor<Tensor>(src_shape, data_type, 1, qinfo, data_layout);
165*c217d954SCole Faust Tensor dst_target = create_tensor<Tensor>(dst_shape, data_type, 1, qinfo);
166*c217d954SCole Faust
167*c217d954SCole Faust // Configure target function
168*c217d954SCole Faust CpuIm2Col im2col_func;
169*c217d954SCole Faust im2col_func.configure(src_target.info(), dst_target.info(), spatial_kernel, conv_info, has_bias);
170*c217d954SCole Faust
171*c217d954SCole Faust // Extend padding
172*c217d954SCole Faust src_target.info()->extend_padding(PaddingSize(3, 5, 9, 1));
173*c217d954SCole Faust dst_target.info()->extend_padding(PaddingSize(8, 1, 1, 3));
174*c217d954SCole Faust
175*c217d954SCole Faust // Validate and allocate tensors
176*c217d954SCole Faust ARM_COMPUTE_EXPECT(src_target.info()->is_resizable(), framework::LogLevel::ERRORS);
177*c217d954SCole Faust ARM_COMPUTE_EXPECT(dst_target.info()->is_resizable(), framework::LogLevel::ERRORS);
178*c217d954SCole Faust
179*c217d954SCole Faust src_target.allocator()->allocate();
180*c217d954SCole Faust dst_target.allocator()->allocate();
181*c217d954SCole Faust
182*c217d954SCole Faust ARM_COMPUTE_EXPECT(!src_target.info()->is_resizable(), framework::LogLevel::ERRORS);
183*c217d954SCole Faust ARM_COMPUTE_EXPECT(!dst_target.info()->is_resizable(), framework::LogLevel::ERRORS);
184*c217d954SCole Faust
185*c217d954SCole Faust // Fill target source
186*c217d954SCole Faust library->fill_tensor_uniform(Accessor(src_target), 0);
187*c217d954SCole Faust
188*c217d954SCole Faust ITensorPack pack =
189*c217d954SCole Faust {
190*c217d954SCole Faust { TensorType::ACL_SRC, &src_target },
191*c217d954SCole Faust { TensorType::ACL_DST, &dst_target }
192*c217d954SCole Faust };
193*c217d954SCole Faust // Run target function
194*c217d954SCole Faust im2col_func.run(pack);
195*c217d954SCole Faust
196*c217d954SCole Faust // Calculate Reference
197*c217d954SCole Faust SimpleTensor<float> src_ref{ src_shape, data_type, 1, qinfo, data_layout };
198*c217d954SCole Faust SimpleTensor<float> dst_ref{ dst_shape, data_type, 1, qinfo, DataLayout::NCHW };
199*c217d954SCole Faust
200*c217d954SCole Faust // Fill reference source
201*c217d954SCole Faust library->fill_tensor_uniform(src_ref, 0);
202*c217d954SCole Faust
203*c217d954SCole Faust #ifndef DOXYGEN_SKIP_THIS
204*c217d954SCole Faust // Run reference function
205*c217d954SCole Faust reference::im2col(src_ref, dst_ref, spatial_kernel, conv_info, has_bias, num_groups);
206*c217d954SCole Faust #endif // DOXYGEN_SKIP_THIS
207*c217d954SCole Faust
208*c217d954SCole Faust // Validate
209*c217d954SCole Faust validate(Accessor(dst_target), dst_ref);
210*c217d954SCole Faust }
211*c217d954SCole Faust TEST_SUITE_END() // Special Cases
212*c217d954SCole Faust TEST_SUITE_END() // Im2Col
213*c217d954SCole Faust TEST_SUITE_END() // Neon
214*c217d954SCole Faust } // namespace validation
215*c217d954SCole Faust } // namespace test
216*c217d954SCole Faust } // namespace arm_compute
217