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