|
| 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 do_bench_cudagraph_with_cache_clear( |
| 11 | + fn: Callable[[], object], |
| 12 | + rep: int = 20, |
| 13 | + grad_to_none: Sequence[torch.Tensor] | None = None, |
| 14 | +) -> float: |
| 15 | + """ |
| 16 | + Clone of triton.testing.do_bench_cudagraph with explicit L2 cache clearing. |
| 17 | + Only supports calculating mean execution time. |
| 18 | +
|
| 19 | + Args: |
| 20 | + fn: Function to benchmark |
| 21 | + rep: Target total measurement time in milliseconds |
| 22 | + grad_to_none: Tensors whose gradients should be cleared before each measurement |
| 23 | +
|
| 24 | + Returns: |
| 25 | + Mean execution time in milliseconds |
| 26 | + """ |
| 27 | + # Get a cache tensor and function to zero it for L2 cache clearing |
| 28 | + cache = triton.runtime.driver.active.get_empty_cache_for_benchmark() # type: ignore[attr-defined] |
| 29 | + clear_cache_fn = cache.zero_ |
| 30 | + |
| 31 | + with torch.cuda.stream(torch.cuda.Stream()): |
| 32 | + # Warmup: clear cache and run function once to ensure it's compiled |
| 33 | + clear_cache_fn() |
| 34 | + fn() |
| 35 | + |
| 36 | + # Reset gradients if needed |
| 37 | + if grad_to_none is not None: |
| 38 | + for x in grad_to_none: |
| 39 | + x.detach_() |
| 40 | + x.requires_grad_(True) |
| 41 | + x.grad = None |
| 42 | + |
| 43 | + # Estimate execution time |
| 44 | + start_event = torch.cuda.Event(enable_timing=True) |
| 45 | + end_event = torch.cuda.Event(enable_timing=True) |
| 46 | + start_event.record() |
| 47 | + for _ in range(5): |
| 48 | + clear_cache_fn() |
| 49 | + fn() |
| 50 | + end_event.record() |
| 51 | + torch.cuda.synchronize() |
| 52 | + estimate_ms = start_event.elapsed_time(end_event) / 5 |
| 53 | + |
| 54 | + # Calculate number of repetitions needed to reach target measurement time (rep) |
| 55 | + n_repeat = 1000 if estimate_ms == 0 else max(1, int(rep / estimate_ms)) |
| 56 | + |
| 57 | + # Create a CUDA graph for the actual kernel execution + cache clearing |
| 58 | + g = torch.cuda.CUDAGraph() |
| 59 | + with torch.cuda.graph(g): |
| 60 | + for _ in range(n_repeat): |
| 61 | + if grad_to_none is not None: |
| 62 | + for x in grad_to_none: |
| 63 | + x.grad = None |
| 64 | + clear_cache_fn() |
| 65 | + fn() |
| 66 | + torch.cuda.synchronize() |
| 67 | + |
| 68 | + # Create a separate CUDA graph for just cache clearing |
| 69 | + cache_clear_graph = torch.cuda.CUDAGraph() |
| 70 | + with torch.cuda.graph(cache_clear_graph): |
| 71 | + for _ in range(n_repeat): |
| 72 | + clear_cache_fn() |
| 73 | + torch.cuda.synchronize() |
| 74 | + |
| 75 | + # Measure time for cache clearing only |
| 76 | + cache_clear_start_event = torch.cuda.Event(enable_timing=True) |
| 77 | + cache_clear_end_event = torch.cuda.Event(enable_timing=True) |
| 78 | + cache_clear_start_event.record() |
| 79 | + cache_clear_graph.replay() |
| 80 | + cache_clear_end_event.record() |
| 81 | + torch.cuda.synchronize() |
| 82 | + cache_clear_time = ( |
| 83 | + cache_clear_start_event.elapsed_time(cache_clear_end_event) / n_repeat |
| 84 | + ) |
| 85 | + |
| 86 | + # Measure total time (cache clearing + kernel execution) |
| 87 | + start_event = torch.cuda.Event(enable_timing=True) |
| 88 | + end_event = torch.cuda.Event(enable_timing=True) |
| 89 | + start_event.record() |
| 90 | + g.replay() |
| 91 | + end_event.record() |
| 92 | + torch.cuda.synchronize() |
| 93 | + total_time = start_event.elapsed_time(end_event) / n_repeat |
| 94 | + |
| 95 | + # Subtract cache clearing overhead to get pure kernel execution time |
| 96 | + return total_time - cache_clear_time |
0 commit comments