pooling_params.py 2.53 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, Optional
5

6
import msgspec
7

8
9
from vllm.sampling_params import RequestOutputKind

10
11
12
if TYPE_CHECKING:
    from vllm.config import ModelConfig

13
14
15
16
17

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

    Attributes:
21
22
        dimensions: Reduce the dimensions of embeddings
                    if model support matryoshka representation.
23
    """
24
25

    dimensions: Optional[int] = None
26

27
    use_cross_encoder: bool = False
28
29
30
31
32
    """Internal use only."""

    logits_processing_needs_token_ids: bool = False
    """Internal use only."""

33
    output_kind: RequestOutputKind = RequestOutputKind.FINAL_ONLY
34
35
36

    def clone(self) -> "PoolingParams":
        """Returns a deep copy of the PoolingParams instance."""
37
38
39
40
41
42
        return PoolingParams(
            dimensions=self.dimensions,
            use_cross_encoder=self.use_cross_encoder,
            logits_processing_needs_token_ids=self.
            logits_processing_needs_token_ids,
        )
43
44
45
46
47
48
49
50

    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.')
51
52
53
54
55
56
57
58
59
60

            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:
61
                raise ValueError("Dimensions must be greater than 0")
62
63

    def __repr__(self) -> str:
64
65
66
67
68
69
        return (
            f"PoolingParams("
            f"dimensions={self.dimensions}, "
            f"use_cross_encoder={self.use_cross_encoder}, "
            f"logits_processing_needs_token_ids={self.logits_processing_needs_token_ids})"
        )
70
71
72
73

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