-
-
Notifications
You must be signed in to change notification settings - Fork 16.7k
[Mamba] Flashinfer selective_state_update #36162
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
mgoin
merged 13 commits into
vllm-project:main
from
roikoren755:feat/flashinfer-selective-state-update
Apr 14, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b2c5d30
FI SSU kernel, wrapper, runtime dispatcher, config options and tests
roikoren755 3d4ee66
Add e2e tests
roikoren755 be9605c
Revert FI mamba SSM tests
roikoren755 e2bb66c
CR
roikoren755 e638350
Add SpecDec support
roikoren755 c2ea207
Fixes and tests
roikoren755 be90ea2
Default philox and revert tests
roikoren755 b9b0696
Better E2E tests
roikoren755 16b346e
Even better E2E tests
roikoren755 443e5c0
Internal CR
roikoren755 c5ff871
Small fix
roikoren755 a6ced90
CR fix
roikoren755 a111463
Revert one test
roikoren755 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from vllm.config.mamba import MambaBackendEnum, MambaConfig | ||
| from vllm.model_executor.layers.mamba.ops.ssu_dispatch import ( | ||
| FlashInferSSUBackend, | ||
| TritonSSUBackend, | ||
| get_mamba_ssu_backend, | ||
| initialize_mamba_ssu_backend, | ||
| selective_state_update, | ||
| ) | ||
| from vllm.utils.torch_utils import set_random_seed | ||
|
|
||
| try: | ||
| import flashinfer.mamba # noqa: F401 | ||
|
|
||
| HAS_FLASHINFER = True | ||
| except ImportError: | ||
| HAS_FLASHINFER = False | ||
|
|
||
|
|
||
| def test_default_backend_is_triton(): | ||
| initialize_mamba_ssu_backend(MambaConfig()) | ||
| backend = get_mamba_ssu_backend() | ||
| assert isinstance(backend, TritonSSUBackend) | ||
| assert backend.name == "triton" | ||
|
|
||
|
|
||
| def test_explicit_triton_backend(): | ||
| initialize_mamba_ssu_backend(MambaConfig(backend=MambaBackendEnum.TRITON)) | ||
| backend = get_mamba_ssu_backend() | ||
| assert isinstance(backend, TritonSSUBackend) | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not HAS_FLASHINFER, reason="flashinfer not installed") | ||
| def test_flashinfer_backend_init(): | ||
| initialize_mamba_ssu_backend(MambaConfig(backend=MambaBackendEnum.FLASHINFER)) | ||
| backend = get_mamba_ssu_backend() | ||
| assert isinstance(backend, FlashInferSSUBackend) | ||
| assert backend.name == "flashinfer" | ||
|
|
||
|
|
||
| def test_uninitialized_backend_raises(): | ||
| import vllm.model_executor.layers.mamba.ops.ssu_dispatch as mod | ||
|
|
||
| old = mod._mamba_ssu_backend | ||
| mod._mamba_ssu_backend = None | ||
| with pytest.raises(RuntimeError, match="not been initialized"): | ||
| get_mamba_ssu_backend() | ||
| mod._mamba_ssu_backend = old | ||
|
|
||
|
|
||
| @pytest.mark.skipif(HAS_FLASHINFER, reason="flashinfer is installed") | ||
| def test_flashinfer_import_error(): | ||
| with pytest.raises(ImportError, match="FlashInfer is required"): | ||
| FlashInferSSUBackend(MambaConfig()) | ||
|
|
||
|
|
||
| def test_triton_basic_call(): | ||
| set_random_seed(0) | ||
| initialize_mamba_ssu_backend(MambaConfig(backend=MambaBackendEnum.TRITON)) | ||
| device = "cuda" | ||
| batch_size = 2 | ||
| dim = 64 | ||
| dstate = 16 | ||
|
|
||
| state = torch.randn(batch_size, dim, dstate, device=device) | ||
|
roikoren755 marked this conversation as resolved.
|
||
| x = torch.randn(batch_size, dim, device=device) | ||
| out = torch.empty_like(x) | ||
| dt = torch.randn(batch_size, dim, device=device) | ||
| dt_bias = torch.rand(dim, device=device) - 4.0 | ||
| A = -torch.rand(dim, dstate, device=device) | ||
| B = torch.randn(batch_size, dstate, device=device) | ||
| C = torch.randn(batch_size, dstate, device=device) | ||
| D = torch.randn(dim, device=device) | ||
|
|
||
| selective_state_update( | ||
| state, | ||
| x, | ||
| dt, | ||
| A, | ||
| B, | ||
| C, | ||
| D=D, | ||
| dt_bias=dt_bias, | ||
| dt_softplus=True, | ||
| out=out, | ||
| ) | ||
| assert not torch.isnan(out).any() | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| from enum import Enum, EnumMeta | ||
| from typing import Any | ||
|
|
||
| from pydantic import field_validator | ||
|
|
||
| from vllm.config.utils import config | ||
|
|
||
|
|
||
| class _MambaBackendEnumMeta(EnumMeta): | ||
| """Metaclass for MambaBackendEnum to provide better error messages.""" | ||
|
|
||
| def __getitem__(cls, name: str): | ||
| try: | ||
| return super().__getitem__(name) | ||
| except KeyError: | ||
| valid = ", ".join(cls.__members__.keys()) | ||
| raise ValueError( | ||
| f"Unknown Mamba SSU backend: '{name}'. Valid options are: {valid}" | ||
| ) from None | ||
|
|
||
|
|
||
| class MambaBackendEnum(Enum, metaclass=_MambaBackendEnumMeta): | ||
| """Enumeration of supported Mamba SSU (selective state update) backends.""" | ||
|
|
||
| TRITON = "triton" | ||
| FLASHINFER = "flashinfer" | ||
|
|
||
|
|
||
| @config | ||
| class MambaConfig: | ||
|
roikoren755 marked this conversation as resolved.
|
||
| """Configuration for Mamba SSM backends.""" | ||
|
|
||
| backend: MambaBackendEnum = MambaBackendEnum.TRITON | ||
| """Mamba SSU backend to use.""" | ||
|
|
||
| enable_stochastic_rounding: bool = False | ||
| """Enable stochastic rounding when writing SSM state to fp16 cache. | ||
| Uses random bits to unbias the rounding error, which can improve | ||
| numerical stability for long sequences.""" | ||
| stochastic_rounding_philox_rounds: int = 0 | ||
| """Number of Philox PRNG rounds for stochastic rounding random number | ||
| generation. 0 uses the Triton default. Higher values improve randomness | ||
| quality at the cost of compute.""" | ||
|
|
||
| @field_validator("backend", mode="before") | ||
| @classmethod | ||
| def validate_backend_before(cls, value: Any) -> Any: | ||
| """Enable parsing of the `backend` enum type from string.""" | ||
| if isinstance(value, str): | ||
| return MambaBackendEnum[value.upper()] | ||
| return value | ||
|
|
||
| def __post_init__(self): | ||
| if self.enable_stochastic_rounding: | ||
| from vllm.platforms import current_platform | ||
|
|
||
| if not current_platform.is_cuda(): | ||
| raise ValueError( | ||
| "Stochastic rounding for Mamba cache is only supported " | ||
| "on NVIDIA CUDA platforms. Please do not specify " | ||
| "`--enable-mamba-cache-stochastic-rounding`." | ||
| ) | ||
| if ( | ||
| self.backend == MambaBackendEnum.TRITON | ||
| and not current_platform.is_device_capability_family(100) | ||
| ): | ||
| raise ValueError( | ||
| "Stochastic rounding for Mamba cache with triton backend requires " | ||
| "compute capability 10.0 (data center Blackwell). The `cvt.rs` " | ||
| "PTX instruction is not supported on your GPU. Please do not " | ||
| "specify `--enable-mamba-cache-stochastic-rounding`, " | ||
| "or set `--mamba-backend flashinfer`." | ||
| ) | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.