Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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/NEON/functions/NEBatchNormalizationLayer.h" |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 26 | #include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h" |
| 27 | #include "arm_compute/runtime/NEON/functions/NEFuseBatchNormalization.h" |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/Tensor.h" |
| 29 | #include "arm_compute/runtime/TensorAllocator.h" |
| 30 | #include "tests/NEON/Accessor.h" |
| 31 | #include "tests/PaddingCalculator.h" |
| 32 | #include "tests/datasets/RandomBatchNormalizationLayerDataset.h" |
| 33 | #include "tests/datasets/ShapeDatasets.h" |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 34 | #include "tests/datasets/SmallConvolutionLayerDataset.h" |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 35 | #include "tests/framework/Asserts.h" |
| 36 | #include "tests/framework/Macros.h" |
| 37 | #include "tests/framework/datasets/Datasets.h" |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 38 | #include "tests/validation/Helpers.h" |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 39 | #include "tests/validation/Validation.h" |
| 40 | #include "tests/validation/fixtures/BatchNormalizationLayerFixture.h" |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 41 | #include "tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h" |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | namespace test |
| 46 | { |
| 47 | namespace validation |
| 48 | { |
| 49 | namespace |
| 50 | { |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 51 | RelativeTolerance<float> rel_tolerance_f32(0.05f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ |
| 52 | constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.0001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 53 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Freddie Liardet | 8b9ce8c | 2021-11-04 14:38:03 +0000 | [diff] [blame] | 54 | constexpr AbsoluteTolerance<float> abs_tolerance_f16(0.015f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */ |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 55 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 56 | |
Vidhya Sudhan Loganathan | 0fc2545 | 2018-06-18 14:40:56 +0100 | [diff] [blame] | 57 | const auto act_infos = framework::dataset::make("ActivationInfo", |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 58 | { |
| 59 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 60 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f), |
| 61 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f), |
| 62 | }); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 63 | const auto common_fusion_dataset = combine(combine(combine(framework::dataset::make("UseBias", { false, true }), |
| 64 | framework::dataset::make("UseBeta", { false, true })), |
| 65 | framework::dataset::make("UseGamma", { false, true })), |
| 66 | framework::dataset::make("Epsilon", { 0.001f })); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 67 | } // namespace |
| 68 | |
| 69 | TEST_SUITE(NEON) |
| 70 | TEST_SUITE(BatchNormalizationLayer) |
| 71 | |
| 72 | template <typename T> |
| 73 | using NEBatchNormalizationLayerFixture = BatchNormalizationLayerValidationFixture<Tensor, Accessor, NEBatchNormalizationLayer, T>; |
| 74 | |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 75 | // *INDENT-OFF* |
| 76 | // clang-format off |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 77 | DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 78 | framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 79 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 80 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 81 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 82 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Fused activation's a < b |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 83 | }), |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 84 | framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 85 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 86 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F16), |
| 87 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 88 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 89 | })), |
| 90 | framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 91 | TensorInfo(TensorShape(2U), 1, DataType::F16), |
| 92 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 93 | TensorInfo(TensorShape(5U), 1, DataType::F32), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 94 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 95 | })), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 96 | framework::dataset::make("ActivationLayerInfo",{ ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 97 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f), |
| 98 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f), |
| 99 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 100 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 2.f, 6.f), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 101 | })), |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 102 | framework::dataset::make("Expected", { true, false, false, false, false})), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 103 | input_info, output_info, mvbg_info, act_info, expected) |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 104 | { |
| 105 | const auto &mean_info = mvbg_info; |
| 106 | const auto &var_info = mvbg_info; |
| 107 | const auto &beta_info = mvbg_info; |
| 108 | const auto &gamma_info = mvbg_info; |
| 109 | bool has_error = bool(NEBatchNormalizationLayer::validate( |
| 110 | &input_info.clone()->set_is_resizable(false), output_info.total_size() ? &output_info.clone()->set_is_resizable(false) : nullptr, |
| 111 | &mean_info.clone()->set_is_resizable(false), &var_info.clone()->set_is_resizable(false), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 112 | &beta_info.clone()->set_is_resizable(false), &gamma_info.clone()->set_is_resizable(false), 1.f, act_info)); |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 113 | ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS); |
| 114 | } |
| 115 | // clang-format on |
| 116 | // *INDENT-ON* |
| 117 | |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 118 | TEST_SUITE(Float) |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 119 | TEST_SUITE(FP32) |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 120 | FIXTURE_DATA_TEST_CASE(RandomSmall, NEBatchNormalizationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallRandomBatchNormalizationLayerDataset(), |
| 121 | combine(framework::dataset::make("UseBeta", { false, true }), |
| 122 | framework::dataset::make("UseGamma", { false, true }))), |
| 123 | act_infos), |
| 124 | framework::dataset::make("DataType", DataType::F32)), |
| 125 | framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 126 | { |
| 127 | // Validate output |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 128 | validate(Accessor(_target), _reference, abs_tolerance_f32, 0); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 129 | } |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 130 | FIXTURE_DATA_TEST_CASE(RandomLarge, NEBatchNormalizationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(datasets::LargeRandomBatchNormalizationLayerDataset(), |
| 131 | combine(framework::dataset::make("UseBeta", { false, true }), |
| 132 | framework::dataset::make("UseGamma", { false, true }))), |
| 133 | act_infos), |
| 134 | framework::dataset::make("DataType", DataType::F32)), |
| 135 | framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) |
| 136 | { |
| 137 | // Validate output |
| 138 | validate(Accessor(_target), _reference, abs_tolerance_f32, 0); |
| 139 | } |
| 140 | TEST_SUITE_END() // F32 |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 141 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 142 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 143 | TEST_SUITE(FP16) |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 144 | FIXTURE_DATA_TEST_CASE(RandomSmall, NEBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallRandomBatchNormalizationLayerDataset(), |
| 145 | combine(framework::dataset::make("UseBeta", { false, true }), |
| 146 | framework::dataset::make("UseGamma", { false, true }))), |
| 147 | framework::dataset::make("ActivationInfo", ActivationLayerInfo())), |
| 148 | framework::dataset::make("DataType", DataType::F16)), |
| 149 | framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) |
| 150 | { |
| 151 | // Validate output |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 152 | validate(Accessor(_target), _reference, abs_tolerance_f16, 0); |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | FIXTURE_DATA_TEST_CASE(RandomLarge, NEBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::LargeRandomBatchNormalizationLayerDataset(), |
| 156 | combine(framework::dataset::make("UseBeta", { false, true }), |
| 157 | framework::dataset::make("UseGamma", { false, true }))), |
| 158 | framework::dataset::make("ActivationInfo", ActivationLayerInfo())), |
| 159 | framework::dataset::make("DataType", DataType::F16)), |
| 160 | framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 161 | { |
| 162 | // Validate output |
Sheri Zhang | 8d5d78b | 2020-12-15 20:25:31 +0000 | [diff] [blame] | 163 | validate(Accessor(_target), _reference, abs_tolerance_f16, 0); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 164 | } |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 165 | TEST_SUITE_END() // FP16 |
| 166 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 167 | TEST_SUITE_END() // Float |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 168 | |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 169 | TEST_SUITE_END() // BatchNormalizationLayer |
| 170 | |
| 171 | TEST_SUITE(BatchNormalizationLayerFusion) |
| 172 | template <typename T> |
| 173 | using NEBatchNormalizationLayerFusionFixture = BatchNormalizationLayerFusionValidationFixture<Tensor, Accessor, NEConvolutionLayer, NEFuseBatchNormalization, T>; |
| 174 | |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 175 | // *INDENT-OFF* |
| 176 | // clang-format off |
| 177 | DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip( |
| 178 | framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), // Valid |
| 179 | TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), // Mismatching data types |
| 180 | TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F16), // Mismatching data types |
| 181 | TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape |
| 182 | }), |
| 183 | framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 184 | TensorInfo(TensorShape(2U), 1, DataType::F16), |
| 185 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 186 | TensorInfo(TensorShape(5U), 1, DataType::F32), |
| 187 | })), |
| 188 | framework::dataset::make("Expected", { true, false, false, false})), |
| 189 | weights_info, mvbg_info, expected) |
| 190 | { |
| 191 | const auto &weights_in_info = weights_info; |
| 192 | const auto &mean_info = mvbg_info; |
| 193 | const auto &var_info = mvbg_info; |
| 194 | const auto &fused_weights_info = weights_info; |
| 195 | const auto &fused_bias_info = mvbg_info; |
| 196 | const auto &conv_bias_info = mvbg_info; |
| 197 | const auto &beta_info = mvbg_info; |
| 198 | const auto &gamma_info = mvbg_info; |
| 199 | bool has_error = bool(NEFuseBatchNormalization::validate( |
| 200 | &weights_in_info.clone()->set_is_resizable(false), &mean_info.clone()->set_is_resizable(false), |
| 201 | &var_info.clone()->set_is_resizable(false), &fused_weights_info.clone()->set_is_resizable(false), |
| 202 | &fused_bias_info.clone()->set_is_resizable(false), &conv_bias_info.clone()->set_is_resizable(false), |
| 203 | &beta_info.clone()->set_is_resizable(false), &gamma_info.clone()->set_is_resizable(false), 1.f)); |
| 204 | ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS); |
| 205 | } |
| 206 | // clang-format on |
| 207 | // *INDENT-ON* |
| 208 | |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 209 | TEST_SUITE(Float) |
| 210 | TEST_SUITE(FP32) |
| 211 | FIXTURE_DATA_TEST_CASE(RunSmall, NEBatchNormalizationLayerFusionFixture<float>, framework::DatasetMode::PRECOMMIT, |
| 212 | combine(combine(combine(datasets::SmallConvolutionLayerDataset(), common_fusion_dataset), |
| 213 | framework::dataset::make("DataType", DataType::F32)), |
| 214 | framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) |
| 215 | { |
| 216 | // Validate output |
| 217 | validate(Accessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32); |
| 218 | } |
| 219 | TEST_SUITE_END() // FP32 |
| 220 | TEST_SUITE_END() // Float |
| 221 | |
| 222 | TEST_SUITE_END() // BatchNormalizationLayerFusion |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 223 | TEST_SUITE_END() // Neon |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 224 | } // namespace validation |
| 225 | } // namespace test |
| 226 | } // namespace arm_compute |