Repository navigation
RMSNorm: keep the final norm's own dX buffer when hidden states are returned to the caller - #14139
Conversation
…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).
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 |
|
Codex Review: Didn't find any major issues. Bravo. 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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! 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". |
Follow-up to #13984.
Problem
#13984 lets the final RMSNorm write dX over the incoming gradient (
reuse_grad) wheneveroutput_hidden_statesis false, on the premise that only lm_head or the fused CE loss read the norm's output. WithUNSLOTH_RETURN_HIDDEN_STATES=1the 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 exampletorch.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'sgrad_outputschanged (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 fromzeros_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 andmistral.py) only enables final-norm reuse when hidden states are not returned to the caller: neitheroutput_hidden_statesnorUNSLOTH_RETURN_HIDDEN_STATES=1. The decoder-layer norms are unchanged.Tests
tests/test_norm_backward_grad_alias.py: newtest_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).