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

4
from typing import TYPE_CHECKING, Any, Optional
5

6
import msgspec
7

8
9
10
if TYPE_CHECKING:
    from vllm.config import ModelConfig

11
12
13
14
15

class PoolingParams(
        msgspec.Struct,
        omit_defaults=True,  # type: ignore[call-arg]
        array_like=True):  # type: ignore[call-arg]
16
    """API parameters for pooling models. This is currently a placeholder.
17
18

    Attributes:
19
20
        dimensions: Reduce the dimensions of embeddings
                    if model support matryoshka representation.
21
22
        additional_data: Any additional data needed for pooling.
    """
23
24

    dimensions: Optional[int] = None
25
    additional_data: Optional[Any] = None
26
27
28

    def clone(self) -> "PoolingParams":
        """Returns a deep copy of the PoolingParams instance."""
29
30
31
32
33
34
35
36
37
38
        return PoolingParams(dimensions=self.dimensions,
                             additional_data=self.additional_data)

    def verify(self, model_config: "ModelConfig") -> None:
        if self.dimensions is not None:
            if not model_config.is_matryoshka:
                raise ValueError(
                    f'Model "{model_config.served_model_name}" does not '
                    f'support matryoshka representation, '
                    f'changing output dimensions will lead to poor results.')
39
40
41
42
43
44
45
46
47
48

            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}" '
                        f'only supports {str(mds)} matryoshka dimensions, '
                        f'use other output dimensions will '
                        f'lead to poor results.')
            elif self.dimensions < 1:
49
                raise ValueError("Dimensions must be greater than 0")
50
51
52

    def __repr__(self) -> str:
        return (f"PoolingParams("
53
                f"dimensions={self.dimensions}, "
54
                f"additional_metadata={self.additional_data})")