[Bugfix][Structured Output] Stop XGrammar token batches at termination (#52805)
Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
@@ -157,11 +157,12 @@ class XgrammarGrammar(StructuredOutputGrammar):
|
||||
def accept_tokens(self, request_id: str, tokens: list[int]) -> bool:
|
||||
"""Accepts a list of tokens and advances the FSM.
|
||||
|
||||
Returns True if the FSM was advanced successfully.
|
||||
Returns False if the FSM failed to advance.
|
||||
Returns True if all grammar-constrained tokens were accepted.
|
||||
Tokens after termination are ignored. Returns False if the FSM
|
||||
failed to advance.
|
||||
"""
|
||||
if self._is_terminated:
|
||||
return False
|
||||
return True
|
||||
for token in tokens:
|
||||
if not self.matcher.accept_token(token):
|
||||
logger.error(
|
||||
@@ -172,7 +173,9 @@ class XgrammarGrammar(StructuredOutputGrammar):
|
||||
)
|
||||
return False
|
||||
self.num_processed_tokens += 1
|
||||
self._is_terminated = self.matcher.is_terminated()
|
||||
self._is_terminated = self.matcher.is_terminated()
|
||||
if self._is_terminated:
|
||||
break
|
||||
return True
|
||||
|
||||
def validate_tokens(self, tokens: list[int]) -> list[int]:
|
||||
@@ -181,10 +184,15 @@ class XgrammarGrammar(StructuredOutputGrammar):
|
||||
|
||||
Returns the prefix list of tokens that are accepted by the FSM.
|
||||
"""
|
||||
if self._is_terminated:
|
||||
return []
|
||||
|
||||
accepted_tokens = []
|
||||
for token in tokens:
|
||||
if self.matcher.accept_token(token):
|
||||
accepted_tokens.append(token)
|
||||
if self.matcher.is_terminated():
|
||||
break
|
||||
else:
|
||||
break
|
||||
if len(accepted_tokens) > 0:
|
||||
@@ -204,8 +212,9 @@ class XgrammarGrammar(StructuredOutputGrammar):
|
||||
return self._is_terminated
|
||||
|
||||
def reset(self):
|
||||
self.num_processed_tokens = 0
|
||||
self.matcher.reset()
|
||||
self.num_processed_tokens = 0
|
||||
self._is_terminated = False
|
||||
|
||||
|
||||
# cf https://github.com/mlc-ai/xgrammar/blob/a32ac892676d2eedc0327416105b9b06edfb94b2/cpp/json_schema_converter.cc
|
||||
|
||||
Reference in New Issue
Block a user