Skip to content

vllm.models.hy_v4.amd.attention

HY V4 MLA attention on ROCm.

Reuses the NVIDIA layer and only drops the CUDA-only FLASHMLA_SPARSE sink fallback, so the learnable sink is applied only by a selected backend that supports attn_sink.

Classes:

Functions:

HYV4MLAAttention

Bases: HYV4MLAAttention

Source code in vllm/models/hy_v4/amd/attention.py
class HYV4MLAAttention(_HYV4MLAAttention):
    def _resolve_sink_backend(
        self, kv_cache_dtype: str
    ) -> type[AttentionBackend] | None:
        """Return the selected MLA backend if it can apply the learnable sink.

        Args:
            kv_cache_dtype: The layer's KV cache dtype string.

        Returns:
            The selected backend, or None when it cannot apply sinks; the
            caller then loads the sink weight but disables the bias.

        """
        try:
            selected_cls = get_attn_backend(
                head_size=self.kv_lora_rank + self.qk_rope_head_dim,
                dtype=torch.get_default_dtype(),
                kv_cache_dtype=kv_cache_dtype,
                use_mla=True,
                use_sparse=self.is_sparse,
                num_heads=self.num_local_heads,
            )
        except Exception as exc:
            # Stringify so warning_once dedupes across layers.
            logger.warning_once(
                "HYV4 failed to select an MLA backend for the learnable sink "
                "(%s); the sink parameter is loaded but the sink bias is "
                "disabled.",
                str(exc),
            )
            return None

        if selected_cls.supports_sink():
            return selected_cls

        logger.warning_once(
            "HYV4 learnable sink is unavailable: the selected backend %s cannot "
            "apply sinks. The sink parameter is loaded but the sink bias is "
            "disabled.",
            selected_cls.get_name(),
        )
        return None

_resolve_sink_backend(kv_cache_dtype)

Return the selected MLA backend if it can apply the learnable sink.

Parameters:

  • kv_cache_dtype

    (str) –

    The layer's KV cache dtype string.

Returns:

Source code in vllm/models/hy_v4/amd/attention.py
def _resolve_sink_backend(
    self, kv_cache_dtype: str
) -> type[AttentionBackend] | None:
    """Return the selected MLA backend if it can apply the learnable sink.

    Args:
        kv_cache_dtype: The layer's KV cache dtype string.

    Returns:
        The selected backend, or None when it cannot apply sinks; the
        caller then loads the sink weight but disables the bias.

    """
    try:
        selected_cls = get_attn_backend(
            head_size=self.kv_lora_rank + self.qk_rope_head_dim,
            dtype=torch.get_default_dtype(),
            kv_cache_dtype=kv_cache_dtype,
            use_mla=True,
            use_sparse=self.is_sparse,
            num_heads=self.num_local_heads,
        )
    except Exception as exc:
        # Stringify so warning_once dedupes across layers.
        logger.warning_once(
            "HYV4 failed to select an MLA backend for the learnable sink "
            "(%s); the sink parameter is loaded but the sink bias is "
            "disabled.",
            str(exc),
        )
        return None

    if selected_cls.supports_sink():
        return selected_cls

    logger.warning_once(
        "HYV4 learnable sink is unavailable: the selected backend %s cannot "
        "apply sinks. The sink parameter is loaded but the sink bias is "
        "disabled.",
        selected_cls.get_name(),
    )
    return None

is_skip_topk_indexer_weight(weight_name, skip_topk_layers)

Return whether an indexer weight belongs to a top-k sharing layer.

Parameters:

  • weight_name

    (str) –

    Checkpoint weight name.

  • skip_topk_layers

    (set[int]) –

    Result of compute_skip_topk_layers.

Returns:

  • bool –

    True when the weight is an indexer weight of a layer that has no

  • bool –

    indexer module and therefore must be dropped.

Source code in vllm/models/hy_v4/nvidia/attention.py
def is_skip_topk_indexer_weight(weight_name: str, skip_topk_layers: set[int]) -> bool:
    """Return whether an indexer weight belongs to a top-k sharing layer.

    Args:
        weight_name: Checkpoint weight name.
        skip_topk_layers: Result of `compute_skip_topk_layers`.

    Returns:
        True when the weight is an indexer weight of a layer that has no
        indexer module and therefore must be dropped.

    """
    if ".indexer." not in weight_name or not skip_topk_layers:
        return False
    match = _WEIGHT_LAYER_INDEX_RE.search(weight_name)
    return match is not None and int(match.group(1)) in skip_topk_layers

sparse_layer_ids(config)

Layers that run sparse attention, MTP layers included.

Source code in vllm/models/hy_v4/nvidia/attention.py
def sparse_layer_ids(config: PreTrainedConfig) -> set[int]:
    """Layers that run sparse attention, MTP layers included."""
    if not hasattr(config, "index_topk"):
        return set()
    layer_types = getattr(config, "layer_types", None) or ()
    return {i for i, t in enumerate(layer_types) if t in _SPARSE_LAYER_TYPES}