-
Notifications
You must be signed in to change notification settings - Fork 380
[MAMBA2] fix initialization for mamba2 #739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @mayank31398, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical initialization error within the Mamba2 implementation in the FLA repository. By correcting the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
WalkthroughModified per-head state-space discretization initialization in Mamba2: Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request aims to correct the initialization of the Mamba2 layer. The changes to the initialization of dt_bias and A_log are a good step forward. However, I've identified a couple of issues. A magic number has been introduced for dt_init_floor, which should ideally be configurable. More importantly, there's a critical inconsistency in the A_log initialization between the Mamba2 layer and the Mamba2PreTrainedModel, which will cause the fix to be ineffective when initializing a new model. Please see my detailed comments below.
| A = torch.empty(self.num_heads, dtype=torch.float32).uniform_(0, 16) | ||
| self.A_log = nn.Parameter(torch.log(A)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initialization of A is changed here, but the corresponding _init_weights method in Mamba2PreTrainedModel (in fla/models/mamba2/modeling_mamba2.py) still uses the old logic (torch.arange(1, module.num_heads + 1)). Since _init_weights is called after the layer's __init__ when creating a model, it will overwrite this new initialization, making the fix ineffective. The logic in _init_weights for A_log needs to be updated to match this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@fla/layers/mamba2.py`:
- Around line 174-180: The long single-line expression that computes dt (the
line assigning dt = torch.exp(torch.rand(self.num_heads) *
(math.log(self.time_step_max) - math.log(self.time_step_min)) +
math.log(self.time_step_min))) should be split into smaller statements for PEP8
readability: compute the random tensor (use rand = torch.rand(self.num_heads)),
compute the log range (log_range = math.log(self.time_step_max) -
math.log(self.time_step_min)), compute the log offset (log_min =
math.log(self.time_step_min)), then compute dt = torch.exp(rand * log_range +
log_min), followed by the existing clamp, inv_dt computation and nn.Parameter
assignment to self.dt_bias to preserve behavior using the same symbols dt, rand,
log_range, log_min, dt_init_floor, inv_dt, and self.dt_bias.
|
One big concern: if a user is training any model with FSDP-2, since all tensors are DTensors, it will skip initialization according to the FLA logic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@fla/models/mamba2/modeling_mamba2.py`:
- Line 203: The CI lint failed due to formatting in the assignment to A (the
line "A = torch.empty(self.num_heads, dtype=torch.float32).uniform_(0, 16)" in
modeling_mamba2.py); run the repository pre-commit hooks or autopep8 (e.g.,
pre-commit run --files fla/models/mamba2/modeling_mamba2.py or autopep8
--in-place) to reformat the file so it passes the autopep8/style checks, then
amend the commit.
- Line 203: The code creates A with torch.empty(self.num_heads, ...) but
self.num_heads doesn't exist; change the array size to use the config property
(self.config.num_heads) so A = torch.empty(self.config.num_heads,
dtype=torch.float32).uniform_(0, 16) in the same method where line 217 already
uses self.config.num_heads (update the creation of A in modeling_mamba2.py,
within the Mamba2PreTrainedModel-derived method).
|
AFAIK this PR still doesnt fix if the meta init is done with DTensors. I am not sure how to fix that |
@yzhangcs i remembered you fixed this before. any thoughts? |
|
@mayank31398 thanks for ur pr! could you fix lint errors? |
Signed-off-by: Mayank Mishra <mayank31398@gmail.com>
The init in FLA repo for mamba2 is incorrect.
This PR fixes the issue.
After the fix, Mamba2 outperforms GDN at 7B MoE scale (1B active params).
The difference between wrong and fixed init is significant.
Training curves: https://wandb.ai/mayank31398/mamba-test?nw=nwusermayank31398&panelDisplayName=train/lm_loss&panelSectionName=train
Summary by CodeRabbit