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/NEON/functions/NEBatchNormalizationLayer.h" |
| 26 | #include "arm_compute/runtime/Tensor.h" |
| 27 | #include "arm_compute/runtime/TensorAllocator.h" |
| 28 | #include "tests/NEON/Accessor.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 | { |
| 47 | constexpr AbsoluteTolerance<float> tolerance_f32(0.00001f); /**< 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] | 48 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 49 | constexpr AbsoluteTolerance<float> tolerance_f16(0.01f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */ |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 50 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Vidhya Sudhan Loganathan | 0fc2545 | 2018-06-18 14:40:56 +0100 | [diff] [blame] | 51 | const auto act_infos = framework::dataset::make("ActivationInfo", |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 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(NEON) |
| 60 | TEST_SUITE(BatchNormalizationLayer) |
| 61 | |
| 62 | template <typename T> |
| 63 | using NEBatchNormalizationLayerFixture = BatchNormalizationLayerValidationFixture<Tensor, Accessor, NEBatchNormalizationLayer, 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 }), framework::dataset::make("UseGamma", { false, true }))), |
Vidhya Sudhan Loganathan | 0fc2545 | 2018-06-18 14:40:56 +0100 | [diff] [blame] | 67 | framework::dataset::make("DataType", { DataType::F32 })), |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 68 | framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), |
| 69 | shape0, shape1, epsilon, use_beta, use_gamma, dt, data_layout) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 70 | { |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 71 | TensorShape src_dst_shapes = shape0; |
| 72 | if(data_layout == DataLayout::NHWC) |
| 73 | { |
| 74 | permute(src_dst_shapes, PermutationVector(2U, 0U, 1U)); |
| 75 | } |
| 76 | |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 77 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 78 | Tensor src = create_tensor<Tensor>(src_dst_shapes, dt, 1, QuantizationInfo(), data_layout); |
| 79 | Tensor dst = create_tensor<Tensor>(src_dst_shapes, dt, 1, QuantizationInfo(), data_layout); |
| 80 | Tensor mean = create_tensor<Tensor>(shape1, dt, 1); |
| 81 | Tensor var = create_tensor<Tensor>(shape1, dt, 1); |
| 82 | Tensor beta = create_tensor<Tensor>(shape1, dt, 1); |
| 83 | Tensor gamma = create_tensor<Tensor>(shape1, dt, 1); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 84 | |
| 85 | // Create and Configure function |
| 86 | NEBatchNormalizationLayer norm; |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 87 | Tensor *beta_ptr = use_beta ? &beta : nullptr; |
| 88 | Tensor *gamma_ptr = use_gamma ? &gamma : nullptr; |
| 89 | norm.configure(&src, &dst, &mean, &var, beta_ptr, gamma_ptr, epsilon); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 90 | |
| 91 | // Validate valid region |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 92 | const ValidRegion valid_region = shape_to_valid_region(src_dst_shapes); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 93 | validate(dst.info()->valid_region(), valid_region); |
| 94 | } |
| 95 | |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 96 | // *INDENT-OFF* |
| 97 | // clang-format off |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 98 | DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 99 | framework::dataset::make("InputInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 100 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink |
| 101 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 102 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 103 | 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] | 104 | 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] | 105 | }), |
| 106 | framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 107 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), |
| 108 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 109 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F16), |
| 110 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 111 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 112 | })), |
| 113 | framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 114 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 115 | TensorInfo(TensorShape(2U), 1, DataType::F16), |
| 116 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 117 | TensorInfo(TensorShape(5U), 1, DataType::F32), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 118 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 119 | })), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 120 | framework::dataset::make("ActivationLayerInfo",{ ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 121 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), |
| 122 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f), |
| 123 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f), |
| 124 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.f), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 125 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 2.f, 6.f), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 126 | })), |
Vidhya Sudhan Loganathan | 0fc2545 | 2018-06-18 14:40:56 +0100 | [diff] [blame] | 127 | framework::dataset::make("Expected", { true, false, false, false, false, false})), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 128 | input_info, output_info, mvbg_info, act_info, expected) |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 129 | { |
| 130 | const auto &mean_info = mvbg_info; |
| 131 | const auto &var_info = mvbg_info; |
| 132 | const auto &beta_info = mvbg_info; |
| 133 | const auto &gamma_info = mvbg_info; |
| 134 | bool has_error = bool(NEBatchNormalizationLayer::validate( |
| 135 | &input_info.clone()->set_is_resizable(false), output_info.total_size() ? &output_info.clone()->set_is_resizable(false) : nullptr, |
| 136 | &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] | 137 | &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] | 138 | ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS); |
| 139 | } |
| 140 | // clang-format on |
| 141 | // *INDENT-ON* |
| 142 | |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 143 | TEST_SUITE(Float) |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 144 | TEST_SUITE(FP32) |
| 145 | FIXTURE_DATA_TEST_CASE(Random, NEBatchNormalizationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(), |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 146 | combine(framework::dataset::make("UseBeta", { false, true }), |
| 147 | framework::dataset::make("UseGamma", { false, true }))), |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 148 | act_infos), |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 149 | framework::dataset::make("DataType", DataType::F32)), |
| 150 | framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 151 | { |
| 152 | // Validate output |
| 153 | validate(Accessor(_target), _reference, tolerance_f32, 0); |
| 154 | } |
| 155 | TEST_SUITE_END() |
| 156 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 157 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 158 | TEST_SUITE(FP16) |
| 159 | FIXTURE_DATA_TEST_CASE(Random, NEBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(), |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 160 | combine(framework::dataset::make("UseBeta", { false, true }), |
| 161 | framework::dataset::make("UseGamma", { false, true }))), |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 162 | framework::dataset::make("ActivationInfo", ActivationLayerInfo())), |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 163 | framework::dataset::make("DataType", DataType::F16)), |
| 164 | framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 165 | { |
| 166 | // Validate output |
| 167 | validate(Accessor(_target), _reference, tolerance_f16, 0); |
| 168 | } |
| 169 | TEST_SUITE_END() |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 170 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 171 | TEST_SUITE_END() |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 172 | |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 173 | TEST_SUITE_END() |
| 174 | TEST_SUITE_END() |
| 175 | } // namespace validation |
| 176 | } // namespace test |
| 177 | } // namespace arm_compute |