support regex in xgrammar backend (#2983)

This commit is contained in:
Enrique Shockwave
2025-01-19 20:34:41 +00:00
committed by GitHub
parent 2c05f81f15
commit 3bcf5ecea7
7 changed files with 200 additions and 8 deletions

View File

@@ -19,6 +19,7 @@ from typing import List, Tuple
import torch
from xgrammar import (
CompiledGrammar,
Grammar,
GrammarCompiler,
GrammarMatcher,
TokenizerInfo,
@@ -133,10 +134,13 @@ class XGrammarGrammarBackend(BaseGrammarBackend):
logging.warning(f"Skip invalid ebnf: ebnf={key_string}, {e=}")
return None
elif key_type == "regex":
logger.warning(
"regex hasn't been supported by xgrammar yet. This is skipped."
)
return None
try:
ctx = self.grammar_compiler.compile_grammar(
Grammar.from_regex(key_string)
)
except RuntimeError as e:
logging.warning(f"Skip invalid regex: regex={key_string}, {e=}")
return None
else:
raise ValueError(f"Invalid key_type: {key_type}")