Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions requirements/cuda.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ torchaudio==2.11.0
torchvision==0.26.0 # Required for phi3v processor. See https://github.com/pytorch/vision?tab=readme-ov-file#installation for corresponding version
PyNvVideoCodec==2.1.0
# FlashInfer should be updated together with the Dockerfile
flashinfer-python==0.6.13
flashinfer-cubin==0.6.13
flashinfer-python[cu13]==0.6.14
# flashinfer-cubin 0.6.14 is not published; use flashinfer-jit-cache cu130
# in CUDA 13 images for prebuilt kernels.
apache-tvm-ffi==0.1.9
tilelang==0.1.9
nvidia-cudnn-frontend>=1.19.1
Expand Down
77 changes: 77 additions & 0 deletions tests/model_executor/layers/test_sparse_attn_indexer_deep_gemm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

from types import SimpleNamespace

import torch

import vllm.model_executor.layers.sparse_attn_indexer as sparse_indexer
from vllm.model_executor.layers.sparse_attn_indexer import SparseAttnIndexer
from vllm.v1.attention.backend import AttentionCGSupport
from vllm.v1.attention.backends.mla import indexer as mla_indexer


def test_sparse_attn_indexer_allows_cuda_paged_mqa_fallback(
monkeypatch,
) -> None:
assert hasattr(sparse_indexer, "is_deep_gemm_paged_mqa_supported")

monkeypatch.setattr(sparse_indexer.current_platform, "is_cuda", lambda: True)
monkeypatch.setattr(
sparse_indexer, "is_deep_gemm_paged_mqa_supported", lambda: False
)
monkeypatch.setattr(
SparseAttnIndexer,
"dispatch_forward",
lambda *_args, **_kwargs: lambda *_args, **_kwargs: None,
)
monkeypatch.setattr(
sparse_indexer,
"get_current_vllm_config",
lambda: SimpleNamespace(
parallel_config=SimpleNamespace(
decode_context_parallel_size=1,
cp_kv_cache_interleave_size=1,
),
),
)

k_cache = SimpleNamespace(
prefix="test_k_cache",
kv_cache=torch.empty((1, 1, 1, 1), dtype=torch.uint8),
)

indexer = SparseAttnIndexer(
k_cache=k_cache,
quant_block_size=128,
scale_fmt="ue8m0",
topk_tokens=1,
head_dim=128,
max_model_len=16,
max_total_seq_len=16,
topk_indices_buffer=torch.empty((1, 1), dtype=torch.int32),
)

assert indexer.k_cache is k_cache


def test_deepseek_v32_indexer_disables_cudagraph_for_cuda_fallback(
monkeypatch,
) -> None:
monkeypatch.setattr(mla_indexer.current_platform, "is_cuda", lambda: True)
monkeypatch.setattr(mla_indexer, "is_deep_gemm_paged_mqa_supported", lambda: False)

assert (
mla_indexer.DeepseekV32IndexerMetadataBuilder.get_cudagraph_support(
SimpleNamespace(), SimpleNamespace()
)
is AttentionCGSupport.NEVER
)

monkeypatch.setattr(mla_indexer, "is_deep_gemm_paged_mqa_supported", lambda: True)
assert (
mla_indexer.DeepseekV32IndexerMetadataBuilder.get_cudagraph_support(
SimpleNamespace(), SimpleNamespace()
)
is AttentionCGSupport.UNIFORM_BATCH
)
38 changes: 38 additions & 0 deletions tests/utils/test_deep_gemm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

import pytest

import vllm.utils.deep_gemm as deep_gemm


@pytest.mark.parametrize(
("env_enabled", "has_module", "device_families", "expected"),
[
(True, True, {90}, True),
(True, True, {100}, True),
(True, True, {120}, False),
(False, True, {90}, False),
(True, False, {90}, False),
],
)
def test_deep_gemm_paged_mqa_support_arch_gate(
monkeypatch,
env_enabled: bool,
has_module: bool,
device_families: set[int],
expected: bool,
) -> None:
assert hasattr(deep_gemm, "is_deep_gemm_paged_mqa_supported")

monkeypatch.setattr(deep_gemm.envs, "VLLM_USE_DEEP_GEMM", env_enabled)
monkeypatch.setattr(deep_gemm, "has_deep_gemm", lambda: has_module)
monkeypatch.setattr(
deep_gemm.current_platform,
"is_device_capability_family",
lambda capability: capability in device_families,
)

deep_gemm.is_deep_gemm_paged_mqa_supported.cache_clear()

assert deep_gemm.is_deep_gemm_paged_mqa_supported() is expected
135 changes: 135 additions & 0 deletions tests/v1/attention/test_flashinfer_sparse_mla_sm120_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
import torch

from vllm.config import set_current_vllm_config
from vllm.model_executor.layers.attention.mla_attention import (
_canonicalize_sparse_mla_kv_cache_dtype,
)
from vllm.platforms.interface import DeviceCapability
from vllm.utils import flashinfer as fi_utils
from vllm.v1.attention.backends.mla import flashinfer_mla_sparse_sm120
from vllm.v1.attention.backends.mla.flashinfer_mla_sparse import (
FlashInferMLASparseSM120Backend,
)
from vllm.v1.attention.backends.mla.flashinfer_mla_sparse_sm120 import (
FlashInferMLASparseSM120Impl,
)
from vllm.v1.attention.backends.registry import AttentionBackendEnum


Expand All @@ -35,6 +42,11 @@ def test_v32_glm_sm120_backend_accepts_glm_block_size(
monkeypatch,
) -> None:
monkeypatch.setattr(fi_utils, "has_flashinfer_sparse_mla_sm120", lambda: True)
monkeypatch.setattr(
fi_utils,
"flashinfer_mla_decode_supports_kv_scale_format",
lambda: True,
)

with set_current_vllm_config(_fake_vllm_config("glm4_moe")):
invalid_reasons = FlashInferMLASparseSM120Backend.validate_configuration(
Expand All @@ -52,3 +64,126 @@ def test_v32_glm_sm120_backend_accepts_glm_block_size(
)

assert invalid_reasons == []


def test_sm120_standard_fp8_cache_uses_packed_flashinfer_shape() -> None:
assert (
_canonicalize_sparse_mla_kv_cache_dtype(FlashInferMLASparseSM120Backend, "auto")
== "fp8_ds_mla"
)
assert (
_canonicalize_sparse_mla_kv_cache_dtype(
FlashInferMLASparseSM120Backend, "fp8_e4m3"
)
== "fp8_ds_mla"
)
assert FlashInferMLASparseSM120Backend.get_kv_cache_shape(
num_blocks=2,
block_size=64,
num_kv_heads=1,
head_size=576,
cache_dtype_str="fp8_e4m3",
) == (2, 64, 576)
assert FlashInferMLASparseSM120Backend.get_kv_cache_shape(
num_blocks=2,
block_size=64,
num_kv_heads=1,
head_size=576,
cache_dtype_str="fp8_ds_mla",
) == (2, 64, 656)


def test_sm120_rejects_fp8_ds_mla_without_flashinfer_packed_support(
monkeypatch,
) -> None:
monkeypatch.setattr(fi_utils, "has_flashinfer_sparse_mla_sm120", lambda: True)
monkeypatch.setattr(
fi_utils,
"flashinfer_mla_decode_supports_kv_scale_format",
lambda: False,
)

with set_current_vllm_config(_fake_vllm_config("glm4_moe")):
invalid_reasons = FlashInferMLASparseSM120Backend.validate_configuration(
head_size=576,
dtype=torch.bfloat16,
kv_cache_dtype="fp8_ds_mla",
block_size=256,
use_mla=True,
has_sink=False,
use_sparse=True,
use_mm_prefix=False,
use_per_head_quant_scales=False,
device_capability=DeviceCapability(12, 0),
attn_type="decoder",
)

assert any("fp8_ds_mla" in reason for reason in invalid_reasons)


def test_sm120_impl_passes_packed_sparse_mla_args(
monkeypatch,
) -> None:
captured_kwargs = {}

def fake_decode(**kwargs):
captured_kwargs.update(kwargs)
return kwargs["out"]

monkeypatch.setattr(
fi_utils,
"flashinfer_mla_decode_supports_kv_scale_format",
lambda: True,
)
monkeypatch.setattr(
fi_utils,
"flashinfer_trtllm_batch_decode_with_kv_cache_mla",
fake_decode,
)
monkeypatch.setattr(
flashinfer_mla_sparse_sm120,
"_get_workspace_buffer",
lambda device: torch.empty((1,), device=device),
)
monkeypatch.setattr(
flashinfer_mla_sparse_sm120,
"triton_convert_req_index_to_global_index",
lambda *_args, **_kwargs: (
torch.zeros((1, 1), dtype=torch.int32),
torch.ones((1,), dtype=torch.int32),
),
)

impl = object.__new__(FlashInferMLASparseSM120Impl)
impl.num_heads = 1
impl.scale = 1.0
impl.kv_lora_rank = 1
impl.qk_nope_head_dim = 1
impl.qk_rope_head_dim = 0
impl.kv_scale_format = "arbitrary_fp32"
impl.topk_indices_buffer = torch.zeros((1, 1), dtype=torch.int32)
impl._workspace_buffer = None
impl.kv_cache_dtype = "fp8_ds_mla"

q = torch.empty((1, 1), dtype=torch.bfloat16)
kv_cache = torch.zeros((1, 1), dtype=torch.uint8)
metadata = SimpleNamespace(
req_id_per_token=torch.zeros((1,), dtype=torch.int32),
block_table=torch.zeros((1, 1), dtype=torch.int32),
block_size=1,
topk_tokens=1,
)
layer = SimpleNamespace(_q_scale_float=2.0, _k_scale_float=3.0)

output, _ = impl.forward_mqa(q, kv_cache, metadata, layer=layer)

assert output.shape == (1, 1, 1)
assert output.dtype == torch.bfloat16
assert captured_kwargs["query"].dtype == torch.bfloat16
assert captured_kwargs["kv_cache"].dtype == torch.uint8
assert captured_kwargs["bmm1_scale"] == 1.0
assert captured_kwargs["bmm2_scale"] == 1.0
assert captured_kwargs["seq_lens"].shape == (1,)
assert captured_kwargs["seq_lens"].dtype == torch.int32
assert captured_kwargs["kv_scale_format"] == "arbitrary_fp32"
assert "backend" not in captured_kwargs
1 change: 1 addition & 0 deletions vllm/model_executor/layers/attention/mla_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def _canonicalize_sparse_mla_kv_cache_dtype(
"auto",
"fp8",
"fp8_e4m3",
"fp8_ds_mla",
):
return "fp8_ds_mla"
return kv_cache_dtype
Expand Down
Loading
Loading