110 lines
3.3 KiB
Python
110 lines
3.3 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
"""ROCm Kimi-K3 specialization of GDN attention metadata.
|
|
|
|
The request classification and cudagraph staging intentionally mirror
|
|
``GDNAttentionMetadataBuilder``. Only the FLA chunk metadata is built
|
|
differently on device rather than on the host.
|
|
"""
|
|
|
|
import torch
|
|
|
|
from vllm.logger import init_logger
|
|
from vllm.third_party.flash_linear_attention.ops.utils import FLA_CHUNK_SIZE
|
|
from vllm.triton_utils import tl, triton
|
|
from vllm.utils.math_utils import next_power_of_2
|
|
from vllm.v1.attention.backends.gdn_attn import (
|
|
GDNAttentionBackend,
|
|
GDNAttentionMetadataBuilder,
|
|
)
|
|
|
|
logger = init_logger(__name__)
|
|
|
|
_BLOCK_T = 256
|
|
_MIN_BLOCK_N = 128
|
|
|
|
|
|
@triton.jit(do_not_specialize=["N"])
|
|
def _chunk_metadata_kernel(
|
|
cu_seqlens,
|
|
chunk_indices,
|
|
chunk_offsets,
|
|
N,
|
|
BT: tl.constexpr,
|
|
BLOCK_N: tl.constexpr,
|
|
BLOCK_T: tl.constexpr,
|
|
):
|
|
i_n = tl.program_id(0)
|
|
offs_n = tl.arange(0, BLOCK_N)
|
|
is_seq = offs_n < N
|
|
bos = tl.load(cu_seqlens + offs_n, mask=is_seq, other=0).to(tl.int32)
|
|
eos = tl.load(cu_seqlens + offs_n + 1, mask=is_seq, other=0).to(tl.int32)
|
|
nt = tl.where(is_seq, tl.cdiv(eos - bos, BT), 0)
|
|
|
|
base = tl.sum(tl.where(offs_n < i_n, nt, 0))
|
|
num_chunks = tl.sum(tl.where(offs_n == i_n, nt, 0))
|
|
|
|
tl.store(chunk_offsets + i_n, base)
|
|
if i_n == 0:
|
|
tl.store(chunk_offsets + N, tl.sum(nt))
|
|
|
|
for t0 in range(0, num_chunks, BLOCK_T):
|
|
offs_t = t0 + tl.arange(0, BLOCK_T)
|
|
mask_t = offs_t < num_chunks
|
|
row = (base + offs_t) * 2
|
|
tl.store(chunk_indices + row, tl.full([BLOCK_T], i_n, tl.int32), mask=mask_t)
|
|
tl.store(chunk_indices + row + 1, offs_t.to(tl.int32), mask=mask_t)
|
|
|
|
|
|
def prepare_chunk_metadata_device(
|
|
cu_seqlens: torch.Tensor,
|
|
cu_seqlens_cpu: torch.Tensor,
|
|
chunk_size: int,
|
|
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
"""Build FLA chunk metadata on device, with no host<->device transfer."""
|
|
num_seqs = cu_seqlens_cpu.numel() - 1
|
|
seq_lens = cu_seqlens_cpu[1:] - cu_seqlens_cpu[:-1]
|
|
num_chunks = int(((seq_lens + chunk_size - 1) // chunk_size).sum())
|
|
|
|
chunk_indices = torch.empty(
|
|
num_chunks, 2, dtype=cu_seqlens.dtype, device=cu_seqlens.device
|
|
)
|
|
chunk_offsets = torch.empty(
|
|
num_seqs + 1, dtype=torch.int64, device=cu_seqlens.device
|
|
)
|
|
_chunk_metadata_kernel[(num_seqs,)](
|
|
cu_seqlens,
|
|
chunk_indices,
|
|
chunk_offsets,
|
|
num_seqs,
|
|
BT=chunk_size,
|
|
BLOCK_N=max(_MIN_BLOCK_N, next_power_of_2(num_seqs + 1)),
|
|
BLOCK_T=_BLOCK_T,
|
|
num_warps=4,
|
|
)
|
|
return chunk_indices, chunk_offsets
|
|
|
|
|
|
class KimiK3ROCmKDAMetadataBuilder(GDNAttentionMetadataBuilder):
|
|
def _build_chunk_metadata(
|
|
self,
|
|
prefill_query_start_loc: torch.Tensor,
|
|
prefill_query_start_loc_cpu: torch.Tensor,
|
|
device: torch.device,
|
|
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
return prepare_chunk_metadata_device(
|
|
prefill_query_start_loc,
|
|
prefill_query_start_loc_cpu,
|
|
FLA_CHUNK_SIZE,
|
|
)
|
|
|
|
|
|
class KimiK3ROCmKDABackend(GDNAttentionBackend):
|
|
@staticmethod
|
|
def get_name() -> str:
|
|
return "KIMI_K3_KDA_ROCM"
|
|
|
|
@staticmethod
|
|
def get_builder_cls() -> type[KimiK3ROCmKDAMetadataBuilder]:
|
|
return KimiK3ROCmKDAMetadataBuilder
|