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