Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Unit-test in PR: Add xgrammar #750

Merged
merged 17 commits into from
Feb 27, 2025
4 changes: 2 additions & 2 deletions lightllm/server/core/objs/sampling_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def initialize(self, constraint: str, tokenizer):
ctypes.memmove(self.constraint, constraint_bytes, len(constraint_bytes))
self.length = len(constraint_bytes)
try:
if self.length > 0:
if self.length > 0 and tokenizer is not None:
import xgrammar as xgr

tokenizer_info = xgr.TokenizerInfo.from_huggingface(tokenizer)
Expand Down Expand Up @@ -144,7 +144,7 @@ def initialize(self, constraint: str, tokenizer):
ctypes.memmove(self.constraint, constraint_bytes, len(constraint_bytes))
self.length = len(constraint_bytes)
try:
if self.length > 0:
if self.length > 0 and tokenizer is not None:
import xgrammar as xgr

tokenizer_info = xgr.TokenizerInfo.from_huggingface(tokenizer)
Expand Down
10 changes: 6 additions & 4 deletions unit_tests/server/core/objs/test_sampling_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
STOP_SEQUENCE_MAX_LENGTH,
REGULAR_CONSTRAINT_MAX_LENGTH,
ALLOWED_TOKEN_IDS_MAX_LENGTH,
JSON_SCHEMA_MAX_LENGTH,
GRAMMAR_CONSTRAINT_MAX_LENGTH,
)

grammar_str = r"""root ::= (expr "=" term)+
Expand Down Expand Up @@ -80,20 +82,20 @@ def test_regular_constraint_initialization():

def test_guided_grammar_initialization():
grammar = GuidedGrammar()
grammar.initialize(grammar_str)
grammar.initialize(grammar_str, None)
assert grammar.to_str() == grammar_str

with pytest.raises(AssertionError):
grammar.initialize("a" * (REGULAR_CONSTRAINT_MAX_LENGTH + 1))
grammar.initialize("a" * (GRAMMAR_CONSTRAINT_MAX_LENGTH + 1), None)


def test_guided_json_schema_initialization():
schema = GuidedJsonSchema()
schema.initialize(schema_str)
schema.initialize(schema_str, None)
assert schema.to_str() == schema_str

with pytest.raises(AssertionError):
schema.initialize("a" * (REGULAR_CONSTRAINT_MAX_LENGTH + 1))
schema.initialize("a" * (JSON_SCHEMA_MAX_LENGTH + 1), None)


def test_allowed_token_ids_initialization():
Expand Down