Skip to content

vllm.models.hy_v4_flash.amd.swa

ROCm sparse MLA backends and causal window indexing for HYV4 Flash.

The AITER sparse path reads each row's first paged_kv_indptr delta entries and maps an invalid index to slot 0 rather than skipping it, so every indexer must pack its live indices to the front of the row, and the builder must count exactly the live entries of each row.

Functions:

  • live_counts –

    Live top-k entries per query token, given its logical position.

  • window_indices –

    Causal window indices packed to the front of each row.

live_counts(seq_lens, query_start_loc, num_tokens, max_query_len, window, ratio, group_budget)

Live top-k entries per query token, given its logical position.

A window row holds min(pos + 1, window) entries. A compressed row holds up to group_budget whole history groups plus the pos % ratio + 1 tokens of its open group, which is pos + 1 until the budget saturates.

Source code in vllm/models/hy_v4_flash/amd/swa.py
def live_counts(
    seq_lens: torch.Tensor,
    query_start_loc: torch.Tensor,
    num_tokens: int,
    max_query_len: int,
    window: int,
    ratio: int,
    group_budget: int,
) -> torch.Tensor:
    """Live top-k entries per query token, given its logical position.

    A window row holds `min(pos + 1, window)` entries. A compressed row holds
    up to `group_budget` whole history groups plus the `pos % ratio + 1`
    tokens of its open group, which is `pos + 1` until the budget saturates.
    """
    out = torch.zeros(num_tokens, dtype=torch.int32, device=seq_lens.device)
    num_reqs = query_start_loc.numel() - 1
    if num_tokens == 0 or num_reqs == 0:
        return out
    block = 64
    _live_counts_kernel[(num_reqs, triton.cdiv(max_query_len, block))](
        seq_lens,
        query_start_loc,
        out,
        WINDOW=window,
        RATIO=ratio,
        GROUP_BUDGET=group_budget,
        BLOCK=block,
    )
    return out

window_indices(positions, window, width)

Causal window indices packed to the front of each row.

Source code in vllm/models/hy_v4_flash/amd/swa.py
def window_indices(positions: torch.Tensor, window: int, width: int) -> torch.Tensor:
    """Causal window indices packed to the front of each row."""
    if window < 1 or width < window:
        raise ValueError("SWA index width must cover its causal window")
    columns = torch.arange(width, device=positions.device)
    first = (positions - window + 1).clamp_min(0)
    count = (positions + 1).clamp(0, window)
    indices = first[:, None] + columns
    live = (positions[:, None] >= 0) & (columns < count[:, None])
    return torch.where(live, indices, -1).int()