2024-02-29 14:13:08 -08:00
|
|
|
# This unit test should be moved to a new
|
|
|
|
# tests/test_guided_decoding directory.
|
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-04-16 08:54:57 +03:00
|
|
|
from vllm.entrypoints.openai.protocol import CompletionRequest
|
|
|
|
from vllm.model_executor.guided_decoding import (
|
|
|
|
get_guided_decoding_logits_processor)
|
|
|
|
from vllm.model_executor.guided_decoding.outlines_logits_processors import (
|
|
|
|
JSONLogitsProcessor, RegexLogitsProcessor)
|
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
|
|
|
|
@pytest.mark.parametrize("backend", ["outlines", "lm-format-enforcer"])
|
2024-07-12 12:55:39 -04:00
|
|
|
async def test_guided_logits_processor_black_box(backend: str, sample_regex,
|
|
|
|
sample_json_schema):
|
2024-04-16 08:54:57 +03:00
|
|
|
tokenizer = AutoTokenizer.from_pretrained('HuggingFaceH4/zephyr-7b-beta')
|
|
|
|
token_ids = tokenizer.encode(
|
2024-07-12 12:55:39 -04:00
|
|
|
f"Give an example IPv4 address with this regex: {sample_regex}")
|
2024-04-16 08:54:57 +03:00
|
|
|
regex_request = CompletionRequest(model='test',
|
|
|
|
prompt=token_ids,
|
2024-07-12 12:55:39 -04:00
|
|
|
guided_regex=sample_regex)
|
2024-04-16 08:54:57 +03:00
|
|
|
regex_lp = await get_guided_decoding_logits_processor(
|
|
|
|
backend, regex_request, tokenizer)
|
|
|
|
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-04-16 08:54:57 +03:00
|
|
|
json_request = CompletionRequest(model='test',
|
|
|
|
prompt=token_ids,
|
2024-07-12 12:55:39 -04:00
|
|
|
guided_json=sample_json_schema)
|
2024-04-16 08:54:57 +03:00
|
|
|
json_lp = await get_guided_decoding_logits_processor(
|
|
|
|
backend, json_request, tokenizer)
|
|
|
|
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)
|