"docs/vscode:/vscode.git/clone" did not exist on "70da0c07483bb0cbcc80a2094cf41b64cb30ac0f"
test_sampling_params_e2e.py 5.76 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
6
7
8
9
10
11
12

import pytest

from vllm import LLM, SamplingParams

MODEL = "meta-llama/Llama-3.2-1B"
PROMPT = "Hello my name is Robert and I"


@pytest.fixture(scope="module")
13
def llm() -> LLM:
14
15
16
17
    # Disable prefix caching so that we can test prompt logprobs.
    # TODO remove this after https://github.com/vllm-project/vllm/pull/13949
    # is merged
    return LLM(MODEL, enforce_eager=True, enable_prefix_caching=False)
18
19


20
def test_n_gt_1(llm):
21
22
23
    """ParallelSampling is supported."""

    params = SamplingParams(n=3)
24
    outputs = llm.generate(PROMPT, params)
25
26
27
    assert len(outputs[0].outputs) == 3


28
def test_best_of(llm):
29
30
31
32
    """Raise a ValueError since best_of is deprecated."""

    params = SamplingParams(n=2, best_of=3)
    with pytest.raises(ValueError):
33
        _ = llm.generate(PROMPT, params)
34
35


36
def test_penalties(llm):
37
38
39
40
41
42
43
44
45
46
47
    """Check that we do not get errors if applied."""

    params = SamplingParams(
        temperature=1.2,
        presence_penalty=1.2,
        frequency_penalty=1.2,
        repetition_penalty=1.2,
        min_p=0.5,
        top_p=0.5,
        top_k=3,
    )
48
    _ = llm.generate(PROMPT, params)
49
50


51
def test_stop(llm):
52
53
    """Check that we respect the stop words."""

54
    output = llm.generate(PROMPT, SamplingParams(temperature=0))
55
56
57
58
    split_text = output[0].outputs[0].text.split()

    STOP_IDX = 5
    params = SamplingParams(temperature=0, stop=split_text[STOP_IDX])
59
    output = llm.generate(PROMPT, params)
60
61
62
63
64
    new_split_text = output[0].outputs[0].text.split()

    # Output should not contain the stop word.
    assert len(new_split_text) == STOP_IDX

65
66
67
    params = SamplingParams(
        temperature=0, stop=split_text[STOP_IDX], include_stop_str_in_output=True
    )
68
    output = llm.generate(PROMPT, params)
69
70
71
72
73
74
    new_split_text = output[0].outputs[0].text.split()

    # Output should contain the stop word.
    assert len(new_split_text) == STOP_IDX + 1


75
def test_stop_token_ids(llm):
76
77
    """Check that we respect the stop token ids."""

78
    output = llm.generate(PROMPT, SamplingParams(temperature=0))
79
80
81
82
83
84

    stop_token_id_0 = output[0].outputs[0].token_ids[5]
    stop_token_id_1 = output[0].outputs[0].token_ids[6]

    stop_token_ids = [stop_token_id_1, stop_token_id_0]
    params = SamplingParams(temperature=0, stop_token_ids=stop_token_ids)
85
    output = llm.generate(PROMPT, params)
86
87
88
89
    assert output[0].outputs[0].token_ids[-1] == stop_token_id_0

    stop_token_ids = [stop_token_id_0, stop_token_id_1]
    params = SamplingParams(temperature=0, stop_token_ids=stop_token_ids)
90
    output = llm.generate(PROMPT, params)
91
92
93
    assert output[0].outputs[0].token_ids[-1] == stop_token_id_0


94
def test_detokenize_false(llm):
95
96
    """Check that detokenize=False option works."""

97
    output = llm.generate(PROMPT, SamplingParams(detokenize=False))
98
99
100
    assert len(output[0].outputs[0].token_ids) > 0
    assert len(output[0].outputs[0].text) == 0

101
    output = llm.generate(
102
103
        PROMPT, SamplingParams(detokenize=False, logprobs=3, prompt_logprobs=3)
    )
104
105
106
107
108
109
110
111
112
113
114
115
116
    assert len(output[0].outputs[0].token_ids) > 0
    assert len(output[0].outputs[0].text) == 0

    prompt_logprobs = output[0].prompt_logprobs
    sampled_logprobs = output[0].outputs[0].logprobs
    assert len(prompt_logprobs) > 1
    assert len(sampled_logprobs) > 1
    for all_logprobs in (prompt_logprobs[1:], sampled_logprobs):
        for logprobs in all_logprobs:
            assert 3 <= len(logprobs) <= 4
            assert all(lp.decoded_token is None for lp in logprobs.values())


117
def test_bad_words(llm):
118
119
    """Check that we respect bad words."""

120
    output = llm.generate(PROMPT, SamplingParams(temperature=0))
121
122
123
124
    split_text = output[0].outputs[0].text.split()

    bad_words_1 = " ".join(split_text[:2])
    params = SamplingParams(temperature=0, bad_words=[bad_words_1])
125
    output = llm.generate(PROMPT, params)
126
127
128
129
    new_text = output[0].outputs[0].text
    assert bad_words_1 not in new_text

    bad_words_2 = new_text.split()[-1]
130
    params = SamplingParams(temperature=0, bad_words=[bad_words_1, bad_words_2])
131
    output = llm.generate(PROMPT, params)
132
133
134
    new_text = output[0].outputs[0].text
    assert bad_words_1 not in new_text
    assert bad_words_2 not in new_text
135
136


137
def test_logits_processor(llm):
138
139
140
141
142
143
144
145
146
147
    """Check that we reject logits processor."""

    # This sample logits processor gives infinite score to the i-th token,
    # where i is the length of the input sequence.
    # We therefore expect the output token sequence to be [0, 1, 2, ...]
    def pick_ith(token_ids, logits):
        logits[len(token_ids)] = float("inf")
        return logits

    with pytest.raises(ValueError):
148
        _ = llm.generate(PROMPT, SamplingParams(logits_processors=[pick_ith]))
149
150


151
def test_allowed_token_ids(llm):
152
153
154
155
    """Check that we can use allowed_token_ids."""

    TOKEN_ID = 10
    allowed_token_ids = [TOKEN_ID]
156
    output = llm.generate(PROMPT, SamplingParams(allowed_token_ids=allowed_token_ids))
157
158
    assert output[0].outputs[0].token_ids[-1] == TOKEN_ID

159
160
    # Reject empty allowed_token_ids.
    with pytest.raises(ValueError):
161
        _ = llm.generate(PROMPT, SamplingParams(allowed_token_ids=[]))
162

163
164
    # Reject negative token id.
    with pytest.raises(ValueError):
165
        _ = llm.generate(PROMPT, SamplingParams(allowed_token_ids=[-1]))
166
167
168

    # Reject out of vocabulary.
    with pytest.raises(ValueError):
169
        _ = llm.generate(PROMPT, SamplingParams(allowed_token_ids=[10000000]))
170
171


172
def test_seed(llm):
173
174
    """Check that seed impacts randomness."""

175
176
177
    out_1 = llm.generate(PROMPT, SamplingParams(seed=42))
    out_2 = llm.generate(PROMPT, SamplingParams(seed=42))
    out_3 = llm.generate(PROMPT, SamplingParams(seed=43))
178
179
180

    assert out_1[0].outputs[0].text == out_2[0].outputs[0].text
    assert out_1[0].outputs[0].text != out_3[0].outputs[0].text