Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 1 | /* |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +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 | */ |
| 24 | #include "arm_compute/core/Types.h" |
| 25 | #include "arm_compute/runtime/NEON/functions/NESoftmaxLayer.h" |
| 26 | #include "arm_compute/runtime/Tensor.h" |
| 27 | #include "arm_compute/runtime/TensorAllocator.h" |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 28 | #include "tests/NEON/Accessor.h" |
| 29 | #include "tests/PaddingCalculator.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 30 | #include "tests/datasets/ShapeDatasets.h" |
| 31 | #include "tests/framework/Asserts.h" |
| 32 | #include "tests/framework/Macros.h" |
| 33 | #include "tests/framework/datasets/Datasets.h" |
| 34 | #include "tests/validation/Validation.h" |
| 35 | #include "tests/validation/fixtures/SoftmaxLayerFixture.h" |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
| 43 | namespace |
| 44 | { |
| 45 | /** Tolerance for float operations */ |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 46 | constexpr AbsoluteTolerance<float> tolerance_f32(0.000001f); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 47 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 48 | constexpr AbsoluteTolerance<float> tolerance_f16(0.0001f); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 49 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/ |
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 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [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 | { |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 59 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 60 | DataType::F16, |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 61 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 62 | DataType::F32, |
| 63 | DataType::QS8, |
| 64 | DataType::QS16, |
| 65 | }); |
| 66 | } // namespace |
| 67 | |
| 68 | TEST_SUITE(NEON) |
| 69 | TEST_SUITE(SoftmaxLayer) |
| 70 | |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 71 | 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] | 72 | { |
| 73 | // Set fixed point position data type allowed |
| 74 | const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; |
| 75 | |
| 76 | // Create tensors |
| 77 | Tensor src = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position); |
| 78 | Tensor dst = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position); |
| 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 | NESoftmaxLayer 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 | |
| 92 | // Validate padding |
| 93 | const int step = 16 / data_size_from_type(data_type); |
| 94 | const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding(); |
| 95 | validate(src.info()->padding(), padding); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame^] | 96 | validate(dst.info()->padding(), PaddingSize()); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 99 | // *INDENT-OFF* |
| 100 | // clang-format off |
| 101 | DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip( |
| 102 | framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types |
| 103 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes |
| 104 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point |
| 105 | TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32), |
| 106 | }), |
| 107 | framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), |
| 108 | TensorInfo(TensorShape(27U, 11U, 2U), 1, DataType::F32), |
| 109 | TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 3), |
| 110 | TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32), |
| 111 | })), |
| 112 | framework::dataset::make("Expected", { false, false, false, true })), |
| 113 | input_info, output_info, expected) |
| 114 | { |
| 115 | bool is_valid = bool(NESoftmaxLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false))); |
| 116 | ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS); |
| 117 | } |
| 118 | // clang-format on |
| 119 | // *INDENT-ON* |
| 120 | |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 121 | template <typename T> |
| 122 | using NESoftmaxLayerFixture = SoftmaxValidationFixture<Tensor, Accessor, NESoftmaxLayer, T>; |
| 123 | |
| 124 | TEST_SUITE(Float) |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 125 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 126 | TEST_SUITE(FP16) |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 127 | FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SoftmaxLayerSmallShapes(), |
| 128 | framework::dataset::make("DataType", DataType::F16)), |
| 129 | framework::dataset::make("Beta", { 1.0f, 2.0f }))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 130 | { |
| 131 | // Validate output |
| 132 | validate(Accessor(_target), _reference, tolerance_f16); |
| 133 | } |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 134 | FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerSmallShapes(), |
| 135 | framework::dataset::make("DataType", DataType::F16)), |
| 136 | framework::dataset::make("Beta", { 1.0f, 2.0f }))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 137 | { |
| 138 | // Validate output |
| 139 | validate(Accessor(_target), _reference, tolerance_f16); |
| 140 | } |
| 141 | TEST_SUITE_END() |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 142 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 143 | |
| 144 | TEST_SUITE(FP32) |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 145 | FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SoftmaxLayerSmallShapes(), |
| 146 | framework::dataset::make("DataType", DataType::F32)), |
| 147 | framework::dataset::make("Beta", { 1.0f, 2.0f }))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 148 | { |
| 149 | // Validate output |
| 150 | validate(Accessor(_target), _reference, tolerance_f32); |
| 151 | } |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 152 | FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(), |
| 153 | framework::dataset::make("DataType", DataType::F32)), |
| 154 | framework::dataset::make("Beta", { 1.0f, 2.0f }))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 155 | { |
| 156 | // Validate output |
| 157 | validate(Accessor(_target), _reference, tolerance_f32); |
| 158 | } |
| 159 | TEST_SUITE_END() |
| 160 | TEST_SUITE_END() |
| 161 | |
| 162 | template <typename T> |
| 163 | using NESoftmaxLayerFixedPointFixture = SoftmaxValidationFixedPointFixture<Tensor, Accessor, NESoftmaxLayer, T>; |
| 164 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame^] | 165 | TEST_SUITE(FixedPoint) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 166 | TEST_SUITE(QS8) |
| 167 | // Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5 |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 168 | FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SoftmaxLayerSmallShapes(), framework::dataset::make("DataType", |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 169 | DataType::QS8)), |
| 170 | framework::dataset::make("FractionalBits", 1, 6))) |
| 171 | { |
| 172 | // Validate output |
| 173 | validate(Accessor(_target), _reference, tolerance_fixed_point); |
| 174 | } |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 175 | FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(), framework::dataset::make("DataType", |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 176 | DataType::QS8)), |
| 177 | framework::dataset::make("FractionalBits", 1, 6))) |
| 178 | { |
| 179 | // Validate output |
| 180 | validate(Accessor(_target), _reference, tolerance_fixed_point); |
| 181 | } |
| 182 | TEST_SUITE_END() |
| 183 | |
| 184 | TEST_SUITE(QS16) |
| 185 | // Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14 |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 186 | FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SoftmaxLayerSmallShapes(), |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 187 | framework::dataset::make("DataType", |
| 188 | DataType::QS16)), |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 189 | framework::dataset::make("FractionalBits", 1, 14))) |
| 190 | { |
| 191 | // Validate output |
| 192 | validate(Accessor(_target), _reference, tolerance_fixed_point); |
| 193 | } |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 194 | FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(), |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 195 | framework::dataset::make("DataType", |
| 196 | DataType::QS16)), |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 197 | framework::dataset::make("FractionalBits", 1, 14))) |
| 198 | { |
| 199 | // Validate output |
| 200 | validate(Accessor(_target), _reference, tolerance_fixed_point); |
| 201 | } |
| 202 | TEST_SUITE_END() |
| 203 | TEST_SUITE_END() |
| 204 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame^] | 205 | template <typename T> |
| 206 | using NESoftmaxLayerQuantizedFixture = SoftmaxValidationQuantizedFixture<Tensor, Accessor, NESoftmaxLayer, T>; |
| 207 | |
| 208 | TEST_SUITE(Quantized) |
| 209 | TEST_SUITE(QASYMM8) |
| 210 | FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(), |
| 211 | framework::dataset::make("DataType", DataType::QASYMM8)), |
| 212 | combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }), |
| 213 | framework::dataset::make("Beta", { 1.0f, 2.0f })))) |
| 214 | { |
| 215 | // Validate output |
| 216 | validate(Accessor(_target), _reference, tolerance_qasymm8); |
| 217 | } |
| 218 | FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(), |
| 219 | framework::dataset::make("DataType", DataType::QASYMM8)), |
| 220 | combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }), |
| 221 | framework::dataset::make("Beta", { 1.0f, 2.0f })))) |
| 222 | { |
| 223 | // Validate output |
| 224 | validate(Accessor(_target), _reference, tolerance_qasymm8); |
| 225 | } |
| 226 | TEST_SUITE_END() |
| 227 | TEST_SUITE_END() |
| 228 | |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 229 | TEST_SUITE_END() |
| 230 | TEST_SUITE_END() |
| 231 | } // namespace validation |
| 232 | } // namespace test |
| 233 | } // namespace arm_compute |