Fix double rmsnorm on B/C in MambaInnerFn.backward at checkpoint_lvl=1#902
Open
Chessing234 wants to merge 1 commit intostate-spaces:mainfrom
Open
Fix double rmsnorm on B/C in MambaInnerFn.backward at checkpoint_lvl=1#902Chessing234 wants to merge 1 commit intostate-spaces:mainfrom
Chessing234 wants to merge 1 commit intostate-spaces:mainfrom
Conversation
When checkpoint_lvl >= 1, forward() normalizes B and C with rmsnorm then saves the already-normalized tensors via ctx.save_for_backward. backward() then loads these tensors and applies rmsnorm again, resulting in double normalization that produces incorrect gradients. Fix by setting B and C to None before saving (matching the existing pattern for delta), and recomputing them from x_dbl in backward before applying rmsnorm exactly once. B_proj_bias and C_proj_bias are now stored on ctx so the recomputation can correctly include projection biases. Fixes state-spaces#885 Co-Authored-By: Claude Opus 4.6 (1M context) <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.
Summary
checkpoint_lvl >= 1,MambaInnerFn.forward()applies rmsnorm to B and C, then saves the normalized tensors.backward()loads these already-normalized tensors and applies rmsnorm again, producing incorrect gradients due to double normalization.Nonebeforectx.save_for_backward(matching the existing pattern fordelta), and recompute them fromx_dblinbackward()before applying rmsnorm exactly once.B_proj_biasandC_proj_biasare now stored onctxso recomputation can correctly include projection biases when present.Fixes #885
Test plan
checkpoint_lvl=0(no recomputation, known correct) withcheckpoint_lvl=1(recomputation) whenb_rms_weightandc_rms_weightare settorch.autograd.gradcheckonMambaInnerFnwith rmsnorm enabledb_rms_weight/c_rms_weightareNone(original code path unchanged)🤖 Generated with Claude Code