blob: 054ed278a2b9545dfeb47bf4d08bad5f27e90d1a [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/NEON/functions/NEBatchNormalizationLayer.h"
26#include "arm_compute/runtime/Tensor.h"
27#include "arm_compute/runtime/TensorAllocator.h"
28#include "tests/NEON/Accessor.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{
46constexpr AbsoluteTolerance<float> tolerance_f32(0.00001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000047#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Sanghoon Lee96883782017-09-15 14:10:48 +010048constexpr AbsoluteTolerance<float> tolerance_f16(0.01f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000049#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Sanghoon Lee96883782017-09-15 14:10:48 +010050constexpr AbsoluteTolerance<float> tolerance_qs8(3.0f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::QS8 */
51constexpr 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 +000052const auto act_infos = framework::dataset::make("ActivationInfo",
53{
54 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
55 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
56 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f),
57});
Sanghoon Lee96883782017-09-15 14:10:48 +010058} // namespace
59
60TEST_SUITE(NEON)
61TEST_SUITE(BatchNormalizationLayer)
62
63template <typename T>
64using NEBatchNormalizationLayerFixture = BatchNormalizationLayerValidationFixture<Tensor, Accessor, NEBatchNormalizationLayer, T>;
65
66DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::RandomBatchNormalizationLayerDataset(), framework::dataset::make("DataType", { DataType::QS8, DataType::QS16, DataType::F32 })),
67 shape0, shape1, epsilon, dt)
68{
69 // Set fixed point position data type allowed
Sanghoon Leec1294fa2017-11-17 11:47:41 +000070 const int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0;
Sanghoon Lee96883782017-09-15 14:10:48 +010071
72 // Create tensors
73 Tensor src = create_tensor<Tensor>(shape0, dt, 1, fixed_point_position);
74 Tensor dst = create_tensor<Tensor>(shape0, dt, 1, fixed_point_position);
75 Tensor mean = create_tensor<Tensor>(shape1, dt, 1, fixed_point_position);
76 Tensor var = create_tensor<Tensor>(shape1, dt, 1, fixed_point_position);
77 Tensor beta = create_tensor<Tensor>(shape1, dt, 1, fixed_point_position);
78 Tensor gamma = create_tensor<Tensor>(shape1, dt, 1, fixed_point_position);
79
80 // Create and Configure function
81 NEBatchNormalizationLayer norm;
82 norm.configure(&src, &dst, &mean, &var, &beta, &gamma, epsilon);
83
84 // Validate valid region
85 const ValidRegion valid_region = shape_to_valid_region(shape0);
86 validate(dst.info()->valid_region(), valid_region);
87}
88
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +000089// *INDENT-OFF*
90// clang-format off
Giorgio Arena11674872018-02-07 15:38:12 +000091DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +000092 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
93 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
94 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
95 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
96 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape
97 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point position
Giorgio Arena11674872018-02-07 15:38:12 +000098 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), // Fused activation with fixed point not supported
99 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Fused activation's a < b
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000100 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2),
101 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2),
102 }),
103 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
104 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
105 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
106 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F16),
107 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
108 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 3),
Giorgio Arena11674872018-02-07 15:38:12 +0000109 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
110 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 3),
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000111 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2),
112 TensorInfo(),
113 })),
114 framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32),
115 TensorInfo(TensorShape(2U), 1, DataType::F32),
116 TensorInfo(TensorShape(2U), 1, DataType::F16),
117 TensorInfo(TensorShape(2U), 1, DataType::F32),
118 TensorInfo(TensorShape(5U), 1, DataType::F32),
119 TensorInfo(TensorShape(2U), 1, DataType::QS8, 2),
120 TensorInfo(TensorShape(2U), 1, DataType::QS8, 2),
Giorgio Arena11674872018-02-07 15:38:12 +0000121 TensorInfo(TensorShape(2U), 1, DataType::F32),
122 TensorInfo(TensorShape(2U), 1, DataType::QS8, 2),
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000123 TensorInfo(TensorShape(2U), 1, DataType::QS8, 2),
124 })),
Giorgio Arena11674872018-02-07 15:38:12 +0000125 framework::dataset::make("ActivationLayerInfo",{ ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
126 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
127 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
128 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
129 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f),
130 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f, 2.f),
131 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f, 2.f),
132 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 2.f, 6.f),
133 ActivationLayerInfo(),
134 ActivationLayerInfo(),
135 })),
136 framework::dataset::make("Expected", { true, false, false, false, false, false, false, false, true, true})),
137 input_info, output_info, mvbg_info, act_info, expected)
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000138{
139 const auto &mean_info = mvbg_info;
140 const auto &var_info = mvbg_info;
141 const auto &beta_info = mvbg_info;
142 const auto &gamma_info = mvbg_info;
143 bool has_error = bool(NEBatchNormalizationLayer::validate(
144 &input_info.clone()->set_is_resizable(false), output_info.total_size() ? &output_info.clone()->set_is_resizable(false) : nullptr,
145 &mean_info.clone()->set_is_resizable(false), &var_info.clone()->set_is_resizable(false),
Giorgio Arena11674872018-02-07 15:38:12 +0000146 &beta_info.clone()->set_is_resizable(false), &gamma_info.clone()->set_is_resizable(false), 1.f, act_info));
Ioan-Cristian Szabo303be902017-11-27 16:31:10 +0000147 ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS);
148}
149// clang-format on
150// *INDENT-ON*
151
Sanghoon Lee96883782017-09-15 14:10:48 +0100152TEST_SUITE(Float)
Giorgio Arena11674872018-02-07 15:38:12 +0000153FIXTURE_DATA_TEST_CASE(Random, NEBatchNormalizationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
154 act_infos),
Sanghoon Lee96883782017-09-15 14:10:48 +0100155 framework::dataset::make("DataType", DataType::F32)))
156{
157 // Validate output
158 validate(Accessor(_target), _reference, tolerance_f32, 0);
159}
160TEST_SUITE_END()
161
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000162#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Sanghoon Lee96883782017-09-15 14:10:48 +0100163TEST_SUITE(Float16)
Giorgio Arena11674872018-02-07 15:38:12 +0000164FIXTURE_DATA_TEST_CASE(Random, NEBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000165 framework::dataset::make("ActivationInfo", ActivationLayerInfo())),
Sanghoon Lee96883782017-09-15 14:10:48 +0100166 framework::dataset::make("DataType", DataType::F16)))
167{
168 // Validate output
169 validate(Accessor(_target), _reference, tolerance_f16, 0);
170}
171TEST_SUITE_END()
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000172#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Sanghoon Lee96883782017-09-15 14:10:48 +0100173
174TEST_SUITE(Quantized)
175template <typename T>
176using NEBatchNormalizationLayerFixedPointFixture = BatchNormalizationLayerValidationFixedPointFixture<Tensor, Accessor, NEBatchNormalizationLayer, T>;
177
178TEST_SUITE(QS8)
Giorgio Arena11674872018-02-07 15:38:12 +0000179FIXTURE_DATA_TEST_CASE(Random, NEBatchNormalizationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
180 framework::dataset::make("ActivationInfo", ActivationLayerInfo())),
Sanghoon Lee96883782017-09-15 14:10:48 +0100181 framework::dataset::make("DataType", DataType::QS8)),
182 framework::dataset::make("FractionalBits", 1, 6)))
183{
184 // Validate output
185 validate(Accessor(_target), _reference, tolerance_qs8, 0);
186}
187TEST_SUITE_END()
188
189TEST_SUITE(QS16)
Giorgio Arena11674872018-02-07 15:38:12 +0000190FIXTURE_DATA_TEST_CASE(Random, NEBatchNormalizationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
191 framework::dataset::make("ActivationInfo", ActivationLayerInfo())),
Sanghoon Lee96883782017-09-15 14:10:48 +0100192 framework::dataset::make("DataType", DataType::QS16)),
193 framework::dataset::make("FractionalBits", 1, 14)))
194{
195 // Validate output
196 validate(Accessor(_target), _reference, tolerance_qs16, 0);
197}
198TEST_SUITE_END()
199
200TEST_SUITE_END()
201
202TEST_SUITE_END()
203TEST_SUITE_END()
204} // namespace validation
205} // namespace test
206} // namespace arm_compute