2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-12-18 03:34:08 -03:00
|
|
|
import pickle
|
|
|
|
|
2024-04-16 08:54:57 +03:00
|
|
|
import pytest
|
2024-02-29 14:13:08 -08:00
|
|
|
import torch
|
2024-03-25 23:59:47 +09:00
|
|
|
from transformers import AutoTokenizer
|
2024-02-29 14:13:08 -08:00
|
|
|
|
2024-12-18 03:34:08 -03:00
|
|
|
from vllm.config import ModelConfig
|
2024-04-16 08:54:57 +03:00
|
|
|
from vllm.model_executor.guided_decoding import (
|
2024-12-18 03:34:08 -03:00
|
|
|
get_guided_decoding_logits_processor,
|
|
|
|
get_local_guided_decoding_logits_processor)
|
2024-04-16 08:54:57 +03:00
|
|
|
from vllm.model_executor.guided_decoding.outlines_logits_processors import (
|
|
|
|
JSONLogitsProcessor, RegexLogitsProcessor)
|
2024-09-30 19:34:25 -06:00
|
|
|
from vllm.sampling_params import GuidedDecodingParams
|
2024-02-29 14:13:08 -08:00
|
|
|
|
2024-12-18 03:34:08 -03:00
|
|
|
MODEL_NAME = 'HuggingFaceH4/zephyr-7b-beta'
|
2024-12-18 23:00:38 -05:00
|
|
|
GUIDED_DECODING_BACKENDS = ["outlines", "lm-format-enforcer", "xgrammar"]
|
2024-12-18 03:34:08 -03:00
|
|
|
|
2024-02-29 14:13:08 -08:00
|
|
|
|
2024-07-12 12:55:39 -04:00
|
|
|
def test_guided_logits_processors(sample_regex, sample_json_schema):
|
2024-02-29 14:13:08 -08:00
|
|
|
"""Basic unit test for RegexLogitsProcessor and JSONLogitsProcessor."""
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained('HuggingFaceH4/zephyr-7b-beta')
|
2024-07-12 12:55:39 -04:00
|
|
|
regex_LP = RegexLogitsProcessor(sample_regex, tokenizer)
|
|
|
|
json_LP = JSONLogitsProcessor(sample_json_schema,
|
2024-05-01 05:48:39 +02:00
|
|
|
tokenizer,
|
|
|
|
whitespace_pattern=None)
|
2024-02-29 14:13:08 -08:00
|
|
|
|
|
|
|
token_ids = tokenizer.encode(
|
2024-07-12 12:55:39 -04:00
|
|
|
f"Give an example IPv4 address with this regex: {sample_regex}")
|
2024-02-29 14:13:08 -08:00
|
|
|
tensor = torch.rand(32000)
|
|
|
|
original_tensor = torch.clone(tensor)
|
|
|
|
regex_LP(token_ids, tensor)
|
|
|
|
assert tensor.shape == original_tensor.shape
|
|
|
|
assert not torch.allclose(tensor, original_tensor)
|
|
|
|
|
|
|
|
token_ids = tokenizer.encode(
|
2024-07-12 12:55:39 -04:00
|
|
|
f"Give an employee profile that fits this schema: {sample_json_schema}"
|
|
|
|
)
|
2024-02-29 14:13:08 -08:00
|
|
|
tensor = torch.rand(32000)
|
|
|
|
original_tensor = torch.clone(tensor)
|
|
|
|
json_LP(token_ids, tensor)
|
|
|
|
assert tensor.shape == original_tensor.shape
|
|
|
|
assert not torch.allclose(tensor, original_tensor)
|
2024-04-16 08:54:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2024-12-18 23:00:38 -05:00
|
|
|
@pytest.mark.parametrize("backend", GUIDED_DECODING_BACKENDS)
|
2024-12-18 03:34:08 -03:00
|
|
|
@pytest.mark.parametrize("is_local", [True, False])
|
|
|
|
async def test_guided_logits_processor_black_box(backend: str, is_local: bool,
|
|
|
|
sample_regex,
|
2024-07-12 12:55:39 -04:00
|
|
|
sample_json_schema):
|
2024-12-18 03:34:08 -03:00
|
|
|
|
|
|
|
config = ModelConfig(
|
|
|
|
MODEL_NAME,
|
|
|
|
task="generate",
|
|
|
|
tokenizer=MODEL_NAME,
|
|
|
|
tokenizer_mode="auto",
|
|
|
|
trust_remote_code=False,
|
|
|
|
seed=0,
|
|
|
|
dtype="bfloat16",
|
|
|
|
)
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
2024-04-16 08:54:57 +03:00
|
|
|
token_ids = tokenizer.encode(
|
2024-07-12 12:55:39 -04:00
|
|
|
f"Give an example IPv4 address with this regex: {sample_regex}")
|
2024-09-30 19:34:25 -06:00
|
|
|
regex_request = GuidedDecodingParams(regex=sample_regex, backend=backend)
|
2024-12-18 03:34:08 -03:00
|
|
|
|
|
|
|
regex_lp = get_local_guided_decoding_logits_processor(
|
|
|
|
regex_request, tokenizer, config) if is_local else \
|
|
|
|
await get_guided_decoding_logits_processor(
|
|
|
|
regex_request, tokenizer, config)
|
2024-04-16 08:54:57 +03:00
|
|
|
assert regex_lp is not None
|
|
|
|
tensor = torch.rand(32000)
|
|
|
|
original_tensor = torch.clone(tensor)
|
|
|
|
tensor = regex_lp(token_ids, tensor)
|
|
|
|
assert tensor.shape == original_tensor.shape
|
|
|
|
assert not torch.allclose(tensor, original_tensor)
|
|
|
|
|
|
|
|
token_ids = tokenizer.encode(
|
2024-07-12 12:55:39 -04:00
|
|
|
f"Give an employee profile that fits this schema: {sample_json_schema}"
|
|
|
|
)
|
2024-09-30 19:34:25 -06:00
|
|
|
json_request = GuidedDecodingParams(json=sample_json_schema,
|
|
|
|
backend=backend)
|
2024-04-16 08:54:57 +03:00
|
|
|
json_lp = await get_guided_decoding_logits_processor(
|
2024-12-18 03:34:08 -03:00
|
|
|
json_request, tokenizer, config)
|
2024-04-16 08:54:57 +03:00
|
|
|
assert json_lp is not None
|
|
|
|
tensor = torch.rand(32000)
|
|
|
|
original_tensor = torch.clone(tensor)
|
|
|
|
tensor = json_lp(token_ids, tensor)
|
|
|
|
assert tensor.shape == original_tensor.shape
|
|
|
|
assert not torch.allclose(tensor, original_tensor)
|
2024-09-30 19:34:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
def test_multiple_guided_options_not_allowed(sample_json_schema, sample_regex):
|
|
|
|
with pytest.raises(ValueError,
|
|
|
|
match="You can only use one kind of guided"):
|
|
|
|
GuidedDecodingParams(json=sample_json_schema, regex=sample_regex)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError,
|
|
|
|
match="You can only use one kind of guided"):
|
|
|
|
GuidedDecodingParams(json=sample_json_schema, json_object=True)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError,
|
|
|
|
match="You can only use one kind of guided"):
|
|
|
|
GuidedDecodingParams(json=sample_json_schema, choice=["a", "b"])
|
|
|
|
|
|
|
|
with pytest.raises(ValueError,
|
|
|
|
match="You can only use one kind of guided"):
|
|
|
|
GuidedDecodingParams(json=sample_json_schema, grammar="test grammar")
|
2024-12-18 03:34:08 -03:00
|
|
|
|
|
|
|
|
|
|
|
def test_pickle_xgrammar_tokenizer_data():
|
|
|
|
|
|
|
|
# TODO: move to another test file for xgrammar
|
|
|
|
try:
|
|
|
|
import xgrammar as xgr
|
|
|
|
except ImportError:
|
|
|
|
pytest.skip("Could not import xgrammar to run test")
|
|
|
|
|
|
|
|
from vllm.model_executor.guided_decoding.xgrammar_decoding import (
|
|
|
|
TokenizerData)
|
|
|
|
tokenizer_data = TokenizerData(vocab_type=xgr.VocabType.RAW)
|
|
|
|
pickled = pickle.dumps(tokenizer_data)
|
|
|
|
|
|
|
|
assert pickled is not None
|
|
|
|
|
|
|
|
depickled: TokenizerData = pickle.loads(pickled)
|
|
|
|
|
|
|
|
assert depickled is not None
|
|
|
|
assert depickled.vocab_type == xgr.VocabType.RAW
|