Skip to content

[V1] Enable Mamba2 layers other than MambaMixer2 in the v1 engine #20660

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

Merged
merged 4 commits into from
Jul 11, 2025

Conversation

nopperl
Copy link
Contributor

@nopperl nopperl commented Jul 9, 2025

Essential Elements of an Effective PR Description Checklist

  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.

Purpose

#19327 and #20016 enabled v1 support for Mamba2 and hybrid models. However, this currently only works for models using MambaMixer2, since it is hardcoded in two code paths. This prevents models with a custom Mamba2 layer (like PLaMo2) from using the v1 engine. This PR proposes to fix this problem with the following changes:

  • An abstract base class Mamba2Layer is introduced from which all Mamba2 layers which support the v1 engine can inherit. This allows to dynamically load these layers in the v1 engine instead of hardcoding MambaMixer2.
  • The Mamba2AttentionBackend now retrieves the chunk_size using the model config instead of accessing the mamba layer's attribute.

The core issue of this PR is also resolved without the second change, but I think it makes sense to include it since it reduces the indirection.

Related RFC: #17140
@heheda12345

Test Plan

tests/models/language/generation/test_hybrid.py

Test Result

Tests passed locally.

(Optional) Documentation Update

Run PLaMo2 with v1

To execute PLaMo2 using the v1 engine, first apply all of the following patches:

Then, run the following example script:

import os

os.environ["VLLM_USE_V1"] = "1"
os.environ["VLLM_ATTENTION_BACKEND"] = "FLASHINFER"

import torch

from vllm import LLM, SamplingParams

torch.manual_seed(123)

prompts = [
    "Hello, my name is",
    "The president of the United States is",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
model_id = "pfnet/plamo-2-1b"

llm = LLM(model=model_id,
          enable_chunked_prefill=True,
          trust_remote_code=True,
          max_model_len=4096,
          dtype="bfloat16",
          enable_prefix_caching=False,
          block_size=1072,
          enforce_eager=True)

outputs = llm.generate(prompts, sampling_params)

for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @nopperl, 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 significantly enhances the flexibility of the v1 engine by enabling support for a broader range of Mamba2 layer implementations beyond just MambaMixer2. It achieves this through the introduction of a new abstract base class for Mamba2 layers and a more centralized, configuration-driven approach to managing Mamba-specific parameters like chunk_size.

Highlights

  • Generalize Mamba2 Layer Support: Introduced an abstract base class Mamba2Layer from which all Mamba2 layers supporting the v1 engine can inherit. This allows the v1 engine to dynamically load various Mamba2 implementations, moving beyond the previous hardcoded dependency on MambaMixer2.
  • Centralized Mamba Chunk Size Retrieval: Refactored the Mamba2AttentionBackend to retrieve the chunk_size directly from the model's configuration (vllm.config.py) rather than from an attribute of the Mamba layer itself. This reduces indirection and simplifies configuration.
  • MambaMixer2 Adaptation: MambaMixer2 now inherits from the new Mamba2Layer abstract base class and no longer requires chunk_size to be passed during its initialization, as this value is now obtained globally from the model configuration.
  • Model-Specific Updates: Updated several model implementations (Bamba, FalconH1, GraniteMoEHybrid, Mamba2, NemotronH, Zamba2) to remove the direct passing of chunk_size when instantiating their respective MambaMixer2 layers, aligning with the centralized configuration approach.
Using Gemini Code Assist

The 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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and 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 to provide feedback.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@mergify mergify bot added the v1 label Jul 9, 2025
Copy link

github-actions bot commented Jul 9, 2025

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 introduces an abstract base class Mamba2Layer to enable support for various Mamba2 layers in the v1 engine, enhancing extensibility. It also refactors the Mamba2AttentionBackend to retrieve the chunk_size from the model config, centralizing configuration. The changes are well-structured and improve the flexibility of the v1 engine.

Comment on lines +56 to +59
self.chunk_size = runner.vllm_config.model_config.get_mamba_chunk_size(
)
assert self.chunk_size is not None, (
"chunk_size needs to be set in the model config for Mamba2 models")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider logging a warning message instead of raising an assertion error. This will allow the program to continue running, while still informing the user that there might be an issue with their configuration.

if self.chunk_size is None:
            logger.warning("chunk_size needs to be set in the model config for Mamba2 models")

@nopperl nopperl force-pushed the v1-custom-mamba2-layers branch from d754b7a to 420066e Compare July 9, 2025 03:40
nopperl added 2 commits July 9, 2025 03:51
…mbaMixer2 in v1 engine

Signed-off-by: nopperl <54780682+nopperl@users.noreply.github.com>
Signed-off-by: nopperl <54780682+nopperl@users.noreply.github.com>
@nopperl nopperl force-pushed the v1-custom-mamba2-layers branch from 420066e to e3af68a Compare July 9, 2025 03:52
@tdoublep
Copy link
Member

tdoublep commented Jul 9, 2025

Why does Plamo2 model not use the MambaMixer2 layer?

There is a to-do in the modeling code that it should be rebased:
https://github.com/vllm-project/vllm/blob/main/vllm/model_executor/models/plamo2.py#L121

Could that be a better solution than introducing more abstractions?

@nopperl
Copy link
Contributor Author

nopperl commented Jul 9, 2025

(The TODO comment was referring to the Mamba2 kernels, not the MambaMixer2 and is removed in #19674)

In theory it is possible to refactor the MambaMixer2 to support PLaMo2. However, there are signficant differences between Plamo2MambaMixer and MambaMixer2 (as explained here: #14323 (comment)). We're unsure if it is prudent to further complicate the MambaMixer2 just in order to support a single model. From upstream perspective, this change might be less intrusive.

(btw thanks for adding hybrid mamba support in the v1 engine @tdoublep !)

Copy link
Collaborator

@heheda12345 heheda12345 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I'm only concerned about the class name
@tdoublep I think the abstraction is necessary for adding other state space models like minimax.
@nopperl https://github.com/pfnet/vllm/tree/plamo2-follow-up-v1 For these changes, as we need to modify MambaMixer2 a lot to support piece-wise cuda graph, I think you can make the PR after we finish updating MambaMixer2.

Copy link

mergify bot commented Jul 10, 2025

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @nopperl.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify bot added the needs-rebase label Jul 10, 2025
Signed-off-by: nopperl <54780682+nopperl@users.noreply.github.com>
@mergify mergify bot removed the needs-rebase label Jul 11, 2025
Signed-off-by: nopperl <54780682+nopperl@users.noreply.github.com>
Copy link
Collaborator

@heheda12345 heheda12345 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much. Though LinearAttentionBase is more general, we are using "mamba" here and there and all of them should be changed if you decide to call it "LinearAttention".

@heheda12345 heheda12345 enabled auto-merge (squash) July 11, 2025 02:12
@github-actions github-actions bot added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 11, 2025
@nopperl
Copy link
Contributor Author

nopperl commented Jul 11, 2025

I understand, makes sense!

@heheda12345 heheda12345 merged commit 5d09152 into vllm-project:main Jul 11, 2025
80 checks passed
Chen-zexi pushed a commit to Chen-zexi/vllm that referenced this pull request Jul 13, 2025
…lm-project#20660)

Signed-off-by: nopperl <54780682+nopperl@users.noreply.github.com>
patrickvonplaten pushed a commit to patrickvonplaten/vllm that referenced this pull request Jul 15, 2025
…lm-project#20660)

Signed-off-by: nopperl <54780682+nopperl@users.noreply.github.com>
Signed-off-by: Patrick von Platen <patrick.v.platen@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready ONLY add when PR is ready to merge/full CI is needed v1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants