Skip to content

vllm.pooling_params

PoolingParams

Bases: Struct

API parameters for pooling models.

Attributes:

Name Type Description
normalize Optional[bool]

Whether to normalize the embeddings outputs.

dimensions Optional[int]

Reduce the dimensions of embeddings if model support matryoshka representation.

activation Optional[bool]

Whether to apply activation function to the classification outputs.

softmax Optional[bool]

Whether to apply softmax to the reward outputs.

Source code in vllm/pooling_params.py
class PoolingParams(
        msgspec.Struct,
        omit_defaults=True,  # type: ignore[call-arg]
        array_like=True):  # type: ignore[call-arg]
    """API parameters for pooling models.

    Attributes:
        normalize: Whether to normalize the embeddings outputs.
        dimensions: Reduce the dimensions of embeddings
                    if model support matryoshka representation.
        activation: Whether to apply activation function to
                    the classification outputs.
        softmax: Whether to apply softmax to the reward outputs.
    """

    ## for embeddings models
    dimensions: Optional[int] = None
    normalize: Optional[bool] = None

    ## for classification models
    activation: Optional[bool] = None

    ## for reward models
    softmax: Optional[bool] = None
    step_tag_id: Optional[int] = None
    returned_token_ids: Optional[list[int]] = None

    task: Optional[PoolingTask] = None
    """Internal use only."""

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

    extra_kwargs: Optional[dict[str, Any]] = None
    """Internal use only."""

    output_kind: RequestOutputKind = RequestOutputKind.FINAL_ONLY

    @property
    def all_parameters(self) -> list[str]:
        return [
            "dimensions", "normalize", "activation", "softmax", "step_tag_id",
            "returned_token_ids"
        ]

    @property
    def valid_parameters(self):
        return {
            "embed": ["dimensions", "normalize"],
            "classify": ["activation"],
            "score": ["activation"],
            "encode": ["softmax", "step_tag_id", "returned_token_ids"],
        }

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

    def verify(self,
               task: PoolingTask,
               model_config: Optional["ModelConfig"] = None) -> None:

        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)

        # 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

        self._merge_default_parameters(model_config)
        self._set_default_parameters(model_config)
        self._verify_valid_parameters()

    def _merge_default_parameters(self,
                                  model_config: Optional["ModelConfig"] = None
                                  ) -> None:

        if model_config is None:
            return

        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))

    def _set_default_parameters(self, model_config: Optional["ModelConfig"]):
        if self.task == "embed":
            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:
                    raise ValueError(
                        f'Model "{model_config.served_model_name}" does not '
                        f'support matryoshka representation, '
                        f'changing output dimensions will lead to poor results.'
                    )

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

        elif self.task in ["classify", "score"]:
            if self.activation is None:
                self.activation = True

        elif self.task == "encode":
            if self.softmax is None:
                self.softmax = 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 "
                f"{invalid_parameters} parameters")

    def __repr__(self) -> str:
        return (f"PoolingParams("
                f"task={self.task}, "
                f"normalize={self.normalize}, "
                f"dimensions={self.dimensions}, "
                f"activation={self.activation}, "
                f"softmax={self.softmax}, "
                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})")

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

activation class-attribute instance-attribute

activation: Optional[bool] = None

all_parameters property

all_parameters: list[str]

dimensions class-attribute instance-attribute

dimensions: Optional[int] = None

extra_kwargs class-attribute instance-attribute

extra_kwargs: Optional[dict[str, Any]] = None

Internal use only.

normalize class-attribute instance-attribute

normalize: Optional[bool] = None

output_kind class-attribute instance-attribute

requires_token_ids class-attribute instance-attribute

requires_token_ids: bool = False

Internal use only.

returned_token_ids class-attribute instance-attribute

returned_token_ids: Optional[list[int]] = None

softmax class-attribute instance-attribute

softmax: Optional[bool] = None

step_tag_id class-attribute instance-attribute

step_tag_id: Optional[int] = None

task class-attribute instance-attribute

task: Optional[PoolingTask] = None

Internal use only.

valid_parameters property

valid_parameters

__post_init__

__post_init__() -> None
Source code in vllm/pooling_params.py
def __post_init__(self) -> None:
    assert self.output_kind == RequestOutputKind.FINAL_ONLY,\
        "For pooling output_kind has to be FINAL_ONLY"

__repr__

__repr__() -> str
Source code in vllm/pooling_params.py
def __repr__(self) -> str:
    return (f"PoolingParams("
            f"task={self.task}, "
            f"normalize={self.normalize}, "
            f"dimensions={self.dimensions}, "
            f"activation={self.activation}, "
            f"softmax={self.softmax}, "
            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})")

_merge_default_parameters

_merge_default_parameters(
    model_config: Optional[ModelConfig] = None,
) -> None
Source code in vllm/pooling_params.py
def _merge_default_parameters(self,
                              model_config: Optional["ModelConfig"] = None
                              ) -> None:

    if model_config is None:
        return

    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))

_set_default_parameters

_set_default_parameters(
    model_config: Optional[ModelConfig],
)
Source code in vllm/pooling_params.py
def _set_default_parameters(self, model_config: Optional["ModelConfig"]):
    if self.task == "embed":
        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:
                raise ValueError(
                    f'Model "{model_config.served_model_name}" does not '
                    f'support matryoshka representation, '
                    f'changing output dimensions will lead to poor results.'
                )

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

    elif self.task in ["classify", "score"]:
        if self.activation is None:
            self.activation = True

    elif self.task == "encode":
        if self.softmax is None:
            self.softmax = True
    else:
        raise ValueError(f"Unknown pooling task: {self.task}")

_verify_valid_parameters

_verify_valid_parameters()
Source code in vllm/pooling_params.py
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 "
            f"{invalid_parameters} parameters")

clone

clone() -> PoolingParams

Returns a deep copy of the PoolingParams instance.

Source code in vllm/pooling_params.py
def clone(self) -> "PoolingParams":
    """Returns a deep copy of the PoolingParams instance."""
    return deepcopy(self)

verify

verify(
    task: PoolingTask,
    model_config: Optional[ModelConfig] = None,
) -> None
Source code in vllm/pooling_params.py
def verify(self,
           task: PoolingTask,
           model_config: Optional["ModelConfig"] = None) -> None:

    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)

    # 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

    self._merge_default_parameters(model_config)
    self._set_default_parameters(model_config)
    self._verify_valid_parameters()