blob: 8c143060cbcb10d945052396785d628c165f6d2a [file] [log] [blame]
Sanghoon Lee96883782017-09-15 14:10:48 +01001/*
Giorgio Arena11674872018-02-07 15:38:12 +00002 * Copyright (c) 2017-2018 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"
28#include "tests/CL/CLAccessor.h"
29#include "tests/PaddingCalculator.h"
30#include "tests/datasets/RandomBatchNormalizationLayerDataset.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/BatchNormalizationLayerFixture.h"
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44namespace
45{
Gian Marco Iodice349feef2017-09-28 11:21:29 +010046constexpr AbsoluteTolerance<float> tolerance_f32(0.00001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
47constexpr AbsoluteTolerance<float> tolerance_f16(0.01f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */
48constexpr AbsoluteTolerance<float> tolerance_qs8(3.0f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::QS8 */
49constexpr AbsoluteTolerance<float> tolerance_qs16(6.0f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::QS16 */
Giorgio Arena11674872018-02-07 15:38:12 +000050const auto act_infos = framework::dataset::make("ActivationInfo",
51{
52 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
53 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
54 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f),
55});
Sanghoon Lee96883782017-09-15 14:10:48 +010056} // namespace
57
58TEST_SUITE(CL)
59TEST_SUITE(BatchNormalizationLayer)
60
61template <typename T>
62using CLBatchNormalizationLayerFixture = BatchNormalizationLayerValidationFixture<CLTensor, CLAccessor, CLBatchNormalizationLayer, T>;
63
Michele Di Giorgio4d336302018-03-02 09:43:54 +000064DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
65 combine(framework::dataset::make("UseBeta", { false, true }),
66 framework::dataset::make("UseGamma", { false, true }))),
67 framework::dataset::make("DataType", { DataType::QS8, DataType::QS16, DataType::F16, DataType::F32 })),
68 shape0, shape1, epsilon, use_gamma, use_beta, dt)
Sanghoon Lee96883782017-09-15 14:10:48 +010069{
70 // Set fixed point position data type allowed
Sanghoon Leec1294fa2017-11-17 11:47:41 +000071 const int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0;
Sanghoon Lee96883782017-09-15 14:10:48 +010072
73 // Create tensors
74 CLTensor src = create_tensor<CLTensor>(shape0, dt, 1, fixed_point_position);
75 CLTensor dst = create_tensor<CLTensor>(shape0, dt, 1, fixed_point_position);
76 CLTensor mean = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
77 CLTensor var = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
78 CLTensor beta = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
79 CLTensor gamma = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
80
81 // Create and Configure function
82 CLBatchNormalizationLayer norm;
Michele Di Giorgio4d336302018-03-02 09:43:54 +000083 CLTensor *beta_ptr = use_beta ? &beta : nullptr;
84 CLTensor *gamma_ptr = use_gamma ? &gamma : nullptr;
85 norm.configure(&src, &dst, &mean, &var, beta_ptr, gamma_ptr, epsilon);
Sanghoon Lee96883782017-09-15 14:10:48 +010086
87 // Validate valid region
88 const ValidRegion valid_region = shape_to_valid_region(shape0);
89 validate(dst.info()->valid_region(), valid_region);
90}
91
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000092// *INDENT-OFF*
93// clang-format off
Giorgio Arena11674872018-02-07 15:38:12 +000094DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
Giorgio Arena70623822017-11-27 15:50:10 +000095 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
96 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
97 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
98 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
99 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape
100 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point position
Giorgio Arena11674872018-02-07 15:38:12 +0000101 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), // Fused activation with fixed point not supported
102 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Unsupported fused activation
103 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Fused activation's a < b
Giorgio Arena70623822017-11-27 15:50:10 +0000104 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2),
105 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2),
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),
112 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 3),
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),
115 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 3),
Giorgio Arena70623822017-11-27 15:50:10 +0000116 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2),
117 TensorInfo(),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000118 })),
119 framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32),
Giorgio Arena70623822017-11-27 15:50:10 +0000120 TensorInfo(TensorShape(2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000121 TensorInfo(TensorShape(2U), 1, DataType::F16),
122 TensorInfo(TensorShape(2U), 1, DataType::F32),
123 TensorInfo(TensorShape(5U), 1, DataType::F32),
124 TensorInfo(TensorShape(2U), 1, DataType::QS8, 2),
125 TensorInfo(TensorShape(2U), 1, DataType::QS8, 2),
Giorgio Arena11674872018-02-07 15:38:12 +0000126 TensorInfo(TensorShape(2U), 1, DataType::F32),
127 TensorInfo(TensorShape(2U), 1, DataType::F32),
128 TensorInfo(TensorShape(2U), 1, DataType::QS8, 2),
Giorgio Arena70623822017-11-27 15:50:10 +0000129 TensorInfo(TensorShape(2U), 1, DataType::QS8, 2),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000130 })),
Giorgio Arena11674872018-02-07 15:38:12 +0000131 framework::dataset::make("ActivationLayerInfo",{ ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
132 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
133 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
134 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
135 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f),
136 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f, 2.f),
137 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f, 2.f),
138 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
139 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 2.f, 6.f),
140 ActivationLayerInfo(),
141 ActivationLayerInfo(),
142 })),
143 framework::dataset::make("Expected", { true, false, false, false, false, false, false, false, false, true, true})),
144 input_info, output_info, mvbg_info, act_info, expected)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000145{
Giorgio Arena70623822017-11-27 15:50:10 +0000146 const auto &mean_info = mvbg_info;
147 const auto &var_info = mvbg_info;
148 const auto &beta_info = mvbg_info;
149 const auto &gamma_info = mvbg_info;
Giorgio Arena11674872018-02-07 15:38:12 +0000150 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 +0000151 ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS);
152}
153// clang-format on
154// *INDENT-ON*
155
Sanghoon Lee96883782017-09-15 14:10:48 +0100156TEST_SUITE(Float)
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100157TEST_SUITE(FP32)
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000158FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
159 combine(framework::dataset::make("UseBeta", { false, true }),
160 framework::dataset::make("UseGamma", { false, true }))),
Giorgio Arena11674872018-02-07 15:38:12 +0000161 act_infos),
Sanghoon Lee96883782017-09-15 14:10:48 +0100162 framework::dataset::make("DataType", DataType::F32)))
163{
164 // Validate output
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100165 validate(CLAccessor(_target), _reference, tolerance_f32, 0);
Sanghoon Lee96883782017-09-15 14:10:48 +0100166}
167TEST_SUITE_END()
168
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100169TEST_SUITE(FP16)
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000170FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
171 combine(framework::dataset::make("UseBeta", { false, true }),
172 framework::dataset::make("UseGamma", { false, true }))),
Giorgio Arena11674872018-02-07 15:38:12 +0000173 framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))),
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100174 framework::dataset::make("DataType", DataType::F16)))
175{
176 // Validate output
177 validate(CLAccessor(_target), _reference, tolerance_f16, 0);
178}
179TEST_SUITE_END()
180TEST_SUITE_END()
181
Sanghoon Lee96883782017-09-15 14:10:48 +0100182TEST_SUITE(Quantized)
183template <typename T>
184using CLBatchNormalizationLayerFixedPointFixture = BatchNormalizationLayerValidationFixedPointFixture<CLTensor, CLAccessor, CLBatchNormalizationLayer, T>;
185
186TEST_SUITE(QS8)
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000187FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT,
188 combine(combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
189 framework::dataset::make("UseBeta", false)),
190 framework::dataset::make("UseGamma", false)),
191 framework::dataset::make("ActivationInfo", ActivationLayerInfo())),
192 framework::dataset::make("DataType", DataType::QS8)),
193 framework::dataset::make("FractionalBits", 1, 6)))
Sanghoon Lee96883782017-09-15 14:10:48 +0100194{
195 // Validate output
196 validate(CLAccessor(_target), _reference, tolerance_qs8, 0);
197}
198TEST_SUITE_END()
199
200TEST_SUITE(QS16)
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000201FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT,
202 combine(combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
203 framework::dataset::make("UseBeta", false)),
204 framework::dataset::make("UseGamma", false)),
205 framework::dataset::make("ActivationInfo", ActivationLayerInfo())),
206 framework::dataset::make("DataType", DataType::QS16)),
207 framework::dataset::make("FractionalBits", 1, 14)))
Sanghoon Lee96883782017-09-15 14:10:48 +0100208{
209 // Validate output
210 validate(CLAccessor(_target), _reference, tolerance_qs16, 0);
211}
212TEST_SUITE_END()
213
214TEST_SUITE_END()
215
216TEST_SUITE_END()
217TEST_SUITE_END()
218} // namespace validation
219} // namespace test
220} // namespace arm_compute