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" |
| 25 | #include "NEON/Helper.h" |
| 26 | #include "NEON/NEAccessor.h" |
Moritz Pflanzer | 5b51229 | 2017-06-21 15:54:07 +0100 | [diff] [blame^] | 27 | #include "PaddingCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "TensorLibrary.h" |
| 29 | #include "TypePrinter.h" |
| 30 | #include "Utils.h" |
| 31 | #include "validation/Datasets.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/NESoftmaxLayer.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 | |
| 46 | using namespace arm_compute; |
| 47 | using namespace arm_compute::test; |
| 48 | using namespace arm_compute::test::neon; |
| 49 | using namespace arm_compute::test::validation; |
| 50 | |
| 51 | namespace |
| 52 | { |
| 53 | /** Tolerance for float operations */ |
| 54 | const float tolerance = 0.000001f; |
| 55 | /** Tolerance for fixed point operations */ |
| 56 | const float tolerance_fixed_point = 2.f; |
| 57 | |
| 58 | /** Compute Neon softmax layer function. |
| 59 | * |
| 60 | * @param[in] shape Shape of the input and output tensors. |
| 61 | * @param[in] dt Shape Data type of tensors. |
| 62 | * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of fixed point numbers. |
| 63 | * |
| 64 | * @return Computed output tensor. |
| 65 | */ |
| 66 | Tensor compute_softmax_layer(const TensorShape &shape, DataType dt, int fixed_point_position = 0) |
| 67 | { |
| 68 | // Create tensors |
| 69 | Tensor src = create_tensor(shape, dt, 1, fixed_point_position); |
| 70 | Tensor dst = create_tensor(shape, dt, 1, fixed_point_position); |
| 71 | |
| 72 | // Create and configure function |
| 73 | NESoftmaxLayer smx_layer; |
| 74 | smx_layer.configure(&src, &dst); |
| 75 | |
| 76 | // Allocate tensors |
| 77 | src.allocator()->allocate(); |
| 78 | dst.allocator()->allocate(); |
| 79 | |
| 80 | BOOST_TEST(!src.info()->is_resizable()); |
| 81 | BOOST_TEST(!dst.info()->is_resizable()); |
| 82 | |
| 83 | // Fill tensors |
| 84 | if(arm_compute::is_data_type_float(dt)) |
| 85 | { |
| 86 | std::uniform_real_distribution<> distribution(-10, 10); |
| 87 | library->fill(NEAccessor(src), distribution, 0); |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | int one_fixed = 1 << fixed_point_position; |
| 92 | std::uniform_int_distribution<> distribution(-one_fixed, one_fixed); |
| 93 | library->fill(NEAccessor(src), distribution, 0); |
| 94 | } |
| 95 | |
| 96 | // Compute function |
| 97 | smx_layer.run(); |
| 98 | |
| 99 | return dst; |
| 100 | } |
| 101 | } // namespace |
| 102 | |
| 103 | #ifndef DOXYGEN_SKIP_THIS |
| 104 | BOOST_AUTO_TEST_SUITE(NEON) |
| 105 | BOOST_AUTO_TEST_SUITE(SoftmaxLayer) |
| 106 | |
| 107 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) |
| 108 | BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * CNNDataTypes(), shape, dt) |
| 109 | { |
| 110 | // Set fixed point position data type allowed |
| 111 | int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0; |
| 112 | |
| 113 | // Create tensors |
| 114 | Tensor src = create_tensor(shape, dt, 1, fixed_point_position); |
| 115 | Tensor dst = create_tensor(shape, dt, 1, fixed_point_position); |
| 116 | |
| 117 | BOOST_TEST(src.info()->is_resizable()); |
| 118 | BOOST_TEST(dst.info()->is_resizable()); |
| 119 | |
| 120 | // Create and configure function |
| 121 | NESoftmaxLayer smx_layer; |
| 122 | smx_layer.configure(&src, &dst); |
| 123 | |
| 124 | // Validate valid region |
| 125 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 126 | validate(src.info()->valid_region(), valid_region); |
| 127 | validate(dst.info()->valid_region(), valid_region); |
| 128 | |
| 129 | // Validate padding |
| 130 | int step = 16 / arm_compute::data_size_from_type(dt); |
Moritz Pflanzer | 5b51229 | 2017-06-21 15:54:07 +0100 | [diff] [blame^] | 131 | const PaddingSize padding(0, PaddingCalculator(shape.x(), step).required_padding(), 0, 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | validate(src.info()->padding(), padding); |
| 133 | validate(dst.info()->padding(), padding); |
| 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_SUITE(Float) |
| 137 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 138 | BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * CNNFloatDataTypes(), shape, dt) |
| 139 | { |
| 140 | // Compute function |
| 141 | Tensor dst = compute_softmax_layer(shape, dt); |
| 142 | |
| 143 | // Compute reference |
| 144 | RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt); |
| 145 | |
| 146 | // Validate output |
| 147 | validate(NEAccessor(dst), ref_dst, tolerance); |
| 148 | } |
| 149 | |
| 150 | BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) |
| 151 | BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * CNNFloatDataTypes(), shape, dt) |
| 152 | { |
| 153 | // Compute function |
| 154 | Tensor dst = compute_softmax_layer(shape, dt); |
| 155 | |
| 156 | // Compute reference |
| 157 | RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt); |
| 158 | |
| 159 | // Validate output |
| 160 | validate(NEAccessor(dst), ref_dst, tolerance); |
| 161 | } |
| 162 | BOOST_AUTO_TEST_SUITE_END() |
| 163 | |
| 164 | BOOST_AUTO_TEST_SUITE(Quantized) |
| 165 | // Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5 |
| 166 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 167 | BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * CNNFixedPointDataTypes() * boost::unit_test::data::xrange(1, 6), |
| 168 | shape, dt, fixed_point_position) |
| 169 | { |
| 170 | // Compute function |
| 171 | Tensor dst = compute_softmax_layer(shape, dt, fixed_point_position); |
| 172 | |
| 173 | // Compute reference |
| 174 | RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt, fixed_point_position); |
| 175 | |
| 176 | // Validate output |
| 177 | validate(NEAccessor(dst), ref_dst, tolerance_fixed_point); |
| 178 | } |
| 179 | |
| 180 | BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) |
| 181 | BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * CNNFixedPointDataTypes() * boost::unit_test::data::xrange(1, 6), |
| 182 | shape, dt, fixed_point_position) |
| 183 | { |
| 184 | // Compute function |
| 185 | Tensor dst = compute_softmax_layer(shape, dt, fixed_point_position); |
| 186 | |
| 187 | // Compute reference |
| 188 | RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt, fixed_point_position); |
| 189 | |
| 190 | // Validate output |
| 191 | validate(NEAccessor(dst), ref_dst, tolerance_fixed_point); |
| 192 | } |
| 193 | BOOST_AUTO_TEST_SUITE_END() |
| 194 | |
| 195 | BOOST_AUTO_TEST_SUITE_END() |
| 196 | BOOST_AUTO_TEST_SUITE_END() |
| 197 | #endif |