blob: e67f4cc19924aec122f433ece631b82de7e48f96 [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
61const auto common_fusion_dataset = combine(combine(combine(framework::dataset::make("UseBias", { false, true }),
62 framework::dataset::make("UseBeta", { false, true })),
63 framework::dataset::make("UseGamma", { false, true })),
64 framework::dataset::make("Epsilon", { 0.001f }));
Sheri Zhang141c31a2020-10-08 12:35:28 +010065
66bool validate_zero_padding(TensorShape shape0, const TensorShape shape1, float epsilon, ActivationLayerInfo act_info, DataType dt, DataLayout data_layout)
67{
68 if(data_layout == DataLayout::NHWC)
69 {
70 permute(shape0, PermutationVector(2U, 0U, 1U));
71 }
72
73 // Create tensors
74 CLTensor src = create_tensor<CLTensor>(shape0, dt, 1, QuantizationInfo(), data_layout);
75 CLTensor dst = create_tensor<CLTensor>(shape0, dt, 1, QuantizationInfo(), data_layout);
76 CLTensor mean = create_tensor<CLTensor>(shape1, dt, 1);
77 CLTensor var = create_tensor<CLTensor>(shape1, dt, 1);
78 CLTensor beta = create_tensor<CLTensor>(shape1, dt, 1);
79 CLTensor gamma = create_tensor<CLTensor>(shape1, dt, 1);
80
81 // Create and configure function
82 CLBatchNormalizationLayer norm;
83 norm.configure(&src, &dst, &mean, &var, &beta, &gamma, epsilon, act_info);
84
85 return src.info()->padding().empty() && dst.info()->padding().empty() && mean.info()->padding().empty() && var.info()->padding().empty() && beta.info()->padding().empty()
86 && gamma.info()->padding().empty();
87}
Sanghoon Lee96883782017-09-15 14:10:48 +010088} // namespace
89
90TEST_SUITE(CL)
91TEST_SUITE(BatchNormalizationLayer)
92
93template <typename T>
94using CLBatchNormalizationLayerFixture = BatchNormalizationLayerValidationFixture<CLTensor, CLAccessor, CLBatchNormalizationLayer, T>;
95
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000096// *INDENT-OFF*
97// clang-format off
Giorgio Arena11674872018-02-07 15:38:12 +000098DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
Giorgio Arena70623822017-11-27 15:50:10 +000099 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
100 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
101 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
102 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
103 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape
Giorgio Arena11674872018-02-07 15:38:12 +0000104 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Unsupported fused activation
105 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Fused activation's a < b
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000106 }),
Giorgio Arena70623822017-11-27 15:50:10 +0000107 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000108 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Giorgio Arena70623822017-11-27 15:50:10 +0000109 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
110 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F16),
111 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Giorgio Arena11674872018-02-07 15:38:12 +0000112 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
113 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000114 })),
115 framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32),
Giorgio Arena70623822017-11-27 15:50:10 +0000116 TensorInfo(TensorShape(2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000117 TensorInfo(TensorShape(2U), 1, DataType::F16),
118 TensorInfo(TensorShape(2U), 1, DataType::F32),
119 TensorInfo(TensorShape(5U), 1, DataType::F32),
Giorgio Arena11674872018-02-07 15:38:12 +0000120 TensorInfo(TensorShape(2U), 1, DataType::F32),
121 TensorInfo(TensorShape(2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000122 })),
Giorgio Arena11674872018-02-07 15:38:12 +0000123 framework::dataset::make("ActivationLayerInfo",{ ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
124 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
125 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
126 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
127 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f),
Giorgio Arena11674872018-02-07 15:38:12 +0000128 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
129 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 2.f, 6.f),
Giorgio Arena11674872018-02-07 15:38:12 +0000130 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100131 framework::dataset::make("Expected", { true, false, false, false, false, false, false})),
Giorgio Arena11674872018-02-07 15:38:12 +0000132 input_info, output_info, mvbg_info, act_info, expected)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000133{
Giorgio Arena70623822017-11-27 15:50:10 +0000134 const auto &mean_info = mvbg_info;
135 const auto &var_info = mvbg_info;
136 const auto &beta_info = mvbg_info;
137 const auto &gamma_info = mvbg_info;
Giorgio Arena11674872018-02-07 15:38:12 +0000138 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 +0000139 ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS);
140}
141// clang-format on
142// *INDENT-ON*
143
Sheri Zhang141c31a2020-10-08 12:35:28 +0100144DATA_TEST_CASE(ValidateZeroPadding, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallRandomBatchNormalizationLayerDataset(),
145 act_infos),
146 framework::dataset::make("DataType", { DataType::F32, DataType::F16 })),
147 framework::dataset::make("DataLayout", { DataLayout::NHWC })),
148 shape0, shape1, episilon, act_infos, data_type, data_layout)
149{
150 bool status = validate_zero_padding(shape0, shape1, episilon, act_infos, data_type, data_layout);
151 ARM_COMPUTE_EXPECT(status, framework::LogLevel::ERRORS);
152}
153
Sanghoon Lee96883782017-09-15 14:10:48 +0100154TEST_SUITE(Float)
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100155TEST_SUITE(FP32)
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000156FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallRandomBatchNormalizationLayerDataset(),
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000157 combine(framework::dataset::make("UseBeta", { false, true }),
158 framework::dataset::make("UseGamma", { false, true }))),
Giorgio Arena11674872018-02-07 15:38:12 +0000159 act_infos),
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000160 framework::dataset::make("DataType", DataType::F32)),
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000161 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
Sanghoon Lee96883782017-09-15 14:10:48 +0100162{
163 // Validate output
Georgios Pinitasc9369172018-09-26 11:25:40 +0100164 validate(CLAccessor(_target), _reference, abs_tolerance_f32, 0);
Sanghoon Lee96883782017-09-15 14:10:48 +0100165}
Georgios Pinitasc9369172018-09-26 11:25:40 +0100166TEST_SUITE_END() //FP32
Sanghoon Lee96883782017-09-15 14:10:48 +0100167
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100168TEST_SUITE(FP16)
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000169FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallRandomBatchNormalizationLayerDataset(),
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000170 combine(framework::dataset::make("UseBeta", { false, true }),
171 framework::dataset::make("UseGamma", { false, true }))),
Giorgio Arena11674872018-02-07 15:38:12 +0000172 framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))),
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000173 framework::dataset::make("DataType", DataType::F16)),
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000174 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100175{
176 // Validate output
177 validate(CLAccessor(_target), _reference, tolerance_f16, 0);
178}
Georgios Pinitasc9369172018-09-26 11:25:40 +0100179TEST_SUITE_END() // FP16
180TEST_SUITE_END() // Float
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100181
Georgios Pinitasc9369172018-09-26 11:25:40 +0100182TEST_SUITE_END() // BatchNormalizationLayer
183
184TEST_SUITE(BatchNormalizationLayerFusion)
Michalis Spyrou80943252019-01-10 17:19:50 +0000185// *INDENT-OFF*
186// clang-format off
187DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(
188 framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), // Valid
189 TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), // Mismatching data types
190 TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape
191 }),
192 framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32),
193 TensorInfo(TensorShape(2U), 1, DataType::F16),
194 TensorInfo(TensorShape(5U), 1, DataType::F32),
195 })),
196 framework::dataset::make("Expected", { true, false, false})),
197 weights_info, mvbg_info, expected)
198{
199 const auto &weights_in_info = weights_info;
200 const auto &mean_info = mvbg_info;
201 const auto &var_info = mvbg_info;
202 const auto &fused_weights_info = weights_info;
203 const auto &fused_bias_info = mvbg_info;
204 const auto &conv_bias_info = mvbg_info;
205 const auto &beta_info = mvbg_info;
206 const auto &gamma_info = mvbg_info;
207 bool has_error = bool(CLFuseBatchNormalization::validate(
208 &weights_in_info.clone()->set_is_resizable(false), &mean_info.clone()->set_is_resizable(false),
209 &var_info.clone()->set_is_resizable(false), &fused_weights_info.clone()->set_is_resizable(false),
210 &fused_bias_info.clone()->set_is_resizable(false), &conv_bias_info.clone()->set_is_resizable(false),
211 &beta_info.clone()->set_is_resizable(false), &gamma_info.clone()->set_is_resizable(false), 1.f));
212 ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS);
213}
214// clang-format on
215// *INDENT-ON*
Georgios Pinitasc9369172018-09-26 11:25:40 +0100216template <typename T>
217using CLBatchNormalizationLayerFusionFixture = BatchNormalizationLayerFusionValidationFixture<CLTensor, CLAccessor, CLConvolutionLayer, CLFuseBatchNormalization, T>;
218
219TEST_SUITE(Float)
220TEST_SUITE(FP32)
221FIXTURE_DATA_TEST_CASE(RunSmall, CLBatchNormalizationLayerFusionFixture<float>, framework::DatasetMode::PRECOMMIT,
Michalis Spyrou80943252019-01-10 17:19:50 +0000222 combine(combine(combine(datasets::SmallConvolutionLayerReducedDataset(), common_fusion_dataset),
223 framework::dataset::make("DataType", DataType::F32)),
224 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
225{
226 // Validate output
227 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
228}
229FIXTURE_DATA_TEST_CASE(RunLarge, CLBatchNormalizationLayerFusionFixture<float>, framework::DatasetMode::NIGHTLY,
Georgios Pinitasc9369172018-09-26 11:25:40 +0100230 combine(combine(combine(datasets::SmallConvolutionLayerDataset(), common_fusion_dataset),
231 framework::dataset::make("DataType", DataType::F32)),
232 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
233{
234 // Validate output
235 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32);
236}
237TEST_SUITE_END() // FP32
238TEST_SUITE_END() // Float
239
240TEST_SUITE_END() // BatchNormalizationLayerFusion
241TEST_SUITE_END() // CL
Sanghoon Lee96883782017-09-15 14:10:48 +0100242} // namespace validation
243} // namespace test
244} // namespace arm_compute