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 | */ |
| 24 | #include "Globals.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | #include "NEON/NEAccessor.h" |
Moritz Pflanzer | 5b51229 | 2017-06-21 15:54:07 +0100 | [diff] [blame] | 26 | #include "PaddingCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "TensorLibrary.h" |
| 28 | #include "TypePrinter.h" |
| 29 | #include "Utils.h" |
| 30 | #include "validation/Datasets.h" |
| 31 | #include "validation/Helpers.h" |
| 32 | #include "validation/Reference.h" |
| 33 | #include "validation/Validation.h" |
| 34 | |
| 35 | #include "arm_compute/core/Helpers.h" |
| 36 | #include "arm_compute/core/Types.h" |
| 37 | #include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" |
| 38 | #include "arm_compute/runtime/Tensor.h" |
| 39 | #include "arm_compute/runtime/TensorAllocator.h" |
| 40 | |
| 41 | #include "boost_wrapper.h" |
| 42 | |
| 43 | #include <random> |
| 44 | #include <string> |
| 45 | #include <tuple> |
| 46 | |
| 47 | using namespace arm_compute; |
| 48 | using namespace arm_compute::test; |
| 49 | using namespace arm_compute::test::neon; |
| 50 | using namespace arm_compute::test::validation; |
| 51 | |
| 52 | namespace |
| 53 | { |
| 54 | /** Define tolerance of the activation layer |
| 55 | * |
| 56 | * @param[in] activation The activation function used. |
| 57 | * @param[in] fixed_point_position Number of bits for the fractional part.. |
| 58 | * |
| 59 | * @return Tolerance depending on the activation function. |
| 60 | */ |
| 61 | float activation_layer_tolerance(ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 0) |
| 62 | { |
| 63 | switch(activation) |
| 64 | { |
| 65 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 66 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 67 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 68 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 69 | return (fixed_point_position != 0) ? 5.f : 0.00001f; |
| 70 | break; |
| 71 | default: |
| 72 | return 0.f; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** Compute Neon activation layer function. |
| 77 | * |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 78 | * @param[in] in_place Compute the activation layer in-place. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | * @param[in] shape Shape of the input and output tensors. |
| 80 | * @param[in] dt Shape Data type of tensors. |
| 81 | * @param[in] act_info Activation layer information. |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 82 | * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of fixed point numbers. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | * |
| 84 | * @return Computed output tensor. |
| 85 | */ |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 86 | Tensor compute_activation_layer(bool in_place, const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position = 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | { |
| 88 | // Create tensors |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 89 | Tensor src = create_tensor<Tensor>(shape, dt, 1, fixed_point_position); |
| 90 | Tensor dst = create_tensor<Tensor>(shape, dt, 1, fixed_point_position); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | |
| 92 | // Create and configure function |
| 93 | NEActivationLayer act_layer; |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 94 | |
| 95 | if(in_place) |
| 96 | { |
| 97 | act_layer.configure(&src, nullptr, act_info); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | act_layer.configure(&src, &dst, act_info); |
| 102 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | |
| 104 | // Allocate tensors |
| 105 | src.allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | BOOST_TEST(!src.info()->is_resizable()); |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 107 | |
| 108 | if(!in_place) |
| 109 | { |
| 110 | dst.allocator()->allocate(); |
| 111 | BOOST_TEST(!dst.info()->is_resizable()); |
| 112 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | |
| 114 | // Fill tensors |
| 115 | if(dt == DataType::F32) |
| 116 | { |
| 117 | float min_bound = 0; |
| 118 | float max_bound = 0; |
| 119 | std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<float>(act_info.activation()); |
| 120 | std::uniform_real_distribution<> distribution(min_bound, max_bound); |
| 121 | library->fill(NEAccessor(src), distribution, 0); |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | int min_bound = 0; |
| 126 | int max_bound = 0; |
| 127 | std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<int8_t>(act_info.activation(), fixed_point_position); |
| 128 | std::uniform_int_distribution<> distribution(min_bound, max_bound); |
| 129 | library->fill(NEAccessor(src), distribution, 0); |
| 130 | } |
| 131 | |
| 132 | // Compute function |
| 133 | act_layer.run(); |
| 134 | |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 135 | if(in_place) |
| 136 | { |
| 137 | return src; |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | return dst; |
| 142 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 143 | } |
| 144 | } // namespace |
| 145 | |
| 146 | #ifndef DOXYGEN_SKIP_THIS |
| 147 | BOOST_AUTO_TEST_SUITE(NEON) |
| 148 | BOOST_AUTO_TEST_SUITE(ActivationLayer) |
| 149 | |
| 150 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 151 | BOOST_DATA_TEST_CASE(Configuration, boost::unit_test::data::make({ false, true }) * (SmallShapes() + LargeShapes()) * CNNDataTypes(), in_place, shape, dt) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | { |
| 153 | // Set fixed point position data type allowed |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 154 | const int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | |
| 156 | // Create tensors |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 157 | Tensor src = create_tensor<Tensor>(shape, dt, 1, fixed_point_position); |
| 158 | Tensor dst = create_tensor<Tensor>(shape, dt, 1, fixed_point_position); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | |
| 160 | BOOST_TEST(src.info()->is_resizable()); |
| 161 | BOOST_TEST(dst.info()->is_resizable()); |
| 162 | |
| 163 | // Create and configure function |
| 164 | NEActivationLayer act_layer; |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 165 | |
| 166 | if(in_place) |
| 167 | { |
| 168 | act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); |
| 173 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | |
| 175 | // Validate valid region |
| 176 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 177 | validate(src.info()->valid_region(), valid_region); |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 178 | |
| 179 | if(!in_place) |
| 180 | { |
| 181 | validate(dst.info()->valid_region(), valid_region); |
| 182 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 183 | |
| 184 | // Validate padding |
Moritz Pflanzer | 2509fba | 2017-06-23 14:15:03 +0100 | [diff] [blame] | 185 | const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 186 | validate(src.info()->padding(), padding); |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 187 | |
| 188 | if(!in_place) |
| 189 | { |
| 190 | validate(dst.info()->padding(), padding); |
| 191 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | BOOST_AUTO_TEST_SUITE(Float) |
| 195 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 196 | BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * CNNFloatDataTypes() * ActivationFunctions(), in_place, shape, dt, act_function) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 197 | { |
| 198 | // Create activation layer info |
| 199 | ActivationLayerInfo act_info(act_function, 1.f, 1.f); |
| 200 | |
| 201 | // Compute function |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 202 | Tensor dst = compute_activation_layer(in_place, shape, dt, act_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 203 | |
| 204 | // Compute reference |
| 205 | RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, dt, act_info); |
| 206 | |
| 207 | // Validate output |
| 208 | validate(NEAccessor(dst), ref_dst, activation_layer_tolerance(act_function)); |
| 209 | } |
| 210 | |
| 211 | BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 212 | BOOST_DATA_TEST_CASE(RunLarge, boost::unit_test::data::make({ false, true }) * LargeShapes() * CNNFloatDataTypes() * ActivationFunctions(), in_place, shape, dt, act_function) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 213 | { |
| 214 | // Create activation layer info |
| 215 | ActivationLayerInfo act_info(act_function, 1.f, 1.f); |
| 216 | |
| 217 | // Compute function |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 218 | Tensor dst = compute_activation_layer(in_place, shape, dt, act_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 219 | |
| 220 | // Compute reference |
| 221 | RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, dt, act_info); |
| 222 | |
| 223 | // Validate output |
| 224 | validate(NEAccessor(dst), ref_dst, activation_layer_tolerance(act_function)); |
| 225 | } |
| 226 | BOOST_AUTO_TEST_SUITE_END() |
| 227 | |
| 228 | /** @note We test for fixed point precision [3,5] because [1,2] and [6,7] ranges |
| 229 | * cause overflowing issues in most of the transcendentals functions. |
| 230 | */ |
| 231 | BOOST_AUTO_TEST_SUITE(Quantized) |
| 232 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 233 | BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * ActivationFunctions() * boost::unit_test::data::xrange(3, 6, 1), |
| 234 | in_place, shape, act_function, fixed_point_position) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 235 | { |
| 236 | // Create activation layer info |
| 237 | ActivationLayerInfo act_info(act_function, 1.f, 1.f); |
| 238 | |
| 239 | // Compute function |
Moritz Pflanzer | 443c8b9 | 2017-06-27 12:36:21 +0100 | [diff] [blame] | 240 | Tensor dst = compute_activation_layer(in_place, shape, DataType::QS8, act_info, fixed_point_position); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 241 | |
| 242 | // Compute reference |
| 243 | RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, DataType::QS8, act_info, fixed_point_position); |
| 244 | |
| 245 | // Validate output |
| 246 | validate(NEAccessor(dst), ref_dst, activation_layer_tolerance(act_function, fixed_point_position)); |
| 247 | } |
| 248 | BOOST_AUTO_TEST_SUITE_END() |
| 249 | |
| 250 | BOOST_AUTO_TEST_SUITE_END() |
| 251 | BOOST_AUTO_TEST_SUITE_END() |
| 252 | #endif |