|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +from typing import Any, Optional |
| 4 | + |
| 5 | +import torch |
| 6 | + |
| 7 | +import vllm._custom_ops as ops |
| 8 | +from vllm.attention.backends.abstract import (AttentionType, |
| 9 | + is_quantized_kv_cache) |
| 10 | +from vllm.logger import init_logger |
| 11 | +from vllm.v1.attention.backends.mla.common import (MLACommonBackend, |
| 12 | + MLACommonImpl, |
| 13 | + MLACommonMetadata) |
| 14 | + |
| 15 | +logger = init_logger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class CutlassMLABackend(MLACommonBackend): |
| 19 | + |
| 20 | + @staticmethod |
| 21 | + def get_name() -> str: |
| 22 | + return "CUTLASS_MLA_VLLM_V1" |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def get_impl_cls() -> type["CutlassMLAImpl"]: |
| 26 | + return CutlassMLAImpl |
| 27 | + |
| 28 | + |
| 29 | +class CutlassMLAImpl(MLACommonImpl[MLACommonMetadata]): |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + num_heads: int, |
| 34 | + head_size: int, |
| 35 | + scale: float, |
| 36 | + num_kv_heads: int, |
| 37 | + alibi_slopes: Optional[list[float]], |
| 38 | + sliding_window: Optional[int], |
| 39 | + kv_cache_dtype: str, |
| 40 | + blocksparse_params: Optional[dict[str, Any]], |
| 41 | + logits_soft_cap: Optional[float], |
| 42 | + attn_type: str, |
| 43 | + # MLA Specific Arguments |
| 44 | + **mla_args) -> None: |
| 45 | + super().__init__(num_heads, head_size, scale, num_kv_heads, |
| 46 | + alibi_slopes, sliding_window, kv_cache_dtype, |
| 47 | + blocksparse_params, logits_soft_cap, attn_type, |
| 48 | + **mla_args) |
| 49 | + |
| 50 | + unsupported_features = [ |
| 51 | + alibi_slopes, sliding_window, blocksparse_params, logits_soft_cap |
| 52 | + ] |
| 53 | + if any(unsupported_features): |
| 54 | + raise NotImplementedError( |
| 55 | + "CutlassMLAImpl does not support one of the following: " |
| 56 | + "alibi_slopes, sliding_window, blocksparse_params, " |
| 57 | + "logits_soft_cap") |
| 58 | + |
| 59 | + if attn_type != AttentionType.DECODER: |
| 60 | + raise NotImplementedError("Encoder self-attention and " |
| 61 | + "encoder/decoder cross-attention " |
| 62 | + "are not implemented for " |
| 63 | + "CutlassMLAImpl") |
| 64 | + |
| 65 | + if is_quantized_kv_cache(self.kv_cache_dtype): |
| 66 | + raise NotImplementedError( |
| 67 | + "CutlassMLA V1 with FP8 KV cache not yet supported") |
| 68 | + |
| 69 | + def _forward_decode( |
| 70 | + self, |
| 71 | + q_nope: torch.Tensor, |
| 72 | + q_pe: torch.Tensor, |
| 73 | + kv_c_and_k_pe_cache: torch.Tensor, |
| 74 | + attn_metadata: MLACommonMetadata, |
| 75 | + ) -> torch.Tensor: |
| 76 | + assert kv_c_and_k_pe_cache.numel() > 0 |
| 77 | + assert attn_metadata.decode is not None |
| 78 | + |
| 79 | + if self.kv_cache_dtype.startswith("fp8"): |
| 80 | + raise NotImplementedError("FP8 Cutlass MLA not yet supported") |
| 81 | + |
| 82 | + B = q_nope.shape[0] |
| 83 | + |
| 84 | + o = torch.empty((B, self.num_heads, self.kv_lora_rank), |
| 85 | + dtype=q_nope.dtype, |
| 86 | + device=q_nope.device) |
| 87 | + |
| 88 | + # Run MLA |
| 89 | + # Clone q_nope and q_pe to make sure strides computation is correct. |
| 90 | + q_nope = q_nope.clone() |
| 91 | + q_pe = q_pe.clone() |
| 92 | + ops.cutlass_mla_decode(o, q_nope, q_pe, kv_c_and_k_pe_cache, |
| 93 | + attn_metadata.decode.seq_lens, |
| 94 | + attn_metadata.decode.block_table, self.scale) |
| 95 | + |
| 96 | + return self._v_up_proj(o) |
0 commit comments