Fix Mamba2 Grouped SSD Support in the torch_forward Path
Author: cyang49
Pull request: yes

# What does this PR do?

We found a bug in Bamba `torch_forward` implementation where the Mamba2 grouped SSD heads are incorrectly expanded for computations. 

In the original code, it uses `torch.repeat` but it results in a tile like pattern, e.g. when ngroups=4 and num_heads=16, `torch.repeat` gives 
```
[W, X, Y, Z] --> [W, X, Y, Z, W, X, Y, Z, W, X, Y, Z, W, X, Y, Z]
```
instead of the desired 
```
[W, X, Y, Z] -->[W, W, W, W, X, X, X, X, Y, Y, Y, Y, Z, Z, Z, Z]
```
This causes models using `ngroups > 1` and `ngroups != num_heads` to fail evaluations. We solve it by using `torch.repeat_interleave` to replace `torch.repeat`.

The bug was left undetected for a while, perhaps because the `cuda_forward` path is used by most people, or because Bamba-9B uses ngroups=1.

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->




## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

@fabianlim @ani300 @ArthurZucker


Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page). This will assign reviewers and trigger CI.

That's a good catch! Can confirm the issue and solution (based on some local tests). For reference, the original mamba repo also points to this in https://github.com/state-spaces/mamba/blob/2e16fc3062cdcd4ebef27a9aa4442676e1c7edf4/mamba_ssm/ops/triton/ssd_chunk_scan.py#L1813-L1814 (when looking at the repeat pattern).

This also affects other mamba2 based models: Could you 
1. Fix the related models (mamba2, zamba2) as well
2. Add a test 

mamba2 😄 cc @molbap 

> That's a good catch! Can confirm the issue and solution (based on some local tests). For reference, the original mamba repo also points to this in https://github.com/state-spaces/mamba/blob/2e16fc3062cdcd4ebef27a9aa4442676e1c7edf4/mamba_ssm/ops/triton/ssd_chunk_scan.py#L1813-L1814 (when looking at the repeat pattern).
> 
> This also affects other mamba2 based models: Could you
> 
> 1. Fix the related models (mamba2, zamba2) as well

Sure, I can patch them as well.

> 2. Add a test

I haven't got much experiences writing tests for transformers. Would this be adding a unit test using a ngroups>1 in the model configuration to all of the models affected?


I think something along https://github.com/huggingface/transformers/blob/4cc6b6065426bdc66cac47bab1ffb26d3807fc53/tests/models/mamba2/test_modeling_mamba2.py#L237-L239

in mamba2 (by adjusting groups/heads for that test) would be sufficient as both bamba and zamba2 basically copied from mamba2. The models should be refactored tbh to allow modular to copy (but thats not in scope for this PR).

> Bamba-9B uses ngroups=1.

Yes, very likely why that was missed haha. Nice catch indeed, following here and yes we should modularize it all, will open an issue!

> I think something along
> 
> https://github.com/huggingface/transformers/blob/4cc6b6065426bdc66cac47bab1ffb26d3807fc53/tests/models/mamba2/test_modeling_mamba2.py#L237-L239
> 
> in mamba2 (by adjusting groups/heads for that test) would be sufficient as both bamba and zamba2 basically copied from mamba2. The models should be refactored tbh to allow modular to copy (but thats not in scope for this PR).

@vasqu I added a test and use half the default n_groups. I tested this locally on mamba2, and I was able to confirm that before the patch the test would fail and after the patch it would pass

run-slow: mamba2

This comment contains run-slow, running the specified jobs: This comment contains run-slow, running the specified jobs:

models: ['models/mamba2']
quantizations: [] ...

Hmm something went wrong on picking up the commit? Not familiar with the new workflow.

@molbap can we try slow runs again? 

Hey, I'm not sure about the new workflow either haha :D I mostly ran it locally to be sure, and it seems to not break, but would be preferrable to check on our runners. Retrying, cc @ydshieh is this the correct current launch? I thought it was (no need for labels, just ready PR + the message from a maintainer?

run-slow mamba2

run-slow mamba2

yes, but i think it should not mixed with other comment text. simply `run-slow: mamba2` with our without `:`

This comment contains run-slow, running the specified jobs: This comment contains run-slow, running the specified jobs:

models: ['models/mamba2']
quantizations: [] ...

all tests passing on the slow CI, congrats :raised_hands: 

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_37533). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

cc @Cyrilvallez for core maintainer review