[3/N] HiSparse: host-resident sparse-MLA decode hot-buffering (#53781)
Signed-off-by: Matthew Bonanni <mbonanni@redhat.com> Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com> Signed-off-by: Lucas Wilkinson <lwilkinson@neuralmagic.com> Signed-off-by: Matthew Bonanni <matthew@neuralmagic.com> Signed-off-by: Nicolò Lucchesi <nicolo.lucchesi@mistral.ai> Co-authored-by: OpenAI Codex <codex@openai.com> Co-authored-by: Lucas Wilkinson <lwilkins@redhat.com> Co-authored-by: Lucas Wilkinson <lwilkinson@neuralmagic.com> Co-authored-by: Nicolò Lucchesi <nicolo.lucchesi@mistral.ai> Co-authored-by: OpenAI Codex <noreply@openai.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Lucas Wilkinson <LucasWilkinson@users.noreply.github.com>
This commit is contained in:
co-authored by
OpenAI Codex
Lucas Wilkinson
Lucas Wilkinson
Nicolò Lucchesi
OpenAI Codex
Claude Opus 5
Lucas Wilkinson
parent
120ec4ebd2
commit
d43bb2f37f
@@ -9,23 +9,23 @@ from vllm import _custom_ops as ops
|
||||
from vllm.config import VllmConfig, get_current_vllm_config
|
||||
from vllm.config.cache import CacheDType
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.layers.attention.mla_attention import MLACommonPrefillMetadata
|
||||
from vllm.model_executor.layers.attention.sparse_mla_attention import (
|
||||
SparseMLACommonImpl,
|
||||
SparseMLACommonMetadata,
|
||||
SparseMLACommonMetadataBuilder,
|
||||
)
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.platforms.interface import DeviceCapability
|
||||
from vllm.utils.platform_utils import num_compute_units
|
||||
from vllm.utils.torch_utils import is_quantized_kv_cache
|
||||
from vllm.utils.torch_utils import current_stream, is_quantized_kv_cache
|
||||
from vllm.v1.attention.backend import (
|
||||
AttentionBackend,
|
||||
AttentionCGSupport,
|
||||
AttentionLayer,
|
||||
AttentionMetadata,
|
||||
CommonAttentionMetadata,
|
||||
MultipleOf,
|
||||
)
|
||||
from vllm.v1.attention.backends.mla.index_group import HiSparseMLAIndexGroup
|
||||
from vllm.v1.attention.backends.mla.sparse_utils import (
|
||||
flat_kv_row_view,
|
||||
triton_convert_req_index_to_global_index,
|
||||
@@ -172,28 +172,7 @@ class FlashMLASparseBackend(AttentionBackend):
|
||||
|
||||
|
||||
@dataclass
|
||||
class FlashMLASparseMetadata(AttentionMetadata):
|
||||
num_reqs: int
|
||||
max_query_len: int
|
||||
max_seq_len: int
|
||||
|
||||
num_actual_tokens: int # Number of tokens excluding padding.
|
||||
query_start_loc: torch.Tensor
|
||||
slot_mapping: torch.Tensor
|
||||
|
||||
block_table: torch.Tensor
|
||||
req_id_per_token: torch.Tensor
|
||||
block_size: int = 64
|
||||
topk_tokens: int = 2048
|
||||
|
||||
num_decodes: int = 0
|
||||
num_prefills: int = 0
|
||||
num_decode_tokens: int = 0
|
||||
seq_lens: torch.Tensor | None = None
|
||||
prefill_max_seq_len: int = 0
|
||||
prefill: MLACommonPrefillMetadata | None = None
|
||||
cp_kv_cache_interleave_size: int = 1
|
||||
|
||||
class FlashMLASparseMetadata(SparseMLACommonMetadata):
|
||||
@dataclass
|
||||
class FP8KernelMetadata:
|
||||
scheduler_metadata: FlashMLASchedMeta
|
||||
@@ -233,6 +212,7 @@ class FlashMLASparseMetadata(AttentionMetadata):
|
||||
req_start_idx: int
|
||||
workspace_starts: torch.Tensor
|
||||
chunk_tot_seqlen: int
|
||||
seq_lens: torch.Tensor | None = None
|
||||
|
||||
chunks: list[Chunk]
|
||||
|
||||
@@ -263,6 +243,7 @@ class FlashMLASparseMetadataBuilder(
|
||||
):
|
||||
_cudagraph_support: ClassVar[AttentionCGSupport] = AttentionCGSupport.UNIFORM_BATCH
|
||||
require_uniform_decodes: ClassVar[bool] = True
|
||||
hisparse_supports_multi_token_decode: ClassVar[bool] = True
|
||||
metadata_cls = FlashMLASparseMetadata
|
||||
|
||||
def __init__(
|
||||
@@ -283,6 +264,9 @@ class FlashMLASparseMetadataBuilder(
|
||||
)
|
||||
else:
|
||||
threshold = {16: 128, 32: 128, 64: 256, 128: 256}.get(num_q_heads, 256)
|
||||
self.use_hisparse = vllm_config.attention_config.hisparse_config is not None
|
||||
if self.use_hisparse:
|
||||
threshold = 1
|
||||
# Varlen decodes are safe under DCP: causality comes from the
|
||||
# indexer's top-k indices, not from the kernel metadata.
|
||||
self._init_reorder_batch_threshold(
|
||||
@@ -347,7 +331,9 @@ class FlashMLASparseMetadataBuilder(
|
||||
device=device,
|
||||
)
|
||||
|
||||
self.fp8_use_mixed_batch = self.num_heads < MIN_HEADS_FOR_BF16_PREFILL
|
||||
self.fp8_use_mixed_batch = (
|
||||
self.num_heads < MIN_HEADS_FOR_BF16_PREFILL and not self.use_hisparse
|
||||
)
|
||||
|
||||
if parallel_config.decode_context_parallel_size > 1:
|
||||
if parallel_config.dcp_comm_backend != "ag_rs":
|
||||
@@ -385,37 +371,21 @@ class FlashMLASparseMetadataBuilder(
|
||||
|
||||
def _build_fp8_mixed_decode_prefill(
|
||||
self,
|
||||
common_attn_metadata: CommonAttentionMetadata,
|
||||
) -> "FlashMLASparseMetadata.FP8KernelMetadata":
|
||||
) -> FlashMLASparseMetadata.FP8KernelMetadata:
|
||||
"""Build FP8 metadata treating MQA tokens as one batch.
|
||||
|
||||
The scheduler initializes lazily from the runtime query shape, which may
|
||||
be the full batch or only decodes when prefills use dense MHA. This avoids
|
||||
the BF16 prefill kernel's head-padding overhead at high TP.
|
||||
"""
|
||||
num_tokens = common_attn_metadata.num_actual_tokens
|
||||
|
||||
# Use padded head count since that's what the kernel will see
|
||||
padded_heads = self.fp8_decode_padded_heads
|
||||
|
||||
# Build metadata for all tokens as a single batch
|
||||
scheduler_metadata, _ = get_mla_metadata(
|
||||
cache_seqlens=self.topk_tokens_tensor[:1], # Single batch
|
||||
num_q_tokens_per_head_k=num_tokens * padded_heads,
|
||||
topk=self.topk_tokens,
|
||||
num_heads_q=padded_heads,
|
||||
num_heads_k=1,
|
||||
is_fp8_kvcache=True,
|
||||
)
|
||||
|
||||
fp8_metadata = FlashMLASparseMetadata.FP8KernelMetadata(
|
||||
scheduler_metadata, _ = get_mla_metadata()
|
||||
return FlashMLASparseMetadata.FP8KernelMetadata(
|
||||
scheduler_metadata=scheduler_metadata,
|
||||
cache_lens=self.max_model_len_tensor[:1],
|
||||
dummy_block_table=self.dummy_block_table[:1],
|
||||
)
|
||||
|
||||
return fp8_metadata
|
||||
|
||||
def _build_fp8_separate_prefill_decode(
|
||||
self,
|
||||
common_attn_metadata: CommonAttentionMetadata,
|
||||
@@ -520,6 +490,9 @@ class FlashMLASparseMetadataBuilder(
|
||||
chunk_block_table = common_attn_metadata.block_table_tensor[
|
||||
num_decodes + chunk_start : num_decodes + chunk_end
|
||||
]
|
||||
chunk_seq_lens = common_attn_metadata.seq_lens[
|
||||
num_decodes + chunk_start : num_decodes + chunk_end
|
||||
]
|
||||
|
||||
prefill_chunks.append(
|
||||
FP8Meta.Prefill.Chunk(
|
||||
@@ -528,6 +501,7 @@ class FlashMLASparseMetadataBuilder(
|
||||
req_start_idx=chunk_start,
|
||||
workspace_starts=chunk_workspace_starts,
|
||||
chunk_tot_seqlen=chunk_tot_seqlen,
|
||||
seq_lens=chunk_seq_lens,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -569,9 +543,7 @@ class FlashMLASparseMetadataBuilder(
|
||||
metadata.fp8_use_mixed_batch = self.fp8_use_mixed_batch
|
||||
if self.use_fp8_kv_cache:
|
||||
if self.fp8_use_mixed_batch:
|
||||
metadata.fp8_extra_metadata = self._build_fp8_mixed_decode_prefill(
|
||||
common_attn_metadata
|
||||
)
|
||||
metadata.fp8_extra_metadata = self._build_fp8_mixed_decode_prefill()
|
||||
else:
|
||||
metadata.fp8_extra_metadata = self._build_fp8_separate_prefill_decode(
|
||||
common_attn_metadata, metadata
|
||||
@@ -679,30 +651,83 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
topk_indices: torch.Tensor,
|
||||
attn_metadata: FlashMLASparseMetadata,
|
||||
actual_num_heads: int,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
# Convert per-request indices to global slots (decode) or workspace
|
||||
# offsets (prefill). req_id_per_token covers the whole batch; slice it
|
||||
# to the MQA tokens (q may exclude prefill tokens routed to dense MHA).
|
||||
) -> 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 > 0:
|
||||
decode_topk, decode_lengths = (
|
||||
index_group.convert_decode_logical_to_physical_topk(
|
||||
self.index_group_index,
|
||||
topk_indices[:num_decode_tokens],
|
||||
attn_metadata,
|
||||
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
|
||||
)
|
||||
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],
|
||||
return_valid_counts=True,
|
||||
decode_only = (
|
||||
attn_metadata.num_decode_tokens
|
||||
== attn_metadata.num_actual_tokens
|
||||
== topk_indices.shape[0]
|
||||
)
|
||||
uses_host_cache = isinstance(index_group, HiSparseMLAIndexGroup)
|
||||
if 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,
|
||||
)
|
||||
|
||||
return self._bf16_flash_mla_kernel(
|
||||
attn_out, lse = self._bf16_flash_mla_kernel(
|
||||
q,
|
||||
kv_rows,
|
||||
topk_indices,
|
||||
topk_length,
|
||||
actual_num_heads,
|
||||
)
|
||||
if decode_out is None:
|
||||
return attn_out, lse
|
||||
return torch.cat([decode_out, attn_out], dim=0), None
|
||||
|
||||
def _forward_fp8_kv_separate_prefill_decode(
|
||||
self,
|
||||
@@ -721,6 +746,28 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
"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_ready = index_group.gather_fp8_prefill(
|
||||
self.index_group_index,
|
||||
kv_c_and_k_pe_cache,
|
||||
self.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
|
||||
@@ -736,17 +783,26 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
# 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
|
||||
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,
|
||||
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,
|
||||
)
|
||||
topk_length = None
|
||||
if 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=None,
|
||||
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,
|
||||
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)
|
||||
@@ -755,6 +811,16 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
q: torch.Tensor,
|
||||
topk_indices: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
assert fp8_metadata.decode is not None
|
||||
if uses_host_cache:
|
||||
return self._host_backed_fp8_decode(
|
||||
q,
|
||||
topk_indices,
|
||||
attn_metadata,
|
||||
fp8_metadata.decode.kernel_metadata,
|
||||
num_decodes,
|
||||
fp8_metadata.decode.decode_query_len,
|
||||
)
|
||||
# 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)
|
||||
@@ -762,7 +828,6 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
# Reshape topk_indices: (num_decode_tokens, topk)
|
||||
# -> (num_decodes, seq_len, topk)
|
||||
topk_indices = topk_indices.view(num_decodes, seq_len, -1)
|
||||
assert fp8_metadata.decode is not None
|
||||
attn_out, _ = self._fp8_flash_mla_kernel(
|
||||
q=q,
|
||||
kv_c_and_k_pe_cache=kv_c_and_k_pe_cache,
|
||||
@@ -776,7 +841,9 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
# 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, topk_indices)
|
||||
attn_out = _fp8_decode(
|
||||
q, decode_topk if decode_topk is not None else topk_indices
|
||||
)
|
||||
else:
|
||||
# Mixed or pure prefill: allocate output tensor
|
||||
attn_out = q.new_empty(
|
||||
@@ -788,13 +855,30 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
if num_decode_tokens > 0:
|
||||
attn_out[:num_decode_tokens] = _fp8_decode(
|
||||
q[:num_decode_tokens],
|
||||
topk_indices[: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 in fp8_metadata.prefill.chunks:
|
||||
for chunk_index, chunk in enumerate(fp8_metadata.prefill.chunks):
|
||||
chunk_workspace = self.prefill_bf16_workspace[: chunk.chunk_tot_seqlen]
|
||||
if self.kv_cache_dtype == "fp8_ds_mla":
|
||||
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,
|
||||
@@ -813,6 +897,7 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
|
||||
chunk_q = q[chunk.tokens_slice]
|
||||
chunk_topk_indices_workspace = topk_indices[chunk.tokens_slice]
|
||||
assert topk_length is not None
|
||||
chunk_topk_length = topk_length[chunk.tokens_slice]
|
||||
|
||||
attn_out[chunk.tokens_slice], _ = self._bf16_flash_mla_kernel(
|
||||
@@ -839,6 +924,17 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
|
||||
The lse is only returned when DCP needs it, otherwise None.
|
||||
"""
|
||||
assert attn_metadata.fp8_extra_metadata is not None
|
||||
assert isinstance(
|
||||
attn_metadata.fp8_extra_metadata,
|
||||
FlashMLASparseMetadata.FP8KernelMetadata,
|
||||
)
|
||||
fp8_metadata = attn_metadata.fp8_extra_metadata
|
||||
|
||||
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]]
|
||||
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
|
||||
@@ -846,8 +942,8 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
# 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(
|
||||
attn_metadata.req_id_per_token[: topk_indices.shape[0]],
|
||||
attn_metadata.block_table,
|
||||
req_id_per_token,
|
||||
block_table,
|
||||
topk_indices,
|
||||
dcp_size=self.dcp_world_size,
|
||||
dcp_rank=self.dcp_rank,
|
||||
@@ -859,19 +955,26 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
else:
|
||||
# Convert per-request indices to global slots (decode) or workspace
|
||||
# offsets (prefill).
|
||||
topk_indices = 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,
|
||||
NUM_TOPK_TOKENS=topk_indices.shape[1],
|
||||
decode_only = (
|
||||
attn_metadata.num_decode_tokens
|
||||
== attn_metadata.num_actual_tokens
|
||||
== topk_indices.shape[0]
|
||||
)
|
||||
|
||||
assert attn_metadata.fp8_extra_metadata is not None
|
||||
assert isinstance(
|
||||
attn_metadata.fp8_extra_metadata, FlashMLASparseMetadata.FP8KernelMetadata
|
||||
)
|
||||
fp8_metadata = attn_metadata.fp8_extra_metadata
|
||||
if decode_only:
|
||||
topk_indices = self._convert_logical_to_physical_topk(
|
||||
topk_indices,
|
||||
attn_metadata,
|
||||
block_stride_rows=None,
|
||||
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,
|
||||
NUM_TOPK_TOKENS=topk_indices.shape[1],
|
||||
)
|
||||
|
||||
_attn_out, _lse = self._fp8_flash_mla_kernel(
|
||||
q=q.unsqueeze(0), # unsqueeze to add batch_dim: (T, H, D) -> (1, T, H, D)
|
||||
@@ -937,6 +1040,37 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
|
||||
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,
|
||||
decode_query_len: int,
|
||||
) -> torch.Tensor:
|
||||
assert isinstance(self.index_group, HiSparseMLAIndexGroup)
|
||||
physical_topk = self.index_group.convert_decode_logical_to_physical_topk(
|
||||
self.index_group_index,
|
||||
topk_indices,
|
||||
attn_metadata,
|
||||
return_valid_counts=False,
|
||||
num_decodes=num_decodes,
|
||||
decode_query_len=decode_query_len,
|
||||
)
|
||||
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 _bf16_flash_mla_kernel(
|
||||
self,
|
||||
q: torch.Tensor,
|
||||
|
||||
Reference in New Issue
Block a user