Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +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 "CL/CLAccessor.h" |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 25 | #include "Globals.h" |
| 26 | #include "PaddingCalculator.h" |
| 27 | #include "TensorLibrary.h" |
| 28 | #include "TypePrinter.h" |
| 29 | #include "Utils.h" |
| 30 | #include "validation/Datasets.h" |
| 31 | #include "validation/Reference.h" |
| 32 | #include "validation/Validation.h" |
| 33 | |
| 34 | #include "arm_compute/core/Helpers.h" |
| 35 | #include "arm_compute/core/Types.h" |
| 36 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 37 | #include "arm_compute/runtime/CL/CLTensorAllocator.h" |
| 38 | #include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h" |
| 39 | |
| 40 | #include "boost_wrapper.h" |
| 41 | |
| 42 | #include <random> |
| 43 | #include <string> |
| 44 | |
| 45 | using namespace arm_compute; |
| 46 | using namespace arm_compute::test; |
| 47 | using namespace arm_compute::test::cl; |
| 48 | using namespace arm_compute::test::validation; |
| 49 | |
| 50 | namespace |
| 51 | { |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame^] | 52 | const float tolerance = 0.000001f; /** Tolerance for float operations */ |
| 53 | const float tolerance_qs8 = 2.f; /** Tolerance for QS8 fixed point operations */ |
| 54 | const float tolerance_qs16 = 2.f; /** Tolerance for QS16 fixed point operations */ |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 55 | |
| 56 | /** Compute OpenCL softmax layer function. |
| 57 | * |
| 58 | * @param[in] shape Shape of the input and output tensors. |
| 59 | * @param[in] dt Shape Data type of tensors. |
| 60 | * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of fixed point numbers. |
| 61 | * |
| 62 | * @return Computed output tensor. |
| 63 | */ |
| 64 | CLTensor compute_softmax_layer(const TensorShape &shape, DataType dt, int fixed_point_position = 0) |
| 65 | { |
| 66 | // Create tensors |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 67 | CLTensor src = create_tensor<CLTensor>(shape, dt, 1, fixed_point_position); |
| 68 | CLTensor dst = create_tensor<CLTensor>(shape, dt, 1, fixed_point_position); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 69 | |
| 70 | // Create and configure function |
| 71 | CLSoftmaxLayer smx_layer; |
| 72 | smx_layer.configure(&src, &dst); |
| 73 | |
| 74 | // Allocate tensors |
| 75 | src.allocator()->allocate(); |
| 76 | dst.allocator()->allocate(); |
| 77 | |
| 78 | BOOST_TEST(!src.info()->is_resizable()); |
| 79 | BOOST_TEST(!dst.info()->is_resizable()); |
| 80 | |
| 81 | // Fill tensors |
| 82 | if(arm_compute::is_data_type_float(dt)) |
| 83 | { |
| 84 | std::uniform_real_distribution<> distribution(-1000.f, 1000.f); |
| 85 | library->fill(CLAccessor(src), distribution, 0); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | int one_fixed = 1 << fixed_point_position; |
| 90 | std::uniform_int_distribution<> distribution(-one_fixed, one_fixed); |
| 91 | library->fill(CLAccessor(src), distribution, 0); |
| 92 | } |
| 93 | |
| 94 | // Compute function |
| 95 | smx_layer.run(); |
| 96 | |
| 97 | return dst; |
| 98 | } |
| 99 | } // namespace |
| 100 | |
| 101 | #ifndef DOXYGEN_SKIP_THIS |
| 102 | BOOST_AUTO_TEST_SUITE(CL) |
| 103 | BOOST_AUTO_TEST_SUITE(SoftmaxLayer) |
| 104 | |
| 105 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) |
| 106 | BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * CNNDataTypes(), shape, dt) |
| 107 | { |
| 108 | // Set fixed point position data type allowed |
| 109 | int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0; |
| 110 | |
| 111 | // Create tensors |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 112 | CLTensor src = create_tensor<CLTensor>(shape, dt, 1, fixed_point_position); |
| 113 | CLTensor dst = create_tensor<CLTensor>(shape, dt, 1, fixed_point_position); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 114 | |
| 115 | BOOST_TEST(src.info()->is_resizable()); |
| 116 | BOOST_TEST(dst.info()->is_resizable()); |
| 117 | |
| 118 | // Create and configure function |
| 119 | CLSoftmaxLayer smx_layer; |
| 120 | smx_layer.configure(&src, &dst); |
| 121 | |
| 122 | // Validate valid region |
| 123 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 124 | validate(src.info()->valid_region(), valid_region); |
| 125 | validate(dst.info()->valid_region(), valid_region); |
| 126 | |
| 127 | // Validate padding |
| 128 | const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); |
| 129 | validate(src.info()->padding(), padding); |
| 130 | validate(dst.info()->padding(), padding); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE(Float) |
| 134 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 135 | BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * CNNFloatDataTypes(), shape, dt) |
| 136 | { |
| 137 | // Compute function |
| 138 | CLTensor dst = compute_softmax_layer(shape, dt); |
| 139 | |
| 140 | // Compute reference |
| 141 | RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt); |
| 142 | |
| 143 | // Validate output |
| 144 | validate(CLAccessor(dst), ref_dst, tolerance); |
| 145 | } |
| 146 | |
| 147 | BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) |
| 148 | BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * CNNFloatDataTypes(), shape, dt) |
| 149 | { |
| 150 | // Compute function |
| 151 | CLTensor dst = compute_softmax_layer(shape, dt); |
| 152 | |
| 153 | // Compute reference |
| 154 | RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt); |
| 155 | |
| 156 | // Validate output |
| 157 | validate(CLAccessor(dst), ref_dst, tolerance); |
| 158 | } |
| 159 | BOOST_AUTO_TEST_SUITE_END() |
| 160 | |
| 161 | BOOST_AUTO_TEST_SUITE(Quantized) |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame^] | 162 | BOOST_AUTO_TEST_SUITE(QS8) |
| 163 | // Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5 |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 164 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame^] | 165 | BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * boost::unit_test::data::xrange(1, 6), |
| 166 | shape, fixed_point_position) |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 167 | { |
| 168 | // Compute function |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame^] | 169 | CLTensor dst = compute_softmax_layer(shape, DataType::QS8, fixed_point_position); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 170 | |
| 171 | // Compute reference |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame^] | 172 | RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS8, fixed_point_position); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 173 | |
| 174 | // Validate output |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame^] | 175 | validate(CLAccessor(dst), ref_dst, tolerance_qs8); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame^] | 179 | BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * boost::unit_test::data::xrange(1, 6), |
| 180 | shape, fixed_point_position) |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 181 | { |
| 182 | // Compute function |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame^] | 183 | CLTensor dst = compute_softmax_layer(shape, DataType::QS8, fixed_point_position); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 184 | |
| 185 | // Compute reference |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame^] | 186 | RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS8, fixed_point_position); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 187 | |
| 188 | // Validate output |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame^] | 189 | validate(CLAccessor(dst), ref_dst, tolerance_qs8); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 190 | } |
| 191 | BOOST_AUTO_TEST_SUITE_END() |
| 192 | |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame^] | 193 | BOOST_AUTO_TEST_SUITE(QS16) |
| 194 | // Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14 |
| 195 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 196 | BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * boost::unit_test::data::xrange(1, 14), |
| 197 | shape, fixed_point_position) |
| 198 | { |
| 199 | // Compute function |
| 200 | CLTensor dst = compute_softmax_layer(shape, DataType::QS16, fixed_point_position); |
| 201 | |
| 202 | // Compute reference |
| 203 | RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS16, fixed_point_position); |
| 204 | |
| 205 | // Validate output |
| 206 | validate(CLAccessor(dst), ref_dst, tolerance_qs16); |
| 207 | } |
| 208 | |
| 209 | BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) |
| 210 | BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * boost::unit_test::data::xrange(1, 14), |
| 211 | shape, fixed_point_position) |
| 212 | { |
| 213 | // Compute function |
| 214 | CLTensor dst = compute_softmax_layer(shape, DataType::QS16, fixed_point_position); |
| 215 | |
| 216 | // Compute reference |
| 217 | RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS16, fixed_point_position); |
| 218 | |
| 219 | // Validate output |
| 220 | validate(CLAccessor(dst), ref_dst, tolerance_qs16); |
| 221 | } |
| 222 | BOOST_AUTO_TEST_SUITE_END() |
| 223 | BOOST_AUTO_TEST_SUITE_END() |
| 224 | |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 225 | BOOST_AUTO_TEST_SUITE_END() |
| 226 | BOOST_AUTO_TEST_SUITE_END() |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 227 | #endif /* DOXYGEN_SKIP_THIS */ |