Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +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/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" |
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 */ |
Michele Di Giorgio | 419f33a | 2018-08-27 14:25:24 +0100 | [diff] [blame] | 46 | constexpr RelativeTolerance<float> rel_tolerance_f32(0.05f); /**< Relative tolerance value for comparing reference's output against implementation's output for DataType:F32 */ |
| 47 | constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.0001f); /**< Absolute tolerance value for comparing reference's output against implementation's output for DataType::F32 */ |
| 48 | RelativeTolerance<half_float::half> tolerance_f16(half(0.2)); /**< Relative tolerance value for comparing reference's output against implementation's output for DataType::F16 */ |
| 49 | constexpr float tolerance_num = 0.07f; /**< Tolerance number */ |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 50 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 51 | /** Tolerance for quantized asymmetric operations */ |
| 52 | constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 53 | |
| 54 | /** CNN data types */ |
| 55 | const auto CNNDataTypes = framework::dataset::make("DataType", |
| 56 | { |
| 57 | DataType::F16, |
| 58 | DataType::F32, |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 59 | DataType::QASYMM8, |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame^] | 60 | DataType::QASYMM8_SIGNED, |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 61 | }); |
| 62 | |
| 63 | const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true })); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 64 | |
| 65 | const auto QuantizationData = framework::dataset::make("QuantizationInfo", |
| 66 | { |
| 67 | QuantizationInfo(1.f / 255.f, 10), |
| 68 | QuantizationInfo(1.1f, 10), |
| 69 | }); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 70 | } // namespace |
| 71 | |
| 72 | TEST_SUITE(CL) |
| 73 | TEST_SUITE(FullyConnectedLayer) |
| 74 | |
Michalis Spyrou | 8094325 | 2019-01-10 17:19:50 +0000 | [diff] [blame] | 75 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(datasets::SmallFullyConnectedLayerDataset(), |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 76 | FullyConnectedParameters), |
| 77 | CNNDataTypes), |
| 78 | src_shape, weights_shape, bias_shape, dst_shape, transpose_weights, reshape_weights, data_type) |
| 79 | { |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 80 | const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type; |
| 81 | const QuantizationInfo quantization_info = is_data_type_quantized_asymmetric(data_type) ? QuantizationInfo(2.f / 255.f, 127) : QuantizationInfo(); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 82 | |
| 83 | TensorShape ws(weights_shape); |
| 84 | |
| 85 | // Transpose weights if not done in the function |
| 86 | if(!reshape_weights || !transpose_weights) |
| 87 | { |
| 88 | const size_t shape_x = ws.x(); |
| 89 | ws.set(0, ws.y()); |
| 90 | ws.set(1, shape_x); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 94 | CLTensor src = create_tensor<CLTensor>(src_shape, data_type, 1, quantization_info); |
| 95 | CLTensor weights = create_tensor<CLTensor>(ws, data_type, 1, quantization_info); |
| 96 | CLTensor bias = create_tensor<CLTensor>(bias_shape, bias_data_type, 1, quantization_info); |
| 97 | CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type, 1, quantization_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 98 | |
| 99 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 100 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 101 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 102 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 103 | |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 104 | const QuantizationInfo src_quantization_info = src.info()->quantization_info(); |
| 105 | const QuantizationInfo weights_quantization_info = weights.info()->quantization_info(); |
| 106 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 107 | // Create Fully Connected layer info |
| 108 | FullyConnectedLayerInfo fc_info; |
| 109 | fc_info.transpose_weights = transpose_weights; |
| 110 | fc_info.are_weights_reshaped = !reshape_weights; |
| 111 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 112 | // Create and configure function. |
| 113 | CLFullyConnectedLayer fc; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 114 | fc.configure(&src, &weights, &bias, &dst, fc_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 115 | |
| 116 | // Validate valid region |
| 117 | const ValidRegion dst_valid_region = shape_to_valid_region(dst_shape); |
| 118 | validate(dst.info()->valid_region(), dst_valid_region); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 119 | |
| 120 | // Validate QuantizationInfo |
| 121 | ARM_COMPUTE_EXPECT(src.info()->quantization_info() == src_quantization_info, framework::LogLevel::ERRORS); |
| 122 | ARM_COMPUTE_EXPECT(weights.info()->quantization_info() == weights_quantization_info, framework::LogLevel::ERRORS); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 125 | // *INDENT-OFF* |
| 126 | // clang-format off |
| 127 | DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip( |
| 128 | framework::dataset::make("InputInfo", { TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Mismatching data types |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 129 | TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32), |
| 130 | TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32), |
| 131 | TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Invalid weights dimensions |
| 132 | TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Wrongly reshaped weights |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 133 | }), |
| 134 | framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(315U, 271U), 1, DataType::F16), |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 135 | TensorInfo(TensorShape(192U, 192U), 1, DataType::F32), |
| 136 | TensorInfo(TensorShape(192U, 192U), 1, DataType::F32), |
Michalis Spyrou | d466c2d | 2018-01-30 10:54:39 +0000 | [diff] [blame] | 137 | TensorInfo(TensorShape(217U, 231U), 1, DataType::F32), |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 138 | TensorInfo(TensorShape(217U, 315U), 1, DataType::F32), |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 139 | })), |
| 140 | framework::dataset::make("BiasInfo",{ TensorInfo(TensorShape(271U), 1, DataType::F32), |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 141 | TensorInfo(TensorShape(192U), 1, DataType::F32), |
| 142 | TensorInfo(TensorShape(192U), 1, DataType::F32), |
| 143 | TensorInfo(TensorShape(271U), 1, DataType::F32), |
| 144 | TensorInfo(TensorShape(271U), 1, DataType::F32), |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 145 | })), |
| 146 | framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(271U, 3U), 1, DataType::F32), |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 147 | TensorInfo(TensorShape(192U, 4U), 1, DataType::F32), |
| 148 | TensorInfo(TensorShape(192U, 4U), 1, DataType::F32), |
| 149 | TensorInfo(TensorShape(271U, 3U), 1, DataType::F32), |
| 150 | TensorInfo(TensorShape(271U, 3U), 1, DataType::F32), |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 151 | })), |
Vidhya Sudhan Loganathan | 0fc2545 | 2018-06-18 14:40:56 +0100 | [diff] [blame] | 152 | framework::dataset::make("TransposeWeights",{ true, true, false, true, true })), |
| 153 | framework::dataset::make("ReshapedWeights",{ false, false, false, false, false})), |
| 154 | framework::dataset::make("Expected", { false, true, true, false, false })), |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 155 | input_info, weights_info, bias_info, output_info, transpose_weights, reshaped_weights, expected) |
| 156 | { |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 157 | // Create Fully Connected layer info |
| 158 | FullyConnectedLayerInfo fc_info; |
| 159 | fc_info.transpose_weights = transpose_weights; |
| 160 | fc_info.are_weights_reshaped = reshaped_weights; |
| 161 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 162 | Status status = CLFullyConnectedLayer::validate(&input_info.clone()->set_is_resizable(false), |
| 163 | &weights_info.clone()->set_is_resizable(false), |
| 164 | &bias_info.clone()->set_is_resizable(false), |
| 165 | &output_info.clone()->set_is_resizable(false), |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 166 | fc_info); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 167 | ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS); |
| 168 | } |
| 169 | // clang-format on |
| 170 | // *INDENT-ON* |
| 171 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 172 | template <typename T> |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 173 | using CLFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 174 | |
| 175 | TEST_SUITE(Float) |
| 176 | TEST_SUITE(FP16) |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 177 | FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), |
| 178 | FullyConnectedParameters), |
| 179 | framework::dataset::make("DataType", DataType::F16))) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 180 | { |
| 181 | // Validate output |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 182 | validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 183 | } |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 184 | FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), |
| 185 | FullyConnectedParameters), |
| 186 | framework::dataset::make("DataType", DataType::F16))) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 187 | { |
| 188 | // Validate output |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 189 | validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 190 | } |
| 191 | TEST_SUITE_END() |
| 192 | |
| 193 | TEST_SUITE(FP32) |
| 194 | FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), |
| 195 | framework::dataset::make("DataType", DataType::F32))) |
| 196 | { |
| 197 | // Validate output |
Michele Di Giorgio | 419f33a | 2018-08-27 14:25:24 +0100 | [diff] [blame] | 198 | validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 199 | } |
| 200 | FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters), |
| 201 | framework::dataset::make("DataType", DataType::F32))) |
| 202 | { |
| 203 | // Validate output |
Michele Di Giorgio | 419f33a | 2018-08-27 14:25:24 +0100 | [diff] [blame] | 204 | validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 205 | } |
| 206 | TEST_SUITE_END() |
| 207 | TEST_SUITE_END() |
| 208 | |
| 209 | template <typename T> |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 210 | using CLFullyConnectedLayerQuantizedFixture = FullyConnectedLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>; |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 211 | |
| 212 | TEST_SUITE(Quantized) |
| 213 | TEST_SUITE(QASYMM8) |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 214 | FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, |
| 215 | combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData)) |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 216 | { |
| 217 | // Validate output |
| 218 | validate(CLAccessor(_target), _reference, tolerance_qasymm8); |
| 219 | } |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 220 | FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, |
| 221 | combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData)) |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 222 | { |
| 223 | // Validate output |
| 224 | validate(CLAccessor(_target), _reference, tolerance_qasymm8); |
| 225 | } |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame^] | 226 | TEST_SUITE_END() /* QASYMM8 */ |
| 227 | TEST_SUITE(QASYMM8_SIGNED) |
| 228 | FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT, |
| 229 | combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)), QuantizationData)) |
| 230 | { |
| 231 | // Validate output |
| 232 | validate(CLAccessor(_target), _reference, tolerance_qasymm8); |
| 233 | } |
| 234 | TEST_SUITE_END() /* QASYMM8_SIGNED */ |
| 235 | TEST_SUITE_END() /* Quantized */ |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 236 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 237 | TEST_SUITE_END() |
| 238 | TEST_SUITE_END() |
| 239 | } // namespace validation |
| 240 | } // namespace test |
| 241 | } // namespace arm_compute |