Skip to content

RMSNorm / LayerNorm: do not hash a symbolic width in the launch settings cache - #14727

Merged
danielhanchen merged 5 commits into
mainfrom
rmsnorm-dynamic-compile
Oct 25, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
rmsnorm-dynamic-compile

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

#13889 cached calculate_settings per width with functools.lru_cache, bypassing the cache only when torch.compiler.is_compiling() is true. Under torch.compile(dynamic=True) the traced unsloth::rms_layernorm op runs its fake implementation outside Dynamo, where is_compiling() is False but the width is a SymInt, so the cache tries to hash it:

Dynamo failed to run FX node with fake tensors: call_function unsloth.rms_layernorm(...): got TypeError('unhashable type: non-nested SymInt')

So a compiled RMSNorm with fullgraph=True, dynamic=True fails on main, where it traced in one graph before #13889 (d64b080).

Fix: _settings also calls calculate_settings directly when the width is not a plain int. Eager calls still hit the cache (the width there is always an int), so the host path is unchanged apart from one type() check. The same guard goes into layernorm.py, which got the same cache in #13889.

Measured on one B200, torch 2.11.0, Triton 3.6.0, compiled norm with fullgraph=True, dynamic=True, sequence lengths 64 / 80 / 96 / 127, widths 64 and 4096, fp32 and bf16:

Tree Result
d64b080 (before #13889) pass, one graph
b67f20f (#13889) TypeError in the fake run
main TypeError in the fake run
this branch pass, matches eager bytes

Update after syncing with main: #15180 moved _settings to a per-(capability, width) num_warps table (norm_num_warps), whose table.get((capability, n_cols)) hashes the width again. On NVIDIA that second lookup kept the same TypeError, so norm_num_warps now matches a symbolic width by equality. Measured on a G4 (sm120): with only the _settings guard, test_rms_layernorm_dynamic_shapes[4096-*] still fails; with both it passes. tests/test_norm_settings_symbolic_width.py::test_a_symbolic_width_reads_the_cuda_warps_table (CPU, capability patched) covers it and fails without the fix.

Tests:

  • tests/test_norm_settings_symbolic_width.py (CPU): a symbolic width skips the cache and returns calculate_settings's answer; an int width still uses the cache. The symbolic cases fail on main and pass here.
  • tests/test_rms_rope_compile.py::test_rms_layernorm_dynamic_shapes (GPU): compiled fullgraph=True, dynamic=True forward and backward match eager bytes across three lengths, Llama and Gemma variants. Fails on main (4 of 4), passes here.
  • The rest of test_rms_rope_compile.py, the multirow, triton_launch and weight-grad norm suites pass on this branch, except the two tiny-Llama fullgraph tests, which fail the same way on main.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-25T05:43:06.900394Z 48f5197 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. More of your lovely PRs please.

Reviewed commit: 234aa4d40e

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

# Conflicts:
#	unsloth/kernels/layernorm.py
#	unsloth/kernels/rms_layernorm.py
@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. Delightful!

Reviewed commit: 48f519783c

ℹ️ 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 da00729 into main Oct 25, 2026
28 of 37 checks passed
@danielhanchen
danielhanchen deleted the rmsnorm-dynamic-compile branch October 25, 2026 08:55
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