Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 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" |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 35 | #include "tests/validation/Helpers.h" |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 36 | #include "tests/validation/Validation.h" |
| 37 | #include "tests/validation/fixtures/BatchNormalizationLayerFixture.h" |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
| 45 | namespace |
| 46 | { |
Gian Marco Iodice | 349feef | 2017-09-28 11:21:29 +0100 | [diff] [blame] | 47 | constexpr AbsoluteTolerance<float> tolerance_f32(0.00001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ |
| 48 | constexpr AbsoluteTolerance<float> tolerance_f16(0.01f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */ |
| 49 | constexpr AbsoluteTolerance<float> tolerance_qs8(3.0f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::QS8 */ |
| 50 | constexpr AbsoluteTolerance<float> tolerance_qs16(6.0f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::QS16 */ |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 51 | const auto act_infos = framework::dataset::make("ActivationInfo", |
| 52 | { |
| 53 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 54 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f), |
| 55 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f), |
| 56 | }); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 57 | } // namespace |
| 58 | |
| 59 | TEST_SUITE(CL) |
| 60 | TEST_SUITE(BatchNormalizationLayer) |
| 61 | |
| 62 | template <typename T> |
| 63 | using CLBatchNormalizationLayerFixture = BatchNormalizationLayerValidationFixture<CLTensor, CLAccessor, CLBatchNormalizationLayer, T>; |
| 64 | |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 65 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(), |
| 66 | combine(framework::dataset::make("UseBeta", { false, true }), |
| 67 | framework::dataset::make("UseGamma", { false, true }))), |
| 68 | framework::dataset::make("DataType", { DataType::QS8, DataType::QS16, DataType::F16, DataType::F32 })), |
| 69 | framework::dataset::make("DataLayout", { DataLayout::NCHW })), |
| 70 | shape0, shape1, epsilon, use_gamma, use_beta, dt, data_layout) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 71 | { |
| 72 | // Set fixed point position data type allowed |
Sanghoon Lee | c1294fa | 2017-11-17 11:47:41 +0000 | [diff] [blame] | 73 | const int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0; |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 74 | |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 75 | TensorShape src_dst_shapes = shape0; |
| 76 | if(data_layout == DataLayout::NHWC) |
| 77 | { |
| 78 | permute(src_dst_shapes, PermutationVector(2U, 0U, 1U)); |
| 79 | } |
| 80 | |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 81 | // Create tensors |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 82 | CLTensor src = create_tensor<CLTensor>(src_dst_shapes, dt, 1, fixed_point_position, QuantizationInfo(), data_layout); |
| 83 | CLTensor dst = create_tensor<CLTensor>(src_dst_shapes, dt, 1, fixed_point_position, QuantizationInfo(), data_layout); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 84 | CLTensor mean = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position); |
| 85 | CLTensor var = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position); |
| 86 | CLTensor beta = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position); |
| 87 | CLTensor gamma = create_tensor<CLTensor>(shape1, dt, 1, fixed_point_position); |
| 88 | |
| 89 | // Create and Configure function |
| 90 | CLBatchNormalizationLayer norm; |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 91 | CLTensor *beta_ptr = use_beta ? &beta : nullptr; |
| 92 | CLTensor *gamma_ptr = use_gamma ? &gamma : nullptr; |
| 93 | norm.configure(&src, &dst, &mean, &var, beta_ptr, gamma_ptr, epsilon); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 94 | |
| 95 | // Validate valid region |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 96 | const ValidRegion valid_region = shape_to_valid_region(src_dst_shapes); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 97 | validate(dst.info()->valid_region(), valid_region); |
| 98 | } |
| 99 | |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 100 | // *INDENT-OFF* |
| 101 | // clang-format off |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 102 | DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 103 | framework::dataset::make("InputInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 104 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink |
| 105 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 106 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 107 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape |
| 108 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point position |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 109 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), // Fused activation with fixed point not supported |
| 110 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Unsupported fused activation |
| 111 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Fused activation's a < b |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 112 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), |
| 113 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 114 | }), |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 115 | framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 116 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 117 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 118 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F16), |
| 119 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 120 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 3), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 121 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 122 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 123 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 3), |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 124 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), |
| 125 | TensorInfo(), |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 126 | })), |
| 127 | framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 128 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 129 | TensorInfo(TensorShape(2U), 1, DataType::F16), |
| 130 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 131 | TensorInfo(TensorShape(5U), 1, DataType::F32), |
| 132 | TensorInfo(TensorShape(2U), 1, DataType::QS8, 2), |
| 133 | TensorInfo(TensorShape(2U), 1, DataType::QS8, 2), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 134 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 135 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 136 | TensorInfo(TensorShape(2U), 1, DataType::QS8, 2), |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 137 | TensorInfo(TensorShape(2U), 1, DataType::QS8, 2), |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 138 | })), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 139 | framework::dataset::make("ActivationLayerInfo",{ ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 140 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 141 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f), |
| 142 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f), |
| 143 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f), |
| 144 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f, 2.f), |
| 145 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f, 2.f), |
| 146 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), |
| 147 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 2.f, 6.f), |
| 148 | ActivationLayerInfo(), |
| 149 | ActivationLayerInfo(), |
| 150 | })), |
| 151 | framework::dataset::make("Expected", { true, false, false, false, false, false, false, false, false, true, true})), |
| 152 | input_info, output_info, mvbg_info, act_info, expected) |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 153 | { |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 154 | const auto &mean_info = mvbg_info; |
| 155 | const auto &var_info = mvbg_info; |
| 156 | const auto &beta_info = mvbg_info; |
| 157 | const auto &gamma_info = mvbg_info; |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 158 | 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 Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 159 | ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS); |
| 160 | } |
| 161 | // clang-format on |
| 162 | // *INDENT-ON* |
| 163 | |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 164 | TEST_SUITE(Float) |
Gian Marco Iodice | 349feef | 2017-09-28 11:21:29 +0100 | [diff] [blame] | 165 | TEST_SUITE(FP32) |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 166 | FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(), |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 167 | combine(framework::dataset::make("UseBeta", { false, true }), |
| 168 | framework::dataset::make("UseGamma", { false, true }))), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 169 | act_infos), |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 170 | framework::dataset::make("DataType", DataType::F32)), |
| 171 | framework::dataset::make("DataLayout", { DataLayout::NCHW }))) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 172 | { |
| 173 | // Validate output |
Gian Marco Iodice | 349feef | 2017-09-28 11:21:29 +0100 | [diff] [blame] | 174 | validate(CLAccessor(_target), _reference, tolerance_f32, 0); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 175 | } |
| 176 | TEST_SUITE_END() |
| 177 | |
Gian Marco Iodice | 349feef | 2017-09-28 11:21:29 +0100 | [diff] [blame] | 178 | TEST_SUITE(FP16) |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 179 | FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(), |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 180 | combine(framework::dataset::make("UseBeta", { false, true }), |
| 181 | framework::dataset::make("UseGamma", { false, true }))), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 182 | framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f))), |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 183 | framework::dataset::make("DataType", DataType::F16)), |
| 184 | framework::dataset::make("DataLayout", { DataLayout::NCHW }))) |
Gian Marco Iodice | 349feef | 2017-09-28 11:21:29 +0100 | [diff] [blame] | 185 | { |
| 186 | // Validate output |
| 187 | validate(CLAccessor(_target), _reference, tolerance_f16, 0); |
| 188 | } |
| 189 | TEST_SUITE_END() |
| 190 | TEST_SUITE_END() |
| 191 | |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 192 | TEST_SUITE(Quantized) |
| 193 | template <typename T> |
| 194 | using CLBatchNormalizationLayerFixedPointFixture = BatchNormalizationLayerValidationFixedPointFixture<CLTensor, CLAccessor, CLBatchNormalizationLayer, T>; |
| 195 | |
| 196 | TEST_SUITE(QS8) |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 197 | FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 198 | combine(combine(combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(), |
| 199 | framework::dataset::make("UseBeta", false)), |
| 200 | framework::dataset::make("UseGamma", false)), |
| 201 | framework::dataset::make("ActivationInfo", ActivationLayerInfo())), |
| 202 | framework::dataset::make("DataType", DataType::QS8)), |
| 203 | framework::dataset::make("DataLayout", DataLayout::NCHW)), |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 204 | framework::dataset::make("FractionalBits", 1, 6))) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 205 | { |
| 206 | // Validate output |
| 207 | validate(CLAccessor(_target), _reference, tolerance_qs8, 0); |
| 208 | } |
| 209 | TEST_SUITE_END() |
| 210 | |
| 211 | TEST_SUITE(QS16) |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 212 | FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 213 | combine(combine(combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(), |
| 214 | framework::dataset::make("UseBeta", false)), |
| 215 | framework::dataset::make("UseGamma", false)), |
| 216 | framework::dataset::make("ActivationInfo", ActivationLayerInfo())), |
| 217 | framework::dataset::make("DataType", DataType::QS16)), |
| 218 | framework::dataset::make("DataLayout", DataLayout::NCHW)), |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 219 | framework::dataset::make("FractionalBits", 1, 14))) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 220 | { |
| 221 | // Validate output |
| 222 | validate(CLAccessor(_target), _reference, tolerance_qs16, 0); |
| 223 | } |
| 224 | TEST_SUITE_END() |
| 225 | |
| 226 | TEST_SUITE_END() |
| 227 | |
| 228 | TEST_SUITE_END() |
| 229 | TEST_SUITE_END() |
| 230 | } // namespace validation |
| 231 | } // namespace test |
| 232 | } // namespace arm_compute |