Skip to content

Key triton < 3.4 driver module cache by Python version - #13409

Merged
danielhanchen merged 4 commits into
mainfrom
fix-triton-cache-python-key
Oct 12, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
fix-triton-cache-python-key

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Fixes #2760

Root cause

triton 3.2.0 and 3.3.x (the versions torch 2.6 and 2.7 pin) compile cuda_utils and every kernel launcher with gcc at run time and cache the result under a hash of the C source only, as cuda_utils.so. Nothing in the key or the file name says which Python built it. A module built by Python 3.13 calls PyArg_ParseTuple, which in 3.13 always takes Py_ssize_t lengths; the same symbol in Python 3.12 and older is the legacy one, so when a 3.12 process picks up that cached module, load_binary("ss#ii") raises:

SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats

at the first Triton kernel launch. That is the reporter's setup (torch 2.7.0, triton 3.3.0, requires-python = "==3.12.0"), and why deleting ~/.triton/cache is the workaround that keeps working in triton-lang/triton#5919 and pytorch/pytorch#153737. triton 3.4 fixed it upstream by naming the cached file with the full EXT_SUFFIX (cuda_utils.cpython-312-x86_64-linux-gnu.so).

The check added in #11023 reads driver.c for a missing macro, but every published triton wheel defines it, so it never fires for this case.

Change

fix_triton_module_cache_python_key() in unsloth/import_fixes.py, run early in _gpu_init.py before anything brings up the triton driver. When a backend driver defines its own compile_module_from_src (triton 3.2 / 3.3, CUDA and ROCm), it is wrapped to append a /* unsloth: <EXT_SUFFIX> */ comment to the C source, so the cache key differs per Python version, matching what triton 3.4 does. On triton 3.4 and newer the driver imports the already keyed function from triton.runtime.build, and nothing is patched. Idempotent, functools.update_wrapper, original kept as __wrapped__.

Cost: affected installs (torch 2.6 / 2.7) recompile cuda_utils and each launcher once into the new key; later runs hit the cache as before. Nothing on the per-call path changes.

Evidence

Reproduced with triton 3.3.0 wheels: a Python 3.13 process fills the cache, then Python 3.12 reuses it.

run base (main) this PR
CudaUtils().load_binary on 3.12 after 3.13 filled the cache SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats parses, new cache entry
fast_rms_layernorm on GPU, torch 2.7.0+cu128, Python 3.12, cache seeded by 3.13 SystemError kernel runs, finite output
tiny Qwen3 LoRA SFT, 3 steps, same seeded cache crashes at step 0 with the same SystemError passes all checks
Python 3.12 fresh cache (control) works works

Patch applied on triton 3.2.0 (nvidia, amd), inert on 3.4.0 and 3.8.0.

Tests: tests/test_triton_module_cache_key.py (a 3.3 style backend loaded the way triton.backends loads it gets keyed once; a 3.4 style one is left alone), plus tests/test_import_time_floors.py: 84 passed.

@danielhanchen danielhanchen self-assigned this Oct 12, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 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-12T11:51:54.399273Z 3610a63 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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 12, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: c36cfe7489

ℹ️ 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 added a commit to Etherll/unsloth that referenced this pull request Oct 12, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 3610a63153

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

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.

[Bug] SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats

1 participant