Skip to content

Commit 4a974c2

Browse files
Fix Unit-test in PR: Add xgrammar (#750)
1 parent f6f7a41 commit 4a974c2

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

lightllm/server/core/objs/sampling_params.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def initialize(self, constraint: str, tokenizer):
114114
ctypes.memmove(self.constraint, constraint_bytes, len(constraint_bytes))
115115
self.length = len(constraint_bytes)
116116
try:
117-
if self.length > 0:
117+
if self.length > 0 and tokenizer is not None:
118118
import xgrammar as xgr
119119

120120
tokenizer_info = xgr.TokenizerInfo.from_huggingface(tokenizer)
@@ -144,7 +144,7 @@ def initialize(self, constraint: str, tokenizer):
144144
ctypes.memmove(self.constraint, constraint_bytes, len(constraint_bytes))
145145
self.length = len(constraint_bytes)
146146
try:
147-
if self.length > 0:
147+
if self.length > 0 and tokenizer is not None:
148148
import xgrammar as xgr
149149

150150
tokenizer_info = xgr.TokenizerInfo.from_huggingface(tokenizer)

unit_tests/server/core/objs/test_sampling_params.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
STOP_SEQUENCE_MAX_LENGTH,
1313
REGULAR_CONSTRAINT_MAX_LENGTH,
1414
ALLOWED_TOKEN_IDS_MAX_LENGTH,
15+
JSON_SCHEMA_MAX_LENGTH,
16+
GRAMMAR_CONSTRAINT_MAX_LENGTH,
1517
)
1618

1719
grammar_str = r"""root ::= (expr "=" term)+
@@ -80,20 +82,20 @@ def test_regular_constraint_initialization():
8082

8183
def test_guided_grammar_initialization():
8284
grammar = GuidedGrammar()
83-
grammar.initialize(grammar_str)
85+
grammar.initialize(grammar_str, None)
8486
assert grammar.to_str() == grammar_str
8587

8688
with pytest.raises(AssertionError):
87-
grammar.initialize("a" * (REGULAR_CONSTRAINT_MAX_LENGTH + 1))
89+
grammar.initialize("a" * (GRAMMAR_CONSTRAINT_MAX_LENGTH + 1), None)
8890

8991

9092
def test_guided_json_schema_initialization():
9193
schema = GuidedJsonSchema()
92-
schema.initialize(schema_str)
94+
schema.initialize(schema_str, None)
9395
assert schema.to_str() == schema_str
9496

9597
with pytest.raises(AssertionError):
96-
schema.initialize("a" * (REGULAR_CONSTRAINT_MAX_LENGTH + 1))
98+
schema.initialize("a" * (JSON_SCHEMA_MAX_LENGTH + 1), None)
9799

98100

99101
def test_allowed_token_ids_initialization():

0 commit comments

Comments
 (0)