blob: 88f00b0eff7ff0c218ef1291d20f90232759c889 [file] [log] [blame]
Sanghoon Lee96883782017-09-15 14:10:48 +01001/*
Michalis Spyroud175ece2020-07-30 23:39:32 +01002 * Copyright (c) 2017-2020 Arm Limited.
Sanghoon Lee96883782017-09-15 14:10:48 +01003 *
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/CL/CLTensor.h"
26#include "arm_compute/runtime/CL/CLTensorAllocator.h"
27#include "arm_compute/runtime/CL/functions/CLBatchNormalizationLayer.h"
Georgios Pinitasc9369172018-09-26 11:25:40 +010028#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
29#include "arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h"
Sanghoon Lee96883782017-09-15 14:10:48 +010030#include "tests/CL/CLAccessor.h"
31#include "tests/PaddingCalculator.h"
Georgios Pinitasc9369172018-09-26 11:25:40 +010032#include "tests/datasets/LargeConvolutionLayerDataset.h"
Sanghoon Lee96883782017-09-15 14:10:48 +010033#include "tests/datasets/RandomBatchNormalizationLayerDataset.h"
Georgios Pinitasc9369172018-09-26 11:25:40 +010034#include "tests/datasets/SmallConvolutionLayerDataset.h"
Sanghoon Lee96883782017-09-15 14:10:48 +010035#include "tests/framework/Asserts.h"
36#include "tests/framework/Macros.h"
37#include "tests/framework/datasets/Datasets.h"
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000038#include "tests/validation/Helpers.h"
Sanghoon Lee96883782017-09-15 14:10:48 +010039#include "tests/validation/Validation.h"
40#include "tests/validation/fixtures/BatchNormalizationLayerFixture.h"
Georgios Pinitasc9369172018-09-26 11:25:40 +010041#include "tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h"
Sanghoon Lee96883782017-09-15 14:10:48 +010042
43namespace arm_compute
44{
45namespace test
46{
47namespace validation
48{
49namespace
50{
Georgios Pinitas51e53a32018-10-22 13:49:08 +010051RelativeTolerance<float> rel_tolerance_f32(0.05f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
52constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.0001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
53constexpr AbsoluteTolerance<float> tolerance_f16(0.01f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */
Giorgio Arena11674872018-02-07 15:38:12 +000054const auto act_infos = framework::dataset::make("ActivationInfo",
55{
56 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
57 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
58 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f),
59});
Georgios Pinitasc9369172018-09-26 11:25:40 +010060
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010061const auto common_fusion_dataset = combine(combine(combine(framework::dataset::make("UseBias",
62{ false, true }),
63framework::dataset::make("UseBeta", { false, true })),
64framework::dataset::make("UseGamma", { false, true })),
65framework::dataset::make("Epsilon", { 0.001f }));
Sheri Zhang141c31a2020-10-08 12:35:28 +010066
67bool validate_zero_padding(TensorShape shape0, const TensorShape shape1, float epsilon, ActivationLayerInfo act_info, DataType dt, DataLayout data_layout)
68{
69 if(data_layout == DataLayout::NHWC)
70 {
71 permute(shape0, PermutationVector(2U, 0U, 1U));
72 }
73
74 // Create tensors
75 CLTensor src = create_tensor<CLTensor>(shape0, dt, 1, QuantizationInfo(), data_layout);
76 CLTensor dst = create_tensor<CLTensor>(shape0, dt, 1, QuantizationInfo(), data_layout);
77 CLTensor mean = create_tensor<CLTensor>(shape1, dt, 1);
78 CLTensor var = create_tensor<CLTensor>(shape1, dt, 1);
79 CLTensor beta = create_tensor<CLTensor>(shape1, dt, 1);
80 CLTensor gamma = create_tensor<CLTensor>(shape1, dt, 1);
81
82 // Create and configure function
83 CLBatchNormalizationLayer norm;
84 norm.configure(&src, &dst, &mean, &var, &beta, &gamma, epsilon, act_info);
85
86 return src.info()->padding().empty() && dst.info()->padding().empty() && mean.info()->padding().empty() && var.info()->padding().empty() && beta.info()->padding().empty()
87 && gamma.info()->padding().empty();
88}
Sanghoon Lee96883782017-09-15 14:10:48 +010089} // namespace
90
91TEST_SUITE(CL)
92TEST_SUITE(BatchNormalizationLayer)
93
94template <typename T>
95using CLBatchNormalizationLayerFixture = BatchNormalizationLayerValidationFixture<CLTensor, CLAccessor, CLBatchNormalizationLayer, T>;
96
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000097// *INDENT-OFF*
98// clang-format off
Giorgio Arena11674872018-02-07 15:38:12 +000099DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
Giorgio Arena70623822017-11-27 15:50:10 +0000100 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
101 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
102 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
103 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
104 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape
Giorgio Arena11674872018-02-07 15:38:12 +0000105 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Unsupported fused activation
106 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Fused activation's a < b
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000107 }),
Giorgio Arena70623822017-11-27 15:50:10 +0000108 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000109 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Giorgio Arena70623822017-11-27 15:50:10 +0000110 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
111 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F16),
112 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Giorgio Arena11674872018-02-07 15:38:12 +0000113 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
114 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000115 })),
116 framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32),
Giorgio Arena70623822017-11-27 15:50:10 +0000117 TensorInfo(TensorShape(2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000118 TensorInfo(TensorShape(2U), 1, DataType::F16),
119 TensorInfo(TensorShape(2U), 1, DataType::F32),
120 TensorInfo(TensorShape(5U), 1, DataType::F32),
Giorgio Arena11674872018-02-07 15:38:12 +0000121 TensorInfo(TensorShape(2U), 1, DataType::F32),
122 TensorInfo(TensorShape(2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000123 })),
Giorgio Arena11674872018-02-07 15:38:12 +0000124 framework::dataset::make("ActivationLayerInfo",{ ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
125 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
126 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
127 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
128 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f),
Giorgio Arena11674872018-02-07 15:38:12 +0000129 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
130 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 2.f, 6.f),
Giorgio Arena11674872018-02-07 15:38:12 +0000131 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100132 framework::dataset::make("Expected", { true, false, false, false, false, false, false})),
Giorgio Arena11674872018-02-07 15:38:12 +0000133 input_info, output_info, mvbg_info, act_info, expected)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000134{
Giorgio Arena70623822017-11-27 15:50:10 +0000135 const auto &mean_info = mvbg_info;
136 const auto &var_info = mvbg_info;
137 const auto &beta_info = mvbg_info;
138 const auto &gamma_info = mvbg_info;
Giorgio Arena11674872018-02-07 15:38:12 +0000139 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));
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000140 ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS);
141}
142// clang-format on
143// *INDENT-ON*
144
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100145DATA_TEST_CASE(ValidateZeroPadding, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallRandomBatchNormalizationLayerDataset(), act_infos), framework::dataset::make("DataType", { DataType::F32, DataType::F16 })),
Sheri Zhang141c31a2020-10-08 12:35:28 +0100146 framework::dataset::make("DataLayout", { DataLayout::NHWC })),
147 shape0, shape1, episilon, act_infos, data_type, data_layout)
148{
149 bool status = validate_zero_padding(shape0, shape1, episilon, act_infos, data_type, data_layout);
150 ARM_COMPUTE_EXPECT(status, framework::LogLevel::ERRORS);
151}
152
Sanghoon Lee96883782017-09-15 14:10:48 +0100153TEST_SUITE(Float)
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100154TEST_SUITE(FP32)
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000155FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallRandomBatchNormalizationLayerDataset(),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100156 combine(framework::dataset::make("UseBeta", { false, true }), framework::dataset::make("UseGamma", { false, true }))),
Giorgio Arena11674872018-02-07 15:38:12 +0000157 act_infos),
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000158 framework::dataset::make("DataType", DataType::F32)),
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000159 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
Sanghoon Lee96883782017-09-15 14:10:48 +0100160{
161 // Validate output
Georgios Pinitasc9369172018-09-26 11:25:40 +0100162 validate(CLAccessor(_target), _reference, abs_tolerance_f32, 0);
Sanghoon Lee96883782017-09-15 14:10:48 +0100163}
Georgios Pinitasc9369172018-09-26 11:25:40 +0100164TEST_SUITE_END() //FP32
Sanghoon Lee96883782017-09-15 14:10:48 +0100165
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100166TEST_SUITE(FP16)
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000167FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallRandomBatchNormalizationLayerDataset(),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100168 combine(framework::dataset::make("UseBeta", { false, true }), framework::dataset::make("UseGamma", { false, true }))),
169 framework::dataset::make("ActivationInfo",
170 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))),
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000171 framework::dataset::make("DataType", DataType::F16)),
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000172 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100173{
174 // Validate output
175 validate(CLAccessor(_target), _reference, tolerance_f16, 0);
176}
Georgios Pinitasc9369172018-09-26 11:25:40 +0100177TEST_SUITE_END() // FP16
178TEST_SUITE_END() // Float
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100179
Georgios Pinitasc9369172018-09-26 11:25:40 +0100180TEST_SUITE_END() // BatchNormalizationLayer
181
182TEST_SUITE(BatchNormalizationLayerFusion)
Michalis Spyrou80943252019-01-10 17:19:50 +0000183// *INDENT-OFF*
184// clang-format off
185DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(
186 framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), // Valid
187 TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), // Mismatching data types
188 TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape
189 }),
190 framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32),
191 TensorInfo(TensorShape(2U), 1, DataType::F16),
192 TensorInfo(TensorShape(5U), 1, DataType::F32),
193 })),
194 framework::dataset::make("Expected", { true, false, false})),
195 weights_info, mvbg_info, expected)
196{
197 const auto &weights_in_info = weights_info;
198 const auto &mean_info = mvbg_info;
199 const auto &var_info = mvbg_info;
200 const auto &fused_weights_info = weights_info;
201 const auto &fused_bias_info = mvbg_info;
202 const auto &conv_bias_info = mvbg_info;
203 const auto &beta_info = mvbg_info;
204 const auto &gamma_info = mvbg_info;
205 bool has_error = bool(CLFuseBatchNormalization::validate(
206 &weights_in_info.clone()->set_is_resizable(false), &mean_info.clone()->set_is_resizable(false),
207 &var_info.clone()->set_is_resizable(false), &fused_weights_info.clone()->set_is_resizable(false),
208 &fused_bias_info.clone()->set_is_resizable(false), &conv_bias_info.clone()->set_is_resizable(false),
209 &beta_info.clone()->set_is_resizable(false), &gamma_info.clone()->set_is_resizable(false), 1.f));
210 ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS);
211}
212// clang-format on
213// *INDENT-ON*
Georgios Pinitasc9369172018-09-26 11:25:40 +0100214template <typename T>
215using CLBatchNormalizationLayerFusionFixture = BatchNormalizationLayerFusionValidationFixture<CLTensor, CLAccessor, CLConvolutionLayer, CLFuseBatchNormalization, T>;
216
217TEST_SUITE(Float)
218TEST_SUITE(FP32)
219FIXTURE_DATA_TEST_CASE(RunSmall, CLBatchNormalizationLayerFusionFixture<float>, framework::DatasetMode::PRECOMMIT,
Michalis Spyrou80943252019-01-10 17:19:50 +0000220 combine(combine(combine(datasets::SmallConvolutionLayerReducedDataset(), common_fusion_dataset),
221 framework::dataset::make("DataType", DataType::F32)),
222 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
223{
224 // Validate output
225 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
226}
227FIXTURE_DATA_TEST_CASE(RunLarge, CLBatchNormalizationLayerFusionFixture<float>, framework::DatasetMode::NIGHTLY,
Georgios Pinitasc9369172018-09-26 11:25:40 +0100228 combine(combine(combine(datasets::SmallConvolutionLayerDataset(), common_fusion_dataset),
229 framework::dataset::make("DataType", DataType::F32)),
230 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
231{
232 // Validate output
233 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
234}
235TEST_SUITE_END() // FP32
236TEST_SUITE_END() // Float
237
238TEST_SUITE_END() // BatchNormalizationLayerFusion
239TEST_SUITE_END() // CL
Sanghoon Lee96883782017-09-15 14:10:48 +0100240} // namespace validation
241} // namespace test
242} // namespace arm_compute