Skip to content

Commit 16e2c8e

Browse files
youkaichaoAlvant
authored andcommitted
[misc][core] lazy import outlines (vllm-project#7831)
1 parent 615b929 commit 16e2c8e

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

.buildkite/test-pipeline.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ steps:
8787
commands:
8888
- pip install -e ./plugins/vllm_add_dummy_model
8989
- pip install git+https://github.com/EleutherAI/lm-evaluation-harness.git@a4987bba6e9e9b3f22bd3a6c1ecf0abd04fd5622#egg=lm_eval[api]
90-
- pytest -v -s entrypoints/llm
90+
- pytest -v -s entrypoints/llm --ignore=entrypoints/llm/test_lazy_outlines.py
91+
- pytest -v -s entrypoints/llm/test_lazy_outlines.py # it needs a clean process
9192
- pytest -v -s entrypoints/openai
9293

9394
- label: Distributed Tests (4 GPUs) # 10min
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
3+
from vllm import LLM, SamplingParams
4+
5+
6+
def test_lazy_outlines(sample_regex):
7+
"""If users don't use guided decoding, outlines should not be imported.
8+
"""
9+
prompts = [
10+
"Hello, my name is",
11+
"The president of the United States is",
12+
"The capital of France is",
13+
"The future of AI is",
14+
]
15+
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
16+
17+
llm = LLM(model="facebook/opt-125m",
18+
enforce_eager=True,
19+
gpu_memory_utilization=0.3)
20+
outputs = llm.generate(prompts, sampling_params)
21+
for output in outputs:
22+
prompt = output.prompt
23+
generated_text = output.outputs[0].text
24+
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
25+
26+
# make sure outlines is not imported
27+
assert 'outlines' not in sys.modules
28+
29+
llm = LLM(model="facebook/opt-125m",
30+
enforce_eager=True,
31+
guided_decoding_backend="lm-format-enforcer",
32+
gpu_memory_utilization=0.3)
33+
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
34+
outputs = llm.generate(
35+
prompts=[
36+
f"Give an example IPv4 address with this regex: {sample_regex}"
37+
] * 2,
38+
sampling_params=sampling_params,
39+
use_tqdm=True,
40+
guided_options_request=dict(guided_regex=sample_regex))
41+
42+
for output in outputs:
43+
prompt = output.prompt
44+
generated_text = output.outputs[0].text
45+
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
46+
47+
# make sure outlines is not imported
48+
assert 'outlines' not in sys.modules

vllm/model_executor/guided_decoding/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
get_local_outlines_guided_decoding_logits_processor,
1414
get_outlines_guided_decoding_logits_processor)
1515

16-
1716
async def get_guided_decoding_logits_processor(
1817
guided_decoding_backend: str, request: Union[CompletionRequest,
1918
ChatCompletionRequest],
2019
tokenizer) -> Optional[LogitsProcessor]:
2120
request = _adapt_request_for_tool_use(request)
2221

2322
if guided_decoding_backend == 'outlines':
23+
# NOTE: lazy import outlines to avoid https://github.com/vllm-project/vllm/issues/4193
24+
from vllm.model_executor.guided_decoding.outlines_decoding import ( # noqa
25+
get_outlines_guided_decoding_logits_processor)
2426
return await get_outlines_guided_decoding_logits_processor(
2527
request, tokenizer)
2628
if guided_decoding_backend == 'lm-format-enforcer':
@@ -40,6 +42,9 @@ def get_local_guided_decoding_logits_processor(
4042
# request = _adapt_request_for_tool_use(request)
4143

4244
if guided_decoding_backend == 'outlines':
45+
# NOTE: lazy import outlines to avoid https://github.com/vllm-project/vllm/issues/4193
46+
from vllm.model_executor.guided_decoding.outlines_decoding import ( # noqa
47+
get_local_outlines_guided_decoding_logits_processor)
4348
return get_local_outlines_guided_decoding_logits_processor(
4449
guided_options, tokenizer)
4550
if guided_decoding_backend == 'lm-format-enforcer':

vllm/model_executor/guided_decoding/lm_format_enforcer_decoding.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
get_local_outlines_guided_decoding_logits_processor,
2020
get_outlines_guided_decoding_logits_processor)
2121

22-
2322
async def get_lm_format_enforcer_guided_decoding_logits_processor(
2423
request: Union[CompletionRequest, ChatCompletionRequest],
2524
tokenizer) -> Optional[LogitsProcessor]:
@@ -43,6 +42,10 @@ async def get_lm_format_enforcer_guided_decoding_logits_processor(
4342
character_level_parser = RegexParser(request.guided_regex)
4443
elif request.guided_grammar:
4544
# CFG grammar not supported by LMFE, revert to outlines
45+
46+
# NOTE: lazy import outlines to avoid https://github.com/vllm-project/vllm/issues/4193
47+
from vllm.model_executor.guided_decoding.outlines_decoding import (
48+
get_outlines_guided_decoding_logits_processor)
4649
return await get_outlines_guided_decoding_logits_processor(
4750
request, tokenizer)
4851
elif (request.response_format is not None
@@ -87,6 +90,10 @@ def get_local_lm_format_enforcer_guided_decoding_logits_processor(
8790
character_level_parser = RegexParser(guided_options.guided_regex)
8891
elif guided_options.guided_grammar:
8992
# CFG grammar not supported by LMFE, revert to outlines
93+
94+
# NOTE: lazy import outlines to avoid https://github.com/vllm-project/vllm/issues/4193
95+
from vllm.model_executor.guided_decoding.outlines_decoding import (
96+
get_local_outlines_guided_decoding_logits_processor)
9097
return get_local_outlines_guided_decoding_logits_processor(
9198
guided_options, tokenizer)
9299
elif guided_options.guided_json_object:

0 commit comments

Comments
 (0)