-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[Attention] Unify mamba and attention backend selection #23171
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
heheda12345
merged 1 commit into
vllm-project:main
from
ayushsatyam146:mamaba-attantion-backend-fix
Aug 25, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
tests/v1/attention/test_attention_backends_selection.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
"""Tests for mamba attention backend selectors.""" | ||
|
||
from types import SimpleNamespace | ||
|
||
import pytest | ||
|
||
from vllm.model_executor.layers.mamba.mamba_mixer import MambaMixer | ||
from vllm.model_executor.layers.mamba.mamba_mixer2 import MambaMixer2 | ||
from vllm.model_executor.layers.mamba.short_conv import ShortConv | ||
from vllm.model_executor.models.minimax_text_01 import ( | ||
MiniMaxText01LinearAttention) | ||
from vllm.v1.attention.backends.linear_attn import LinearAttentionBackend | ||
from vllm.v1.attention.backends.mamba1_attn import Mamba1AttentionBackend | ||
from vllm.v1.attention.backends.mamba2_attn import Mamba2AttentionBackend | ||
from vllm.v1.attention.backends.short_conv_attn import ( | ||
ShortConvAttentionBackend) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"layer_class, init_kwargs, expected_backend, expected_mamba_type", [ | ||
( | ||
MambaMixer, | ||
dict( | ||
hidden_size=128, | ||
ssm_state_size=16, | ||
conv_kernel_size=4, | ||
intermediate_size=256, | ||
time_step_rank=8, | ||
use_conv_bias=True, | ||
use_bias=False, | ||
use_rms_norm=True, | ||
), | ||
Mamba1AttentionBackend, | ||
"mamba1", | ||
), | ||
( | ||
MambaMixer2, | ||
dict( | ||
hidden_size=128, | ||
ssm_state_size=16, | ||
conv_kernel_size=4, | ||
intermediate_size=256, | ||
use_conv_bias=True, | ||
use_bias=False, | ||
n_groups=1, | ||
num_heads=8, | ||
head_dim=32, | ||
), | ||
Mamba2AttentionBackend, | ||
"mamba2", | ||
), | ||
( | ||
MiniMaxText01LinearAttention, | ||
dict( | ||
hidden_size=128, | ||
hidden_inner_size=256, | ||
num_heads=8, | ||
head_dim=32, | ||
max_position=2048, | ||
block_size=64, | ||
num_hidden_layer=12, | ||
layer_idx=0, | ||
linear_layer_idx=0, | ||
), | ||
LinearAttentionBackend, | ||
"linear_attention", | ||
), | ||
( | ||
ShortConv, | ||
dict( | ||
config=SimpleNamespace(conv_L_cache=32, conv_bias=True), | ||
dim=128, | ||
layer_idx=0, | ||
), | ||
ShortConvAttentionBackend, | ||
"short_conv", | ||
), | ||
]) | ||
def test_mamba_layers_get_attn_backend(dist_init, layer_class, init_kwargs, | ||
expected_backend, expected_mamba_type): | ||
"""Test that Mamba-like layers return the correct attention backend.""" | ||
layer = layer_class(**init_kwargs) | ||
|
||
backend_class = layer.get_attn_backend() | ||
assert backend_class is expected_backend | ||
assert layer.mamba_type == expected_mamba_type | ||
|
||
|
||
@pytest.mark.parametrize("layer_class,expected_backend,expected_mamba_type", [ | ||
(MambaMixer, Mamba1AttentionBackend, "mamba1"), | ||
(MambaMixer2, Mamba2AttentionBackend, "mamba2"), | ||
(MiniMaxText01LinearAttention, LinearAttentionBackend, "linear_attention"), | ||
(ShortConv, ShortConvAttentionBackend, "short_conv"), | ||
]) | ||
def test_mamba_layers_have_unified_interface(layer_class, expected_backend, | ||
expected_mamba_type): | ||
"""Test that all Mamba layers have the unified get_attn_backend | ||
interface.""" | ||
assert hasattr(layer_class, 'get_attn_backend'), ( | ||
f"{layer_class.__name__} should have get_attn_backend method") | ||
assert hasattr(layer_class, 'mamba_type'), ( | ||
f"{layer_class.__name__} should have mamba_type property") |
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
"""Base class for attention-like layers.""" | ||
from abc import ABC, abstractmethod | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from vllm.attention.backends.abstract import AttentionBackend | ||
|
||
|
||
class AttentionLayerBase(ABC): | ||
""" | ||
Base class for attention-like layers (Attention, Mamba, etc.) | ||
that support the v1 engine. | ||
|
||
This provides a common interface for getting attention backends | ||
from different layer types. | ||
""" | ||
|
||
@abstractmethod | ||
def get_attn_backend(self) -> type["AttentionBackend"]: | ||
"""Get the attention backend class for this layer.""" | ||
pass |
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
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.
Uh oh!
There was an error while loading. Please reload this page.