[Feature][PCP] Add DeepSeek-V4.1-Flash support (#59857)

Signed-off-by: wangyicong <wangyicong@bytedance.com>
Co-authored-by: OpenAI Codex <noreply@openai.com>
Co-authored-by: Lucas Wilkinson <lwilkins@redhat.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Kevin H. Luu <khluu000@gmail.com>
This commit is contained in:
Yicong Wang
2026-10-15 23:17:51 -07:00
committed by GitHub
co-authored by OpenAI Codex Lucas Wilkinson Claude Opus 5.5 Kevin H. Luu
parent 074c83a09a
commit fa75d3456d
15 changed files with 873 additions and 37 deletions
+4
View File
@@ -140,6 +140,10 @@ class DeepseekV4MoE(DeepseekV4MoEBase):
def _select_dsv4_attn_cls(vllm_config: VllmConfig) -> type[DeepseekV4Attention]:
"""Pick the ROCm sparse-MLA implementation for DeepSeek V4.1."""
if vllm_config.parallel_config.prefill_context_parallel_size > 1:
raise NotImplementedError(
"DeepSeek-V4.1 PCP currently supports NVIDIA CUDA only."
)
backend = vllm_config.attention_config.backend
if backend is not None and backend not in (
AttentionBackendEnum.ROCM_FLASHMLA_SPARSE_DSV4,
+53 -8
View File
@@ -53,7 +53,7 @@ from vllm.config import (
get_current_vllm_config,
)
from vllm.config.cache import CacheDType
from vllm.distributed import get_tensor_model_parallel_world_size
from vllm.distributed import get_pcp_group, get_tensor_model_parallel_world_size
from vllm.forward_context import get_forward_context
from vllm.logger import init_logger
from vllm.model_executor.layers.attention_layer_base import AttentionLayerBase
@@ -297,6 +297,7 @@ class DeepseekV4Attention(nn.Module, AttentionLayerBase, ABC):
self.layer_id = layer_id
self.prefix = prefix # Alias for compatibility with compressor
self.use_pcp = vllm_config.parallel_config.prefill_context_parallel_size > 1
self.hidden_size = config.hidden_size
self.n_heads = config.num_attention_heads
assert self.n_heads % tp_size == 0
@@ -878,7 +879,10 @@ class DeepseekV4Attention(nn.Module, AttentionLayerBase, ABC):
index_q_scale: torch.Tensor | None = None
index_weights_out: torch.Tensor | None = None
latent: torch.Tensor | None = None
aux_stream = aux_streams[0] if aux_streams is not None else None
# Keep PCP collectives on one stream in the same order on every rank.
aux_stream = (
aux_streams[0] if aux_streams is not None and not self.use_pcp else None
)
if compressor is not None:
# Q projection / KV insertion on the default stream overlaps the
@@ -893,6 +897,17 @@ class DeepseekV4Attention(nn.Module, AttentionLayerBase, ABC):
else:
q = project_query_and_cache_kv()
cache_latent = latent
cache_positions = positions
if self.use_pcp and latent is not None:
assert isinstance(attn_metadata, dict)
swa_metadata = cast(
"DeepseekSparseSWAMetadata", attn_metadata[self.swa_cache_layer.prefix]
)
assert swa_metadata.cache_positions is not None
cache_latent = get_pcp_group().all_gather(latent, dim=0)
cache_positions = swa_metadata.cache_positions
def prepare_indexer():
if indexer is None:
return None, None, None
@@ -903,12 +918,16 @@ class DeepseekV4Attention(nn.Module, AttentionLayerBase, ABC):
positions,
self.indexer_rotary_emb,
qr_scale,
cache_latent=cache_latent,
cache_positions=cache_positions,
)
if compressor is not None:
indexer_result, _ = maybe_execute_in_parallel(
prepare_indexer,
lambda: compressor.insert_cache(latent, positions, self.rotary_emb),
lambda: compressor.insert_cache(
cache_latent, cache_positions, self.rotary_emb
),
self.ln_events[0],
self.ln_events[1],
aux_stream,
@@ -1071,6 +1090,25 @@ class DeepseekV4Attention(nn.Module, AttentionLayerBase, ABC):
assert positions.dtype == torch.int64
cos_sin_cache = self.rotary_emb.cos_sin_cache
cache_dtype = swa_kv_cache.dtype
slot_mapping = swa_metadata.slot_mapping
if self.use_pcp:
cache_kv = get_pcp_group().all_gather(kv.contiguous(), dim=0)
assert swa_metadata.cache_slot_mapping is not None
assert swa_metadata.cache_positions is not None
torch.ops._C.fused_deepseek_v4_kv_rope_insert(
cache_kv,
swa_kv_cache,
swa_metadata.cache_slot_mapping,
swa_metadata.cache_positions,
cos_sin_cache,
swa_metadata.block_size,
self._flashinfer_fp8_kv_scale
if cache_dtype == torch.float8_e4m3fn
else None,
self.kv_mxfp8,
)
# The fused op now only prepares the rank-local queries.
slot_mapping = slot_mapping[:0]
# kv is unchanged; attention reads kv solely via swa_kv_cache.
if cache_dtype == torch.uint8:
@@ -1092,7 +1130,7 @@ class DeepseekV4Attention(nn.Module, AttentionLayerBase, ABC):
q,
kv,
swa_kv_cache_2d,
swa_metadata.slot_mapping,
slot_mapping,
positions,
cos_sin_cache,
pad_to,
@@ -1120,7 +1158,7 @@ class DeepseekV4Attention(nn.Module, AttentionLayerBase, ABC):
q,
kv,
swa_kv_cache_3d,
swa_metadata.slot_mapping,
slot_mapping,
positions,
cos_sin_cache,
self.eps,
@@ -1136,7 +1174,7 @@ class DeepseekV4Attention(nn.Module, AttentionLayerBase, ABC):
kv,
q_fp8,
swa_kv_cache_3d,
swa_metadata.slot_mapping,
slot_mapping,
positions,
cos_sin_cache,
self._flashinfer_fp8_kv_scale,
@@ -1497,7 +1535,14 @@ class DeepseekV4Indexer(nn.Module):
positions: torch.Tensor,
rotary_emb: nn.Module,
qr_scale: torch.Tensor | None = None,
*,
cache_latent: torch.Tensor | None = None,
cache_positions: torch.Tensor | None = None,
) -> tuple[torch.Tensor | None, torch.Tensor | None, torch.Tensor | None]:
if cache_latent is None:
cache_latent = latent
if cache_positions is None:
cache_positions = positions
attn_metadata = get_forward_context().attn_metadata
if isinstance(attn_metadata, dict):
indexer_metadata = cast(Any, attn_metadata[self.k_cache.prefix])
@@ -1508,7 +1553,7 @@ class DeepseekV4Indexer(nn.Module):
# candidates num smaller than topk, every candidate is selected
# but we still need to build k cache
if self.owns_k:
self._produce_k(latent, positions, rotary_emb)
self._produce_k(cache_latent, cache_positions, rotary_emb)
assert self.topk_indices_buffer is not None
num_tokens = (
indexer_metadata.num_decode_tokens
@@ -1528,7 +1573,7 @@ class DeepseekV4Indexer(nn.Module):
if self.owns_k:
# K write must land before indexer_op reads the cache
# (skip_k_cache_insert=True).
self._produce_k(latent, positions, rotary_emb)
self._produce_k(cache_latent, cache_positions, rotary_emb)
return self.forward_q(qr, qr_scale, indexer_weights, positions, rotary_emb)
@@ -30,6 +30,8 @@ def fused_save_compress_norm(
rms_norm_eps: float,
compress_ratio: int,
latent_out: torch.Tensor,
prev_rows: torch.Tensor | None = None,
prev_row_indices: torch.Tensor | None = None,
) -> None:
"""Pool each closed group into a normalized BF16 latent; save FP32 states.
@@ -58,6 +60,11 @@ def fused_save_compress_norm(
rms_norm_eps: RMSNorm epsilon.
compress_ratio: Group size, either 1 or 2.
latent_out: BF16 [tokens, 512], written only at valid group boundaries.
prev_rows: FP32 [rows, 1024] rows gathered from all PCP ranks (CR2). A
chunk reads its predecessor here where ``prev_row_indices`` is set,
and the ring is left to ``save_ring_rows``.
prev_row_indices: [num_reqs] row in ``prev_rows`` holding the token
before each chunk's first token, or -1 to read the ring.
"""
assert compress_ratio in (1, 2)
@@ -82,6 +89,11 @@ def fused_save_compress_norm(
state_cache.shape[1],
)
num_reqs = query_start_loc.numel() - 1
if prev_rows is not None:
assert prev_row_indices is not None
assert prev_row_indices.numel() >= num_reqs
assert prev_rows.dtype == torch.float32 and prev_rows.shape[1] == 1024
assert prev_rows.stride(1) == 1 and prev_rows.stride(0) % 16 == 0
else:
state_cache = query_start_loc = token_to_req_indices = None
state_stride = state_row_stride = state_block = 1
@@ -90,6 +102,8 @@ def fused_save_compress_norm(
assert num_tokens <= min(kv_score.shape[0], positions.numel())
if num_tokens == 0:
return
has_prev_rows = prev_rows is not None
prev_row_stride = prev_rows.stride(0) if prev_rows is not None else 1
grid = num_reqs + triton.cdiv(num_tokens, compress_ratio)
_fused_save_compress_norm_kernel[(grid,)](
kv_score,
@@ -100,15 +114,19 @@ def fused_save_compress_norm(
token_to_req_indices,
rms_norm_weight,
latent_out,
prev_rows if has_prev_rows else kv_score,
prev_row_indices if has_prev_rows else slot_mapping,
num_tokens,
num_reqs,
RAW_STRIDE=kv_score.stride(0),
PREV_ROW_STRIDE=prev_row_stride,
STATE_STRIDE=state_stride,
STATE_ROW_STRIDE=state_row_stride,
STATE_BLOCK=state_block,
COMPRESS_RATIO=compress_ratio,
EPS=rms_norm_eps,
JOIN_ROW_PTRS=_JOIN_ROW_PTRS,
HAS_PREV_ROWS=has_prev_rows,
num_warps=4,
**({"launch_pdl": False} if current_platform.is_cuda() else {}),
)
@@ -124,15 +142,19 @@ def _fused_save_compress_norm_kernel(
req_ids,
norm_weight,
latent,
prev_rows,
prev_row_indices,
num_tokens,
num_reqs,
RAW_STRIDE: tl.constexpr,
PREV_ROW_STRIDE: tl.constexpr,
STATE_STRIDE: tl.constexpr,
STATE_ROW_STRIDE: tl.constexpr,
STATE_BLOCK: tl.constexpr,
COMPRESS_RATIO: tl.constexpr,
EPS: tl.constexpr,
JOIN_ROW_PTRS: tl.constexpr,
HAS_PREV_ROWS: tl.constexpr,
):
pid = tl.program_id(0)
d = tl.arange(0, 512)
@@ -152,6 +174,10 @@ def _fused_save_compress_norm_kernel(
if (position + 1) % 2 == 0:
ring = state + (state_slot // STATE_BLOCK).to(tl.int64) * STATE_STRIDE
prev = ring + ((position - 1) % STATE_BLOCK) * STATE_ROW_STRIDE
if HAS_PREV_ROWS:
prev_idx = tl.load(prev_row_indices + pid)
if prev_idx >= 0:
prev = prev_rows + prev_idx.to(tl.int64) * PREV_ROW_STRIDE
current = raw + start.to(tl.int64) * RAW_STRIDE
if JOIN_ROW_PTRS:
# Joining pointers hides row alignment from Triton; restate it.
@@ -177,19 +203,20 @@ def _fused_save_compress_norm_kernel(
weight_prev + weight_current
)
_store_latent(pooled, start, norm_weight, latent, EPS)
num_rows = tl.minimum(end - start, STATE_BLOCK)
for k in tl.range(0, num_rows):
token = end - num_rows + k
slot = tl.load(state_slots + token)
if slot >= 0:
row = (
state
+ (slot // STATE_BLOCK).to(tl.int64) * STATE_STRIDE
+ (slot % STATE_BLOCK) * STATE_ROW_STRIDE
)
src = raw + token.to(tl.int64) * RAW_STRIDE
tl.store(row + d, tl.load(src + d))
tl.store(row + 512 + d, tl.load(src + 512 + d))
if not HAS_PREV_ROWS:
num_rows = tl.minimum(end - start, STATE_BLOCK)
for k in tl.range(0, num_rows):
token = end - num_rows + k
slot = tl.load(state_slots + token)
if slot >= 0:
row = (
state
+ (slot // STATE_BLOCK).to(tl.int64) * STATE_STRIDE
+ (slot % STATE_BLOCK) * STATE_ROW_STRIDE
)
src = raw + token.to(tl.int64) * RAW_STRIDE
tl.store(row + d, tl.load(src + d))
tl.store(row + 512 + d, tl.load(src + 512 + d))
return
# Group program: one token (ratio 1) or the token of the pair that ends
@@ -212,6 +239,50 @@ def _fused_save_compress_norm_kernel(
_store_latent(pooled, t, norm_weight, latent, EPS)
def save_ring_rows(
rows: torch.Tensor, slot_mapping: torch.Tensor, state_cache: torch.Tensor
) -> None:
"""Store FP32 [kv, score] rows to their ring slots, skipping slot -1."""
assert rows.dtype == torch.float32 and rows.shape[1] == 1024
assert rows.stride(1) == 1 and state_cache.stride(2) == 1
assert slot_mapping.numel() == rows.shape[0]
if rows.shape[0] == 0:
return
_save_ring_rows_kernel[(rows.shape[0],)](
rows,
slot_mapping,
state_cache,
ROW_STRIDE=rows.stride(0),
STATE_STRIDE=state_cache.stride(0),
STATE_ROW_STRIDE=state_cache.stride(1),
STATE_BLOCK=state_cache.shape[1],
num_warps=4,
)
@triton.jit
def _save_ring_rows_kernel(
rows,
slot_mapping,
state,
ROW_STRIDE: tl.constexpr,
STATE_STRIDE: tl.constexpr,
STATE_ROW_STRIDE: tl.constexpr,
STATE_BLOCK: tl.constexpr,
):
pid = tl.program_id(0)
slot = tl.load(slot_mapping + pid)
if slot < 0:
return
d = tl.arange(0, 1024)
dst = (
state
+ (slot // STATE_BLOCK).to(tl.int64) * STATE_STRIDE
+ (slot % STATE_BLOCK) * STATE_ROW_STRIDE
)
tl.store(dst + d, tl.load(rows + pid.to(tl.int64) * ROW_STRIDE + d))
@triton.jit
def _store_latent(pooled, t, norm_weight, latent, EPS: tl.constexpr):
d = tl.arange(0, 512)
+92 -3
View File
@@ -8,6 +8,7 @@ import torch
from torch import nn
from vllm.config import VllmConfig, get_current_vllm_config
from vllm.distributed import get_pcp_group
from vllm.forward_context import get_forward_context
from vllm.model_executor.layers.attention_layer_base import AttentionLayerBase
from vllm.model_executor.layers.layernorm import RMSNorm
@@ -15,6 +16,7 @@ from vllm.model_executor.layers.linear import MergedColumnParallelLinear
from vllm.models.deepseek_v41.common.ops.fused_compress_quant_cache import (
fused_save_compress_norm,
rope_quant_insert,
save_ring_rows,
)
from vllm.platforms import current_platform
from vllm.triton_utils import tl, triton
@@ -36,6 +38,10 @@ class CompressorBackend(AttentionBackend):
def get_name() -> str:
return "CompressorBackend"
@classmethod
def supports_pcp(cls) -> bool:
return True
@staticmethod
def get_supported_kernel_block_sizes(kv_cache_spec=None) -> list[int | MultipleOf]:
return [MultipleOf(1)]
@@ -55,6 +61,12 @@ class CompressorMetadata:
slot_mapping: torch.Tensor
query_start_loc: torch.Tensor # [num_reqs + 1]
token_to_req_indices: torch.Tensor # [num_tokens]
# PCP only: every rank gathers the last row of each rank's local requests.
gather_tokens: torch.Tensor | None = None # [rows] this rank's rows
# [pcp * rows] ring slot of each request's newest row if it is open, else -1.
ring_write_slots: torch.Tensor | None = None
# [num_reqs] gathered row preceding each local chunk, or -1 to use the ring.
prev_row_indices: torch.Tensor | None = None
@triton.jit(do_not_specialize=["block_table_stride", "num_actual_tokens", "num_tokens"])
@@ -91,6 +103,7 @@ class CompressorMetadataBuilder(AttentionMetadataBuilder):
super().__init__(*args, **kwargs)
assert isinstance(self.kv_cache_spec, CircularBufferSpec)
self.capacity = self.kv_cache_spec.block_size
self.pcp_size = self.vllm_config.parallel_config.prefill_context_parallel_size
max_num_batched_tokens = (
self.vllm_config.scheduler_config.max_num_batched_tokens
)
@@ -100,6 +113,8 @@ class CompressorMetadataBuilder(AttentionMetadataBuilder):
self.slot_mapping_buffer = torch.empty(
max_num_batched_tokens, dtype=torch.int64, device=self.device
)
# The ring keeps only the open row under PCP, too few for spec decoding.
assert self.pcp_size == 1 or self.vllm_config.speculative_config is None
def build(
self,
@@ -107,7 +122,7 @@ class CompressorMetadataBuilder(AttentionMetadataBuilder):
common_attn_metadata: CommonAttentionMetadata,
fast_build: bool = False,
) -> CompressorMetadata:
num_tokens = common_attn_metadata.slot_mapping.numel()
num_tokens = common_attn_metadata.slot_mapping.numel() // self.pcp_size
positions = common_attn_metadata.positions
assert positions is not None
token_to_req_indices = common_attn_metadata.token_to_req_indices(
@@ -126,11 +141,66 @@ class CompressorMetadataBuilder(AttentionMetadataBuilder):
CAPACITY=self.capacity,
BLOCK=256,
)
return CompressorMetadata(
metadata = CompressorMetadata(
slot_mapping=slot_mapping,
query_start_loc=common_attn_metadata.query_start_loc,
token_to_req_indices=token_to_req_indices,
)
if self.pcp_size > 1:
self._link_pcp_chunks(metadata, common_attn_metadata.num_reqs, positions)
return metadata
def _link_pcp_chunks(
self, metadata: CompressorMetadata, num_reqs: int, positions: torch.Tensor
) -> None:
"""Link ratio-2 groups split across PCP chunks and pick the ring writes.
Every rank gathers ``(ring block, position)`` of each local request's
last token; the ring block names a request on every rank. Sorted, they
give each local chunk's predecessor row and each request's newest row.
"""
num_tokens = metadata.slot_mapping.numel()
# Equal on every rank, so the gathers line up.
num_rows = min(num_tokens, 2 * self.vllm_config.scheduler_config.max_num_seqs)
num_reqs = min(num_reqs, num_rows)
query_start_loc = metadata.query_start_loc.long()
starts = query_start_loc[:num_reqs].clamp(max=num_tokens - 1)
last = (query_start_loc[1 : num_reqs + 1] - 1).clamp(min=0)
nonempty = query_start_loc[1 : num_reqs + 1] > query_start_loc[:num_reqs]
def key(slots: torch.Tensor, pos: torch.Tensor) -> torch.Tensor:
return torch.where(slots >= 0, (slots // self.capacity) << 32 | pos, -1)
local_keys = torch.full((num_rows,), -1, dtype=torch.int64, device=self.device)
local_keys[:num_reqs] = key(
torch.where(nonempty, metadata.slot_mapping[last], -1), positions[last]
)
sorted_keys, order = get_pcp_group().all_gather(local_keys, dim=0).sort()
blocks, sorted_positions = sorted_keys >> 32, sorted_keys & 0xFFFFFFFF
newest = torch.ones_like(sorted_keys, dtype=torch.bool)
newest[:-1] = blocks[:-1] != blocks[1:]
is_open = (sorted_keys >= 0) & newest & (sorted_positions % 2 == 0)
ring_write_slots = torch.empty_like(sorted_keys)
ring_write_slots[order] = torch.where(
is_open, blocks * self.capacity + sorted_positions % self.capacity, -1
)
first_positions = positions[starts]
wanted = key(metadata.slot_mapping[starts], first_positions - 1)
found = torch.searchsorted(sorted_keys, wanted).clamp_(max=order.numel() - 1)
has_prev = (
nonempty
& (wanted >= 0)
& (first_positions % 2 == 1)
& (sorted_keys[found] == wanted)
)
gather_tokens = torch.zeros_like(local_keys)
gather_tokens[:num_reqs] = last
metadata.gather_tokens = gather_tokens
metadata.ring_write_slots = ring_write_slots
metadata.prev_row_indices = torch.where(has_prev, order[found], -1)
class CompressorStateCache(torch.nn.Module, AttentionLayerBase):
@@ -205,6 +275,8 @@ class DeepseekCompressor(nn.Module):
self.rotate = rotate
self.prefix = prefix
self.k_cache_prefix = k_cache_prefix
self.pcp_size = vllm_config.parallel_config.prefill_context_parallel_size
self.pcp_rank = get_pcp_group().rank_in_group if self.pcp_size > 1 else 0
# Ratio 1 pools single tokens, so the checkpoint carries no gate.
self.has_gate = compress_ratio > 1
@@ -256,15 +328,20 @@ class DeepseekCompressor(nn.Module):
) -> torch.Tensor | None:
"""Save states and return the BF16 latent for cache insertion and indexing.
Only valid group-boundary rows are written.
Only valid group-boundary rows are written. Under PCP each rank pools
its own rows; the last row of every rank's chunks is gathered, both to
close groups split across chunks and to keep every rank's ring equal.
"""
attn_metadata = get_forward_context().attn_metadata
if not isinstance(attn_metadata, dict):
return None
prev_rows = prev_row_indices = ring_write_slots = None
if self.state_cache is None:
state_cache = query_start_loc = token_to_req_indices = None
slot_mapping = cast(Any, attn_metadata[self.k_cache_prefix]).slot_mapping
if self.pcp_size > 1:
slot_mapping = slot_mapping.chunk(self.pcp_size)[self.pcp_rank]
else:
state_metadata = cast(
CompressorMetadata, attn_metadata[self.state_cache.prefix]
@@ -273,6 +350,13 @@ class DeepseekCompressor(nn.Module):
slot_mapping = state_metadata.slot_mapping
query_start_loc = state_metadata.query_start_loc
token_to_req_indices = state_metadata.token_to_req_indices
if self.pcp_size > 1:
assert state_metadata.gather_tokens is not None
prev_rows = get_pcp_group().all_gather(
kv_score[state_metadata.gather_tokens], dim=0
)
prev_row_indices = state_metadata.prev_row_indices
ring_write_slots = state_metadata.ring_write_slots
latent = torch.empty(
kv_score.shape[0],
@@ -291,7 +375,12 @@ class DeepseekCompressor(nn.Module):
self.rms_norm_eps,
self.compress_ratio,
latent,
prev_rows=prev_rows,
prev_row_indices=prev_row_indices,
)
if prev_rows is not None:
assert state_cache is not None and ring_write_slots is not None
save_ring_rows(prev_rows, ring_write_slots, state_cache)
return latent
def insert_cache(
@@ -42,6 +42,10 @@ class DeepseekSparseSWAFlashMLAMetadataBuilder(DeepseekV41SparseSWAMetadataBuild
class DeepseekSparseSWAFlashMLABackend(DeepseekSparseSWABackend):
@classmethod
def supports_pcp(cls) -> bool:
return True
@staticmethod
def get_builder_cls() -> type[DeepseekSparseSWAFlashMLAMetadataBuilder]:
return DeepseekSparseSWAFlashMLAMetadataBuilder
@@ -132,6 +136,14 @@ class DeepseekV4FlashMLAAttention(DeepseekV4Attention):
)
assert swa_metadata is not None
if self.use_pcp:
num_tokens = (
swa_metadata.num_decode_tokens + swa_metadata.num_prefill_tokens
)
output[num_tokens:].zero_()
if num_tokens == 0:
return
swa_only = self.compress_ratio == 0
# SWA-only layers (compress_ratio == 0) don't have their own KV cache
# allocation; consumers read the kv source's cache, which is bound on
+34
View File
@@ -14,6 +14,7 @@ from vllm.config import VllmConfig
from vllm.config.kernel import MEGA_MOE_BACKENDS, NATIVE_MEGA_MOE_BACKENDS
from vllm.distributed import (
get_engram_dp_size,
get_pcp_group,
get_pp_group,
get_tensor_model_parallel_world_size,
get_tp_group,
@@ -204,6 +205,17 @@ def _select_dsv4_attn_cls(vllm_config: VllmConfig) -> type[DeepseekV4Attention]:
FlashMLA path.
"""
backend = vllm_config.attention_config.backend
if vllm_config.parallel_config.prefill_context_parallel_size > 1:
if backend not in (
None,
AttentionBackendEnum.FLASHMLA_SPARSE,
AttentionBackendEnum.FLASHMLA_SPARSE_DSV4,
AttentionBackendEnum.FLASHMLA_SPARSE_DSV41,
):
raise NotImplementedError(
"DeepSeek-V4.1 PCP requires FlashMLA sparse attention."
)
return DeepseekV4FlashMLAAttention
device_capability = current_platform.get_device_capability()
if backend in (
AttentionBackendEnum.FLASHINFER_MLA_SPARSE,
@@ -684,6 +696,21 @@ class DeepseekV4Model(nn.Module, EagleModelMixin):
self.config = config
self.quant_config = quant_config
self.parallel_config = vllm_config.parallel_config
if self.parallel_config.prefill_context_parallel_size > 1 and (
self.parallel_config.pipeline_parallel_size != 1
or self.parallel_config.data_parallel_size != 1
or self.parallel_config.decode_context_parallel_size != 1
or self.parallel_config.use_ubatching
or vllm_config.speculative_config is not None
or vllm_config.kv_transfer_config is not None
or vllm_config.cache_config.enable_prefix_caching
or vllm_config.kernel_config.moe_backend in MEGA_MOE_BACKENDS
):
raise NotImplementedError(
"DeepSeek-V4.1 PCP currently requires PP=DP=DCP=1, with no "
"microbatching, speculative decoding, KV transfer, prefix "
"caching, or MegaMoE."
)
self.use_native_mega_moe = (
vllm_config.kernel_config.moe_backend in NATIVE_MEGA_MOE_BACKENDS
)
@@ -1019,6 +1046,13 @@ class DeepseekV4Model(nn.Module, EagleModelMixin):
hidden_states = sp_shard(hidden_states)
input_ids = sp_shard(input_ids)
if (
input_ids is not None
and self.parallel_config.prefill_context_parallel_size > 1
and not self.parallel_config.enable_expert_parallel
):
input_ids = get_pcp_group().all_gather(input_ids, dim=0)
residual, post_mix, res_mix = None, None, None
pre_mix: torch.Tensor | None = None
if not get_pp_group().is_first_rank:
+13 -5
View File
@@ -41,7 +41,8 @@ from vllm.v1.worker.utils import AttentionGroup
def _gather_lookback_kernel(
lookback_ptr,
idx_mapping_ptr,
num_computed_tokens_ptr,
query_start_loc_ptr,
positions_ptr,
all_token_ids_ptr,
all_token_ids_stride,
num_reqs,
@@ -52,13 +53,16 @@ def _gather_lookback_kernel(
batch_idx = tl.program_id(0)
in_batch = batch_idx < num_reqs
req_state_idx = tl.load(idx_mapping_ptr + batch_idx, mask=in_batch, other=0)
num_computed = tl.load(num_computed_tokens_ptr + req_state_idx)
begin = tl.load(query_start_loc_ptr + batch_idx, mask=in_batch, other=0)
end = tl.load(query_start_loc_ptr + batch_idx + 1, mask=in_batch, other=0)
in_batch = in_batch & (end > begin)
num_computed = tl.load(positions_ptr + begin, mask=in_batch, other=0)
offs = tl.arange(0, BLOCK_DEPTH)
pos = num_computed - 1 - offs
valid = in_batch & (offs < DEPTH) & (pos >= 0)
ids = tl.load(
all_token_ids_ptr + req_state_idx * all_token_ids_stride + pos,
all_token_ids_ptr + req_state_idx.to(tl.int64) * all_token_ids_stride + pos,
mask=valid,
other=-1,
)
@@ -200,8 +204,11 @@ class DeepseekV41ModelState(DefaultModelState):
self.lookback_token_ids: torch.Tensor | None = None
if depth > 0:
# Persistent so a captured graph can read it on replay.
max_num_reqs = self.max_num_reqs
if vllm_config.parallel_config.prefill_context_parallel_size > 1:
max_num_reqs *= 2
self.lookback_token_ids = torch.full(
(self.max_num_reqs, depth), -1, dtype=torch.int32, device=device
(max_num_reqs, depth), -1, dtype=torch.int32, device=device
)
# Per request state index; batches gather from it (see prepare_attn).
@@ -287,7 +294,8 @@ class DeepseekV41ModelState(DefaultModelState):
_gather_lookback_kernel[(window.shape[0],)](
window,
input_batch.idx_mapping,
req_states.num_computed_tokens.gpu,
input_batch.query_start_loc,
input_batch.positions,
all_token_ids,
all_token_ids.stride(0),
input_batch.idx_mapping.shape[0],
+44 -1
View File
@@ -9,6 +9,7 @@ import torch
from vllm.config import VllmConfig
from vllm.config.cache import CacheDType
from vllm.distributed import get_pcp_group
from vllm.platforms import current_platform
from vllm.platforms.interface import DeviceCapability
from vllm.v1.attention.backend import (
@@ -24,6 +25,7 @@ from vllm.v1.attention.backends.mla.sparse_swa import (
_LAYER_TYPE_C1A,
_LAYER_TYPE_C2A,
_LAYER_TYPE_SWAONLY,
DeepseekSparseSWAMetadata,
DeepseekSparseSWAMetadataBuilder,
)
from vllm.v1.kv_cache_interface import AttentionSpec
@@ -60,6 +62,12 @@ class DeepseekV41SparseSWAMetadataBuilder(DeepseekSparseSWAMetadataBuilder):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pcp_size = self.vllm_config.parallel_config.prefill_context_parallel_size
self.pcp_rank = get_pcp_group().rank_in_group if self.pcp_size > 1 else 0
if self.pcp_size > 1:
self.no_replay_start = self.no_replay_start.new_zeros(
2 * self.vllm_config.scheduler_config.max_num_seqs + 1
)
compress_ratios = getattr(
self.vllm_config.model_config.hf_config, "compress_ratios", None
) or [0]
@@ -67,6 +75,24 @@ class DeepseekV41SparseSWAMetadataBuilder(DeepseekSparseSWAMetadataBuilder):
deepseek_v41_layer_type(int(ratio)) for ratio in compress_ratios
}
def build(
self,
common_prefix_len: int,
common_attn_metadata: CommonAttentionMetadata,
fast_build: bool = False,
replay_start: torch.Tensor | None = None,
) -> DeepseekSparseSWAMetadata:
cm = common_attn_metadata
if self.pcp_size > 1:
local_slots = cm.slot_mapping.chunk(self.pcp_size)[self.pcp_rank]
cm = cm.replace(slot_mapping=local_slots)
metadata = super().build(common_prefix_len, cm, fast_build, replay_start)
if self.pcp_size > 1:
metadata.cache_slot_mapping = common_attn_metadata.slot_mapping
assert cm.positions is not None
metadata.cache_positions = get_pcp_group().all_gather(cm.positions, dim=0)
return metadata
class DeepseekV4SparseMLABackend(AttentionBackend):
"""DeepSeek-V4.1 sparse-MLA backend base.
@@ -155,6 +181,8 @@ class DeepseekV4SparseMLAMetadataBuilder(
) -> None:
super().__init__(kv_cache_spec, layer_names, vllm_config, device)
self.model_config = vllm_config.model_config
self.pcp_size = vllm_config.parallel_config.prefill_context_parallel_size
self.pcp_rank = get_pcp_group().rank_in_group if self.pcp_size > 1 else 0
# Classify single-token queries (plus num_speculative_tokens via
# supports_spec_as_decode=True) as decodes; longer queries go to prefill.
self._init_reorder_batch_threshold(1, supports_spec_as_decode=True)
@@ -193,9 +221,12 @@ class DeepseekV4SparseMLAMetadataBuilder(
slot_mapping = cm.slot_mapping
if self.compress_ratio > 1:
local_slots = slot_mapping
if self.pcp_size > 1:
local_slots = slot_mapping.chunk(self.pcp_size)[self.pcp_rank]
slot_mapping = get_compressed_slot_mapping(
cm.num_actual_tokens,
cm.slot_mapping,
local_slots,
cm.query_start_loc,
cm.seq_lens,
cm.block_table_tensor.clamp_(min=0),
@@ -203,6 +234,10 @@ class DeepseekV4SparseMLAMetadataBuilder(
self.compress_ratio,
out=self.compressed_slot_mapping_buffer,
)
if self.pcp_size > 1:
slot_mapping = get_pcp_group().all_gather(
self.compressed_slot_mapping_buffer[: local_slots.numel()], dim=0
)
return DeepseekV4FlashMLAMetadata(
num_reqs=cm.num_reqs,
@@ -223,6 +258,10 @@ class DeepseekV4FlashMLAMetadataBuilder(DeepseekV4SparseMLAMetadataBuilder):
class DeepseekV4FlashMLABackend(DeepseekV4SparseMLABackend):
@classmethod
def supports_pcp(cls) -> bool:
return True
@staticmethod
def get_name() -> str:
return "FLASHMLA_SPARSE_DSV41"
@@ -250,6 +289,10 @@ class FlashMLAMegaAttnBackend(DeepseekV4FlashMLABackend):
"nvfp4_ds_mla", # V4.1 fp8 SWA cache + NVFP4 compressed cache
]
@classmethod
def supports_pcp(cls) -> bool:
return False
@staticmethod
def get_name() -> str:
return "FLASHMLA_MEGA_ATTN_DSV41"