Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
change
sglang
Commits
c722d9bd
"vscode:/vscode.git/clone" did not exist on "ec217d6f403b0dee823e4e0ee356f2b6e1b9ed9a"
Unverified
Commit
c722d9bd
authored
Nov 13, 2024
by
Lianmin Zheng
Committed by
GitHub
Nov 13, 2024
Browse files
Fix dependency and error message for xgrammar (#2024)
parent
218ab361
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
23 deletions
+38
-23
python/sglang/srt/constrained/base_grammar_backend.py
python/sglang/srt/constrained/base_grammar_backend.py
+1
-1
python/sglang/srt/constrained/outlines_backend.py
python/sglang/srt/constrained/outlines_backend.py
+14
-20
python/sglang/srt/constrained/xgrammar_backend.py
python/sglang/srt/constrained/xgrammar_backend.py
+23
-2
No files found.
python/sglang/srt/constrained/base_grammar_backend.py
View file @
c722d9bd
...
...
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
"""
"""The baseclass of backend
s
for grammar-guided constrained decoding."""
"""The baseclass of
a
backend for grammar-guided constrained decoding."""
from
concurrent.futures
import
Future
,
ThreadPoolExecutor
from
dataclasses
import
dataclass
...
...
python/sglang/srt/constrained/outlines_backend.py
View file @
c722d9bd
...
...
@@ -22,7 +22,9 @@ from typing import Dict, List, Optional, Tuple, Union
import
interegular
import
torch
from
outlines.fsm.guide
import
RegexGuide
from
outlines.fsm.json_schema
import
build_regex_from_schema
from
outlines.models.transformers
import
TransformerTokenizer
from
pydantic
import
BaseModel
from
sglang.srt.constrained.base_grammar_backend
import
(
BaseGrammarBackend
,
...
...
@@ -33,26 +35,6 @@ from sglang.srt.constrained.outlines_jump_forward import OutlinesJumpForwardMap
logger
=
logging
.
getLogger
(
__name__
)
try
:
from
outlines.fsm.json_schema
import
build_regex_from_object
except
ImportError
:
# Since outlines 0.0.32, build_regex_from_object is replaced by build_regex_from_schema,
# which only accepts string schema as input.
from
outlines.fsm.json_schema
import
build_regex_from_schema
from
pydantic
import
BaseModel
def
build_regex_from_object
(
object
:
Union
[
str
,
BaseModel
,
Dict
],
whitespace_pattern
:
Optional
[
str
]
=
None
):
if
isinstance
(
object
,
type
(
BaseModel
)):
schema
=
json
.
dumps
(
object
.
model_json_schema
())
elif
isinstance
(
object
,
Dict
):
schema
=
json
.
dumps
(
object
)
else
:
schema
=
object
return
build_regex_from_schema
(
schema
,
whitespace_pattern
)
class
OutlinesGrammar
(
BaseGrammarObject
):
def
__init__
(
self
,
...
...
@@ -169,3 +151,15 @@ class OutlinesGrammarBackend(BaseGrammarBackend):
else
:
jump_forward_map
=
None
return
OutlinesGrammar
(
guide
,
jump_forward_map
)
def
build_regex_from_object
(
object
:
Union
[
str
,
BaseModel
,
Dict
],
whitespace_pattern
:
Optional
[
str
]
=
None
):
if
isinstance
(
object
,
type
(
BaseModel
)):
schema
=
json
.
dumps
(
object
.
model_json_schema
())
elif
isinstance
(
object
,
Dict
):
schema
=
json
.
dumps
(
object
)
else
:
schema
=
object
return
build_regex_from_schema
(
schema
,
whitespace_pattern
)
python/sglang/srt/constrained/xgrammar_backend.py
View file @
c722d9bd
...
...
@@ -19,7 +19,16 @@ import logging
from
typing
import
List
,
Tuple
import
torch
from
xgrammar
import
CachedGrammarCompiler
,
CompiledGrammar
,
GrammarMatcher
try
:
from
xgrammar
import
CachedGrammarCompiler
,
CompiledGrammar
,
GrammarMatcher
import_error
=
None
except
ImportError
as
e
:
CachedGrammarCompiler
=
CompiledGrammar
=
GrammarMatcher
=
TokenizerInfo
=
(
ImportError
)
import_error
=
e
from
sglang.srt.constrained.base_grammar_backend
import
(
BaseGrammarBackend
,
...
...
@@ -95,10 +104,21 @@ class XGrammarGrammarBackend(BaseGrammarBackend):
vocab_size
:
int
,
):
super
().
__init__
()
if
import_error
:
logger
.
warning
(
f
"Ignore import error for the grammar backend:
{
import_error
}
"
)
self
.
grammar_cache
=
None
return
self
.
grammar_cache
=
CachedGrammarCompiler
(
tokenizer_or_vocab
=
tokenizer
)
self
.
vocab_size
=
vocab_size
def
init_value_impl
(
self
,
key
:
Tuple
[
str
,
str
])
->
XGrammarGrammar
:
if
import_error
:
raise
import_error
key_type
,
key_string
=
key
if
key_type
==
"json"
:
try
:
...
...
@@ -126,4 +146,5 @@ class XGrammarGrammarBackend(BaseGrammarBackend):
return
XGrammarGrammar
(
matcher
,
self
.
vocab_size
,
ctx
)
def
reset
(
self
):
self
.
grammar_cache
.
clear
()
if
self
.
grammar_cache
:
self
.
grammar_cache
.
clear
()
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