Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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/CL/kernels/CLGEMMMatrixMultiplyKernel.h" |
| 25 | #include "arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h" |
| 26 | #include "arm_compute/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h" |
| 27 | #include "arm_compute/core/KernelDescriptors.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 30 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 31 | #include "arm_compute/runtime/CL/CLTensorAllocator.h" |
| 32 | #include "tests/CL/CLAccessor.h" |
| 33 | #include "tests/CL/Helper.h" |
| 34 | #include "tests/PaddingCalculator.h" |
| 35 | #include "tests/datasets/ShapeDatasets.h" |
| 36 | #include "tests/framework/Asserts.h" |
| 37 | #include "tests/framework/Macros.h" |
| 38 | #include "tests/framework/datasets/Datasets.h" |
| 39 | #include "tests/validation/Validation.h" |
| 40 | #include "tests/validation/fixtures/GEMMFixture.h" |
| 41 | |
| 42 | namespace arm_compute |
| 43 | { |
| 44 | namespace test |
| 45 | { |
| 46 | namespace validation |
| 47 | { |
| 48 | using namespace arm_compute::misc::shape_calculator; |
| 49 | |
| 50 | // Create function for CLGEMMReshapeLHSMatrixKernel |
| 51 | using CLGEMMReshapeLHSMatrix = CLSynthetizeFunction<CLGEMMReshapeLHSMatrixKernel>; |
| 52 | |
| 53 | // Create function for CLGEMMReshapeRHSMatrixKernel |
| 54 | using CLGEMMReshapeRHSMatrix = CLSynthetizeFunction<CLGEMMReshapeRHSMatrixKernel>; |
| 55 | |
| 56 | // Create function for CLGEMMMatrixMultiplyKernel |
| 57 | using CLGEMMMatrixMultiplyReshaped = CLSynthetizeFunction<CLGEMMMatrixMultiplyKernel>; |
| 58 | |
| 59 | // Fixture for GEMMMatrixMultiplyInterleavedTransposedValidationFixture |
| 60 | template <typename T> |
| 61 | using CLGEMMMatrixMultiplyReshapedFixture = |
| 62 | GEMMMatrixMultiplyInterleavedTransposedValidationFixture<CLTensor, CLAccessor, T, CLGEMMReshapeLHSMatrix, CLGEMMReshapeRHSMatrix, CLGEMMMatrixMultiplyReshaped>; |
| 63 | |
| 64 | // Fixture for GEMMMatrixMultiplyInterleavedTransposed3DValidationFixture |
| 65 | template <typename T> |
| 66 | using CLGEMMMatrixMultiplyReshaped3DFixture = |
| 67 | GEMMMatrixMultiplyInterleavedTransposed3DValidationFixture<CLTensor, CLAccessor, T, CLGEMMReshapeLHSMatrix, CLGEMMReshapeRHSMatrix, CLGEMMMatrixMultiplyReshaped>; |
| 68 | |
| 69 | namespace |
| 70 | { |
| 71 | // *INDENT-OFF* |
| 72 | // clang-format off |
| 73 | RelativeTolerance<float> rel_tolerance_f32(0.001f); |
| 74 | constexpr float abs_tolerance_f32(0.0001f); |
| 75 | |
| 76 | RelativeTolerance<half> rel_tolerance_f16(half(0.2)); |
| 77 | constexpr float tolerance_num_f16 = 0.02f; |
| 78 | |
| 79 | /** Alpha values to test - Precommit */ |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 80 | const auto alpha_values = framework::dataset::make("alpha", {1.0f, -0.75f} ); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 81 | |
| 82 | /** Beta values to test - Precommit */ |
Gian Marco Iodice | d820db6 | 2019-08-05 14:23:23 +0100 | [diff] [blame] | 83 | const auto beta_values = framework::dataset::make("beta", {-0.35f, 0.0f} ); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 84 | |
| 85 | /** M values to test - Precommit */ |
| 86 | const auto m_values_precommit = framework::dataset::make("M", 37); |
| 87 | |
| 88 | /** N values to test - Precommit */ |
| 89 | const auto n_values_precommit = framework::dataset::make("N", 51); |
| 90 | |
| 91 | /** K values to test - Precommit */ |
| 92 | const auto k_values_precommit = framework::dataset::make("K", 23); |
| 93 | |
| 94 | /** M values to test - Nightly */ |
| 95 | const auto m_values_nightly = framework::dataset::make("M", {421, 1}); |
| 96 | |
| 97 | /** N values to test - Nightly */ |
| 98 | const auto n_values_nightly = framework::dataset::make("N", 323); |
| 99 | |
| 100 | /** K values to test - Nightly */ |
| 101 | const auto k_values_nightly = framework::dataset::make("K", 207); |
| 102 | |
| 103 | /** M_W values to test - Precommit */ |
| 104 | const auto m_w_values_precommit = framework::dataset::make("M_W", 5); |
| 105 | |
| 106 | /** M_H values to test - Precommit */ |
| 107 | const auto m_h_values_precommit = framework::dataset::make("M_H", 7); |
| 108 | |
| 109 | /** M_W values to test - Nightly */ |
| 110 | const auto m_w_values_nightly = framework::dataset::make("M_W", 13); |
| 111 | |
| 112 | /** M_H values to test - Nightly */ |
| 113 | const auto m_h_values_nightly = framework::dataset::make("M_H", 27); |
| 114 | |
| 115 | /** Batch size values to test */ |
| 116 | const auto b_values = framework::dataset::make("batch_size", 1, 3); |
| 117 | |
| 118 | /** Activation values to test */ |
| 119 | const auto act_values = framework::dataset::make("Activation", |
| 120 | { |
| 121 | ActivationLayerInfo(), |
| 122 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f), |
| 123 | }); |
| 124 | |
| 125 | /** V0 values to test - Precommit */ |
| 126 | const auto v0_values_precommit = framework::dataset::make("V0", 2); |
| 127 | |
| 128 | /** H0 values to test - Precommit */ |
| 129 | const auto h0_values_precommit = framework::dataset::make("H0", 4); |
| 130 | |
| 131 | /** V0 values to test - Nightly */ |
| 132 | const auto v0_values_nightly = framework::dataset::make("V0", {2, 4}); |
| 133 | |
| 134 | /** H0 values to test - Nightly */ |
| 135 | const auto h0_values_nightly = framework::dataset::make("H0", { 2, 4 }); |
| 136 | |
| 137 | /** Broadcast bias from vector to matrix */ |
| 138 | const auto broadcast_bias_values = framework::dataset::make("broadcast_bias", {false, true} ); |
| 139 | |
| 140 | /** GPU architectures values to test */ |
| 141 | const auto gpu_arch_values = framework::dataset::make("GPUArch", |
| 142 | { |
| 143 | GPUTarget::MIDGARD, |
| 144 | GPUTarget::BIFROST |
| 145 | }); |
| 146 | |
| 147 | /** Data types values to test in the configuration */ |
| 148 | const auto data_type_values = framework::dataset::make("DataType", |
| 149 | { |
| 150 | DataType::F32, |
| 151 | DataType::F16 |
| 152 | }); |
| 153 | |
| 154 | /** M values to test */ |
| 155 | const auto fp16_mixed_precision_values = framework::dataset::make("fp16_mixed_precision", {true, false}); |
| 156 | |
| 157 | /** Configuration test */ |
| 158 | void validate_configuration(unsigned int m_value, unsigned int n_value, unsigned int k_value, unsigned int b_value, unsigned int v0_value, unsigned int h0_value, bool broadcast_bias, bool fp16_mixed_precision, const ActivationLayerInfo &act_info, DataType data_type, GPUTarget gpu_arch_value) |
| 159 | { |
| 160 | GEMMLHSMatrixInfo lhs_info; |
| 161 | lhs_info.m0 = 4; |
| 162 | lhs_info.k0 = 4; |
| 163 | lhs_info.v0 = v0_value; |
| 164 | lhs_info.interleave = true; |
| 165 | lhs_info.transpose = true; |
| 166 | |
| 167 | GEMMRHSMatrixInfo rhs_info; |
| 168 | rhs_info.n0 = data_type == DataType::F32? 4 : 8; |
| 169 | rhs_info.k0 = 1; |
| 170 | rhs_info.h0 = h0_value; |
| 171 | rhs_info.interleave = false; |
| 172 | rhs_info.transpose = false; |
| 173 | |
| 174 | GEMMReshapeInfo reshape_info(m_value, n_value, k_value, rhs_info.h0, lhs_info.v0, 0, false, broadcast_bias); |
| 175 | |
| 176 | const TensorShape lhs_shape(k_value, m_value, b_value); |
| 177 | const TensorShape lhs_shape_reshaped = compute_lhs_reshaped_shape(TensorInfo(lhs_shape, 1, data_type), |
| 178 | lhs_info, |
| 179 | false); |
| 180 | |
| 181 | const TensorShape rhs_shape(n_value, k_value, b_value); |
| 182 | const TensorShape rhs_shape_reshaped = compute_rhs_reshaped_shape(TensorInfo(rhs_shape, 1, data_type), |
| 183 | rhs_info); |
| 184 | |
| 185 | const TensorShape dst_shape = compute_mm_shape(TensorInfo(lhs_shape_reshaped, 1, data_type), |
| 186 | TensorInfo(rhs_shape_reshaped, 1, data_type), |
| 187 | reshape_info); |
| 188 | |
| 189 | const TensorShape bias_shape(n_value, |
| 190 | broadcast_bias? 1 : m_value, |
| 191 | broadcast_bias? 1 : b_value); |
| 192 | |
| 193 | // Create tensors |
| 194 | CLTensor lhs_reshaped = create_tensor<CLTensor>(lhs_shape_reshaped, data_type); |
| 195 | CLTensor rhs_reshaped = create_tensor<CLTensor>(rhs_shape_reshaped, data_type); |
| 196 | CLTensor bias = create_tensor<CLTensor>(bias_shape, data_type); |
| 197 | CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type); |
| 198 | |
| 199 | ARM_COMPUTE_EXPECT(lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 200 | ARM_COMPUTE_EXPECT(rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 201 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 202 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 203 | |
| 204 | // Create and configure function |
| 205 | CLGEMMMatrixMultiplyReshaped gemm; |
| 206 | gemm.configure(gpu_arch_value, &lhs_reshaped, &rhs_reshaped, &bias, &dst, 1.0f, 2.0f, true, reshape_info, fp16_mixed_precision, act_info); |
| 207 | } |
| 208 | } // namespace |
| 209 | |
| 210 | TEST_SUITE(CL) |
| 211 | TEST_SUITE(GEMMMatrixMultiplyInterleavedTransposed) |
| 212 | TEST_SUITE(Float) |
| 213 | TEST_SUITE(FP32) |
| 214 | DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(combine(combine(combine(combine(combine( |
| 215 | m_values_precommit, |
| 216 | n_values_precommit), |
| 217 | k_values_precommit), |
| 218 | framework::dataset::make("batch_size", 1)), |
| 219 | v0_values_precommit), |
| 220 | h0_values_precommit), |
| 221 | broadcast_bias_values), |
| 222 | framework::dataset::make("fp16_mixed_precision", false)), |
| 223 | act_values), |
| 224 | data_type_values), |
| 225 | gpu_arch_values), |
| 226 | m_value, n_value, k_value, b_value, v0_value, h0_value, broadcast_bias, fp16_mixed_precision_value, act_value, data_type_value, gpu_arch_value) |
| 227 | { |
| 228 | validate_configuration(m_value, n_value, k_value, b_value, v0_value, h0_value, broadcast_bias, fp16_mixed_precision_value, act_value, data_type_value, gpu_arch_value); |
| 229 | } |
| 230 | |
| 231 | FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyReshapedFixture<float>, framework::DatasetMode::ALL, |
| 232 | combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine( |
| 233 | m_values_precommit, |
| 234 | n_values_precommit), |
| 235 | k_values_precommit), |
| 236 | b_values), |
| 237 | alpha_values), |
| 238 | beta_values), |
| 239 | v0_values_precommit), |
| 240 | h0_values_precommit), |
| 241 | broadcast_bias_values), |
| 242 | framework::dataset::make("fp16_mixed_precision", false)), |
| 243 | act_values), |
| 244 | framework::dataset::make("DataType", DataType::F32)), |
| 245 | gpu_arch_values)) |
| 246 | { |
| 247 | // Validate output |
| 248 | validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32); |
| 249 | } |
| 250 | |
| 251 | FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMMatrixMultiplyReshapedFixture<float>, framework::DatasetMode::NIGHTLY, |
| 252 | combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine( |
| 253 | m_values_nightly, |
| 254 | n_values_nightly), |
| 255 | k_values_nightly), |
| 256 | b_values), |
| 257 | alpha_values), |
| 258 | beta_values), |
| 259 | v0_values_nightly), |
| 260 | h0_values_nightly), |
| 261 | broadcast_bias_values), |
| 262 | framework::dataset::make("fp16_mixed_precision", false)), |
| 263 | act_values), |
| 264 | framework::dataset::make("DataType", DataType::F32)), |
| 265 | gpu_arch_values)) |
| 266 | { |
| 267 | // Validate output |
| 268 | validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32); |
| 269 | } |
| 270 | |
| 271 | FIXTURE_DATA_TEST_CASE(RunSmall3D, CLGEMMMatrixMultiplyReshaped3DFixture<float>, framework::DatasetMode::ALL, |
| 272 | combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine( |
| 273 | m_w_values_precommit, |
| 274 | m_h_values_precommit), |
| 275 | n_values_precommit), |
| 276 | k_values_precommit), |
| 277 | b_values), |
| 278 | alpha_values), |
| 279 | beta_values), |
| 280 | v0_values_precommit), |
| 281 | h0_values_precommit), |
| 282 | broadcast_bias_values), |
| 283 | framework::dataset::make("fp16_mixed_precision", false)), |
| 284 | act_values), |
| 285 | framework::dataset::make("DataType", DataType::F32)), |
| 286 | gpu_arch_values)) |
| 287 | { |
| 288 | // Validate output |
| 289 | validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32); |
| 290 | } |
| 291 | |
| 292 | FIXTURE_DATA_TEST_CASE(RunLarge3D, CLGEMMMatrixMultiplyReshaped3DFixture<float>, framework::DatasetMode::NIGHTLY, |
| 293 | combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine( |
| 294 | m_w_values_nightly, |
| 295 | m_h_values_nightly), |
| 296 | n_values_nightly), |
| 297 | k_values_nightly), |
| 298 | b_values), |
| 299 | alpha_values), |
| 300 | beta_values), |
| 301 | v0_values_nightly), |
| 302 | h0_values_nightly), |
| 303 | broadcast_bias_values), |
| 304 | framework::dataset::make("fp16_mixed_precision", false)), |
| 305 | act_values), |
| 306 | framework::dataset::make("DataType", DataType::F32)), |
| 307 | gpu_arch_values)) |
| 308 | { |
| 309 | // Validate output |
| 310 | validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32); |
| 311 | } |
| 312 | |
| 313 | TEST_SUITE_END() // FP32 |
| 314 | |
| 315 | TEST_SUITE(FP16) |
| 316 | FIXTURE_DATA_TEST_CASE(RunSmall, CLGEMMMatrixMultiplyReshapedFixture<half>, framework::DatasetMode::ALL, |
| 317 | combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine( |
| 318 | m_values_precommit, |
| 319 | n_values_precommit), |
| 320 | k_values_precommit), |
| 321 | b_values), |
| 322 | alpha_values), |
| 323 | beta_values), |
| 324 | v0_values_precommit), |
| 325 | h0_values_precommit), |
| 326 | broadcast_bias_values), |
| 327 | fp16_mixed_precision_values), |
| 328 | act_values), |
| 329 | framework::dataset::make("DataType", DataType::F16)), |
| 330 | gpu_arch_values)) |
| 331 | { |
| 332 | // Validate output |
| 333 | validate(CLAccessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16); |
| 334 | } |
| 335 | |
| 336 | FIXTURE_DATA_TEST_CASE(RunLarge, CLGEMMMatrixMultiplyReshapedFixture<half>, framework::DatasetMode::NIGHTLY, |
| 337 | combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine( |
| 338 | m_values_nightly, |
| 339 | n_values_nightly), |
| 340 | k_values_nightly), |
| 341 | b_values), |
| 342 | alpha_values), |
| 343 | beta_values), |
| 344 | v0_values_nightly), |
| 345 | h0_values_nightly), |
| 346 | broadcast_bias_values), |
| 347 | fp16_mixed_precision_values), |
| 348 | act_values), |
| 349 | framework::dataset::make("DataType", DataType::F16)), |
| 350 | gpu_arch_values)) |
| 351 | { |
| 352 | // Validate output |
| 353 | validate(CLAccessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16); |
| 354 | } |
| 355 | |
| 356 | FIXTURE_DATA_TEST_CASE(RunSmall3D, CLGEMMMatrixMultiplyReshaped3DFixture<half>, framework::DatasetMode::ALL, |
| 357 | combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine( |
| 358 | m_w_values_precommit, |
| 359 | m_h_values_precommit), |
| 360 | n_values_precommit), |
| 361 | k_values_precommit), |
| 362 | b_values), |
| 363 | alpha_values), |
| 364 | beta_values), |
| 365 | v0_values_precommit), |
| 366 | h0_values_precommit), |
| 367 | broadcast_bias_values), |
| 368 | fp16_mixed_precision_values), |
| 369 | act_values), |
| 370 | framework::dataset::make("DataType", DataType::F16)), |
| 371 | gpu_arch_values)) |
| 372 | { |
| 373 | // Validate output |
| 374 | validate(CLAccessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16); |
| 375 | } |
| 376 | |
| 377 | FIXTURE_DATA_TEST_CASE(RunLarge3D, CLGEMMMatrixMultiplyReshaped3DFixture<half>, framework::DatasetMode::NIGHTLY, |
| 378 | combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine(combine( |
| 379 | m_w_values_nightly, |
| 380 | m_h_values_nightly), |
| 381 | n_values_nightly), |
| 382 | k_values_nightly), |
| 383 | b_values), |
| 384 | alpha_values), |
| 385 | beta_values), |
| 386 | v0_values_nightly), |
| 387 | h0_values_nightly), |
| 388 | broadcast_bias_values), |
| 389 | fp16_mixed_precision_values), |
| 390 | act_values), |
| 391 | framework::dataset::make("DataType", DataType::F16)), |
| 392 | gpu_arch_values)) |
| 393 | { |
| 394 | // Validate output |
| 395 | validate(CLAccessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16); |
| 396 | } |
| 397 | |
| 398 | TEST_SUITE_END() // FP16 |
| 399 | TEST_SUITE_END() // Float |
| 400 | TEST_SUITE_END() // GEMMMatrixMulipltyInterleavedTransposed |
| 401 | TEST_SUITE_END() // CL |
| 402 | } // namespace validation |
| 403 | } // namespace test |
| 404 | } // namespace arm_compute |