Skip to content

vllm.model_executor.layers.attention.encoder_only_attention

Classes:

EncoderOnlyAttention

Bases: Attention

Encoder attention is a special case that doesn't need a KV Cache.

Methods:

  • __init__ –

    attn_backend replaces the selected backend (e.g. a model's own

Source code in vllm/model_executor/layers/attention/encoder_only_attention.py
class EncoderOnlyAttention(Attention):
    """Encoder attention is a special case that doesn't need a KV Cache."""

    def __init__(
        self,
        num_heads: int,
        head_size: int,
        scale: float,
        cache_config: CacheConfig | None = None,
        attn_type: str | None = None,
        attn_backend: type[AttentionBackend] | None = None,
        **kwargs,
    ):
        """`attn_backend` replaces the selected backend (e.g. a model's own
        subclass); it is wrapped for non-causal metadata like the default.
        A given backend skips the backend selector and its validation, and
        overrides `--attention-backend`: checking that it supports the head
        size, dtype, KV cache dtype and platform is the caller's
        responsibility. A warning names the backend that is used. Its
        `get_name()` must be the name of a registered `AttentionBackendEnum`
        member.

        Of the in-tree models, only the BERT classes (`BertModel`,
        `BertPoolingModel` and their layers) pass `attn_backend` through."""
        dtype = torch.get_default_dtype()

        if cache_config is not None:
            kv_cache_dtype = cache_config.cache_dtype
        else:
            kv_cache_dtype = "auto"

        if attn_backend is None:
            attn_backend = get_attn_backend(
                head_size,
                dtype,
                kv_cache_dtype,
                attn_type=AttentionType.ENCODER_ONLY,
            )
        else:
            _warn_given_attn_backend(attn_backend)

        if attn_type is not None:
            assert attn_type == AttentionType.ENCODER_ONLY, (
                "EncoderOnlyAttention only supports AttentionType.ENCODER_ONLY"
            )

        super().__init__(
            num_heads=num_heads,
            head_size=head_size,
            scale=scale,
            cache_config=cache_config,
            attn_backend=create_encoder_only_attention_backend(attn_backend),
            attn_type=AttentionType.ENCODER_ONLY,
            **kwargs,
        )

    def get_kv_cache_config(self, vllm_config: VllmConfig) -> KVCacheLayerConfig | None:
        # Does not need KV cache
        return None

__init__(num_heads, head_size, scale, cache_config=None, attn_type=None, attn_backend=None, **kwargs)

attn_backend replaces the selected backend (e.g. a model's own subclass); it is wrapped for non-causal metadata like the default. A given backend skips the backend selector and its validation, and overrides --attention-backend: checking that it supports the head size, dtype, KV cache dtype and platform is the caller's responsibility. A warning names the backend that is used. Its get_name() must be the name of a registered AttentionBackendEnum member.

Of the in-tree models, only the BERT classes (BertModel, BertPoolingModel and their layers) pass attn_backend through.

Source code in vllm/model_executor/layers/attention/encoder_only_attention.py
def __init__(
    self,
    num_heads: int,
    head_size: int,
    scale: float,
    cache_config: CacheConfig | None = None,
    attn_type: str | None = None,
    attn_backend: type[AttentionBackend] | None = None,
    **kwargs,
):
    """`attn_backend` replaces the selected backend (e.g. a model's own
    subclass); it is wrapped for non-causal metadata like the default.
    A given backend skips the backend selector and its validation, and
    overrides `--attention-backend`: checking that it supports the head
    size, dtype, KV cache dtype and platform is the caller's
    responsibility. A warning names the backend that is used. Its
    `get_name()` must be the name of a registered `AttentionBackendEnum`
    member.

    Of the in-tree models, only the BERT classes (`BertModel`,
    `BertPoolingModel` and their layers) pass `attn_backend` through."""
    dtype = torch.get_default_dtype()

    if cache_config is not None:
        kv_cache_dtype = cache_config.cache_dtype
    else:
        kv_cache_dtype = "auto"

    if attn_backend is None:
        attn_backend = get_attn_backend(
            head_size,
            dtype,
            kv_cache_dtype,
            attn_type=AttentionType.ENCODER_ONLY,
        )
    else:
        _warn_given_attn_backend(attn_backend)

    if attn_type is not None:
        assert attn_type == AttentionType.ENCODER_ONLY, (
            "EncoderOnlyAttention only supports AttentionType.ENCODER_ONLY"
        )

    super().__init__(
        num_heads=num_heads,
        head_size=head_size,
        scale=scale,
        cache_config=cache_config,
        attn_backend=create_encoder_only_attention_backend(attn_backend),
        attn_type=AttentionType.ENCODER_ONLY,
        **kwargs,
    )