[Model] add GLM-5.3-Flash support (#53906)
This commit is contained in:
@@ -54,7 +54,10 @@ class _FlashInferMLASparseBackendBase(AttentionBackend):
|
||||
|
||||
@classmethod
|
||||
def get_supported_head_sizes(cls) -> list[int]:
|
||||
return [576]
|
||||
# 576 = 512 NoPE + 64 RoPE (with-rope layout); 512 = 512 NoPE only
|
||||
# (no-rope layout, qk_rope_head_dim == 0). Both share D_V = 512 and are
|
||||
# served by the TRTLLM-GEN sparse MLA kernel.
|
||||
return [512, 576]
|
||||
|
||||
@classmethod
|
||||
def is_mla(cls) -> bool:
|
||||
@@ -117,8 +120,20 @@ class FlashInferMLASparseTRTLLMBackend(_FlashInferMLASparseBackendBase):
|
||||
# FlashInfer MLA sparse SM10 kernel requires qk_nope_head_dim in [128, 192].
|
||||
if vllm_config.model_config is not None:
|
||||
hf_text_config = vllm_config.model_config.hf_text_config
|
||||
qk_nope_head_dim = getattr(hf_text_config, "qk_nope_head_dim", 1)
|
||||
if qk_nope_head_dim not in [128, 192]:
|
||||
qk_nope_head_dim = hf_text_config.qk_nope_head_dim
|
||||
qk_rope_head_dim = hf_text_config.qk_rope_head_dim
|
||||
kv_lora_rank = hf_text_config.kv_lora_rank
|
||||
if qk_rope_head_dim == 0:
|
||||
# Native no-rope MLA: FlashInfer only ships the one shape
|
||||
# (nope_mla_dimensions in flashinfer.mla._core).
|
||||
if qk_nope_head_dim != 256 or kv_lora_rank != 512:
|
||||
return (
|
||||
"FlashInfer native no-rope MLA requires "
|
||||
"qk_nope_head_dim=256 and kv_lora_rank=512, but got "
|
||||
f"qk_nope_head_dim={qk_nope_head_dim}, "
|
||||
f"kv_lora_rank={kv_lora_rank}"
|
||||
)
|
||||
elif qk_nope_head_dim not in [128, 192]:
|
||||
return (
|
||||
"FlashInfer MLA Sparse kernel requires qk_nope_head_dim "
|
||||
f"in [128, 192], but got {qk_nope_head_dim}"
|
||||
@@ -358,6 +373,10 @@ class FlashInferMLASparseImpl(SparseMLACommonImpl[FlashInferMLASparseMetadata]):
|
||||
self.bmm1_scale: float | None = None
|
||||
self.bmm2_scale: float | None = None
|
||||
|
||||
# Native no-rope MLA additionally requires a per-query-token active
|
||||
# top-k length tensor.
|
||||
self.is_nope_mla = self.qk_rope_head_dim == 0
|
||||
|
||||
# fp8 query quantization is required when using fp8 kv_cache,
|
||||
# as the TRTLLM-GEN sparse MLA kernel requires matching dtypes
|
||||
# for query and kv_cache (mixed bf16+fp8 is not supported).
|
||||
@@ -429,6 +448,31 @@ class FlashInferMLASparseImpl(SparseMLACommonImpl[FlashInferMLASparseMetadata]):
|
||||
block_tables = topk_indices_physical.unsqueeze(1)
|
||||
seq_lens_arg = seq_lens
|
||||
|
||||
# page_table width = topk buffer width, which kpool widens past
|
||||
# index_topk (topk_tokens) and rounds up to a multiple of 128. The
|
||||
# kernel treats sparse_mla_top_k as the page-table *capacity* and bounds
|
||||
# the active per-query length by ``seq_lens`` (the compacted valid
|
||||
# count), so the -1 padding slots past seq_lens are never attended to.
|
||||
# Use the actual buffer width instead of the fixed topk_tokens, which
|
||||
# mismatches the page_table when index_kpool > 1.
|
||||
sparse_topk_capacity = topk_indices_physical.shape[1]
|
||||
|
||||
extra_kwargs: dict[str, torch.Tensor] = {}
|
||||
empty_rows: torch.Tensor | None = None
|
||||
if self.is_nope_mla:
|
||||
# The native no-rope kernel takes the active top-k length per query
|
||||
# token (``seq_lens`` here is already the compacted per-token valid
|
||||
# count, int32) and rejects zero-length rows. Point empty rows at a
|
||||
# single valid dummy slot with length 1 and zero their output after
|
||||
# the launch. ``triton_convert_req_index_to_global_index`` packs the
|
||||
# valid indices into a contiguous prefix, which is what the kernel
|
||||
# requires of the page table.
|
||||
empty_rows = seq_lens == 0
|
||||
topk_indices_physical[:, 0] = topk_indices_physical[:, 0].masked_fill(
|
||||
empty_rows, 0
|
||||
)
|
||||
extra_kwargs["sparse_mla_top_k_lens"] = seq_lens.clamp(min=1)
|
||||
|
||||
kernel_out = trtllm_batch_decode_with_kv_cache_mla(
|
||||
query=query,
|
||||
kv_cache=kv_c_and_k_pe_cache.unsqueeze(1),
|
||||
@@ -438,11 +482,12 @@ class FlashInferMLASparseImpl(SparseMLACommonImpl[FlashInferMLASparseMetadata]):
|
||||
qk_rope_head_dim=self.qk_rope_head_dim,
|
||||
block_tables=block_tables,
|
||||
seq_lens=seq_lens_arg,
|
||||
max_seq_len=attn_metadata.topk_tokens,
|
||||
max_seq_len=sparse_topk_capacity,
|
||||
bmm1_scale=self.bmm1_scale,
|
||||
bmm2_scale=self.bmm2_scale,
|
||||
sparse_mla_top_k=attn_metadata.topk_tokens,
|
||||
sparse_mla_top_k=sparse_topk_capacity,
|
||||
return_lse=self.need_to_return_lse_for_decode,
|
||||
**extra_kwargs,
|
||||
)
|
||||
if self.need_to_return_lse_for_decode:
|
||||
assert isinstance(kernel_out, tuple)
|
||||
@@ -455,9 +500,12 @@ class FlashInferMLASparseImpl(SparseMLACommonImpl[FlashInferMLASparseMetadata]):
|
||||
out = o.view(-1, o.shape[-2], o.shape[-1])
|
||||
if lse is not None:
|
||||
lse = self._normalize_lse(lse, out.shape[0], out.shape[1])
|
||||
if empty_rows is None and lse is not None:
|
||||
empty_rows = (topk_indices_physical == -1).all(dim=-1)
|
||||
if empty_rows is not None:
|
||||
out.masked_fill_(empty_rows.view(-1, 1, 1), 0.0)
|
||||
lse.masked_fill_(empty_rows.view(-1, 1), float("-inf"))
|
||||
if lse is not None:
|
||||
lse.masked_fill_(empty_rows.view(-1, 1), float("-inf"))
|
||||
return out, lse
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user