# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from typing import ClassVar import torch import vllm.envs as envs from vllm.config import VllmConfig from vllm.config.cache import CacheDType from vllm.logger import init_logger from vllm.model_executor.layers.attention.mla_attention import ( MLACommonBackend, MLACommonImpl, MLACommonMetadata, MLACommonMetadataBuilder, ) from vllm.platforms import current_platform from vllm.platforms.interface import DeviceCapability from vllm.triton_utils import triton from vllm.utils.torch_utils import is_quantized_kv_cache from vllm.v1.attention.backend import ( AttentionCGSupport, AttentionLayer, AttentionType, MultipleOf, ) from vllm.v1.attention.ops.triton_decode_attention import decode_attention_fwd from vllm.v1.kv_cache_interface import KVCacheSpec from vllm.v1.worker.workspace import ( current_workspace_manager, is_workspace_manager_initialized, ) logger = init_logger(__name__) # num_kv_splits selection (shared by forward_mqa and the workspace reservation # so the two cannot drift). Both are hardware dependent. _MIN_WORK_PER_SPLIT = 512 _SPLIT_OCCUPANCY_MULTIPLIER = 2 def _compute_num_kv_splits(max_seq_len: int, sm_count: int) -> int: # Power of 2 to avoid excessive kernel instantiations, capped by an SM-based # maximum (occupancy multiplier allows multiple blocks per SM # for latency hiding). ideal_splits = triton.next_power_of_2(max(1, max_seq_len // _MIN_WORK_PER_SPLIT)) max_splits = sm_count * _SPLIT_OCCUPANCY_MULTIPLIER return min(ideal_splits, max_splits) class TritonMLAMetadataBuilder(MLACommonMetadataBuilder[MLACommonMetadata]): _cudagraph_support: ClassVar[AttentionCGSupport] = ( AttentionCGSupport.UNIFORM_SINGLE_TOKEN_DECODE ) # Non-causal DSpark block is flattened to one decode row per query token in # forward_mqa, so no intra-block causal masking is required. supports_non_causal_multi_token_decode: ClassVar[bool] = True @classmethod def get_cudagraph_support( cls, vllm_config: VllmConfig, kv_cache_spec: KVCacheSpec, ) -> AttentionCGSupport: """Report UNIFORM_BATCH where a non-causal multi-token block is served. ``_cudagraph_support`` is a class constant, so serving the DSpark draft's (1 + num_spec) block through the decode path reports UNIFORM_SINGLE_TOKEN_DECODE and, because the engine takes the minimum over all attention groups, downgrades the *whole* engine off full cudagraphs. ``forward_mqa`` flattens that block with ``repeat_interleave`` on a Python int and performs no device->host sync, so it does satisfy the UNIFORM_BATCH contract. ``non_causal_multi_token_decode`` is a KV-cache-group property, not a per-layer one: ``MLAAttentionSpec.merge`` ORs it over every layer in the group, so a group holding both a draft and its target reports it for both. That is the same predicate ``__init__`` below already uses to raise ``reorder_batch_threshold``, so the two stay consistent, but it does mean this lifts a causal target sharing the draft's KV cache group as well. """ if getattr(kv_cache_spec, "non_causal_multi_token_decode", False): return AttentionCGSupport.UNIFORM_BATCH return cls._cudagraph_support def __init__(self, kv_cache_spec, layer_names, vllm_config, device): super().__init__(kv_cache_spec, layer_names, vllm_config, device) # DCP local sequence lengths are not advanced between draft steps. self.supports_draft_decode_metadata_update = self.dcp_world_size == 1 # Only the non-causal DSpark draft group serves multi-token blocks via # the decode path; raise its reorder threshold to the spec block length # so full-cudagraph capture admits it. Causal usage stays single-token. if getattr(self, "non_causal_multi_token_decode", False): self._init_reorder_batch_threshold(1, supports_spec_as_decode=True) self._reserve_attn_logits_workspace() def update_draft_decode_metadata(self, _metadata: MLACommonMetadata) -> None: pass def _reserve_attn_logits_workspace(self) -> None: """Pre-size the shared workspace for the decode split-KV attn logits. Reserving at the worst case (max_model_len -> max num_kv_splits, max_num_seqs decode tokens) before warmup/cudagraph capture means the per-call ``get_simultaneous`` in ``forward_mqa`` never has to grow the buffer at runtime (which would raise once the workspace is locked). """ if not is_workspace_manager_initialized(): return # Decode reorder threshold is 1, so decode tokens <= max_num_seqs. B = self.vllm_config.scheduler_config.max_num_seqs # Non-causal DSpark draft flattens each request's block to query_len # decode rows; cover max_num_seqs * block_len rows. if getattr(self, "non_causal_multi_token_decode", False): B *= self.reorder_batch_threshold # DCP all-gathers the query heads before forward_mqa. q_num_heads = self.num_heads * self.dcp_world_size max_splits = _compute_num_kv_splits( self.model_config.max_model_len, current_platform.num_compute_units(), ) lse_dim = self.mla_dims.kv_lora_rank + 1 current_workspace_manager().get_simultaneous( ((B, q_num_heads, max_splits, lse_dim), torch.float32), ) class TritonMLABackend(MLACommonBackend): supported_dtypes: ClassVar[list[torch.dtype]] = [torch.float16, torch.bfloat16] supported_kv_cache_dtypes: ClassVar[list[CacheDType]] = [ "auto", "float16", "bfloat16", "fp8", "fp8_e4m3", ] @classmethod def get_supported_head_sizes(cls) -> list[int]: return [] @staticmethod def get_supported_kernel_block_sizes() -> list[int | MultipleOf]: return [MultipleOf(16)] @classmethod def supports_block_size(cls, block_size: int | None) -> bool: if block_size is None: return True return block_size % 16 == 0 @staticmethod def get_name() -> str: return "TRITON_MLA" @classmethod def supports_batch_invariance(cls) -> bool: return True @staticmethod def get_impl_cls() -> type["TritonMLAImpl"]: return TritonMLAImpl @staticmethod def get_builder_cls() -> type["TritonMLAMetadataBuilder"]: return TritonMLAMetadataBuilder @classmethod def supports_compute_capability(cls, capability: DeviceCapability) -> bool: return True @classmethod def supports_non_causal(cls) -> bool: # DSpark non-causal blocks are flattened to single-token decode rows in # TritonMLAImpl.forward_mqa (decode_attention_fwd has no causal flag / # no intra-block masking). Enables the non-causal AMD MLA path. return True class TritonMLAImpl(MLACommonImpl[MLACommonMetadata]): can_return_lse_for_decode: bool = True 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 **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, **mla_args, ) unsupported_features = [alibi_slopes, sliding_window, logits_soft_cap] if any(unsupported_features): raise NotImplementedError( "TritonMLAImpl does not support one of the following: " "alibi_slopes, sliding_window, logits_soft_cap" ) if attn_type != AttentionType.DECODER: raise NotImplementedError( "Encoder self-attention and " "encoder/decoder cross-attention " "are not implemented for " "TritonMLAImpl" ) if current_platform.is_cuda(): cap = current_platform.get_device_capability() cap_str = cap.as_version_str() if cap is not None else "unknown" dev = current_platform.get_device_name() if self.kv_cache_dtype.startswith("fp8") and not ( current_platform.has_device_capability(89) ): suggested = ( "float16" if (cap is None or cap.to_int() < 80) else "bfloat16" ) raise ValueError( f"FP8 KV cache is not supported by the Triton MLA backend " f"on {dev} (compute capability {cap_str}); native FP8 " f"(fp8e4nv) requires SM89+. Re-run with " f"--kv-cache-dtype {suggested}." ) if self.kv_cache_dtype == "bfloat16" and not ( current_platform.has_device_capability(80) ): raise ValueError( f"bfloat16 KV cache is not supported by the Triton MLA " f"backend on {dev} (compute capability {cap_str}); " f"bfloat16 requires SM80+. Re-run with " f"--kv-cache-dtype float16." ) # For FP8 KV cache, we dequantize to BF16 on load inside the # Triton kernel. Tell the common layer not to quantize queries # to FP8 — we handle FP8 KV cache with BF16 queries (Mode 1). if is_quantized_kv_cache(self.kv_cache_dtype): self.supports_quant_query_input = False self._sm_count = current_platform.num_compute_units() def forward_mqa( self, q: torch.Tensor | tuple[torch.Tensor, torch.Tensor], kv_c_and_k_pe_cache: torch.Tensor, attn_metadata: MLACommonMetadata, layer: AttentionLayer, ) -> tuple[torch.Tensor, torch.Tensor | None]: assert kv_c_and_k_pe_cache.numel() > 0 assert attn_metadata.decode is not None if type(q) is tuple: q = torch.cat(q, dim=-1) assert isinstance(q, torch.Tensor) B = q.shape[0] q_num_heads = q.shape[1] o = torch.zeros( B, q_num_heads, self.kv_lora_rank, dtype=q.dtype, device=q.device ) lse = torch.zeros(B, q_num_heads, dtype=q.dtype, device=q.device) # For batch invariance, use only 1 split to ensure deterministic reduction if envs.VLLM_BATCH_INVARIANT: num_kv_splits = 1 else: num_kv_splits = _compute_num_kv_splits( attn_metadata.max_seq_len, self._sm_count ) # NOTE: the +1 stores the LogSumExp (LSE) that the stage2 kernel uses to # merge partial attention outputs across splits. The scratch is served # from the shared workspace (reserved at max in the metadata builder), so # there is no per-call allocation on the decode hot path. Fall back to a # direct allocation when the workspace manager is not initialized (e.g. # unit tests without a GPUModelRunner). logits_shape = (B, q_num_heads, num_kv_splits, self.kv_lora_rank + 1) if is_workspace_manager_initialized(): (attn_logits,) = current_workspace_manager().get_simultaneous( (logits_shape, torch.float32), ) else: attn_logits = torch.empty( logits_shape, dtype=torch.float32, device=q.device ) # Add a head dim of 1 kv_c_and_k_pe_cache = kv_c_and_k_pe_cache.unsqueeze(2) kv_c_cache = kv_c_and_k_pe_cache[..., : self.kv_lora_rank] PAGE_SIZE = kv_c_and_k_pe_cache.size(1) block_table = attn_metadata.decode.block_table seq_lens = attn_metadata.decode.seq_lens if not attn_metadata.causal: # Non-causal DSpark block: flatten to one decode row per query token. # Each row attends to the same committed KV prefix (per-row seq_lens) # and never to sibling block tokens = non-causal block semantics. # Mirrors FlashInferMLA's non-causal path. query_len = attn_metadata.num_decode_tokens // attn_metadata.num_decodes if query_len > 1: block_table = block_table.repeat_interleave(query_len, dim=0) seq_lens = seq_lens.repeat_interleave(query_len) # Run MQA — always pass layer scales. When KV cache is # BF16 the kernel's `if dtype.is_fp8()` check is a no-op. decode_attention_fwd( q, kv_c_and_k_pe_cache, kv_c_cache, o, lse, block_table, seq_lens, attn_logits, num_kv_splits, self.scale, PAGE_SIZE, k_scale=layer._k_scale, v_scale=layer._k_scale, is_mla=True, ) return o, lse