Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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" |
| 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 | { |
| 46 | 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] | 47 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 48 | 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] | 49 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 50 | constexpr AbsoluteTolerance<float> tolerance_qs8(3.0f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::QS8 */ |
| 51 | constexpr AbsoluteTolerance<float> tolerance_qs16(6.0f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::QS16 */ |
| 52 | } // namespace |
| 53 | |
| 54 | TEST_SUITE(NEON) |
| 55 | TEST_SUITE(BatchNormalizationLayer) |
| 56 | |
| 57 | template <typename T> |
| 58 | using NEBatchNormalizationLayerFixture = BatchNormalizationLayerValidationFixture<Tensor, Accessor, NEBatchNormalizationLayer, T>; |
| 59 | |
| 60 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::RandomBatchNormalizationLayerDataset(), framework::dataset::make("DataType", { DataType::QS8, DataType::QS16, DataType::F32 })), |
| 61 | shape0, shape1, epsilon, dt) |
| 62 | { |
| 63 | // Set fixed point position data type allowed |
Sanghoon Lee | c1294fa | 2017-11-17 11:47:41 +0000 | [diff] [blame] | 64 | 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] | 65 | |
| 66 | // Create tensors |
| 67 | Tensor src = create_tensor<Tensor>(shape0, dt, 1, fixed_point_position); |
| 68 | Tensor dst = create_tensor<Tensor>(shape0, dt, 1, fixed_point_position); |
| 69 | Tensor mean = create_tensor<Tensor>(shape1, dt, 1, fixed_point_position); |
| 70 | Tensor var = create_tensor<Tensor>(shape1, dt, 1, fixed_point_position); |
| 71 | Tensor beta = create_tensor<Tensor>(shape1, dt, 1, fixed_point_position); |
| 72 | Tensor gamma = create_tensor<Tensor>(shape1, dt, 1, fixed_point_position); |
| 73 | |
| 74 | // Create and Configure function |
| 75 | NEBatchNormalizationLayer norm; |
| 76 | norm.configure(&src, &dst, &mean, &var, &beta, &gamma, epsilon); |
| 77 | |
| 78 | // Validate valid region |
| 79 | const ValidRegion valid_region = shape_to_valid_region(shape0); |
| 80 | validate(dst.info()->valid_region(), valid_region); |
| 81 | } |
| 82 | |
Ioan-Cristian Szabo | 303be90 | 2017-11-27 16:31:10 +0000 | [diff] [blame] | 83 | // *INDENT-OFF* |
| 84 | // clang-format off |
| 85 | DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip( |
| 86 | framework::dataset::make("InputInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 87 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink |
| 88 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 89 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 90 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Invalid mean/var/beta/gamma shape |
| 91 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point position |
| 92 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), |
| 93 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), |
| 94 | }), |
| 95 | framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 96 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), |
| 97 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 98 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F16), |
| 99 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 100 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 3), |
| 101 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2), |
| 102 | TensorInfo(), |
| 103 | })), |
| 104 | framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 105 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 106 | TensorInfo(TensorShape(2U), 1, DataType::F16), |
| 107 | TensorInfo(TensorShape(2U), 1, DataType::F32), |
| 108 | TensorInfo(TensorShape(5U), 1, DataType::F32), |
| 109 | TensorInfo(TensorShape(2U), 1, DataType::QS8, 2), |
| 110 | TensorInfo(TensorShape(2U), 1, DataType::QS8, 2), |
| 111 | TensorInfo(TensorShape(2U), 1, DataType::QS8, 2), |
| 112 | })), |
| 113 | framework::dataset::make("Expected", { false, true, true, true, true, true, false, false})), |
| 114 | input_info, output_info, mvbg_info, expected) |
| 115 | { |
| 116 | const auto &mean_info = mvbg_info; |
| 117 | const auto &var_info = mvbg_info; |
| 118 | const auto &beta_info = mvbg_info; |
| 119 | const auto &gamma_info = mvbg_info; |
| 120 | bool has_error = bool(NEBatchNormalizationLayer::validate( |
| 121 | &input_info.clone()->set_is_resizable(false), output_info.total_size() ? &output_info.clone()->set_is_resizable(false) : nullptr, |
| 122 | &mean_info.clone()->set_is_resizable(false), &var_info.clone()->set_is_resizable(false), |
| 123 | &beta_info.clone()->set_is_resizable(false), &gamma_info.clone()->set_is_resizable(false), 1.f)); |
| 124 | ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS); |
| 125 | } |
| 126 | // clang-format on |
| 127 | // *INDENT-ON* |
| 128 | |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 129 | TEST_SUITE(Float) |
| 130 | FIXTURE_DATA_TEST_CASE(Random, NEBatchNormalizationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(datasets::RandomBatchNormalizationLayerDataset(), |
| 131 | framework::dataset::make("DataType", DataType::F32))) |
| 132 | { |
| 133 | // Validate output |
| 134 | validate(Accessor(_target), _reference, tolerance_f32, 0); |
| 135 | } |
| 136 | TEST_SUITE_END() |
| 137 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 138 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 139 | TEST_SUITE(Float16) |
| 140 | FIXTURE_DATA_TEST_CASE(Random, NEBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(datasets::RandomBatchNormalizationLayerDataset(), |
| 141 | framework::dataset::make("DataType", DataType::F16))) |
| 142 | { |
| 143 | // Validate output |
| 144 | validate(Accessor(_target), _reference, tolerance_f16, 0); |
| 145 | } |
| 146 | TEST_SUITE_END() |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 147 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 148 | |
| 149 | TEST_SUITE(Quantized) |
| 150 | template <typename T> |
| 151 | using NEBatchNormalizationLayerFixedPointFixture = BatchNormalizationLayerValidationFixedPointFixture<Tensor, Accessor, NEBatchNormalizationLayer, T>; |
| 152 | |
| 153 | TEST_SUITE(QS8) |
| 154 | FIXTURE_DATA_TEST_CASE(Random, NEBatchNormalizationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::RandomBatchNormalizationLayerDataset(), |
| 155 | framework::dataset::make("DataType", DataType::QS8)), |
| 156 | framework::dataset::make("FractionalBits", 1, 6))) |
| 157 | { |
| 158 | // Validate output |
| 159 | validate(Accessor(_target), _reference, tolerance_qs8, 0); |
| 160 | } |
| 161 | TEST_SUITE_END() |
| 162 | |
| 163 | TEST_SUITE(QS16) |
| 164 | FIXTURE_DATA_TEST_CASE(Random, NEBatchNormalizationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::RandomBatchNormalizationLayerDataset(), |
| 165 | framework::dataset::make("DataType", DataType::QS16)), |
| 166 | framework::dataset::make("FractionalBits", 1, 14))) |
| 167 | { |
| 168 | // Validate output |
| 169 | validate(Accessor(_target), _reference, tolerance_qs16, 0); |
| 170 | } |
| 171 | TEST_SUITE_END() |
| 172 | |
| 173 | TEST_SUITE_END() |
| 174 | |
| 175 | TEST_SUITE_END() |
| 176 | TEST_SUITE_END() |
| 177 | } // namespace validation |
| 178 | } // namespace test |
| 179 | } // namespace arm_compute |