[PCP][Spec Decode] Adds PCP support for single-module MTP and replicated DSpark. (#56107)
Signed-off-by: Summer Yang <girasoleyang@gmail.com> Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com> Co-authored-by: Summer Yang <girasoleyang@gmail.com> Co-authored-by: OpenAI Codex <codex@openai.com> Co-authored-by: OpenAI Codex <noreply@openai.com>
This commit is contained in:
co-authored by
Summer Yang
OpenAI Codex
OpenAI Codex
parent
e7edf17cea
commit
980c16c8e4
@@ -6,7 +6,7 @@ from dataclasses import dataclass, replace
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
from vllm.config import CUDAGraphMode, VllmConfig
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.distributed.parallel_state import get_dcp_group, get_pcp_group
|
||||
from vllm.logger import init_logger
|
||||
from vllm.v1.attention.backends.utils import PAD_SLOT_ID
|
||||
@@ -15,10 +15,7 @@ from vllm.v1.worker.gpu.buffer_utils import async_copy_to_gpu
|
||||
from vllm.v1.worker.gpu.input_batch import (
|
||||
InputBatch,
|
||||
InputBuffers,
|
||||
combine_sampled_and_draft_tokens,
|
||||
prepare_pos_seq_lens,
|
||||
)
|
||||
from vllm.v1.worker.gpu.states import RequestState
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -48,7 +45,6 @@ class PCPManager:
|
||||
pcp_world_size: int,
|
||||
pcp_rank: int,
|
||||
device: torch.device,
|
||||
req_states: RequestState | None = None,
|
||||
max_num_reqs: int | None = None,
|
||||
max_num_tokens: int | None = None,
|
||||
block_tables: BlockTables | None = None,
|
||||
@@ -64,7 +60,9 @@ class PCPManager:
|
||||
self.cp_interleave = cp_interleave
|
||||
|
||||
self._global_batch: InputBatch | None = None
|
||||
self._req_states = req_states
|
||||
self._local_batch: InputBatch | None = None
|
||||
self._local_gather_idx: torch.Tensor | None = None
|
||||
self.draft_prefill_batch: InputBatch | None = None
|
||||
self._block_tables = block_tables
|
||||
self._hidden_restore_idx: torch.Tensor | None = None
|
||||
self._padded_gather_idx: torch.Tensor | None = None
|
||||
@@ -77,11 +75,6 @@ class PCPManager:
|
||||
if max_num_local_reqs is not None and max_num_tokens is not None
|
||||
else None
|
||||
)
|
||||
self._local_req_idx = (
|
||||
torch.arange(max_num_local_reqs, dtype=torch.int32, device=device)
|
||||
if max_num_local_reqs is not None
|
||||
else None
|
||||
)
|
||||
self._local_block_tables: tuple[torch.Tensor, ...] | None
|
||||
self._local_block_table_ptrs: torch.Tensor | None
|
||||
if block_tables is not None and max_num_local_reqs is not None:
|
||||
@@ -144,19 +137,23 @@ class PCPManager:
|
||||
raise NotImplementedError("MRV2 PCP does not support MM inputs yet.")
|
||||
if vllm_config.lora_config is not None:
|
||||
raise NotImplementedError("MRV2 PCP does not support LoRA yet.")
|
||||
if vllm_config.speculative_config is not None:
|
||||
raise NotImplementedError(
|
||||
"MRV2 PCP does not support speculative decoding yet."
|
||||
)
|
||||
is_sparse_mla = hasattr(model_config.hf_text_config, "index_topk")
|
||||
if (
|
||||
is_sparse_mla
|
||||
and vllm_config.compilation_config.cudagraph_mode != CUDAGraphMode.NONE
|
||||
):
|
||||
raise NotImplementedError(
|
||||
"MRV2 sparse MLA PCP does not support CUDA graphs yet. "
|
||||
"Set -cc.cudagraph_mode=NONE."
|
||||
)
|
||||
speculative_config = vllm_config.speculative_config
|
||||
if speculative_config is not None:
|
||||
if speculative_config.use_dspark():
|
||||
dcp_size = parallel_config.decode_context_parallel_size
|
||||
if dcp_size not in (1, pcp_size):
|
||||
raise NotImplementedError(
|
||||
"MRV2 PCP DSpark requires DCP=1 or DCP=PCP; got "
|
||||
f"DCP={dcp_size}, PCP={pcp_size}."
|
||||
)
|
||||
elif (
|
||||
speculative_config.method != "mtp"
|
||||
or speculative_config.use_multi_module_mtp()
|
||||
):
|
||||
raise NotImplementedError(
|
||||
"MRV2 PCP only supports DSpark or single-module MTP "
|
||||
"speculative decoding."
|
||||
)
|
||||
if vllm_config.compilation_config.cudagraph_mode.has_full_cudagraphs():
|
||||
raise NotImplementedError("MRV2 PCP supports PIECEWISE CUDA graphs only.")
|
||||
|
||||
@@ -358,12 +355,8 @@ class PCPManager:
|
||||
input_batch: InputBatch,
|
||||
padded_num_tokens: int | None = None,
|
||||
) -> InputBatch:
|
||||
assert self._req_states is not None
|
||||
assert self._input_buffers is not None
|
||||
req_states = self._req_states
|
||||
input_buffers = self._input_buffers
|
||||
if input_batch.num_draft_tokens > 0:
|
||||
raise NotImplementedError("MRV2 PCP does not support spec decode yet.")
|
||||
|
||||
global_batch = input_batch
|
||||
self._global_batch = global_batch
|
||||
@@ -458,12 +451,21 @@ class PCPManager:
|
||||
local_gather_idx = self._padded_gather_idx[
|
||||
rank_token_start : rank_token_start + num_local_tokens_padded
|
||||
]
|
||||
self._local_gather_idx = local_gather_idx
|
||||
torch.index_select(
|
||||
global_batch.input_ids,
|
||||
0,
|
||||
local_gather_idx,
|
||||
out=input_buffers.input_ids[:num_local_tokens_padded],
|
||||
)
|
||||
# Keep the GPU request-state cursor materialized by prepare_inputs().
|
||||
# The CPU cursor can lag after speculative rejection.
|
||||
torch.index_select(
|
||||
global_batch.positions,
|
||||
0,
|
||||
local_gather_idx,
|
||||
out=input_buffers.positions[:num_local_tokens_padded],
|
||||
)
|
||||
|
||||
local_query_start_loc_np = np.empty(
|
||||
input_buffers.max_num_reqs + 1, dtype=np.int32
|
||||
@@ -478,17 +480,16 @@ class PCPManager:
|
||||
local_to_global_req_idx = async_copy_to_gpu(
|
||||
local_to_global_req_idx_np, device=self.device
|
||||
)
|
||||
local_start_pos = async_copy_to_gpu(local_start_pos_np, device=self.device)
|
||||
|
||||
assert self._local_req_idx is not None
|
||||
prepare_pos_seq_lens(
|
||||
self._local_req_idx[:num_local_reqs],
|
||||
local_query_start_loc,
|
||||
local_start_pos,
|
||||
input_buffers.positions,
|
||||
input_buffers.seq_lens[:num_local_reqs],
|
||||
)
|
||||
seq_lens = input_buffers.seq_lens[:num_local_reqs]
|
||||
if num_local_tokens > 0:
|
||||
local_end_positions = torch.index_select(
|
||||
input_buffers.positions,
|
||||
0,
|
||||
local_query_start_loc[1:] - 1,
|
||||
)
|
||||
seq_lens.copy_(local_end_positions + 1)
|
||||
else:
|
||||
seq_lens.zero_()
|
||||
is_padding = input_buffers.is_padding[:num_local_tokens_padded]
|
||||
is_padding[:num_local_tokens].fill_(False)
|
||||
is_padding[num_local_tokens:].fill_(True)
|
||||
@@ -511,18 +512,9 @@ class PCPManager:
|
||||
cu_num_logits = torch.zeros(
|
||||
num_local_reqs + 1, device=self.device, dtype=torch.int32
|
||||
)
|
||||
logits_indices = combine_sampled_and_draft_tokens(
|
||||
input_buffers.input_ids,
|
||||
local_to_global_req_idx,
|
||||
req_states.last_sampled_tokens,
|
||||
local_query_start_loc,
|
||||
seq_lens,
|
||||
req_states.prefill_len.gpu,
|
||||
req_states.draft_tokens,
|
||||
cu_num_logits,
|
||||
total_num_logits,
|
||||
1,
|
||||
)
|
||||
# Local logits are never sampled. The complete hidden-state tensor is
|
||||
# restored first and sampled with the untouched global InputBatch.
|
||||
logits_indices = local_query_start_loc[1:] - 1
|
||||
|
||||
local_prefill_len_np = global_batch.prefill_len_np[
|
||||
local_to_global_batch_req_idx_np
|
||||
@@ -536,7 +528,7 @@ class PCPManager:
|
||||
seq_lens_cpu_upper_bound_np = np.zeros(num_local_reqs, dtype=np.int32)
|
||||
seq_lens_cpu_upper_bound_np[:] = local_start_pos_np + local_num_scheduled_tokens
|
||||
|
||||
return replace(
|
||||
self._local_batch = replace(
|
||||
input_batch,
|
||||
req_ids=local_req_ids,
|
||||
num_reqs=num_local_reqs,
|
||||
@@ -570,6 +562,7 @@ class PCPManager:
|
||||
cu_num_logits_np=cu_num_logits_np,
|
||||
prompt_lens=None,
|
||||
)
|
||||
return self._local_batch
|
||||
|
||||
def prepare_attn(
|
||||
self, input_batch: InputBatch
|
||||
@@ -640,6 +633,46 @@ class PCPManager:
|
||||
gathered = get_pcp_group().all_gather(hidden_states, dim=0)
|
||||
return gathered[self._hidden_restore_idx]
|
||||
|
||||
def get_draft_input_buffers(
|
||||
self, input_buffers: InputBuffers
|
||||
) -> InputBatch | InputBuffers:
|
||||
return self.draft_prefill_batch or input_buffers
|
||||
|
||||
def prepare_draft_prefill(
|
||||
self,
|
||||
input_batch: InputBatch,
|
||||
input_ids: torch.Tensor,
|
||||
) -> None:
|
||||
if input_batch is not self._global_batch or self._local_batch is None:
|
||||
return
|
||||
local_batch = self._local_batch
|
||||
assert self._local_gather_idx is not None
|
||||
num_local_tokens = self._local_gather_idx.shape[0]
|
||||
torch.index_select(
|
||||
input_ids,
|
||||
0,
|
||||
self._local_gather_idx,
|
||||
out=local_batch.input_ids[:num_local_tokens],
|
||||
)
|
||||
self.draft_prefill_batch = local_batch
|
||||
|
||||
def restore_draft_prefill(
|
||||
self,
|
||||
last_hidden_states: torch.Tensor,
|
||||
hidden_states: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
if self.draft_prefill_batch is None:
|
||||
return last_hidden_states, hidden_states
|
||||
local_last_hidden_states = last_hidden_states
|
||||
last_hidden_states = self.restore_hidden_states(local_last_hidden_states)
|
||||
hidden_states = (
|
||||
last_hidden_states
|
||||
if local_last_hidden_states is hidden_states
|
||||
else self.restore_hidden_states(hidden_states)
|
||||
)
|
||||
self.draft_prefill_batch = None
|
||||
return last_hidden_states, hidden_states
|
||||
|
||||
def restore_for_sampling(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
@@ -686,7 +719,6 @@ def maybe_build_pcp_manager(
|
||||
vllm_config: VllmConfig,
|
||||
device: torch.device,
|
||||
supports_mm_inputs: bool,
|
||||
req_states: RequestState,
|
||||
block_tables: BlockTables,
|
||||
cls: type[PCPManager] = PCPManager,
|
||||
) -> PCPManager | None:
|
||||
@@ -705,7 +737,6 @@ def maybe_build_pcp_manager(
|
||||
pcp_world_size=pcp_size,
|
||||
pcp_rank=pcp_rank,
|
||||
device=device,
|
||||
req_states=req_states,
|
||||
max_num_reqs=vllm_config.scheduler_config.max_num_seqs,
|
||||
max_num_tokens=vllm_config.scheduler_config.max_num_batched_tokens,
|
||||
block_tables=block_tables,
|
||||
|
||||
Reference in New Issue
Block a user