Skip to content

Commit 8abac64

Browse files
committed
Streamlined the code and removed unnecessary parts.
Signed-off-by: vx120 <893600387@qq.com>
1 parent 5c9f82a commit 8abac64

11 files changed

Lines changed: 89 additions & 368 deletions

File tree

vllm/config/vllm.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -944,11 +944,6 @@ def _verify_sampling_replay_config(self) -> None:
944944
return
945945
if not self.use_v2_model_runner:
946946
raise ValueError("sampling distribution replay requires Model Runner V2")
947-
if model_config.logprobs_mode != "processed_logprobs":
948-
raise ValueError(
949-
"sampling distribution replay requires "
950-
"logprobs_mode='processed_logprobs'"
951-
)
952947
if self.speculative_config is not None:
953948
raise ValueError(
954949
"sampling distribution replay does not support speculative decoding"

vllm/outputs.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ def add(self, next_output: "RequestOutput", aggregate: bool) -> None:
186186
if next_completion.logprobs:
187187
assert completion.logprobs is not None
188188
completion.logprobs.extend(next_completion.logprobs) # type: ignore[arg-type]
189-
_merge_sampling_masks(completion, next_completion)
190189
completion.cumulative_logprob = (
191190
next_completion.cumulative_logprob
192191
)
@@ -216,23 +215,6 @@ def __repr__(self) -> str:
216215
)
217216

218217

219-
def _merge_sampling_masks(
220-
completion: CompletionOutput, next_completion: CompletionOutput
221-
) -> None:
222-
current_mask = completion.sampling_mask
223-
next_mask = next_completion.sampling_mask
224-
if current_mask is None and next_mask is None:
225-
return
226-
if current_mask is None or next_mask is None:
227-
raise RuntimeError("cannot merge partially missing sampling masks")
228-
current_token_ids = list(current_mask.token_ids)
229-
current_offsets = list(current_mask.offsets)
230-
offset = len(current_token_ids)
231-
current_token_ids.extend(next_mask.token_ids)
232-
current_offsets.extend(offset + item for item in next_mask.offsets[1:])
233-
completion.sampling_mask = SamplingMask(current_token_ids, current_offsets)
234-
235-
236218
# Sentinel to indicate request is finished, used with streaming inputs.
237219
STREAM_FINISHED = RequestOutput(
238220
request_id="",

vllm/v1/core/sched/scheduler.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,6 @@ def __init__(
332332
self.enable_return_routed_experts = (
333333
vllm_config.model_config.enable_return_routed_experts
334334
)
335-
self.enable_return_sampling_mask = (
336-
vllm_config.model_config.enable_return_sampling_mask
337-
)
338-
339335
if self.enable_return_routed_experts:
340336
assert self.dcp_world_size == 1 and self.pcp_world_size == 1, (
341337
"enable_return_routed_experts does not support context parallelism "
@@ -1825,16 +1821,10 @@ def update_from_output(
18251821
new_logprobs = logprobs.slice_request(req_index, len(new_token_ids))
18261822

18271823
sampling_masks = model_runner_output.sampling_masks
1828-
if new_token_ids:
1829-
if sampling_masks is None:
1830-
if self.enable_return_sampling_mask:
1831-
raise RuntimeError(
1832-
f"missing sampling mask for request {req_id}"
1833-
)
1834-
else:
1835-
new_sampling_mask = sampling_masks.slice_request(
1836-
req_index, len(new_token_ids)
1837-
)
1824+
if new_token_ids and sampling_masks is not None:
1825+
new_sampling_mask = sampling_masks.slice_request(
1826+
req_index, len(new_token_ids)
1827+
)
18381828

18391829
if num_nans_in_logits is not None and req_id in num_nans_in_logits:
18401830
request.num_nans_in_logits = num_nans_in_logits[req_id]

vllm/v1/engine/input_processor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ def process_inputs(
326326
)
327327
if self.tokenizer is not None:
328328
sampling_params.update_from_tokenizer(self.tokenizer)
329+
if (
330+
self.model_config.enable_return_sampling_mask
331+
and sampling_params.temperature <= 0
332+
):
333+
raise ValueError(
334+
"sampling distribution replay requires temperature > 0"
335+
)
329336
else:
330337
pooling_params = params.clone()
331338

vllm/v1/engine/output_processor.py

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ def __init__(
180180

181181
# Routed experts accumulation (prompt + sample chunks)
182182
self.routed_experts_chunks: list[np.ndarray] = []
183-
self.sampling_mask_token_ids: list[int] = []
184-
self.sampling_mask_offsets: list[int] = [0]
183+
self.sampling_mask_chunks: list[SamplingMaskLists] = []
185184

186185
# Stream Interval
187186
self.stream_interval = stream_interval
@@ -340,33 +339,6 @@ def make_request_output(
340339
ec_transfer_params,
341340
)
342341

343-
def update_sampling_mask(
344-
self,
345-
new_token_ids: list[int],
346-
sampling_mask: SamplingMaskLists | None,
347-
) -> None:
348-
if sampling_mask is None:
349-
if new_token_ids and len(self.sampling_mask_offsets) > 1:
350-
raise RuntimeError(
351-
f"missing sampling mask for request {self.request_id}"
352-
)
353-
return
354-
if len(sampling_mask.counts) != len(new_token_ids):
355-
raise RuntimeError(
356-
f"sampling mask row count does not match tokens for request "
357-
f"{self.request_id}: {len(sampling_mask.counts)} rows for "
358-
f"{len(new_token_ids)} tokens"
359-
)
360-
for row, raw_count in zip(sampling_mask.token_ids, sampling_mask.counts):
361-
count = int(raw_count)
362-
if count <= 0 or count > len(row):
363-
raise RuntimeError(
364-
f"invalid sampling mask count {count} for request {self.request_id}"
365-
)
366-
kept_ids = [int(token_id) for token_id in row[:count]]
367-
self.sampling_mask_token_ids.extend(kept_ids)
368-
self.sampling_mask_offsets.append(len(self.sampling_mask_token_ids))
369-
370342
def _new_request_output(
371343
self,
372344
external_req_id: str,
@@ -434,7 +406,14 @@ def _new_completion_output(
434406
if delta and logprobs:
435407
logprobs = logprobs[-len(token_ids) :]
436408

437-
sampling_mask = self._get_sampling_mask(token_ids, delta)
409+
sampling_mask = None
410+
if (
411+
finished
412+
and self.output_kind == RequestOutputKind.FINAL_ONLY
413+
and self.sampling_mask_chunks
414+
):
415+
merged = SamplingMaskLists.merge(self.sampling_mask_chunks)
416+
sampling_mask = SamplingMask(merged.token_ids, merged.offsets)
438417

439418
# Concatenate routed experts on finish
440419
routed_experts = None
@@ -453,27 +432,6 @@ def _new_completion_output(
453432
stop_reason=stop_reason if finished else None,
454433
)
455434

456-
def _get_sampling_mask(
457-
self, token_ids: list[int], delta: bool
458-
) -> SamplingMask | None:
459-
if len(self.sampling_mask_offsets) == 1:
460-
return None
461-
num_rows = len(self.sampling_mask_offsets) - 1
462-
if num_rows != self.detokenizer.num_output_tokens():
463-
raise RuntimeError(
464-
f"sampling mask is misaligned for request {self.request_id}: "
465-
f"{num_rows} rows for {self.detokenizer.num_output_tokens()} tokens"
466-
)
467-
start_row = num_rows - len(token_ids) if delta else 0
468-
flat_start = self.sampling_mask_offsets[start_row]
469-
offsets = [
470-
offset - flat_start for offset in self.sampling_mask_offsets[start_row:]
471-
]
472-
return SamplingMask(
473-
token_ids=self.sampling_mask_token_ids[flat_start:],
474-
offsets=offsets,
475-
)
476-
477435
def _new_pooling_output(self, pooling_output: torch.Tensor) -> PoolingOutput:
478436
return PoolingOutput(data=pooling_output)
479437

@@ -704,9 +662,13 @@ def process_outputs(
704662
if pooling_output is None:
705663
assert req_state.detokenizer is not None
706664
assert req_state.logprobs_processor is not None
707-
req_state.update_sampling_mask(
708-
new_token_ids, engine_core_output.new_sampling_mask
709-
)
665+
if (
666+
engine_core_output.new_sampling_mask is not None
667+
and req_state.output_kind == RequestOutputKind.FINAL_ONLY
668+
):
669+
req_state.sampling_mask_chunks.append(
670+
engine_core_output.new_sampling_mask
671+
)
710672
# 2) Detokenize the token ids into text and perform stop checks.
711673
stop_string = req_state.detokenizer.update(
712674
new_token_ids, finish_reason == FinishReason.STOP

vllm/v1/outputs.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,36 @@ def slice_request(self, req_idx: int, num_positions: int):
5151

5252

5353
class SamplingMaskLists(NamedTuple):
54-
# [num_generated_tokens, max_kept_tokens]
54+
# [num_kept_tokens]
5555
token_ids: np.ndarray
56-
# [num_generated_tokens]
57-
counts: np.ndarray
56+
# [num_generated_tokens + 1]
57+
offsets: np.ndarray
5858
# [num_reqs + 1]
5959
cu_num_generated_tokens: list[int] | None = None
6060

6161
def slice_request(self, req_idx: int, num_positions: int) -> "SamplingMaskLists":
6262
if self.cu_num_generated_tokens is None:
6363
start_idx = req_idx
64-
req_end_idx = req_idx + 1
6564
else:
6665
start_idx = self.cu_num_generated_tokens[req_idx]
67-
req_end_idx = self.cu_num_generated_tokens[req_idx + 1]
6866
end_idx = start_idx + num_positions
69-
if end_idx > req_end_idx:
70-
raise RuntimeError(
71-
"sampling mask has fewer rows than the generated tokens: "
72-
f"request index {req_idx}, requested {num_positions}, "
73-
f"available {req_end_idx - start_idx}"
74-
)
67+
flat_start = self.offsets[start_idx]
68+
flat_end = self.offsets[end_idx]
7569
return SamplingMaskLists(
76-
self.token_ids[start_idx:end_idx],
77-
self.counts[start_idx:end_idx],
70+
self.token_ids[flat_start:flat_end],
71+
self.offsets[start_idx : end_idx + 1] - flat_start,
7872
None,
7973
)
8074

75+
@staticmethod
76+
def merge(chunks: Sequence["SamplingMaskLists"]) -> "SamplingMaskLists":
77+
token_ids = np.concatenate([chunk.token_ids for chunk in chunks])
78+
counts = np.concatenate([np.diff(chunk.offsets) for chunk in chunks])
79+
offsets = np.empty(len(counts) + 1, dtype=np.int64)
80+
offsets[0] = 0
81+
np.cumsum(counts, dtype=np.int64, out=offsets[1:])
82+
return SamplingMaskLists(token_ids, offsets)
83+
8184

8285
class LogprobsTensors(NamedTuple):
8386
# [num_reqs x num_generated_tokens, max_num_logprobs + 1]

0 commit comments

Comments
 (0)