Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +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/NEON/functions/NEFullyConnectedLayer.h" |
| 26 | #include "arm_compute/runtime/Tensor.h" |
| 27 | #include "arm_compute/runtime/TensorAllocator.h" |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +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/FullyConnectedLayerDataset.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/FullyConnectedLayerFixture.h" |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +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 | ff1c360 | 2017-09-22 12:41:25 +0100 | [diff] [blame] | 46 | constexpr RelativeTolerance<float> tolerance_f32(0.01f); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 47 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | ff1c360 | 2017-09-22 12:41:25 +0100 | [diff] [blame] | 48 | constexpr RelativeTolerance<float> tolerance_f16(0.01f); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 49 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/ |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 50 | /** Tolerance for fixed point operations */ |
| 51 | constexpr AbsoluteTolerance<float> tolerance_fixed_point(1.f); |
| 52 | |
| 53 | /** CNN data types */ |
| 54 | const auto CNNDataTypes = framework::dataset::make("DataType", |
| 55 | { |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 56 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 57 | DataType::F16, |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 58 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 59 | DataType::F32, |
| 60 | DataType::QS8, |
| 61 | DataType::QS16, |
| 62 | }); |
| 63 | |
| 64 | const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true })); |
| 65 | } // namespace |
| 66 | |
| 67 | TEST_SUITE(NEON) |
| 68 | TEST_SUITE(FullyConnectedLayer) |
| 69 | |
| 70 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(framework::dataset::concat(datasets::SmallFullyConnectedLayerDataset(), datasets::LargeFullyConnectedLayerDataset()), |
| 71 | FullyConnectedParameters), |
| 72 | CNNDataTypes), |
| 73 | src_shape, weights_shape, bias_shape, dst_shape, transpose_weights, reshape_weights, data_type) |
| 74 | { |
| 75 | // Set fixed point position data type allowed |
| 76 | int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; |
| 77 | |
| 78 | TensorShape ws(weights_shape); |
| 79 | |
| 80 | // Transpose weights if not done in the function |
| 81 | if(!reshape_weights || !transpose_weights) |
| 82 | { |
| 83 | const size_t shape_x = ws.x(); |
| 84 | ws.set(0, ws.y()); |
| 85 | ws.set(1, shape_x); |
| 86 | |
| 87 | // Weights have to be passed reshaped |
| 88 | // Transpose 1xW for batched version |
| 89 | if(!reshape_weights && dst_shape.y() > 1) |
| 90 | { |
| 91 | const float transpose_width = 16.0f / data_size_from_type(data_type); |
| 92 | const size_t shape_x = ws.x(); |
| 93 | ws.set(0, ws.y() * static_cast<unsigned int>(transpose_width)); |
| 94 | ws.set(1, static_cast<unsigned int>(std::ceil(shape_x / transpose_width))); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Create tensors |
| 99 | Tensor src = create_tensor<Tensor>(src_shape, data_type, 1, fixed_point_position); |
| 100 | Tensor weights = create_tensor<Tensor>(ws, data_type, 1, fixed_point_position); |
| 101 | Tensor bias = create_tensor<Tensor>(bias_shape, data_type, 1, fixed_point_position); |
| 102 | Tensor dst = create_tensor<Tensor>(dst_shape, data_type, 1, fixed_point_position); |
| 103 | |
| 104 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 105 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 106 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 107 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 108 | |
| 109 | // Create and configure function. |
| 110 | NEFullyConnectedLayer fc; |
| 111 | fc.configure(&src, &weights, &bias, &dst, transpose_weights, !reshape_weights); |
| 112 | |
| 113 | // Validate valid region |
| 114 | const ValidRegion dst_valid_region = shape_to_valid_region(dst_shape); |
| 115 | validate(dst.info()->valid_region(), dst_valid_region); |
| 116 | } |
| 117 | |
| 118 | template <typename T> |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 119 | using NEFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<Tensor, Accessor, NEFullyConnectedLayer, T, true>; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 120 | |
| 121 | TEST_SUITE(Float) |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 122 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 123 | TEST_SUITE(FP16) |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 124 | FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), |
| 125 | FullyConnectedParameters), |
| 126 | framework::dataset::make("DataType", DataType::F16))) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 127 | { |
| 128 | // Validate output |
| 129 | validate(Accessor(_target), _reference, tolerance_f16); |
| 130 | } |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 131 | FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), |
| 132 | FullyConnectedParameters), |
| 133 | framework::dataset::make("DataType", DataType::F16))) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 134 | { |
| 135 | // Validate output |
| 136 | validate(Accessor(_target), _reference, tolerance_f16); |
| 137 | } |
| 138 | TEST_SUITE_END() |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 139 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 140 | |
| 141 | TEST_SUITE(FP32) |
| 142 | FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), |
| 143 | framework::dataset::make("DataType", DataType::F32))) |
| 144 | { |
| 145 | // Validate output |
| 146 | validate(Accessor(_target), _reference, tolerance_f32); |
| 147 | } |
| 148 | FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters), |
| 149 | framework::dataset::make("DataType", DataType::F32))) |
| 150 | { |
| 151 | // Validate output |
| 152 | validate(Accessor(_target), _reference, tolerance_f32); |
| 153 | } |
| 154 | TEST_SUITE_END() |
| 155 | TEST_SUITE_END() |
| 156 | |
| 157 | template <typename T> |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 158 | using NEFullyConnectedLayerFixedPointFixture = FullyConnectedLayerValidationFixedPointFixture<Tensor, Accessor, NEFullyConnectedLayer, T, true>; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 159 | |
| 160 | TEST_SUITE(Quantized) |
| 161 | TEST_SUITE(QS8) |
| 162 | // Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5 |
| 163 | FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), |
| 164 | FullyConnectedParameters), |
| 165 | framework::dataset::make("DataType", |
| 166 | DataType::QS8)), |
| 167 | framework::dataset::make("FractionalBits", 1, 6))) |
| 168 | { |
| 169 | // Validate output |
| 170 | validate(Accessor(_target), _reference, tolerance_fixed_point); |
| 171 | } |
| 172 | FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), |
| 173 | FullyConnectedParameters), |
| 174 | framework::dataset::make("DataType", |
| 175 | DataType::QS8)), |
| 176 | framework::dataset::make("FractionalBits", 1, 6))) |
| 177 | { |
| 178 | // Validate output |
| 179 | validate(Accessor(_target), _reference, tolerance_fixed_point); |
| 180 | } |
| 181 | TEST_SUITE_END() |
| 182 | |
| 183 | TEST_SUITE(QS16) |
| 184 | // Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14 |
| 185 | FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), |
| 186 | FullyConnectedParameters), |
| 187 | framework::dataset::make("DataType", |
| 188 | DataType::QS16)), |
| 189 | framework::dataset::make("FractionalBits", 1, 14))) |
| 190 | { |
| 191 | // Validate output |
| 192 | validate(Accessor(_target), _reference, tolerance_fixed_point); |
| 193 | } |
| 194 | FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), |
| 195 | FullyConnectedParameters), |
| 196 | framework::dataset::make("DataType", |
| 197 | DataType::QS16)), |
| 198 | framework::dataset::make("FractionalBits", 1, 14))) |
| 199 | { |
| 200 | // Validate output |
| 201 | validate(Accessor(_target), _reference, tolerance_fixed_point); |
| 202 | } |
| 203 | TEST_SUITE_END() |
| 204 | TEST_SUITE_END() |
| 205 | |
| 206 | TEST_SUITE_END() |
| 207 | TEST_SUITE_END() |
| 208 | } // namespace validation |
| 209 | } // namespace test |
| 210 | } // namespace arm_compute |