blob: c29a4004027eceb2dae2e0800dcee92139352987 [file] [log] [blame]
Sanghoon Lee96883782017-09-15 14:10:48 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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 */
Sanghoon Lee96883782017-09-15 14:10:48 +010050} // namespace
51
52TEST_SUITE(CL)
53TEST_SUITE(BatchNormalizationLayer)
54
55template <typename T>
56using CLBatchNormalizationLayerFixture = BatchNormalizationLayerValidationFixture<CLTensor, CLAccessor, CLBatchNormalizationLayer, T>;
57
Gian Marco Iodice349feef2017-09-28 11:21:29 +010058DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::RandomBatchNormalizationLayerDataset(), framework::dataset::make("DataType", { DataType::QS8, DataType::QS16, DataType::F16, DataType::F32 })),
Sanghoon Lee96883782017-09-15 14:10:48 +010059 shape0, shape1, epsilon, dt)
60{
61 // Set fixed point position data type allowed
62 int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0;
63
64 // Create tensors
65 CLTensor src = create_tensor<CLTensor>(shape0, dt, 1, fixed_point_position);
66 CLTensor dst = create_tensor<CLTensor>(shape0, dt, 1, fixed_point_position);
67 CLTensor mean = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
68 CLTensor var = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
69 CLTensor beta = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
70 CLTensor gamma = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
71
72 // Create and Configure function
73 CLBatchNormalizationLayer norm;
74 norm.configure(&src, &dst, &mean, &var, &beta, &gamma, epsilon);
75
76 // Validate valid region
77 const ValidRegion valid_region = shape_to_valid_region(shape0);
78 validate(dst.info()->valid_region(), valid_region);
79}
80
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000081// *INDENT-OFF*
82// clang-format off
83DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
84 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
85 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
86 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
87 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape
88 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point position
89 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 2),
90 }),
91 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
92 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
93 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
94 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
95 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 3),
96 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 2),
97 })),
98 framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32),
99 TensorInfo(TensorShape(2U), 1, DataType::F16),
100 TensorInfo(TensorShape(2U), 1, DataType::F32),
101 TensorInfo(TensorShape(5U), 1, DataType::F32),
102 TensorInfo(TensorShape(2U), 1, DataType::QS8, 2),
103 TensorInfo(TensorShape(2U), 1, DataType::QS8, 2),
104 })),
105 framework::dataset::make("Expected", { false, true, true, true, true, false})),
106 input_info, output_info, mvbg_info, expected)
107{
108 auto mean_info = mvbg_info;
109 auto var_info = mvbg_info;
110 auto beta_info = mvbg_info;
111 auto gamma_info = mvbg_info;
112 bool has_error = bool(CLBatchNormalizationLayer::validate(&input_info, &output_info, &mean_info, &var_info, &beta_info, &gamma_info, 1.f));
113 ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS);
114}
115// clang-format on
116// *INDENT-ON*
117
Sanghoon Lee96883782017-09-15 14:10:48 +0100118TEST_SUITE(Float)
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100119TEST_SUITE(FP32)
Sanghoon Lee96883782017-09-15 14:10:48 +0100120FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(datasets::RandomBatchNormalizationLayerDataset(),
121 framework::dataset::make("DataType", DataType::F32)))
122{
123 // Validate output
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100124 validate(CLAccessor(_target), _reference, tolerance_f32, 0);
Sanghoon Lee96883782017-09-15 14:10:48 +0100125}
126TEST_SUITE_END()
127
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100128TEST_SUITE(FP16)
129FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(datasets::RandomBatchNormalizationLayerDataset(),
130 framework::dataset::make("DataType", DataType::F16)))
131{
132 // Validate output
133 validate(CLAccessor(_target), _reference, tolerance_f16, 0);
134}
135TEST_SUITE_END()
136TEST_SUITE_END()
137
Sanghoon Lee96883782017-09-15 14:10:48 +0100138TEST_SUITE(Quantized)
139template <typename T>
140using CLBatchNormalizationLayerFixedPointFixture = BatchNormalizationLayerValidationFixedPointFixture<CLTensor, CLAccessor, CLBatchNormalizationLayer, T>;
141
142TEST_SUITE(QS8)
143FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
144 framework::dataset::make("DataType", DataType::QS8)),
145 framework::dataset::make("FractionalBits", 1, 6)))
146{
147 // Validate output
148 validate(CLAccessor(_target), _reference, tolerance_qs8, 0);
149}
150TEST_SUITE_END()
151
152TEST_SUITE(QS16)
153FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
154 framework::dataset::make("DataType", DataType::QS16)),
155 framework::dataset::make("FractionalBits", 1, 14)))
156{
157 // Validate output
158 validate(CLAccessor(_target), _reference, tolerance_qs16, 0);
159}
160TEST_SUITE_END()
161
162TEST_SUITE_END()
163
164TEST_SUITE_END()
165TEST_SUITE_END()
166} // namespace validation
167} // namespace test
168} // namespace arm_compute