class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
supports_shardq: ClassVar[bool] = True
can_return_lse_for_decode: bool = True
supports_dcp: bool = True
supports_mtp_with_cp_non_trivial_interleave_size: bool = True
@staticmethod
def _compute_fp8_decode_padded_heads(num_heads: int) -> int:
# FP8 decode kernel only supports h_q = 64 or 128
# Compute padded head count for decode
return 64 if num_heads <= 64 else 128
def __init__(
self,
num_heads: int,
head_size: int,
scale: float,
num_kv_heads: int,
alibi_slopes: list[float] | None,
sliding_window: int | None,
kv_cache_dtype: str,
logits_soft_cap: float | None,
attn_type: str,
kv_sharing_target_layer_name: str | None,
# MLA Specific Arguments
topk_indices_buffer: torch.Tensor | None = None,
indexer: "Indexer | None" = None,
**mla_args,
) -> None:
super().__init__(
num_heads,
head_size,
scale,
num_kv_heads,
alibi_slopes,
sliding_window,
kv_cache_dtype,
logits_soft_cap,
attn_type,
kv_sharing_target_layer_name,
indexer=indexer,
topk_indices_buffer=topk_indices_buffer,
**mla_args,
)
self.softmax_scale = scale
# Keep padded workspaces for kernels without small-head support.
self.prefill_padding = 64
self._sm_count = num_compute_units(torch.accelerator.current_device_index())
# The kernels read queries as wide as the cache rows. A NoPE model on
# fp8_ds_mla keeps the DeepSeek 576 layout: the cache writer zero-fills
# the RoPE slot and forward_mqa zero-pads q_pe to match (q_pe . 0 = 0).
self.q_head_size = head_size
if kv_cache_dtype == "fp8_ds_mla" and self.qk_rope_head_dim == 0:
self.q_head_size = self.kv_lora_rank + FP8_DS_MLA_ROPE_DIM
# Dense-MHA prefill would gather the cache into a
# kv_lora_rank-wide workspace, too narrow for the 576 rows.
self.supports_dense_mha_prefill = False
# ShardQ runs on the mixed-batch fp8 kernel.
self.use_shardq = (
self.use_shardq
and kv_cache_dtype in QUANTIZED_DS_MLA_CACHE_FORMATS
and num_heads < MIN_HEADS_FOR_BF16_PREFILL
)
vllm_config = get_current_vllm_config()
max_tokens = vllm_config.scheduler_config.max_num_batched_tokens
q_concat_heads = num_heads
if not is_quantized_kv_cache(kv_cache_dtype):
q_concat_heads = (
(num_heads + self.prefill_padding - 1)
// self.prefill_padding
* self.prefill_padding
)
q_concat_shape = (max_tokens, q_concat_heads, self.q_head_size)
if is_quantized_kv_cache(kv_cache_dtype):
assert kv_cache_dtype in QUANTIZED_DS_MLA_CACHE_FORMATS, (
"FlashMLA Sparse Attention backend only supports the "
f"{sorted(QUANTIZED_DS_MLA_CACHE_FORMATS)} quantized kv-cache "
f"dtypes, got {kv_cache_dtype}"
)
self.pcp_dcp_kv_gather = False
self.fp8_use_mixed_batch = False
self.workspace_specs: list[tuple[tuple[int, ...], torch.dtype]] = [
(q_concat_shape, torch.bfloat16)
]
if kv_cache_dtype in QUANTIZED_DS_MLA_CACHE_FORMATS:
assert vllm_config is not None and vllm_config.model_config is not None
parallel_config = vllm_config.parallel_config
self.pcp_dcp_kv_gather = (
parallel_config.prefill_context_parallel_size > 1
and parallel_config.decode_context_parallel_size > 1
)
# Same decision as the metadata builder.
self.fp8_use_mixed_batch = use_fp8_mixed_batch(
num_heads,
vllm_config.attention_config.hisparse_config is not None,
self.pcp_dcp_kv_gather,
)
if kv_cache_dtype in QUANTIZED_DS_MLA_CACHE_FORMATS and (
not self.fp8_use_mixed_batch
):
# Reserve the BF16 prefill workspace during initialization. Mixed
# batch mode attends straight from the fp8 cache and never uses it.
prefill_workspace_size = get_prefill_workspace_size(vllm_config)
shard_rows = prefill_workspace_size
if self.pcp_dcp_kv_gather:
# PCP+DCP upconverts this rank's KV shard, then all-gathers the
# shards into a workspace of the full prefill size.
shard_rows //= parallel_config.decode_context_parallel_size
self.prefill_workspace_shape = (shard_rows, self.q_head_size)
self.workspace_specs.append((self.prefill_workspace_shape, torch.bfloat16))
if self.pcp_dcp_kv_gather:
self.workspace_specs.append(
((prefill_workspace_size, self.q_head_size), torch.bfloat16)
)
prefill_query_heads = num_heads
if self.pcp_dcp_kv_gather and self.dcp_world_size > self.pcp_world_size:
prefill_query_heads *= parallel_config.tensor_parallel_size
padded_prefill_query_heads = round_up(
prefill_query_heads, self.prefill_padding
)
self.workspace_specs.extend(
(shape, torch.bfloat16)
for shape in (
(max_tokens, padded_prefill_query_heads, self.q_head_size),
(max_tokens, padded_prefill_query_heads, self.kv_lora_rank),
(max_tokens, prefill_query_heads, self.kv_lora_rank),
)
)
# Reserve capacity without retaining views that prevent old storage
# from being released when another layer grows the shared workspace.
current_workspace_manager().get_simultaneous(*self.workspace_specs)
# Transient peak of one mixed-batch kernel call, claimed in the profile
# run (which returns before attention). flash_mla_with_kvcache sizes
# its split-KV accumulators by b + num_sm_parts splits.
self.mixed_batch_profile_bytes = 0
if self.fp8_use_mixed_batch:
self.mixed_batch_profile_bytes = self._mixed_batch_peak_bytes(
max_tokens, num_heads
)
if self.use_shardq:
# A ShardQ call attends this rank's token slice with all heads.
# It owns at most two chunks of ceil(n / (2 * tp)) per request
# (allocate_shardq_buffers).
tp_size = vllm_config.parallel_config.tensor_parallel_size
max_reqs = vllm_config.scheduler_config.max_num_seqs
rank_tokens = min(max_tokens, -(-max_tokens // tp_size) + 2 * max_reqs)
self.mixed_batch_profile_bytes = max(
self.mixed_batch_profile_bytes,
self._mixed_batch_peak_bytes(rank_tokens, num_heads * tp_size),
)
def _mixed_batch_peak_bytes(self, num_tokens: int, num_heads: int) -> int:
heads = self._compute_fp8_decode_padded_heads(num_heads)
chunk = min(num_tokens, fp8_mixed_batch_chunk_tokens(num_heads))
num_splits = 1 + max(1, num_compute_units() // chunk)
per_token = (
heads * self.q_head_size * 2 # head-padded Q
+ heads * 512 * 2 # output
+ heads * 4 # LSE
+ num_splits * heads * (512 + 1) * 4 # o_accum, lse_accum
)
peak = chunk * per_token
if num_tokens > chunk:
# The chunked path's assembled output.
peak += num_tokens * num_heads * 512 * 2
return peak
def reserve_workspace(self) -> None:
if self.mixed_batch_profile_bytes > 0:
_ = torch.empty(
self.mixed_batch_profile_bytes,
dtype=torch.uint8,
device=current_platform.device_type,
)
def _forward_bf16_kv(
self,
q: torch.Tensor,
kv_c_and_k_pe_cache: torch.Tensor,
topk_indices: torch.Tensor,
attn_metadata: FlashMLASparseMetadata,
actual_num_heads: int,
) -> tuple[torch.Tensor, torch.Tensor | None]:
index_group = self.index_group
if isinstance(index_group, HiSparseMLAIndexGroup):
cache = index_group.cache(self.index_group_index)
else:
cache = None
block_table = attn_metadata.block_table
# req_id_per_token covers the whole batch; slice it to the MQA tokens
# (q may exclude prefill tokens routed to dense MHA).
req_id_per_token = attn_metadata.req_id_per_token[: topk_indices.shape[0]]
decode_out: torch.Tensor | None = None
if cache is not None:
assert isinstance(index_group, HiSparseMLAIndexGroup)
num_decode_tokens = attn_metadata.num_decode_tokens
if num_decode_tokens < q.shape[0] and cache.all_context_pages_resident:
physical_topk, lengths = index_group.convert_logical_to_physical_topk(
self.index_group_index,
topk_indices,
attn_metadata,
block_stride_rows=None,
return_valid_counts=True,
)
return self._bf16_flash_mla_kernel(
q,
index_group.physical_kv_cache(self.index_group_index),
physical_topk,
lengths,
actual_num_heads,
)
if num_decode_tokens > 0:
decode_topk, decode_lengths = (
index_group.convert_logical_to_physical_topk(
self.index_group_index,
topk_indices[:num_decode_tokens],
attn_metadata,
block_stride_rows=None,
return_valid_counts=True,
)
)
decode_out, _ = self._bf16_flash_mla_kernel(
q[:num_decode_tokens],
index_group.physical_kv_cache(self.index_group_index),
decode_topk,
decode_lengths,
actual_num_heads,
)
if num_decode_tokens == q.shape[0]:
return decode_out, None
q = q[num_decode_tokens:]
topk_indices = topk_indices[num_decode_tokens:]
kv_c_and_k_pe_cache, block_table, req_id_per_token = (
index_group.stage_prefill_rows(
self.index_group_index, kv_c_and_k_pe_cache, attn_metadata
)
)
# Convert per-request indices to global slots (decode) or workspace offsets.
kv_rows, block_stride_rows = flat_kv_row_view(
kv_c_and_k_pe_cache, attn_metadata.block_size
)
decode_only = (
attn_metadata.num_decode_tokens
== attn_metadata.num_actual_tokens
== topk_indices.shape[0]
)
uses_host_cache = isinstance(index_group, HiSparseMLAIndexGroup)
if self.dcp_world_size > 1:
# The indexer emits global token ids; keep this rank's shard and
# pack its local slots to the front of each row for topk_length.
topk_indices, topk_length = triton_filter_and_convert_dcp_index(
req_id_per_token,
block_table,
topk_indices,
dcp_size=self.dcp_world_size,
dcp_rank=self.dcp_rank,
cp_kv_cache_interleave_size=attn_metadata.cp_kv_cache_interleave_size,
BLOCK_SIZE=attn_metadata.block_size,
BLOCK_STRIDE_ROWS=block_stride_rows,
NUM_TOPK_TOKENS=topk_indices.shape[1],
return_valid_counts=True,
)
elif not uses_host_cache and decode_only:
topk_indices, topk_length = self._convert_logical_to_physical_topk(
topk_indices,
attn_metadata,
block_stride_rows=block_stride_rows,
return_valid_counts=True,
)
else:
topk_indices, topk_length = triton_convert_req_index_to_global_index(
req_id_per_token,
block_table,
topk_indices,
BLOCK_SIZE=attn_metadata.block_size,
BLOCK_STRIDE_ROWS=block_stride_rows,
NUM_TOPK_TOKENS=topk_indices.shape[1],
return_valid_counts=True,
)
attn_out, lse = self._bf16_flash_mla_kernel(
q,
kv_rows,
topk_indices,
topk_length,
actual_num_heads,
)
if self.need_to_return_lse_for_decode:
# The kernel returns (0, +inf) for rows where this rank owns none
# of the selected tokens; (0, -inf) is the identity element of the
# cross-rank LSE merge, so it drops this rank from those rows.
lse.masked_fill_((topk_length == 0).view(-1, 1), float("-inf"))
# The head-padding slice leaves `attn_out` non-contiguous, and the
# DCP combines (reduce_scatter, direct A2A) need packed heads.
return attn_out.contiguous(), lse
if decode_out is None:
return attn_out, lse
return torch.cat([decode_out, attn_out], dim=0), None
def _gather_prefill_chunk(
self,
chunk: "FlashMLASparseMetadata.FP8SeparatePrefillDecode.Prefill.Chunk",
shard: torch.Tensor,
topk_indices: torch.Tensor,
attn_metadata: FlashMLASparseMetadata,
prefill_meta: "FlashMLASparseMetadata.FP8SeparatePrefillDecode.Prefill",
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""All-gather this rank's upconverted KV shard so the chunk's rows attend
the whole context, and map their top-k onto the rank-major result."""
shard_rows = int(chunk.chunk_tot_seqlen)
_, _, gathered_kv_workspace, *_ = current_workspace_manager().get_simultaneous(
*self.workspace_specs
)
gathered_kv = gathered_kv_workspace[: self.dcp_world_size * shard_rows]
dist.all_gather_into_tensor(
gathered_kv, shard, group=get_dcp_group().device_group
)
tokens = chunk.tokens_slice
topk_indices, topk_length = triton_convert_req_index_to_global_index(
attn_metadata.req_id_per_token[tokens],
attn_metadata.block_table,
topk_indices,
BLOCK_SIZE=attn_metadata.block_size,
NUM_TOPK_TOKENS=topk_indices.shape[1],
HAS_PREFILL_WORKSPACE=True,
prefill_workspace_request_ids=prefill_meta.request_ids[tokens],
prefill_workspace_starts=prefill_meta.workspace_starts,
prefill_workspace_rank_stride=shard_rows,
dcp_size=self.dcp_world_size,
dcp_rank=self.dcp_rank,
cp_kv_cache_interleave_size=attn_metadata.cp_kv_cache_interleave_size,
return_valid_counts=True,
)
return gathered_kv, topk_indices, topk_length
def _forward_fp8_kv_separate_prefill_decode(
self,
q: torch.Tensor,
kv_c_and_k_pe_cache: torch.Tensor,
topk_indices: torch.Tensor,
attn_metadata: FlashMLASparseMetadata,
) -> tuple[torch.Tensor, torch.Tensor | None]:
"""The lse covers the decode rows and is only returned under PCP+DCP."""
fp8_metadata = attn_metadata.fp8_extra_metadata
assert isinstance(fp8_metadata, FlashMLASparseMetadata.FP8SeparatePrefillDecode)
num_decodes = fp8_metadata.num_decodes
num_mqa_tokens = q.shape[0]
num_decode_tokens = fp8_metadata.num_decode_tokens
num_prefill_tokens = num_mqa_tokens - num_decode_tokens
assert num_prefill_tokens in (0, fp8_metadata.num_prefill_tokens), (
"FP8 sparse MLA expects either the decode subset or the full batch"
)
decode_topk: torch.Tensor | None = None
index_group = self.index_group
uses_host_cache = isinstance(index_group, HiSparseMLAIndexGroup)
if uses_host_cache and num_decode_tokens > 0:
decode_topk = topk_indices[:num_decode_tokens]
prefill_ready = None
if num_prefill_tokens > 0 and uses_host_cache:
assert fp8_metadata.prefill is not None
first_chunk = fp8_metadata.prefill.chunks[0]
assert isinstance(index_group, HiSparseMLAIndexGroup)
_, prefill_bf16_workspace, *_ = (
current_workspace_manager().get_simultaneous(*self.workspace_specs)
)
prefill_ready = index_group.gather_fp8_prefill(
self.index_group_index,
kv_c_and_k_pe_cache,
prefill_bf16_workspace[: first_chunk.chunk_tot_seqlen],
first_chunk.block_table,
first_chunk.workspace_starts,
len(first_chunk.block_table),
attn_metadata,
first_chunk.req_start_idx,
)
prefill_request_ids = None
prefill_workspace_starts = None
has_prefill_workspace = False
if num_prefill_tokens > 0:
assert fp8_metadata.prefill is not None
prefill_request_ids = fp8_metadata.prefill.request_ids
prefill_workspace_starts = fp8_metadata.prefill.workspace_starts
has_prefill_workspace = True
# Convert per-request indices to global slots (decode) or workspace
# offsets (prefill).
# For FP8 cache: prefill uses workspace mapping (upconverted to BF16)
# For BF16 cache: always use global cache slots (no workspace)
# prefill_workspace_starts has been adjusted in-place per chunk so
# prefill indices automatically come out chunk-local
# Under PCP+DCP the decode rows are converted by the mixed-batch call
# and the prefill rows per chunk, against the gathered workspace.
lse: torch.Tensor | None = None
topk_length: torch.Tensor | None = None
kv_pages, block_stride_rows = _fp8_kv_pages(
kv_c_and_k_pe_cache, attn_metadata.block_size
)
if self.pcp_dcp_kv_gather:
pass
elif num_prefill_tokens == 0 and not uses_host_cache:
topk_indices, topk_length = self._convert_logical_to_physical_topk(
topk_indices,
attn_metadata,
block_stride_rows=block_stride_rows,
return_valid_counts=True,
)
elif num_prefill_tokens > 0:
topk_indices, topk_length = triton_convert_req_index_to_global_index(
attn_metadata.req_id_per_token[: topk_indices.shape[0]],
attn_metadata.block_table,
topk_indices,
BLOCK_SIZE=attn_metadata.block_size,
BLOCK_STRIDE_ROWS=block_stride_rows,
NUM_TOPK_TOKENS=topk_indices.shape[1],
HAS_PREFILL_WORKSPACE=has_prefill_workspace,
prefill_workspace_request_ids=prefill_request_ids,
prefill_workspace_starts=prefill_workspace_starts,
return_valid_counts=True,
)
fp8_metadata = attn_metadata.fp8_extra_metadata
assert isinstance(fp8_metadata, FlashMLASparseMetadata.FP8SeparatePrefillDecode)
def _fp8_decode(
q: torch.Tensor,
topk_indices: torch.Tensor,
) -> torch.Tensor:
assert fp8_metadata.decode is not None
if self.pcp_dcp_kv_gather:
# Attend this rank's KV shard; the LSE feeds the DCP merge.
nonlocal lse
out, lse = self._forward_fp8_kv_mixed_batch(
q,
kv_c_and_k_pe_cache,
topk_indices,
attn_metadata,
kernel_metadata=fp8_metadata.decode.kernel_metadata,
)
return out
if uses_host_cache:
return self._host_backed_fp8_decode(
q,
topk_indices,
attn_metadata,
fp8_metadata.decode.kernel_metadata,
num_decodes,
)
# Reshape q: (num_decode_tokens, num_heads, head_dim)
# -> (num_decodes, seq_len, num_heads, head_dim)
q = reshape_query_for_spec_decode(q, num_decodes)
seq_len = q.shape[1]
# Reshape topk_indices: (num_decode_tokens, topk)
# -> (num_decodes, seq_len, topk)
topk_indices = topk_indices.view(num_decodes, seq_len, -1)
attn_out, _ = self._fp8_flash_mla_kernel(
q=q,
kv_c_and_k_pe_cache=kv_pages,
topk_indices=topk_indices,
kernel_metadata=fp8_metadata.decode.kernel_metadata,
)
# Reshape output: (num_decodes, seq_len, num_heads, head_dim_v)
# -> (num_decode_tokens, num_heads, head_dim_v)
return reshape_attn_output_for_spec_decode(attn_out)
# Pure decode: direct call without allocation
if num_decode_tokens > 0 and num_prefill_tokens == 0:
assert fp8_metadata.decode is not None
attn_out = _fp8_decode(
q, decode_topk if decode_topk is not None else topk_indices
)
else:
_, prefill_bf16_workspace, *_, padded_out, compact_out = (
current_workspace_manager().get_simultaneous(*self.workspace_specs)
)
attn_out = compact_out[:num_mqa_tokens]
if num_decode_tokens > 0:
attn_out[:num_decode_tokens] = _fp8_decode(
q[:num_decode_tokens],
decode_topk
if decode_topk is not None
else topk_indices[:num_decode_tokens],
)
assert fp8_metadata.prefill is not None
for chunk_index, chunk in enumerate(fp8_metadata.prefill.chunks):
chunk_workspace = prefill_bf16_workspace[: chunk.chunk_tot_seqlen]
if uses_host_cache and chunk_index > 0:
assert isinstance(index_group, HiSparseMLAIndexGroup)
prefill_ready = index_group.gather_fp8_prefill(
self.index_group_index,
kv_c_and_k_pe_cache,
chunk_workspace,
chunk.block_table,
chunk.workspace_starts,
len(chunk.block_table),
attn_metadata,
chunk.req_start_idx,
)
if uses_host_cache:
assert prefill_ready is not None
current_stream().wait_event(prefill_ready)
elif self.kv_cache_dtype == "fp8_ds_mla":
ops.cp_gather_and_upconvert_fp8_kv_cache(
kv_c_and_k_pe_cache,
chunk_workspace,
chunk.block_table,
chunk.workspace_starts,
len(chunk.block_table),
)
else:
ops.cp_gather_and_upconvert_nvfp4_kv_cache(
kv_c_and_k_pe_cache.view(torch.uint8),
chunk_workspace,
chunk.block_table,
chunk.workspace_starts,
len(chunk.block_table),
)
chunk_q = q[chunk.tokens_slice]
if self.pcp_dcp_kv_gather:
chunk_workspace, chunk_topk_indices_workspace, chunk_topk_length = (
self._gather_prefill_chunk(
chunk,
chunk_workspace,
topk_indices[chunk.tokens_slice],
attn_metadata,
fp8_metadata.prefill,
)
)
else:
chunk_topk_indices_workspace = topk_indices[chunk.tokens_slice]
assert topk_length is not None
chunk_topk_length = topk_length[chunk.tokens_slice]
chunk_out, chunk_lse = self._bf16_flash_mla_kernel(
chunk_q,
chunk_workspace,
chunk_topk_indices_workspace,
chunk_topk_length,
out=padded_out[chunk.tokens_slice],
)
attn_out[chunk.tokens_slice].copy_(chunk_out)
del chunk_out, chunk_lse
if self.pcp_dcp_kv_gather and lse is None:
# No decode rows: the DCP merge still expects an LSE, of no rows.
lse = q.new_empty((0, q.shape[1]), dtype=torch.float32)
return attn_out, lse
def _forward_fp8_kv_mixed_batch(
self,
q: torch.Tensor,
kv_c_and_k_pe_cache: torch.Tensor,
topk_indices: torch.Tensor,
attn_metadata: FlashMLASparseMetadata,
kernel_metadata: "FlashMLASparseMetadata.FP8KernelMetadata | None" = None,
req_id_per_token: torch.Tensor | None = None,
) -> tuple[torch.Tensor, torch.Tensor | None]:
"""Mixed batch FP8 forward path that treats all tokens as one batch.
This is equivalent to main branch's approach and avoids the BF16
prefill kernel which has head padding overhead when num_heads is small.
Used when use_mixed_batch is True.
The lse is only returned when DCP needs it, otherwise None.
"""
fp8_metadata = kernel_metadata or attn_metadata.fp8_extra_metadata
assert isinstance(fp8_metadata, FlashMLASparseMetadata.FP8KernelMetadata)
block_table = attn_metadata.block_table
if req_id_per_token is None:
# req_id_per_token covers the whole batch; slice it to the MQA
# tokens (q may exclude prefill tokens routed to dense MHA).
req_id_per_token = attn_metadata.req_id_per_token[: topk_indices.shape[0]]
kv_pages, block_stride_rows = _fp8_kv_pages(
kv_c_and_k_pe_cache, attn_metadata.block_size
)
if self.dcp_world_size > 1:
# The indexer emits global token ids; keep this rank's shard and
# convert to local slots. compact_valid_to_front=False keeps the
# scattered -1s, which the fp8 kernel masks natively and the
# empty-row neutralization below relies on. req_id is sliced to
# topk_indices rows (the converter grids from req_id).
topk_indices = triton_filter_and_convert_dcp_index(
req_id_per_token,
block_table,
topk_indices,
dcp_size=self.dcp_world_size,
dcp_rank=self.dcp_rank,
cp_kv_cache_interleave_size=attn_metadata.cp_kv_cache_interleave_size,
BLOCK_SIZE=attn_metadata.block_size,
BLOCK_STRIDE_ROWS=block_stride_rows,
NUM_TOPK_TOKENS=topk_indices.shape[1],
compact_valid_to_front=False,
)
else:
# Convert per-request indices to global slots (decode) or workspace
# offsets (prefill).
decode_only = (
attn_metadata.num_decode_tokens
== attn_metadata.num_actual_tokens
== topk_indices.shape[0]
)
if decode_only:
topk_indices = self._convert_logical_to_physical_topk(
topk_indices,
attn_metadata,
block_stride_rows=block_stride_rows,
return_valid_counts=False,
)
else:
topk_indices = triton_convert_req_index_to_global_index(
req_id_per_token,
block_table,
topk_indices,
BLOCK_SIZE=attn_metadata.block_size,
BLOCK_STRIDE_ROWS=block_stride_rows,
NUM_TOPK_TOKENS=topk_indices.shape[1],
)
num_tokens = q.shape[0]
chunk_tokens = fp8_mixed_batch_chunk_tokens(q.shape[1])
if num_tokens <= chunk_tokens:
_attn_out, _lse = self._fp8_flash_mla_kernel(
q=q.unsqueeze(0), # add batch_dim: (T, H, D) -> (1, T, H, D)
kv_c_and_k_pe_cache=kv_pages,
topk_indices=topk_indices.unsqueeze(0), # (T, topk) -> (1, T, topk)
kernel_metadata=fp8_metadata,
)
# Output is (1, T, H, D_v), squeeze back to (T, H, D_v)
out = _attn_out.squeeze(0)
if _lse is not None:
_lse = _lse.squeeze(0)
else:
# The kernel's head-padded Q, output and split-KV accumulators grow
# with the token count (~400 KB per token at 64 padded heads), so
# long prefill steps run in chunks. Tokens attend independently
# (sparse indices), so chunking does not change the result.
out = q.new_empty((num_tokens, q.shape[1], 512))
_lse = q.new_empty((q.shape[1], num_tokens), dtype=torch.float32)
chunk_metas = fp8_metadata.chunk_scheduler_metadata
for start in range(0, num_tokens, chunk_tokens):
end = min(num_tokens, start + chunk_tokens)
# A scheduler metadata is reusable only for one query shape.
if (sched := chunk_metas.get(end - start)) is None:
sched = chunk_metas[end - start] = get_mla_metadata()[0]
chunk_out, chunk_lse = self._fp8_flash_mla_kernel(
q=q[start:end].unsqueeze(0),
kv_c_and_k_pe_cache=kv_pages,
topk_indices=topk_indices[start:end].unsqueeze(0),
kernel_metadata=replace(fp8_metadata, scheduler_metadata=sched),
)
out[start:end] = chunk_out[0]
_lse[:, start:end] = chunk_lse[0]
del chunk_out, chunk_lse
if not self.need_to_return_lse_for_decode:
return out, None
# Kernel LSE is (H, T); the DCP merge consumes (T, H).
lse = _lse.transpose(0, 1)
# Rows where this rank owns none of the selected tokens (all indices
# -1) have undefined out/lse; (0, -inf) is the identity element of the
# cross-rank LSE merge, so it drops this rank from those rows.
empty_rows = (topk_indices == -1).all(dim=-1)
out.masked_fill_(empty_rows.view(-1, 1, 1), 0.0)
lse.masked_fill_(empty_rows.view(-1, 1), float("-inf"))
# The head-padding slice above can leave `out` non-contiguous, and the
# DCP combines (reduce_scatter, direct A2A) need packed heads.
return out.contiguous(), lse
def _fp8_flash_mla_kernel(
self,
q: torch.Tensor,
kv_c_and_k_pe_cache: torch.Tensor,
topk_indices: torch.Tensor,
kernel_metadata: FlashMLASparseMetadata.FP8KernelMetadata,
) -> tuple[torch.Tensor, torch.Tensor]:
# q shape: (batch, seq_len, num_heads, head_dim)
actual_num_heads = q.size(2)
padded_num_heads = self._compute_fp8_decode_padded_heads(actual_num_heads)
# Pad query if needed (kernel only supports h_q = 64 or 128)
if actual_num_heads < padded_num_heads:
logger.warning_once(
f"Padding num_heads from {actual_num_heads} to "
f"{padded_num_heads} for FP8 sparse decode kernel"
)
q_padded = q.new_zeros((q.size(0), q.size(1), padded_num_heads, q.size(3)))
q_padded[:, :, :actual_num_heads, :] = q
q = q_padded
out, lse = flash_mla_with_kvcache(
q=q,
k_cache=kv_c_and_k_pe_cache.view(torch.uint8).unsqueeze(-2),
block_table=kernel_metadata.dummy_block_table,
head_dim_v=512,
cache_seqlens=kernel_metadata.cache_lens,
tile_scheduler_metadata=kernel_metadata.scheduler_metadata,
is_fp8_kvcache=True,
indices=topk_indices,
softmax_scale=self.softmax_scale,
)
# Slice output and lse back to actual head count if we padded
if actual_num_heads < padded_num_heads:
out = out[:, :, :actual_num_heads, :]
lse = lse[:, :actual_num_heads, :]
return out, lse
def _host_backed_fp8_decode(
self,
q: torch.Tensor,
topk_indices: torch.Tensor,
attn_metadata: FlashMLASparseMetadata,
kernel_metadata: FlashMLASparseMetadata.FP8KernelMetadata,
num_decodes: int,
) -> torch.Tensor:
assert isinstance(self.index_group, HiSparseMLAIndexGroup)
physical_topk = self.index_group.convert_logical_to_physical_topk(
self.index_group_index,
topk_indices,
attn_metadata,
block_stride_rows=None,
return_valid_counts=False,
)
assert isinstance(physical_topk, torch.Tensor)
q = reshape_query_for_spec_decode(q, num_decodes)
physical_topk = physical_topk.view(num_decodes, q.shape[1], -1)
output, _ = self._fp8_flash_mla_kernel(
q=q,
kv_c_and_k_pe_cache=self.index_group.physical_kv_cache(
self.index_group_index
),
topk_indices=physical_topk,
kernel_metadata=kernel_metadata,
)
return reshape_attn_output_for_spec_decode(output)
def _should_use_bf16_small_head_kernel(
self, num_tokens: int, actual_num_heads: int
) -> bool:
if not current_platform.is_device_capability_family(90):
return False
if actual_num_heads not in (8, 16, 32):
return False
# Each query launches one CTA. These empirical token-per-SM cutoffs
# favor padding for small batches; 32 heads use a higher cutoff.
# They are performance heuristics, not correctness requirements.
min_tokens_per_sm = 2 if actual_num_heads == 32 else 1
return num_tokens > self._sm_count * min_tokens_per_sm
def _bf16_flash_mla_kernel(
self,
q: torch.Tensor,
kv_c_and_k_pe_cache: torch.Tensor,
topk_indices: torch.Tensor,
topk_length: torch.Tensor | None = None,
actual_num_heads: int | None = None,
out: torch.Tensor | None = None,
) -> tuple[torch.Tensor, torch.Tensor]:
num_tokens = q.shape[0]
kv_c_and_k_pe_cache = kv_c_and_k_pe_cache.view(
-1, 1, kv_c_and_k_pe_cache.shape[-1]
)
# DCP can all-gather heads before this call.
if actual_num_heads is None:
actual_num_heads = q.shape[1]
padded_num_heads = (
(actual_num_heads + self.prefill_padding - 1)
// self.prefill_padding
* self.prefill_padding
)
if self._should_use_bf16_small_head_kernel(num_tokens, actual_num_heads):
q = q[:, :actual_num_heads]
if out is not None:
out = out[:, :actual_num_heads]
elif q.shape[1] < padded_num_heads:
logger.warning_once(
f"Padding num_heads from {actual_num_heads} to "
f"{padded_num_heads} for BF16 sparse prefill kernel"
)
if out is None:
q_padded = q.new_empty((q.shape[0], padded_num_heads, q.shape[2]))
else:
*_, q_padded, _, _ = current_workspace_manager().get_simultaneous(
*self.workspace_specs
)
q_padded = q_padded[:num_tokens]
q_padded[:, actual_num_heads:].zero_()
q_padded[:, :actual_num_heads, :] = q
q = q_padded
topk_indices = topk_indices.view(num_tokens, 1, -1)
output, _, lse = flash_mla_sparse_fwd(
q,
kv_c_and_k_pe_cache,
topk_indices,
self.softmax_scale,
topk_length=topk_length,
out=out,
)
output = output[:, :actual_num_heads, :]
lse = lse[:, :actual_num_heads]
return output, lse
def forward_mqa(
self,
q: torch.Tensor | tuple[torch.Tensor, torch.Tensor],
kv_c_and_k_pe_cache: torch.Tensor,
attn_metadata: FlashMLASparseMetadata,
layer: AttentionLayer,
) -> tuple[torch.Tensor, torch.Tensor | None]:
# NOTE(lucas): for the sparse FlashMLA kernels the kernels want to use
# MQA 576/512 approach for both prefill and decode
# Concatenate q if it's a tuple (ql_nope, q_pe)
actual_num_heads = self.num_heads
if isinstance(q, tuple):
ql_nope, q_pe = q
q_concat_buffer, *_ = current_workspace_manager().get_simultaneous(
*self.workspace_specs
)
q = q_concat_buffer[: ql_nope.shape[0]]
if q_pe.size(-1) == 0:
# NoPE (GLM5Next): concat_mla_q requires rope_dim == 64,
# copy directly into the head-padded buffer instead.
q[:, : ql_nope.shape[1], : self.kv_lora_rank].copy_(ql_nope)
if self.q_head_size > self.kv_lora_rank:
q[..., self.kv_lora_rank :].zero_()
else:
ops.concat_mla_q(ql_nope, q_pe, q)
else:
actual_num_heads = q.shape[1]
if q.shape[-1] < self.q_head_size:
# DCP gathers the unpadded NoPE query.
q = F.pad(q, (0, self.q_head_size - q.shape[-1]))
num_actual_toks = q.shape[0]
# Get topk indices
assert self.topk_indices_buffer is not None
topk_start = attn_metadata.topk_start
topk_indices = self.topk_indices_buffer[
topk_start : topk_start + num_actual_toks
]
use_fp8_cache = self.kv_cache_dtype in QUANTIZED_DS_MLA_CACHE_FORMATS
lse: torch.Tensor | None = None
if not use_fp8_cache:
attn_out, bf16_lse = self._forward_bf16_kv(
q,
kv_c_and_k_pe_cache,
topk_indices,
attn_metadata,
actual_num_heads,
)
if self.need_to_return_lse_for_decode:
lse = bf16_lse
elif attn_metadata.fp8_use_mixed_batch:
attn_out, lse = self._forward_fp8_kv_mixed_batch(
q, kv_c_and_k_pe_cache, topk_indices, attn_metadata
)
else:
attn_out, lse = self._forward_fp8_kv_separate_prefill_decode(
q, kv_c_and_k_pe_cache, topk_indices, attn_metadata
)
return attn_out, lse