aboutsummaryrefslogtreecommitdiff
path: root/tests/validation/CL/Im2Col.cpp
blob: 7ccd0c32bf2251ee9c0bc613d9d07dcffefc3d6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * Copyright (c) 2018-2020 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "arm_compute/core/CL/kernels/CLIm2ColKernel.h"
#include "arm_compute/core/Types.h"

#include "tests/CL/CLAccessor.h"
#include "tests/CL/Helper.h"
#include "tests/framework/Asserts.h"
#include "tests/framework/Macros.h"
#include "tests/framework/datasets/Datasets.h"
#include "tests/validation/Validation.h"
#include "tests/validation/fixtures/Im2ColFixture.h"

namespace arm_compute
{
namespace test
{
namespace validation
{
TEST_SUITE(CL)
TEST_SUITE(Im2Col)

using CLIm2Col = CLSynthetizeFunction<CLIm2ColKernel>;

/** Negative tests
 *
 * A series of validation tests on configurations which according to the API specification
 * the function should fail against.
 *
 * Checks performed in order:
 *     - Pass unsupported data type for input
 *     - Pass a quantized input and ask to compress the bias into the resulting matrix
 *     - Pass a dilation factor of 0
 *     - Check NHWC data layout while requesting to perform a grouped operation
 *     - Check NCHW grouped operation when the number of channels is not multiple of the groups
 *     - Pass an invalid output shape
 */
TEST_CASE(Negative, framework::DatasetMode::ALL)
{
    // Unsupported data type
    {
        const auto input     = TensorInfo(TensorShape(10U, 12U, 1U, 2U), 1, DataType::SIZET);
        const auto output    = TensorInfo(TensorShape(9U, 10U, 12U, 2U), 1, DataType::F32);
        const auto conv_size = Size2D(3, 3);
        const bool has_bias  = false;
        const auto status    = CLIm2ColKernel::validate(&input, &output, conv_size, PadStrideInfo(), has_bias);
        ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
    }

    // Passing quantized input and ask to merge the bias in the output
    {
        const auto input     = TensorInfo(TensorShape(10U, 12U, 1U, 2U), 1, DataType::QASYMM8);
        const auto output    = TensorInfo(TensorShape(9U, 80U, 2U), 1, DataType::QASYMM8);
        const auto conv_size = Size2D(3, 3);
        const bool has_bias  = true;
        const auto status    = CLIm2ColKernel::validate(&input, &output, conv_size, PadStrideInfo(), has_bias);
        ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
    }

    // Invalid dilation
    {
        const auto input     = TensorInfo(TensorShape(10U, 12U, 1U, 2U), 1, DataType::F32);
        const auto output    = TensorInfo(TensorShape(9U, 80U, 2U), 1, DataType::F32);
        const auto conv_size = Size2D(3, 3);
        const auto dilation  = Size2D(0, 1);
        const bool has_bias  = false;
        const auto status    = CLIm2ColKernel::validate(&input, &output, conv_size, PadStrideInfo(), has_bias, dilation);
        ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
    }

    // NHWC and grouping greater than 1
    {
        const auto         input      = TensorInfo(TensorShape(10U, 12U, 1U, 2U), 1, DataType::F32, DataLayout::NHWC);
        const auto         output     = TensorInfo(TensorShape(9U, 80U, 2U), 1, DataType::F32);
        const auto         conv_size  = Size2D(3, 3);
        const auto         dilation   = Size2D(1, 1);
        const bool         has_bias   = false;
        const unsigned int num_groups = 2;
        const auto         status     = CLIm2ColKernel::validate(&input, &output, conv_size, PadStrideInfo(), has_bias, dilation, num_groups);
        ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
    }

    // NCWH and channels % num_groups !=0
    {
        const auto         input      = TensorInfo(TensorShape(10U, 12U, 1U, 2U), 1, DataType::F32, DataLayout::NCHW);
        const auto         output     = TensorInfo(TensorShape(9U, 80U, 2U), 1, DataType::F32);
        const auto         conv_size  = Size2D(3, 3);
        const auto         dilation   = Size2D(1, 1);
        const bool         has_bias   = false;
        const unsigned int num_groups = 2;
        const auto         status     = CLIm2ColKernel::validate(&input, &output, conv_size, PadStrideInfo(), has_bias, dilation, num_groups);
        ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
    }

    // Invalid output shape
    {
        const auto input     = TensorInfo(TensorShape(10U, 12U, 1U, 2U), 1, DataType::F32);
        const auto output    = TensorInfo(TensorShape(9U, 81U, 2U), 1, DataType::F32);
        const auto conv_size = Size2D(3, 3);
        const bool has_bias  = false;
        const auto status    = CLIm2ColKernel::validate(&input, &output, conv_size, PadStrideInfo(), has_bias);
        ARM_COMPUTE_EXPECT(bool(status) == false, framework::LogLevel::ERRORS);
    }
}

template <typename T>
using CLIm2ColFixture = Im2ColValidationFixture<CLTensor, CLAccessor, CLIm2Col, T, true>;

TEST_SUITE(NHWC)

/** Test special kernel used for NHWC for 3x3 kernels
 *
 * @note 2 elements processed per iteration
 *
 * Three tests will be run:
 *  - Channels are multiple of elements processed
 *  - Channels larger and non multiple of elements used
 *  - Channels smaller and not multiple of elements used
 *
 *  Kernel tested im2col3x3_nhwc
 */
FIXTURE_DATA_TEST_CASE(W3x3,
                       CLIm2ColFixture<float>,
                       framework::DatasetMode::ALL,
                       combine(combine(combine(combine(combine(combine(
                                                                   framework::dataset::make("InputShape",
{
    TensorShape(2U, 5U, 7U, 2U), TensorShape(3U, 4U, 6U, 2U), TensorShape(1U, 5U, 3U, 2U),
}),
framework::dataset::make("DataType", DataType::F32)),
framework::dataset::make("Kernel", Size2D(3, 3))),
framework::dataset::make("PadStride", PadStrideInfo(1, 2, 1, 2))),
framework::dataset::make("QInfo", QuantizationInfo())),
framework::dataset::make("DataLayout", DataLayout::NHWC)),
framework::dataset::make("Groups", 1)))
{
    // Validate output
    validate(CLAccessor(_target), _reference);
}

/** Test special kernel used for NHWC for 9x9 kernels
 *
 * @note 2 elements processed per iteration
 *
 * Three tests will be run:
 *  - Channels are multiple of elements processed
 *  - Channels larger and non multiple of elements used
 *  - Channels smaller and not multiple of elements used
 *
 *  Kernel tested im2col9x9_nhwc
 */
FIXTURE_DATA_TEST_CASE(W9x9,
                       CLIm2ColFixture<float>,
                       framework::DatasetMode::ALL,
                       combine(combine(combine(combine(combine(combine(
                                                                   framework::dataset::make("InputShape",
{
    TensorShape(2U, 13U, 15U, 2U), TensorShape(3U, 15U, 12U, 2U), TensorShape(1U, 1U, 2U, 2U),
}),
framework::dataset::make("DataType", DataType::F32)),
framework::dataset::make("Kernel", Size2D(9, 9))),
framework::dataset::make("PadStride", PadStrideInfo(2, 2, 1, 2))),
framework::dataset::make("QInfo", QuantizationInfo())),
framework::dataset::make("DataLayout", DataLayout::NHWC)),
framework::dataset::make("Groups", 1)))
{
    // Validate output
    validate(CLAccessor(_target), _reference);
}
TEST_SUITE_END() // NHWC

TEST_SUITE(NCHW)

/** Test special kernel used for NCHW for 1x1 kernels with stride 1 and no padding
 *
 * @note 4 elements processed per iteration
 *
 * Three tests will be run:
 *  - Channels are multiple of elements processed
 *  - Channels larger and non multiple of elements used
 *  - Channels smaller and not multiple of elements used
 *
 *  Kernel tested im2col1x1_stridex1_nchw
 */
FIXTURE_DATA_TEST_CASE(W1x1_Stride1_NoPad,
                       CLIm2ColFixture<float>,
                       framework::DatasetMode::ALL,
                       combine(combine(combine(combine(combine(combine(
                                                                   framework::dataset::make("InputShape", { TensorShape(4U, 4U, 3U, 2U), TensorShape(5U, 4U, 3U, 2U), TensorShape(3U, 4U, 3U, 2U) }),
                                                                   framework::dataset::make("DataType", DataType::F32)),
                                                               framework::dataset::make("Kernel", Size2D(1, 1))),
                                                       framework::dataset::make("PadStride", PadStrideInfo(1, 1, 0, 0))),
                                               framework::dataset::make("QInfo", QuantizationInfo())),
                                       framework::dataset::make("DataLayout", DataLayout::NCHW)),
                               framework::dataset::make("Groups", 1)))
{
    // Validate output
    validate(CLAccessor(_target), _reference);
}

/** Test special kernel used for NCHW for 3x3 kernels
 *
 * @note 1 elements processed per iteration
 *
 * Executed single test as padding is required.
 *
 *  Kernel tested im2col3x3_nchw
 */
FIXTURE_DATA_TEST_CASE(W3x3,
                       CLIm2ColFixture<float>,
                       framework::DatasetMode::ALL,
                       combine(combine(combine(combine(combine(combine(
                                                                   framework::dataset::make("InputShape", TensorShape(4U, 4U, 3U, 2U)),
                                                                   framework::dataset::make("DataType", DataType::F32)),
                                                               framework::dataset::make("Kernel", Size2D(3, 3))),
                                                       framework::dataset::make("PadStride", PadStrideInfo(1, 2, 1, 2))),
                                               framework::dataset::make("QInfo", QuantizationInfo())),
                                       framework::dataset::make("DataLayout", DataLayout::NCHW)),
                               framework::dataset::make("Groups", { 1, 3 })))
{
    // Validate output
    validate(CLAccessor(_target), _reference);
}

/** Test special kernel used for NCHW for 5x5 kernels
 *
 * @note 1 elements processed per iteration
 *
 * Executed single test as padding is required.
 *
 *  Kernel tested im2col5x5_nchw
 */
FIXTURE_DATA_TEST_CASE(W5x5,
                       CLIm2ColFixture<float>,
                       framework::DatasetMode::ALL,
                       combine(combine(combine(combine(combine(combine(
                                                                   framework::dataset::make("InputShape", TensorShape(7U, 4U, 3U, 2U)),
                                                                   framework::dataset::make("DataType", DataType::F32)),
                                                               framework::dataset::make("Kernel", Size2D(5, 5))),
                                                       framework::dataset::make("PadStride", PadStrideInfo(2, 1, 2, 1))),
                                               framework::dataset::make("QInfo", QuantizationInfo())),
                                       framework::dataset::make("DataLayout", DataLayout::NCHW)),
                               framework::dataset::make("Groups", { 1, 3 })))
{
    // Validate output
    validate(CLAccessor(_target), _reference);
}

/** Test special kernel used for NCHW for 11x11 kernels when no padding present
 *
 * @note 1 elements processed per iteration
 *
 * Two tests will be run:
 *  - Without padding requirements
 *  - With padding requirements
 *
 * Kernel tested im2col11x11_padx0_pady0_nchw
 */
FIXTURE_DATA_TEST_CASE(W11x11_NoPad,
                       CLIm2ColFixture<float>,
                       framework::DatasetMode::ALL,
                       combine(combine(combine(combine(combine(combine(
                                                                   framework::dataset::make("InputShape", { TensorShape(11U, 11U, 2U, 2U), TensorShape(14U, 13U, 1U, 2U) }),
                                                                   framework::dataset::make("DataType", DataType::F32)),
                                                               framework::dataset::make("Kernel", Size2D(11, 11))),
                                                       framework::dataset::make("PadStride", PadStrideInfo(1, 1, 0, 0))),
                                               framework::dataset::make("QInfo", QuantizationInfo())),
                                       framework::dataset::make("DataLayout", DataLayout::NCHW)),
                               framework::dataset::make("Groups", 1)))
{
    // Validate output
    validate(CLAccessor(_target), _reference);
}

/** Test special kernel used for NCHW for kernels which do not fall in the categories above and have no padding present
 *
 * @note 1 elements processed per iteration
 *
 * Executed single test as padding is required.
 *
 * Kernel tested im2col_generic_padx0_pady0_nchw
 */
FIXTURE_DATA_TEST_CASE(GenericZeroPad,
                       CLIm2ColFixture<float>,
                       framework::DatasetMode::ALL,
                       combine(combine(combine(combine(combine(combine(
                                                                   framework::dataset::make("InputShape", TensorShape(13U, 11U, 2U, 2U)),
                                                                   framework::dataset::make("DataType", DataType::F32)),
                                                               framework::dataset::make("Kernel", Size2D(3, 2))),
                                                       framework::dataset::make("PadStride", PadStrideInfo(2, 1, 0, 0))),
                                               framework::dataset::make("QInfo", QuantizationInfo())),
                                       framework::dataset::make("DataLayout", DataLayout::NCHW)),
                               framework::dataset::make("Groups", { 1, 2 })))
{
    // Validate output
    validate(CLAccessor(_target), _reference);
}
TEST_SUITE_END() // NCHW

/** Generic NCHW/NHWC kernel
 *
 * @note 1 elements processed per iteration
 *
 * Padding is not needed thus executed sample tests with different kernels sizes
 * and stride/padding information
 *
 * Kernel tested im2col_generic_(nchw|nhwc)
 */
FIXTURE_DATA_TEST_CASE(Generic,
                       CLIm2ColFixture<float>,
                       framework::DatasetMode::ALL,
                       combine(combine(combine(combine(combine(combine(
                                                                   framework::dataset::make("InputShape", TensorShape(13U, 11U, 2U, 2U)),
                                                                   framework::dataset::make("DataType", DataType::F32)),
                                                               framework::dataset::make("Kernel", { Size2D(3, 2), Size2D(3, 5) })),
                                                       framework::dataset::make("PadStride", PadStrideInfo(2, 1, 2, 1))),
                                               framework::dataset::make("QInfo", QuantizationInfo())),
                                       framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
                               framework::dataset::make("Groups", 1)))
{
    // Validate output
    validate(CLAccessor(_target), _reference);
}

/** Tests to check that quantized padding value is set correctly
 *
 * Kernels tested:
 *  - im2col_generic_nhwc
 *  - im2col_generic_nchw
 *  - im2col5x5_nchw
 *  - im2col3x3_nhwc
 *  - im2col3x3_nchw
 *  - im2col9x9_nhwc
 */
FIXTURE_DATA_TEST_CASE(Quantized,
                       CLIm2ColFixture<uint8_t>,
                       framework::DatasetMode::ALL,
                       combine(combine(combine(combine(combine(combine(
                                                                   framework::dataset::make("InputShape", TensorShape(13U, 11U, 2U, 2U)),
                                                                   framework::dataset::make("DataType", DataType::QASYMM8)),
                                                               framework::dataset::make("Kernel", { Size2D(1, 1), Size2D(3, 3), Size2D(5, 5), Size2D(3, 5), Size2D(9, 9) })),
                                                       framework::dataset::make("PadStride", { PadStrideInfo(1, 2, 1, 1) })),
                                               framework::dataset::make("QInfo", QuantizationInfo(0.5f, 10))),
                                       framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
                               framework::dataset::make("Groups", 1)))
{
    // Validate output
    validate(CLAccessor(_target), _reference);
}

/** Tests to check that half-precision execution
 *
 * Kernels tested:
 *  - im2col_generic_nhwc
 *  - im2col_generic_nchw
 *  - im2col5x5_nchw
 *  - im2col3x3_nhwc
 *  - im2col3x3_nchw
 *  - im2col9x9_nhwc
 */
FIXTURE_DATA_TEST_CASE(FP16,
                       CLIm2ColFixture<half>,
                       framework::DatasetMode::ALL,
                       combine(combine(combine(combine(combine(combine(
                                                                   framework::dataset::make("InputShape", TensorShape(13U, 11U, 2U, 2U)),
                                                                   framework::dataset::make("DataType", DataType::F16)),
                                                               framework::dataset::make("Kernel", { Size2D(1, 1), Size2D(3, 3), Size2D(5, 5), Size2D(3, 5), Size2D(9, 9) })),
                                                       framework::dataset::make("PadStride", { PadStrideInfo(1, 2, 1, 1) })),
                                               framework::dataset::make("QInfo", QuantizationInfo())),
                                       framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
                               framework::dataset::make("Groups", 1)))
{
    // Validate output
    validate(CLAccessor(_target), _reference);
}

TEST_SUITE_END() // Im2Col
TEST_SUITE_END() // CL
} // namespace validation
} // namespace test
} // namespace arm_compute