Skip to content

[Inductor] max_autotune benchmark runs under CUDA Graph - #179246

Closed
mattteochen wants to merge 12 commits into
pytorch:mainfrom
mattteochen:max-autotune-cg-bench
Closed

mattteochen wants to merge 12 commits into
pytorch:mainfrom
mattteochen:max-autotune-cg-bench

Conversation

@mattteochen

@mattteochen mattteochen commented Apr 3, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #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:

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.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

@pytorch-bot

pytorch-bot Bot commented Apr 3, 2026 •

Copy link
Copy Markdown

🔗 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 Failures

As of commit 9d357f4 with merge base a702838 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@mattteochen

Copy link
Copy Markdown
Collaborator Author

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Apr 3, 2026
Comment thread torch/_inductor/runtime/benchmarking.py Outdated

def __init__(self: Self) -> None:
pass
self._in_cudagraph_benchmark = False

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 → recursion

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mikaylagawarecki mikaylagawarecki added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Apr 6, 2026
@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests labels Apr 14, 2026
@pytorch-bot

pytorch-bot Bot commented Apr 14, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@pytorch-bot

pytorch-bot Bot commented Apr 14, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/inductor
  • ciflow/torchtitan

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
eellison previously approved these changes Apr 20, 2026

@eellison eellison left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! cc @shunting314 @PaulZhang12

Comment thread torch/_inductor/runtime/benchmarking.py Resolved
Comment thread torch/_inductor/runtime/benchmarking.py Resolved
@mattteochen

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -f “unrelated failures”

@pytorch-bot

pytorch-bot Bot commented Apr 28, 2026

Copy link
Copy Markdown

❌ 🤖 pytorchbot command failed:

@pytorchbot: error: unrecognized arguments: failures”

usage: @pytorchbot [-h]
                   
                   {merge,revert,rebase,label,drci,lint,fix-lint,apply-lint,cherry-pick}
                   ...

Try @pytorchbot --help for more info.

@crcrpar

crcrpar commented Apr 28, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -f "unrelated failures"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@aorenste

Copy link
Copy Markdown
Contributor

Sorry - this is breaking:

python test/inductor/test_custom_op_autotune.py TestCustomOpAutoTune.test_benchmark_with_cudagraphs_uses_cuda_graph_benchmarking

@pytorchbot revert -m="breaking trunk" -c=nosignal

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@mattteochen your PR has been successfully reverted.

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased max-autotune-cg-bench onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout max-autotune-cg-bench && git pull --rebase)

@pytorchmergebot
pytorchmergebot force-pushed the max-autotune-cg-bench branch from a914e80 to 9d357f4 Compare July 2, 2026 01:37
@mattteochen

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jul 5, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…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
vishalgoyal316 pushed a commit to vishalgoyal316/pytorch that referenced this pull request Jul 16, 2026
…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
DamJanusz pushed a commit to DamJanusz/pytorch that referenced this pull request Jul 21, 2026
…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
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…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
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…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
pytorchmergebot pushed a commit that referenced this pull request Nov 2, 2026
…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
pytorchmergebot pushed a commit that referenced this pull request Nov 3, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged module: inductor open source Reverted topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Inductor] Autotune should ignore host launch latency when requested

10 participants