test_sampling_params_e2e.py 5.59 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
6
7

import pytest

from vllm import LLM, SamplingParams

8
MODEL = "hmellor/tiny-random-LlamaForCausalLM"
9
10
11
12
PROMPT = "Hello my name is Robert and I"


@pytest.fixture(scope="module")
13
def llm() -> LLM:
14
    return LLM(MODEL, enforce_eager=True)
15
16


17
def test_n_gt_1(llm):
18
19
20
    """ParallelSampling is supported."""

    params = SamplingParams(n=3)
21
    outputs = llm.generate(PROMPT, params)
22
23
24
    assert len(outputs[0].outputs) == 3


25
def test_best_of(llm):
26
27
28
29
    """Raise a ValueError since best_of is deprecated."""

    params = SamplingParams(n=2, best_of=3)
    with pytest.raises(ValueError):
30
        _ = llm.generate(PROMPT, params)
31
32


33
def test_penalties(llm):
34
35
36
37
38
39
40
41
42
43
44
    """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,
    )
45
    _ = llm.generate(PROMPT, params)
46
47


48
def test_stop(llm):
49
50
    """Check that we respect the stop words."""

51
    output = llm.generate(PROMPT, SamplingParams(temperature=0))
52
53
54
55
    split_text = output[0].outputs[0].text.split()

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

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

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

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


72
def test_stop_token_ids(llm):
73
74
    """Check that we respect the stop token ids."""

75
    output = llm.generate(PROMPT, SamplingParams(temperature=0))
76
77
78
79
80
81

    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)
82
    output = llm.generate(PROMPT, params)
83
84
85
86
    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)
87
    output = llm.generate(PROMPT, params)
88
89
90
    assert output[0].outputs[0].token_ids[-1] == stop_token_id_0


91
def test_detokenize_false(llm):
92
93
    """Check that detokenize=False option works."""

94
    output = llm.generate(PROMPT, SamplingParams(detokenize=False))
95
96
97
    assert len(output[0].outputs[0].token_ids) > 0
    assert len(output[0].outputs[0].text) == 0

98
    output = llm.generate(
99
100
        PROMPT, SamplingParams(detokenize=False, logprobs=3, prompt_logprobs=3)
    )
101
102
103
104
105
106
107
108
109
110
111
112
113
    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())


114
def test_bad_words(llm):
115
116
    """Check that we respect bad words."""

117
    output = llm.generate(PROMPT, SamplingParams(temperature=0))
118
119
120
121
    split_text = output[0].outputs[0].text.split()

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

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


134
def test_logits_processor(llm):
135
136
137
138
139
140
141
142
143
144
    """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):
145
        _ = llm.generate(PROMPT, SamplingParams(logits_processors=[pick_ith]))
146
147


148
def test_allowed_token_ids(llm):
149
150
151
152
    """Check that we can use allowed_token_ids."""

    TOKEN_ID = 10
    allowed_token_ids = [TOKEN_ID]
153
    output = llm.generate(PROMPT, SamplingParams(allowed_token_ids=allowed_token_ids))
154
155
    assert output[0].outputs[0].token_ids[-1] == TOKEN_ID

156
157
    # Reject empty allowed_token_ids.
    with pytest.raises(ValueError):
158
        _ = llm.generate(PROMPT, SamplingParams(allowed_token_ids=[]))
159

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

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


169
def test_seed(llm):
170
171
    """Check that seed impacts randomness."""

172
173
174
    out_1 = llm.generate(PROMPT, SamplingParams(seed=42))
    out_2 = llm.generate(PROMPT, SamplingParams(seed=42))
    out_3 = llm.generate(PROMPT, SamplingParams(seed=43))
175
176
177

    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