fix: remove double RMSNorm on B/C in MambaInnerFn.backward at checkpoint_lvl=1#939
Open
Chessing234 wants to merge 1 commit into
Open
fix: remove double RMSNorm on B/C in MambaInnerFn.backward at checkpoint_lvl=1#939Chessing234 wants to merge 1 commit into
Chessing234 wants to merge 1 commit into
Conversation
…int_lvl=1 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
MambaInnerFn.backwardapplies RMSNorm toBandCa second time whencheckpoint_lvl == 1, silently corrupting gradients.Forward pass (
selective_scan_interface.py, around line 250):Then
ctx.save_for_backward(..., B, C, ...)saves post-norm B and C.Only
conv1d_outanddeltaare set toNonebefore saving:B and C are not set to None — they are saved as already-normalized tensors.
Backward pass (
checkpoint_lvl == 1):deltais correctly recomputed from scratch (was None), so re-applying dt_rms is correct.BandCcome fromctx.saved_tensorsalready normalized. The removed blocks applied RMSNorm a second time, making the effective normalizationrms_norm(rms_norm(B))instead ofrms_norm(B).This causes incorrect gradients for any model using
b_rms_weightorc_rms_weightwith activation checkpointing (checkpoint_lvl=1), such as Mamba2 trained with--checkpoint-activations.Root cause
The
b_rms_weight/c_rms_weightnormalization blocks were added inside thecheckpoint_lvl == 1recompute block by analogy withdt_rms_weight, but unlikedelta, B and C are saved post-norm and never recomputed — so they must not be re-normalized.Fix
Remove the 14 lines that re-apply
rms_norm_forwardto B and C inside thecheckpoint_lvl == 1block.deltare-normalization is unaffected and remains correct.Fixes #885.