Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +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 "arm_compute/core/Types.h" |
| 25 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 26 | #include "arm_compute/runtime/CL/CLTensorAllocator.h" |
| 27 | #include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h" |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 28 | #include "tests/CL/CLAccessor.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 */ |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame^] | 46 | RelativeTolerance<half> tolerance_f16(half(0.2)); |
| 47 | RelativeTolerance<float> tolerance_f32(0.001f); |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 48 | |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 49 | /** Tolerance for fixed point operations */ |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 50 | constexpr AbsoluteTolerance<int16_t> tolerance_fixed_point(2); |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 51 | |
| 52 | /** CNN data types */ |
| 53 | const auto CNNDataTypes = framework::dataset::make("DataType", |
| 54 | { |
| 55 | DataType::F16, |
| 56 | DataType::F32, |
| 57 | DataType::QS8, |
| 58 | DataType::QS16, |
| 59 | }); |
| 60 | } // namespace |
| 61 | |
| 62 | TEST_SUITE(CL) |
| 63 | TEST_SUITE(SoftmaxLayer) |
| 64 | |
| 65 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), shape, data_type) |
| 66 | { |
| 67 | // Set fixed point position data type allowed |
| 68 | const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; |
| 69 | |
| 70 | // Create tensors |
| 71 | CLTensor src = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position); |
| 72 | CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position); |
| 73 | |
| 74 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 75 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 76 | |
| 77 | // Create and configure function |
| 78 | CLSoftmaxLayer smx_layer; |
| 79 | smx_layer.configure(&src, &dst); |
| 80 | |
| 81 | // Validate valid region |
| 82 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 83 | validate(src.info()->valid_region(), valid_region); |
| 84 | validate(dst.info()->valid_region(), valid_region); |
| 85 | |
| 86 | // Validate padding |
| 87 | const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); |
| 88 | validate(src.info()->padding(), padding); |
| 89 | validate(dst.info()->padding(), padding); |
| 90 | } |
| 91 | |
| 92 | template <typename T> |
| 93 | using CLSoftmaxLayerFixture = SoftmaxValidationFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>; |
| 94 | |
| 95 | TEST_SUITE(Float) |
| 96 | TEST_SUITE(FP16) |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame^] | 97 | FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 98 | { |
| 99 | // Validate output |
| 100 | validate(CLAccessor(_target), _reference, tolerance_f16); |
| 101 | } |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame^] | 102 | FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F16))) |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 103 | { |
| 104 | // Validate output |
| 105 | validate(CLAccessor(_target), _reference, tolerance_f16); |
| 106 | } |
| 107 | TEST_SUITE_END() |
| 108 | |
| 109 | TEST_SUITE(FP32) |
| 110 | FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32))) |
| 111 | { |
| 112 | // Validate output |
| 113 | validate(CLAccessor(_target), _reference, tolerance_f32); |
| 114 | } |
| 115 | FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F32))) |
| 116 | { |
| 117 | // Validate output |
| 118 | validate(CLAccessor(_target), _reference, tolerance_f32); |
| 119 | } |
| 120 | TEST_SUITE_END() |
| 121 | TEST_SUITE_END() |
| 122 | |
| 123 | template <typename T> |
| 124 | using CLSoftmaxLayerFixedPointFixture = SoftmaxValidationFixedPointFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>; |
| 125 | |
| 126 | TEST_SUITE(Quantized) |
| 127 | TEST_SUITE(QS8) |
| 128 | // Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5 |
| 129 | FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", |
| 130 | DataType::QS8)), |
| 131 | framework::dataset::make("FractionalBits", 1, 6))) |
| 132 | { |
| 133 | // Validate output |
| 134 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 135 | } |
| 136 | FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", |
| 137 | DataType::QS8)), |
| 138 | framework::dataset::make("FractionalBits", 1, 6))) |
| 139 | { |
| 140 | // Validate output |
| 141 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 142 | } |
| 143 | TEST_SUITE_END() |
| 144 | |
| 145 | TEST_SUITE(QS16) |
| 146 | // Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14 |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 147 | FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), |
| 148 | framework::dataset::make("DataType", |
| 149 | DataType::QS16)), |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 150 | framework::dataset::make("FractionalBits", 1, 14))) |
| 151 | { |
| 152 | // Validate output |
| 153 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 154 | } |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 155 | FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), |
| 156 | framework::dataset::make("DataType", |
| 157 | DataType::QS16)), |
Moritz Pflanzer | f6ad98a | 2017-07-21 17:19:58 +0100 | [diff] [blame] | 158 | framework::dataset::make("FractionalBits", 1, 14))) |
| 159 | { |
| 160 | // Validate output |
| 161 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 162 | } |
| 163 | TEST_SUITE_END() |
| 164 | TEST_SUITE_END() |
| 165 | |
| 166 | TEST_SUITE_END() |
| 167 | TEST_SUITE_END() |
| 168 | } // namespace validation |
| 169 | } // namespace test |
| 170 | } // namespace arm_compute |