[Cleanup][MLA] Remove FlashInfer DSpark DCP support (#53139)
Signed-off-by: Summer Yang <girasoleyang@gmail.com>
This commit is contained in:
@@ -886,79 +886,54 @@ def test_tokenspeed_mla_noncausal_capability():
|
|||||||
assert tokenspeed_mla_module.TokenspeedMLABackend.supports_non_causal()
|
assert tokenspeed_mla_module.TokenspeedMLABackend.supports_non_causal()
|
||||||
|
|
||||||
|
|
||||||
def test_flashinfer_mla_dcp_multi_token_decode_uses_per_query_bounds(monkeypatch):
|
def test_flashinfer_mla_dspark_support_is_tp_only(monkeypatch):
|
||||||
flashinfer_mla_module = pytest.importorskip(
|
flashinfer_mla_module = pytest.importorskip(
|
||||||
"vllm.v1.attention.backends.mla.flashinfer_mla"
|
"vllm.v1.attention.backends.mla.flashinfer_mla"
|
||||||
)
|
)
|
||||||
|
vllm_config = SimpleNamespace(
|
||||||
decode_call = None
|
speculative_config=SimpleNamespace(method="dspark"),
|
||||||
|
parallel_config=SimpleNamespace(decode_context_parallel_size=2),
|
||||||
def fake_decode(**kwargs):
|
model_config=None,
|
||||||
nonlocal decode_call
|
|
||||||
decode_call = kwargs
|
|
||||||
query = kwargs["query"]
|
|
||||||
output = torch.empty(*query.shape[:-1], 512, dtype=torch.bfloat16)
|
|
||||||
lse = torch.empty(query.shape[0], query.shape[-2], dtype=torch.float32)
|
|
||||||
return output, lse
|
|
||||||
|
|
||||||
monkeypatch.setattr(
|
|
||||||
flashinfer_mla_module,
|
|
||||||
"trtllm_batch_decode_with_kv_cache_mla",
|
|
||||||
fake_decode,
|
|
||||||
)
|
)
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr(
|
||||||
flashinfer_mla_module,
|
flashinfer_mla_module,
|
||||||
"_get_workspace_buffer",
|
"get_current_vllm_config",
|
||||||
lambda return_lse: torch.empty(1, dtype=torch.int8),
|
lambda: vllm_config,
|
||||||
)
|
)
|
||||||
|
|
||||||
impl = object.__new__(flashinfer_mla_module.FlashInferMLAImpl)
|
backend = flashinfer_mla_module.FlashInferMLABackend
|
||||||
impl.dcp_world_size = 2
|
builder = flashinfer_mla_module.FlashInferMLAMetadataBuilder
|
||||||
impl.dcp_rank = 1
|
reason = backend.supports_combination(
|
||||||
impl.cp_kv_cache_interleave_size = 1
|
head_size=576,
|
||||||
impl.need_to_return_lse_for_decode = True
|
dtype=torch.bfloat16,
|
||||||
impl.kv_lora_rank = 512
|
kv_cache_dtype="fp8",
|
||||||
impl.qk_nope_head_dim = 128
|
block_size=64,
|
||||||
impl.qk_rope_head_dim = 64
|
use_mla=True,
|
||||||
impl.bmm1_scale = 1.0
|
has_sink=False,
|
||||||
impl.bmm2_scale = 1.0
|
use_sparse=False,
|
||||||
|
use_mm_prefix=False,
|
||||||
block_table = torch.tensor([[1], [2]], dtype=torch.int32)
|
device_capability=SimpleNamespace(),
|
||||||
metadata = SimpleNamespace(
|
|
||||||
num_decodes=2,
|
|
||||||
num_decode_tokens=6,
|
|
||||||
max_seq_len=7,
|
|
||||||
causal=True,
|
|
||||||
decode=SimpleNamespace(
|
|
||||||
block_table=block_table,
|
|
||||||
seq_lens=torch.tensor([5, 6], dtype=torch.int32),
|
|
||||||
dcp_tot_seq_lens=torch.tensor([10, 13], dtype=torch.int32),
|
|
||||||
flattened_block_table=None,
|
|
||||||
flattened_seq_lens=None,
|
|
||||||
query_len=0,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
query = torch.empty(6, 2, 576, dtype=torch.bfloat16)
|
|
||||||
kv_cache = torch.empty(3, 16, 576, dtype=torch.bfloat16)
|
|
||||||
|
|
||||||
output, lse = impl.forward_mqa(
|
|
||||||
query,
|
|
||||||
kv_cache,
|
|
||||||
metadata,
|
|
||||||
SimpleNamespace(),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
assert output.shape == (6, 2, 512)
|
assert reason == "FlashInfer MLA does not support DSpark with DCP"
|
||||||
assert lse is not None
|
assert backend.supports_non_causal()
|
||||||
assert lse.shape == (6, 2)
|
assert builder.supports_non_causal_multi_token_decode
|
||||||
assert decode_call is not None
|
assert not backend.supports_non_causal_dcp()
|
||||||
assert decode_call["query"].shape == (6, 1, 2, 576)
|
|
||||||
torch.testing.assert_close(
|
vllm_config.parallel_config.decode_context_parallel_size = 1
|
||||||
decode_call["seq_lens"],
|
assert (
|
||||||
torch.tensor([4, 4, 5, 5, 6, 6], dtype=torch.int32),
|
backend.supports_combination(
|
||||||
)
|
head_size=576,
|
||||||
torch.testing.assert_close(
|
dtype=torch.bfloat16,
|
||||||
decode_call["block_tables"], block_table.repeat_interleave(3, dim=0)
|
kv_cache_dtype="fp8",
|
||||||
|
block_size=64,
|
||||||
|
use_mla=True,
|
||||||
|
has_sink=False,
|
||||||
|
use_sparse=False,
|
||||||
|
use_mm_prefix=False,
|
||||||
|
device_capability=SimpleNamespace(),
|
||||||
|
)
|
||||||
|
is None
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from typing import ClassVar
|
||||||
from typing import TYPE_CHECKING, ClassVar
|
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from flashinfer.decode import trtllm_batch_decode_with_kv_cache_mla
|
from flashinfer.decode import trtllm_batch_decode_with_kv_cache_mla
|
||||||
@@ -16,7 +15,6 @@ from vllm.config.cache import CacheDType
|
|||||||
from vllm.logger import init_logger
|
from vllm.logger import init_logger
|
||||||
from vllm.model_executor.layers.attention.mla_attention import (
|
from vllm.model_executor.layers.attention.mla_attention import (
|
||||||
MLACommonBackend,
|
MLACommonBackend,
|
||||||
MLACommonDecodeMetadata,
|
|
||||||
MLACommonImpl,
|
MLACommonImpl,
|
||||||
MLACommonMetadata,
|
MLACommonMetadata,
|
||||||
MLACommonMetadataBuilder,
|
MLACommonMetadataBuilder,
|
||||||
@@ -32,10 +30,6 @@ from vllm.v1.attention.backend import (
|
|||||||
)
|
)
|
||||||
from vllm.v1.attention.backends.utils import KVCacheLayoutType
|
from vllm.v1.attention.backends.utils import KVCacheLayoutType
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from vllm.config import VllmConfig
|
|
||||||
from vllm.v1.kv_cache_interface import AttentionSpec
|
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
logger = init_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@@ -118,56 +112,12 @@ def _get_multi_ctas_kv_counter_buffer(
|
|||||||
return _fi_multi_ctas_kv_counter
|
return _fi_multi_ctas_kv_counter
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
class FlashInferMLAMetadataBuilder(MLACommonMetadataBuilder[MLACommonMetadata]):
|
||||||
class FlashInferMLADecodeMetadata(MLACommonDecodeMetadata):
|
|
||||||
flattened_block_table: torch.Tensor | None = None
|
|
||||||
flattened_seq_lens: torch.Tensor | None = None
|
|
||||||
query_len: int = 0
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class FlashInferMLAMetadata(MLACommonMetadata[FlashInferMLADecodeMetadata]):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class FlashInferMLAMetadataBuilder(MLACommonMetadataBuilder[FlashInferMLAMetadata]):
|
|
||||||
_cudagraph_support: ClassVar[AttentionCGSupport] = AttentionCGSupport.UNIFORM_BATCH
|
_cudagraph_support: ClassVar[AttentionCGSupport] = AttentionCGSupport.UNIFORM_BATCH
|
||||||
query_len_support: ClassVar[QueryLenSupport] = QueryLenSupport.UNIFORM
|
query_len_support: ClassVar[QueryLenSupport] = QueryLenSupport.UNIFORM
|
||||||
# Non-causal DSpark blocks are flattened to single-token rows in forward_mqa.
|
# Non-causal DSpark blocks are flattened to single-token rows in forward_mqa.
|
||||||
supports_non_causal_multi_token_decode: ClassVar[bool] = True
|
supports_non_causal_multi_token_decode: ClassVar[bool] = True
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
kv_cache_spec: "AttentionSpec",
|
|
||||||
layer_names: list[str],
|
|
||||||
vllm_config: "VllmConfig",
|
|
||||||
device: torch.device,
|
|
||||||
) -> None:
|
|
||||||
super().__init__(
|
|
||||||
kv_cache_spec,
|
|
||||||
layer_names,
|
|
||||||
vllm_config,
|
|
||||||
device,
|
|
||||||
FlashInferMLAMetadata,
|
|
||||||
supports_dcp_with_varlen=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
def _build_decode(
|
|
||||||
self,
|
|
||||||
block_table_tensor: torch.Tensor,
|
|
||||||
seq_lens_device: torch.Tensor,
|
|
||||||
max_seq_len: int,
|
|
||||||
query_start_loc_cpu: torch.Tensor,
|
|
||||||
query_start_loc_device: torch.Tensor,
|
|
||||||
num_decode_tokens: int,
|
|
||||||
dcp_tot_seq_lens_device: torch.Tensor | None,
|
|
||||||
) -> FlashInferMLADecodeMetadata:
|
|
||||||
return FlashInferMLADecodeMetadata(
|
|
||||||
block_table=block_table_tensor,
|
|
||||||
seq_lens=seq_lens_device,
|
|
||||||
dcp_tot_seq_lens=dcp_tot_seq_lens_device,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class FlashInferMLABackend(MLACommonBackend):
|
class FlashInferMLABackend(MLACommonBackend):
|
||||||
supported_dtypes: ClassVar[list[torch.dtype]] = [torch.float16, torch.bfloat16]
|
supported_dtypes: ClassVar[list[torch.dtype]] = [torch.float16, torch.bfloat16]
|
||||||
@@ -224,10 +174,16 @@ class FlashInferMLABackend(MLACommonBackend):
|
|||||||
use_mm_prefix: bool,
|
use_mm_prefix: bool,
|
||||||
device_capability: DeviceCapability,
|
device_capability: DeviceCapability,
|
||||||
) -> str | None:
|
) -> str | None:
|
||||||
# FlashInfer MLA kernel requires qk_nope_head_dim in [64, 128, 192]
|
|
||||||
from vllm.config import get_current_vllm_config
|
|
||||||
|
|
||||||
vllm_config = get_current_vllm_config()
|
vllm_config = get_current_vllm_config()
|
||||||
|
speculative_config = vllm_config.speculative_config
|
||||||
|
if (
|
||||||
|
speculative_config is not None
|
||||||
|
and speculative_config.method == "dspark"
|
||||||
|
and vllm_config.parallel_config.decode_context_parallel_size > 1
|
||||||
|
):
|
||||||
|
return "FlashInfer MLA does not support DSpark with DCP"
|
||||||
|
|
||||||
|
# FlashInfer MLA kernel requires qk_nope_head_dim in [64, 128, 192]
|
||||||
if vllm_config.model_config is not None:
|
if vllm_config.model_config is not None:
|
||||||
hf_text_config = vllm_config.model_config.hf_text_config
|
hf_text_config = vllm_config.model_config.hf_text_config
|
||||||
qk_nope_head_dim = getattr(hf_text_config, "qk_nope_head_dim", 1)
|
qk_nope_head_dim = getattr(hf_text_config, "qk_nope_head_dim", 1)
|
||||||
@@ -243,7 +199,7 @@ class FlashInferMLABackend(MLACommonBackend):
|
|||||||
return "HND"
|
return "HND"
|
||||||
|
|
||||||
|
|
||||||
class FlashInferMLAImpl(MLACommonImpl[FlashInferMLAMetadata]):
|
class FlashInferMLAImpl(MLACommonImpl[MLACommonMetadata]):
|
||||||
can_return_lse_for_decode: bool = True
|
can_return_lse_for_decode: bool = True
|
||||||
# trtllm-gen MLA decode emits LSE in log2 (per flashinfer's own
|
# trtllm-gen MLA decode emits LSE in log2 (per flashinfer's own
|
||||||
# reference at flashinfer/trace/templates/attention.py:81:
|
# reference at flashinfer/trace/templates/attention.py:81:
|
||||||
@@ -314,7 +270,7 @@ class FlashInferMLAImpl(MLACommonImpl[FlashInferMLAMetadata]):
|
|||||||
self,
|
self,
|
||||||
q: torch.Tensor | tuple[torch.Tensor, torch.Tensor],
|
q: torch.Tensor | tuple[torch.Tensor, torch.Tensor],
|
||||||
kv_c_and_k_pe_cache: torch.Tensor,
|
kv_c_and_k_pe_cache: torch.Tensor,
|
||||||
attn_metadata: FlashInferMLAMetadata,
|
attn_metadata: MLACommonMetadata,
|
||||||
layer: AttentionLayer,
|
layer: AttentionLayer,
|
||||||
) -> tuple[torch.Tensor, torch.Tensor | None]:
|
) -> tuple[torch.Tensor, torch.Tensor | None]:
|
||||||
assert kv_c_and_k_pe_cache.numel() > 0
|
assert kv_c_and_k_pe_cache.numel() > 0
|
||||||
@@ -326,28 +282,15 @@ class FlashInferMLAImpl(MLACommonImpl[FlashInferMLAMetadata]):
|
|||||||
|
|
||||||
block_table = attn_metadata.decode.block_table
|
block_table = attn_metadata.decode.block_table
|
||||||
seq_lens = attn_metadata.decode.seq_lens
|
seq_lens = attn_metadata.decode.seq_lens
|
||||||
query_len = attn_metadata.num_decode_tokens // attn_metadata.num_decodes
|
|
||||||
|
|
||||||
if not attn_metadata.causal:
|
if not attn_metadata.causal:
|
||||||
# Non-causal DSpark block: flatten to single-token decode rows with
|
# FlashInfer decode has no causal flag. For TP-only DSpark, flatten
|
||||||
# per-row context seq_lens (trtllm-gen has no causal flag and would
|
# each non-causal query block into independent single-token rows.
|
||||||
# otherwise mask the block causally).
|
query_len = attn_metadata.num_decode_tokens // attn_metadata.num_decodes
|
||||||
q = q.unsqueeze(1)
|
q = q.unsqueeze(1)
|
||||||
if query_len > 1:
|
if query_len > 1:
|
||||||
block_table, seq_lens = self._prepare_flattened_decode_metadata(
|
block_table = block_table.repeat_interleave(query_len, dim=0)
|
||||||
attn_metadata,
|
seq_lens = seq_lens.repeat_interleave(query_len)
|
||||||
query_len,
|
|
||||||
causal=False,
|
|
||||||
)
|
|
||||||
elif self.dcp_world_size > 1 and query_len > 1:
|
|
||||||
# Causal DCP block: flatten to single-token decode rows with
|
|
||||||
# per-row rank-local seq_lens for each query's visible prefix.
|
|
||||||
block_table, seq_lens = self._prepare_flattened_decode_metadata(
|
|
||||||
attn_metadata,
|
|
||||||
query_len,
|
|
||||||
causal=True,
|
|
||||||
)
|
|
||||||
q = q.unsqueeze(1)
|
|
||||||
# trtllm API requires extra dimension q_len_per_request for MTP
|
# trtllm API requires extra dimension q_len_per_request for MTP
|
||||||
elif attn_metadata.num_decode_tokens % attn_metadata.num_decodes != 0:
|
elif attn_metadata.num_decode_tokens % attn_metadata.num_decodes != 0:
|
||||||
logger.warning_once(
|
logger.warning_once(
|
||||||
@@ -420,49 +363,3 @@ class FlashInferMLAImpl(MLACommonImpl[FlashInferMLAMetadata]):
|
|||||||
o = o.view(-1, o.shape[-2], o.shape[-1])
|
o = o.view(-1, o.shape[-2], o.shape[-1])
|
||||||
|
|
||||||
return o, lse
|
return o, lse
|
||||||
|
|
||||||
def _prepare_flattened_decode_metadata(
|
|
||||||
self,
|
|
||||||
attn_metadata: FlashInferMLAMetadata,
|
|
||||||
query_len: int,
|
|
||||||
*,
|
|
||||||
causal: bool,
|
|
||||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
||||||
"""Prepare flattened decode metadata once for all layers in the group."""
|
|
||||||
decode = attn_metadata.decode
|
|
||||||
assert decode is not None
|
|
||||||
if decode.query_len:
|
|
||||||
assert decode.query_len == query_len
|
|
||||||
assert decode.flattened_block_table is not None
|
|
||||||
assert decode.flattened_seq_lens is not None
|
|
||||||
return decode.flattened_block_table, decode.flattened_seq_lens
|
|
||||||
|
|
||||||
block_table = decode.block_table.repeat_interleave(query_len, dim=0)
|
|
||||||
if causal:
|
|
||||||
global_seq_lens = decode.dcp_tot_seq_lens
|
|
||||||
assert global_seq_lens is not None
|
|
||||||
offsets = torch.arange(
|
|
||||||
query_len - 1,
|
|
||||||
-1,
|
|
||||||
-1,
|
|
||||||
device=global_seq_lens.device,
|
|
||||||
dtype=global_seq_lens.dtype,
|
|
||||||
)
|
|
||||||
per_query_global_lens = torch.clamp(
|
|
||||||
(global_seq_lens.unsqueeze(1) - offsets).reshape(-1), min=0
|
|
||||||
)
|
|
||||||
interleave = self.cp_kv_cache_interleave_size
|
|
||||||
dcp_span = self.dcp_world_size * interleave
|
|
||||||
remainder = torch.clamp(
|
|
||||||
per_query_global_lens % dcp_span - self.dcp_rank * interleave,
|
|
||||||
min=0,
|
|
||||||
max=interleave,
|
|
||||||
)
|
|
||||||
seq_lens = per_query_global_lens // dcp_span * interleave + remainder
|
|
||||||
else:
|
|
||||||
seq_lens = decode.seq_lens.repeat_interleave(query_len)
|
|
||||||
|
|
||||||
decode.flattened_block_table = block_table
|
|
||||||
decode.flattened_seq_lens = seq_lens
|
|
||||||
decode.query_len = query_len
|
|
||||||
return block_table, seq_lens
|
|
||||||
|
|||||||
Reference in New Issue
Block a user