Compute Library
 20.02.1
Im2Col.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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/datasets/ShapeDatasets.h"
29 #include "tests/framework/Macros.h"
32 #include "tests/validation/fixtures/Im2ColFixture.h"
33 
34 namespace arm_compute
35 {
36 namespace test
37 {
38 namespace validation
39 {
40 namespace
41 {
42 const auto conv_filter_sizes = framework::dataset::make("KernelDims", { Size2D(3U, 3U), Size2D(3U, 1U), Size2D(1U, 5U), Size2D(5U, 5U), Size2D(7U, 7U) });
43 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) })),
44  framework::dataset::make("QuantizationInfo", QuantizationInfo(0.5f, 10))),
46  framework::dataset::make("NumGroups", { 1 }));
47 
48 const auto conv_filter_sizes_small = framework::dataset::make("KernelDims", { Size2D(3U, 3U), Size2D(3U, 1U), Size2D(1U, 5U) });
49 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) })),
50  framework::dataset::make("QuantizationInfo", QuantizationInfo(0.5f, 10))),
52  framework::dataset::make("NumGroups", { 1 }));
53 } // namespace
54 TEST_SUITE(NEON)
55 TEST_SUITE(Im2Col)
56 
57 // *INDENT-OFF*
58 // clang-format off
59 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
60  framework::dataset::make("InputInfo", { TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::U8), // Unsupported data type
61  TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::F32), // Mismatching data type
62  TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::QASYMM8), // Bias not supported with QASYMM8
63  TensorInfo(TensorShape(10U, 12U, 2U), 1, DataType::QASYMM8), // Mismatching shapes
64  TensorInfo(TensorShape(10U, 12U, 2U, 2U), 1, DataType::QASYMM8),
65  }),
66  framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(3U, 4U, 10U, 2U), 1, DataType::F16),
67  TensorInfo(TensorShape(3U, 4U, 10U, 2U), 1, DataType::F16),
68  TensorInfo(TensorShape(3U, 3U, 10U, 2U), 1, DataType::QASYMM8),
69  TensorInfo(TensorShape(3U, 4U, 10U, 2U), 1, DataType::QASYMM8),
70  TensorInfo(TensorShape(18U, 80U, 1U, 2U), 1, DataType::QASYMM8),
71  })),
72  framework::dataset::make("HasBias", { true, true, true, false, false })),
73  framework::dataset::make("Expected", { false, false, false, false, true })),
75 {
76  bool status = bool(NEIm2Col::validate(&input_info, &output_info, Size2D(3U, 3U), PadStrideInfo(), has_bias));
78 }
79 // clang-format on
80 // *INDENT-ON*
81 
82 template <typename T>
83 using NEIm2ColFixture = Im2ColValidationFixture<Tensor, Accessor, NEIm2Col, T, false>;
84 
85 TEST_SUITE(Float)
86 TEST_SUITE(FP32)
87 FIXTURE_DATA_TEST_CASE(RunSmall, NEIm2ColFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)),
88  conv_args_small))
89 {
90  // Validate output
91  validate(Accessor(_target), _reference);
92 }
93 FIXTURE_DATA_TEST_CASE(RunLarge, NEIm2ColFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), framework::dataset::make("DataType",
94  DataType::F32)),
95  conv_args))
96 {
97  // Validate output
98  validate(Accessor(_target), _reference);
99 }
100 TEST_SUITE_END() // FP32
101 
102 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
103 
104 TEST_SUITE(FP16)
105 FIXTURE_DATA_TEST_CASE(RunSmall, NEIm2ColFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)),
106  conv_args_small))
107 {
108  // Validate output
109  validate(Accessor(_target), _reference);
110 }
111 FIXTURE_DATA_TEST_CASE(RunLarge, NEIm2ColFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), framework::dataset::make("DataType",
112  DataType::F16)),
113  conv_args))
114 {
115  // Validate output
116  validate(Accessor(_target), _reference);
117 }
118 TEST_SUITE_END() // FP16
119 
120 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
121 
122 TEST_SUITE_END() // Float
123 
124 TEST_SUITE(QASYMM8)
125 FIXTURE_DATA_TEST_CASE(RunSmall, NEIm2ColFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::QASYMM8)),
126  conv_args_small))
127 {
128  // Validate output
129  validate(Accessor(_target), _reference);
130 }
131 FIXTURE_DATA_TEST_CASE(RunLarge, NEIm2ColFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()),
133  conv_args))
134 {
135  // Validate output
136  validate(Accessor(_target), _reference);
137 }
138 TEST_SUITE_END() // QASYMM8
139 
140 TEST_SUITE(SpecialCases)
141 TEST_CASE(PaddedChannelNHWC, framework::DatasetMode::PRECOMMIT)
142 {
143  // Const data
144  const TensorShape src_shape = TensorShape(7U, 27U, 13U);
147  const bool has_bias = false;
148  const unsigned int num_groups = 1;
149  const Size2D spatial_kernel(3, 3);
151  const PadStrideInfo conv_info(1U, 1U, 0U, 0U);
152 
153  // Calculate destination shape
154  TensorInfo src_info(src_shape, 1, data_type);
157 
158  // Compute target
159  Tensor src_target = create_tensor<Tensor>(src_shape, data_type, 1, qinfo, data_layout);
160  Tensor dst_target = create_tensor<Tensor>(dst_shape, data_type, 1, qinfo);
161 
162  // Configure target function
165 
166  // Extend padding
167  src_target.info()->extend_padding(PaddingSize(3, 5, 9, 1));
168  dst_target.info()->extend_padding(PaddingSize(8, 1, 1, 3));
169 
170  // Validate and allocate tensors
173 
176 
179 
180  // Fill target source
181  library->fill_tensor_uniform(Accessor(src_target), 0);
182 
183  // Run target function
184  im2col_func.run();
185 
186  // Calculate Reference
189 
190  // Fill reference source
191  library->fill_tensor_uniform(src_ref, 0);
192 
193 #ifndef DOXYGEN_SKIP_THIS
194  // Run reference function
196 #endif // DOXYGEN_SKIP_THIS
197 
198  // Validate
199  validate(Accessor(dst_target), dst_ref);
200 }
201 TEST_SUITE_END() // Special Cases
202 TEST_SUITE_END() // Im2Col
203 TEST_SUITE_END() // NEON
204 } // namespace validation
205 } // namespace test
206 } // namespace arm_compute
Shape of a tensor.
Definition: TensorShape.h:39
const DataLayout data_layout
Definition: Im2Col.cpp:146
void run() override
Run the kernels contained in the function.
Definition: NEIm2Col.cpp:50
1 channel, 1 U8 per channel
1 channel, 1 F32 per channel
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
const Size2D spatial_kernel(3, 3)
TEST_CASE(Configuration, framework::DatasetMode::ALL)
Copyright (c) 2017-2020 ARM Limited.
1 channel, 1 F16 per channel
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:142
TensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: Tensor.cpp:48
ITensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: Tensor.cpp:33
void configure(const ITensor *input, ITensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U), unsigned int num_groups=1)
Configure the im2col NEON kernel.
Definition: NEIm2Col.cpp:37
Im2ColValidationFixture< Tensor, Accessor, NEIm2Col, T, false > NEIm2ColFixture
Definition: Im2Col.cpp:83
virtual bool is_resizable() const =0
Flag indicating whether the size of the tensor can be changed.
Quantization information.
Accessor implementation for Tensor objects.
Definition: Accessor.h:35
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
ITensorInfo & set_data_layout(const DataLayout &data_layout) override
Set the data layout of the tensor.
Definition: TensorInfo.cpp:378
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:77
quantized, asymmetric fixed-point 8-bit number unsigned
const unsigned int num_groups
Definition: Im2Col.cpp:148
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
Basic implementation of the tensor interface.
Definition: Tensor.h:37
Padding and stride information class.
Definition: Types.h:686
BorderSize PaddingSize
Container for 2D padding size.
Definition: Types.h:356
SimpleTensor< float > src_ref
Definition: Im2Col.cpp:187
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U), unsigned int num_groups=1)
Static function to check if given info will lead to a valid configuration of NEIm2Col.
Definition: NEIm2Col.cpp:44
Num samples, channels, height, width.
Basic function to run NEIm2ColKernel.
Definition: NEIm2Col.h:39
TensorInfo src_info(src_shape, 1, data_type)
FIXTURE_DATA_TEST_CASE(RunSmall, CLAbsLayerFixture< half >, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)))
Definition: AbsLayer.cpp:50
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:59
validate(dst.info() ->valid_region(), valid_region)
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
const QuantizationInfo qinfo
Definition: Im2Col.cpp:150
Num samples, height, width, channels.
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
Input data sets.
Store the tensor's metadata.
Definition: TensorInfo.h:45
JoinDataset< T, U > concat(T &&dataset1, U &&dataset2)
Helper function to create a JoinDataset.
Definition: JoinDataset.h:160
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}))
DataType
Available data types.
Definition: Types.h:75
DataLayout
[DataLayout enum definition]
Definition: Types.h:117
virtual bool extend_padding(const PaddingSize &padding)=0
Update the offset to the first element, the strides and the total size.
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)
Calculate the im2col output shape of a tensor.
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65
TEST_SUITE(U8_to_S8) DATA_TEST_CASE(Configuration
DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), AbsoluteDifferenceU8Dataset), shape, data_type0, data_type1, output_data_type)