Skip to content

Inductor readback barrier: key the compile cache when a user pass is already set - #15208

Merged
danielhanchen merged 3 commits into
mainfrom
dh/s6-fix-barrier-cachekey
Oct 25, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/s6-fix-barrier-cachekey

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow up to #15000.

Problem

fix_inductor_reduction_readback_race (torch < 2.14) adds a tl.debug_barrier() before a reduction kernel reloads a buffer it stored in an earlier loop. So that warm FX / AOTAutograd caches do not keep serving barrier-free kernels, it sets a no-op CustomGraphPass whose uuid enters the cache key.

That marker was only installed when torch._inductor.config.post_grad_custom_post_pass was None. If a user (or another library) had already set a CustomGraphPass, the key stayed that pass's uuid alone. A cache written before the fix was then still a hit, and the racy kernels came back.

Fix

_key_inductor_caches_on_readback_barrier now handles the existing pass:

  • A CustomGraphPass with a uuid is wrapped. The wrapper's uuid is (barrier marker, user uuid), so the key changes. Calling it calls the user pass. A CustomInferenceAwareGraphPass is wrapped by an inference aware subclass, so is_inference still reaches it.
  • A list or tuple of passes (torch 2.13 accepts these) gets the marker pass appended.
  • Raw callables and passes without a uuid are left unchanged. torch already bypasses the FX cache for those. Wrapping one would make it cacheable without its identity in the key.
  • Re-running the fix does not wrap twice.

Tests

tests/test_inductor_readback_barrier.py, CPU:

  • test_a_user_custom_graph_pass_keys_the_cache_on_the_barrier_too: the wrapped uuid carries both ids, the user pass still runs, and a second run does not re-wrap.
  • test_an_inference_aware_user_pass_still_gets_is_inference
  • test_a_list_of_user_passes_gets_the_marker_appended
  • test_a_raw_callable_user_pass_is_left_alone
  • test_a_warm_cache_from_before_the_fix_is_not_served_with_a_user_pass: end to end with the Inductor CPU backend. A process without the fix warms the cache. A second process without the fix hits it, which shows the key is otherwise stable. A process with the fix misses.

Results:

  • Before (main): 3 failed, 9 passed, 1 skipped. The warm cache test gets fxgraph_cache_hit == 1.
  • After: 12 passed, 1 skipped. The skip is the existing CUDA codegen test.

GPU, Colab L4

Each row is a separate process. The kernel is a fused add plus layer norm reduction that reloads its stored buffer. The cache is first warmed by a process without the fix (the cache from before #15000). The rows below then run on that warm cache, each arm on its own copy of it.

torch 2.13.0, user CustomGraphPass set before the fix:

run barriers in the kernel FX cache
warm, no fix 0 miss
main fix on the warm cache 0 hit (the racy kernel is served)
this PR on the warm cache 1 miss
this PR again 1 hit

torch 2.13.0, no user pass (control): main and this PR both miss the warm cache and emit the barrier. torch 2.14.1 (control): the fix skips itself, and every run has the native barrier. Numerics matched eager in every run. The race itself is timing dependent, so this check covers the cache key and the kernel text, not wrong outputs.

Not covered

  • A pass set after import unsloth replaces the marker, before and after this change.

The barrier marker entered the FX / AOTAutograd cache key only when
post_grad_custom_post_pass was None. With a user CustomGraphPass set
before Unsloth, the key stayed the user pass uuid alone, so a warm cache
written without the barrier was still served on torch < 2.14. The user
pass is now wrapped: its uuid carries the marker and the user uuid, and
it still runs (is_inference is forwarded for inference aware passes).
A list of passes gets the marker appended; raw callables and uuid-less
passes are left as is since torch bypasses the cache for them.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T06:32:40.351498Z 6b78b98 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 3a89f7124c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 6b78b9899c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 7ebf5e4 into main Oct 25, 2026
@danielhanchen
danielhanchen deleted the dh/s6-fix-barrier-cachekey branch October 25, 2026 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant