Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Use .clone() for sampling params and deepcopy XGrammarLogitsProcessor #11380

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions vllm/model_executor/guided_decoding/xgrammar_decoding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# noqa: UP007
from __future__ import annotations

import copy
import json
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any
Expand Down Expand Up @@ -238,7 +239,6 @@ class XGrammarLogitsProcessor:
token_bitmask: torch.Tensor = None # type: ignore[assignment]
matchers: list[xgr.GrammarMatcher] = field(default_factory=list)
batch_size: int = field(default=1)
prefilled: bool = field(default=False)

def __getstate__(self) -> dict[str, Any]:
return {'config': self.config}
Expand All @@ -250,7 +250,6 @@ def __setstate__(self, state: dict[str, Any]):
self.matchers = []
self.batch_size = 1
self.token_bitmask = None # type: ignore[assignment]
self.prefilled = False

def _ensure_ctx(self):
"""Lazily initialize the processor in the worker process"""
Expand Down Expand Up @@ -278,10 +277,7 @@ def __call__(self, input_ids: list[int],
self.token_bitmask = xgr.allocate_token_bitmask(
self.batch_size, self.config.vocab_size)

if not self.prefilled:
# Have not sampled a token yet
self.prefilled = True
else:
if len(input_ids) > 0:
for i, matcher in enumerate(self.matchers):
if not matcher.is_terminated():
sampled_token = input_ids[-1]
Expand Down Expand Up @@ -309,3 +305,7 @@ def __call__(self, input_ids: list[int],
scores = scores.to(device_type).squeeze()

return scores

def clone(self) -> XGrammarLogitsProcessor:
"""Deepcopy due to per-sequence state in the matchers"""
return copy.deepcopy(self)
9 changes: 5 additions & 4 deletions vllm/sampling_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,15 +450,16 @@ def all_stop_token_ids(self) -> Set[int]:
return self._all_stop_token_ids

def clone(self) -> "SamplingParams":
"""Deep copy excluding LogitsProcessor objects.
"""Deep copy, but maybe not the LogitsProcessor objects.

LogitsProcessor objects are excluded because they may contain an
arbitrary, nontrivial amount of data.
LogitsProcessor objects may contain an arbitrary, nontrivial amount of
data that is expensive to copy. However, if not copied, the processor
needs to support parallel decoding for multiple sequences
See https://github.com/vllm-project/vllm/issues/3087
"""

logit_processor_refs = None if self.logits_processors is None else {
id(lp): lp
id(lp): lp.clone() if hasattr(lp, 'clone') else lp
for lp in self.logits_processors
}
return copy.deepcopy(self, memo=logit_processor_refs)
Expand Down
4 changes: 2 additions & 2 deletions vllm/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,9 +1366,9 @@
class ParallelSampleSequenceGroup(SequenceGroupBase):

@staticmethod
def add_request(request_id: str, engine, params, **kwargs):
def add_request(request_id: str, engine, params: Union[SamplingParams, PoolingParams], **kwargs):

Check failure on line 1369 in vllm/sequence.py

View workflow job for this annotation

GitHub Actions / ruff (3.12)

Ruff (E501)

vllm/sequence.py:1369:81: E501 Line too long (101 > 80)
original_params = params
params = copy.deepcopy(original_params)
params = original_params.clone()
params.n = 1
group = ParallelSampleSequenceGroup(request_id)
seqs = []
Expand Down
Loading