Repository navigation
LayerNorm / RMSNorm: per-GPU num_warps tables - #15180
Conversation
Measured on T4, L4, A100, H100 and RTX PRO 6000: the forward + frozen-W backward and the dW backward each take a per (compute capability, width) warp count where a sweep found it faster outside A/A in some cell, never slower, >= 3% geomean, with max error vs fp64 no worse than the default. UNSLOTH_NORM_ARCH_WARPS=0 restores calculate_settings everywhere.
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
The
from real_accelerator import has_real_cuda
@pytest.mark.skipif(not has_real_cuda(), reason = "needs CUDA")( |
…CUDA maxnreg launch option [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
## Summary #15180 (per-GPU num_warps tables) added `tests/test_norm_arch_warps.py`, whose CUDA case skips with `torch.cuda.is_available()`. `tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe` rejects that on main, so `Repo tests (CPU, studio)` is red on unrelated PRs (for example #15189): ``` test_norm_arch_warps.py:114: torch.cuda.is_available() ``` `tests/_zoo_aggressive_cuda_spoof.py` patches `torch.cuda.is_available()` to True for the whole process, so on a CPU-only runner that skip stops skipping whenever it shares a session with `tests/version_compat` or `tests/vllm_compat`. The test is wrong here, not the kernels. ## Change - `tests/test_norm_arch_warps.py`: skip on `has_real_cuda()` from `tests/_shared/real_accelerator.py`, as `test_block_compile.py`, `test_bnb_nf4_override.py` and others do. ## Verification - `tests/python/test_accelerator_skip_guards.py`: `1 failed, 23 passed` on main, `24 passed` with this change. - `tests/test_norm_arch_warps.py` on CPU: `5 passed, 4 skipped`, the same as before. - On a GPU host `has_real_cuda()` is True, so the CUDA cases still run there. On a CPU host it is False.
…ngs cache (unslothai#14727) unslothai#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 unslothai#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 unslothai#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 unslothai#13889) | pass, one graph | | b67f20f (unslothai#13889) | TypeError in the fake run | | main | TypeError in the fake run | | this branch | pass, matches eager bytes | Update after syncing with main: unslothai#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.
The Triton LayerNorm and RMSNorm kernels take their
num_warpsfromcalculate_settings, which only looks at the block size. A sweep over hidden size and GPU found that a different warp count is faster in some cases, and that the best count for the forward + frozen-weight backward often differs from the best one for the weight-gradient (dW) backward.Change
unsloth/kernels/utils.pygetsnorm_num_warps(table, n_cols, num_warps, device). It returnstable[(capability, n_cols)]on CUDA devices and the existing default everywhere else (CPU, ROCm, widths and GPUs that have no entry).layernorm.pyandrms_layernorm.pyeach get two tables, one for the forward + frozen-W backward and one for the dW backward. The dW kernel and its program count use their own table.torch.compileit is computed directly, so it adds no graph break. Host time per call stays within main's range:_settingstakes 209-222 ns against 145-253 ns on main.UNSLOTH_NORM_ARCH_WARPS=0turns the tables off.Table entries
An entry is in the table only if it held up when this branch was run against main on torch 2.14.1. The method:
Fast_Layernorm/Fast_RMS_Layernormforward and backward as a CUDA graph of 10 calls.An entry is kept only when:
Cells: rows 2k and 16k, bf16 and fp16 (fp16 only on T4).
Dropped: two T4 LayerNorm candidates did not replicate, so they are not in the table: forward at 2048, and dW at 1024 (0.898-1.007).
Earlier lead: an earlier lead of about 1.25x for 4 warps at hidden 4096 did not hold on any timed tier:
Accuracy: outputs are not bitwise equal to the default, because the reduction order changes. Every entry's maximum error against an fp64 reference is within one ulp of the default's maximum error, in fp16 and bf16, for 1, 777 and 4096 rows.
dX mismatch on table GPUs: dX from the dW backward can now differ in the last bits from dX of the frozen-W backward, because the two paths use different warps. Both stay within that bound.
End to end
LoRA training step: 16-bit, batch 2, sequence 1024, r 16, 20 steps; median of steps 6-20, main / branch / main interleaved over 2 rounds. Losses match between arms.
With LoRA the norm weights are frozen, so only the forward + frozen-W entries can move a LoRA step.
Tests
New
tests/test_norm_arch_warps.py(9 tests) covers:Bypassing the lookup fails 8 of the 9.
The 16 norm test files plus the new test, on B200:
The 9 extra passes are the new test.
Not covered