-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
47 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pytest | ||
import torch | ||
from transformers import AutoTokenizer | ||
|
||
from outlines.serve.vllm import ( | ||
CFGLogitsProcessor, | ||
JSONLogitsProcessor, | ||
RegexLogitsProcessor, | ||
) | ||
|
||
TEST_REGEX = r"(-)?(0|[1-9][0-9]*)(.[0-9]+)?([eE][+-][0-9]+)?" | ||
TEST_CFG = """ | ||
start: DECIMAL | ||
DIGIT: "0".."9" | ||
INT: DIGIT+ | ||
DECIMAL: INT "." INT? | "." INT | ||
""" | ||
TEST_SCHEMA = '{"type": "string", "maxLength": 5}' | ||
|
||
LOGIT_PROCESSORS = ( | ||
(CFGLogitsProcessor, TEST_CFG), | ||
(RegexLogitsProcessor, TEST_REGEX), | ||
(JSONLogitsProcessor, TEST_SCHEMA), | ||
) | ||
|
||
TEST_MODEL = "hf-internal-testing/tiny-random-GPTJForCausalLM" | ||
|
||
|
||
@pytest.mark.skipif(not torch.cuda.is_available(), reason="requires cuda available") | ||
@pytest.mark.parametrize("logit_processor, fsm_str", LOGIT_PROCESSORS) | ||
def test_logit_processor(logit_processor, fsm_str: str): | ||
class MockvLLMEngine: | ||
def __init__(self, tokenizer): | ||
self.tokenizer = tokenizer | ||
|
||
def __call__(*_): | ||
return torch.tensor([[0, 1, 2, 3, 4]], dtype=torch.float), None | ||
|
||
tokenizer = AutoTokenizer.from_pretrained(TEST_MODEL) | ||
engine = MockvLLMEngine(tokenizer) | ||
logit_processor(fsm_str, engine) | ||
assert isinstance(engine.tokenizer.decode([0, 1, 2, 3]), list) | ||
logit_processor(fsm_str, engine) | ||
assert isinstance(engine.tokenizer.decode([0, 1, 2, 3]), list) |