| 340 | } |
| 341 | |
| 342 | void matrix_mul::benchmark_with_contrast( |
| 343 | Handle* handle, const std::vector<TestArg>& args, DType A_dtype, DType B_dtype, |
| 344 | DType C_dtype, const char* algo, param::MatrixMul::Format format, |
| 345 | DType contrast_A_dtype, DType contrast_B_dtype, DType contrast_C_dtype, |
| 346 | const char* contrast_algo, param::MatrixMul::Format contrast_format) { |
| 347 | using Param = MatrixMul::Param; |
| 348 | |
| 349 | megdnn_assert(A_dtype.enumv() == B_dtype.enumv()); |
| 350 | megdnn_assert(contrast_A_dtype.enumv() == contrast_B_dtype.enumv()); |
| 351 | Benchmarker<MatrixMul> benchmark_contrast(handle); |
| 352 | Benchmarker<MatrixMul> benchmark(handle); |
| 353 | constexpr size_t RUNS = 50; |
| 354 | if (algo) { |
| 355 | benchmark.set_before_exec_callback(AlgoChecker<MatrixMul>(algo)); |
| 356 | } |
| 357 | if (contrast_algo) { |
| 358 | benchmark_contrast.set_before_exec_callback( |
| 359 | AlgoChecker<MatrixMul>(contrast_algo)); |
| 360 | } |
| 361 | benchmark.set_dtype(0, A_dtype).set_dtype(1, B_dtype).set_dtype(2, C_dtype); |
| 362 | benchmark.set_times(RUNS); |
| 363 | benchmark_contrast.set_dtype(0, contrast_A_dtype) |
| 364 | .set_dtype(1, contrast_B_dtype) |
| 365 | .set_dtype(2, contrast_C_dtype); |
| 366 | benchmark_contrast.set_times(RUNS); |
| 367 | |
| 368 | auto bench = [](Benchmarker<MatrixMul>& benchmark, Param param, |
| 369 | param::MatrixMul::Format format, size_t m, size_t n, size_t k, |
| 370 | size_t pack_size) -> float { |
| 371 | param.format = format; |
| 372 | benchmark.set_param(param); |
| 373 | float used_algo = 1.0; |
| 374 | if (format == param::MatrixMul::Format::DEFAULT) { |
| 375 | size_t A0 = m * pack_size, A1 = k * pack_size, B0 = k * pack_size, B1 = n; |
| 376 | TensorShape A, B; |
| 377 | if (param.transposeA) { |
| 378 | std::swap(A0, A1); |
| 379 | } |
| 380 | if (param.transposeB) { |
| 381 | std::swap(B0, B1); |
| 382 | } |
| 383 | used_algo = benchmark.execs({{A0, A1}, {B0, B1}, {}}) / RUNS; |
| 384 | } else { |
| 385 | size_t A0 = m, A1 = k, B0 = k, B1 = n; |
| 386 | if (param.transposeA) { |
| 387 | std::swap(A0, A1); |
| 388 | } |
| 389 | if (param.transposeB) { |
| 390 | std::swap(B0, B1); |
| 391 | } |
| 392 | |
| 393 | used_algo = |
| 394 | benchmark.execs( |
| 395 | {{A0, A1, pack_size, pack_size}, {B0, B1, pack_size}, {}}) / |
| 396 | RUNS; |
| 397 | } |
| 398 | return used_algo; |
| 399 | }; |