A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 14, 05:30 UTC
[V0 Deprecation] Remove vestigial V0 logits_processors.py file (#27601)
Signed-off-by: Nick Hill <nhill@redhat.com>
Nick Hill committed
Oct 28, 2025 at 04:17 UTC
7a865f2325b948f9b6bc6523b1ab4dfe2aa267a0
1 file changed
-92
vllm/entrypoints/openai/logits_processors.py
deleted
-92
@@ -1,92 +0,0 @@
1
-# SPDX-License-Identifier: Apache-2.0
2
-# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
-
4
-from collections.abc import Iterable
5
-from functools import lru_cache, partial
6
-
7
-import torch
8
-
9
-from vllm.sampling_params import LogitsProcessor
10
-from vllm.transformers_utils.tokenizer import AnyTokenizer
11
-
12
-
13
-class AllowedTokenIdsLogitsProcessor:
14
- """Logits processor for constraining generated tokens to a
15
- specific set of token ids."""
16
-
17
- def __init__(self, allowed_ids: Iterable[int]):
18
- self.allowed_ids: list[int] | None = list(allowed_ids)
19
- self.mask: torch.Tensor | None = None
20
-
21
- def __call__(self, token_ids: list[int], logits: torch.Tensor) -> torch.Tensor:
22
- if self.mask is None:
23
- self.mask = torch.ones(
24
- (logits.shape[-1],), dtype=torch.bool, device=logits.device
25
- )
26
- self.mask[self.allowed_ids] = False
27
- self.allowed_ids = None
28
- logits.masked_fill_(self.mask, float("-inf"))
29
- return logits
30
-
31
-
32
-@lru_cache(maxsize=32)
33
-def _get_allowed_token_ids_logits_processor(
34
- allowed_token_ids: frozenset[int],
35
- vocab_size: int,
36
-) -> LogitsProcessor:
37
- if not allowed_token_ids:
38
- raise ValueError("Empty allowed_token_ids provided")
39
- if not all(0 <= tid < vocab_size for tid in allowed_token_ids):
40
- raise ValueError("allowed_token_ids contains out-of-vocab token id")
41
- return AllowedTokenIdsLogitsProcessor(allowed_token_ids)
42
-
43
-
44
-def logit_bias_logits_processor(
45
- logit_bias: dict[int, float],
46
- token_ids: list[int],
47
- logits: torch.Tensor,
48
-) -> torch.Tensor:
49
- for token_id, bias in logit_bias.items():
50
- logits[token_id] += bias
51
- return logits
52
-
53
-
54
-def get_logits_processors(
55
- logit_bias: dict[int, float] | dict[str, float] | None,
56
- allowed_token_ids: list[int] | None,
57
- tokenizer: AnyTokenizer,
58
-) -> list[LogitsProcessor]:
59
- logits_processors: list[LogitsProcessor] = []
60
- if logit_bias:
61
- try:
62
- # Convert token_id to integer
63
- # Clamp the bias between -100 and 100 per OpenAI API spec
64
- clamped_logit_bias: dict[int, float] = {
65
- int(token_id): min(100.0, max(-100.0, bias))
66
- for token_id, bias in logit_bias.items()
67
- }
68
- except ValueError as exc:
69
- raise ValueError(
70
- "Found token_id in logit_bias that is not "
71
- "an integer or string representing an integer"
72
- ) from exc
73
-
74
- # Check if token_id is within the vocab size
75
- for token_id, bias in clamped_logit_bias.items():
76
- if token_id < 0 or token_id >= len(tokenizer):
77
- raise ValueError(
78
- f"token_id {token_id} in logit_bias contains out-of-vocab token id"
79
- )
80
-
81
- logits_processors.append(
82
- partial(logit_bias_logits_processor, clamped_logit_bias)
83
- )
84
-
85
- if allowed_token_ids is not None:
86
- logits_processors.append(
87
- _get_allowed_token_ids_logits_processor(
88
- frozenset(allowed_token_ids), len(tokenizer)
89
- )
90
- )
91
-
92
- return logits_processors