test_sampling_params.py 8.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

import numpy as np
import pytest

from vllm.benchmarks.datasets.utils import get_sampling_params
from vllm.tokenizers import TokenizerLike


class _FakeTokenizer(TokenizerLike):
    """Minimal tokenizer implementing the TokenizerLike protocol
    for testing get_sampling_params."""

    def __init__(self, vocab_size: int = 1000, num_special_tokens: int = 0) -> None:
        self._vocab_size = vocab_size
        self._num_special_tokens = num_special_tokens

    # -- Properties required by TokenizerLike --

    @classmethod
    def from_pretrained(cls, path_or_repo_id, *a, **kw):  # type: ignore[override]
        return cls()

    @property
    def vocab_size(self) -> int:
        return self._vocab_size

    @property
    def all_special_tokens(self) -> list[str]:
        return []

    @property
    def all_special_ids(self) -> list[int]:
        return []

    @property
    def bos_token_id(self) -> int:
        return 0

    @property
    def eos_token_id(self) -> int:
        return 1

    @property
    def pad_token_id(self) -> int:
        return 2

    @property
    def is_fast(self) -> bool:
        return False

    @property
    def max_token_id(self) -> int:
        return self._vocab_size - 1

    @property
    def max_chars_per_token(self) -> int:
        return 4

    @property
    def truncation_side(self) -> str:
        return "right"

    def num_special_tokens_to_add(self) -> int:
        return self._num_special_tokens

    def __call__(self, text, text_pair=None, **kw):  # type: ignore[override]
        raise NotImplementedError

    def get_vocab(self) -> dict[str, int]:
        return {}

    def get_added_vocab(self) -> dict[str, int]:
        return {}

    def encode(self, text, **kw) -> list[int]:  # type: ignore[override]
        raise NotImplementedError

    def apply_chat_template(self, messages, **kw):  # type: ignore[override]
        raise NotImplementedError

    def convert_tokens_to_ids(self, tokens):  # type: ignore[override]
        raise NotImplementedError

    def convert_tokens_to_string(self, tokens: list[str]) -> str:
        raise NotImplementedError

    def decode(self, ids, skip_special_tokens: bool = False) -> str:  # type: ignore[override]
        raise NotImplementedError

    def convert_ids_to_tokens(  # type: ignore[override]
        self, ids, skip_special_tokens: bool = False
    ) -> list[str]:
        raise NotImplementedError


class TestGetSamplingParams:
    """Tests for ``get_sampling_params`` in ``vllm.benchmarks.datasets.shared``."""

    # -- helpers --

    @staticmethod
    def _tok(vocab_size: int = 1000, num_special: int = 0) -> _FakeTokenizer:
        return _FakeTokenizer(vocab_size=vocab_size, num_special_tokens=num_special)

    # -- return shape / dtype --

    def test_returns_three_arrays(self):
        rng = np.random.default_rng(0)
        result = get_sampling_params(rng, 5, 0.0, 100, 50, self._tok())
        assert len(result) == 3
        for arr in result:
            assert isinstance(arr, np.ndarray)

    @pytest.mark.parametrize("n", [1, 10, 100])
    def test_output_length_matches_num_requests(self, n: int):
        rng = np.random.default_rng(42)
        input_lens, output_lens, offsets = get_sampling_params(
            rng, n, 0.0, 64, 32, self._tok()
        )
        assert input_lens.shape == (n,)
        assert output_lens.shape == (n,)
        assert offsets.shape == (n,)

    # -- fixed lengths (range_ratio = 0) --

    def test_zero_range_ratio_gives_constant_lengths(self):
        rng = np.random.default_rng(7)
        input_lens, output_lens, _ = get_sampling_params(
            rng, 20, 0.0, 128, 64, self._tok()
        )
        assert np.all(input_lens == 128)
        assert np.all(output_lens == 64)

    def test_special_tokens_subtracted_from_input_only(self):
        rng = np.random.default_rng(7)
        input_lens, output_lens, _ = get_sampling_params(
            rng, 10, 0.0, 100, 50, self._tok(num_special=4)
        )
        # real_input_len = 100 - 4 = 96, range_ratio 0 → all 96
        assert np.all(input_lens == 96)
        # special tokens are not subtracted from output length
        assert np.all(output_lens == 50)

    # -- range ratios --

    def test_input_range_bounds(self):
        rng = np.random.default_rng(0)
        ratio = 0.5
        base = 200
        input_lens, _, _ = get_sampling_params(
            rng, 500, {"input": ratio, "output": 0.0}, base, 50, self._tok()
        )
        lo = int(np.floor(base * (1 - ratio)))
        hi = int(np.ceil(base * (1 + ratio)))
        assert np.all(input_lens >= lo)
        assert np.all(input_lens <= hi)

    def test_output_range_bounds(self):
        rng = np.random.default_rng(0)
        ratio = 0.3
        base = 100
        _, output_lens, _ = get_sampling_params(
            rng, 500, {"input": 0.0, "output": ratio}, 50, base, self._tok()
        )
        lo = max(1, int(np.floor(base * (1 - ratio))))
        hi = int(np.ceil(base * (1 + ratio)))
        assert np.all(output_lens >= lo)
        assert np.all(output_lens <= hi)

    def test_output_low_clamped_to_one(self):
        """Even with a high ratio that would push output_low to 0,
        the function clamps it to 1."""
        rng = np.random.default_rng(0)
        # output_len=1, ratio=0.99 → floor(1*0.01)=0, should clamp to 1
        _, output_lens, _ = get_sampling_params(
            rng, 50, {"input": 0.0, "output": 0.99}, 100, 1, self._tok()
        )
        assert np.all(output_lens >= 1)

    # -- offsets bounded by vocab_size --

    @pytest.mark.parametrize("vocab", [100, 32000, 128256])
    def test_offsets_within_vocab(self, vocab: int):
        rng = np.random.default_rng(0)
        _, _, offsets = get_sampling_params(
            rng, 200, 0.0, 64, 32, self._tok(vocab_size=vocab)
        )
        assert np.all(offsets >= 0)
        assert np.all(offsets < vocab)

    # -- reproducibility --

    def test_same_seed_same_results(self):
        tok = self._tok()
        rr = {"input": 0.3, "output": 0.2}
        a = get_sampling_params(np.random.default_rng(42), 50, rr, 256, 64, tok)
        b = get_sampling_params(np.random.default_rng(42), 50, rr, 256, 64, tok)
        for arr_a, arr_b in zip(a, b):
            np.testing.assert_array_equal(arr_a, arr_b)

    def test_different_seed_different_results(self):
        tok = self._tok()
        rr = {"input": 0.3, "output": 0.2}
        a = get_sampling_params(np.random.default_rng(0), 50, rr, 256, 64, tok)
        b = get_sampling_params(np.random.default_rng(1), 50, rr, 256, 64, tok)
        # Extremely unlikely all three arrays match with different seeds
        assert not all(np.array_equal(arr_a, arr_b) for arr_a, arr_b in zip(a, b))

    # -- validation / error paths --

    @pytest.mark.parametrize("bad_ratio", [-0.1, 1.0, 1.5])
    def test_invalid_input_range_ratio(self, bad_ratio: float):
        rng = np.random.default_rng(0)
        with pytest.raises(ValueError, match="input_range_ratio"):
            get_sampling_params(
                rng, 10, {"input": bad_ratio, "output": 0.0}, 100, 50, self._tok()
            )

    @pytest.mark.parametrize("bad_ratio", [-0.1, 1.0, 1.5])
    def test_invalid_output_range_ratio(self, bad_ratio: float):
        rng = np.random.default_rng(0)
        with pytest.raises(ValueError, match="output_range_ratio"):
            get_sampling_params(
                rng, 10, {"input": 0.0, "output": bad_ratio}, 100, 50, self._tok()
            )

    def test_invalid_dict_missing_keys(self):
        rng = np.random.default_rng(0)
        with pytest.raises(ValueError, match="input.*output"):
            get_sampling_params(rng, 10, {"input": 0.1}, 100, 50, self._tok())

    def test_input_len_zero_with_special_tokens(self):
        """input_len < num_special_tokens → real_input_len = 0, which is fine
        (range [0, 0])."""
        rng = np.random.default_rng(0)
        input_lens, _, _ = get_sampling_params(
            rng, 5, 0.0, 5, 50, self._tok(num_special=10)
        )
        # real_input_len = max(0, 5 - 10) = 0
        assert np.all(input_lens == 0)

    # -- edge cases --

    def test_single_request(self):
        rng = np.random.default_rng(0)
        i, o, off = get_sampling_params(rng, 1, 0.0, 100, 50, self._tok())
        assert i.shape == (1,)
        assert o.shape == (1,)
        assert off.shape == (1,)

    def test_large_num_requests(self):
        rng = np.random.default_rng(0)
        i, o, off = get_sampling_params(rng, 10_000, 0.5, 512, 128, self._tok())
        assert i.shape == (10_000,)
        assert o.shape == (10_000,)
        assert off.shape == (10_000,)