[Bugfix] Mask special added tokens in xgrammar JSON, regex and EBNF grammars (#60749)
Signed-off-by: Vadim Gimpelson <vadim.gimpelson@gmail.com> Signed-off-by: Artem Perevedentsev <aperevedents@nvidia.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Artem Perevedentsev <aperevedents@nvidia.com>
This commit is contained in:
co-authored by
Claude
Artem Perevedentsev
parent
cd7517fb17
commit
818f70c295
@@ -26,6 +26,8 @@ from vllm.v1.structured_output.utils import (
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import xgrammar as xgr
|
||||
|
||||
from vllm.tokenizers import TokenizerLike
|
||||
else:
|
||||
xgr = LazyLoader("xgr", globals(), "xgrammar")
|
||||
|
||||
@@ -43,6 +45,7 @@ class XgrammarBackend(StructuredOutputBackend):
|
||||
model_config is not None and model_config.hf_config.model_type == "plamo3"
|
||||
)
|
||||
|
||||
full_vocab_tokenizer_info = None
|
||||
if is_mistral_tokenizer(self.tokenizer):
|
||||
# NOTE: ideally, xgrammar should handle this accordingly.
|
||||
# refer to https://github.com/mlc-ai/xgrammar/blob/d77c0a0173ef14779c918e3be7966ba852f7910f/python/xgrammar/tokenizer_info.py#L98
|
||||
@@ -66,15 +69,46 @@ class XgrammarBackend(StructuredOutputBackend):
|
||||
):
|
||||
tokenizer_info, _ = init_xgrammar()
|
||||
else:
|
||||
tokenizer_info = xgr.TokenizerInfo.from_huggingface(
|
||||
# tokenizer for structural tags
|
||||
# accepts some of the special tokens
|
||||
full_vocab_tokenizer_info = xgr.TokenizerInfo.from_huggingface(
|
||||
self.tokenizer,
|
||||
vocab_size=self.vocab_size,
|
||||
)
|
||||
self.compiler = xgr.GrammarCompiler(
|
||||
tokenizer_info,
|
||||
max_threads=8,
|
||||
cache_enabled=True,
|
||||
cache_limit_bytes=vllm.envs.VLLM_XGRAMMAR_CACHE_MB * 1024 * 1024,
|
||||
# tokenizer for json
|
||||
# does NOT accept any special tokens
|
||||
tokenizer_info = self._filter_special_tokens(
|
||||
self.tokenizer, full_vocab_tokenizer_info
|
||||
)
|
||||
|
||||
# FIXME(arpera):
|
||||
# Here we create not one but TWO compilers
|
||||
# because xgrammar currently does not properly handle
|
||||
# special tokens.
|
||||
# Proper special tokens handling depends on structured output
|
||||
# request type: json, structural_tag, etc.
|
||||
# Structural tag may accept some of the special tokens
|
||||
# At the same time json must NOT accept any special tokens
|
||||
# Currently xgrammar does not properly handle all the cases
|
||||
# So, we have to implement additional logic on our side
|
||||
# We maintain TWO compilers for different structural output types
|
||||
# If in future xgrammar will fix this problem,
|
||||
# then we can consider to remove this
|
||||
# Also, note that other backends like guidance
|
||||
# do NOT have this problem (as far as I see, I may be wrong).
|
||||
# So, if someone wants to go and fix the problem on xgrammar side
|
||||
# you should first definitely check how other backends deal with it
|
||||
# Some work on xgrammar side has been started:
|
||||
# https://github.com/mlc-ai/xgrammar/issues/832
|
||||
|
||||
# Compiler for json. Must NOT accept any special tokens.
|
||||
self.compiler = self._create_compiler(tokenizer_info)
|
||||
|
||||
# Compiler for structural tag. May accept some of the special tokens.
|
||||
self.full_vocab_compiler = (
|
||||
self.compiler
|
||||
if full_vocab_tokenizer_info is None
|
||||
else self._create_compiler(full_vocab_tokenizer_info)
|
||||
)
|
||||
|
||||
self.num_speculative_tokens = 0
|
||||
@@ -122,7 +156,7 @@ class XgrammarBackend(StructuredOutputBackend):
|
||||
)
|
||||
elif request_type == StructuredOutputOptions.GRAMMAR:
|
||||
if grammar_is_likely_lark(grammar_spec):
|
||||
ctx = self.compiler.compile_lark(grammar_spec)
|
||||
ctx = self.full_vocab_compiler.compile_lark(grammar_spec)
|
||||
else:
|
||||
ctx = self.compiler.compile_grammar(grammar_spec)
|
||||
elif request_type == StructuredOutputOptions.REGEX:
|
||||
@@ -142,9 +176,11 @@ class XgrammarBackend(StructuredOutputBackend):
|
||||
)
|
||||
for s in s_tag["structures"]
|
||||
]
|
||||
ctx = self.compiler.compile_structural_tag(tags, s_tag["triggers"])
|
||||
ctx = self.full_vocab_compiler.compile_structural_tag(
|
||||
tags, s_tag["triggers"]
|
||||
)
|
||||
else:
|
||||
ctx = self.compiler.compile_structural_tag(grammar_spec)
|
||||
ctx = self.full_vocab_compiler.compile_structural_tag(grammar_spec)
|
||||
else:
|
||||
logger.error(
|
||||
"Validation should have already occurred. Please file an issue."
|
||||
@@ -168,6 +204,38 @@ class XgrammarBackend(StructuredOutputBackend):
|
||||
|
||||
def destroy(self):
|
||||
del self.compiler
|
||||
del self.full_vocab_compiler
|
||||
|
||||
@staticmethod
|
||||
def _create_compiler(
|
||||
tokenizer_info: "xgr.TokenizerInfo",
|
||||
) -> "xgr.GrammarCompiler":
|
||||
return xgr.GrammarCompiler(
|
||||
tokenizer_info,
|
||||
max_threads=8,
|
||||
cache_enabled=True,
|
||||
cache_limit_bytes=vllm.envs.VLLM_XGRAMMAR_CACHE_MB * 1024 * 1024,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _filter_special_tokens(
|
||||
tokenizer: "TokenizerLike", tokenizer_info: "xgr.TokenizerInfo"
|
||||
) -> "xgr.TokenizerInfo":
|
||||
"""Filter ALL special tokens from tokenizer_info"""
|
||||
special_token_ids = {
|
||||
token_id
|
||||
for token_id, token in getattr(
|
||||
tokenizer, "added_tokens_decoder", {}
|
||||
).items()
|
||||
if token.special
|
||||
}
|
||||
encoded_vocab = [""] * tokenizer_info.vocab_size
|
||||
for token, token_id in tokenizer.get_vocab().items():
|
||||
if token_id < len(encoded_vocab) and token_id not in special_token_ids:
|
||||
encoded_vocab[token_id] = token
|
||||
return xgr.TokenizerInfo.from_vocab_and_metadata(
|
||||
encoded_vocab, tokenizer_info.dump_metadata()
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
Reference in New Issue
Block a user