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