xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/NEON/DilatedConvolutionLayer.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-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"
25 #include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h"
26 #include "arm_compute/runtime/NEON/functions/NEGEMMConvolutionLayer.h"
27 #include "arm_compute/runtime/Tensor.h"
28 #include "arm_compute/runtime/TensorAllocator.h"
29 #include "src/cpu/operators/CpuConv2d.h"
30 #include "tests/NEON/Accessor.h"
31 #include "tests/PaddingCalculator.h"
32 #include "tests/datasets/DilatedConvolutionLayerDataset.h"
33 #include "tests/framework/Asserts.h"
34 #include "tests/framework/Macros.h"
35 #include "tests/framework/datasets/Datasets.h"
36 #include "tests/validation/Validation.h"
37 #include "tests/validation/fixtures/ConvolutionLayerFixture.h"
38 
39 namespace arm_compute
40 {
41 namespace test
42 {
43 namespace validation
44 {
45 namespace
46 {
47 const AbsoluteTolerance<float> tolerance_f32(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
48 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
49 const AbsoluteTolerance<float>            abs_tolerance_f16(0.3f);                   /**< Absolute tolerance value for comparing reference's output against implementation's output for DataType::F16 */
50 const RelativeTolerance<half_float::half> rel_tolerance_f16(half_float::half(0.2f)); /**< Relative tolerance value for comparing reference's output against implementation's output for DataType::F16 */
51 constexpr float                           tolerance_num_f16 = 0.07f;                 /**< Tolerance number for FP16 */
52 #endif                                                                               /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
53 constexpr AbsoluteTolerance<float> tolerance_qasymm8(0.0);                           /**< Tolerance value for comparing reference's output against implementation's output for quantized data types */
54 
55 /** CNN data types */
56 const auto CNNDataTypes = framework::dataset::make("DataType",
57 {
58 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
59     DataType::F16,
60 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
61     DataType::F32,
62     DataType::QASYMM8,
63 });
64 } // namespace
65 
66 TEST_SUITE(NEON)
TEST_SUITE(DilatedConvolutionLayer)67 TEST_SUITE(DilatedConvolutionLayer)
68 
69 // *INDENT-OFF*
70 // clang-format off
71 DATA_TEST_CASE(ValidateConvolutionMethod, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(
72                                           framework::dataset::make("InputInfo", { TensorInfo(TensorShape(8U, 8U, 2U), 1, DataType::F32),
73                                                                                   TensorInfo(TensorShape(23U, 27U, 5U, 4U), 1, DataType::F32),
74                                                                                   TensorInfo(TensorShape(3U, 3U, 2U, 1U), 1, DataType::F32),
75                                                                                   TensorInfo(TensorShape(33U, 27U, 7U, 4U), 1, DataType::F32)
76                                           }),
77                                           framework::dataset::make("WeightsInfo", { TensorInfo(TensorShape(3U, 3U, 5U, 21U), 1, DataType::F32),
78                                                                                     TensorInfo(TensorShape(3U, 3U, 5U, 21U), 1, DataType::F32),
79                                                                                     TensorInfo(TensorShape(3U, 3U, 5U, 21U), 1, DataType::F32),
80                                                                                     TensorInfo(TensorShape(5U, 5U, 7U, 16U), 1, DataType::F16)
81                                           })),
82                                           framework::dataset::make("OutputInfo", { TensorInfo(TensorShape(6U, 6U, 1U), 1, DataType::F32),
83                                                                                    TensorInfo(TensorShape(21U, 25U, 21U, 4U), 1, DataType::F32),
84                                                                                    TensorInfo(TensorShape(11U, 25U, 21U), 1, DataType::F32),
85                                                                                    TensorInfo(TensorShape(11U, 12U, 16U, 4U), 1, DataType::F32)
86                                           })),
87                                           framework::dataset::make("ConvInfo", { PadStrideInfo(1, 1, 0, 0),
88                                                                                  PadStrideInfo(1, 1, 0, 0),
89                                                                                  PadStrideInfo(2, 1, 0, 0),
90                                                                                  PadStrideInfo(3, 2, 1, 0)
91                                           })),
92                                           framework::dataset::make("Dilation", { Size2D(1U, 2U),
93                                                                                  Size2D(2U, 1U),
94                                                                                  Size2D(2U, 2U),
95                                                                                  Size2D(3U, 3U)
96                                           })),
97                                           framework::dataset::make("Expected", { ConvolutionMethod::GEMM, ConvolutionMethod::GEMM, ConvolutionMethod::GEMM, ConvolutionMethod::GEMM })),
98                input_info, weights_info, output_info, conv_info, dilation, expected)
99 {
100     ConvolutionMethod is_valid = cpu::CpuConv2d::get_convolution_method(&input_info.clone()->set_is_resizable(false),
101                                                                             &weights_info.clone()->set_is_resizable(false),
102                                                                             &output_info.clone()->set_is_resizable(false),
103                                                                             conv_info, WeightsInfo(), dilation);
104     ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
105 }
106 // clang-format on
107 // *INDENT-ON*
108 TEST_SUITE_END() // DilatedConvolutionLayer
109 
110 TEST_SUITE(GEMMDilatedConvolutionLayer)
111 
112 template <typename T>
113 using NEGEMMDilatedConvolutionLayerFixture = ConvolutionValidationFixture<Tensor, Accessor, NEConvolutionLayer, T>;
114 
115 TEST_SUITE(Float)
116 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
TEST_SUITE(FP16)117 TEST_SUITE(FP16)
118 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMDilatedConvolutionLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallDilatedConvolutionLayerDataset(),
119                                                                                                                         framework::dataset::make("ReshapeWeights", { true })),
120                                                                                                                         framework::dataset::make("DataType", DataType::F16)),
121                                                                                                                         framework::dataset::make("DataLayout", { DataLayout::NCHW })),
122                                                                                                                         framework::dataset::make("ActivationLayerInfo", ActivationLayerInfo())))
123 {
124     // Validate output
125     validate(Accessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16, abs_tolerance_f16);
126 }
127 FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMDilatedConvolutionLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(datasets::LargeDilatedConvolutionLayerDataset(),
128                                                                                                                       framework::dataset::make("ReshapeWeights", { true })),
129                                                                                                                       framework::dataset::make("DataType", DataType::F16)),
130                                                                                                                       framework::dataset::make("DataLayout", { DataLayout::NCHW })),
131                                                                                                                       framework::dataset::make("ActivationLayerInfo", ActivationLayerInfo())))
132 {
133     // Validate output
134     validate(Accessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16, abs_tolerance_f16);
135 }
136 TEST_SUITE_END() // FP16
137 #endif           /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
138 
TEST_SUITE(FP32)139 TEST_SUITE(FP32)
140 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMDilatedConvolutionLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallDilatedConvolutionLayerDataset(),
141                        framework::dataset::make("ReshapeWeights", { true })),
142                        framework::dataset::make("DataType", DataType::F32)),
143                        framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
144                        framework::dataset::make("ActivationLayerInfo", ActivationLayerInfo())))
145 {
146     // Validate output
147     validate(Accessor(_target), _reference, tolerance_f32);
148 }
149 FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMDilatedConvolutionLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(datasets::LargeDilatedConvolutionLayerDataset(),
150                                                                                                                        framework::dataset::make("ReshapeWeights", { true })),
151                                                                                                                        framework::dataset::make("DataType", DataType::F32)),
152                                                                                                                        framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
153                                                                                                                        framework::dataset::make("ActivationLayerInfo", ActivationLayerInfo())))
154 {
155     // Validate output
156     validate(Accessor(_target), _reference, tolerance_f32);
157 }
158 TEST_SUITE_END() // FP32
159 TEST_SUITE_END() // Float
160 
161 template <typename T>
162 using NEGEMMDilatedConvolutionLayerQuantizedFixture = ConvolutionValidationQuantizedFixture<Tensor, Accessor, NEGEMMConvolutionLayer, T>;
163 
164 TEST_SUITE(Quantized)
TEST_SUITE(QASYMM8)165 TEST_SUITE(QASYMM8)
166 FIXTURE_DATA_TEST_CASE(RunSmall, NEGEMMDilatedConvolutionLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT,
167                        combine(combine(combine(combine(combine(datasets::SmallDilatedConvolutionLayerDataset(),
168                                                                framework::dataset::make("ReshapeWeights", { true })),
169                                                        framework::dataset::make("DataType", DataType::QASYMM8)),
170                                                framework::dataset::make("DataLayout", { DataLayout::NCHW })),
171                                        framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 255.f, 10) })),
172                                framework::dataset::make("ActivationLayerInfo", ActivationLayerInfo())))
173 {
174     // Validate output
175     validate(Accessor(_target), _reference, tolerance_qasymm8);
176 }
177 FIXTURE_DATA_TEST_CASE(RunLarge, NEGEMMDilatedConvolutionLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY,
178                        combine(combine(combine(combine(combine(datasets::LargeDilatedConvolutionLayerDataset(),
179                                                                framework::dataset::make("ReshapeWeights", { true })),
180                                                        framework::dataset::make("DataType", DataType::QASYMM8)),
181                                                framework::dataset::make("DataLayout", { DataLayout::NCHW })),
182                                        framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 255.f, 10) })),
183                                framework::dataset::make("ActivationLayerInfo", ActivationLayerInfo())))
184 {
185     // Validate output
186     validate(Accessor(_target), _reference, tolerance_qasymm8);
187 }
188 TEST_SUITE_END() // QASYMM8
189 TEST_SUITE_END() // Quantized
190 
191 TEST_SUITE_END() // GEMMDilatedConvolutionLayer
192 TEST_SUITE_END() // Neon
193 } // namespace validation
194 } // namespace test
195 } // namespace arm_compute
196