KernelBench-compatible performance benchmarking via CUDA event timing. Returns speedup = ref_time / kernel_time.
(
model_new,
model_ref,
get_inputs_fn: Callable,
n_warmup: int = DEFAULT_N_WARMUP,
n_timed: int = DEFAULT_N_TIMED,
device: str = "cuda",
)
| 425 | # --------------------------------------------------------------------------- |
| 426 | |
| 427 | def run_performance( |
| 428 | model_new, |
| 429 | model_ref, |
| 430 | get_inputs_fn: Callable, |
| 431 | n_warmup: int = DEFAULT_N_WARMUP, |
| 432 | n_timed: int = DEFAULT_N_TIMED, |
| 433 | device: str = "cuda", |
| 434 | ) -> Dict[str, Any]: |
| 435 | """ |
| 436 | KernelBench-compatible performance benchmarking via CUDA event timing. |
| 437 | Returns speedup = ref_time / kernel_time. |
| 438 | """ |
| 439 | import torch |
| 440 | |
| 441 | result: Dict[str, Any] = { |
| 442 | "kernel_time_ms": 0.0, |
| 443 | "reference_time_ms": 0.0, |
| 444 | "speedup": 0.0, |
| 445 | "kernel_times": [], |
| 446 | "reference_times": [], |
| 447 | } |
| 448 | |
| 449 | torch.manual_seed(0) |
| 450 | inputs = get_inputs_fn() |
| 451 | inputs_dev = [ |
| 452 | inp.to(device) if isinstance(inp, torch.Tensor) else inp |
| 453 | for inp in inputs |
| 454 | ] |
| 455 | |
| 456 | def _time_model(model, inputs_list, n_warm, n_iter): |
| 457 | """Time a model using CUDA events.""" |
| 458 | for _ in range(n_warm): |
| 459 | with torch.no_grad(): |
| 460 | model(*inputs_list) |
| 461 | torch.cuda.synchronize() |
| 462 | |
| 463 | times = [] |
| 464 | for _ in range(n_iter): |
| 465 | start = torch.cuda.Event(enable_timing=True) |
| 466 | end = torch.cuda.Event(enable_timing=True) |
| 467 | start.record() |
| 468 | with torch.no_grad(): |
| 469 | model(*inputs_list) |
| 470 | end.record() |
| 471 | torch.cuda.synchronize() |
| 472 | times.append(start.elapsed_time(end)) |
| 473 | return times |
| 474 | |
| 475 | try: |
| 476 | ref_times = _time_model(model_ref, inputs_dev, n_warmup, n_timed) |
| 477 | result["reference_times"] = ref_times |
| 478 | result["reference_time_ms"] = _robust_median(ref_times) |
| 479 | |
| 480 | kernel_times = _time_model(model_new, inputs_dev, n_warmup, n_timed) |
| 481 | result["kernel_times"] = kernel_times |
| 482 | result["kernel_time_ms"] = _robust_median(kernel_times) |
| 483 | |
| 484 | if result["kernel_time_ms"] > 0: |
no test coverage detected