Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | 1c0d0ff | 2018-01-31 13:05:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 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 | */ |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 24 | #include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h" |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 25 | #include "arm_compute/core/Types.h" |
| 26 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 27 | #include "arm_compute/runtime/CL/CLTensorAllocator.h" |
| 28 | #include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h" |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 29 | #include "tests/CL/CLAccessor.h" |
| 30 | #include "tests/PaddingCalculator.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 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/SoftmaxLayerFixture.h" |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace validation |
| 43 | { |
| 44 | namespace |
| 45 | { |
| 46 | /** Tolerance for float operations */ |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 47 | RelativeTolerance<half> tolerance_f16(half(0.2)); |
| 48 | RelativeTolerance<float> tolerance_f32(0.001f); |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 49 | |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 50 | /** Tolerance for fixed point operations */ |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 51 | constexpr AbsoluteTolerance<int16_t> tolerance_fixed_point(2); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 52 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 53 | /** Tolerance for quantized operations */ |
| 54 | constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1); |
| 55 | |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 56 | /** CNN data types */ |
| 57 | const auto CNNDataTypes = framework::dataset::make("DataType", |
| 58 | { |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 59 | DataType::QASYMM8, |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 60 | DataType::F16, |
| 61 | DataType::F32, |
| 62 | DataType::QS8, |
| 63 | DataType::QS16, |
| 64 | }); |
| 65 | } // namespace |
| 66 | |
| 67 | TEST_SUITE(CL) |
| 68 | TEST_SUITE(SoftmaxLayer) |
| 69 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 70 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(concat(datasets::SoftmaxLayerSmallShapes(), datasets::SoftmaxLayerLargeShapes()), CNNDataTypes), shape, data_type) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 71 | { |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 72 | // Set fixed point position and quantization info if is allowed |
| 73 | const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; |
| 74 | const QuantizationInfo quantization_info = is_data_type_quantized_asymmetric(data_type) ? QuantizationInfo(1.f / 255.f, 0) : QuantizationInfo(); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 75 | |
| 76 | // Create tensors |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 77 | CLTensor src = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position, quantization_info); |
| 78 | CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position, QuantizationInfo(1.f / 256.f, 0)); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 79 | |
| 80 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 81 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 82 | |
| 83 | // Create and configure function |
| 84 | CLSoftmaxLayer smx_layer; |
| 85 | smx_layer.configure(&src, &dst); |
| 86 | |
| 87 | // Validate valid region |
| 88 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 89 | validate(src.info()->valid_region(), valid_region); |
| 90 | validate(dst.info()->valid_region(), valid_region); |
| 91 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 92 | // Get reduction kernel info |
| 93 | CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo reduction_info = CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(shape.x()); |
| 94 | |
| 95 | // Validate src padding |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame^] | 96 | const PaddingSize padding_src = PaddingCalculator(shape.x(), std::get<1>(reduction_info)).required_padding(); |
| 97 | validate(src.info()->padding(), padding_src); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 98 | |
| 99 | // Validate dst padding |
| 100 | const PaddingSize padding_dst = PaddingCalculator(shape.x(), 16).required_padding(); |
| 101 | validate(dst.info()->padding(), padding_dst); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 104 | // *INDENT-OFF* |
| 105 | // clang-format off |
| 106 | DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip( |
| 107 | framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 108 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes |
| 109 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point |
| 110 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8, // Invalid output quantization info |
| 111 | QuantizationInfo(1.f/256, 12)), |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 112 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink |
| 113 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 114 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 3), |
| 115 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8, |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 116 | QuantizationInfo(1.f/256, 12)), |
| 117 | }), |
| 118 | framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), |
| 119 | TensorInfo(TensorShape(27U, 11U, 2U), 1, DataType::F32), |
| 120 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 3), |
| 121 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8, |
| 122 | QuantizationInfo(1.f/256, 12)), |
| 123 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 124 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), |
| 125 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 3), |
| 126 | TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8, |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 127 | QuantizationInfo(1.f/256, 0)), |
| 128 | })), |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 129 | framework::dataset::make("Expected", { false, false, false, false, false, true, true, true })), |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 130 | input_info, output_info, expected) |
| 131 | { |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 132 | ARM_COMPUTE_EXPECT(bool(CLSoftmaxLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false))) == expected, framework::LogLevel::ERRORS); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 133 | } |
| 134 | // clang-format on |
| 135 | // *INDENT-ON* |
| 136 | |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 137 | template <typename T> |
| 138 | using CLSoftmaxLayerFixture = SoftmaxValidationFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>; |
| 139 | |
| 140 | TEST_SUITE(Float) |
| 141 | TEST_SUITE(FP16) |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 142 | FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(), |
| 143 | framework::dataset::make("DataType", DataType::F16)), |
| 144 | framework::dataset::make("Beta", { 1.0f, 2.0f }))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 145 | { |
| 146 | // Validate output |
| 147 | validate(CLAccessor(_target), _reference, tolerance_f16); |
| 148 | } |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 149 | FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(), |
| 150 | framework::dataset::make("DataType", DataType::F16)), |
| 151 | framework::dataset::make("Beta", { 1.0f, 2.0f }))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 152 | { |
| 153 | // Validate output |
| 154 | validate(CLAccessor(_target), _reference, tolerance_f16); |
| 155 | } |
| 156 | TEST_SUITE_END() |
| 157 | |
| 158 | TEST_SUITE(FP32) |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 159 | FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(), |
| 160 | framework::dataset::make("DataType", DataType::F32)), |
| 161 | framework::dataset::make("Beta", { 1.0f, 2.0f }))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 162 | { |
| 163 | // Validate output |
| 164 | validate(CLAccessor(_target), _reference, tolerance_f32); |
| 165 | } |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 166 | FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(), |
| 167 | framework::dataset::make("DataType", DataType::F32)), |
| 168 | framework::dataset::make("Beta", { 1.0f, 2.0f }))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 169 | { |
| 170 | // Validate output |
| 171 | validate(CLAccessor(_target), _reference, tolerance_f32); |
| 172 | } |
| 173 | TEST_SUITE_END() |
| 174 | TEST_SUITE_END() |
| 175 | |
| 176 | template <typename T> |
| 177 | using CLSoftmaxLayerFixedPointFixture = SoftmaxValidationFixedPointFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>; |
| 178 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 179 | TEST_SUITE(FixedPoint) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 180 | TEST_SUITE(QS8) |
| 181 | // Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5 |
Anthony Barbier | 1c0d0ff | 2018-01-31 13:05:09 +0000 | [diff] [blame] | 182 | FIXTURE_DATA_TEST_CASE(RunTiny, CLSoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerTinyShapes(), framework::dataset::make("DataType", |
| 183 | DataType::QS8)), |
| 184 | framework::dataset::make("FractionalBits", 1, 6))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 185 | { |
| 186 | // Validate output |
| 187 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 188 | } |
Anthony Barbier | 1c0d0ff | 2018-01-31 13:05:09 +0000 | [diff] [blame] | 189 | FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerSmallShapes(), framework::dataset::make("DataType", |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 190 | DataType::QS8)), |
| 191 | framework::dataset::make("FractionalBits", 1, 6))) |
| 192 | { |
| 193 | // Validate output |
| 194 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 195 | } |
| 196 | TEST_SUITE_END() |
| 197 | |
| 198 | TEST_SUITE(QS16) |
| 199 | // Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14 |
Anthony Barbier | 1c0d0ff | 2018-01-31 13:05:09 +0000 | [diff] [blame] | 200 | FIXTURE_DATA_TEST_CASE(RunTiny, CLSoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerTinyShapes(), |
| 201 | framework::dataset::make("DataType", |
| 202 | DataType::QS16)), |
| 203 | framework::dataset::make("FractionalBits", 1, 14))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 204 | { |
| 205 | // Validate output |
| 206 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 207 | } |
Anthony Barbier | 1c0d0ff | 2018-01-31 13:05:09 +0000 | [diff] [blame] | 208 | FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerSmallShapes(), |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 209 | framework::dataset::make("DataType", |
| 210 | DataType::QS16)), |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 211 | framework::dataset::make("FractionalBits", 1, 14))) |
| 212 | { |
| 213 | // Validate output |
| 214 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 215 | } |
| 216 | TEST_SUITE_END() |
| 217 | TEST_SUITE_END() |
| 218 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 219 | template <typename T> |
| 220 | using CLSoftmaxLayerQuantizedFixture = SoftmaxValidationQuantizedFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>; |
| 221 | |
| 222 | TEST_SUITE(Quantized) |
| 223 | TEST_SUITE(QASYMM8) |
| 224 | FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(), |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 225 | framework::dataset::make("DataType", DataType::QASYMM8)), |
| 226 | combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }), |
| 227 | framework::dataset::make("Beta", { 1.0f, 2.f })))) |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 228 | { |
| 229 | // Validate output |
| 230 | validate(CLAccessor(_target), _reference, tolerance_qasymm8); |
| 231 | } |
| 232 | FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(), |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 233 | framework::dataset::make("DataType", DataType::QASYMM8)), |
| 234 | combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }), |
| 235 | framework::dataset::make("Beta", { 1.0f, 2.0f })))) |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 236 | { |
| 237 | // Validate output |
| 238 | validate(CLAccessor(_target), _reference, tolerance_qasymm8); |
| 239 | } |
| 240 | TEST_SUITE_END() |
| 241 | TEST_SUITE_END() |
| 242 | |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 243 | TEST_SUITE_END() |
| 244 | TEST_SUITE_END() |
| 245 | } // namespace validation |
| 246 | } // namespace test |
| 247 | } // namespace arm_compute |