[Core] Add MRV2 virtual-batch PCP for MLA (#46570)

Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com>
Signed-off-by: Matthew Bonanni <mbonanni@redhat.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
Co-authored-by: Matthew Bonanni <mbonanni@redhat.com>
This commit is contained in:
Lucas Wilkinson
2026-07-19 02:53:15 +00:00
committed by GitHub
co-authored by Codex OpenAI Codex Matthew Bonanni
parent 9243e0124e
commit b6ff8a2f50
39 changed files with 1204 additions and 196 deletions
+14 -2
View File
@@ -281,6 +281,13 @@ class AttentionBackend(ABC):
def supports_kv_connector(cls) -> bool:
return True
@classmethod
def supports_pcp(cls) -> bool:
try:
return cls.get_impl_cls().supports_pcp
except NotImplementedError:
return False
@classmethod
def supports_attn_type(cls, attn_type: str) -> bool:
"""Check if backend supports a given attention type.
@@ -327,6 +334,7 @@ class AttentionBackend(ABC):
use_non_causal: bool = False,
use_batch_invariant: bool = False,
use_kv_connector: bool = False,
use_pcp: bool = False,
) -> list[str]:
invalid_reasons = []
if not cls.supports_head_size(head_size):
@@ -367,6 +375,8 @@ class AttentionBackend(ABC):
invalid_reasons.append("batch invariance not supported")
if use_kv_connector and not cls.supports_kv_connector():
invalid_reasons.append("KV connector not supported")
if use_pcp and not cls.supports_pcp():
invalid_reasons.append("PCP not supported")
combination_reason = cls.supports_combination(
head_size,
dtype,
@@ -858,8 +868,8 @@ class AttentionImplBase(ABC, Generic[T]):
except AssertionError:
self.pcp_world_size = 1
self.pcp_rank = 0
self.total_cp_world_size = self.pcp_world_size * self.dcp_world_size
self.total_cp_rank = self.pcp_rank * self.dcp_world_size + self.dcp_rank
self.total_cp_world_size = self.dcp_world_size
self.total_cp_rank = self.dcp_rank
self.need_to_return_lse_for_decode = (
self.dcp_world_size > 1 and self.can_return_lse_for_decode
@@ -987,6 +997,8 @@ class AttentionImpl(AttentionImplBase[T], Generic[T]):
class MLAAttentionImpl(AttentionImplBase[T], Generic[T]):
"""MLA attention implementation with forward_mqa and forward_mha methods."""
supports_pcp: bool = True
@abstractmethod
def __init__(
self,
+19 -4
View File
@@ -6,7 +6,7 @@ import torch
import vllm.envs as envs
from vllm.config import VllmConfig
from vllm.distributed import get_dcp_group
from vllm.distributed import get_dcp_group, get_pcp_group
from vllm.logger import init_logger
from vllm.platforms import current_platform
from vllm.triton_utils import tl, triton
@@ -29,7 +29,7 @@ from vllm.v1.attention.backends.utils import (
split_decodes_and_prefills,
)
from vllm.v1.kv_cache_interface import AttentionSpec, MLAAttentionSpec
from vllm.v1.worker.cp_utils import get_total_cp_world_size
from vllm.v1.worker.cp_utils import get_kv_cache_shard_count
logger = init_logger(__name__)
@@ -109,7 +109,7 @@ def split_indexer_prefill_chunks(
end += 1
req_slice = slice(start + request_offset, end + request_offset)
max_q = max(1, max_logits_elems // chunk_n) if chunk_n > 0 else chunk_m
max_q = max(1, max_logits_elems // chunk_n) if chunk_n > 0 else max(1, chunk_m)
for q_off in range(0, chunk_m, max_q):
sub_m = min(max_q, chunk_m - q_off)
chunks.append((req_slice, slice(q_off, q_off + sub_m)))
@@ -118,6 +118,10 @@ def split_indexer_prefill_chunks(
class DeepseekV32IndexerBackend(AttentionBackend):
@classmethod
def supports_pcp(cls) -> bool:
return True
@staticmethod
def get_name() -> str:
return "DEEPSEEK_V32_INDEXER"
@@ -256,6 +260,8 @@ class DeepseekV32IndexerMetadataBuilder(AttentionMetadataBuilder):
parallel_config = self.vllm_config.parallel_config
self.dcp_world_size = parallel_config.decode_context_parallel_size
self.dcp_rank = get_dcp_group().rank_in_group if self.dcp_world_size > 1 else 0
self.pcp_world_size = parallel_config.prefill_context_parallel_size
self.use_pcp = self.pcp_world_size > 1
self.cp_kv_cache_interleave_size = parallel_config.cp_kv_cache_interleave_size
# The DCP sparse-indexer code is parameterized by interleave size, but
# interleave > 1 is not yet validated end-to-end (gsm8k parity fails),
@@ -339,7 +345,7 @@ class DeepseekV32IndexerMetadataBuilder(AttentionMetadataBuilder):
)
max_num_blocks_per_req = cdiv(
self.vllm_config.model_config.max_model_len,
self.kv_cache_spec.block_size * get_total_cp_world_size(),
self.kv_cache_spec.block_size * get_kv_cache_shard_count(),
)
self.expanded_block_table_buffer = torch.zeros(
(
@@ -570,6 +576,7 @@ class DeepseekV32IndexerMetadataBuilder(AttentionMetadataBuilder):
common_attn_metadata,
decode_threshold=self.decode_threshold,
require_uniform=not self.use_flattening,
treat_short_extends_as_decodes=not self.use_pcp,
)
)
@@ -579,6 +586,9 @@ class DeepseekV32IndexerMetadataBuilder(AttentionMetadataBuilder):
compressed_slot_mapping = slot_mapping
compressed_seq_lens = seq_lens
if self.compress_ratio > 1:
padded_num_tokens = num_tokens
if self.pcp_world_size > 1:
padded_num_tokens = slot_mapping.shape[0] // self.pcp_world_size
compressed_slot_mapping = get_compressed_slot_mapping(
num_tokens,
query_start_loc,
@@ -588,6 +598,11 @@ class DeepseekV32IndexerMetadataBuilder(AttentionMetadataBuilder):
self.compress_ratio,
out=self.compressed_slot_mapping_buffer,
)
if self.pcp_world_size > 1:
compressed_slot_mapping = get_pcp_group().all_gather(
self.compressed_slot_mapping_buffer[:padded_num_tokens],
dim=0,
)
compressed_seq_lens = seq_lens // self.compress_ratio
prefill_metadata = None
+3 -1
View File
@@ -612,7 +612,9 @@ def split_decodes_and_prefills(
# check if we are in a padded uniform batch; this is used for full-CGs, some
# requests may have a query length of 0 but since they are padding its fine
# to treat them as decodes (ensures num_decodes matches the captured size)
if torch.all((query_lens == query_lens[0]) | (query_lens == 0)):
if treat_short_extends_as_decodes and torch.all(
(query_lens == query_lens[0]) | (query_lens == 0)
):
return num_reqs, 0, num_tokens, 0 # all decodes
is_prefill = query_lens != query_lens[0]
else:
+4 -1
View File
@@ -36,6 +36,7 @@ class AttentionSelectorConfig(NamedTuple):
use_non_causal: bool = False
use_batch_invariant: bool = False
use_kv_connector: bool = False
use_pcp: bool = False
def __repr__(self):
return (
@@ -52,7 +53,8 @@ class AttentionSelectorConfig(NamedTuple):
f"has_sliding_window={self.has_sliding_window}, "
f"use_non_causal={self.use_non_causal}, "
f"use_batch_invariant={self.use_batch_invariant}, "
f"use_kv_connector={self.use_kv_connector})"
f"use_kv_connector={self.use_kv_connector}, "
f"use_pcp={self.use_pcp})"
)
@@ -150,6 +152,7 @@ def get_attn_backend(
use_non_causal=vllm_config.attention_config.use_non_causal,
use_batch_invariant=envs.VLLM_BATCH_INVARIANT,
use_kv_connector=use_kv_connector,
use_pcp=vllm_config.parallel_config.prefill_context_parallel_size > 1,
)
# A per-KV-group override (keyed by KVCacheSpecKind) takes precedence over