Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 24 | #include "NEON/NEAccessor.h" |
| 25 | #include "TypePrinter.h" |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 26 | #include "tests/Globals.h" |
| 27 | #include "tests/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "validation/Datasets.h" |
| 29 | #include "validation/Reference.h" |
| 30 | #include "validation/Validation.h" |
| 31 | |
| 32 | #include "arm_compute/runtime/NEON/functions/NENormalizationLayer.h" |
| 33 | |
| 34 | #include <random> |
| 35 | |
| 36 | using namespace arm_compute; |
| 37 | using namespace arm_compute::test; |
| 38 | using namespace arm_compute::test::neon; |
| 39 | using namespace arm_compute::test::validation; |
| 40 | |
| 41 | namespace |
| 42 | { |
| 43 | /** Define tolerance of the normalization layer depending on values data type. |
| 44 | * |
| 45 | * @param[in] dt Data type of the tensors' values. |
| 46 | * |
| 47 | * @return Tolerance depending on the data type. |
| 48 | */ |
| 49 | float normalization_layer_tolerance(DataType dt) |
| 50 | { |
| 51 | switch(dt) |
| 52 | { |
| 53 | case DataType::QS8: |
| 54 | return 2.0f; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame^] | 55 | case DataType::F16: |
| 56 | return 0.001f; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | case DataType::F32: |
| 58 | return 1e-05; |
| 59 | default: |
| 60 | return 0.f; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** Compute Neon normalization layer function. |
| 65 | * |
| 66 | * @param[in] shape Shape of the input and output tensors. |
| 67 | * @param[in] dt Data type of input and output tensors. |
| 68 | * @param[in] norm_info Normalization Layer information. |
| 69 | * @param[in] fixed_point_position (Optional) Fixed point position that expresses the number of bits for the fractional part of the number when the tensor's data type is QS8 or QS16 (default = 0). |
| 70 | * |
| 71 | * @return Computed output tensor. |
| 72 | */ |
| 73 | Tensor compute_normalization_layer(const TensorShape &shape, DataType dt, NormalizationLayerInfo norm_info, int fixed_point_position = 0) |
| 74 | { |
| 75 | // Create tensors |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 76 | Tensor src = create_tensor<Tensor>(shape, dt, 1, fixed_point_position); |
| 77 | Tensor dst = create_tensor<Tensor>(shape, dt, 1, fixed_point_position); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | |
| 79 | // Create and configure function |
| 80 | NENormalizationLayer norm; |
| 81 | norm.configure(&src, &dst, norm_info); |
| 82 | |
| 83 | // Allocate tensors |
| 84 | src.allocator()->allocate(); |
| 85 | dst.allocator()->allocate(); |
| 86 | |
| 87 | BOOST_TEST(!src.info()->is_resizable()); |
| 88 | BOOST_TEST(!dst.info()->is_resizable()); |
| 89 | |
| 90 | // Fill tensors |
| 91 | if(dt == DataType::QS8) |
| 92 | { |
| 93 | const int8_t one_fixed_point = 1 << fixed_point_position; |
| 94 | const int8_t minus_one_fixed_point = -one_fixed_point; |
| 95 | library->fill_tensor_uniform(NEAccessor(src), 0, minus_one_fixed_point, one_fixed_point); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | library->fill_tensor_uniform(NEAccessor(src), 0); |
| 100 | } |
| 101 | |
| 102 | // Compute function |
| 103 | norm.run(); |
| 104 | |
| 105 | return dst; |
| 106 | } |
| 107 | } // namespace |
| 108 | |
| 109 | #ifndef DOXYGEN_SKIP_THIS |
| 110 | BOOST_AUTO_TEST_SUITE(NEON) |
| 111 | BOOST_AUTO_TEST_SUITE(NormalizationLayer) |
| 112 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame^] | 113 | #ifdef ARM_COMPUTE_ENABLE_FP16 |
| 114 | BOOST_AUTO_TEST_SUITE(Float16) |
| 115 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 116 | BOOST_DATA_TEST_CASE(RunSmall, |
| 117 | SmallShapes() * DataType::F16 *NormalizationTypes() * boost::unit_test::data::xrange(3, 9, 2) * boost::unit_test::data::make({ 0.5f, 1.0f, 2.0f }), |
| 118 | shape, dt, norm_type, norm_size, beta) |
| 119 | { |
| 120 | // Provide normalization layer information |
| 121 | NormalizationLayerInfo norm_info(norm_type, norm_size, 5, beta); |
| 122 | |
| 123 | // Compute function |
| 124 | Tensor dst = compute_normalization_layer(shape, dt, norm_info); |
| 125 | |
| 126 | // Compute reference |
| 127 | RawTensor ref_dst = Reference::compute_reference_normalization_layer(shape, dt, norm_info); |
| 128 | |
| 129 | // Validate output |
| 130 | validate(NEAccessor(dst), ref_dst, normalization_layer_tolerance(DataType::F16)); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE_END() |
| 134 | #endif /* ARM_COMPUTE_ENABLE_FP16 */ |
| 135 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 136 | BOOST_AUTO_TEST_SUITE(Float) |
| 137 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 138 | BOOST_DATA_TEST_CASE(RunSmall, |
| 139 | SmallShapes() * DataType::F32 *NormalizationTypes() * boost::unit_test::data::xrange(3, 9, 2) * boost::unit_test::data::make({ 0.5f, 1.0f, 2.0f }), |
| 140 | shape, dt, norm_type, norm_size, beta) |
| 141 | { |
| 142 | // Provide normalization layer information |
| 143 | NormalizationLayerInfo norm_info(norm_type, norm_size, 5, beta); |
| 144 | |
| 145 | // Compute function |
| 146 | Tensor dst = compute_normalization_layer(shape, dt, norm_info); |
| 147 | |
| 148 | // Compute reference |
| 149 | RawTensor ref_dst = Reference::compute_reference_normalization_layer(shape, dt, norm_info); |
| 150 | |
| 151 | // Validate output |
| 152 | validate(NEAccessor(dst), ref_dst, normalization_layer_tolerance(DataType::F32)); |
| 153 | } |
| 154 | BOOST_AUTO_TEST_SUITE_END() |
| 155 | |
| 156 | BOOST_AUTO_TEST_SUITE(Quantized) |
| 157 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 158 | BOOST_DATA_TEST_CASE(RunSmall, |
| 159 | SmallShapes() * DataType::QS8 *NormalizationTypes() * boost::unit_test::data::xrange(3, 7, 2) * (boost::unit_test::data::xrange(1, 6) * boost::unit_test::data::make({ 0.5f, 1.0f, 2.0f })), |
| 160 | shape, dt, norm_type, norm_size, fixed_point_position, beta) |
| 161 | { |
| 162 | // Provide normalization layer information |
| 163 | NormalizationLayerInfo norm_info(norm_type, norm_size, 5, beta, 1.f); |
| 164 | |
| 165 | // Compute function |
| 166 | Tensor dst = compute_normalization_layer(shape, dt, norm_info, fixed_point_position); |
| 167 | |
| 168 | // Compute reference |
| 169 | RawTensor ref_dst = Reference::compute_reference_normalization_layer(shape, dt, norm_info, fixed_point_position); |
| 170 | |
| 171 | // Validate output |
| 172 | validate(NEAccessor(dst), ref_dst, normalization_layer_tolerance(DataType::QS8)); |
| 173 | } |
| 174 | BOOST_AUTO_TEST_SUITE_END() |
| 175 | |
| 176 | BOOST_AUTO_TEST_SUITE_END() |
| 177 | BOOST_AUTO_TEST_SUITE_END() |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 178 | #endif /* DOXYGEN_SKIP_THIS */ |