[ROCm][MLA] Enable sparse MLA Gluon kernel from Aiter (#53492)
Signed-off-by: Mehmet Cagri Kaymak <mehmet.kaymak@amd.com> Signed-off-by: Mehmet Cagri <mehmet.kaymak@amd.com>
This commit is contained in:
@@ -396,9 +396,11 @@ class ROCMAiterMLASparseMetadataBuilder(
|
||||
attention_context = vllm_config.compilation_config.static_forward_context
|
||||
# Sink decode must use AITER's nonpersistent path. In particular,
|
||||
# gfx942 has no persistent+LSE kernel, and its metadata heuristic
|
||||
# terminates for HY-V4's TP1 H64 shape.
|
||||
# terminates for HY-V4's TP1 H64 shape. The Triton sparse MLA kernel
|
||||
# never reads the persistent metadata either.
|
||||
self._use_persistent_metadata = all(
|
||||
getattr(attention_context[name].impl, "sinks", None) is None
|
||||
and not getattr(attention_context[name].impl, "use_aiter_sparse_mla", False)
|
||||
for name in layer_names
|
||||
)
|
||||
# Bounds the KV-split heuristic (see `_sparse_decode_max_split`).
|
||||
@@ -716,6 +718,7 @@ class ROCMAiterMLASparseImpl(
|
||||
is_sparse = True
|
||||
supports_dense_mha_prefill = False
|
||||
supports_dcp = False
|
||||
use_aiter_sparse_mla = False
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -765,6 +768,32 @@ class ROCMAiterMLASparseImpl(
|
||||
(self.q_concat_buffer,) = current_workspace_manager().get_simultaneous(
|
||||
(q_concat_shape, vllm_config.model_config.dtype),
|
||||
)
|
||||
self.qk_rope_head_dim: int = mla_args["qk_rope_head_dim"]
|
||||
|
||||
if rocm_aiter_ops.is_triton_sparse_mla_enabled():
|
||||
reason = self._aiter_sparse_mla_unsupported_reason(vllm_config)
|
||||
if reason is None:
|
||||
self.use_aiter_sparse_mla = True
|
||||
else:
|
||||
logger.warning_once(
|
||||
"VLLM_ROCM_USE_AITER_TRITON_SPARSE_MLA is set, but %s; "
|
||||
"using the default sparse MLA path instead.",
|
||||
reason,
|
||||
)
|
||||
|
||||
def _aiter_sparse_mla_unsupported_reason(
|
||||
self, vllm_config: VllmConfig
|
||||
) -> str | None:
|
||||
model_dtype = vllm_config.model_config.dtype
|
||||
if model_dtype != torch.bfloat16:
|
||||
return f"the model dtype is {model_dtype}, not bfloat16"
|
||||
cache_dtype = self.kv_cache_dtype
|
||||
if cache_dtype not in ("auto", "bfloat16", "fp8", "fp8_e4m3"):
|
||||
return f"kv-cache dtype {cache_dtype!r} is neither bfloat16 nor e4m3 fp8"
|
||||
# Context parallelism merges each rank's partials with a per-token LSE.
|
||||
if self.dcp_world_size > 1 or self.pcp_world_size > 1:
|
||||
return "context parallelism is not supported"
|
||||
return None
|
||||
|
||||
def record_logical_topk_ready(self) -> None:
|
||||
# This impl shares the top-k indices buffer via SharedTopkIndicesBuffer
|
||||
@@ -975,6 +1004,43 @@ class ROCMAiterMLASparseImpl(
|
||||
|
||||
return output, lse
|
||||
|
||||
def _forward_mla_aiter(
|
||||
self,
|
||||
layer: AttentionLayer,
|
||||
q: torch.Tensor, # [sq, heads, d_qk], not head-padded
|
||||
kv_c_and_k_pe_cache: torch.Tensor,
|
||||
attn_metadata: ROCMAiterMLASparseMetadata,
|
||||
) -> torch.Tensor:
|
||||
"""_forward_mla on aiter's Triton sparse MLA kernel.
|
||||
|
||||
Reads the same index stream, but needs no q head padding and no
|
||||
persistent metadata, and its launch depends on shapes only, so it is
|
||||
CUDA-graph capturable.
|
||||
"""
|
||||
num_actual_tokens = attn_metadata.num_actual_tokens
|
||||
output = torch.empty(
|
||||
[q.shape[0], self.num_heads, self.kv_lora_rank],
|
||||
dtype=attn_metadata.attn_out_dtype,
|
||||
device=q.device,
|
||||
)
|
||||
rocm_aiter_ops.triton_sparse_mla_fwd(
|
||||
q[:num_actual_tokens],
|
||||
kv_c_and_k_pe_cache.view(-1, 1, 1, q.shape[-1]),
|
||||
output[:num_actual_tokens],
|
||||
self.scale,
|
||||
attn_metadata.paged_kv_indptr,
|
||||
attn_metadata.paged_kv_indices,
|
||||
kv_lora_rank=self.kv_lora_rank,
|
||||
qk_rope_head_dim=self.qk_rope_head_dim,
|
||||
q_scale=layer._q_scale,
|
||||
kv_scale=layer._k_scale,
|
||||
attn_sink=self.sinks,
|
||||
# triton_convert_req_index_to_global_index writes 0, never -1,
|
||||
# for an invalid top-k entry, so no slot in the stream is negative.
|
||||
has_invalid=False,
|
||||
)
|
||||
return output
|
||||
|
||||
def forward_mqa(
|
||||
self,
|
||||
q: torch.Tensor | tuple[torch.Tensor, torch.Tensor],
|
||||
@@ -1026,6 +1092,11 @@ class ROCMAiterMLASparseImpl(
|
||||
original_q_shape = q.shape
|
||||
q, _ = ops.scaled_fp8_quant(q.view(q.shape[0], -1), layer._q_scale)
|
||||
q = q.view(original_q_shape)
|
||||
if self.use_aiter_sparse_mla:
|
||||
output = self._forward_mla_aiter(
|
||||
layer, q, kv_c_and_k_pe_cache, attn_metadata
|
||||
)
|
||||
return output, None
|
||||
mla_padded_q = AiterMLAHelper.get_mla_padded_q(self.num_heads, q)
|
||||
return self._forward_mla(
|
||||
layer, mla_padded_q, kv_c_and_k_pe_cache, attn_metadata
|
||||
|
||||
@@ -4306,3 +4306,99 @@ def rocm_sparse_attn_decode(
|
||||
if direct_out is None:
|
||||
output.copy_(attn_out.to(output.dtype))
|
||||
return output.shape[0] if inv_rope_positions is not None else 0
|
||||
|
||||
|
||||
@triton.jit(do_not_specialize=["token_offset"])
|
||||
def _prefill_topk_global_slots_kernel(
|
||||
out_ptr,
|
||||
lens_ptr,
|
||||
topk_ptr,
|
||||
topk_stride,
|
||||
token_to_req_ptr,
|
||||
query_start_loc_ptr,
|
||||
seq_lens_ptr,
|
||||
is_valid_token_ptr,
|
||||
block_table_ptr,
|
||||
block_table_stride,
|
||||
token_offset,
|
||||
num_compressed,
|
||||
TOPK: tl.constexpr,
|
||||
COMPRESS_RATIO: tl.constexpr,
|
||||
BLOCK_SIZE: tl.constexpr,
|
||||
BLOCK_W: tl.constexpr,
|
||||
):
|
||||
row = tl.program_id(0)
|
||||
token = row + token_offset
|
||||
req = tl.load(token_to_req_ptr + token)
|
||||
query_start = tl.load(query_start_loc_ptr + req)
|
||||
query_len = tl.load(query_start_loc_ptr + req + 1) - query_start
|
||||
pos = tl.load(seq_lens_ptr + req) - query_len + token - query_start
|
||||
# Only (pos + 1) // ratio compressed tokens exist yet; the rest of the row
|
||||
# can hold stale indices.
|
||||
row_len = tl.minimum((pos + 1) // COMPRESS_RATIO, TOPK)
|
||||
row_len = tl.where(tl.load(is_valid_token_ptr + token), row_len, 0)
|
||||
|
||||
for start in range(0, TOPK, BLOCK_W):
|
||||
cols = start + tl.arange(0, BLOCK_W)
|
||||
live = cols < row_len
|
||||
local = tl.load(topk_ptr + row * topk_stride + cols, mask=live, other=-1)
|
||||
valid = live & (local >= 0) & (local < num_compressed)
|
||||
local = tl.where(valid, local, 0)
|
||||
block = tl.load(
|
||||
block_table_ptr + req * block_table_stride + local // BLOCK_SIZE,
|
||||
mask=valid,
|
||||
other=0,
|
||||
)
|
||||
slot = tl.where(valid, block * BLOCK_SIZE + local % BLOCK_SIZE, -1)
|
||||
tl.store(out_ptr + row * TOPK + cols, slot, mask=cols < TOPK)
|
||||
tl.store(lens_ptr + row, row_len)
|
||||
|
||||
|
||||
def build_prefill_topk_ragged_indices(
|
||||
topk_indices: torch.Tensor,
|
||||
token_to_req_indices: torch.Tensor,
|
||||
query_start_loc: torch.Tensor,
|
||||
seq_lens: torch.Tensor,
|
||||
is_valid_token: torch.Tensor,
|
||||
block_table: torch.Tensor,
|
||||
block_size: int,
|
||||
compress_ratio: int,
|
||||
num_compressed: int,
|
||||
token_offset: int,
|
||||
num_rows: int = -1,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""Map prefill top-k rows to a ragged stream of compressed-cache slots.
|
||||
|
||||
``topk_indices`` holds local compressed positions for the prefill tokens,
|
||||
which sit at ``token_offset`` in the batch; ``token_to_req_indices``,
|
||||
``query_start_loc``, ``seq_lens`` and ``block_table`` are batch-wide.
|
||||
``block_size`` is the compressed cache's, i.e. already divided by the ratio.
|
||||
"""
|
||||
topk_indices = topk_indices.reshape(topk_indices.shape[0], -1)
|
||||
num_tokens, width = topk_indices.shape
|
||||
dense = torch.empty(
|
||||
(num_tokens, width), dtype=torch.int32, device=topk_indices.device
|
||||
)
|
||||
lens = torch.empty(num_tokens, dtype=torch.int32, device=topk_indices.device)
|
||||
if num_tokens > 0 and width > 0:
|
||||
_prefill_topk_global_slots_kernel[(num_tokens,)](
|
||||
dense,
|
||||
lens,
|
||||
topk_indices,
|
||||
topk_indices.stride(0),
|
||||
token_to_req_indices,
|
||||
query_start_loc,
|
||||
seq_lens,
|
||||
is_valid_token,
|
||||
block_table,
|
||||
block_table.stride(0),
|
||||
token_offset,
|
||||
num_compressed,
|
||||
TOPK=width,
|
||||
COMPRESS_RATIO=compress_ratio,
|
||||
BLOCK_SIZE=block_size,
|
||||
BLOCK_W=min(triton.next_power_of_2(width), 1024),
|
||||
)
|
||||
else:
|
||||
lens.zero_()
|
||||
return build_ragged_indices_from_dense(dense, lens, num_rows=num_rows)
|
||||
|
||||
Reference in New Issue
Block a user