xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/NEON/Convolution3D.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 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/Helpers.h"
25 #include "arm_compute/core/Types.h"
26 #include "arm_compute/runtime/NEON/functions/NEConv3D.h"
27 #include "arm_compute/runtime/Tensor.h"
28 #include "arm_compute/runtime/TensorAllocator.h"
29 #include "tests/NEON/Accessor.h"
30 #include "tests/PaddingCalculator.h"
31 #include "tests/datasets/ShapeDatasets.h"
32 #include "tests/framework/Asserts.h"
33 #include "tests/framework/Macros.h"
34 #include "tests/framework/datasets/Datasets.h"
35 #include "tests/validation/Validation.h"
36 #include "tests/validation/fixtures/DirectConvolution3DFixture.h"
37 
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 namespace
45 {
46 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
47 const RelativeTolerance<half_float::half> rel_tolerance_f16(half_float::half(0.2f)); /**< Relative tolerance value for FP16 types */
48 const AbsoluteTolerance<float>            abs_tolerance_f16(0.2f);                   /**< Absolute tolerance for FP16 types */
49 constexpr float                           tolerance_num = 0.07f;                     /**< Tolerance number for the FP16 implementation */
50 #endif                                                                               /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
51 constexpr AbsoluteTolerance<float>   tolerance_fp32(0.001f);                         /**< Tolerance for floating point tests */
52 constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);                           /**< Tolerance for quantized tests */
53 
54 /** Activation function Dataset*/
55 const auto ActivationFunctionsDataset = framework::dataset::make("ActivationInfo",
56 {
57     ActivationLayerInfo(),
58     ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 0.5f)
59 });
60 
61 const auto data_precommit = combine(combine(zip(zip(zip(zip(zip(zip(zip(zip(zip(zip(
62                                                                                     datasets::SmallDirectConv3DShapes(),
63                                                                                     framework::dataset::make("StrideX", { 1, 5, 8 })),
64                                                                                 framework::dataset::make("StrideY", { 1, 2, 3 })),
65                                                                             framework::dataset::make("StrideZ", { 1, 2, 1 })),
66                                                                         framework::dataset::make("PadX", { 0, 1, 2 })),
67                                                                     framework::dataset::make("PadY", { 0, 2, 1 })),
68                                                                 framework::dataset::make("PadZ", { 0, 3, 5 })),
69                                                             framework::dataset::make("KernelWidth", { 3, 5, 9 })),
70                                                         framework::dataset::make("KernelHeight", { 2, 1, 3 })),
71                                                     framework::dataset::make("KernelDepth", { 1, 2, 3 })),
72                                                 framework::dataset::make("NumKernels", { 2, 3, 8 })),
73                                             framework::dataset::make("HasBias", { true, false })),
74                                     ActivationFunctionsDataset);
75 } // namespace
76 
77 TEST_SUITE(NEON)
TEST_SUITE(Convolution3D)78 TEST_SUITE(Convolution3D)
79 
80 // *INDENT-OFF*
81 // clang-format off
82 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
83         framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U, 4U), 1U, DataType::F32, DataLayout::NDHWC), // Mismatching data type input/weights
84                                                 TensorInfo(TensorShape(27U, 13U, 2U, 4U), 1U, DataType::F32, DataLayout::NDHWC), // Mismatching input feature maps
85                                                 TensorInfo(TensorShape(27U, 13U, 2U, 4U), 1U, DataType::F32, DataLayout::NDHWC), // Invalid weights dimensions
86                                                 TensorInfo(TensorShape(27U, 13U, 2U, 4U), 1U, DataType::F32, DataLayout::NHWC), // Invalid data layout
87                                                 TensorInfo(TensorShape(27U, 13U, 2U, 4U), 1U, DataType::F32, DataLayout::NDHWC), // Invalid biases size
88                                                 TensorInfo(TensorShape(27U, 13U, 2U, 4U), 1U, DataType::F32, DataLayout::NDHWC), // Invalid biases dimensions
89                                                 TensorInfo(TensorShape(27U, 13U, 2U, 4U), 1U, DataType::F32, DataLayout::NDHWC), // Invalid output size
90                                                 TensorInfo(TensorShape(27U, 13U, 2U, 4U), 1U, DataType::U32, DataLayout::NDHWC), // Invalid data type
91                                               }),
92         framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(4U, 3U, 3U, 3U, 2U), 1U, DataType::F16),
93                                                  TensorInfo(TensorShape(4U, 3U, 3U, 3U, 3U), 1U, DataType::F32),
94                                                  TensorInfo(TensorShape(4U, 3U, 3U, 3U, 2U, 3U), 1U, DataType::F32),
95                                                  TensorInfo(TensorShape(4U, 3U, 3U, 3U, 2U), 1U, DataType::F32),
96                                                  TensorInfo(TensorShape(4U, 3U, 3U, 3U, 2U), 1U, DataType::F32),
97                                                  TensorInfo(TensorShape(4U, 3U, 3U, 3U, 2U), 1U, DataType::F32),
98                                                  TensorInfo(TensorShape(4U, 3U, 3U, 3U, 2U), 1U, DataType::F32),
99                                                  TensorInfo(TensorShape(4U, 3U, 3U, 3U, 2U), 1U, DataType::U32),
100                                               })),
101         framework::dataset::make("BiasesInfo",{ TensorInfo(TensorShape(4U), 1U, DataType::F32),
102                                                 TensorInfo(TensorShape(4U), 1U, DataType::F32),
103                                                 TensorInfo(TensorShape(4U), 1U, DataType::F32),
104                                                 TensorInfo(TensorShape(4U), 1U, DataType::F32),
105                                                 TensorInfo(TensorShape(3U), 1U, DataType::F32),
106                                                 TensorInfo(TensorShape(4U, 2U), 1U, DataType::F32),
107                                                 TensorInfo(TensorShape(4U), 1U, DataType::F32),
108                                                 TensorInfo(TensorShape(4U), 1U, DataType::F32),
109                                               })),
110         framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(25U, 11U, 4U), 1U, DataType::F32),
111                                                 TensorInfo(TensorShape(25U, 11U, 4U), 1U, DataType::F32),
112                                                 TensorInfo(TensorShape(25U, 11U, 4U), 1U, DataType::F32),
113                                                 TensorInfo(TensorShape(25U, 11U, 4U), 1U, DataType::F32),
114                                                 TensorInfo(TensorShape(25U, 11U, 4U), 1U, DataType::F32),
115                                                 TensorInfo(TensorShape(25U, 11U, 4U), 1U, DataType::F32),
116                                                 TensorInfo(TensorShape(26U, 11U, 4U), 1U, DataType::F32),
117                                                 TensorInfo(TensorShape(25U, 11U, 4U), 1U, DataType::U32),
118                                               })),
119         framework::dataset::make("Expected", { false, false, false, false, false, false, false, false})),
120         input_info, weights_info, biases_info, output_info, expected)
121 {
122         const Conv3dInfo  conv3d_info(Size3D(1, 1, 1), Padding3D(0, 0, 0), ActivationLayerInfo(), Size3D(1U, 1U, 1U), DimensionRoundingType::FLOOR, false);
123         bool is_valid = bool(NEConv3D::validate(&input_info.clone()->set_is_resizable(false), &weights_info.clone()->set_is_resizable(false), &biases_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), conv3d_info));
124         ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
125 }
126 // clang-format on
127 // *INDENT-ON*
128 
129 template <typename T>
130 using NEDirectConvolution3DFixture = DirectConvolution3DValidationFixture<Tensor, Accessor, NEConv3D, T>;
131 
132 TEST_SUITE(Float)
TEST_SUITE(FP32)133 TEST_SUITE(FP32)
134 FIXTURE_DATA_TEST_CASE(RunSmall, NEDirectConvolution3DFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(data_precommit,
135                                                                                                                  framework::dataset::make("DataType", DataType::F32)),
136                                                                                                                  framework::dataset::make("DataLayout", { DataLayout::NDHWC })))
137 {
138     // Validate output
139     validate(Accessor(_target), _reference, tolerance_fp32);
140 }
141 TEST_SUITE_END() // FP32
142 
143 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
TEST_SUITE(FP16)144 TEST_SUITE(FP16)
145 FIXTURE_DATA_TEST_CASE(RunSmall, NEDirectConvolution3DFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(data_precommit,
146                                                                                                                         framework::dataset::make("DataType", DataType::F16)),
147                                                                                                                 framework::dataset::make("DataLayout", { DataLayout::NDHWC })))
148 {
149     // Validate output
150     validate(Accessor(_target), _reference, rel_tolerance_f16, tolerance_num, abs_tolerance_f16);
151 }
152 TEST_SUITE_END() // FP16
153 #endif           /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
154 
155 TEST_SUITE_END() // Float
156 
157 template <typename T>
158 using NEDirectConvolution3DQuantizedFixture = DirectConvolution3DValidationQuantizedFixture<Tensor, Accessor, NEConv3D, T>;
159 
160 TEST_SUITE(Quantized)
TEST_SUITE(QASYMM8)161 TEST_SUITE(QASYMM8)
162 FIXTURE_DATA_TEST_CASE(RunSmall, NEDirectConvolution3DQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT,
163                        combine(combine(combine(combine(combine(combine(zip(zip(zip(zip(zip(zip(zip(zip(zip(zip(zip(
164                                                                                                                    framework::dataset::make("InputShape", { TensorShape(7U, 5U, 3U, 13U, 3U),
165                                                                                                                            TensorShape(15U, 7U, 11U, 7U),
166                                                                                                                            TensorShape(19U, 5U, 16U, 4U),
167                                                                                                                            TensorShape(13U, 5U, 17U, 2U)
168                                                                                                                                                           }),
169                                                                                                                    framework::dataset::make("StrideX", { 1, 3, 2, 1 })),
170                                                                                                                framework::dataset::make("StrideY", { 2, 1, 3, 1 })),
171                                                                                                            framework::dataset::make("StrideZ", { 3, 2, 1, 1 })),
172                                                                                                        framework::dataset::make("PadX", { 0, 2, 1, 0 })),
173                                                                                                    framework::dataset::make("PadY", { 1, 0, 2, 0 })),
174                                                                                                framework::dataset::make("PadZ", { 2, 1, 0, 0 })),
175                                                                                            framework::dataset::make("KernelWidth", { 3, 7, 5, 1 })),
176                                                                                        framework::dataset::make("KernelHeight", { 5, 3, 7, 1 })),
177                                                                                    framework::dataset::make("KernelDepth", { 7, 5, 3, 1 })),
178                                                                                framework::dataset::make("NumKernels", { 5, 3, 1, 11 })),
179                                                                            framework::dataset::make("HasBias", { true, true, true, false })),
180                                                                        framework::dataset::make("Activation", ActivationLayerInfo())),
181                                                                framework::dataset::make("DataType", DataType::QASYMM8)),
182                                                        framework::dataset::make("DataLayout", DataLayout::NDHWC)),
183                                                framework::dataset::make("SrcQuantizationInfo", QuantizationInfo(0.1f, 10))),
184                                        framework::dataset::make("WeightsQuantizationInfo", QuantizationInfo(0.3f, 20))),
185                                framework::dataset::make("DstQuantizationInfo", QuantizationInfo(0.2f, 5))))
186 {
187     validate(Accessor(_target), _reference, tolerance_qasymm8);
188 }
189 
190 TEST_SUITE_END() // QASYMM8
191 
TEST_SUITE(QASYMM8_SIGNED)192 TEST_SUITE(QASYMM8_SIGNED)
193 FIXTURE_DATA_TEST_CASE(RunSmall, NEDirectConvolution3DQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT,
194                        combine(combine(combine(combine(combine(combine(zip(zip(zip(zip(zip(zip(zip(zip(zip(zip(zip(
195                                                                                                                    framework::dataset::make("InputShape", { TensorShape(7U, 5U, 3U, 13U, 3U),
196                                                                                                                            TensorShape(15U, 7U, 11U, 7U),
197                                                                                                                            TensorShape(19U, 5U, 16U, 4U),
198                                                                                                                            TensorShape(13U, 5U, 17U, 2U)
199                                                                                                                                                           }),
200                                                                                                                    framework::dataset::make("StrideX", { 1, 3, 2, 1 })),
201                                                                                                                framework::dataset::make("StrideY", { 2, 1, 3, 1 })),
202                                                                                                            framework::dataset::make("StrideZ", { 3, 2, 1, 1 })),
203                                                                                                        framework::dataset::make("PadX", { 0, 2, 1, 0 })),
204                                                                                                    framework::dataset::make("PadY", { 1, 0, 2, 0 })),
205                                                                                                framework::dataset::make("PadZ", { 2, 1, 0, 0 })),
206                                                                                            framework::dataset::make("KernelWidth", { 3, 7, 5, 1 })),
207                                                                                        framework::dataset::make("KernelHeight", { 5, 3, 7, 1 })),
208                                                                                    framework::dataset::make("KernelDepth", { 7, 5, 3, 1 })),
209                                                                                framework::dataset::make("NumKernels", { 5, 3, 1, 11 })),
210                                                                            framework::dataset::make("HasBias", { true, true, true, false })),
211                                                                        framework::dataset::make("Activation", ActivationLayerInfo())),
212                                                                framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
213                                                        framework::dataset::make("DataLayout", DataLayout::NDHWC)),
214                                                framework::dataset::make("SrcQuantizationInfo", QuantizationInfo(0.1f, 10))),
215                                        framework::dataset::make("WeightsQuantizationInfo", QuantizationInfo(0.3f, 20))),
216                                framework::dataset::make("DstQuantizationInfo", QuantizationInfo(0.2f, 5))))
217 {
218     validate(Accessor(_target), _reference, tolerance_qasymm8);
219 }
220 
221 TEST_SUITE_END() // QASYMM8_SIGNED
222 TEST_SUITE_END() // Quantized
223 
224 TEST_SUITE_END() // Convolution3D
225 TEST_SUITE_END() // Neon
226 } // namespace validation
227 } // namespace test
228 } // namespace arm_compute
229