Compute Library
 23.11
Im2Col.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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 #include "arm_compute/core/Types.h"
26 #include "tests/NEON/Accessor.h"
27 #include "tests/NEON/Helper.h"
28 #include "tests/datasets/ShapeDatasets.h"
30 #include "tests/framework/Macros.h"
33 #include "tests/validation/fixtures/Im2ColFixture.h"
34 
35 namespace arm_compute
36 {
37 namespace test
38 {
39 namespace validation
40 {
41 namespace
42 {
43 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 
45 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 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  framework::dataset::make("QuantizationInfo", QuantizationInfo(0.5f, 10))),
49  framework::dataset::make("NumGroups", { 1 }));
50 
51 const auto conv_filter_sizes_small = framework::dataset::make("KernelDims", { Size2D(3U, 3U), Size2D(3U, 1U), Size2D(1U, 5U) });
52 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  framework::dataset::make("QuantizationInfo", QuantizationInfo(0.5f, 10))),
55  framework::dataset::make("NumGroups", { 1 }));
56 } // namespace
57 TEST_SUITE(NEON)
58 TEST_SUITE(Im2Col)
59 
60 using CpuIm2Col = NESynthetizeFunctionWithZeroConstantKernelBorder<cpu::kernels::CpuIm2ColKernel>;
61 
62 // *INDENT-OFF*
63 // clang-format off
64 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
65  framework::dataset::make("InputInfo", { TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::U8), // Unsupported data type
66  TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::F32), // Mismatching data type
67  TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::QASYMM8), // Bias not supported with QASYMM8
68  TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::QASYMM8), // Mismatching shapes
69  TensorInfo(TensorShape(10U, 12U, 2U, 2U), 1, DataType::QASYMM8),
70  }),
71  framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(3U, 4U, 10U, 2U), 1, DataType::F16),
72  TensorInfo(TensorShape(3U, 4U, 10U, 2U), 1, DataType::F16),
73  TensorInfo(TensorShape(3U, 3U, 10U, 2U), 1, DataType::QASYMM8),
74  TensorInfo(TensorShape(3U, 4U, 10U, 2U), 1, DataType::QASYMM8),
75  TensorInfo(TensorShape(18U, 80U, 1U, 2U), 1, DataType::QASYMM8),
76  })),
77  framework::dataset::make("HasBias", { true, true, true, false, false })),
78  framework::dataset::make("Expected", { false, false, false, false, true })),
80 {
81  bool status = bool(cpu::kernels::CpuIm2ColKernel::validate(&input_info, &output_info, Size2D(3U, 3U), PadStrideInfo(), has_bias));
83 }
84 // clang-format on
85 // *INDENT-ON*
86 
87 template <typename T>
88 using CpuIm2ColFixture = Im2ColOpValidationFixture<Tensor, Accessor, CpuIm2Col, T, false>;
89 
90 TEST_SUITE(Float)
91 TEST_SUITE(FP32)
92 FIXTURE_DATA_TEST_CASE(RunSmall, CpuIm2ColFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(im2col_shapes, framework::dataset::make("DataType", DataType::F32)),
93  conv_args_small))
94 {
95  // Validate output
96  validate(Accessor(_target), _reference);
97 }
99  DataType::F32)),
100  conv_args))
101 {
102  // Validate output
103  validate(Accessor(_target), _reference);
104 }
105 TEST_SUITE_END() // FP32
106 
107 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
108 
109 TEST_SUITE(FP16)
110 FIXTURE_DATA_TEST_CASE(RunSmall, CpuIm2ColFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(im2col_shapes, framework::dataset::make("DataType", DataType::F16)),
111  conv_args_small))
112 {
113  // Validate output
114  validate(Accessor(_target), _reference);
115 }
116 FIXTURE_DATA_TEST_CASE(RunLarge, CpuIm2ColFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(concat(im2col_shapes, datasets::LargeShapes()), framework::dataset::make("DataType",
117  DataType::F16)),
118  conv_args))
119 {
120  // Validate output
121  validate(Accessor(_target), _reference);
122 }
123 TEST_SUITE_END() // FP16
124 
125 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
126 
127 TEST_SUITE_END() // Float
128 
129 TEST_SUITE(QASYMM8)
130 FIXTURE_DATA_TEST_CASE(RunSmall, CpuIm2ColFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(im2col_shapes, framework::dataset::make("DataType", DataType::QASYMM8)),
131  conv_args_small))
132 {
133  // Validate output
134  validate(Accessor(_target), _reference);
135 }
138  conv_args))
139 {
140  // Validate output
141  validate(Accessor(_target), _reference);
142 }
143 TEST_SUITE_END() // QASYMM8
144 
145 TEST_SUITE(SpecialCases)
146 TEST_CASE(PaddedChannelNHWC, framework::DatasetMode::PRECOMMIT)
147 {
148  // Const data
149  const TensorShape src_shape = TensorShape(7U, 27U, 13U);
152  const bool has_bias = false;
153  const unsigned int num_groups = 1;
154  const Size2D spatial_kernel(3, 3);
156  const PadStrideInfo conv_info(1U, 1U, 0U, 0U);
157 
158  // Calculate destination shape
159  TensorInfo src_info(src_shape, 1, data_type);
162 
163  // Compute target
164  Tensor src_target = create_tensor<Tensor>(src_shape, data_type, 1, qinfo, data_layout);
165  Tensor dst_target = create_tensor<Tensor>(dst_shape, data_type, 1, qinfo);
166 
167  // Configure target function
170 
171  // Extend padding
172  src_target.info()->extend_padding(PaddingSize(3, 5, 9, 1));
173  dst_target.info()->extend_padding(PaddingSize(8, 1, 1, 3));
174 
175  // Validate and allocate tensors
178 
181 
184 
185  // Fill target source
186  library->fill_tensor_uniform(Accessor(src_target), 0);
187 
189  {
192  };
193  // Run target function
195 
196  // Calculate Reference
199 
200  // Fill reference source
201  library->fill_tensor_uniform(src_ref, 0);
202 
203 #ifndef DOXYGEN_SKIP_THIS
204  // Run reference function
206 #endif // DOXYGEN_SKIP_THIS
207 
208  // Validate
209  validate(Accessor(dst_target), dst_ref);
210 }
211 TEST_SUITE_END() // Special Cases
212 TEST_SUITE_END() // Im2Col
213 TEST_SUITE_END() // Neon
214 } // namespace validation
215 } // namespace test
216 } // namespace arm_compute
arm_compute::DataLayout::NCHW
@ NCHW
Num samples, channels, height, width.
Datasets.h
arm_compute::test::validation::TEST_SUITE_END
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
Definition: DequantizationLayer.cpp:111
arm_compute::test::validation::dst_shape
TensorShape dst_shape
Definition: DFT.cpp:164
arm_compute::test::validation::TEST_CASE
TEST_CASE(FusedActivation, framework::DatasetMode::ALL)
Validate fused activation expecting the following behaviours:
Definition: ArithmeticAddition.cpp:93
arm_compute::test::validation::input_info
input_info
Definition: DirectConvolutionLayer.cpp:547
arm_compute::test::validation::FIXTURE_DATA_TEST_CASE
FIXTURE_DATA_TEST_CASE(RunSmall, CLAbsLayerFixture< half >, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)))
Definition: AbsLayer.cpp:50
arm_compute::test::validation::DATA_TEST_CASE
DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), }), framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), })), framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT), })), framework::dataset::make("Expected", { false, true, true, true, false, false, true, true, false })), input_info, output_info, act_info, expected)
Definition: ActivationLayer.cpp:100
arm_compute::QuantizationInfo
Quantization information.
Definition: QuantizationInfo.h:67
arm_compute::test::validation::combine
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65
arm_compute::DataLayout
DataLayout
[DataLayout enum definition]
Definition: CoreTypes.h:110
CpuIm2ColKernel.h
arm_compute::DataLayout::NHWC
@ NHWC
Num samples, height, width, channels.
arm_compute::test::NESynthetizeFunctionWithZeroConstantKernelBorder::run
void run(ITensorPack &tensors)
Run the kernels contained in the function.
Definition: Helper.h:129
arm_compute::DataType::QASYMM8
@ QASYMM8
quantized, asymmetric fixed-point 8-bit number unsigned
arm_compute::TensorShape
Shape of a tensor.
Definition: TensorShape.h:39
arm_compute::test::validation::src_ref
SimpleTensor< float > src_ref
Definition: Im2Col.cpp:197
arm_compute::ITensorInfo::extend_padding
virtual bool extend_padding(const PaddingSize &padding)=0
Update the offset to the first element, the strides and the total size.
Types.h
arm_compute::Size2D
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
arm_compute::cpu::kernels::CpuIm2ColKernel::validate
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U), unsigned int num_groups=1, unsigned int input_pad_right=0)
Static function to check if given info will lead to a valid configuration.
Definition: CpuIm2ColKernel.cpp:382
arm_compute::test::Accessor
Accessor implementation for Tensor objects.
Definition: Accessor.h:35
arm_compute::misc::shape_calculator::compute_im2col_conv_shape
TensorShape compute_im2col_conv_shape(const ITensorInfo *input, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation, bool batch_size_on_z, unsigned int num_groups=1, unsigned int input_pad_right=0)
Calculate the im2col output shape of a tensor.
Definition: ShapeCalculator.h:578
arm_compute::test::validation::validate
validate(CLAccessor(output_state), expected_output)
arm_compute::test::validation::has_bias
const bool has_bias
Definition: Im2Col.cpp:152
arm_compute::utils::cast::U
U
Definition: SaturateCast.h:65
arm_compute::test::validation::output_info
output_info
Definition: DirectConvolutionLayer.cpp:547
arm_compute::Tensor::allocator
TensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: Tensor.cpp:47
arm_compute::ITensorPack
Tensor packing service.
Definition: ITensorPack.h:39
arm_compute::test::NESynthetizeFunctionWithZeroConstantKernelBorder
As above but this also setups a Zero border on the input tensor of the kernel's bordersize.
Definition: Helper.h:109
arm_compute::test::validation::ARM_COMPUTE_EXPECT
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
arm_compute::test::framework::dataset::concat
JoinDataset< T, U > concat(T &&dataset1, U &&dataset2)
Helper function to create a JoinDataset.
Definition: JoinDataset.h:160
arm_compute::ACL_DST
@ ACL_DST
Definition: Types.h:55
arm_compute::test::framework::DatasetMode::NIGHTLY
@ NIGHTLY
arm_compute::DataType::U8
@ U8
unsigned 8-bit number
Asserts.h
Accessor.h
arm_compute::TensorInfo::set_data_layout
ITensorInfo & set_data_layout(const DataLayout &data_layout) override
Set the data layout of the tensor.
Definition: TensorInfo.cpp:404
arm_compute::test::validation::dst_target
Tensor dst_target
Definition: Im2Col.cpp:165
arm_compute::test::validation::CpuIm2ColFixture
Im2ColOpValidationFixture< Tensor, Accessor, CpuIm2Col, T, false > CpuIm2ColFixture
Definition: Im2Col.cpp:88
Macros.h
arm_compute::test::framework::DatasetMode::PRECOMMIT
@ PRECOMMIT
arm_compute::test::validation::pack
ITensorPack pack
Definition: Im2Col.cpp:188
arm_compute::Tensor::info
ITensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: Tensor.cpp:32
arm_compute::PadStrideInfo
Definition: CoreTypes.h:139
arm_compute::test::framework::dataset::make
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
Definition: ContainerDataset.h:160
arm_compute::test::validation::data_type
data_type
Definition: Cast.cpp:222
Validation.h
arm_compute::test::library
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:77
arm_compute::test::validation::data_layout
const DataLayout data_layout
Definition: Im2Col.cpp:151
arm_compute::test::SimpleTensor
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
arm_compute::TensorInfo
Store the tensor's metadata.
Definition: TensorInfo.h:41
arm_compute::test::validation::num_groups
const unsigned int num_groups
Definition: Im2Col.cpp:153
arm_compute::test::validation::im2col_func
CpuIm2Col im2col_func
Definition: Im2Col.cpp:168
arm_compute::test::validation::zip
zip(zip(framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), }), framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), TensorInfo(TensorShape(2U), 1, DataType::F16), TensorInfo(TensorShape(5U), 1, DataType::F32), })), framework::dataset::make("Expected", { true, false, false}))
arm_compute::ITensorInfo::is_resizable
virtual bool is_resizable() const =0
Flag indicating whether the size of the tensor can be changed.
arm_compute::test::NESynthetizeFunctionWithZeroConstantKernelBorder::configure
void configure(T first, Args &&... args)
Configure the kernel.
Definition: Helper.h:118
arm_compute::PaddingSize
BorderSize PaddingSize
Container for 2D padding size.
Definition: Types.h:346
arm_compute::TensorAllocator::allocate
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
Definition: TensorAllocator.cpp:133
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::test::validation::conv_info
conv_info
Definition: DirectConvolutionLayer.cpp:547
arm_compute::test::validation::TEST_SUITE
TEST_SUITE(QASYMM8_to_F32) FIXTURE_DATA_TEST_CASE(RunSmall
arm_compute::DataType::F16
@ F16
16-bit floating-point number
arm_compute::test::validation::src_info
TensorInfo src_info(src_shape, 1, data_type)
arm_compute::test::validation::spatial_kernel
const Size2D spatial_kernel(3, 3)
arm_compute::ACL_SRC
@ ACL_SRC
Definition: Types.h:44
arm_compute::test::validation::expected
expected
Definition: BatchNormalizationLayer.cpp:166
arm_compute::DataType::F32
@ F32
32-bit floating-point number
arm_compute::Tensor
Basic implementation of the tensor interface.
Definition: Tensor.h:37
arm_compute::test::validation::src_target
Tensor src_target
Definition: Im2Col.cpp:164
arm_compute::test::framework::DatasetMode
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
arm_compute::DataType
DataType
Available data types.
Definition: CoreTypes.h:83
arm_compute::test::validation::dst_ref
auto dst_ref
Definition: DFT.cpp:172
arm_compute::test::validation::qinfo
const QuantizationInfo qinfo
Definition: Im2Col.cpp:155
Helper.h
arm_compute::test::validation::reference::im2col
void im2col(const SimpleTensor< T > &src, SimpleTensor< T > &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups)
Definition: Im2Col.cpp:144
arm_compute::test::framework::LogLevel::ERRORS
@ ERRORS