Skip to content

vllm.model_executor.kernels.attention.dsa.dcp_indexer_rocm

ROCm DCP candidates ordered by descending FP32 score, then ascending token id.

Functions:

pack_dcp_candidates(logits, indices, rank, world_size, interleave, row_starts=None)

Pack each local candidate into one sortable int64 collective element.

Source code in vllm/model_executor/kernels/attention/dsa/dcp_indexer_rocm.py
def pack_dcp_candidates(
    logits: torch.Tensor,
    indices: torch.Tensor,
    rank: int,
    world_size: int,
    interleave: int,
    row_starts: torch.Tensor | None = None,
) -> torch.Tensor:
    """Pack each local candidate into one sortable int64 collective element."""
    assert logits.dtype == torch.float32 and indices.dtype == torch.int32
    assert 0 <= rank < world_size and interleave > 0
    invalid = torch.iinfo(torch.int64).min
    if logits.shape[1] == 0:
        return torch.full_like(indices, invalid, dtype=torch.int64)
    output = torch.empty(indices.shape, dtype=torch.int64, device=indices.device)
    if output.numel() == 0:
        return output
    _pack_candidates_kernel[(triton.cdiv(output.numel(), 256),)](
        logits,
        indices,
        row_starts if row_starts is not None else logits,
        output,
        rank,
        *indices.shape,
        logits.shape[1],
        *logits.stride(),
        *indices.stride(),
        row_starts.stride(0) if row_starts is not None else 0,
        world_size,
        interleave,
        row_starts is not None,
        BLOCK=256,
    )
    return output

select_dcp_candidates(gathered, out)

Select global candidates without floating-point conversion of token ids.

The selected set follows the packed (score, token id) order, but the ids are written in no particular order within a row.

Source code in vllm/model_executor/kernels/attention/dsa/dcp_indexer_rocm.py
def select_dcp_candidates(gathered: torch.Tensor, out: torch.Tensor) -> None:
    """Select global candidates without floating-point conversion of token ids.

    The selected set follows the packed (score, token id) order, but the ids are
    written in no particular order within a row.
    """
    if not out.numel():
        return
    count = min(out.shape[1], gathered.shape[1])
    if count == 0:
        out.fill_(-1)
        return
    keys = gathered.topk(count, dim=-1, sorted=False).values
    _decode_selected_candidates[(out.shape[0], triton.cdiv(out.shape[1], 256))](
        keys,
        out,
        out.shape[1],
        count,
        keys.stride(0),
        out.stride(0),
        out.stride(1),
        B=256,
    )