|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Callable |
| 4 | +from typing import Sequence |
| 5 | + |
| 6 | +import torch |
| 7 | +import triton |
| 8 | + |
| 9 | + |
| 10 | +def _summarize_statistics( |
| 11 | + times: torch.Tensor, |
| 12 | + quantiles: Sequence[float] | None, |
| 13 | + return_mode: str, |
| 14 | +) -> float | list[float]: |
| 15 | + if quantiles is not None: |
| 16 | + ret = torch.quantile(times, torch.tensor(quantiles, dtype=torch.float)).tolist() |
| 17 | + if len(ret) == 1: |
| 18 | + ret = ret[0] |
| 19 | + return ret |
| 20 | + if return_mode == "all": |
| 21 | + return times.tolist() |
| 22 | + return getattr(torch, return_mode)(times).item() |
| 23 | + |
| 24 | + |
| 25 | +def do_bench_cudagraph_with_cache_clear( |
| 26 | + fn: Callable[[], object], |
| 27 | + rep: int = 20, |
| 28 | + grad_to_none: Sequence[torch.Tensor] | None = None, |
| 29 | + quantiles: Sequence[float] | None = None, |
| 30 | + return_mode: str = "mean", |
| 31 | +) -> float | list[float]: |
| 32 | + """ |
| 33 | + Clone of triton.testing.do_bench_cudagraph with explicit L2 cache clearing. |
| 34 | +
|
| 35 | + NOTE: We will switch to use triton.testing.do_bench_cudagraph once it has explicit L2 cache clearing. |
| 36 | +
|
| 37 | + Args: |
| 38 | + fn: Function to benchmark |
| 39 | + rep: Target total measurement time in milliseconds |
| 40 | + grad_to_none: Tensors whose gradients should be cleared before each measurement |
| 41 | + quantiles: Quantiles to compute from the timing measurements |
| 42 | + return_mode: "min", "max", "mean", "median", or "all" |
| 43 | +
|
| 44 | + Returns: |
| 45 | + Timing measurement(s) in milliseconds according to return_mode |
| 46 | + """ |
| 47 | + assert return_mode in ["min", "max", "mean", "median", "all"] |
| 48 | + |
| 49 | + # Get a cache tensor and function to zero it for L2 cache clearing |
| 50 | + cache = triton.runtime.driver.active.get_empty_cache_for_benchmark() # type: ignore[attr-defined] |
| 51 | + clear_cache_fn = cache.zero_ |
| 52 | + |
| 53 | + # Use a separate CUDA stream for all benchmark operations |
| 54 | + with torch.cuda.stream(torch.cuda.Stream()): |
| 55 | + # Warmup: clear cache and run function once to ensure it's compiled |
| 56 | + clear_cache_fn() |
| 57 | + fn() |
| 58 | + |
| 59 | + # Reset gradients if needed (for autograd-enabled benchmarks) |
| 60 | + if grad_to_none is not None: |
| 61 | + for x in grad_to_none: |
| 62 | + x.detach_() |
| 63 | + x.requires_grad_(True) |
| 64 | + x.grad = None |
| 65 | + |
| 66 | + # Estimate execution time |
| 67 | + start_event = torch.cuda.Event(enable_timing=True) |
| 68 | + end_event = torch.cuda.Event(enable_timing=True) |
| 69 | + start_event.record() |
| 70 | + for _ in range(5): |
| 71 | + clear_cache_fn() |
| 72 | + fn() |
| 73 | + end_event.record() |
| 74 | + torch.cuda.synchronize() |
| 75 | + estimate_ms = start_event.elapsed_time(end_event) / 5 |
| 76 | + |
| 77 | + # Calculate number of repetitions needed to reach target measurement time (rep) |
| 78 | + n_repeat = 1000 if estimate_ms == 0 else max(1, int(rep / estimate_ms)) |
| 79 | + |
| 80 | + # Create a CUDA graph for the actual kernel execution + cache clearing |
| 81 | + g = torch.cuda.CUDAGraph() |
| 82 | + with torch.cuda.graph(g): |
| 83 | + for _ in range(n_repeat): |
| 84 | + if grad_to_none is not None: |
| 85 | + for x in grad_to_none: |
| 86 | + x.grad = None |
| 87 | + clear_cache_fn() |
| 88 | + fn() |
| 89 | + torch.cuda.synchronize() |
| 90 | + |
| 91 | + # Create a separate CUDA graph for just cache clearing |
| 92 | + cache_clear_graph = torch.cuda.CUDAGraph() |
| 93 | + with torch.cuda.graph(cache_clear_graph): |
| 94 | + for _ in range(n_repeat): |
| 95 | + clear_cache_fn() |
| 96 | + torch.cuda.synchronize() |
| 97 | + |
| 98 | + # Run multiple retries to get stable measurements |
| 99 | + n_retries = 10 |
| 100 | + cache_clear_times = [] |
| 101 | + total_times = [] |
| 102 | + for _ in range(n_retries): |
| 103 | + # Measure time for cache clearing only |
| 104 | + cache_clear_start_event = torch.cuda.Event(enable_timing=True) |
| 105 | + cache_clear_end_event = torch.cuda.Event(enable_timing=True) |
| 106 | + cache_clear_start_event.record() |
| 107 | + cache_clear_graph.replay() |
| 108 | + cache_clear_end_event.record() |
| 109 | + torch.cuda.synchronize() |
| 110 | + cache_clear_times.append( |
| 111 | + cache_clear_start_event.elapsed_time(cache_clear_end_event) / n_repeat |
| 112 | + ) |
| 113 | + |
| 114 | + # Measure total time (cache clearing + kernel execution) |
| 115 | + start_event = torch.cuda.Event(enable_timing=True) |
| 116 | + end_event = torch.cuda.Event(enable_timing=True) |
| 117 | + start_event.record() |
| 118 | + g.replay() |
| 119 | + end_event.record() |
| 120 | + torch.cuda.synchronize() |
| 121 | + total_times.append(start_event.elapsed_time(end_event) / n_repeat) |
| 122 | + |
| 123 | + # Subtract cache clearing overhead to get pure kernel execution time |
| 124 | + all_kernel_times = [] |
| 125 | + for total_time, cache_clear_time in zip( |
| 126 | + total_times, cache_clear_times, strict=True |
| 127 | + ): |
| 128 | + kernel_time = total_time - cache_clear_time |
| 129 | + all_kernel_times.append(kernel_time) |
| 130 | + |
| 131 | + # Compute the requested statistic |
| 132 | + times = torch.tensor(all_kernel_times, dtype=torch.float) |
| 133 | + return _summarize_statistics(times, quantiles, return_mode) |
0 commit comments