bugfix: Fix XGrammar backend to use model's EOS tokens for constrained generation (#8422)

This commit is contained in:
Chang Su
2025-07-28 10:01:02 +08:00
committed by GitHub
parent bb81daefb8
commit dd487e5553
3 changed files with 21 additions and 8 deletions
@@ -150,14 +150,16 @@ class XGrammarGrammarBackend(BaseGrammarBackend):
self,
tokenizer,
vocab_size: int,
model_eos_token_ids: Optional[List[int]] = None,
):
super().__init__()
if True:
tokenizer_info = TokenizerInfo.from_huggingface(
tokenizer, vocab_size=vocab_size
)
override_stop_tokens = None
# Create TokenizerInfo with model's EOS tokens as the authoritative stop tokens
# This ensures consistency between what the model considers EOS and what XGrammar uses
tokenizer_info = TokenizerInfo.from_huggingface(
tokenizer, vocab_size=vocab_size, stop_token_ids=model_eos_token_ids
)
override_stop_tokens = None
self.grammar_compiler = GrammarCompiler(tokenizer_info=tokenizer_info)
self.vocab_size = vocab_size