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