Skip to content

vllm.models.kimi_k3.nvidia.ops.third_party.kda.chunk_intra_token_parallel

Functions:

chunk_kda_fwd_intra_token_parallel(q, k, gk, beta, Aqk, Akk, scale, cu_seqlens=None, chunk_size=64, sub_chunk_size=16)

Token-parallel implementation: each token gets its own thread block. Supports both fixed-length and variable-length sequences. Reduces wasted computation on padding.

Writes directly to Aqk and Akk tensors (in-place).

Parameters:

  • q

    (Tensor) –

    [B, T, H, K]

  • k

    (Tensor) –

    [B, T, H, K]

  • gk

    (Tensor) –

    [B, T, HV, K] cumsum of gates (HV >= H for GVA)

  • beta

    (Tensor) –

    [B, T, HV]

  • Aqk

    (Tensor) –

    [B, T, HV, BT] output tensor to write to

  • Akk

    (Tensor) –

    [B, T, HV, BC] output tensor for diagonal blocks (fp32)

  • scale

    (float) –

    attention scale

  • chunk_size

    (int, default: 64 ) –

    BT (default 64)

  • sub_chunk_size

    (int, default: 16 ) –

    BC (default 16)

Source code in vllm/models/kimi_k3/nvidia/ops/third_party/kda/chunk_intra_token_parallel.py
def chunk_kda_fwd_intra_token_parallel(
    q: torch.Tensor,
    k: torch.Tensor,
    gk: torch.Tensor,
    beta: torch.Tensor,
    Aqk: torch.Tensor,
    Akk: torch.Tensor,
    scale: float,
    cu_seqlens: torch.LongTensor | None = None,
    chunk_size: int = 64,
    sub_chunk_size: int = 16,
) -> None:
    """
    Token-parallel implementation: each token gets its own thread block.
    Supports both fixed-length and variable-length sequences.
    Reduces wasted computation on padding.

    Writes directly to Aqk and Akk tensors (in-place).

    Args:
        q: [B, T, H, K]
        k: [B, T, H, K]
        gk: [B, T, HV, K] cumsum of gates (HV >= H for GVA)
        beta: [B, T, HV]
        Aqk: [B, T, HV, BT] output tensor to write to
        Akk: [B, T, HV, BC] output tensor for diagonal blocks (fp32)
        scale: attention scale
        chunk_size: BT (default 64)
        sub_chunk_size: BC (default 16)
    """
    B, T, H, K, HV = *q.shape, gk.shape[2]
    N = len(cu_seqlens) - 1 if cu_seqlens is not None else B
    BT = chunk_size
    BC = sub_chunk_size

    def grid(meta): return (B * T, triton.cdiv(HV, meta['BH']))
    chunk_kda_fwd_kernel_intra_token_parallel[grid](
        q=q,
        k=k,
        g=gk,
        beta=beta,
        Aqk=Aqk,
        Akk=Akk,
        scale=scale,
        cu_seqlens=cu_seqlens,
        N=N,
        T=T,
        H=H,
        HV=HV,
        K=K,
        BT=BT,
        BC=BC,
    )
    return Aqk, Akk