[Bugfix][Structured Output] Mask request stop tokens in xgrammar until grammar terminates (#49227)
Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
@@ -184,7 +184,14 @@ class StructuredOutputManager:
|
||||
try:
|
||||
request_type, grammar_spec = struct_request.structured_output_key
|
||||
assert self.backend is not None
|
||||
return self.backend.compile_grammar(request_type, grammar_spec)
|
||||
stop_token_ids = (
|
||||
request.sampling_params.all_stop_token_ids
|
||||
if request.sampling_params is not None
|
||||
else None
|
||||
)
|
||||
return self.backend.compile_grammar(
|
||||
request_type, grammar_spec, stop_token_ids=stop_token_ids
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to compile grammar for request %s", request.request_id
|
||||
|
||||
@@ -106,7 +106,10 @@ class GuidanceBackend(StructuredOutputBackend):
|
||||
)
|
||||
|
||||
def compile_grammar(
|
||||
self, request_type: StructuredOutputOptions, grammar_spec: str
|
||||
self,
|
||||
request_type: StructuredOutputOptions,
|
||||
grammar_spec: str,
|
||||
stop_token_ids: set[int] | None = None,
|
||||
) -> StructuredOutputGrammar:
|
||||
self.serialized_grammar = serialize_guidance_grammar(
|
||||
request_type,
|
||||
|
||||
@@ -99,7 +99,10 @@ class LMFormatEnforcerBackend(StructuredOutputBackend):
|
||||
)
|
||||
|
||||
def compile_grammar(
|
||||
self, request_type: StructuredOutputOptions, grammar_spec: str
|
||||
self,
|
||||
request_type: StructuredOutputOptions,
|
||||
grammar_spec: str,
|
||||
stop_token_ids: set[int] | None = None,
|
||||
) -> StructuredOutputGrammar:
|
||||
character_level_parser: lmformatenforcer.CharacterLevelParser
|
||||
if request_type == StructuredOutputOptions.JSON:
|
||||
|
||||
@@ -71,7 +71,10 @@ class OutlinesBackend(StructuredOutputBackend):
|
||||
return index
|
||||
|
||||
def compile_grammar(
|
||||
self, request_type: StructuredOutputOptions, grammar_spec: str
|
||||
self,
|
||||
request_type: StructuredOutputOptions,
|
||||
grammar_spec: str,
|
||||
stop_token_ids: set[int] | None = None,
|
||||
) -> StructuredOutputGrammar:
|
||||
if request_type == StructuredOutputOptions.JSON:
|
||||
regex = json_schema.build_regex_from_schema(grammar_spec)
|
||||
|
||||
@@ -105,7 +105,10 @@ class StructuredOutputBackend(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def compile_grammar(
|
||||
self, request_type: StructuredOutputOptions, grammar_spec: str
|
||||
self,
|
||||
request_type: StructuredOutputOptions,
|
||||
grammar_spec: str,
|
||||
stop_token_ids: set[int] | None = None,
|
||||
) -> StructuredOutputGrammar:
|
||||
"""
|
||||
Compiles a grammar specification into a structured output grammar.
|
||||
@@ -114,6 +117,9 @@ class StructuredOutputBackend(ABC):
|
||||
request_type (StructuredOutputOptions): The type of structured
|
||||
output request.
|
||||
grammar_spec (str): The grammar specification to compile.
|
||||
stop_token_ids (set[int] | None): The request's EOS and user
|
||||
stop-token ids (``SamplingParams.all_stop_token_ids``), masked
|
||||
until the grammar terminates.
|
||||
|
||||
Returns:
|
||||
StructuredOutputGrammar: The compiled structured output grammar.
|
||||
|
||||
@@ -76,7 +76,10 @@ class XgrammarBackend(StructuredOutputBackend):
|
||||
)
|
||||
|
||||
def compile_grammar(
|
||||
self, request_type: StructuredOutputOptions, grammar_spec: str
|
||||
self,
|
||||
request_type: StructuredOutputOptions,
|
||||
grammar_spec: str,
|
||||
stop_token_ids: set[int] | None = None,
|
||||
) -> StructuredOutputGrammar:
|
||||
if request_type == StructuredOutputOptions.JSON:
|
||||
ctx = self.compiler.compile_json_schema(
|
||||
@@ -119,6 +122,7 @@ class XgrammarBackend(StructuredOutputBackend):
|
||||
return XgrammarGrammar(
|
||||
matcher=xgr.GrammarMatcher(
|
||||
ctx,
|
||||
override_stop_tokens=list(stop_token_ids) if stop_token_ids else None,
|
||||
max_rollback_tokens=self.num_speculative_tokens,
|
||||
),
|
||||
vocab_size=self.vocab_size,
|
||||
|
||||
Reference in New Issue
Block a user