Skip to content

[tunableop] harden compile-time GEMM routing and warn when caches hide the pass - #203284

Open
ruddhic wants to merge 25 commits into
gh/ruddhic/7/basefrom
gh/ruddhic/7/head
Open

ruddhic wants to merge 25 commits into
gh/ruddhic/7/basefrom
gh/ruddhic/7/head

Conversation

@ruddhic

@ruddhic ruddhic commented Nov 3, 2026 •

Copy link
Copy Markdown

Stack from ghstack (oldest at bottom):

Follow-up fixes for the compile-time TunableOp stack. Each of the four PRs
below is at GitHub's limit on lines added per PR, so fixes found later sit in
this diff on top. Some of them fix code those diffs own; they are listed by
owner at the end.

An earlier version bypassed FxGraphCache, AOTAutogradCache and the local
autotune cache. That was replaced by a warning (item 1), and this summary now
describes the code as it is.

Suggested review order:

  1. Caches and pipelining (tunableop.py, autotune_process.py, config.py).
    Both passes work during lowering and autotune, so a cache hit skips them. The
    caches are not bypassed, because suppressing them costs every graph the entry
    that standalone_compile and precompile build their artifact from. Instead,
    _warn_if_caching_enabled warns once per mode unless force_disable_caches
    is set. For aot_compile and precompile, which reject that flag, it suggests a
    fresh TORCHINDUCTOR_CACHE_DIR. Known gap: the warning is emitted from
    lowering/autotune, so a run where every graph is a cache hit gets no warning.
    Pipelined autotuning is turned off while a mode is set: that path benchmarks
    the bmreq directly and never reaches the routing.

  2. Routing hardening in ExternKernelCaller.benchmark (select_algorithm.py,
    tunableop.py). Once a mode is set and the candidate is a GEMM TunableOp tunes,
    every exit that times inline does so with tuning off, not only the
    post-fault one. Three gates keep candidates the worker cannot benchmark out
    of it: a circuit breaker (routing_is_open, after 3 consecutive faults),
    metas that failed to build (output_tensor_meta), and VERIFY. The breaker
    is one process-wide counter shared by every device's worker and resets only
    on a fresh cache, so with default caching it lasts for the rest of the
    process. submit() also treats a bool result as a fault, since bool is an
    int and True would pass as a 1 ms timing.

  3. Behaviour change in borrow validation (TunableOp.h). When validating a
    borrow throws c10::Error, ResolveWithoutExactHit used to return the
    wildcard (or Null). It now reports the shape untuned and returns Default,
    the same answer as the no-donor and stream-capture exits.
    AcceleratorError and OutOfMemoryError are reported untuned and
    rethrown, so the worker is torn down. This changes code added in
    D116493590.

  4. Non-OK logging (TunableOp.h). A tuned solution that fails at runtime
    returns non-OK and every caller falls back silently; the failing solution is
    now named at TUNABLE_LOG2. Previously reviewed as D128384389.

  5. Linking. StreamTimer, StreamTimerNoSync and ResultEntry's
    operator<< are exported with TORCH_CUDA_CPP_API, which the TunableOp.h
    templates need to link from the new gtests outside libtorch_cuda.

Fixes to earlier diffs in the stack:

  • D116493593: FlagGuard in cuda_tunable_nearest_test.cpp snapshotted
    IsNearestMatchEnabled(), which folds in recording, so its restore could
    clear the raw setting. The kill-switch test in test_linalg.py now also
    checks the pristine defaults.
  • D116493592: _warn_fault_once attached exc_info even outside an except
    block.
  • D116493595: the rotating-buffer restore in test cleanup (the getter returns
    bytes, the setter takes MiB) and the _Job.measure test stub.
  • D116493590: the ResolveWithoutExactHit exits in item 3 and the
    ExternKernelCaller.benchmark inline fallback in item 2.

Differential Revision: D129441755

Differential Revision: D129441755

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:

The PR stops Inductor's compile-time TunableOp "tune" and "reuse" passes from
silently doing nothing. It adds a caching warning, a routing breaker,
inline-fallback tuning gates, and C++ borrow-validation fallback and logging
changes, and it turns pipelined autotuning off while a mode is set.
Recommendation: Approve. Every point is non-blocking, but before landing the
review asks to fix the config.py comment, the title and the description, and to
add ciflow/trunk.

Findings (15):

[minor] torch/_inductor/config.py:3124
Non-blocking: the title, the first two description bullets and the
"routes_to_worker mirrors the gate" paragraph describe an
FxGraphCache/AOTAutogradCache bypass, an autotune local-cache bypass and a
`routes_to_worker` helper. None of these exist at HEAD: `routes_to_worker` is
not defined anywhere, and codecache.py, compile_fx.py and torch/_functorch never
mention tunableop. This comment ("a graph that routes a CUDA GEMM skips the FX
and autotune caches while a mode is set") says the same thing and contradicts
the `_warn_if_caching_enabled` docstring ("the caches are left alone
deliberately"). Only the pipelined-autotuning clause is accurate. The
description becomes the commit message, yet it leaves out what the PR actually
adds: the routing breaker, the metas/VERIFY inline gates, rejecting `bool`
results in `submit()`, and the C++ change. Fix the comment, the title and the
description before landing.

[minor] aten/src/ATen/cuda/tunable/TunableOp.h:302
Non-blocking: this C++ behaviour change is undocumented. A failed borrow
validation now returns `Default` where it used to return the wildcard fallback.
Direct users of `_nearest_match_enable(True)` see the change, and the
description doesn't mention it. The new comment "a donor that was not measured
is never served" also doesn't hold for the worker's measured call, which still
dispatches the unvalidated wildcard through the tail.

[minor] test/test_linalg.py:12174
Non-blocking: with the current labels, CI runs neither the new C++ FakeOp tests
(cuda_tunable_nearest_test.cpp, reached only through test_aten) nor the new
TestLinalgCudaOnly tests. ciflow/inductor and ciflow/torchtitan don't run them
either. Adding ciflow/trunk would run both on CUDA and ROCm. Without it, nothing
shows that the dropped `#ifdef` and the new `TORCH_CUDA_CPP_API` exports link
and pass on either GPU build. ciflow/inductor also runs the changed
test/inductor/test_tunableop_*.py files on CUDA only, although hipBLASLt is the
main target of the routing work. Add ciflow/trunk before landing.

[minor] test/test_linalg.py:12174
Non-blocking: `test_unusable_donor_is_never_benchmarked_tunableop` only asserts
that two strings are absent, so it still passes if the child never reaches the
borrow path. The child already runs at VERBOSE=3, so add a positive check on the
LOG3 `nearest match for ... ->` line.

[minor] test/inductor/test_tunableop_pretune.py:978
Non-blocking leftovers: `_gemm_gm` (:936) is never called. `@​
instantiate_parametrized_tests` on `TunableOpPretuneRealGpuTest` (:1009) does
nothing, because the class has no `@​ parametrize`.
`tunableop._env_warned.clear()` at :978 and :996 has no cleanup, so `caching:*`
entries leak into later tests. Use
`self.addCleanup(tunableop._env_warned.clear)`, as test_tunableop_worker.py:669
does.

[minor] test/inductor/test_tunableop_pretune.py:993
Non-blocking: the test name claims more than the test checks.
`test_the_caching_warning_is_emitted_once_per_mode` only patches mode "tune". It
would still pass if the memo key at tunableop.py:131 dropped the mode, which
would let a tune warning silence the reuse warning. Exercise both modes in one
process.

(9 more not shown, see the review run)

Reviewed commit: 79b6e11 (NOT the current head 63042d5)

Review run

🐛 Report a bad review

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/203284

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 Pending, 1 Unclassified Failure

As of commit 63042d5 with merge base 836c141 (image):

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

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

github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
ruddhic added a commit that referenced this pull request Nov 3, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 464193091
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
ruddhic added a commit that referenced this pull request Nov 4, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 464255156
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
ruddhic added a commit that referenced this pull request Nov 4, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 464330524
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added the release notes: linalg_frontend release notes category label Nov 4, 2026
ruddhic added a commit that referenced this pull request Nov 4, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 464475902
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
github-actions[bot]

This comment was marked as outdated.

ruddhic added a commit that referenced this pull request Nov 10, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 469352089
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
ruddhic added a commit that referenced this pull request Nov 10, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 469380747
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
github-actions[bot]

This comment was marked as outdated.

[ghstack-poisoned]
ruddhic added a commit that referenced this pull request Nov 10, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 469402204
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
github-actions[bot]

This comment was marked as outdated.

@github-actions
github-actions Bot dismissed stale reviews from themself November 10, 2026 16:03

Withdrawn: the automated review of 46bee2c found nothing blocking.

ruddhic added a commit that referenced this pull request Nov 13, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 469402204
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
ruddhic added a commit that referenced this pull request Nov 13, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 469402204
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
ruddhic added a commit that referenced this pull request Nov 13, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 469402204
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
[ghstack-poisoned]
ruddhic added a commit that referenced this pull request Nov 17, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 476467819
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)

@github-actions github-actions Bot 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.

Automated review of 79b6e11: nothing blocking; ready for human review.

The PR stops Inductor's compile-time TunableOp "tune" and "reuse" passes from silently doing nothing. It adds a caching warning, a routing breaker, inline-fallback tuning gates, and C++ borrow-validation fallback and logging changes, and it turns pipelined autotuning off while a mode is set. Recommendation: Approve. Every point is non-blocking, but before landing the review asks to fix the config.py comment, the title and the description, and to add ciflow/trunk.

⚪ 15 non-blocking findings

# lowering, so a graph that routes a CUDA GEMM skips the FX and autotune

Non-blocking: the title, the first two description bullets and the "routes_to_worker mirrors the gate" paragraph describe an FxGraphCache/AOTAutogradCache bypass, an autotune local-cache bypass and a routes_to_worker helper. None of these exist at HEAD: routes_to_worker is not defined anywhere, and codecache.py, compile_fx.py and torch/_functorch never mention tunableop. This comment ("a graph that routes a CUDA GEMM skips the FX and autotune caches while a mode is set") says the same thing and contradicts the _warn_if_caching_enabled docstring ("the caches are left alone deliberately"). Only the pipelined-autotuning clause is accurate. The description becomes the commit message, yet it leaves out what the PR actually adds: the routing breaker, the metas/VERIFY inline gates, rejecting bool results in submit(), and the C++ change. Fix the comment, the title and the description before landing.


catch (const c10::Error& e) {

Non-blocking: this C++ behaviour change is undocumented. A failed borrow validation now returns Default where it used to return the wildcard fallback. Direct users of _nearest_match_enable(True) see the change, and the description doesn't mention it. The new comment "a donor that was not measured is never served" also doesn't hold for the worker's measured call, which still dispatches the unvalidated wildcard through the tail.


def test_unusable_donor_is_never_benchmarked_tunableop(self, device, dtype):

Non-blocking: with the current labels, CI runs neither the new C++ FakeOp tests (cuda_tunable_nearest_test.cpp, reached only through test_aten) nor the new TestLinalgCudaOnly tests. ciflow/inductor and ciflow/torchtitan don't run them either. Adding ciflow/trunk would run both on CUDA and ROCm. Without it, nothing shows that the dropped #ifdef and the new TORCH_CUDA_CPP_API exports link and pass on either GPU build. ciflow/inductor also runs the changed test/inductor/test_tunableop_*.py files on CUDA only, although hipBLASLt is the main target of the routing work. Add ciflow/trunk before landing.


def test_unusable_donor_is_never_benchmarked_tunableop(self, device, dtype):

Non-blocking: test_unusable_donor_is_never_benchmarked_tunableop only asserts that two strings are absent, so it still passes if the child never reaches the borrow path. The child already runs at VERBOSE=3, so add a positive check on the LOG3 nearest match for ... -&gt; line.


tunableop._env_warned.clear()

Non-blocking leftovers: _gemm_gm (:936) is never called. @ instantiate_parametrized_tests on TunableOpPretuneRealGpuTest (:1009) does nothing, because the class has no @ parametrize. tunableop._env_warned.clear() at :978 and :996 has no cleanup, so caching:* entries leak into later tests. Use self.addCleanup(tunableop._env_warned.clear), as test_tunableop_worker.py:669 does.


def test_the_caching_warning_is_emitted_once_per_mode(self):

Non-blocking: the test name claims more than the test checks. test_the_caching_warning_is_emitted_once_per_mode only patches mode "tune". It would still pass if the memo key at tunableop.py:131 dropped the mode, which would let a tune warning silence the reuse warning. Exercise both modes in one process.


env.pop("PYTHONPATH", None)

Non-blocking: the env.pop("PYTHONPATH") / env.pop("PYTHONHOME") additions copy an existing pattern, but the reasoning is backwards. The child is the same sys.executable, and the parent imported torch with that same PYTHONPATH. Removing it breaks setups where torch is importable only through PYTHONPATH, and check=True then raises.


Non-blocking: the routing breaker's scope and accounting don't match their comments. Scope: this warning says routing is off "for the rest of this compile". The breaker is reset only by a routed success or by clear_caches()/fresh_cache(), and no routed success can happen once it trips. So in-process it stays tripped for the rest of the process, while compile_fx_subproc resets it every compile. Accounting: note_routing_outcome counts every non-ok status, including submit() "error" returns that never kill the worker (:571-576, :620-622, :626-635). That contradicts "each fault costs a forced worker kill" at :659-660. Fix: reset the breaker per compile or reword the warning, and count only faults that killed the worker.


def _warn_if_caching_enabled(consequence: str) -> None:

Non-blocking: the caching warning can't fire on the run it exists for. _warn_if_caching_enabled is reached only through is_tune_mode/is_reuse_mode, during lowering or autotuning. An AOTAutogradCache hit, or by default an FxGraphCache hit, never lowers, so a tune pass over a fully warm cache records nothing and logs nothing. The exception is pipeline_max_autotune_gemm=1, where compile_fx.py:1144 runs it before the FxGraphCache lookup. Check once at compile entry, before the AOTAutograd cache lookup, whenever a mode is set.


if torch.compiler.config.force_disable_caches:

Non-blocking: the caching warning checks too little and is worded for tune only. :129-130 checks only force_disable_caches, so a user who turned off the FX, AOTAutograd and autotune caches one by one is still told to set TORCHINDUCTOR_FORCE_DISABLE_CACHES=1. The shared message at :134-138 says "for a tuning run" even when it fires from reuse mode.


Routed GEMMs are timed in the worker so hipBLASLt tuning cannot abort

Non-blocking: the ExternKernelCaller.benchmark docstring overstates what the fast path does. It says "every other path times inline with tuning off", but the first early return (:3997-4002: mode off, non-CUDA output, or a non-GEMM choice) calls _benchmark_inline without touching the tuning flags. Only the routable-GEMM fallback (:4051-4060) turns tuning off.


loses. It borrows only where Inductor actually benchmarks an aten GEMM, so a

Non-blocking: the docs list only one cache. This section gives only an autotune-cache hit as the reason reuse does nothing. It leaves out FxGraphCache and AOTAutogradCache hits, which skip lowering entirely, and it doesn't give the force-disable advice the warning gives. config.py:3128-3130 and the is_reuse_mode docstring (tunableop.py:155-157) leave out the same two caches.


friend TORCH_CUDA_CPP_API std::ostream& operator<<(

Non-blocking stale comment: cuda_tunable_nearest_test.cpp:41-45 still says the friend operator&lt;&lt; "is not exported and the test fails to link". Tunable.h:50 now exports it, and lines 168-172 of the test file say so. Update the test comment.


Non-blocking: the comment at :237-247 is wrong. It says that without the override, the unusable-donor test "passes for the wrong reason". In fact AnUnregisteredDonorIsNeverBenchmarked takes the same !RegisterOpForResult early return either way, so it can't detect a broken override. The GetOp(...) != nullptr check at TunableOp.h:204 never decides anything on CUDA.


// The race itself. Replace FindFastestAmong with the borrow and neither

Non-blocking: the comment is wrong and the assertion is weak. :301-302 says a trusted-borrow mutant would call "neither candidate", but operator() still dispatches the donor once, so EXPECT_GT(op.donor_op()-&gt;calls(), 0) at :318 passes in that mutant. Fix the comment, or tighten the check to &gt; 1 as the test at :377 already does.

This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review

@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Nov 18, 2026
[ghstack-poisoned]
ruddhic added a commit that referenced this pull request Nov 18, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 477482144
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
ruddhic added a commit that referenced this pull request Nov 19, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 477482144
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
@aorenste
aorenste removed their request for review November 30, 2026 22:25
[ghstack-poisoned]
ruddhic added a commit that referenced this pull request Dec 1, 2026
… work

Pull Request resolved: #203284

Inductor caches a compile at three levels, and all three can skip the work the
"tune" and "reuse" passes exist to do. Tuning and borrow validation happen
inside benchmark(), so a cache hit does neither and the pass silently records
nothing.

- FxGraphCache and AOTAutogradCache: bypassed while a mode is set, but only for
  a graph that actually routes a CUDA GEMM. Bypassing on the mode alone
  recompiles CPU-only and GEMM-free graphs every time for no benefit.
- The local autotune cache: neither read nor written for a routed GEMM. An
  empty cache rather than an early return, so the missing-choices assertion
  still runs and the precompile lookup misses too -- otherwise its choices are
  skipped and then compiled one at a time. Not written either: the timing is of
  a tuned kernel a plain run would not get, and update_local_cache rewrites the
  whole file, so a partial dict would discard the machine's cache.
- Pipelined autotuning: turned off, because that path benchmarks the bmreq
  directly and never reaches ExternKernelCaller.benchmark where routing lives.
  Routing into that pool is not an option -- TunableOp's flags are
  process-global and the pool is shared.

routes_to_worker mirrors the gate in ExternKernelCaller.benchmark, device
included: a CPU extern candidate is timed inline and keeps its cache entry.

Also folds in the non-OK logging previously sent separately: a tuned solution
that fails at runtime returns non-OK and every caller falls back silently, so
the failing candidate is never named.
ghstack-source-id: 486905202
@exported-using-ghexport

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
[ghstack-poisoned]
ruddhic added a commit that referenced this pull request Dec 2, 2026
…e the pass

Pull Request resolved: #203284

Follow-up fixes for the compile-time TunableOp stack. Each of the four PRs
below is at GitHub's limit on lines added per PR, so fixes found later sit in
this diff on top. Some of them fix code those diffs own; they are listed by
owner at the end.

An earlier version bypassed FxGraphCache, AOTAutogradCache and the local
autotune cache. That was replaced by a warning (item 1), and this summary now
describes the code as it is.

Suggested review order:

1. Caches and pipelining (tunableop.py, autotune_process.py, config.py).
Both passes work during lowering and autotune, so a cache hit skips them. The
caches are not bypassed, because suppressing them costs every graph the entry
that standalone_compile and precompile build their artifact from. Instead,
`_warn_if_caching_enabled` warns once per mode unless `force_disable_caches`
is set. For aot_compile and precompile, which reject that flag, it suggests a
fresh `TORCHINDUCTOR_CACHE_DIR`. Known gap: the warning is emitted from
lowering/autotune, so a run where every graph is a cache hit gets no warning.
Pipelined autotuning is turned off while a mode is set: that path benchmarks
the bmreq directly and never reaches the routing.

2. Routing hardening in `ExternKernelCaller.benchmark` (select_algorithm.py,
tunableop.py). Once a mode is set and the candidate is a GEMM TunableOp tunes,
every exit that times inline does so with tuning off, not only the
post-fault one. Three gates keep candidates the worker cannot benchmark out
of it: a circuit breaker (`routing_is_open`, after 3 consecutive faults),
metas that failed to build (`output_tensor_meta`), and `VERIFY`. The breaker
is one process-wide counter shared by every device's worker and resets only
on a fresh cache, so with default caching it lasts for the rest of the
process. `submit()` also treats a bool result as a fault, since bool is an
int and True would pass as a 1 ms timing.

3. Behaviour change in borrow validation (TunableOp.h). When validating a
borrow throws `c10::Error`, `ResolveWithoutExactHit` used to return the
wildcard (or Null). It now reports the shape untuned and returns Default,
the same answer as the no-donor and stream-capture exits.
`AcceleratorError` and `OutOfMemoryError` are reported untuned and
rethrown, so the worker is torn down. This changes code added in
D116493590.

4. Non-OK logging (TunableOp.h). A tuned solution that fails at runtime
returns non-OK and every caller falls back silently; the failing solution is
now named at `TUNABLE_LOG2`. Previously reviewed as D128384389.

5. Linking. `StreamTimer`, `StreamTimerNoSync` and `ResultEntry`'s
`operator<<` are exported with `TORCH_CUDA_CPP_API`, which the TunableOp.h
templates need to link from the new gtests outside libtorch_cuda.

Fixes to earlier diffs in the stack:
- D116493593: `FlagGuard` in cuda_tunable_nearest_test.cpp snapshotted
  `IsNearestMatchEnabled()`, which folds in recording, so its restore could
  clear the raw setting. The kill-switch test in test_linalg.py now also
  checks the pristine defaults.
- D116493592: `_warn_fault_once` attached `exc_info` even outside an except
  block.
- D116493595: the rotating-buffer restore in test cleanup (the getter returns
  bytes, the setter takes MiB) and the `_Job.measure` test stub.
- D116493590: the `ResolveWithoutExactHit` exits in item 3 and the
  `ExternKernelCaller.benchmark` inline fallback in item 2.

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
ghstack-source-id: 487448143
@ruddhic ruddhic changed the title [tunableop] keep Inductor's caches from hiding compile-time TunableOp work [tunableop] harden compile-time GEMM routing and warn when caches hide the pass Dec 2, 2026
ruddhic added a commit that referenced this pull request Dec 2, 2026
…e the pass

Pull Request resolved: #203284

Follow-up fixes for the compile-time TunableOp stack. Each of the four PRs
below is at GitHub's limit on lines added per PR, so fixes found later sit in
this diff on top. Some of them fix code those diffs own; they are listed by
owner at the end.

An earlier version bypassed FxGraphCache, AOTAutogradCache and the local
autotune cache. That was replaced by a warning (item 1), and this summary now
describes the code as it is.

Suggested review order:

1. Caches and pipelining (tunableop.py, autotune_process.py, config.py).
Both passes work during lowering and autotune, so a cache hit skips them. The
caches are not bypassed, because suppressing them costs every graph the entry
that standalone_compile and precompile build their artifact from. Instead,
`_warn_if_caching_enabled` warns once per mode unless `force_disable_caches`
is set. For aot_compile and precompile, which reject that flag, it suggests a
fresh `TORCHINDUCTOR_CACHE_DIR`. Known gap: the warning is emitted from
lowering/autotune, so a run where every graph is a cache hit gets no warning.
Pipelined autotuning is turned off while a mode is set: that path benchmarks
the bmreq directly and never reaches the routing.

2. Routing hardening in `ExternKernelCaller.benchmark` (select_algorithm.py,
tunableop.py). Once a mode is set and the candidate is a GEMM TunableOp tunes,
every exit that times inline does so with tuning off, not only the
post-fault one. Three gates keep candidates the worker cannot benchmark out
of it: a circuit breaker (`routing_is_open`, after 3 consecutive faults),
metas that failed to build (`output_tensor_meta`), and `VERIFY`. The breaker
is one process-wide counter shared by every device's worker and resets only
on a fresh cache, so with default caching it lasts for the rest of the
process. `submit()` also treats a bool result as a fault, since bool is an
int and True would pass as a 1 ms timing.

3. Behaviour change in borrow validation (TunableOp.h). When validating a
borrow throws `c10::Error`, `ResolveWithoutExactHit` used to return the
wildcard (or Null). It now reports the shape untuned and returns Default,
the same answer as the no-donor and stream-capture exits.
`AcceleratorError` and `OutOfMemoryError` are reported untuned and
rethrown, so the worker is torn down. This changes code added in
D116493590.

4. Non-OK logging (TunableOp.h). A tuned solution that fails at runtime
returns non-OK and every caller falls back silently; the failing solution is
now named at `TUNABLE_LOG2`. Previously reviewed as D128384389.

5. Linking. `StreamTimer`, `StreamTimerNoSync` and `ResultEntry`'s
`operator<<` are exported with `TORCH_CUDA_CPP_API`, which the TunableOp.h
templates need to link from the new gtests outside libtorch_cuda.

Fixes to earlier diffs in the stack:
- D116493593: `FlagGuard` in cuda_tunable_nearest_test.cpp snapshotted
  `IsNearestMatchEnabled()`, which folds in recording, so its restore could
  clear the raw setting. The kill-switch test in test_linalg.py now also
  checks the pristine defaults.
- D116493592: `_warn_fault_once` attached `exc_info` even outside an except
  block.
- D116493595: the rotating-buffer restore in test cleanup (the getter returns
  bytes, the setter takes MiB) and the `_Job.measure` test stub.
- D116493590: the `ResolveWithoutExactHit` exits in item 3 and the
  `ExternKernelCaller.benchmark` inline fallback in item 2.
ghstack-source-id: 487448143

Differential Revision: [D129441755](https://our.internmc.facebook.com/intern/diff/D129441755/)
@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request meta-exported module: inductor ready for review Automated review passed, ready for a final review by a maintainer. release notes: linalg_frontend release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants