Kaizen | 8938bd3 | 2017-09-28 14:38:23 +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 CONCLCTION 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/CLConvolutionLayer.h" |
| 28 | #include "tests/CL/CLAccessor.h" |
| 29 | #include "tests/PaddingCalculator.h" |
| 30 | #include "tests/datasets/LargeConvolutionLayerDataset.h" |
| 31 | #include "tests/datasets/SmallConvolutionLayerDataset.h" |
| 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Macros.h" |
| 34 | #include "tests/framework/datasets/Datasets.h" |
| 35 | #include "tests/validation/Validation.h" |
| 36 | #include "tests/validation/fixtures/ConvolutionLayerFixture.h" |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace validation |
| 43 | { |
| 44 | namespace |
| 45 | { |
| 46 | RelativeTolerance<float> tolerance_f32(0.05f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ |
| 47 | RelativeTolerance<half_float::half> tolerance_f16(half_float::half(0.2)); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */ |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 48 | constexpr AbsoluteTolerance<float> tolerance_fixed(1.0f); /**< Tolerance value for comparing reference's output against implementation's output for fixed point data types */ |
| 49 | constexpr AbsoluteTolerance<float> tolerance_qasymm8(0.0); /**< Tolerance value for comparing reference's output against implementation's output for quantized data types */ |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 50 | constexpr float tolerance_num = 0.07f; /**< Tolerance number */ |
| 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, |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 59 | DataType::QASYMM8, |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 60 | }); |
| 61 | } // namespace |
| 62 | |
| 63 | TEST_SUITE(CL) |
| 64 | TEST_SUITE(ConvolutionLayer) |
| 65 | |
| 66 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(framework::dataset::concat(datasets::SmallConvolutionLayerDataset(), datasets::LargeConvolutionLayerDataset()), CNNDataTypes), |
| 67 | input_shape, weights_shape, bias_shape, output_shape, info, data_type) |
| 68 | { |
| 69 | // Set fixed point position data type allowed |
| 70 | int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; |
| 71 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 72 | auto bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type; |
| 73 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 74 | // Create tensors |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 75 | CLTensor src = create_tensor<CLTensor>(input_shape, data_type, 1, fixed_point_position, QuantizationInfo(2.f / 255.f, 127)); |
| 76 | CLTensor weights = create_tensor<CLTensor>(weights_shape, data_type, 1, fixed_point_position, QuantizationInfo(2.f / 255.f, 127)); |
| 77 | CLTensor bias = create_tensor<CLTensor>(bias_shape, bias_data_type, 1, fixed_point_position, QuantizationInfo(2.f / 255.f, 127)); |
| 78 | CLTensor dst = create_tensor<CLTensor>(output_shape, data_type, 1, fixed_point_position, QuantizationInfo(2.f / 255.f, 127)); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 79 | |
| 80 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 81 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 82 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 83 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 84 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 85 | const QuantizationInfo src_quantization_info = src.info()->quantization_info(); |
| 86 | const QuantizationInfo weights_quantization_info = weights.info()->quantization_info(); |
| 87 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 88 | // Create and configure function |
| 89 | CLConvolutionLayer conv; |
| 90 | conv.configure(&src, &weights, &bias, &dst, info); |
| 91 | |
| 92 | // Validate valid region |
| 93 | const ValidRegion src_valid_region = shape_to_valid_region(input_shape); |
| 94 | const ValidRegion weights_valid_region = shape_to_valid_region(weights_shape); |
| 95 | const ValidRegion bias_valid_region = shape_to_valid_region(bias_shape); |
| 96 | const ValidRegion dst_valid_region = shape_to_valid_region(output_shape); |
| 97 | |
| 98 | validate(src.info()->valid_region(), src_valid_region); |
| 99 | validate(weights.info()->valid_region(), weights_valid_region); |
| 100 | validate(bias.info()->valid_region(), bias_valid_region); |
| 101 | validate(dst.info()->valid_region(), dst_valid_region); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 102 | |
| 103 | // Validate QuantizationInfo |
| 104 | ARM_COMPUTE_EXPECT(src.info()->quantization_info() == src_quantization_info, framework::LogLevel::ERRORS); |
| 105 | ARM_COMPUTE_EXPECT(weights.info()->quantization_info() == weights_quantization_info, framework::LogLevel::ERRORS); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | template <typename T> |
| 109 | using CLConvolutionLayerFixture = ConvolutionValidationFixture<CLTensor, CLAccessor, CLConvolutionLayer, T>; |
| 110 | |
| 111 | TEST_SUITE(Float) |
| 112 | TEST_SUITE(FP16) |
| 113 | FIXTURE_DATA_TEST_CASE(RunSmall, CLConvolutionLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallConvolutionLayerDataset(), |
| 114 | framework::dataset::make("ReshapeWeights", { true, false })), |
| 115 | framework::dataset::make("DataType", |
| 116 | DataType::F16))) |
| 117 | { |
| 118 | // Validate output |
| 119 | validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num); |
| 120 | } |
| 121 | FIXTURE_DATA_TEST_CASE(RunLarge, CLConvolutionLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeConvolutionLayerDataset(), |
| 122 | framework::dataset::make("ReshapeWeights", { true, false })), |
| 123 | framework::dataset::make("DataType", |
| 124 | DataType::F16))) |
| 125 | { |
| 126 | // Validate output |
| 127 | validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num); |
| 128 | } |
| 129 | TEST_SUITE_END() |
| 130 | |
| 131 | TEST_SUITE(FP32) |
| 132 | FIXTURE_DATA_TEST_CASE(RunSmall, CLConvolutionLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallConvolutionLayerDataset(), |
| 133 | framework::dataset::make("ReshapeWeights", { true, false })), |
| 134 | framework::dataset::make("DataType", |
| 135 | DataType::F32))) |
| 136 | { |
| 137 | // Validate output |
| 138 | validate(CLAccessor(_target), _reference, tolerance_f32); |
| 139 | } |
| 140 | FIXTURE_DATA_TEST_CASE(RunLarge, CLConvolutionLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeConvolutionLayerDataset(), |
| 141 | framework::dataset::make("ReshapeWeights", { true, false })), |
| 142 | framework::dataset::make("DataType", |
| 143 | DataType::F32))) |
| 144 | { |
| 145 | // Validate output |
| 146 | validate(CLAccessor(_target), _reference, tolerance_f32); |
| 147 | } |
| 148 | TEST_SUITE_END() |
| 149 | TEST_SUITE_END() |
| 150 | |
| 151 | template <typename T> |
| 152 | using CLConvolutionLayerFixedPointFixture = ConvolutionValidationFixedPointFixture<CLTensor, CLAccessor, CLConvolutionLayer, T>; |
| 153 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 154 | TEST_SUITE(FixedPoint) |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 155 | TEST_SUITE(QS8) |
| 156 | // We test for fixed point precision [4,6] |
| 157 | FIXTURE_DATA_TEST_CASE(RunSmall, CLConvolutionLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallConvolutionLayerDataset(), |
| 158 | framework::dataset::make("ReshapeWeights", { true, false })), |
| 159 | framework::dataset::make("DataType", |
| 160 | DataType::QS8)), |
| 161 | framework::dataset::make("FractionalBits", 4, 7))) |
| 162 | { |
| 163 | // Validate output |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 164 | validate(CLAccessor(_target), _reference, tolerance_fixed); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 165 | } |
| 166 | FIXTURE_DATA_TEST_CASE(RunLarge, CLConvolutionLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeConvolutionLayerDataset(), |
| 167 | framework::dataset::make("ReshapeWeights", { true, false })), |
| 168 | framework::dataset::make("DataType", |
| 169 | DataType::QS8)), |
| 170 | framework::dataset::make("FractionalBits", 4, 7))) |
| 171 | { |
| 172 | // Validate output |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 173 | validate(CLAccessor(_target), _reference, tolerance_fixed); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 174 | } |
| 175 | TEST_SUITE_END() |
| 176 | |
| 177 | TEST_SUITE(QS16) |
| 178 | // Testing for fixed point position [1,14) |
| 179 | FIXTURE_DATA_TEST_CASE(RunSmall, CLConvolutionLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallConvolutionLayerDataset(), |
| 180 | framework::dataset::make("ReshapeWeights", { true, false })), |
| 181 | framework::dataset::make("DataType", |
| 182 | DataType::QS16)), |
| 183 | framework::dataset::make("FractionalBits", 1, 14))) |
| 184 | { |
| 185 | // Validate output |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 186 | validate(CLAccessor(_target), _reference, tolerance_fixed); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 187 | } |
| 188 | FIXTURE_DATA_TEST_CASE(RunLarge, CLConvolutionLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeConvolutionLayerDataset(), |
| 189 | framework::dataset::make("ReshapeWeights", { true, false })), |
| 190 | framework::dataset::make("DataType", |
| 191 | DataType::QS16)), |
| 192 | framework::dataset::make("FractionalBits", 1, 14))) |
| 193 | { |
| 194 | // Validate output |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 195 | validate(CLAccessor(_target), _reference, tolerance_fixed); |
| 196 | } |
| 197 | TEST_SUITE_END() |
| 198 | TEST_SUITE_END() |
| 199 | |
| 200 | template <typename T> |
| 201 | using CLConvolutionLayerQuantizedFixture = ConvolutionValidationQuantizedFixture<CLTensor, CLAccessor, CLConvolutionLayer, T>; |
| 202 | |
| 203 | TEST_SUITE(Quantized) |
| 204 | TEST_SUITE(QASYMM8) |
| 205 | FIXTURE_DATA_TEST_CASE(RunSmall, CLConvolutionLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallConvolutionLayerDataset(), |
| 206 | framework::dataset::make("ReshapeWeights", { true })), |
| 207 | framework::dataset::make("DataType", DataType::QASYMM8)), |
| 208 | framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 255.f, 10) }))) |
| 209 | { |
| 210 | // Validate output |
| 211 | validate(CLAccessor(_target), _reference, tolerance_qasymm8); |
| 212 | } |
| 213 | FIXTURE_DATA_TEST_CASE(RunLarge, CLConvolutionLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeConvolutionLayerDataset(), |
| 214 | framework::dataset::make("ReshapeWeights", { true })), |
| 215 | framework::dataset::make("DataType", DataType::QASYMM8)), |
| 216 | framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 255.f, 0) }))) |
| 217 | { |
| 218 | // Validate output |
| 219 | validate(CLAccessor(_target), _reference, tolerance_qasymm8); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 220 | } |
| 221 | TEST_SUITE_END() |
| 222 | TEST_SUITE_END() |
| 223 | |
| 224 | TEST_SUITE_END() |
| 225 | TEST_SUITE_END() |
| 226 | } // namespace validation |
| 227 | } // namespace test |
| 228 | } // namespace arm_compute |