Skip to content

Studio: key Triton and Inductor caches on the 8-bit prefetch fix state (sm_8x) - #17793

Merged
danielhanchen merged 2 commits into
mainfrom
studio-triton-prefetch-cache-key
Nov 15, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-triton-prefetch-cache-key

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

#17669 skips Triton's sm_8x prefetch pass for 8-bit tt.dot modules by wrapping CUDABackend.make_ttgir. A compiler-stage patch does not enter Triton's cache key, so with the persistent <studio root>/cache/triton directory:

  • kernels compiled without the skip (an older Studio, or UNSLOTH_TRITON_8BIT_PREFETCH_FIX=0) keep being loaded with the fix on, so sm80 / sm89 keep running the spilling kernel;
  • kernels compiled with the skip keep being loaded after the kill switch turns it off.

Studio's own int8 GEMM already carries a PREFETCH_FIX constexpr for this; Inductor-generated and other Triton kernels do not.

Why the key ignores it

  • Triton 3.8 / 3.9 compiler.py compile() builds key = get_cache_key(src, backend, options, env_vars) from triton_key(), src.hash(), backend.hash(), options.hash() and the cache-invalidating env vars, then returns the cached CompiledKernel on a metadata hit before backend.add_stages runs. A patched stage never runs on a hit.
  • CUDABackend.hash() (backends/nvidia/compiler.py) is f"{ptxas_version}-{target.arch}".
  • Inductor's FxGraphHashDetails (torch/_inductor/codecache.py, torch 2.10 through main) hashes triton_key() but not the backend hash, and stores torch.compiler.config.cache_key_tag. An FX graph cache hit can reload bundled kernels without a Triton cache lookup.
  • Triton 3.7 is unaffected: its pipeline adds the pass for every capability, so the fix never installs.

Fix

  • While the fix is installed, CUDABackend.hash() appends a salt for sm_8x targets unless the kill switch is set. The salt is applied before the stage swap, and also to an install left by an older copy of this module.
  • On import of torch._inductor.codecache, FxGraphHashDetails appends the same salt to its cache_key_tag when the fix is installed, the current CUDA device is sm_8x, and the kill switch is off. ROCm is skipped.

This gives one cache entry per fix state. sm_9x / sm_10x / sm_12x keys are unchanged, and the kill switch maps back to the stock key and finds the stock entries again, so nothing outside sm_8x recompiles. On sm_8x each kernel recompiles once with the fix on. No cache directory is moved or wiped.

Tests

studio/backend/tests/test_triton_8bit_prefetch_fix.py: 10 new cases, all failing on main and passing here.

  • Real Triton (CPU cross-compile, sm89 / sm90), warm cache. Compiling with the fix off and then installing the fix gets a new hash and 0 prmt.b32 (352 before). sm90 keeps its hash. The kill switch finds the stock entry again.
  • The backend hash is salted only on sm_8x, only with the fix on, never twice, and also on an earlier unsalted install.
  • The FX graph tag covers each case: installed or not, capability, kill switch, user tag. A real compiled_fx_graph_hash changes with the salt and returns to stock under the kill switch.

Cross-compile check, warm cache, hash[:12] / prmt.b32:

Triton Stale entry (fix off) Fix on, main Fix on, this branch Kill switch, this branch
3.8.0 4c1a6aefd815 / 352 4c1a6aefd815 / 352 fec229049be7 / 0 4c1a6aefd815 / 352
3.9.0 4ee39f9d5d70 / 352 4ee39f9d5d70 / 352 baef9a4de4d0 / 0 4ee39f9d5d70 / 352

GPU, Colab L4 (sm89), Triton 3.8.0, torch 2.14.1+cu130. A 4096^3 int8 tl.dot GEMM (128x128x64 tile, 4 warps, 3 stages), one process per row, one shared TRITON_CACHE_DIR; output checked against a CPU matmul in every row:

Step Hash New compile Spills Regs ms
Fill the cache with the fix off bb6b0c885581 yes 146 255 7.19
main, fix on, that cache bb6b0c885581 no 146 255 7.23
this branch, fix on, that cache e158c16fab21 yes 0 234 1.31
this branch, fix on, again e158c16fab21 no 0 234 1.31
this branch, kill switch bb6b0c885581 no 146 255 7.18
main, fix on, fresh cache bb6b0c885581 yes 0 234 1.31
main, kill switch after that bb6b0c885581 no 0 234 1.31
this branch, kill switch after a fixed fresh cache bb6b0c885581 yes 146 255 7.24

Not covered: an A100 (sm80) run, and an end-to-end Inductor FX graph cache hit on a GPU. The FX tag is covered by the CPU key test only.

A compiler-stage patch does not enter Triton's cache key, so kernels compiled
without the prefetch skip (an older Studio, or the kill switch) were reused
with the fix on, and fixed kernels survived the kill switch. Salt the CUDA
backend hash on sm_8x while the fix is active, and tag Inductor's FX graph
cache key the same way, keeping one cache entry per fix state. Other cards and
the kill switch keep their stock keys, so nothing else recompiles.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T05:03:40.500602Z 868aa4e 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 05a4f0f8b9

ℹ️ 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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 868aa4eaf9

ℹ️ 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 8f42f46 into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the studio-triton-prefetch-cache-key branch November 15, 2026 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant