1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2022 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 "arm_compute/runtime/CL/CLTensor.h"
26*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTensorAllocator.h"
27*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLIndirectConvolutionLayer.h"
28*c217d954SCole Faust #include "tests/CL/CLAccessor.h"
29*c217d954SCole Faust #include "tests/datasets/ShapeDatasets.h"
30*c217d954SCole Faust #include "tests/framework/Macros.h"
31*c217d954SCole Faust #include "tests/validation/Validation.h"
32*c217d954SCole Faust #include "tests/validation/fixtures/DirectConvolutionLayerFixture.h"
33*c217d954SCole Faust
34*c217d954SCole Faust // Note: Since the interface of indirect convolution is the same of direct convolution, we can reuse
35*c217d954SCole Faust // the direct convolution fixture
36*c217d954SCole Faust
37*c217d954SCole Faust namespace arm_compute
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace test
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace validation
42*c217d954SCole Faust {
43*c217d954SCole Faust namespace
44*c217d954SCole Faust {
45*c217d954SCole Faust RelativeTolerance<half> tolerance_fp16(half(0.2)); /**< Tolerance for floating point tests */
46*c217d954SCole Faust RelativeTolerance<float> tolerance_fp32(0.05f); /**< Tolerance for floating point tests */
47*c217d954SCole Faust constexpr float abs_tolerance_f32(0.0001f); /**< Absolute tolerance for FP32 tests*/
48*c217d954SCole Faust constexpr float tolerance_num = 0.07f; /**< Tolerance number */
49*c217d954SCole Faust
50*c217d954SCole Faust /** Activation function Dataset*/
51*c217d954SCole Faust const auto ActivationFunctionsDataset = framework::dataset::make("ActivationInfo",
52*c217d954SCole Faust { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 0.5f) });
53*c217d954SCole Faust } // namespace
54*c217d954SCole Faust
55*c217d954SCole Faust TEST_SUITE(CL)
TEST_SUITE(IndirectConvolutionLayer)56*c217d954SCole Faust TEST_SUITE(IndirectConvolutionLayer)
57*c217d954SCole Faust
58*c217d954SCole Faust /** Check whether the configuration of a indirect convolution layer with no
59*c217d954SCole Faust * bias leads to a successful run.
60*c217d954SCole Faust */
61*c217d954SCole Faust TEST_CASE(NoBias, framework::DatasetMode::PRECOMMIT)
62*c217d954SCole Faust {
63*c217d954SCole Faust const TensorShape src_shape_nhwc = TensorShape(8U, 27U, 13U);
64*c217d954SCole Faust const TensorShape wei_shape_nhwc = TensorShape(8U, 3U, 3U, 4U);
65*c217d954SCole Faust const TensorShape bia_shape = TensorShape(4U);
66*c217d954SCole Faust const TensorShape dst_shape_nhwc = TensorShape(4U, 25U, 11U);
67*c217d954SCole Faust constexpr DataType dt = DataType::F32;
68*c217d954SCole Faust constexpr DataLayout data_layout = DataLayout::NHWC;
69*c217d954SCole Faust
70*c217d954SCole Faust auto src_nhwc = create_tensor<CLTensor>(src_shape_nhwc, dt, 1, QuantizationInfo(), data_layout);
71*c217d954SCole Faust auto wei_nhwc = create_tensor<CLTensor>(wei_shape_nhwc, dt, 1, QuantizationInfo(), data_layout);
72*c217d954SCole Faust auto dst_nhwc = create_tensor<CLTensor>(dst_shape_nhwc, dt, 1, QuantizationInfo(), data_layout);
73*c217d954SCole Faust
74*c217d954SCole Faust TensorShape src_shape_nchw = src_shape_nhwc;
75*c217d954SCole Faust TensorShape wei_shape_nchw = wei_shape_nhwc;
76*c217d954SCole Faust TensorShape dst_shape_nchw = dst_shape_nhwc;
77*c217d954SCole Faust
78*c217d954SCole Faust permute(src_shape_nchw, PermutationVector(1U, 2U, 0U));
79*c217d954SCole Faust permute(wei_shape_nchw, PermutationVector(1U, 2U, 0U, 3U));
80*c217d954SCole Faust permute(dst_shape_nchw, PermutationVector(1U, 2U, 0U));
81*c217d954SCole Faust
82*c217d954SCole Faust const PadStrideInfo conv_info = PadStrideInfo(1, 1, 0, 0);
83*c217d954SCole Faust
84*c217d954SCole Faust // Create indirect Convolution function
85*c217d954SCole Faust CLIndirectConvolutionLayer conv{};
86*c217d954SCole Faust conv.configure(&src_nhwc, &wei_nhwc, nullptr, &dst_nhwc, conv_info);
87*c217d954SCole Faust
88*c217d954SCole Faust src_nhwc.allocator()->allocate();
89*c217d954SCole Faust wei_nhwc.allocator()->allocate();
90*c217d954SCole Faust dst_nhwc.allocator()->allocate();
91*c217d954SCole Faust
92*c217d954SCole Faust library->fill_tensor_value(CLAccessor(src_nhwc), 1.f);
93*c217d954SCole Faust library->fill_tensor_value(CLAccessor(wei_nhwc), 1.f);
94*c217d954SCole Faust
95*c217d954SCole Faust conv.run();
96*c217d954SCole Faust
97*c217d954SCole Faust // Compute reference to compare
98*c217d954SCole Faust SimpleTensor<float> ref_src{ src_shape_nchw, dt };
99*c217d954SCole Faust SimpleTensor<float> ref_wei{ wei_shape_nchw, dt };
100*c217d954SCole Faust SimpleTensor<float> ref_bia{ bia_shape, dt };
101*c217d954SCole Faust library->fill_tensor_value(ref_src, 1.f);
102*c217d954SCole Faust library->fill_tensor_value(ref_wei, 1.f);
103*c217d954SCole Faust // No bias
104*c217d954SCole Faust library->fill_tensor_value(ref_bia, 0.f);
105*c217d954SCole Faust auto ref_dst = reference::convolution_layer<float>(ref_src, ref_wei, ref_bia, dst_shape_nchw, conv_info);
106*c217d954SCole Faust
107*c217d954SCole Faust validate(CLAccessor(dst_nhwc), ref_dst);
108*c217d954SCole Faust }
109*c217d954SCole Faust
110*c217d954SCole Faust /** Check whether the case of rectangle kernels i.e. when width and height of the weight_shape are not equal
111*c217d954SCole Faust * would lead to successful run
112*c217d954SCole Faust */
TEST_CASE(NonSquareKernel,framework::DatasetMode::PRECOMMIT)113*c217d954SCole Faust TEST_CASE(NonSquareKernel, framework::DatasetMode::PRECOMMIT)
114*c217d954SCole Faust {
115*c217d954SCole Faust const TensorShape src_shape_nhwc = TensorShape(3U, 33U, 27U);
116*c217d954SCole Faust const TensorShape wei_shape_nhwc = TensorShape(3U, 5U, 7U, 4U); // non-square kernel
117*c217d954SCole Faust const TensorShape bia_shape = TensorShape(4U);
118*c217d954SCole Faust const TensorShape dst_shape_nhwc = TensorShape(4U, 11U, 12U);
119*c217d954SCole Faust constexpr DataType dt = DataType::F32;
120*c217d954SCole Faust constexpr DataLayout data_layout = DataLayout::NHWC;
121*c217d954SCole Faust
122*c217d954SCole Faust auto src_nhwc = create_tensor<CLTensor>(src_shape_nhwc, dt, 1, QuantizationInfo(), data_layout);
123*c217d954SCole Faust auto wei_nhwc = create_tensor<CLTensor>(wei_shape_nhwc, dt, 1, QuantizationInfo(), data_layout);
124*c217d954SCole Faust auto dst_nhwc = create_tensor<CLTensor>(dst_shape_nhwc, dt, 1, QuantizationInfo(), data_layout);
125*c217d954SCole Faust
126*c217d954SCole Faust TensorShape src_shape_nchw = src_shape_nhwc;
127*c217d954SCole Faust TensorShape wei_shape_nchw = wei_shape_nhwc;
128*c217d954SCole Faust TensorShape dst_shape_nchw = dst_shape_nhwc;
129*c217d954SCole Faust
130*c217d954SCole Faust permute(src_shape_nchw, PermutationVector(1U, 2U, 0U));
131*c217d954SCole Faust permute(wei_shape_nchw, PermutationVector(1U, 2U, 0U, 3U));
132*c217d954SCole Faust permute(dst_shape_nchw, PermutationVector(1U, 2U, 0U));
133*c217d954SCole Faust
134*c217d954SCole Faust const PadStrideInfo conv_info = PadStrideInfo(3, 2, 1, 1, 2, 0, DimensionRoundingType::FLOOR);
135*c217d954SCole Faust
136*c217d954SCole Faust // Create indirect convolution function
137*c217d954SCole Faust CLIndirectConvolutionLayer conv{};
138*c217d954SCole Faust conv.configure(&src_nhwc, &wei_nhwc, nullptr, &dst_nhwc, conv_info);
139*c217d954SCole Faust
140*c217d954SCole Faust src_nhwc.allocator()->allocate();
141*c217d954SCole Faust wei_nhwc.allocator()->allocate();
142*c217d954SCole Faust dst_nhwc.allocator()->allocate();
143*c217d954SCole Faust
144*c217d954SCole Faust library->fill_tensor_value(CLAccessor(src_nhwc), 1.f);
145*c217d954SCole Faust library->fill_tensor_value(CLAccessor(wei_nhwc), 1.f);
146*c217d954SCole Faust
147*c217d954SCole Faust conv.run();
148*c217d954SCole Faust
149*c217d954SCole Faust // Compute reference to compare
150*c217d954SCole Faust SimpleTensor<float> ref_src{ src_shape_nchw, dt };
151*c217d954SCole Faust SimpleTensor<float> ref_wei{ wei_shape_nchw, dt };
152*c217d954SCole Faust SimpleTensor<float> ref_bia{ bia_shape, dt };
153*c217d954SCole Faust library->fill_tensor_value(ref_src, 1.f);
154*c217d954SCole Faust library->fill_tensor_value(ref_wei, 1.f);
155*c217d954SCole Faust // No bias
156*c217d954SCole Faust library->fill_tensor_value(ref_bia, 0.f);
157*c217d954SCole Faust auto ref_dst = reference::convolution_layer<float>(ref_src, ref_wei, ref_bia, dst_shape_nchw, conv_info);
158*c217d954SCole Faust
159*c217d954SCole Faust validate(CLAccessor(dst_nhwc), ref_dst);
160*c217d954SCole Faust }
161*c217d954SCole Faust // *INDENT-OFF*
162*c217d954SCole Faust // clang-format off
163*c217d954SCole Faust // Note: Since the interface of indirect convolution is the same of direct convolution, we can reuse
164*c217d954SCole Faust // the direct convolution fixture
165*c217d954SCole Faust template <typename T>
166*c217d954SCole Faust using CLIndirectConvolutionLayerFixture = DirectConvolutionValidationFixture<CLTensor, CLAccessor, CLIndirectConvolutionLayer, T>;
167*c217d954SCole Faust template <typename T>
168*c217d954SCole Faust using CLIndirectConvolutionLayerMixedDataLayoutFixture = DirectConvolutionValidationFixture<CLTensor, CLAccessor, CLIndirectConvolutionLayer, T, true>;
169*c217d954SCole Faust
170*c217d954SCole Faust TEST_SUITE(NHWC)
TEST_SUITE(FP16)171*c217d954SCole Faust TEST_SUITE(FP16)
172*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLIndirectConvolutionLayerFixture<half>, framework::DatasetMode::PRECOMMIT,
173*c217d954SCole Faust combine(combine(combine(zip(zip(zip(zip(zip(zip(
174*c217d954SCole Faust framework::dataset::make("InputShape", { TensorShape(27U, 13U, 23U),
175*c217d954SCole Faust TensorShape(19U, 5U, 16U, 4U),
176*c217d954SCole Faust TensorShape(13U, 5U, 17U, 2U),
177*c217d954SCole Faust TensorShape(32U, 37U, 13U) } ),
178*c217d954SCole Faust framework::dataset::make("StrideX", { 1, 3, 1, 1 })),
179*c217d954SCole Faust framework::dataset::make("StrideY", { 1, 3, 2, 1 })),
180*c217d954SCole Faust framework::dataset::make("PadX", { 1, 3, 0, 4 })),
181*c217d954SCole Faust framework::dataset::make("PadY", { 1, 3, 0, 4 })),
182*c217d954SCole Faust framework::dataset::make("KernelSize", { 3, 8, 1, 9 })),
183*c217d954SCole Faust framework::dataset::make("NumKernels", { 17, 3, 1, 19 })),
184*c217d954SCole Faust framework::dataset::make("DataType", DataType::F16)),
185*c217d954SCole Faust framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) )),
186*c217d954SCole Faust framework::dataset::make("DataLayout", DataLayout::NHWC)))
187*c217d954SCole Faust {
188*c217d954SCole Faust validate(CLAccessor(_target), _reference, tolerance_fp16, tolerance_num);
189*c217d954SCole Faust }
190*c217d954SCole Faust
191*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunLarge, CLIndirectConvolutionLayerFixture<half>, framework::DatasetMode::NIGHTLY,
192*c217d954SCole Faust combine(combine(combine(zip(zip(zip(zip(zip(zip(
193*c217d954SCole Faust framework::dataset::make("InputShape", { TensorShape(800U, 800U, 3U) } ),
194*c217d954SCole Faust framework::dataset::make("StrideX", { 1 })),
195*c217d954SCole Faust framework::dataset::make("StrideY", { 1 })),
196*c217d954SCole Faust framework::dataset::make("PadX", { 1 })),
197*c217d954SCole Faust framework::dataset::make("PadY", { 1 })),
198*c217d954SCole Faust framework::dataset::make("KernelSize", { 9 })),
199*c217d954SCole Faust framework::dataset::make("NumKernels", { 3 })),
200*c217d954SCole Faust framework::dataset::make("DataType", DataType::F16)),
201*c217d954SCole Faust framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::IDENTITY) )),
202*c217d954SCole Faust framework::dataset::make("DataLayout", DataLayout::NHWC)))
203*c217d954SCole Faust {
204*c217d954SCole Faust validate(CLAccessor(_target), _reference, tolerance_fp16, tolerance_num);
205*c217d954SCole Faust }
206*c217d954SCole Faust
207*c217d954SCole Faust TEST_SUITE_END() // FP16
208*c217d954SCole Faust
TEST_SUITE(FP32)209*c217d954SCole Faust TEST_SUITE(FP32)
210*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLIndirectConvolutionLayerFixture<float>, framework::DatasetMode::PRECOMMIT,
211*c217d954SCole Faust combine(combine(combine(zip(zip(zip(zip(zip(zip(
212*c217d954SCole Faust framework::dataset::make("InputShape", { TensorShape(27U, 13U, 23U),
213*c217d954SCole Faust TensorShape(19U, 5U, 16U, 4U),
214*c217d954SCole Faust TensorShape(13U, 5U, 17U, 2U),
215*c217d954SCole Faust TensorShape(32U, 37U, 13U) } ),
216*c217d954SCole Faust framework::dataset::make("StrideX", { 1, 3, 1, 1 })),
217*c217d954SCole Faust framework::dataset::make("StrideY", { 1, 3, 2, 1 })),
218*c217d954SCole Faust framework::dataset::make("PadX", { 1, 3, 0, 4 })),
219*c217d954SCole Faust framework::dataset::make("PadY", { 1, 3, 0, 4 })),
220*c217d954SCole Faust framework::dataset::make("KernelSize", { 3, 8, 1, 9 })),
221*c217d954SCole Faust framework::dataset::make("NumKernels", { 17, 3, 1, 19 })),
222*c217d954SCole Faust framework::dataset::make("DataType", DataType::F32)),
223*c217d954SCole Faust framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) )),
224*c217d954SCole Faust framework::dataset::make("DataLayout", DataLayout::NHWC)))
225*c217d954SCole Faust {
226*c217d954SCole Faust validate(CLAccessor(_target), _reference, tolerance_fp32, 0.0, abs_tolerance_f32);
227*c217d954SCole Faust }
228*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunMixedDataLayout, CLIndirectConvolutionLayerMixedDataLayoutFixture<float>, framework::DatasetMode::PRECOMMIT,
229*c217d954SCole Faust combine(combine(combine(zip(zip(zip(zip(zip(zip(
230*c217d954SCole Faust framework::dataset::make("InputShape", { TensorShape(27U, 13U, 23U),
231*c217d954SCole Faust TensorShape(19U, 5U, 16U, 4U),
232*c217d954SCole Faust TensorShape(13U, 5U, 17U, 2U),
233*c217d954SCole Faust TensorShape(32U, 37U, 13U) } ),
234*c217d954SCole Faust framework::dataset::make("StrideX", { 1 })),
235*c217d954SCole Faust framework::dataset::make("StrideY", { 2 })),
236*c217d954SCole Faust framework::dataset::make("PadX", { 1 })),
237*c217d954SCole Faust framework::dataset::make("PadY", { 3 })),
238*c217d954SCole Faust framework::dataset::make("KernelSize", { 3 })),
239*c217d954SCole Faust framework::dataset::make("NumKernels", { 3 })),
240*c217d954SCole Faust framework::dataset::make("DataType", DataType::F32)),
241*c217d954SCole Faust framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU) )),
242*c217d954SCole Faust framework::dataset::make("DataLayout", DataLayout::NHWC)))
243*c217d954SCole Faust {
244*c217d954SCole Faust validate(CLAccessor(_target), _reference, tolerance_fp32, 0.0, abs_tolerance_f32);
245*c217d954SCole Faust }
246*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunLarge, CLIndirectConvolutionLayerFixture<float>, framework::DatasetMode::NIGHTLY,
247*c217d954SCole Faust combine(combine(combine(zip(zip(zip(zip(zip(zip(
248*c217d954SCole Faust framework::dataset::make("InputShape", { TensorShape(800U, 800U, 3U) } ),
249*c217d954SCole Faust framework::dataset::make("StrideX", { 1 })),
250*c217d954SCole Faust framework::dataset::make("StrideY", { 1 })),
251*c217d954SCole Faust framework::dataset::make("PadX", { 1 })),
252*c217d954SCole Faust framework::dataset::make("PadY", { 1 })),
253*c217d954SCole Faust framework::dataset::make("KernelSize", { 9 })),
254*c217d954SCole Faust framework::dataset::make("NumKernels", { 3 })),
255*c217d954SCole Faust framework::dataset::make("DataType", DataType::F32)),
256*c217d954SCole Faust framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::IDENTITY) )),
257*c217d954SCole Faust framework::dataset::make("DataLayout", DataLayout::NHWC)))
258*c217d954SCole Faust {
259*c217d954SCole Faust validate(CLAccessor(_target), _reference, tolerance_fp32, 0.0, abs_tolerance_f32);
260*c217d954SCole Faust }
261*c217d954SCole Faust TEST_SUITE_END() // FP32
262*c217d954SCole Faust TEST_SUITE_END() // NHWC
263*c217d954SCole Faust TEST_SUITE_END() // IndirectConvolutionLayer
264*c217d954SCole Faust TEST_SUITE_END() // CL
265*c217d954SCole Faust
266*c217d954SCole Faust } // namespace validation
267*c217d954SCole Faust } // namespace test
268*c217d954SCole Faust } // namespace arm_compute
269