Repository navigation
[Inductor] max_autotune benchmark runs under CUDA Graph - #179246
mattteochen wants to merge 12 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/179246
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 9d357f4 with merge base a702838 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot label "topic: not user facing" |
|
|
||
| def __init__(self: Self) -> None: | ||
| pass | ||
| self._in_cudagraph_benchmark = False |
There was a problem hiding this comment.
This flag is need to stop this recursion:
InductorBenchmarker.benchmark_gpu(kernel_call) # config is on
→ calls self.benchmark_gpu_with_cuda_graph(kernel_call)
→ this is Benchmarker.benchmark_gpu_with_cuda_graph (base class)
→ captures kernel_call into graph
→ calls self.benchmark_gpu(graph.replay, **kwargs)
→ this resolves to InductorBenchmarker.benchmark_gpu again (same object)
→ config is STILL on → would call benchmark_gpu_with_cuda_graph again → recursionThere was a problem hiding this comment.
It doesn't seem natural to have this flag in the base class Benchmarker that is only checked in InductorBenchmarker.
Can it be done on InductorBenchmarker's side? I'm afraid it may be tricky, given that _benchmark_with_cudagraphs calls benchmark_gpu_with_cuda_graph directly.
|
|
|
The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:
Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows. |
eellison
left a comment
There was a problem hiding this comment.
Looks good to me! cc @shunting314 @PaulZhang12
|
@pytorchbot merge -f “unrelated failures” |
|
❌ 🤖 pytorchbot command failed: Try |
|
@pytorchbot merge -f "unrelated failures" |
Merge startedYour change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
Sorry - this is breaking: @pytorchbot revert -m="breaking trunk" -c=nosignal |
|
@pytorchbot successfully started a revert job. Check the current status here. |
|
@mattteochen your PR has been successfully reverted. |
…79246)" This reverts commit 4c61d4a. Reverted #179246 on behalf of https://github.com/aorenste due to breaking trunk ([comment](#179246 (comment)))
|
Successfully rebased |
a914e80 to
9d357f4
Compare
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…9246) Closes pytorch#179236. For memory-bound kernels like RMSNorm, `max_autotune` selects worse configs than `default` mode when the workload runs under an externally captured CUDA graph. This happens because the autotuner's eager benchmarking includes host-side kernel launch overhead in its measurements, which distorts the ranking of configs that differ primarily in GPU-side execution time. This PR adds `autotune_cudagraph_benchmarking` — when enabled, the Triton config autotuner captures each candidate kernel in a CUDA graph and benchmarks graph replay instead of eager launches. This eliminates launch overhead from measurements, giving timings representative of CUDA graph replay execution. Enable via config or environment variable: ```python torch._inductor.config.autotune_cudagraph_benchmarking = True # or TORCHINDUCTOR_AUTOTUNE_CUDAGRAPH_BENCHMARKING=1 ``` On GB200 with the RMSNorm repro from the issue: | Config | Time | |---|---| | max_autotune (eager bench) | 6.19 us | | max_autotune (cudagraph bench) | **4.14 us** | | default compile | 4.13 us | The implementation reuses the existing `benchmark_gpu_with_cuda_graph` path (used by custom op autotuning) — `InductorBenchmarker.benchmark_gpu` delegates to it when the config flag is on, with fallback to eager benchmarking if CUDA graph capture fails. Pull Request resolved: pytorch#179246 Approved by: https://github.com/eellison
…torch#179246)" This reverts commit 4c61d4a. Reverted pytorch#179246 on behalf of https://github.com/aorenste due to breaking trunk ([comment](pytorch#179246 (comment)))
…9246) Closes pytorch#179236. For memory-bound kernels like RMSNorm, `max_autotune` selects worse configs than `default` mode when the workload runs under an externally captured CUDA graph. This happens because the autotuner's eager benchmarking includes host-side kernel launch overhead in its measurements, which distorts the ranking of configs that differ primarily in GPU-side execution time. This PR adds `autotune_cudagraph_benchmarking` — when enabled, the Triton config autotuner captures each candidate kernel in a CUDA graph and benchmarks graph replay instead of eager launches. This eliminates launch overhead from measurements, giving timings representative of CUDA graph replay execution. Enable via config or environment variable: ```python torch._inductor.config.autotune_cudagraph_benchmarking = True # or TORCHINDUCTOR_AUTOTUNE_CUDAGRAPH_BENCHMARKING=1 ``` On GB200 with the RMSNorm repro from the issue: | Config | Time | |---|---| | max_autotune (eager bench) | 6.19 us | | max_autotune (cudagraph bench) | **4.14 us** | | default compile | 4.13 us | The implementation reuses the existing `benchmark_gpu_with_cuda_graph` path (used by custom op autotuning) — `InductorBenchmarker.benchmark_gpu` delegates to it when the config flag is on, with fallback to eager benchmarking if CUDA graph capture fails. Pull Request resolved: pytorch#179246 Approved by: https://github.com/eellison, https://github.com/jansel
…9246) Closes pytorch#179236. For memory-bound kernels like RMSNorm, `max_autotune` selects worse configs than `default` mode when the workload runs under an externally captured CUDA graph. This happens because the autotuner's eager benchmarking includes host-side kernel launch overhead in its measurements, which distorts the ranking of configs that differ primarily in GPU-side execution time. This PR adds `autotune_cudagraph_benchmarking` — when enabled, the Triton config autotuner captures each candidate kernel in a CUDA graph and benchmarks graph replay instead of eager launches. This eliminates launch overhead from measurements, giving timings representative of CUDA graph replay execution. Enable via config or environment variable: ```python torch._inductor.config.autotune_cudagraph_benchmarking = True # or TORCHINDUCTOR_AUTOTUNE_CUDAGRAPH_BENCHMARKING=1 ``` On GB200 with the RMSNorm repro from the issue: | Config | Time | |---|---| | max_autotune (eager bench) | 6.19 us | | max_autotune (cudagraph bench) | **4.14 us** | | default compile | 4.13 us | The implementation reuses the existing `benchmark_gpu_with_cuda_graph` path (used by custom op autotuning) — `InductorBenchmarker.benchmark_gpu` delegates to it when the config flag is on, with fallback to eager benchmarking if CUDA graph capture fails. Pull Request resolved: pytorch#179246 Approved by: https://github.com/eellison, https://github.com/jansel
…9246) Closes pytorch#179236. For memory-bound kernels like RMSNorm, `max_autotune` selects worse configs than `default` mode when the workload runs under an externally captured CUDA graph. This happens because the autotuner's eager benchmarking includes host-side kernel launch overhead in its measurements, which distorts the ranking of configs that differ primarily in GPU-side execution time. This PR adds `autotune_cudagraph_benchmarking` — when enabled, the Triton config autotuner captures each candidate kernel in a CUDA graph and benchmarks graph replay instead of eager launches. This eliminates launch overhead from measurements, giving timings representative of CUDA graph replay execution. Enable via config or environment variable: ```python torch._inductor.config.autotune_cudagraph_benchmarking = True # or TORCHINDUCTOR_AUTOTUNE_CUDAGRAPH_BENCHMARKING=1 ``` On GB200 with the RMSNorm repro from the issue: | Config | Time | |---|---| | max_autotune (eager bench) | 6.19 us | | max_autotune (cudagraph bench) | **4.14 us** | | default compile | 4.13 us | The implementation reuses the existing `benchmark_gpu_with_cuda_graph` path (used by custom op autotuning) — `InductorBenchmarker.benchmark_gpu` delegates to it when the config flag is on, with fallback to eager benchmarking if CUDA graph capture fails. Pull Request resolved: pytorch#179246 Approved by: https://github.com/eellison
…torch#179246)" This reverts commit 4c61d4a. Reverted pytorch#179246 on behalf of https://github.com/aorenste due to breaking trunk ([comment](pytorch#179246 (comment)))
…9246) Closes pytorch#179236. For memory-bound kernels like RMSNorm, `max_autotune` selects worse configs than `default` mode when the workload runs under an externally captured CUDA graph. This happens because the autotuner's eager benchmarking includes host-side kernel launch overhead in its measurements, which distorts the ranking of configs that differ primarily in GPU-side execution time. This PR adds `autotune_cudagraph_benchmarking` — when enabled, the Triton config autotuner captures each candidate kernel in a CUDA graph and benchmarks graph replay instead of eager launches. This eliminates launch overhead from measurements, giving timings representative of CUDA graph replay execution. Enable via config or environment variable: ```python torch._inductor.config.autotune_cudagraph_benchmarking = True # or TORCHINDUCTOR_AUTOTUNE_CUDAGRAPH_BENCHMARKING=1 ``` On GB200 with the RMSNorm repro from the issue: | Config | Time | |---|---| | max_autotune (eager bench) | 6.19 us | | max_autotune (cudagraph bench) | **4.14 us** | | default compile | 4.13 us | The implementation reuses the existing `benchmark_gpu_with_cuda_graph` path (used by custom op autotuning) — `InductorBenchmarker.benchmark_gpu` delegates to it when the config flag is on, with fallback to eager benchmarking if CUDA graph capture fails. Pull Request resolved: pytorch#179246 Approved by: https://github.com/eellison, https://github.com/jansel
…UDA graphs (#200460) Author's note: With CUDA-graph autotune benchmarking, multi-kernel candidates launched on the wrong stream and nested lazy-autotune benchmarks were timed eagerly, so the wrong candidate could win. Each fix has a test that fails without it, and both match how `CachingAutotuner.bench` already handles the stream. The description below was drafted with an AI assistant (Claude): > With `max_autotune` and `autotune_cudagraph_benchmarking`, two bugs make > candidates that launch through multi-kernel, or that autotune on their first > launch, escape graph timing: they are timed eagerly with launch overhead, or > not at all. Either way the wrong candidate can win. > > 1. `MultiKernelCall.benchmark_sub_kernels` forwards the `stream` kwarg it was > called with to every candidate launch. The benchmarker captures the callable > on a side stream, so the launch escapes the capture: the graph is empty and > the candidate times as nearly free, or the capture errors and falls back to > eager timing. The stream is now resolved at call time, as > `CachingAutotuner.bench` already does (#179246). Outside of capture the > current stream is the one that was passed in, so eager benchmarking is > unchanged. > > 2. `InductorBenchmarker.benchmark_gpu` times GPU callables through > `benchmark_gpu_with_cuda_graph`: warmup, capture, then > `self.benchmark_gpu(cuda_graph.replay)`. `_in_cudagraph_benchmark` (a > thread-local depth since #194649) exists so that last call times the replay > directly instead of capturing it again, but the `InductorBenchmarker` > override raised it for the whole method, warmup included. A callable that autotunes on its first launch benchmarks other > callables during that warmup, and those nested benchmarks saw the flag and > were timed eagerly. Two callers hit this: > `MultiKernelCall.benchmark_sub_kernels`, which times `CachingAutotuner.run` > on sub-kernels that have not picked a config yet, and > `SubgraphChoiceCaller.benchmark`, which times a compiled module whose > kernels autotune on first call and calls `benchmark_gpu_with_cuda_graph` > directly. The thread-local depth now moves to the base `Benchmarker`, and > `benchmark_gpu_with_cuda_graph` raises it only around the replay timing; > the `InductorBenchmarker` override is dropped. > > An alternative is running the callable once before entering the graph path, > so lazy autotuning finishes first. That adds a launch to every graph-mode > benchmark and misses the direct `benchmark_gpu_with_cuda_graph` caller. > > Test Plan: > ``` > python -m pytest test/inductor/test_benchmarking.py test/inductor/test_multi_kernel.py test/inductor/test_static_triton_launcher.py test/inductor/test_custom_op_autotune.py > ``` > `test_benchmark_sub_kernels_captures_on_current_stream` captures a candidate in > a real CUDA graph and checks that replay runs it; it fails without fix 1. > `test_benchmark_gpu_with_cuda_graph_nested_benchmark_uses_graph` fails when the > flag covers the whole method (the nested benchmark is timed eagerly). On > MI300X, compiling a softmax with `triton.multi_kernel=1`, the 10 sub-kernel > config benchmarks run eagerly before this change and through graph capture > after it. > > This change was prepared with an AI assistant. Pull Request resolved: #200460 Approved by: https://github.com/drisspg ghstack dependencies: #200459
…UDA graphs (#200460) Author's note: With CUDA-graph autotune benchmarking, multi-kernel candidates launched on the wrong stream and nested lazy-autotune benchmarks were timed eagerly, so the wrong candidate could win. Each fix has a test that fails without it, and both match how `CachingAutotuner.bench` already handles the stream. The description below was drafted with an AI assistant (Claude): > With `max_autotune` and `autotune_cudagraph_benchmarking`, two bugs make > candidates that launch through multi-kernel, or that autotune on their first > launch, escape graph timing: they are timed eagerly with launch overhead, or > not at all. Either way the wrong candidate can win. > > 1. `MultiKernelCall.benchmark_sub_kernels` forwards the `stream` kwarg it was > called with to every candidate launch. The benchmarker captures the callable > on a side stream, so the launch escapes the capture: the graph is empty and > the candidate times as nearly free, or the capture errors and falls back to > eager timing. The stream is now resolved at call time, as > `CachingAutotuner.bench` already does (#179246). Outside of capture the > current stream is the one that was passed in, so eager benchmarking is > unchanged. > > 2. `InductorBenchmarker.benchmark_gpu` times GPU callables through > `benchmark_gpu_with_cuda_graph`: warmup, capture, then > `self.benchmark_gpu(cuda_graph.replay)`. `_in_cudagraph_benchmark` (a > thread-local depth since #194649) exists so that last call times the replay > directly instead of capturing it again, but the `InductorBenchmarker` > override raised it for the whole method, warmup included. A callable that autotunes on its first launch benchmarks other > callables during that warmup, and those nested benchmarks saw the flag and > were timed eagerly. Two callers hit this: > `MultiKernelCall.benchmark_sub_kernels`, which times `CachingAutotuner.run` > on sub-kernels that have not picked a config yet, and > `SubgraphChoiceCaller.benchmark`, which times a compiled module whose > kernels autotune on first call and calls `benchmark_gpu_with_cuda_graph` > directly. The thread-local depth now moves to the base `Benchmarker`, and > `benchmark_gpu_with_cuda_graph` raises it only around the replay timing; > the `InductorBenchmarker` override is dropped. > > An alternative is running the callable once before entering the graph path, > so lazy autotuning finishes first. That adds a launch to every graph-mode > benchmark and misses the direct `benchmark_gpu_with_cuda_graph` caller. > > Test Plan: > ``` > python -m pytest test/inductor/test_benchmarking.py test/inductor/test_multi_kernel.py test/inductor/test_static_triton_launcher.py test/inductor/test_custom_op_autotune.py > ``` > `test_benchmark_sub_kernels_captures_on_current_stream` captures a candidate in > a real CUDA graph and checks that replay runs it; it fails without fix 1. > `test_benchmark_gpu_with_cuda_graph_nested_benchmark_uses_graph` fails when the > flag covers the whole method (the nested benchmark is timed eagerly). On > MI300X, compiling a softmax with `triton.multi_kernel=1`, the 10 sub-kernel > config benchmarks run eagerly before this change and through graph capture > after it. > > This change was prepared with an AI assistant. Pull Request resolved: #200460 Approved by: https://github.com/drisspg ghstack dependencies: #200459
…UDA graphs (pytorch#200460) Author's note: With CUDA-graph autotune benchmarking, multi-kernel candidates launched on the wrong stream and nested lazy-autotune benchmarks were timed eagerly, so the wrong candidate could win. Each fix has a test that fails without it, and both match how `CachingAutotuner.bench` already handles the stream. The description below was drafted with an AI assistant (Claude): > With `max_autotune` and `autotune_cudagraph_benchmarking`, two bugs make > candidates that launch through multi-kernel, or that autotune on their first > launch, escape graph timing: they are timed eagerly with launch overhead, or > not at all. Either way the wrong candidate can win. > > 1. `MultiKernelCall.benchmark_sub_kernels` forwards the `stream` kwarg it was > called with to every candidate launch. The benchmarker captures the callable > on a side stream, so the launch escapes the capture: the graph is empty and > the candidate times as nearly free, or the capture errors and falls back to > eager timing. The stream is now resolved at call time, as > `CachingAutotuner.bench` already does (pytorch#179246). Outside of capture the > current stream is the one that was passed in, so eager benchmarking is > unchanged. > > 2. `InductorBenchmarker.benchmark_gpu` times GPU callables through > `benchmark_gpu_with_cuda_graph`: warmup, capture, then > `self.benchmark_gpu(cuda_graph.replay)`. `_in_cudagraph_benchmark` (a > thread-local depth since pytorch#194649) exists so that last call times the replay > directly instead of capturing it again, but the `InductorBenchmarker` > override raised it for the whole method, warmup included. A callable that autotunes on its first launch benchmarks other > callables during that warmup, and those nested benchmarks saw the flag and > were timed eagerly. Two callers hit this: > `MultiKernelCall.benchmark_sub_kernels`, which times `CachingAutotuner.run` > on sub-kernels that have not picked a config yet, and > `SubgraphChoiceCaller.benchmark`, which times a compiled module whose > kernels autotune on first call and calls `benchmark_gpu_with_cuda_graph` > directly. The thread-local depth now moves to the base `Benchmarker`, and > `benchmark_gpu_with_cuda_graph` raises it only around the replay timing; > the `InductorBenchmarker` override is dropped. > > An alternative is running the callable once before entering the graph path, > so lazy autotuning finishes first. That adds a launch to every graph-mode > benchmark and misses the direct `benchmark_gpu_with_cuda_graph` caller. > > Test Plan: > ``` > python -m pytest test/inductor/test_benchmarking.py test/inductor/test_multi_kernel.py test/inductor/test_static_triton_launcher.py test/inductor/test_custom_op_autotune.py > ``` > `test_benchmark_sub_kernels_captures_on_current_stream` captures a candidate in > a real CUDA graph and checks that replay runs it; it fails without fix 1. > `test_benchmark_gpu_with_cuda_graph_nested_benchmark_uses_graph` fails when the > flag covers the whole method (the nested benchmark is timed eagerly). On > MI300X, compiling a softmax with `triton.multi_kernel=1`, the 10 sub-kernel > config benchmarks run eagerly before this change and through graph capture > after it. > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200460 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459
…UDA graphs (pytorch#200460) Author's note: With CUDA-graph autotune benchmarking, multi-kernel candidates launched on the wrong stream and nested lazy-autotune benchmarks were timed eagerly, so the wrong candidate could win. Each fix has a test that fails without it, and both match how `CachingAutotuner.bench` already handles the stream. The description below was drafted with an AI assistant (Claude): > With `max_autotune` and `autotune_cudagraph_benchmarking`, two bugs make > candidates that launch through multi-kernel, or that autotune on their first > launch, escape graph timing: they are timed eagerly with launch overhead, or > not at all. Either way the wrong candidate can win. > > 1. `MultiKernelCall.benchmark_sub_kernels` forwards the `stream` kwarg it was > called with to every candidate launch. The benchmarker captures the callable > on a side stream, so the launch escapes the capture: the graph is empty and > the candidate times as nearly free, or the capture errors and falls back to > eager timing. The stream is now resolved at call time, as > `CachingAutotuner.bench` already does (pytorch#179246). Outside of capture the > current stream is the one that was passed in, so eager benchmarking is > unchanged. > > 2. `InductorBenchmarker.benchmark_gpu` times GPU callables through > `benchmark_gpu_with_cuda_graph`: warmup, capture, then > `self.benchmark_gpu(cuda_graph.replay)`. `_in_cudagraph_benchmark` (a > thread-local depth since pytorch#194649) exists so that last call times the replay > directly instead of capturing it again, but the `InductorBenchmarker` > override raised it for the whole method, warmup included. A callable that autotunes on its first launch benchmarks other > callables during that warmup, and those nested benchmarks saw the flag and > were timed eagerly. Two callers hit this: > `MultiKernelCall.benchmark_sub_kernels`, which times `CachingAutotuner.run` > on sub-kernels that have not picked a config yet, and > `SubgraphChoiceCaller.benchmark`, which times a compiled module whose > kernels autotune on first call and calls `benchmark_gpu_with_cuda_graph` > directly. The thread-local depth now moves to the base `Benchmarker`, and > `benchmark_gpu_with_cuda_graph` raises it only around the replay timing; > the `InductorBenchmarker` override is dropped. > > An alternative is running the callable once before entering the graph path, > so lazy autotuning finishes first. That adds a launch to every graph-mode > benchmark and misses the direct `benchmark_gpu_with_cuda_graph` caller. > > Test Plan: > ``` > python -m pytest test/inductor/test_benchmarking.py test/inductor/test_multi_kernel.py test/inductor/test_static_triton_launcher.py test/inductor/test_custom_op_autotune.py > ``` > `test_benchmark_sub_kernels_captures_on_current_stream` captures a candidate in > a real CUDA graph and checks that replay runs it; it fails without fix 1. > `test_benchmark_gpu_with_cuda_graph_nested_benchmark_uses_graph` fails when the > flag covers the whole method (the nested benchmark is timed eagerly). On > MI300X, compiling a softmax with `triton.multi_kernel=1`, the 10 sub-kernel > config benchmarks run eagerly before this change and through graph capture > after it. > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200460 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459
…UDA graphs (pytorch#200460) Author's note: With CUDA-graph autotune benchmarking, multi-kernel candidates launched on the wrong stream and nested lazy-autotune benchmarks were timed eagerly, so the wrong candidate could win. Each fix has a test that fails without it, and both match how `CachingAutotuner.bench` already handles the stream. The description below was drafted with an AI assistant (Claude): > With `max_autotune` and `autotune_cudagraph_benchmarking`, two bugs make > candidates that launch through multi-kernel, or that autotune on their first > launch, escape graph timing: they are timed eagerly with launch overhead, or > not at all. Either way the wrong candidate can win. > > 1. `MultiKernelCall.benchmark_sub_kernels` forwards the `stream` kwarg it was > called with to every candidate launch. The benchmarker captures the callable > on a side stream, so the launch escapes the capture: the graph is empty and > the candidate times as nearly free, or the capture errors and falls back to > eager timing. The stream is now resolved at call time, as > `CachingAutotuner.bench` already does (pytorch#179246). Outside of capture the > current stream is the one that was passed in, so eager benchmarking is > unchanged. > > 2. `InductorBenchmarker.benchmark_gpu` times GPU callables through > `benchmark_gpu_with_cuda_graph`: warmup, capture, then > `self.benchmark_gpu(cuda_graph.replay)`. `_in_cudagraph_benchmark` (a > thread-local depth since pytorch#194649) exists so that last call times the replay > directly instead of capturing it again, but the `InductorBenchmarker` > override raised it for the whole method, warmup included. A callable that autotunes on its first launch benchmarks other > callables during that warmup, and those nested benchmarks saw the flag and > were timed eagerly. Two callers hit this: > `MultiKernelCall.benchmark_sub_kernels`, which times `CachingAutotuner.run` > on sub-kernels that have not picked a config yet, and > `SubgraphChoiceCaller.benchmark`, which times a compiled module whose > kernels autotune on first call and calls `benchmark_gpu_with_cuda_graph` > directly. The thread-local depth now moves to the base `Benchmarker`, and > `benchmark_gpu_with_cuda_graph` raises it only around the replay timing; > the `InductorBenchmarker` override is dropped. > > An alternative is running the callable once before entering the graph path, > so lazy autotuning finishes first. That adds a launch to every graph-mode > benchmark and misses the direct `benchmark_gpu_with_cuda_graph` caller. > > Test Plan: > ``` > python -m pytest test/inductor/test_benchmarking.py test/inductor/test_multi_kernel.py test/inductor/test_static_triton_launcher.py test/inductor/test_custom_op_autotune.py > ``` > `test_benchmark_sub_kernels_captures_on_current_stream` captures a candidate in > a real CUDA graph and checks that replay runs it; it fails without fix 1. > `test_benchmark_gpu_with_cuda_graph_nested_benchmark_uses_graph` fails when the > flag covers the whole method (the nested benchmark is timed eagerly). On > MI300X, compiling a softmax with `triton.multi_kernel=1`, the 10 sub-kernel > config benchmarks run eagerly before this change and through graph capture > after it. > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200460 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459
…UDA graphs (pytorch#200460) Author's note: With CUDA-graph autotune benchmarking, multi-kernel candidates launched on the wrong stream and nested lazy-autotune benchmarks were timed eagerly, so the wrong candidate could win. Each fix has a test that fails without it, and both match how `CachingAutotuner.bench` already handles the stream. The description below was drafted with an AI assistant (Claude): > With `max_autotune` and `autotune_cudagraph_benchmarking`, two bugs make > candidates that launch through multi-kernel, or that autotune on their first > launch, escape graph timing: they are timed eagerly with launch overhead, or > not at all. Either way the wrong candidate can win. > > 1. `MultiKernelCall.benchmark_sub_kernels` forwards the `stream` kwarg it was > called with to every candidate launch. The benchmarker captures the callable > on a side stream, so the launch escapes the capture: the graph is empty and > the candidate times as nearly free, or the capture errors and falls back to > eager timing. The stream is now resolved at call time, as > `CachingAutotuner.bench` already does (pytorch#179246). Outside of capture the > current stream is the one that was passed in, so eager benchmarking is > unchanged. > > 2. `InductorBenchmarker.benchmark_gpu` times GPU callables through > `benchmark_gpu_with_cuda_graph`: warmup, capture, then > `self.benchmark_gpu(cuda_graph.replay)`. `_in_cudagraph_benchmark` (a > thread-local depth since pytorch#194649) exists so that last call times the replay > directly instead of capturing it again, but the `InductorBenchmarker` > override raised it for the whole method, warmup included. A callable that autotunes on its first launch benchmarks other > callables during that warmup, and those nested benchmarks saw the flag and > were timed eagerly. Two callers hit this: > `MultiKernelCall.benchmark_sub_kernels`, which times `CachingAutotuner.run` > on sub-kernels that have not picked a config yet, and > `SubgraphChoiceCaller.benchmark`, which times a compiled module whose > kernels autotune on first call and calls `benchmark_gpu_with_cuda_graph` > directly. The thread-local depth now moves to the base `Benchmarker`, and > `benchmark_gpu_with_cuda_graph` raises it only around the replay timing; > the `InductorBenchmarker` override is dropped. > > An alternative is running the callable once before entering the graph path, > so lazy autotuning finishes first. That adds a launch to every graph-mode > benchmark and misses the direct `benchmark_gpu_with_cuda_graph` caller. > > Test Plan: > ``` > python -m pytest test/inductor/test_benchmarking.py test/inductor/test_multi_kernel.py test/inductor/test_static_triton_launcher.py test/inductor/test_custom_op_autotune.py > ``` > `test_benchmark_sub_kernels_captures_on_current_stream` captures a candidate in > a real CUDA graph and checks that replay runs it; it fails without fix 1. > `test_benchmark_gpu_with_cuda_graph_nested_benchmark_uses_graph` fails when the > flag covers the whole method (the nested benchmark is timed eagerly). On > MI300X, compiling a softmax with `triton.multi_kernel=1`, the 10 sub-kernel > config benchmarks run eagerly before this change and through graph capture > after it. > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200460 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459
Closes #179236.
For memory-bound kernels like RMSNorm,
max_autotuneselects worse configs thandefaultmode when the workload runs under an externally captured CUDA graph. This happens because the autotuner's eager benchmarking includes host-side kernel launch overhead in its measurements, which distorts the ranking of configs that differ primarily in GPU-side execution time.This PR adds
autotune_cudagraph_benchmarking— when enabled, the Triton config autotuner captures each candidate kernel in a CUDA graph and benchmarks graph replay instead of eager launches. This eliminates launch overhead from measurements, giving timings representative of CUDA graph replay execution.Enable via config or environment variable:
On GB200 with the RMSNorm repro from the issue:
The implementation reuses the existing
benchmark_gpu_with_cuda_graphpath (used by custom op autotuning) —InductorBenchmarker.benchmark_gpudelegates to it when the config flag is on, with fallback to eager benchmarking if CUDA graph capture fails.cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo