Skip to content

Fused Qwen3 QK norm + RoPE: on by default on NVIDIA CUDA - #15189

Merged
danielhanchen merged 2 commits into
mainfrom
qknorm-rope-default
Oct 25, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
qknorm-rope-default

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

#13890 added a fused Triton kernel for Qwen3's q/k RMSNorm + RoPE in training. It sits behind UNSLOTH_FUSED_QKNORM_ROPE=1, which is off by default; the decode variant already ships on. That PR listed T4, older torch and ROCm as its exit criteria. This PR covers the NVIDIA side and turns the training kernel on by default there.

Change

New _qk_norm_rope_default() in unsloth/kernels/qk_norm_rope.py, read once at import into QK_NORM_ROPE_ENABLED:

  • UNSLOTH_FUSED_QKNORM_ROPE = 0 / false / no / off: unfused everywhere;
  • 1 / true / yes / on: fused everywhere, including ROCm;
  • unset: fused when DEVICE_TYPE == "cuda" (NVIDIA, Linux and Windows).

ROCm (DEVICE_TYPE == "hip"), XPU and CPU keep the unfused training path. The fused backward has not been measured on ROCm.

Results

Timing. Qwen3 LoRA SFT steps, torch 2.14 (T4 and A100 also on 2.10):

  • every arm in one process per job, interleaved in rotated blocks, with a fixed batch of about 4k tokens;
  • median of block medians over 4-5 rounds;
  • negative is faster than main; the A/A (main vs main) delta is in brackets.
GPU torch 1.7B 16-bit padded 1.7B 4-bit packed 0.6B 16-bit packed 0.6B 4-bit padded 8B padded
T4 (fp16) 2.14 -0.44% (-0.03) -0.63% (-0.23) -1.79% (-0.20) -1.27% (-0.89) 4-bit +0.27% (+0.83)
T4 (fp16) 2.10 -2.05% (-0.51) -2.34% (-1.01) 4-bit -0.14% (+0.32)
A100 2.14 -1.58% (-0.25) -5.28% (+0.69) -6.53% (-0.38) -3.74% (-1.82) 16-bit -0.86% (-0.07)
A100 2.10 -3.00% (+0.46) -4.04% (+0.55) 16-bit -0.75% (-0.15)
L4 2.14 -0.09% (+0.67) -1.21% (0.00) -0.67% (-0.06) -0.10% (+0.49) 4-bit -0.27% (-0.01)

No cell is slower beyond its A/A band. Peak memory is unchanged.

Loss. 50-step runs (20-30 for 8B) from the same LoRA init, seed and data:

  • max |loss difference| vs main is 0.6-1.6x the A/A difference (A/A is 0.002-0.0075 in bf16, 0.0003-0.0011 in fp16);
  • final loss is within 0.003.

Compile and engagement:

  • 0 graph breaks and 0 recompiles in both arms;
  • the same unique graph counts in separate processes;
  • per step, the fused forward runs 56 times and the fused backward 28 times; the unfused RoPE kernel runs 0 times and there are 0 eager fallbacks.

Tests

New cases in tests/test_qk_norm_rope.py:

  • the env / device decision table (11 cases);
  • a real tiny Qwen3 training step in a subprocess, checking that the fused kernels run with the flag unset and do not run with 0.

The file gives 57 passed on B200 with torch 2.14 and with 2.10. The existing parity cases (bf16 forward bit-identical to unfused, gradients vs an fp64 reference) pass on T4 fp16, A100 bf16 and B200.

Not covered

  • GPUs: no H100 (Colab refused it), sm120 or RTX 40xx timing.
  • ROCm: keeps the unfused path by the gate.
  • Qwen3-MoE: on transformers 5.x, Qwen3-MoE does not go through this attention path, so it is unaffected.

UNSLOTH_FUSED_QKNORM_ROPE was opt-in. The training path now uses the
fused q/k RMSNorm + RoPE kernel by default on CUDA. ROCm keeps the
unfused path unless UNSLOTH_FUSED_QKNORM_ROPE=1; =0 (or false/no/off)
disables it everywhere.
@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-25T03:23:59.086688Z ec45d17 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. Hooray!

Reviewed commit: ec45d173eb

ℹ️ 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 28ace86 into main Oct 25, 2026
32 of 37 checks passed
@danielhanchen
danielhanchen deleted the qknorm-rope-default branch October 25, 2026 04:00
danielhanchen added a commit that referenced this pull request Oct 25, 2026
## 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.
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