Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
vllm_cscc
Commits
2c97eca1
Unverified
Commit
2c97eca1
authored
Dec 12, 2024
by
Cody Yu
Committed by
GitHub
Dec 12, 2024
Browse files
[Misc] Validate grammar and fail early (#11119)
parent
5d712571
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
18 deletions
+26
-18
vllm/model_executor/guided_decoding/xgrammar_decoding.py
vllm/model_executor/guided_decoding/xgrammar_decoding.py
+22
-10
vllm/model_executor/guided_decoding/xgrammar_utils.py
vllm/model_executor/guided_decoding/xgrammar_utils.py
+4
-8
No files found.
vllm/model_executor/guided_decoding/xgrammar_decoding.py
View file @
2c97eca1
...
@@ -131,22 +131,25 @@ class GrammarConfig:
...
@@ -131,22 +131,25 @@ class GrammarConfig:
max_threads
:
int
=
8
)
->
GrammarConfig
:
max_threads
:
int
=
8
)
->
GrammarConfig
:
tokenizer_hash
=
hash
(
tokenizer
)
tokenizer_hash
=
hash
(
tokenizer
)
# Only get tokenizer data if not already cached
tokenizer_data
=
TokenizerDataCache
.
get_tokenizer_data
(
tokenizer
)
if
tokenizer_hash
in
TokenizerDataCache
.
_cache
:
encoded_vocab
=
tokenizer_data
.
encoded_vocab
encoded_vocab
=
None
stop_token_ids
=
tokenizer_data
.
stop_token_ids
stop_token_ids
=
None
backend_str
=
tokenizer_data
.
backend_str
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
if
guided_params
.
json
:
if
guided_params
.
json
:
if
not
isinstance
(
guided_params
.
json
,
str
):
if
not
isinstance
(
guided_params
.
json
,
str
):
json_str
=
json
.
dumps
(
guided_params
.
json
)
json_str
=
json
.
dumps
(
guided_params
.
json
)
else
:
else
:
json_str
=
guided_params
.
json
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
,
return
cls
(
json_str
=
json_str
,
vocab_size
=
model_config
.
hf_text_config
.
vocab_size
,
vocab_size
=
model_config
.
hf_text_config
.
vocab_size
,
encoded_vocab
=
encoded_vocab
,
encoded_vocab
=
encoded_vocab
,
...
@@ -167,6 +170,15 @@ class GrammarConfig:
...
@@ -167,6 +170,15 @@ class GrammarConfig:
f
"Conversion error:
{
str
(
e
)
}
"
)
from
e
f
"Conversion error:
{
str
(
e
)
}
"
)
from
e
else
:
else
:
grammar_str
=
guided_params
.
grammar
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
,
return
cls
(
grammar_str
=
grammar_str
,
vocab_size
=
model_config
.
hf_text_config
.
vocab_size
,
vocab_size
=
model_config
.
hf_text_config
.
vocab_size
,
encoded_vocab
=
encoded_vocab
,
encoded_vocab
=
encoded_vocab
,
...
...
vllm/model_executor/guided_decoding/xgrammar_utils.py
View file @
2c97eca1
...
@@ -26,15 +26,11 @@ def grammar_is_likely_lark(grammar_str: str) -> bool:
...
@@ -26,15 +26,11 @@ def grammar_is_likely_lark(grammar_str: str) -> bool:
if
not
line
:
if
not
line
:
continue
continue
# Look for
Lark-style
rule definition
s
# Look for
GBNF
rule definition
if
':
'
in
line
and
'::='
not
in
line
:
if
':
:='
in
line
:
return
Tru
e
return
Fals
e
# Look for Lark-specific features
return
True
if
any
(
pattern
in
line
for
pattern
in
[
'?start:'
,
'|'
,
'~'
]):
return
True
return
False
def
convert_lark_to_gbnf
(
grammar_str
:
str
)
->
str
:
def
convert_lark_to_gbnf
(
grammar_str
:
str
)
->
str
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment