Skip to content

RMSNorm: keep the final norm's own dX buffer when hidden states are returned to the caller - #14139

Merged
danielhanchen merged 4 commits into
mainfrom
dh/v2-final-norm-returned-hidden
Oct 18, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
dh/v2-final-norm-returned-hidden

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #13984.

Problem

#13984 lets the final RMSNorm write dX over the incoming gradient (reuse_grad) whenever output_hidden_states is false, on the premise that only lm_head or the fused CE loss read the norm's output. With UNSLOTH_RETURN_HIDDEN_STATES=1 the CausalLM forward instead returns the normalized hidden states to the caller as .logits. The caller then owns that gradient, and can share it with another branch: for example torch.autograd.grad(hidden + inputs_embeds, inputs_embeds, grad_outputs) hands the same tensor to both inputs of the add. The norm's backward overwrote it, so the caller's grad_outputs changed (max 6.16 on a tiny Qwen3) and the shared branch's dX was off by 6.0. Forward outputs were identical.

Unsloth's own GRPO / DPO / fused CE consumers are not affected: each hands the norm a fresh gradient (the zoo chunked log-softmax calls autograd.grad, the fused preference loss starts from zeros_like, fused CE multiplies out of place). But the variable is process wide, so once a GRPO trainer sets it, any later caller in the same process that reads the returned hidden states can hit this.

Fix

_run_backbone (used by the Llama-family CausalLM forward and mistral.py) only enables final-norm reuse when hidden states are not returned to the caller: neither output_hidden_states nor UNSLOTH_RETURN_HIDDEN_STATES=1. The decoder-layer norms are unchanged.

Tests

tests/test_norm_backward_grad_alias.py: new test_returned_hidden_states_leave_caller_and_shared_grads_alone, and #13984's own assertion for this path flipped. Current main: 2 failed, 42 passed. This branch: 44 passed (GPU-marked tests included).

Peak memory is byte-identical to main for default SFT and for a GRPO-style returned-hidden-states loss on tiny Llama and Qwen3, so #13984's SFT memory saving is kept. Reuse on vs off on the default path stays bitwise identical in gradients, losses and generated tokens (4-bit and 16-bit, gradient checkpointing on and off, full finetuning).

…eturned as logits

#13984 lets the final norm write dX over its incoming grad whenever
output_hidden_states is off. Under UNSLOTH_RETURN_HIDDEN_STATES=1 the
CausalLM forward returns that norm's output to the caller as .logits, so
the caller owns its grad: autograd.grad(h + x, x, g) overwrote g and gave
x a wrong grad (max abs error 6.0 on tiny Qwen3, bf16).

_run_backbone now enables the reuse only when the hidden states stay
inside the CausalLM head (lm_head / fused CE). The default SFT path is
unchanged; the built-in GRPO / DPO chunked losses see the same peak
memory on tiny Llama and Qwen3 (the chunked logits dominate the peak).

Tests: test_returned_hidden_states_leave_caller_and_shared_grads_alone
is new, and the GRPO-style assertion in
test_final_norm_reuses_grad_only_under_the_causal_lm_head now expects
the norm's own buffer. Both fail before this change and pass after
(44 passed with -m gpu).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 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-18T15:25:13.911497Z 1b786d3 Manual request
🔒 Security Review ⚠️ Failed 2026-10-18T13:52:17.794038Z 3c0b4f7 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. Bravo.

Reviewed commit: 3c0b4f7651

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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 1b786d3191

ℹ️ 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 7462b48 into main Oct 18, 2026
@danielhanchen
danielhanchen deleted the dh/v2-final-norm-returned-hidden branch October 18, 2026 22:25
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