Skip to content

Commit

Permalink
[Misc] Validate grammar and fail early (vllm-project#11119)
Browse files Browse the repository at this point in the history
  • Loading branch information
comaniac authored Dec 12, 2024
1 parent 5d71257 commit 2c97eca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
32 changes: 22 additions & 10 deletions vllm/model_executor/guided_decoding/xgrammar_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,25 @@ def from_guided_params(cls,
max_threads: int = 8) -> GrammarConfig:

tokenizer_hash = hash(tokenizer)
# Only get tokenizer data if not already cached
if tokenizer_hash in TokenizerDataCache._cache:
encoded_vocab = None
stop_token_ids = None
backend_str = None
else:
tokenizer_data = TokenizerDataCache.get_tokenizer_data(tokenizer)
encoded_vocab = tokenizer_data.encoded_vocab
stop_token_ids = tokenizer_data.stop_token_ids
backend_str = tokenizer_data.backend_str
tokenizer_data = TokenizerDataCache.get_tokenizer_data(tokenizer)
encoded_vocab = tokenizer_data.encoded_vocab
stop_token_ids = tokenizer_data.stop_token_ids
backend_str = tokenizer_data.backend_str

if guided_params.json:
if not isinstance(guided_params.json, str):
json_str = json.dumps(guided_params.json)
else:
json_str = guided_params.json

# Validate the schema and raise ValueError here if it is invalid.
# This is to avoid exceptions in model execution, which will crash
# the engine worker process.
try:
xgr.Grammar.from_json_schema(json_str)
except RuntimeError as err:
raise ValueError(str(err)) from err

return cls(json_str=json_str,
vocab_size=model_config.hf_text_config.vocab_size,
encoded_vocab=encoded_vocab,
Expand All @@ -167,6 +170,15 @@ def from_guided_params(cls,
f"Conversion error: {str(e)}") from e
else:
grammar_str = guided_params.grammar

# Validate the grammar and raise ValueError here if it is invalid.
# This is to avoid exceptions in model execution, which will crash
# the engine worker process.
try:
xgr.Grammar.from_ebnf(grammar_str)
except RuntimeError as err:
raise ValueError(str(err)) from err

return cls(grammar_str=grammar_str,
vocab_size=model_config.hf_text_config.vocab_size,
encoded_vocab=encoded_vocab,
Expand Down
12 changes: 4 additions & 8 deletions vllm/model_executor/guided_decoding/xgrammar_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ def grammar_is_likely_lark(grammar_str: str) -> bool:
if not line:
continue

# Look for Lark-style rule definitions
if ':' in line and '::=' not in line:
return True
# Look for GBNF rule definition
if '::=' in line:
return False

# Look for Lark-specific features
if any(pattern in line for pattern in ['?start:', '|', '~']):
return True

return False
return True


def convert_lark_to_gbnf(grammar_str: str) -> str:
Expand Down

0 comments on commit 2c97eca

Please sign in to comment.