Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [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/NEConvolution.h" |
| 26 | #include "arm_compute/runtime/Tensor.h" |
| 27 | #include "arm_compute/runtime/TensorAllocator.h" |
| 28 | #include "tests/NEON/Accessor.h" |
| 29 | #include "tests/PaddingCalculator.h" |
| 30 | #include "tests/datasets/BorderModeDataset.h" |
| 31 | #include "tests/datasets/ShapeDatasets.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/ConvolutionFixture.h" |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace validation |
| 43 | { |
| 44 | namespace |
| 45 | { |
Georgios Pinitas | acacc32 | 2017-12-13 12:48:44 +0000 | [diff] [blame] | 46 | /** Tolerance value for comparing reference's output against implementation |
| 47 | * |
| 48 | * This is due to the fact that NEON target performs multiplication with reciprocal of scale, |
| 49 | * while reference performs direct division with scale. |
| 50 | */ |
| 51 | constexpr AbsoluteTolerance<uint8_t> tolerance_u8(1); |
| 52 | |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 53 | /* Convolution3x3 */ |
| 54 | constexpr unsigned int filter_size_3x3 = 3; /* Size of the kernel/filter in number of elements. */ |
| 55 | constexpr BorderSize border_size_3x3(filter_size_3x3 / 2); /* Border size of the kernel/filter around its central element. */ |
| 56 | |
| 57 | /* Convolution5x5 */ |
| 58 | constexpr unsigned int filter_size_5x5 = 5; /* Size of the kernel/filter in number of elements. */ |
| 59 | constexpr BorderSize border_size_5x5(filter_size_5x5 / 2); /* Border size of the kernel/filter around its central element. */ |
| 60 | |
| 61 | /* Convolution7x7 */ |
| 62 | constexpr unsigned int filter_size_7x7 = 7; /* Size of the kernel/filter in number of elements. */ |
| 63 | constexpr BorderSize border_size_7x7(filter_size_7x7 / 2); /* Border size of the kernel/filter around its central element. */ |
| 64 | |
| 65 | /* Convolutionx */ |
| 66 | constexpr unsigned int filter_size_9x9 = 9; /* Size of the kernel/filter in number of elements. */ |
| 67 | constexpr BorderSize border_size_9x9(filter_size_9x9 / 2); /* Border size of the kernel/filter around its central element. */ |
| 68 | |
| 69 | /** Create conv matrix with filter size, and fill them with random value |
| 70 | * |
| 71 | * @param[in/out] conv Convolution matrix to be filled with random int16_t |
| 72 | * @param[in] filter_size Filter Size. |
| 73 | */ |
| 74 | void create_conv(int16_t *conv, const unsigned int filter_size) |
| 75 | { |
| 76 | std::mt19937 gen(library->seed()); |
| 77 | std::uniform_int_distribution<int16_t> distribution_int16(-32768, 32767); |
| 78 | |
| 79 | for(unsigned int i = 0; i < filter_size * filter_size; ++i) |
| 80 | { |
| 81 | conv[i] = distribution_int16(gen); |
| 82 | } |
| 83 | } |
| 84 | } // namespace |
| 85 | |
| 86 | TEST_SUITE(NEON) |
| 87 | TEST_SUITE(CustomConvolution) |
| 88 | TEST_SUITE(CustomConvolution3x3) |
| 89 | |
| 90 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), framework::dataset::make("DataType", DataType::U8)), |
| 91 | datasets::BorderModes()), |
| 92 | shape, data_type, border_mode) |
| 93 | { |
| 94 | // Create tensors |
| 95 | Tensor src = create_tensor<Tensor>(shape, data_type); |
| 96 | Tensor dst = create_tensor<Tensor>(shape, data_type); |
| 97 | |
| 98 | // Create conv matrix |
| 99 | int16_t conv[9]; |
| 100 | create_conv(conv, filter_size_3x3); |
| 101 | |
| 102 | // Generate random scale value between 0 and 255. |
| 103 | std::mt19937 gen(library->seed()); |
| 104 | std::uniform_int_distribution<uint8_t> distribution(0, 255); |
| 105 | uint32_t scale = distribution(gen); |
| 106 | |
| 107 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 108 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 109 | |
| 110 | // Create and configure function |
| 111 | NEConvolution3x3 convolution; |
| 112 | convolution.configure(&src, &dst, conv, scale, border_mode); |
| 113 | |
| 114 | // Validate valid region |
| 115 | const ValidRegion dst_valid_region = shape_to_valid_region(shape, (border_mode == BorderMode::UNDEFINED), border_size_3x3); |
| 116 | validate(dst.info()->valid_region(), dst_valid_region); |
| 117 | |
| 118 | // Validate padding |
| 119 | PaddingCalculator calculator(shape.x(), 8); |
| 120 | calculator.set_border_size(1); |
| 121 | calculator.set_border_mode(border_mode); |
| 122 | |
| 123 | const PaddingSize dst_padding = calculator.required_padding(); |
| 124 | |
| 125 | calculator.set_accessed_elements(16); |
| 126 | calculator.set_access_offset(-1); |
| 127 | |
| 128 | const PaddingSize src_padding = calculator.required_padding(); |
| 129 | |
| 130 | validate(src.info()->padding(), src_padding); |
| 131 | validate(dst.info()->padding(), dst_padding); |
| 132 | } |
| 133 | |
| 134 | template <typename T> |
| 135 | using NEConvolutionFixture = ConvolutionValidationFixture<Tensor, Accessor, NEConvolution3x3, T, filter_size_3x3>; |
| 136 | |
| 137 | FIXTURE_DATA_TEST_CASE(RunSmall, NEConvolutionFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", |
| 138 | DataType::U8)), |
| 139 | datasets::BorderModes())) |
| 140 | { |
| 141 | // Validate output |
Georgios Pinitas | acacc32 | 2017-12-13 12:48:44 +0000 | [diff] [blame] | 142 | validate(Accessor(_target), _reference, shape_to_valid_region(_reference.shape(), (_border_mode == BorderMode::UNDEFINED), border_size_3x3), tolerance_u8); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | FIXTURE_DATA_TEST_CASE(RunLarge, NEConvolutionFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", |
| 146 | DataType::U8)), |
| 147 | datasets::BorderModes())) |
| 148 | { |
| 149 | // Validate output |
Georgios Pinitas | acacc32 | 2017-12-13 12:48:44 +0000 | [diff] [blame] | 150 | validate(Accessor(_target), _reference, shape_to_valid_region(_reference.shape(), (_border_mode == BorderMode::UNDEFINED), border_size_3x3), tolerance_u8); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 151 | } |
| 152 | TEST_SUITE_END() /* Custom Convolution3x3 */ |
| 153 | |
| 154 | TEST_SUITE(CustomConvolution5x5) |
| 155 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), framework::dataset::make("DataType", DataType::U8)), |
| 156 | datasets::BorderModes()), |
| 157 | shape, data_type, border_mode) |
| 158 | { |
| 159 | // Create tensors |
| 160 | Tensor src = create_tensor<Tensor>(shape, data_type); |
| 161 | Tensor dst = create_tensor<Tensor>(shape, data_type); |
| 162 | |
| 163 | // Create conv matrix |
| 164 | int16_t conv[25]; |
| 165 | create_conv(conv, filter_size_5x5); |
| 166 | |
| 167 | // Generate random scale value between 0 and 255. |
| 168 | std::mt19937 gen(library->seed()); |
| 169 | std::uniform_int_distribution<uint8_t> distribution(0, 255); |
| 170 | uint32_t scale = distribution(gen); |
| 171 | |
| 172 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 173 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 174 | |
| 175 | // Create and configure function |
| 176 | NEConvolution5x5 convolution; |
| 177 | convolution.configure(&src, &dst, conv, scale, border_mode); |
| 178 | |
| 179 | // Validate valid region |
| 180 | const ValidRegion dst_valid_region = shape_to_valid_region(shape, (border_mode == BorderMode::UNDEFINED), border_size_5x5); |
| 181 | validate(dst.info()->valid_region(), dst_valid_region); |
| 182 | |
| 183 | // Validate padding |
| 184 | PaddingCalculator calculator(shape.x(), 8); |
| 185 | calculator.set_border_size(2); |
| 186 | calculator.set_border_mode(border_mode); |
| 187 | |
| 188 | const PaddingSize dst_padding = calculator.required_padding(); |
| 189 | |
| 190 | calculator.set_accessed_elements(16); |
| 191 | calculator.set_access_offset(-2); |
| 192 | |
| 193 | const PaddingSize src_padding = calculator.required_padding(); |
| 194 | |
| 195 | validate(src.info()->padding(), src_padding); |
| 196 | validate(dst.info()->padding(), dst_padding); |
| 197 | } |
| 198 | |
| 199 | template <typename T> |
| 200 | using NEConvolutionFixture = ConvolutionValidationFixture<Tensor, Accessor, NEConvolution5x5, T, filter_size_5x5>; |
| 201 | |
| 202 | FIXTURE_DATA_TEST_CASE(RunSmall, NEConvolutionFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", |
| 203 | DataType::U8)), |
| 204 | datasets::BorderModes())) |
| 205 | { |
| 206 | // Validate output |
Georgios Pinitas | acacc32 | 2017-12-13 12:48:44 +0000 | [diff] [blame] | 207 | validate(Accessor(_target), _reference, shape_to_valid_region(_reference.shape(), (_border_mode == BorderMode::UNDEFINED), border_size_5x5), tolerance_u8); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | FIXTURE_DATA_TEST_CASE(RunLarge, NEConvolutionFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", |
| 211 | DataType::U8)), |
| 212 | datasets::BorderModes())) |
| 213 | { |
| 214 | // Validate output |
Georgios Pinitas | acacc32 | 2017-12-13 12:48:44 +0000 | [diff] [blame] | 215 | validate(Accessor(_target), _reference, shape_to_valid_region(_reference.shape(), (_border_mode == BorderMode::UNDEFINED), border_size_5x5), tolerance_u8); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 216 | } |
| 217 | TEST_SUITE_END() /* Custom Convolution 5x5 */ |
| 218 | |
| 219 | TEST_SUITE(CustomConvolution7x7) |
| 220 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), framework::dataset::make("DataType", DataType::U8)), |
| 221 | datasets::BorderModes()), |
| 222 | shape, data_type, border_mode) |
| 223 | { |
| 224 | // Create tensors |
| 225 | Tensor src = create_tensor<Tensor>(shape, data_type); |
| 226 | Tensor dst = create_tensor<Tensor>(shape, data_type); |
| 227 | |
| 228 | // Create conv matrix |
| 229 | int16_t conv[49]; |
| 230 | create_conv(conv, filter_size_7x7); |
| 231 | |
| 232 | // Generate random scale value between 0 and 255. |
| 233 | std::mt19937 gen(library->seed()); |
| 234 | std::uniform_int_distribution<uint8_t> distribution(0, 255); |
| 235 | uint32_t scale = distribution(gen); |
| 236 | |
| 237 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 238 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 239 | |
| 240 | // Create and configure function |
| 241 | NEConvolution7x7 convolution; |
| 242 | convolution.configure(&src, &dst, conv, scale, border_mode); |
| 243 | |
| 244 | // Validate valid region |
| 245 | const ValidRegion dst_valid_region = shape_to_valid_region(shape, (border_mode == BorderMode::UNDEFINED), border_size_7x7); |
| 246 | validate(dst.info()->valid_region(), dst_valid_region); |
| 247 | |
| 248 | // Validate padding |
| 249 | PaddingCalculator calculator(shape.x(), 8); |
| 250 | calculator.set_border_size(3); |
| 251 | calculator.set_border_mode(border_mode); |
| 252 | |
| 253 | const PaddingSize dst_padding = calculator.required_padding(); |
| 254 | |
| 255 | calculator.set_accessed_elements(16); |
| 256 | calculator.set_access_offset(-3); |
| 257 | |
| 258 | const PaddingSize src_padding = calculator.required_padding(); |
| 259 | |
| 260 | validate(src.info()->padding(), src_padding); |
| 261 | validate(dst.info()->padding(), dst_padding); |
| 262 | } |
| 263 | |
| 264 | template <typename T> |
| 265 | using NEConvolutionFixture = ConvolutionValidationFixture<Tensor, Accessor, NEConvolution7x7, T, filter_size_7x7>; |
| 266 | |
| 267 | FIXTURE_DATA_TEST_CASE(RunSmall, NEConvolutionFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", |
| 268 | DataType::U8)), |
| 269 | datasets::BorderModes())) |
| 270 | { |
| 271 | // Validate output |
Georgios Pinitas | acacc32 | 2017-12-13 12:48:44 +0000 | [diff] [blame] | 272 | validate(Accessor(_target), _reference, shape_to_valid_region(_reference.shape(), (_border_mode == BorderMode::UNDEFINED), border_size_7x7), tolerance_u8); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | FIXTURE_DATA_TEST_CASE(RunLarge, NEConvolutionFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", |
| 276 | DataType::U8)), |
| 277 | datasets::BorderModes())) |
| 278 | { |
| 279 | // Validate output |
Georgios Pinitas | acacc32 | 2017-12-13 12:48:44 +0000 | [diff] [blame] | 280 | validate(Accessor(_target), _reference, shape_to_valid_region(_reference.shape(), (_border_mode == BorderMode::UNDEFINED), border_size_7x7), tolerance_u8); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 281 | } |
| 282 | TEST_SUITE_END() /* Custom Convolution 7x7 */ |
| 283 | |
| 284 | TEST_SUITE(CustomConvolution9x9) |
| 285 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), framework::dataset::make("DataType", DataType::U8)), |
| 286 | datasets::BorderModes()), |
| 287 | shape, data_type, border_mode) |
| 288 | { |
| 289 | // Create tensors |
| 290 | Tensor src = create_tensor<Tensor>(shape, data_type); |
| 291 | Tensor dst = create_tensor<Tensor>(shape, data_type); |
| 292 | |
| 293 | // Create conv matrix |
| 294 | int16_t conv[81]; |
| 295 | create_conv(conv, filter_size_9x9); |
| 296 | |
| 297 | // Generate random scale value between 0 and 255. |
| 298 | std::mt19937 gen(library->seed()); |
| 299 | std::uniform_int_distribution<uint8_t> distribution(0, 255); |
| 300 | uint32_t scale = distribution(gen); |
| 301 | |
| 302 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 303 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 304 | |
| 305 | // Create and configure function |
| 306 | NEConvolution9x9 convolution; |
| 307 | convolution.configure(&src, &dst, conv, scale, border_mode); |
| 308 | |
| 309 | // Validate valid region |
| 310 | const ValidRegion dst_valid_region = shape_to_valid_region(shape, (border_mode == BorderMode::UNDEFINED), border_size_9x9); |
| 311 | validate(dst.info()->valid_region(), dst_valid_region); |
| 312 | |
| 313 | // Validate padding |
| 314 | PaddingCalculator calculator(shape.x(), 8); |
| 315 | calculator.set_border_size(4); |
| 316 | calculator.set_border_mode(border_mode); |
| 317 | |
| 318 | const PaddingSize dst_padding = calculator.required_padding(); |
| 319 | |
| 320 | calculator.set_accessed_elements(16); |
| 321 | calculator.set_access_offset(-4); |
| 322 | |
| 323 | const PaddingSize src_padding = calculator.required_padding(); |
| 324 | |
| 325 | validate(src.info()->padding(), src_padding); |
| 326 | validate(dst.info()->padding(), dst_padding); |
| 327 | } |
| 328 | |
| 329 | template <typename T> |
| 330 | using NEConvolutionFixture = ConvolutionValidationFixture<Tensor, Accessor, NEConvolution9x9, T, filter_size_9x9>; |
| 331 | |
| 332 | FIXTURE_DATA_TEST_CASE(RunSmall, NEConvolutionFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", |
| 333 | DataType::U8)), |
| 334 | datasets::BorderModes())) |
| 335 | { |
| 336 | // Validate output |
Georgios Pinitas | acacc32 | 2017-12-13 12:48:44 +0000 | [diff] [blame] | 337 | validate(Accessor(_target), _reference, shape_to_valid_region(_reference.shape(), (_border_mode == BorderMode::UNDEFINED), border_size_9x9), tolerance_u8); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | FIXTURE_DATA_TEST_CASE(RunLarge, NEConvolutionFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", |
| 341 | DataType::U8)), |
| 342 | datasets::BorderModes())) |
| 343 | { |
| 344 | // Validate output |
Georgios Pinitas | acacc32 | 2017-12-13 12:48:44 +0000 | [diff] [blame] | 345 | validate(Accessor(_target), _reference, shape_to_valid_region(_reference.shape(), (_border_mode == BorderMode::UNDEFINED), border_size_9x9), tolerance_u8); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 346 | } |
| 347 | TEST_SUITE_END() /* Custom Convolution 9x9 */ |
| 348 | TEST_SUITE_END() |
| 349 | TEST_SUITE_END() |
| 350 | } // namespace validation |
| 351 | } // namespace test |
| 352 | } // namespace arm_compute |