main
py 125 lines 4.2 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 import weakref
5
6 import pytest
7
8 from vllm import LLM, SamplingParams
9 from vllm.exceptions import VLLMValidationError
10
11 MODEL_NAME = "distilbert/distilgpt2"
12
13 PROMPTS = [
14 "Hello, my name is",
15 "The president of the United States is",
16 "The capital of France is",
17 "The future of AI is",
18 ]
19
20 TOKEN_IDS = [
21 [0],
22 [0, 1],
23 [0, 2, 1],
24 [0, 3, 1, 2],
25 ]
26
27
28 @pytest.fixture(scope="module")
29 def llm(vllm_runner):
30 with vllm_runner(
31 MODEL_NAME,
32 max_num_batched_tokens=4096,
33 tensor_parallel_size=1,
34 gpu_memory_utilization=0.10,
35 enforce_eager=True,
36 ) as runner:
37 # pytest caches yielded fixtures until after teardown, so use a proxy to
38 # avoid retaining the LLM while VllmRunner.__exit__ releases ROCm memory.
39 yield weakref.proxy(runner.llm)
40
41
42 @pytest.mark.skip_global_cleanup
43 def test_multiple_sampling_params(llm: LLM):
44 sampling_params = [
45 SamplingParams(temperature=0.01, top_p=0.95),
46 SamplingParams(temperature=0.3, top_p=0.95),
47 SamplingParams(temperature=0.7, top_p=0.95),
48 SamplingParams(temperature=0.99, top_p=0.95),
49 ]
50
51 # Multiple SamplingParams should be matched with each prompt
52 outputs = llm.generate(PROMPTS, sampling_params=sampling_params)
53 assert len(PROMPTS) == len(outputs)
54
55 # Exception raised, if the size of params does not match the size of prompts
56 with pytest.raises(VLLMValidationError):
57 outputs = llm.generate(PROMPTS, sampling_params=sampling_params[:3])
58
59 # Single SamplingParams should be applied to every prompt
60 single_sampling_params = SamplingParams(temperature=0.3, top_p=0.95)
61 outputs = llm.generate(PROMPTS, sampling_params=single_sampling_params)
62 assert len(PROMPTS) == len(outputs)
63
64 # sampling_params is None, default params should be applied
65 outputs = llm.generate(PROMPTS, sampling_params=None)
66 assert len(PROMPTS) == len(outputs)
67
68
69 def test_multiple_priority(llm: LLM):
70 # Generate works when priority is None
71 outputs = llm.generate(PROMPTS, sampling_params=None, priority=None)
72 assert len(PROMPTS) == len(outputs)
73
74 # Generate works when length of priority is same as the len(PROMPTS)
75 outputs = llm.generate(PROMPTS, sampling_params=None, priority=[0] * len(PROMPTS))
76 assert len(PROMPTS) == len(outputs)
77
78 # Exception raised, if the length of priority does not match the length of prompts
79 with pytest.raises(VLLMValidationError):
80 outputs = llm.generate(
81 PROMPTS, sampling_params=None, priority=[0] * (len(PROMPTS) - 1)
82 )
83
84 # Exception raised, if the priority list is empty
85 with pytest.raises(VLLMValidationError):
86 outputs = llm.generate(PROMPTS, sampling_params=None, priority=[])
87
88
89 def test_single_prompt_priority(llm: LLM):
90 # Single string prompts should be normalized to one request.
91 outputs = llm.generate(PROMPTS[0], sampling_params=None, priority=[0])
92 assert len(outputs) == 1
93
94
95 def test_max_model_len(vllm_runner):
96 max_model_len = 20
97 with vllm_runner(
98 MODEL_NAME,
99 max_model_len=max_model_len,
100 gpu_memory_utilization=0.10,
101 enforce_eager=True, # reduce test time
102 ) as runner:
103 sampling_params = SamplingParams(max_tokens=max_model_len + 10)
104 outputs = runner.llm.generate(PROMPTS, sampling_params)
105 for output in outputs:
106 num_total_tokens = len(output.prompt_token_ids) + len(
107 output.outputs[0].token_ids
108 )
109 # Total tokens must not exceed max_model_len.
110 # It can be less if generation finishes due to other reasons (e.g., EOS)
111 # before reaching the absolute model length limit.
112 assert num_total_tokens <= max_model_len
113
114
115 def test_log_stats(vllm_runner):
116 with vllm_runner(
117 MODEL_NAME,
118 disable_log_stats=False,
119 gpu_memory_utilization=0.10,
120 enforce_eager=True, # reduce test time
121 ) as runner:
122 outputs = runner.llm.generate(PROMPTS, sampling_params=None)
123
124 # disable_log_stats is False, every output should have metrics
125 assert all(output.metrics is not None for output in outputs)