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/CL/CLTensor.h" |
| 26 | #include "arm_compute/runtime/CL/CLTensorAllocator.h" |
| 27 | #include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h" |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +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/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" |
| 36 | #include "tests/validation/half.h" |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +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 */ |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame^] | 47 | RelativeTolerance<float> tolerance_f32(0.001f); |
| 48 | RelativeTolerance<half_float::half> tolerance_f16(half_float::half(0.2)); |
| 49 | constexpr float tolerance_num = 0.07f; /**< Tolerance number */ |
| 50 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 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 | DataType::F16, |
| 58 | DataType::F32, |
| 59 | DataType::QS8, |
| 60 | DataType::QS16, |
| 61 | }); |
| 62 | |
| 63 | const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true })); |
| 64 | } // namespace |
| 65 | |
| 66 | TEST_SUITE(CL) |
| 67 | TEST_SUITE(FullyConnectedLayer) |
| 68 | |
| 69 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(framework::dataset::concat(datasets::SmallFullyConnectedLayerDataset(), datasets::LargeFullyConnectedLayerDataset()), |
| 70 | FullyConnectedParameters), |
| 71 | CNNDataTypes), |
| 72 | src_shape, weights_shape, bias_shape, dst_shape, transpose_weights, reshape_weights, data_type) |
| 73 | { |
| 74 | // Set fixed point position data type allowed |
| 75 | int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; |
| 76 | |
| 77 | TensorShape ws(weights_shape); |
| 78 | |
| 79 | // Transpose weights if not done in the function |
| 80 | if(!reshape_weights || !transpose_weights) |
| 81 | { |
| 82 | const size_t shape_x = ws.x(); |
| 83 | ws.set(0, ws.y()); |
| 84 | ws.set(1, shape_x); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Create tensors |
| 88 | CLTensor src = create_tensor<CLTensor>(src_shape, data_type, 1, fixed_point_position); |
| 89 | CLTensor weights = create_tensor<CLTensor>(ws, data_type, 1, fixed_point_position); |
| 90 | CLTensor bias = create_tensor<CLTensor>(bias_shape, data_type, 1, fixed_point_position); |
| 91 | CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type, 1, fixed_point_position); |
| 92 | |
| 93 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 94 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 95 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 96 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 97 | |
| 98 | // Create and configure function. |
| 99 | CLFullyConnectedLayer fc; |
| 100 | fc.configure(&src, &weights, &bias, &dst, transpose_weights, !reshape_weights); |
| 101 | |
| 102 | // Validate valid region |
| 103 | const ValidRegion dst_valid_region = shape_to_valid_region(dst_shape); |
| 104 | validate(dst.info()->valid_region(), dst_valid_region); |
| 105 | } |
| 106 | |
| 107 | template <typename T> |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 108 | using CLFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, false>; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 109 | |
| 110 | TEST_SUITE(Float) |
| 111 | TEST_SUITE(FP16) |
| 112 | FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<half_float::half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), |
| 113 | FullyConnectedParameters), |
| 114 | framework::dataset::make("DataType", DataType::F16))) |
| 115 | { |
| 116 | // Validate output |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame^] | 117 | validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 118 | } |
| 119 | FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<half_float::half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), |
| 120 | FullyConnectedParameters), |
| 121 | framework::dataset::make("DataType", DataType::F16))) |
| 122 | { |
| 123 | // Validate output |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame^] | 124 | validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 125 | } |
| 126 | TEST_SUITE_END() |
| 127 | |
| 128 | TEST_SUITE(FP32) |
| 129 | FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), |
| 130 | framework::dataset::make("DataType", DataType::F32))) |
| 131 | { |
| 132 | // Validate output |
| 133 | validate(CLAccessor(_target), _reference, tolerance_f32); |
| 134 | } |
| 135 | FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters), |
| 136 | framework::dataset::make("DataType", DataType::F32))) |
| 137 | { |
| 138 | // Validate output |
| 139 | validate(CLAccessor(_target), _reference, tolerance_f32); |
| 140 | } |
| 141 | TEST_SUITE_END() |
| 142 | TEST_SUITE_END() |
| 143 | |
| 144 | template <typename T> |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 145 | using CLFullyConnectedLayerFixedPointFixture = FullyConnectedLayerValidationFixedPointFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, false>; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 146 | |
| 147 | TEST_SUITE(Quantized) |
| 148 | TEST_SUITE(QS8) |
| 149 | // Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5 |
| 150 | FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), |
| 151 | FullyConnectedParameters), |
| 152 | framework::dataset::make("DataType", |
| 153 | DataType::QS8)), |
| 154 | framework::dataset::make("FractionalBits", 1, 6))) |
| 155 | { |
| 156 | // Validate output |
| 157 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 158 | } |
| 159 | FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), |
| 160 | FullyConnectedParameters), |
| 161 | framework::dataset::make("DataType", |
| 162 | DataType::QS8)), |
| 163 | framework::dataset::make("FractionalBits", 1, 6))) |
| 164 | { |
| 165 | // Validate output |
| 166 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 167 | } |
| 168 | TEST_SUITE_END() |
| 169 | |
| 170 | TEST_SUITE(QS16) |
| 171 | // Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14 |
| 172 | FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), |
| 173 | FullyConnectedParameters), |
| 174 | framework::dataset::make("DataType", |
| 175 | DataType::QS16)), |
| 176 | framework::dataset::make("FractionalBits", 1, 14))) |
| 177 | { |
| 178 | // Validate output |
| 179 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 180 | } |
| 181 | FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), |
| 182 | FullyConnectedParameters), |
| 183 | framework::dataset::make("DataType", |
| 184 | DataType::QS16)), |
| 185 | framework::dataset::make("FractionalBits", 1, 14))) |
| 186 | { |
| 187 | // Validate output |
| 188 | validate(CLAccessor(_target), _reference, tolerance_fixed_point); |
| 189 | } |
| 190 | TEST_SUITE_END() |
| 191 | TEST_SUITE_END() |
| 192 | |
| 193 | TEST_SUITE_END() |
| 194 | TEST_SUITE_END() |
| 195 | } // namespace validation |
| 196 | } // namespace test |
| 197 | } // namespace arm_compute |