Skip to content

vllm.v1.attention.ops.dcp

MLA DCP collective selection and direct symmetric-memory implementations.

Classes:

Functions:

CPTritonContext

The CPTritonContext is used to avoid recompilation of the Triton JIT.

CorrectAttnCPOutKernel

Bases: VllmTritonJitKernel['CorrectAttnCPOutKernel.CompileKey']

Methods:

  • kernel –

    Apply the all-gathered lses to correct each local rank's attention

kernel(outputs_ptr, new_output_ptr, lses_ptr, vlse_ptr, outputs_stride_B, outputs_stride_H, outputs_stride_D, lses_stride_N, lses_stride_B, lses_stride_H, lse_idx, HEAD_DIM, N_ROUNDED, IS_BASE_E) staticmethod

Apply the all-gathered lses to correct each local rank's attention output. we still need perform a cross-rank reduction to obtain the final attention output.

Parameters:

  • outputs_ptr

    (PointerType) –

    Pointer to input tensor of shape [ B, H, D ]

  • lses_ptr

    (PointerType) –

    Pointer to input tensor of shape [ N, B, H ]

  • new_output_ptr

    (PointerType) –

    Pointer to output tensor of shape [ B, H, D ]

  • vlse_ptr

    (PointerType) –

    Pointer to output tensor of shape [ B, H ]

  • outputs_stride_B

    (int) –

    Batch stride of outputs_ptr

  • outputs_stride_H

    (int) –

    Head stride of outputs_ptr

  • outputs_stride_D

    (int) –

    Head-dim stride of outputs_ptr

  • lses_stride_N

    (int) –

    Rank stride of lses_ptr

  • lses_stride_B

    (int) –

    Batch stride of lses_ptr

  • lses_stride_H

    (int) –

    Head stride of lses_ptr

  • lse_idx

    (int) –

    Index of this rank's lse within the all-gathered tensor

  • HEAD_DIM

    (constexpr) –

    Head dimension, as a constexpr

  • N_ROUNDED

    (constexpr) –

    Rank count rounded to a power of two, as a constexpr

  • IS_BASE_E

    (constexpr) –

    Whether the lses are natural-log based, as a constexpr

DirectDCPA2AWorkspace

Bases: DirectCPWorkspace

Persistent symmetric buffers for direct DCP output exchange.

DirectDCPKVGatherWorkspace

Bases: DirectCPWorkspace

Persistent symmetric buffers for direct DCP KV gather.

DirectDCPQGatherWorkspace

Bases: DirectCPWorkspace

Publish query shards directly into the consumer-final symmetric buffer.

The final buffer is reusable after the downstream DCP output combine. That combine orders all ranks after attention has consumed the gathered query.

MLADCPManager

Select and own layer-level collective implementations for MLA DCP.

_cp_lse_common(cp_attn_out, cp_attn_lse, cp_group, ctx=None, is_lse_base_on_e=True, seq_lens=None, query_start_loc=None)

cp_attn_out: [ B, H, D ] cp_attn_lse: [ B, H ]

_lse_weighted_combine(outputs, lses, return_lse=False, is_lse_base_on_e=True)

CPU reference implementation for LSE-weighted combination.

This is a pure PyTorch implementation used for testing and validation.

Parameters:

  • outputs

    (Tensor) –

    Partial attention outputs [N, B, H, D] N = number of KV shards (ranks) B = batch size (num_tokens) H = number of heads per rank D = head dimension

  • lses

    (Tensor) –

    Log-sum-exp values [N, B, H]

  • return_lse

    (bool, default: False ) –

    If True, also return the global LSE

  • is_lse_base_on_e

    (bool, default: True ) –

    If True, LSE is base e; if False, base 2

Returns:

_next_num_seqs_specialization(num_seqs)

Advance to the next num_seqs with a new pack-kernel specialization.

The pack kernel specializes on num_seqs (Triton's == 1 and multiple-of-16 integer classes) and on NUM_SEQS_POW2. Every value up to 16 is visited; above that, each power-of-two bucket (p / 2, p] holds only a generic value (first: p / 2 + 1) and multiples of 16 (first: p / 2 + 16), so any max_num_seqs cap keeps every reachable class.

_validate_dcp_empty_shard_args(seq_lens, query_start_loc)

Validate the empty-shard mask inputs.

Returns True when masking is requested, False when it is disabled. Raises on an inconsistent pair. Performs no device work so it is safe to call from a CUDA-graph-captured region.

correct_attn_out(out, lses, cp_rank, ctx, is_lse_base_on_e=True)

Correct the attention output using the all-gathered lses.

Parameters:

  • out

    (Tensor) –

    Tensor of shape [ B, H, D ]

  • lses

    (Tensor) –

    Tensor of shape [ N, B, H ]

  • cp_rank

    (int) –

    Current rank in the context-parallel group

  • ctx

    (CPTritonContext) –

    Triton context to avoid recompilation

  • is_lse_base_on_e

    (bool, default: True ) –

    Whether the lses use base e rather than base 2

Returns:

  • tuple[Tensor, Tensor] –

    Tuple of (out, lse) with corrected attention and final log-sum-exp.

cp_lse_ag_out_ar(cp_attn_out, cp_attn_lse, cp_group, ctx=None, return_lse=False, is_lse_base_on_e=True, seq_lens=None, query_start_loc=None)

cp_attn_out: [ B, H, D ] cp_attn_lse: [ B, H ]

cp_lse_ag_out_rs(cp_attn_out, cp_attn_lse, cp_group, ctx=None, return_lse=False, is_lse_base_on_e=True, seq_lens=None, query_start_loc=None)

cp_attn_out: [ B, H, D ] cp_attn_lse: [ B, H ]

dcp_a2a_lse_reduce(cp_attn_out, cp_attn_lse, cp_group, ctx=None, return_lse=False, is_lse_base_on_e=True, seq_lens=None, query_start_loc=None, out=None)

Combine partial attention outputs across DCP ranks using All-to-All.

The output and LSE are packed into a single output-dtype buffer, sent with one All-to-All, then unpacked and combined with exact LSE weighting.

Parameters:

  • cp_attn_out

    (Tensor) –

    [B, H, D] where B=num_tokens, H=total_heads, D=head_dim

  • cp_attn_lse

    (Tensor) –

    [B, H] floating-point log-sum-exp values

  • cp_group

    (GroupCoordinator) –

    GroupCoordinator for DCP communication

  • ctx

    (CPTritonContext | None, default: None ) –

    CPTritonContext (unused, for signature compatibility)

  • return_lse

    (bool, default: False ) –

    If True, also return the combined global LSE

  • is_lse_base_on_e

    (bool, default: True ) –

    If True, LSE is base e; if False, base 2

  • seq_lens

    (Tensor | None, default: None ) –

    Local KV lengths. Empty shards contribute zero weight.

  • query_start_loc

    (Tensor | None, default: None ) –

    Cumulative query-token offsets for each request.

  • out

    (Tensor | None, default: None ) –

    Optional [B, H/N, D] buffer the combined output is written to.

Returns:

dcp_a2a_v_up_proj_lse_reduce(cp_attn_out, cp_attn_lse, w_uv, out, cp_group, is_lse_base_on_e=True, seq_lens=None, query_start_loc=None)

Combine partial MLA outputs with All-to-All and up-project them by W_UV.

The LSE merge is a per-head weighted sum and W_UV is linear per head, so up-projecting first matches dcp_a2a_lse_reduce followed by the up-projection up to rounding, while the All-to-All carries V instead of L values per head. Below _V_UP_PROJ_FIRST_MIN_TOKENS tokens it merges first and projects only this rank's heads instead.

Parameters:

  • cp_attn_out

    (Tensor) –

    [B, H, L] partial latent outputs, H = total heads

  • cp_attn_lse

    (Tensor) –

    [B, H] floating-point log-sum-exp values

  • w_uv

    (Tensor) –

    [H, L, V] up-projection of all H heads, in DCP rank order

  • out

    (Tensor) –

    [B, H/N * V] combined output of this rank's heads

  • cp_group

    (GroupCoordinator) –

    GroupCoordinator for DCP communication

  • is_lse_base_on_e

    (bool, default: True ) –

    If True, LSE is base e; if False, base 2

  • seq_lens

    (Tensor | None, default: None ) –

    Local KV lengths. Empty shards contribute zero weight.

  • query_start_loc

    (Tensor | None, default: None ) –

    Cumulative query-token offsets for each request.

reserve_query_head_storage(query, padded_num_heads)

Reserve backing storage for fixed-head decode kernels.