| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | # adapted from: https://github.com/deepseek-ai/FlashMLA/blob/main/flash_mla/flash_mla_interface.py |
| 4 | |
| 5 | import torch |
| 6 | |
| 7 | from vllm.logger import init_logger |
| 8 | from vllm.platforms import current_platform |
| 9 | |
| 10 | logger = init_logger(__name__) |
| 11 | |
| 12 | if current_platform.is_cuda(): |
| 13 | try: |
| 14 | import vllm._flashmla_C # noqa: F401 |
| 15 | |
| 16 | _flashmla_C_AVAILABLE = True |
| 17 | except ImportError: |
| 18 | _flashmla_C_AVAILABLE = False |
| 19 | else: |
| 20 | _flashmla_C_AVAILABLE = False |
| 21 | |
| 22 | if current_platform.is_cuda(): |
| 23 | try: |
| 24 | import vllm._flashmla_extension_C # noqa: F401 |
| 25 | |
| 26 | _flashmla_extension_C_AVAILABLE = True |
| 27 | except ImportError: |
| 28 | _flashmla_extension_C_AVAILABLE = False |
| 29 | else: |
| 30 | _flashmla_extension_C_AVAILABLE = False |
| 31 | |
| 32 | |
| 33 | def _is_flashmla_available() -> tuple[bool, str | None]: |
| 34 | if not _flashmla_C_AVAILABLE: |
| 35 | return ( |
| 36 | False, |
| 37 | "vllm._flashmla_C is not available, likely was not " |
| 38 | "compiled due to insufficient nvcc version or a supported arch " |
| 39 | "was not in the list of target arches to compile for.", |
| 40 | ) |
| 41 | if not _flashmla_extension_C_AVAILABLE: |
| 42 | return ( |
| 43 | False, |
| 44 | "vllm._flashmla_extension_C is not available, likely " |
| 45 | "was not compiled due to a build error.", |
| 46 | ) |
| 47 | |
| 48 | return True, None |
| 49 | |
| 50 | |
| 51 | def is_flashmla_dense_supported() -> tuple[bool, str | None]: |
| 52 | """Return: is_supported_flag, unsupported_reason (optional).""" |
| 53 | is_available, maybe_reason = _is_flashmla_available() |
| 54 | if not is_available: |
| 55 | return False, maybe_reason |
| 56 | if not current_platform.is_device_capability_family(90): |
| 57 | return False, "FlashMLA Dense is only supported on Hopper devices." |
| 58 | return True, None |
| 59 | |
| 60 | |
| 61 | def is_flashmla_sparse_supported() -> tuple[bool, str | None]: |
| 62 | """Return: is_supported_flag, unsupported_reason (optional).""" |
| 63 | is_available, maybe_reason = _is_flashmla_available() |
| 64 | if not is_available: |
| 65 | return False, maybe_reason |
| 66 | if not ( |
| 67 | current_platform.is_device_capability_family(90) |
| 68 | or current_platform.is_device_capability_family(100) |
| 69 | ): |
| 70 | return ( |
| 71 | False, |
| 72 | "FlashMLA Sparse is only supported on Hopper and Blackwell DC devices.", |
| 73 | ) |
| 74 | return True, None |
| 75 | |
| 76 | |
| 77 | def _raise_flashmla_unavailable(*_args, **_kwargs): |
| 78 | _, reason = _is_flashmla_available() |
| 79 | raise RuntimeError(reason or "FlashMLA is not available") |
| 80 | |
| 81 | |
| 82 | if _is_flashmla_available()[0]: |
| 83 | from vllm.third_party.flashmla.flash_mla_interface import ( # noqa: F401 |
| 84 | FlashMLASchedMeta, |
| 85 | flash_attn_varlen_func, |
| 86 | flash_attn_varlen_kvpacked_func, |
| 87 | flash_attn_varlen_qkvpacked_func, |
| 88 | flash_mla_sparse_fwd, |
| 89 | flash_mla_with_kvcache, |
| 90 | get_mla_metadata, |
| 91 | ) |
| 92 | else: |
| 93 | |
| 94 | class FlashMLASchedMeta: # type: ignore[no-redef] |
| 95 | pass |
| 96 | |
| 97 | flash_attn_varlen_func = _raise_flashmla_unavailable # type: ignore[assignment] |
| 98 | flash_attn_varlen_kvpacked_func = _raise_flashmla_unavailable # type: ignore[assignment] |
| 99 | flash_attn_varlen_qkvpacked_func = _raise_flashmla_unavailable # type: ignore[assignment] |
| 100 | flash_mla_sparse_fwd = _raise_flashmla_unavailable # type: ignore[assignment] |
| 101 | flash_mla_with_kvcache = _raise_flashmla_unavailable # type: ignore[assignment] |
| 102 | get_mla_metadata = _raise_flashmla_unavailable # type: ignore[assignment] |
| 103 | |
| 104 | |
| 105 | def get_mla_metadata_dense_fp8( |
| 106 | cache_seqlens: torch.Tensor, |
| 107 | num_q_tokens_per_head_k: int, |
| 108 | num_heads_k: int, |
| 109 | ) -> tuple[torch.Tensor, torch.Tensor]: |
| 110 | if not _is_flashmla_available()[0]: |
| 111 | _raise_flashmla_unavailable() |
| 112 | return torch.ops._flashmla_extension_C.get_mla_decoding_metadata_dense_fp8( |
| 113 | cache_seqlens, |
| 114 | num_q_tokens_per_head_k, |
| 115 | num_heads_k, |
| 116 | ) |
| 117 | |
| 118 | |
| 119 | def flash_mla_with_kvcache_fp8( |
| 120 | q: torch.Tensor, |
| 121 | k_cache: torch.Tensor, |
| 122 | block_table: torch.Tensor, |
| 123 | cache_seqlens: torch.Tensor, |
| 124 | head_dim_v: int, |
| 125 | tile_scheduler_metadata: torch.Tensor, |
| 126 | num_splits: torch.Tensor, |
| 127 | softmax_scale: float | None = None, |
| 128 | causal: bool = False, |
| 129 | descale_q: torch.Tensor | None = None, |
| 130 | descale_k: torch.Tensor | None = None, |
| 131 | ) -> tuple[torch.Tensor, torch.Tensor]: |
| 132 | if not _is_flashmla_available()[0]: |
| 133 | _raise_flashmla_unavailable() |
| 134 | if softmax_scale is None: |
| 135 | softmax_scale = q.shape[-1] ** (-0.5) |
| 136 | out, softmax_lse = torch.ops._flashmla_extension_C.fwd_kvcache_mla_fp8( |
| 137 | q, |
| 138 | k_cache, |
| 139 | head_dim_v, |
| 140 | cache_seqlens, |
| 141 | block_table, |
| 142 | softmax_scale, |
| 143 | causal, |
| 144 | tile_scheduler_metadata, |
| 145 | num_splits, |
| 146 | descale_q, |
| 147 | descale_k, |
| 148 | ) |
| 149 | return out, softmax_lse |