topk_topp_sampler.py 11.4 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
from typing import Optional
5
6
7

import torch
import torch.nn as nn
8
from packaging import version
9
10

from vllm import envs
11
from vllm.config import LogprobsMode
12
13
14
15
16
17
18
from vllm.logger import init_logger
from vllm.platforms import current_platform

logger = init_logger(__name__)

try:
    import flashinfer.sampling
19

20
21
22
23
24
25
    is_flashinfer_available = True
except ImportError:
    is_flashinfer_available = False


class TopKTopPSampler(nn.Module):
26
27
28
29
30
31
    """
    Module that performs optional top-k and top-p filtering followed by
    weighted random sampling of logits.

    Implementations may update the logits tensor in-place.
    """
32

33
    def __init__(self, logprobs_mode: LogprobsMode = "raw_logprobs") -> None:
34
        super().__init__()
35
36
37
        self.logprobs_mode = logprobs_mode
        # flashinfer optimization does not apply if intermediate
        # logprobs/logits after top_k/top_p need to be returned
38
39
40
41
        if (
            logprobs_mode not in ("processed_logits", "processed_logprobs")
            and current_platform.is_cuda()
        ):
42
            if is_flashinfer_available:
43
                flashinfer_version = flashinfer.__version__
44
                if version.parse(flashinfer_version) < version.parse("0.2.3"):
45
                    logger.warning_once(
46
                        "FlashInfer version >= 0.2.3 required. "
47
48
                        "Falling back to default sampling implementation."
                    )
49
50
                    self.forward = self.forward_native
                elif envs.VLLM_USE_FLASHINFER_SAMPLER is not False:
51
52
53
54
55
56
57
58
                    # NOTE(woosuk): The V0 sampler doesn't use FlashInfer for
                    # sampling unless VLLM_USE_FLASHINFER_SAMPLER=1 (i.e., by
                    # default it is unused). For backward compatibility, we set
                    # `VLLM_USE_FLASHINFER_SAMPLER` as None by default and
                    # interpret it differently in V0 and V1 samplers: In V0,
                    # None means False, while in V1, None means True. This is
                    # why we use the condition
                    # `envs.VLLM_USE_FLASHINFER_SAMPLER is not False` here.
59
                    logger.info_once("Using FlashInfer for top-p & top-k sampling.")
60
61
                    self.forward = self.forward_cuda
                else:
62
                    logger.warning_once(
63
64
65
                        "FlashInfer is available, but it is not enabled. "
                        "Falling back to the PyTorch-native implementation of "
                        "top-p & top-k sampling. For the best performance, "
66
67
                        "please set VLLM_USE_FLASHINFER_SAMPLER=1."
                    )
68
69
                    self.forward = self.forward_native
            else:
70
                logger.warning_once(
71
72
                    "FlashInfer is not available. Falling back to the PyTorch-"
                    "native implementation of top-p & top-k sampling. For the "
73
74
                    "best performance, please install FlashInfer."
                )
75
                self.forward = self.forward_native
76
77
        elif current_platform.is_cpu():
            self.forward = self.forward_cpu
78
79
        else:
            self.forward = self.forward_native
80
81

        self.apply_top_k_top_p = apply_top_k_top_p
82
83
84
85

    def forward_native(
        self,
        logits: torch.Tensor,
86
        generators: dict[int, torch.Generator],
87
88
        k: Optional[torch.Tensor],
        p: Optional[torch.Tensor],
89
    ) -> tuple[torch.Tensor, Optional[torch.Tensor]]:
90
91
92
93
94
        """
        PyTorch-native implementation of top-k and top-p sampling.

        The logits tensor may be updated in-place.
        """
95
96
        logits = self.apply_top_k_top_p(logits, k, p)
        logits_to_return = None
97
        if self.logprobs_mode == "processed_logits":
98
            logits_to_return = logits
99
        elif self.logprobs_mode == "processed_logprobs":
100
            logits_to_return = logits.log_softmax(dim=-1, dtype=torch.float32)
101
        probs = logits.softmax(dim=-1, dtype=torch.float32)
102
        return random_sample(probs, generators), logits_to_return
103
104
105
106

    def forward_cuda(
        self,
        logits: torch.Tensor,
107
        generators: dict[int, torch.Generator],
108
109
        k: Optional[torch.Tensor],
        p: Optional[torch.Tensor],
110
    ) -> tuple[torch.Tensor, Optional[torch.Tensor]]:
111
        """More optimized implementation for top-k and top-p sampling."""
112
113
114
115
116
        # We prefer `random_sample` over `flashinfer_sample` when sorting is
        # not needed. This is because `random_sample` does not require
        # CPU-GPU synchronization while `flashinfer_sample` does.
        if (k is None and p is None) or generators:
            if generators:
117
118
119
120
121
                logger.debug_once(
                    "FlashInfer 0.2.3+ does not support "
                    "per-request generators. Falling back to "
                    "PyTorch-native implementation."
                )
122
            return self.forward_native(logits, generators, k, p)
123
124
125
        assert self.logprobs_mode not in ("processed_logits", "processed_logprobs"), (
            "FlashInfer does not support returning logits/logprobs"
        )
126
127
128
        # flashinfer sampling functions expect contiguous logits.
        # In flex_attn/triton_attn fp32 inference, logits can be non-contiguous
        # because of slicing operation in logits_processor.
129
        return flashinfer_sample(logits.contiguous(), k, p, generators), None
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
    def forward_cpu(
        self,
        logits: torch.Tensor,
        generators: dict[int, torch.Generator],
        k: Optional[torch.Tensor],
        p: Optional[torch.Tensor],
    ) -> tuple[torch.Tensor, Optional[torch.Tensor]]:
        """
        PyTorch-native implementation of top-k and top-p sampling for CPU.

        The logits tensor may be updated in-place.
        """
        logits = self.apply_top_k_top_p(logits, k, p)
        logits_to_return = None
        if self.logprobs_mode == "processed_logits":
            logits_to_return = logits
        elif self.logprobs_mode == "processed_logprobs":
            logits_to_return = logits.log_softmax(dim=-1, dtype=torch.float32)

        # Note: this is a workaround for
        # https://github.com/pytorch/pytorch/pull/151218
        @torch.compile(dynamic=True)
        def compiled_random_sample(logits: torch.Tensor) -> torch.Tensor:
            probs = logits.softmax(dim=-1, dtype=torch.float32)
            q = torch.empty_like(probs)
            q.exponential_()
            return probs.div(q).argmax(dim=-1).view(-1)

        if len(generators) != logits.shape[0]:
            return compiled_random_sample(logits), logits_to_return
        else:
            probs = logits.softmax(dim=-1, dtype=torch.float32)
            q = torch.empty_like(probs)
            q.exponential_()
            for i, generator in generators.items():
                q[i].exponential_(generator=generator)

            return probs.div_(q).argmax(dim=-1).view(-1), logits_to_return

170
171
172

def apply_top_k_top_p(
    logits: torch.Tensor,
173
174
    k: Optional[torch.Tensor],
    p: Optional[torch.Tensor],
175
176
177
) -> torch.Tensor:
    """Apply top-k and top-p masks to the logits.

178
179
180
181
    If a top-p is used, this function will sort the logits tensor,
    which can be slow for large batches.

    The logits tensor may be updated in-place.
182
    """
183
184
185
186
187
188
189
    if p is None:
        if k is None:
            return logits

        # Avoid sorting vocab for top-k only case.
        return apply_top_k_only(logits, k)

190
191
    logits_sort, logits_idx = logits.sort(dim=-1, descending=False)

192
    if k is not None:
193
        # Apply top-k.
194
        top_k_mask = logits_sort.size(1) - k.to(torch.long)  # shape: B
195
196
197
198
199
        # Get all the top_k values.
        top_k_mask = logits_sort.gather(1, top_k_mask.unsqueeze(dim=1))
        top_k_mask = logits_sort < top_k_mask
        logits_sort.masked_fill_(top_k_mask, -float("inf"))

200
    if p is not None:
201
202
        # Apply top-p.
        probs_sort = logits_sort.softmax(dim=-1)
203
        probs_sum = torch.cumsum(probs_sort, dim=-1, out=probs_sort)
204
205
206
207
208
209
210
211
212
213
        top_p_mask = probs_sum <= 1 - p.unsqueeze(dim=1)
        # at least one
        top_p_mask[:, -1] = False
        logits_sort.masked_fill_(top_p_mask, -float("inf"))

    # Re-sort the probabilities.
    logits = logits_sort.scatter(dim=-1, index=logits_idx, src=logits_sort)
    return logits


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
def apply_top_k_only(
    logits: torch.Tensor,
    k: torch.Tensor,
) -> torch.Tensor:
    """
    Apply top-k mask to the logits.

    This implementation doesn't involve sorting the entire vocab.

    The logits tensor may be updated in-place.
    """
    no_top_k_mask = k == logits.shape[1]
    # Set non-top-k rows to 1 so that we can gather.
    k = k.masked_fill(no_top_k_mask, 1)
    max_top_k = k.max()
    # topk.values tensor has shape [batch_size, max_top_k].
    # Convert top k to 0-based index in range [0, max_top_k).
231
    k_index = k.sub_(1).unsqueeze(1)
232
    top_k_mask = logits.topk(max_top_k, dim=1).values.gather(1, k_index.long())
233
234
235
236
237
238
    # Handle non-topk rows.
    top_k_mask.masked_fill_(no_top_k_mask.unsqueeze(1), -float("inf"))
    logits.masked_fill_(logits < top_k_mask, -float("inf"))
    return logits


239
240
def random_sample(
    probs: torch.Tensor,
241
    generators: dict[int, torch.Generator],
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
) -> torch.Tensor:
    """Randomly sample from the probabilities.

    We use this function instead of torch.multinomial because torch.multinomial
    causes CPU-GPU synchronization.
    """
    q = torch.empty_like(probs)
    # NOTE(woosuk): To batch-process the requests without their own seeds,
    # which is the common case, we first assume that every request does
    # not have its own seed. Then, we overwrite the values for the requests
    # that have their own seeds.
    if len(generators) != probs.shape[0]:
        q.exponential_()
    if generators:
        # TODO(woosuk): This can be slow because we handle each request
        # one by one. Optimize this.
        for i, generator in generators.items():
            q[i].exponential_(generator=generator)
    return probs.div_(q).argmax(dim=-1).view(-1)


def flashinfer_sample(
264
    logits: torch.Tensor,
265
266
    k: Optional[torch.Tensor],
    p: Optional[torch.Tensor],
267
    generators: dict[int, torch.Generator],
268
) -> torch.Tensor:
269
    """Sample from the logits using FlashInfer.
270
271
272
273

    Statistically, this function is equivalent to the `random_sample` function.
    However, this function is faster because it avoids sorting the logits tensor
    via rejection sampling.
274

275
276
277
278
279
280
281
282
    NOTE: The outputs of this function do not necessarily match the outputs of
    the `random_sample` function. It only guarantees that the outputs are
    statistically equivalent.

    NOTE: This function includes CPU-GPU synchronization, while `random_sample`
    does not. Call this function at the end of the forward pass to minimize
    the synchronization overhead.
    """
283
284
    assert not (k is None and p is None)
    if k is None:
285
        # Top-p only.
286
        probs = logits.softmax(dim=-1, dtype=torch.float32)
287
        next_token_ids = flashinfer.sampling.top_p_sampling_from_probs(
288
289
            probs, p, deterministic=True
        )
290
    elif p is None:
291
        # Top-k only.
292
        probs = logits.softmax(dim=-1, dtype=torch.float32)
293
        next_token_ids = flashinfer.sampling.top_k_sampling_from_probs(
294
295
            probs, k, deterministic=True
        )
296
297
    else:
        # Both top-k and top-p.
298
        next_token_ids = flashinfer.sampling.top_k_top_p_sampling_from_logits(
299
300
            logits, k, p, deterministic=True
        )
301

302
    return next_token_ids.view(-1)