[Attention][DSA] Enable W4A16 DSA (#51724)
Signed-off-by: Shiyang Chen <shiychen@nvidia.com>
This commit is contained in:
@@ -3082,6 +3082,31 @@ def cp_gather_and_upconvert_fp8_kv_cache(
|
||||
)
|
||||
|
||||
|
||||
def cp_gather_and_upconvert_nvfp4_kv_cache(
|
||||
src_cache: torch.Tensor,
|
||||
dst: torch.Tensor,
|
||||
block_table: torch.Tensor,
|
||||
workspace_starts: torch.Tensor,
|
||||
batch_size: int,
|
||||
) -> None:
|
||||
"""Gather and upconvert an nvfp4_ds_mla KV cache to a BF16 workspace.
|
||||
|
||||
Args:
|
||||
src_cache: NVFP4 KV cache [num_blocks, block_size, 352] (uint8)
|
||||
dst: BF16 output workspace [total_tokens, 576]
|
||||
block_table: Block indices [num_reqs, max_blocks]
|
||||
workspace_starts: Workspace start offsets [num_reqs]
|
||||
batch_size: Number of requests
|
||||
"""
|
||||
torch.ops._C_cache_ops.cp_gather_and_upconvert_nvfp4_kv_cache(
|
||||
src_cache,
|
||||
dst,
|
||||
block_table,
|
||||
workspace_starts,
|
||||
batch_size,
|
||||
)
|
||||
|
||||
|
||||
def concat_mla_q(
|
||||
ql_nope: torch.Tensor,
|
||||
q_pe: torch.Tensor,
|
||||
|
||||
@@ -45,6 +45,7 @@ CacheDType = Literal[
|
||||
"fp8_e5m2",
|
||||
"fp8_inc",
|
||||
"fp8_ds_mla",
|
||||
"nvfp4_ds_mla",
|
||||
"turboquant_k8v4",
|
||||
"turboquant_4bit_nc",
|
||||
"turboquant_k3v4_nc",
|
||||
|
||||
+5
-1
@@ -2821,14 +2821,18 @@ class VllmConfig:
|
||||
def validate_nvfp4_kv_cache_with_mla(self) -> "VllmConfig":
|
||||
if self.model_config is None:
|
||||
return self
|
||||
# The ds_mla layouts are MLA-only by construction; the plain nvfp4
|
||||
# layout (head_size//2 + head_size//16) does not apply to MLA.
|
||||
if (
|
||||
self.cache_config.cache_dtype.startswith("nvfp4")
|
||||
and not self.cache_config.cache_dtype.endswith("_ds_mla")
|
||||
and self.model_config.use_mla
|
||||
):
|
||||
raise ValueError(
|
||||
"nvfp4 KV cache is not supported with MLA (Multi-head Latent "
|
||||
"Attention) backends. Please use a different --kv-cache-dtype "
|
||||
"(e.g., 'fp8' or 'auto') for MLA models such as DeepSeek."
|
||||
"(e.g., 'fp8', 'auto', or 'nvfp4_ds_mla' with a sparse MLA "
|
||||
"backend) for MLA models such as DeepSeek."
|
||||
)
|
||||
return self
|
||||
|
||||
|
||||
@@ -352,6 +352,10 @@ def _canonicalize_sparse_mla_kv_cache_dtype(
|
||||
) -> CacheDType:
|
||||
backend_name = attn_backend.get_name()
|
||||
if backend_name == "FLASHMLA_SPARSE" and is_quantized_kv_cache(kv_cache_dtype):
|
||||
# The NVFP4 DS-MLA format is used as-is; any other quantized dtype
|
||||
# (fp8, fp8_e4m3, ...) is served via the fp8_ds_mla format.
|
||||
if kv_cache_dtype == "nvfp4_ds_mla":
|
||||
return kv_cache_dtype
|
||||
return "fp8_ds_mla"
|
||||
if backend_name == "FLASHINFER_MLA_SPARSE_SM120" and kv_cache_dtype in (
|
||||
"auto",
|
||||
@@ -861,7 +865,11 @@ class MLAAttention(nn.Module, AttentionLayerBase):
|
||||
k_c_normed = k_c_normed[:num_actual_toks, ...]
|
||||
k_pe = k_pe[:num_actual_toks, ...]
|
||||
|
||||
if fp8_attention and self.kv_cache_dtype != "fp8_ds_mla":
|
||||
if fp8_attention and self.kv_cache_dtype not in (
|
||||
# Opaque per-token byte formats stay as raw uint8
|
||||
"fp8_ds_mla",
|
||||
"nvfp4_ds_mla",
|
||||
):
|
||||
kv_cache = kv_cache.view(current_platform.fp8_dtype())
|
||||
|
||||
assert (
|
||||
@@ -1262,9 +1270,12 @@ class MLAAttention(nn.Module, AttentionLayerBase):
|
||||
dtype=kv_cache_dtype,
|
||||
cache_dtype_str=self.kv_cache_dtype,
|
||||
kv_quant_mode=get_kv_quant_mode(self.kv_cache_dtype),
|
||||
# fp8_ds_mla: 656-byte custom layout (kv_lora_rank=512 +
|
||||
# qk_rope_head_dim=64, head_size=576). See flashmla_sparse.py.
|
||||
state_content_bytes=656 if self.kv_cache_dtype == "fp8_ds_mla" else None,
|
||||
# ds_mla layouts pack NoPE + RoPE + scales into one opaque per-token
|
||||
# blob, so the size is not derivable from head_size.
|
||||
# See flashmla_sparse.py.
|
||||
state_content_bytes={"fp8_ds_mla": 656, "nvfp4_ds_mla": 352}.get(
|
||||
self.kv_cache_dtype
|
||||
),
|
||||
)
|
||||
if self.sliding_window is not None:
|
||||
return SlidingWindowMLASpec(
|
||||
|
||||
@@ -45,6 +45,7 @@ STR_DTYPE_TO_TORCH_DTYPE = {
|
||||
"fp8_per_token_head": torch.uint8,
|
||||
"fp8_inc": torch.float8_e4m3fn,
|
||||
"fp8_ds_mla": torch.uint8,
|
||||
"nvfp4_ds_mla": torch.uint8,
|
||||
"turboquant_k8v4": torch.uint8,
|
||||
"turboquant_4bit_nc": torch.uint8,
|
||||
"turboquant_k3v4_nc": torch.uint8,
|
||||
|
||||
@@ -84,8 +84,28 @@ Bytes, structured as:
|
||||
- **Last 8 bytes:** Scale factors, containing 7 `ue8m0` values + 1B pad.
|
||||
The first `ue8m0` is the scale for the first 64 `float8_e4m3` values,
|
||||
the second for the next 64, and so on.
|
||||
|
||||
In the "nvfp4_ds_mla" format (SM100 only, DeepSeek V3.2 geometry), each
|
||||
token's KV cache is 352 Bytes, structured as:
|
||||
- **First 256 bytes:** 512 `e2m1` NoPE values packed 2/byte (low nibble =
|
||||
even element).
|
||||
- **Next 64 bytes:** 64 `float8_e4m3` RoPE values. These carry no scale
|
||||
factor: `e4m3`'s 4 exponent bits span the RoPE magnitude range unaided.
|
||||
- **Last 32 bytes:** 32 `float8_e4m3` NoPE scale factors, one per 16
|
||||
elements, stored permuted (an 8x4 -> 4x8 transpose: the scale for element
|
||||
block `s` lives at byte `8 * (s & 3) + (s >> 2)`) so that the 8 scales one
|
||||
FlashMLA dequant thread needs are contiguous. See the layout comment in
|
||||
`csrc/libtorch_stable/cache_kernels.cu`.
|
||||
|
||||
"""
|
||||
|
||||
# Quantized DS-MLA cache formats served by the FP8/NVFP4 sparse decode kernel
|
||||
# path (as opposed to the plain bf16 cache). FlashMLA infers which of these the
|
||||
# cache holds from its bytes-per-token, so nothing else needs to be passed down.
|
||||
QUANTIZED_DS_MLA_CACHE_FORMATS: frozenset[str] = frozenset(
|
||||
{"fp8_ds_mla", "nvfp4_ds_mla"}
|
||||
)
|
||||
|
||||
|
||||
class FlashMLASparseBackend(AttentionBackend):
|
||||
supported_dtypes: ClassVar[list[torch.dtype]] = [torch.bfloat16]
|
||||
@@ -94,6 +114,7 @@ class FlashMLASparseBackend(AttentionBackend):
|
||||
"bfloat16",
|
||||
"fp8_ds_mla",
|
||||
"fp8", # alias for fp8_ds_mla
|
||||
"nvfp4_ds_mla", # NVFP4 NoPE + FP8 RoPE (SM100 only)
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
@@ -129,6 +150,26 @@ class FlashMLASparseBackend(AttentionBackend):
|
||||
def supports_compute_capability(cls, capability: DeviceCapability) -> bool:
|
||||
return capability.major in [9, 10]
|
||||
|
||||
@classmethod
|
||||
def supports_combination(
|
||||
cls,
|
||||
head_size: int,
|
||||
dtype: torch.dtype,
|
||||
kv_cache_dtype: "CacheDType | None",
|
||||
block_size: int | None,
|
||||
use_mla: bool,
|
||||
has_sink: bool,
|
||||
use_sparse: bool,
|
||||
use_mm_prefix: bool,
|
||||
device_capability: DeviceCapability,
|
||||
) -> str | None:
|
||||
if kv_cache_dtype == "nvfp4_ds_mla" and device_capability.major != 10:
|
||||
return (
|
||||
f"FLASHMLA_SPARSE only supports the {kv_cache_dtype} kv-cache "
|
||||
"dtype on SM100 (Blackwell)"
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
@dataclass
|
||||
class FlashMLASparseMetadata(AttentionMetadata):
|
||||
@@ -258,7 +299,9 @@ class FlashMLASparseMetadataBuilder(
|
||||
FlashMLASparseImpl._compute_fp8_decode_padded_heads(self.num_heads)
|
||||
)
|
||||
|
||||
self.use_fp8_kv_cache = cache_config.cache_dtype == "fp8_ds_mla"
|
||||
self.use_fp8_kv_cache = (
|
||||
cache_config.cache_dtype in QUANTIZED_DS_MLA_CACHE_FORMATS
|
||||
)
|
||||
max_num_seqs = vllm_config.scheduler_config.max_num_seqs
|
||||
# Shape: [max_num_seqs], all elements = topk_tokens (constant for full-CG)
|
||||
self.topk_tokens_tensor = torch.full(
|
||||
@@ -596,9 +639,10 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
)
|
||||
q_concat_shape = (max_tokens, q_concat_heads, head_size)
|
||||
if is_quantized_kv_cache(kv_cache_dtype):
|
||||
assert kv_cache_dtype == "fp8_ds_mla", (
|
||||
"FlashMLA Sparse Attention backend fp8 only supports "
|
||||
"fp8_ds_mla kv-cache dtype"
|
||||
assert kv_cache_dtype in QUANTIZED_DS_MLA_CACHE_FORMATS, (
|
||||
"FlashMLA Sparse Attention backend only supports the "
|
||||
f"{sorted(QUANTIZED_DS_MLA_CACHE_FORMATS)} quantized kv-cache "
|
||||
f"dtypes, got {kv_cache_dtype}"
|
||||
)
|
||||
|
||||
if self.need_to_return_lse_for_decode and not is_quantized_kv_cache(
|
||||
@@ -609,7 +653,7 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
"the bf16 sparse path is not supported under DCP."
|
||||
)
|
||||
|
||||
if kv_cache_dtype == "fp8_ds_mla":
|
||||
if kv_cache_dtype in QUANTIZED_DS_MLA_CACHE_FORMATS:
|
||||
# Reserve workspace during initialization
|
||||
assert vllm_config is not None and vllm_config.model_config is not None
|
||||
prefill_workspace_size = get_prefill_workspace_size(
|
||||
@@ -749,13 +793,22 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
assert fp8_metadata.prefill is not None
|
||||
for chunk in fp8_metadata.prefill.chunks:
|
||||
chunk_workspace = self.prefill_bf16_workspace[: chunk.chunk_tot_seqlen]
|
||||
ops.cp_gather_and_upconvert_fp8_kv_cache(
|
||||
kv_c_and_k_pe_cache,
|
||||
chunk_workspace,
|
||||
chunk.block_table,
|
||||
chunk.workspace_starts,
|
||||
len(chunk.block_table),
|
||||
)
|
||||
if self.kv_cache_dtype == "fp8_ds_mla":
|
||||
ops.cp_gather_and_upconvert_fp8_kv_cache(
|
||||
kv_c_and_k_pe_cache,
|
||||
chunk_workspace,
|
||||
chunk.block_table,
|
||||
chunk.workspace_starts,
|
||||
len(chunk.block_table),
|
||||
)
|
||||
else:
|
||||
ops.cp_gather_and_upconvert_nvfp4_kv_cache(
|
||||
kv_c_and_k_pe_cache.view(torch.uint8),
|
||||
chunk_workspace,
|
||||
chunk.block_table,
|
||||
chunk.workspace_starts,
|
||||
len(chunk.block_table),
|
||||
)
|
||||
|
||||
chunk_q = q[chunk.tokens_slice]
|
||||
chunk_topk_indices_workspace = topk_indices[chunk.tokens_slice]
|
||||
@@ -953,7 +1006,7 @@ class FlashMLASparseImpl(SparseMLACommonImpl[FlashMLASparseMetadata]):
|
||||
assert self.topk_indices_buffer is not None
|
||||
topk_indices = self.topk_indices_buffer[:num_actual_toks]
|
||||
|
||||
use_fp8_cache = self.kv_cache_dtype == "fp8_ds_mla"
|
||||
use_fp8_cache = self.kv_cache_dtype in QUANTIZED_DS_MLA_CACHE_FORMATS
|
||||
|
||||
lse: torch.Tensor | None = None
|
||||
|
||||
|
||||
@@ -54,6 +54,7 @@ class KVQuantMode(IntEnum):
|
||||
TURBOQUANT_4BIT_NC = 7
|
||||
TURBOQUANT_K3V4_NC = 8
|
||||
TURBOQUANT_3BIT_NC = 9
|
||||
NVFP4_DS_MLA = 10 # opaque-bytes NVFP4 DS-MLA layouts (FlashMLA sparse)
|
||||
|
||||
@property
|
||||
def is_per_token_head(self) -> bool:
|
||||
@@ -88,6 +89,11 @@ def get_kv_quant_mode(kv_cache_dtype: str) -> KVQuantMode:
|
||||
return KVQuantMode.INT8_PER_TOKEN_HEAD
|
||||
if kv_cache_dtype == "fp8_per_token_head":
|
||||
return KVQuantMode.FP8_PER_TOKEN_HEAD
|
||||
# Must precede the ``nvfp4`` prefix test below, which would otherwise match.
|
||||
if kv_cache_dtype == "nvfp4_ds_mla":
|
||||
# Page size is keyed on cache_dtype_str in the MLA specs, not
|
||||
# nvfp4_kv_cache_full_dim.
|
||||
return KVQuantMode.NVFP4_DS_MLA
|
||||
if kv_cache_dtype.startswith("nvfp4"):
|
||||
return KVQuantMode.NVFP4
|
||||
if isinstance(kv_cache_dtype, str) and kv_cache_dtype.startswith("turboquant_"):
|
||||
|
||||
Reference in New Issue
Block a user