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

4
from copy import deepcopy
5
from typing import TYPE_CHECKING, Annotated, Any, Optional
6

7
import msgspec
8

9
from vllm.sampling_params import RequestOutputKind
10
from vllm.tasks import PoolingTask
11

12
if TYPE_CHECKING:
13
    from vllm.config import ModelConfig, PoolerConfig
14

15
16

class PoolingParams(
17
18
19
20
    msgspec.Struct,
    omit_defaults=True,  # type: ignore[call-arg]
    array_like=True,
):  # type: ignore[call-arg]
21
    """API parameters for pooling models.
22
23

    Attributes:
24
25
26
        truncate_prompt_tokens: Controls prompt truncation.
            Set to -1 to use the model's default truncation size.
            Set to k to keep only the last k tokens (left truncation).
27
            Set to None to disable truncation.
28
        normalize: Whether to normalize the embeddings outputs.
29
        dimensions: Reduce the dimensions of embeddings
30
            if model support matryoshka representation.
31
        activation: Whether to apply activation function to
32
            the classification outputs.
33
    """
34
35

    # --8<-- [start:common-pooling-params]
36
    truncate_prompt_tokens: Annotated[int, msgspec.Meta(ge=-1)] | None = None
37
    # --8<-- [end:common-pooling-params]
38

39
    ## for embeddings models
40
    # --8<-- [start:embedding-pooling-params]
41
42
    dimensions: int | None = None
    normalize: bool | None = None
43
    # --8<-- [end:embedding-pooling-params]
44

45
46
    ## for classification, scoring and rerank
    # --8<-- [start:classification-pooling-params]
47
    activation: bool | None = None
48
    # --8<-- [end:classification-pooling-params]
49

50
    ## for step pooling models
51
52
    step_tag_id: int | None = None
    returned_token_ids: list[int] | None = None
53

54
    ## Internal use only
55
    task: PoolingTask | None = None
56
    requires_token_ids: bool = False
57
    extra_kwargs: dict[str, Any] | None = None
58
59
60
61
    output_kind: RequestOutputKind = RequestOutputKind.FINAL_ONLY

    @property
    def all_parameters(self) -> list[str]:
62
        return ["dimensions", "normalize", "activation"]
63
64
65
66
67
68
69

    @property
    def valid_parameters(self):
        return {
            "embed": ["dimensions", "normalize"],
            "classify": ["activation"],
            "score": ["activation"],
70
71
            "token_embed": ["dimensions", "normalize"],
            "token_classify": ["activation"],
72
73
        }

74
75
    def clone(self) -> "PoolingParams":
        """Returns a deep copy of the PoolingParams instance."""
76
77
        return deepcopy(self)

78
79
80
    def verify(
        self, task: PoolingTask, model_config: Optional["ModelConfig"] = None
    ) -> None:
81
82
83
84
85
86
        if self.task is None:
            self.task = task
        elif self.task != task:
            msg = f"You cannot overwrite {self.task=!r} with {task=!r}!"
            raise ValueError(msg)

87
88
89
90
91
        # plugin task uses io_processor.parse_request to verify inputs,
        # skipping PoolingParams verify
        if self.task == "plugin":
            return

92
93
94
        # NOTE: Task validation needs to done against the model instance,
        # which is not available in model config. So, it's not included
        # in this method
95
96
97
98
        self._merge_default_parameters(model_config)
        self._set_default_parameters(model_config)
        self._verify_valid_parameters()

99
100
101
    def _merge_default_parameters(
        self, model_config: Optional["ModelConfig"] = None
    ) -> None:
102
103
        if model_config is None:
            return
104

105
106
107
108
109
110
111
112
113
114
115
116
117
118
        pooler_config = model_config.pooler_config
        if pooler_config is None:
            return

        assert self.task is not None, "task must be set"
        valid_parameters = self.valid_parameters[self.task]

        for k in valid_parameters:
            if getattr(pooler_config, k, None) is None:
                continue

            if getattr(self, k, None) is None:
                setattr(self, k, getattr(pooler_config, k))

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
        self._verify_step_pooling(pooler_config, valid_parameters)

    def _verify_step_pooling(
        self, pooler_config: "PoolerConfig", valid_parameters: list[str]
    ):
        step_pooling_parameters = ["step_tag_id", "returned_token_ids"]
        if pooler_config.pooling_type != "STEP":
            invalid_parameters = []
            for k in step_pooling_parameters:
                if getattr(self, k, None) is not None:
                    invalid_parameters.append(k)

            if invalid_parameters:
                raise ValueError(
                    f"Task {self.task} only supports {valid_parameters} "
                    f"parameters, does not support "
                    f"{invalid_parameters} parameters"
                )
        else:
            for k in step_pooling_parameters:
                if getattr(pooler_config, k, None) is None:
                    continue

                if getattr(self, k, None) is None:
                    setattr(self, k, getattr(pooler_config, k))

145
    def _set_default_parameters(self, model_config: Optional["ModelConfig"]):
146
        if self.task in ["embed", "token_embed"]:
147
148
149
150
151
            if self.normalize is None:
                self.normalize = True

            if self.dimensions is not None and model_config is not None:
                if not model_config.is_matryoshka:
152
                    raise ValueError(
153
                        f'Model "{model_config.served_model_name}" does not '
154
155
                        f"support matryoshka representation, "
                        f"changing output dimensions will lead to poor results."
156
157
158
159
160
161
162
                    )

                mds = model_config.matryoshka_dimensions
                if mds is not None:
                    if self.dimensions not in mds:
                        raise ValueError(
                            f'Model "{model_config.served_model_name}" '
163
164
165
166
                            f"only supports {str(mds)} matryoshka dimensions, "
                            f"use other output dimensions will "
                            f"lead to poor results."
                        )
167
168
169
                elif self.dimensions < 1:
                    raise ValueError("Dimensions must be greater than 0")

170
        elif self.task in ["classify", "score", "token_classify"]:
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
            if self.activation is None:
                self.activation = True
        else:
            raise ValueError(f"Unknown pooling task: {self.task}")

    def _verify_valid_parameters(self):
        assert self.task is not None, "task must be set"
        valid_parameters = self.valid_parameters[self.task]
        invalid_parameters = []
        for k in self.all_parameters:
            if k in valid_parameters:
                continue

            if getattr(self, k, None) is not None:
                invalid_parameters.append(k)

        if invalid_parameters:
            raise ValueError(
                f"Task {self.task} only supports {valid_parameters} "
                f"parameters, does not support "
191
192
                f"{invalid_parameters} parameters"
            )
193
194

    def __repr__(self) -> str:
195
196
197
198
199
200
201
202
203
204
205
        return (
            f"PoolingParams("
            f"task={self.task}, "
            f"normalize={self.normalize}, "
            f"dimensions={self.dimensions}, "
            f"activation={self.activation}, "
            f"step_tag_id={self.step_tag_id}, "
            f"returned_token_ids={self.returned_token_ids}, "
            f"requires_token_ids={self.requires_token_ids}, "
            f"extra_kwargs={self.extra_kwargs})"
        )
206
207

    def __post_init__(self) -> None:
208
        assert self.output_kind == RequestOutputKind.FINAL_ONLY, (
209
            "For pooling output_kind has to be FINAL_ONLY"
210
        )