blob: de775bf807b63925f88b72682797a66afc553c25 [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"
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000035#include "tests/validation/Helpers.h"
Sanghoon Lee96883782017-09-15 14:10:48 +010036#include "tests/validation/Validation.h"
37#include "tests/validation/fixtures/BatchNormalizationLayerFixture.h"
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45namespace
46{
Gian Marco Iodice349feef2017-09-28 11:21:29 +010047constexpr AbsoluteTolerance<float> tolerance_f32(0.00001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
48constexpr 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 +000049const auto act_infos = framework::dataset::make("ActivationInfo",
50{
51 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
52 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f),
53 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f),
54});
Sanghoon Lee96883782017-09-15 14:10:48 +010055} // namespace
56
57TEST_SUITE(CL)
58TEST_SUITE(BatchNormalizationLayer)
59
60template <typename T>
61using CLBatchNormalizationLayerFixture = BatchNormalizationLayerValidationFixture<CLTensor, CLAccessor, CLBatchNormalizationLayer, T>;
62
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000063DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
64 combine(framework::dataset::make("UseBeta", { false, true }),
65 framework::dataset::make("UseGamma", { false, true }))),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +010066 framework::dataset::make("DataType", { DataType::F16, DataType::F32 })),
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +000067 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000068 shape0, shape1, epsilon, use_gamma, use_beta, dt, data_layout)
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
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000073 TensorShape src_dst_shapes = shape0;
74 if(data_layout == DataLayout::NHWC)
75 {
76 permute(src_dst_shapes, PermutationVector(2U, 0U, 1U));
77 }
78
Sanghoon Lee96883782017-09-15 14:10:48 +010079 // Create tensors
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000080 CLTensor src = create_tensor<CLTensor>(src_dst_shapes, dt, 1, fixed_point_position, QuantizationInfo(), data_layout);
81 CLTensor dst = create_tensor<CLTensor>(src_dst_shapes, dt, 1, fixed_point_position, QuantizationInfo(), data_layout);
Sanghoon Lee96883782017-09-15 14:10:48 +010082 CLTensor mean = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
83 CLTensor var = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
84 CLTensor beta = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
85 CLTensor gamma = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position);
86
87 // Create and Configure function
88 CLBatchNormalizationLayer norm;
Michele Di Giorgio4d336302018-03-02 09:43:54 +000089 CLTensor *beta_ptr = use_beta ? &beta : nullptr;
90 CLTensor *gamma_ptr = use_gamma ? &gamma : nullptr;
91 norm.configure(&src, &dst, &mean, &var, beta_ptr, gamma_ptr, epsilon);
Sanghoon Lee96883782017-09-15 14:10:48 +010092
93 // Validate valid region
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000094 const ValidRegion valid_region = shape_to_valid_region(src_dst_shapes);
Sanghoon Lee96883782017-09-15 14:10:48 +010095 validate(dst.info()->valid_region(), valid_region);
96}
97
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000098// *INDENT-OFF*
99// clang-format off
Giorgio Arena11674872018-02-07 15:38:12 +0000100DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
Giorgio Arena70623822017-11-27 15:50:10 +0000101 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
102 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
103 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
104 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types
105 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape
Giorgio Arena11674872018-02-07 15:38:12 +0000106 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Unsupported fused activation
107 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Fused activation's a < b
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000108 }),
Giorgio Arena70623822017-11-27 15:50:10 +0000109 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000110 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Giorgio Arena70623822017-11-27 15:50:10 +0000111 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
112 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F16),
113 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Giorgio Arena11674872018-02-07 15:38:12 +0000114 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
115 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000116 })),
117 framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32),
Giorgio Arena70623822017-11-27 15:50:10 +0000118 TensorInfo(TensorShape(2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000119 TensorInfo(TensorShape(2U), 1, DataType::F16),
120 TensorInfo(TensorShape(2U), 1, DataType::F32),
121 TensorInfo(TensorShape(5U), 1, DataType::F32),
Giorgio Arena11674872018-02-07 15:38:12 +0000122 TensorInfo(TensorShape(2U), 1, DataType::F32),
123 TensorInfo(TensorShape(2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000124 })),
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),
Giorgio Arena11674872018-02-07 15:38:12 +0000130 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
131 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 2.f, 6.f),
Giorgio Arena11674872018-02-07 15:38:12 +0000132 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100133 framework::dataset::make("Expected", { true, false, false, false, false, false, false})),
Giorgio Arena11674872018-02-07 15:38:12 +0000134 input_info, output_info, mvbg_info, act_info, expected)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000135{
Giorgio Arena70623822017-11-27 15:50:10 +0000136 const auto &mean_info = mvbg_info;
137 const auto &var_info = mvbg_info;
138 const auto &beta_info = mvbg_info;
139 const auto &gamma_info = mvbg_info;
Giorgio Arena11674872018-02-07 15:38:12 +0000140 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 +0000141 ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS);
142}
143// clang-format on
144// *INDENT-ON*
145
Sanghoon Lee96883782017-09-15 14:10:48 +0100146TEST_SUITE(Float)
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100147TEST_SUITE(FP32)
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000148FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000149 combine(framework::dataset::make("UseBeta", { false, true }),
150 framework::dataset::make("UseGamma", { false, true }))),
Giorgio Arena11674872018-02-07 15:38:12 +0000151 act_infos),
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000152 framework::dataset::make("DataType", DataType::F32)),
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000153 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
Sanghoon Lee96883782017-09-15 14:10:48 +0100154{
155 // Validate output
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100156 validate(CLAccessor(_target), _reference, tolerance_f32, 0);
Sanghoon Lee96883782017-09-15 14:10:48 +0100157}
158TEST_SUITE_END()
159
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100160TEST_SUITE(FP16)
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000161FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(),
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000162 combine(framework::dataset::make("UseBeta", { false, true }),
163 framework::dataset::make("UseGamma", { false, true }))),
Giorgio Arena11674872018-02-07 15:38:12 +0000164 framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))),
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000165 framework::dataset::make("DataType", DataType::F16)),
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000166 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
Gian Marco Iodice349feef2017-09-28 11:21:29 +0100167{
168 // Validate output
169 validate(CLAccessor(_target), _reference, tolerance_f16, 0);
170}
171TEST_SUITE_END()
172TEST_SUITE_END()
173
Sanghoon Lee96883782017-09-15 14:10:48 +0100174TEST_SUITE_END()
175TEST_SUITE_END()
176} // namespace validation
177} // namespace test
178} // namespace arm_compute