Skip to content

Fused QKNorm + RoPE: keep type promotion for mixed dtype tables, reuse grad in the trainable norm fallback - #14200

Merged
danielhanchen merged 1 commit into
mainfrom
dh/fu-qkrope
Oct 19, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/fu-qkrope

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Two follow-ups for the opt-in fused QKNorm + RoPE kernel (UNSLOTH_FUSED_QKNORM_ROPE=1, #13890). The flag-off path is untouched: qwen3.py, rope_embedding.py and rms_layernorm.py do not change.

  1. Mixed dtype tables. With fp32 or fp16 activations and bf16 cos / sin tables, the fused kernel cast the norm output down to bf16 before the rotation. The indexed RoPE kernel it replaces (the usual Qwen3 case, with position indices) multiplies with type promotion and never casts the activation down. transformers' apply_rotary_pos_emb does the same: the tables follow the activation dtype and the product uses normal torch promotion. The fused kernel now casts to the table dtype only in the no-indices mode, so each mode matches the unfused kernel it replaces. When the dtypes already match, nothing changes: 72 output and gradient tensors are byte-identical between the old and new kernel.
  2. Trainable norm fallback. _unfused now passes reuse_grad = True to both norms, as the flag-off Qwen3 path does. The norm output only feeds the RoPE kernel there.

Tests

tests/test_qk_norm_rope.py (GPU, tiny shapes):

  • test_mixed_dtype_tables_match_unfused: fp32 and fp16 activations against bf16 tables, with and without indices. The two indexed cases fail on main; the no-indices cases pass on both.
  • test_trainable_norm_fallback_reuses_grad_buffer: peak memory goes from 24,450,048 to 20,255,744 bytes. Fails on main.

All 44 tests in the file pass here, including the existing fullgraph / no graph break tests.

Not covered here

  • Only B200, torch 2.13, Triton 3.7.1; no end-to-end Qwen3 training run with the flag on.
  • In the no-indices mode the unfused RoPE kernel still rounds fp32 activations to bf16 tables, and the fused forward keeps matching it. Changing that would alter the default RoPE kernel.

… tables, reuse grad in the trainable norm fallback
@danielhanchen danielhanchen self-assigned this Oct 19, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 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-19T05:48:20.549863Z 2e1ad87 Manual request
🔒 Security Review ⚠️ Failed 2026-10-19T05:45:27.884317Z 2e1ad87 PR opened
ℹ️ 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. Keep them coming!

Reviewed commit: 2e1ad879ba

ℹ️ 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 1fe718b into main Oct 19, 2026
@danielhanchen
danielhanchen deleted the dh/fu-qkrope branch October 19, 2026 10:00
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