Skip to content

Commit

Permalink
Fix OpenAI parallel sampling when using xgrammar
Browse files Browse the repository at this point in the history
Signed-off-by: mgoin <[email protected]>
  • Loading branch information
mgoin committed Dec 30, 2024
1 parent 5886aa4 commit 0b3e057
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
19 changes: 10 additions & 9 deletions tests/entrypoints/openai/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
# need to change to match the prompt adapter
PA_NUM_VIRTUAL_TOKENS = 8

GUIDED_DECODING_BACKENDS = ["outlines", "lm-format-enforcer", "xgrammar"]


@pytest.fixture(scope="module")
def zephyr_lora_files():
Expand Down Expand Up @@ -635,8 +637,7 @@ async def test_allowed_token_ids(client: openai.AsyncOpenAI):


@pytest.mark.asyncio
@pytest.mark.parametrize("guided_decoding_backend",
["outlines", "lm-format-enforcer"])
@pytest.mark.parametrize("guided_decoding_backend", GUIDED_DECODING_BACKENDS)
async def test_guided_json_completion(client: openai.AsyncOpenAI,
guided_decoding_backend: str,
sample_json_schema):
Expand All @@ -658,8 +659,7 @@ async def test_guided_json_completion(client: openai.AsyncOpenAI,


@pytest.mark.asyncio
@pytest.mark.parametrize("guided_decoding_backend",
["outlines", "lm-format-enforcer"])
@pytest.mark.parametrize("guided_decoding_backend", GUIDED_DECODING_BACKENDS)
async def test_guided_regex_completion(client: openai.AsyncOpenAI,
guided_decoding_backend: str,
sample_regex):
Expand All @@ -680,8 +680,7 @@ async def test_guided_regex_completion(client: openai.AsyncOpenAI,


@pytest.mark.asyncio
@pytest.mark.parametrize("guided_decoding_backend",
["outlines", "lm-format-enforcer"])
@pytest.mark.parametrize("guided_decoding_backend", GUIDED_DECODING_BACKENDS)
async def test_guided_choice_completion(client: openai.AsyncOpenAI,
guided_decoding_backend: str,
sample_guided_choice):
Expand All @@ -701,7 +700,9 @@ async def test_guided_choice_completion(client: openai.AsyncOpenAI,


@pytest.mark.asyncio
@pytest.mark.parametrize("guided_decoding_backend", GUIDED_DECODING_BACKENDS)
async def test_guided_grammar(client: openai.AsyncOpenAI,
guided_decoding_backend: str,
sample_sql_statements):

completion = await client.completions.create(
Expand All @@ -710,7 +711,8 @@ async def test_guided_grammar(client: openai.AsyncOpenAI,
"table_1 where it is equals to 1"),
temperature=1.0,
max_tokens=500,
extra_body=dict(guided_grammar=sample_sql_statements))
extra_body=dict(guided_grammar=sample_sql_statements,
guided_decoding_backend=guided_decoding_backend))

content = completion.choices[0].text

Expand Down Expand Up @@ -761,8 +763,7 @@ async def test_echo_logprob_completion(client: openai.AsyncOpenAI,


@pytest.mark.asyncio
@pytest.mark.parametrize("guided_decoding_backend",
["outlines", "lm-format-enforcer"])
@pytest.mark.parametrize("guided_decoding_backend", GUIDED_DECODING_BACKENDS)
async def test_guided_decoding_type_error(client: openai.AsyncOpenAI,
guided_decoding_backend: str,
sample_json_schema, sample_regex):
Expand Down
5 changes: 5 additions & 0 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 @@ -309,3 +310,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
2 changes: 1 addition & 1 deletion vllm/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ class ParallelSampleSequenceGroup(SequenceGroupBase):
@staticmethod
def add_request(request_id: str, engine, params, **kwargs):
original_params = params
params = copy.deepcopy(original_params)
params = original_params.clone()
params.n = 1
group = ParallelSampleSequenceGroup(request_id)
seqs = []
Expand Down

0 comments on commit 0b3e057

Please sign in to comment.