1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2021 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/CLBatchNormalizationLayer.h"
28*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
29*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h"
30*c217d954SCole Faust #include "tests/CL/CLAccessor.h"
31*c217d954SCole Faust #include "tests/PaddingCalculator.h"
32*c217d954SCole Faust #include "tests/datasets/LargeConvolutionLayerDataset.h"
33*c217d954SCole Faust #include "tests/datasets/RandomBatchNormalizationLayerDataset.h"
34*c217d954SCole Faust #include "tests/datasets/SmallConvolutionLayerDataset.h"
35*c217d954SCole Faust #include "tests/framework/Asserts.h"
36*c217d954SCole Faust #include "tests/framework/Macros.h"
37*c217d954SCole Faust #include "tests/framework/datasets/Datasets.h"
38*c217d954SCole Faust #include "tests/validation/Helpers.h"
39*c217d954SCole Faust #include "tests/validation/Validation.h"
40*c217d954SCole Faust #include "tests/validation/fixtures/BatchNormalizationLayerFixture.h"
41*c217d954SCole Faust #include "tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h"
42*c217d954SCole Faust
43*c217d954SCole Faust namespace arm_compute
44*c217d954SCole Faust {
45*c217d954SCole Faust namespace test
46*c217d954SCole Faust {
47*c217d954SCole Faust namespace validation
48*c217d954SCole Faust {
49*c217d954SCole Faust namespace
50*c217d954SCole Faust {
51*c217d954SCole Faust RelativeTolerance<float> rel_tolerance_f32(0.05f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
52*c217d954SCole Faust constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.0001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
53*c217d954SCole Faust constexpr AbsoluteTolerance<float> tolerance_f16(0.02f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */
54*c217d954SCole Faust const auto act_infos = framework::dataset::make("ActivationInfo",
55*c217d954SCole Faust {
56*c217d954SCole Faust ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
57*c217d954SCole Faust ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
58*c217d954SCole Faust ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f),
59*c217d954SCole Faust });
60*c217d954SCole Faust
61*c217d954SCole Faust const auto common_fusion_dataset = combine(combine(combine(framework::dataset::make("UseBias",
62*c217d954SCole Faust { false, true }),
63*c217d954SCole Faust framework::dataset::make("UseBeta", { false, true })),
64*c217d954SCole Faust framework::dataset::make("UseGamma", { false, true })),
65*c217d954SCole Faust framework::dataset::make("Epsilon", { 0.001f }));
66*c217d954SCole Faust
67*c217d954SCole Faust } // namespace
68*c217d954SCole Faust
69*c217d954SCole Faust TEST_SUITE(CL)
70*c217d954SCole Faust TEST_SUITE(BatchNormalizationLayer)
71*c217d954SCole Faust
72*c217d954SCole Faust template <typename T>
73*c217d954SCole Faust using CLBatchNormalizationLayerFixture = BatchNormalizationLayerValidationFixture<CLTensor, CLAccessor, CLBatchNormalizationLayer, T>;
74*c217d954SCole Faust
75*c217d954SCole Faust // *INDENT-OFF*
76*c217d954SCole Faust // clang-format off
77*c217d954SCole Faust DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
78*c217d954SCole Faust framework::dataset::make("InputInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
79*c217d954SCole Faust TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
80*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
81*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
82*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape
83*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Unsupported fused activation
84*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Fused activation's a < b
85*c217d954SCole Faust }),
86*c217d954SCole Faust framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
87*c217d954SCole Faust TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
88*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
89*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F16),
90*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
91*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
92*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
93*c217d954SCole Faust })),
94*c217d954SCole Faust framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32),
95*c217d954SCole Faust TensorInfo(TensorShape(2U), 1, DataType::F32),
96*c217d954SCole Faust TensorInfo(TensorShape(2U), 1, DataType::F16),
97*c217d954SCole Faust TensorInfo(TensorShape(2U), 1, DataType::F32),
98*c217d954SCole Faust TensorInfo(TensorShape(5U), 1, DataType::F32),
99*c217d954SCole Faust TensorInfo(TensorShape(2U), 1, DataType::F32),
100*c217d954SCole Faust TensorInfo(TensorShape(2U), 1, DataType::F32),
101*c217d954SCole Faust })),
102*c217d954SCole Faust framework::dataset::make("ActivationLayerInfo",{ ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
103*c217d954SCole Faust ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
104*c217d954SCole Faust ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
105*c217d954SCole Faust ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
106*c217d954SCole Faust ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f),
107*c217d954SCole Faust ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
108*c217d954SCole Faust ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 2.f, 6.f),
109*c217d954SCole Faust })),
110*c217d954SCole Faust framework::dataset::make("Expected", { true, false, false, false, false, false, false})),
111*c217d954SCole Faust input_info, output_info, mvbg_info, act_info, expected)
112*c217d954SCole Faust {
113*c217d954SCole Faust const auto &mean_info = mvbg_info;
114*c217d954SCole Faust const auto &var_info = mvbg_info;
115*c217d954SCole Faust const auto &beta_info = mvbg_info;
116*c217d954SCole Faust const auto &gamma_info = mvbg_info;
117*c217d954SCole Faust bool has_error = bool(CLBatchNormalizationLayer::validate(&input_info.clone()->set_is_resizable(false), (output_info.total_size() == 0) ? nullptr : &output_info.clone()->set_is_resizable(false), &mean_info.clone()->set_is_resizable(false), &var_info.clone()->set_is_resizable(false), &beta_info.clone()->set_is_resizable(false), &gamma_info.clone()->set_is_resizable(false), 1.f, act_info));
118*c217d954SCole Faust ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS);
119*c217d954SCole Faust }
120*c217d954SCole Faust // clang-format on
121*c217d954SCole Faust // *INDENT-ON*
122*c217d954SCole Faust
123*c217d954SCole Faust TEST_SUITE(Float)
TEST_SUITE(FP32)124*c217d954SCole Faust TEST_SUITE(FP32)
125*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallRandomBatchNormalizationLayerDataset(),
126*c217d954SCole Faust combine(framework::dataset::make("UseBeta", { false, true }), framework::dataset::make("UseGamma", { false, true }))),
127*c217d954SCole Faust act_infos),
128*c217d954SCole Faust framework::dataset::make("DataType", DataType::F32)),
129*c217d954SCole Faust framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
130*c217d954SCole Faust {
131*c217d954SCole Faust // Validate output
132*c217d954SCole Faust validate(CLAccessor(_target), _reference, abs_tolerance_f32, 0);
133*c217d954SCole Faust }
134*c217d954SCole Faust TEST_SUITE_END() //FP32
135*c217d954SCole Faust
TEST_SUITE(FP16)136*c217d954SCole Faust TEST_SUITE(FP16)
137*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallRandomBatchNormalizationLayerDataset(),
138*c217d954SCole Faust combine(framework::dataset::make("UseBeta", { false, true }), framework::dataset::make("UseGamma", { false, true }))),
139*c217d954SCole Faust framework::dataset::make("ActivationInfo",
140*c217d954SCole Faust ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))),
141*c217d954SCole Faust framework::dataset::make("DataType", DataType::F16)),
142*c217d954SCole Faust framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
143*c217d954SCole Faust {
144*c217d954SCole Faust // Validate output
145*c217d954SCole Faust validate(CLAccessor(_target), _reference, tolerance_f16, 0);
146*c217d954SCole Faust }
147*c217d954SCole Faust TEST_SUITE_END() // FP16
TEST_SUITE_END()148*c217d954SCole Faust TEST_SUITE_END() // Float
149*c217d954SCole Faust
150*c217d954SCole Faust TEST_SUITE_END() // BatchNormalizationLayer
151*c217d954SCole Faust
152*c217d954SCole Faust TEST_SUITE(BatchNormalizationLayerFusion)
153*c217d954SCole Faust // *INDENT-OFF*
154*c217d954SCole Faust // clang-format off
155*c217d954SCole Faust DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(
156*c217d954SCole Faust framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), // Valid
157*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), // Mismatching data types
158*c217d954SCole Faust TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape
159*c217d954SCole Faust }),
160*c217d954SCole Faust framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32),
161*c217d954SCole Faust TensorInfo(TensorShape(2U), 1, DataType::F16),
162*c217d954SCole Faust TensorInfo(TensorShape(5U), 1, DataType::F32),
163*c217d954SCole Faust })),
164*c217d954SCole Faust framework::dataset::make("Expected", { true, false, false})),
165*c217d954SCole Faust weights_info, mvbg_info, expected)
166*c217d954SCole Faust {
167*c217d954SCole Faust const auto &weights_in_info = weights_info;
168*c217d954SCole Faust const auto &mean_info = mvbg_info;
169*c217d954SCole Faust const auto &var_info = mvbg_info;
170*c217d954SCole Faust const auto &fused_weights_info = weights_info;
171*c217d954SCole Faust const auto &fused_bias_info = mvbg_info;
172*c217d954SCole Faust const auto &conv_bias_info = mvbg_info;
173*c217d954SCole Faust const auto &beta_info = mvbg_info;
174*c217d954SCole Faust const auto &gamma_info = mvbg_info;
175*c217d954SCole Faust bool has_error = bool(CLFuseBatchNormalization::validate(
176*c217d954SCole Faust &weights_in_info.clone()->set_is_resizable(false), &mean_info.clone()->set_is_resizable(false),
177*c217d954SCole Faust &var_info.clone()->set_is_resizable(false), &fused_weights_info.clone()->set_is_resizable(false),
178*c217d954SCole Faust &fused_bias_info.clone()->set_is_resizable(false), &conv_bias_info.clone()->set_is_resizable(false),
179*c217d954SCole Faust &beta_info.clone()->set_is_resizable(false), &gamma_info.clone()->set_is_resizable(false), 1.f));
180*c217d954SCole Faust ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS);
181*c217d954SCole Faust }
182*c217d954SCole Faust // clang-format on
183*c217d954SCole Faust // *INDENT-ON*
184*c217d954SCole Faust template <typename T>
185*c217d954SCole Faust using CLBatchNormalizationLayerFusionFixture = BatchNormalizationLayerFusionValidationFixture<CLTensor, CLAccessor, CLConvolutionLayer, CLFuseBatchNormalization, T>;
186*c217d954SCole Faust
187*c217d954SCole Faust TEST_SUITE(Float)
TEST_SUITE(FP32)188*c217d954SCole Faust TEST_SUITE(FP32)
189*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLBatchNormalizationLayerFusionFixture<float>, framework::DatasetMode::PRECOMMIT,
190*c217d954SCole Faust combine(combine(combine(datasets::SmallConvolutionLayerReducedDataset(), common_fusion_dataset),
191*c217d954SCole Faust framework::dataset::make("DataType", DataType::F32)),
192*c217d954SCole Faust framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
193*c217d954SCole Faust {
194*c217d954SCole Faust // Validate output
195*c217d954SCole Faust validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
196*c217d954SCole Faust }
197*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunLarge, CLBatchNormalizationLayerFusionFixture<float>, framework::DatasetMode::NIGHTLY,
198*c217d954SCole Faust combine(combine(combine(datasets::SmallConvolutionLayerDataset(), common_fusion_dataset),
199*c217d954SCole Faust framework::dataset::make("DataType", DataType::F32)),
200*c217d954SCole Faust framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
201*c217d954SCole Faust {
202*c217d954SCole Faust // Validate output
203*c217d954SCole Faust validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
204*c217d954SCole Faust }
205*c217d954SCole Faust TEST_SUITE_END() // FP32
206*c217d954SCole Faust TEST_SUITE_END() // Float
207*c217d954SCole Faust
208*c217d954SCole Faust TEST_SUITE_END() // BatchNormalizationLayerFusion
209*c217d954SCole Faust TEST_SUITE_END() // CL
210*c217d954SCole Faust } // namespace validation
211*c217d954SCole Faust } // namespace test
212*c217d954SCole Faust } // namespace arm_compute
213