vllm.v1.attention.backends.mla.rocm_aiter_mla
¶
Classes:
-
AiterMLADCPVerifyMetadata–One paged-KV row per verify token for causal DCP verification.
-
AiterMLAHelper–AITER MLA persistent (asm) decode requires a multiple of 16 heads. Unaligned
-
AiterMLAImpl– -
AiterMLAMetadataBuilder–
AiterMLADCPVerifyMetadata
dataclass
¶
One paged-KV row per verify token for causal DCP verification.
Segmented MLA and the gfx942 Triton path both consume this view. Which
kernel runs is AiterMLADecodeMetadata.dcp_route.
AiterMLAHelper
¶
AITER MLA persistent (asm) decode requires a multiple of 16 heads. Unaligned head counts through 128 are padded to the next multiple of 16 by tiling the query heads and slicing to the padded size. Native H24 AITER builds bypass that padding. Small divisors of 16 retain the existing repeat_interleave and strided-unpad behavior. Native and aligned counts pass through without copies.
Methods:
-
dcp_local_verify_row_lens–Local KV length of every verify row, in global-causal order.
-
get_fp8_prefill_num_heads–Head count the FP8 PS asm prefill runs at: the next multiple of 16.
-
has_fp8_non_causal_qlen2_kernel–Whether fp8 (num_heads, qlen=2) folds onto a non-causal ASM kernel.
-
is_valid_fp8_prefill_num_heads–Whether the FP8 PS asm prefill should run at
num_headsper rank. -
qo_indptr_for_uniform_qlen–Build
[0, qlen, 2*qlen, ..., num_reqs*qlen]. -
use_gluon_verify–Whether a small-head multi-token verify uses native Gluon MTP.
dcp_local_verify_row_lens(tot_seq_lens, qlen, dcp_world_size, dcp_rank, cp_interleave)
staticmethod
¶
Local KV length of every verify row, in global-causal order.
Row t of a qlen-token verify attends global positions
[0, seq_len - qlen + t], its own token included, so its local length
is the round-robin count evaluated at that bound. Truncating causally in
global coordinates has to happen before the shard mapping: the tokens a
row drops sit on qlen - 1 - t different ranks, while subtracting the
offset from a request's local length takes one from every rank.
get_fp8_prefill_num_heads(num_heads)
staticmethod
¶
Head count the FP8 PS asm prefill runs at: the next multiple of 16.
Deliberately not get_actual_mla_num_heads. That one carves out
native H24 when _aiter_mla_native_h24_supported(), which probes the
asm decode reducer and metadata. The prefill is a different kernel
pair (mla_prefill_ps_asm_fwd + mla_reduce_v1) with no such
probe, so 24 heads pad to 32 here even on a native-H24 build.
The PS metadata in _init_fp8_prefill_ps_buffers/build() must be
sized with this same function, or the work/reduce maps describe a
different head count than the kernel is handed.
This function itself has no upper bound; the ceiling comes from the
is_valid_num_heads gate, which rejects counts above
_AITER_MAX_PADDED_MLA_HEADS (128) unless they are already
16-aligned. That bound was established for the asm decode padding, so
an architecture with, say, 136 heads per rank would be refused the
prefill here for a reason that was never measured against this kernel
pair. Revisit the constant rather than special-casing prefill.
has_fp8_non_causal_qlen2_kernel(num_heads)
staticmethod
¶
Whether fp8 (num_heads, qlen=2) folds onto a non-causal ASM kernel.
Pinned AITER v0.1.21.post2 folds 32/64/96/128 heads at qlen 2 onto the non-causal 16-head / 4-token kernel. A 16-head (or padded-to-16) qlen-2 block has no matching entry, and padded 48/80/112 keep Q2 after the H16 fold.
is_valid_fp8_prefill_num_heads(num_heads)
staticmethod
¶
Whether the FP8 PS asm prefill should run at num_heads per rank.
Rejects counts that padding to the kernel width would at least double (1-8 heads). At 8 heads (Kimi-K2.x at TP8) the doubled kernel plus its reduce is slower than bf16 flash attention, so those counts keep the bf16 prefill.
qo_indptr_for_uniform_qlen(num_reqs, qlen, device, dtype=torch.int32)
staticmethod
¶
Build [0, qlen, 2*qlen, ..., num_reqs*qlen].
use_gluon_verify(num_heads, max_qo_len, kv_cache_dtype, dcp_world_size=1, causal=True, kv_cache_bytes=None)
staticmethod
¶
Whether a small-head multi-token verify uses native Gluon MTP.
bf16 has no gqa<16, qseqlen>1 asm kernel, so verify goes through
mla_gluon's 4-D MTP entry (q shaped [batch, qlen, nhead, dim])
with use_2d_view=False. fp8 has one via the q-row fold and must not
come here: the MTP path hands Gluon the batch size its fp8 regime
asserts against. A predicate rather than inline in forward_mqa so the
builder sees the same answer the impl acts on.
DCP verify is excluded: its per-row causal windows are served by the segmented path, which Gluon's MTP entry cannot express.
Gluon masks the block causally with no way to turn it off, so a non-causal block takes the padded asm decode whatever the head count -- padding is what gives it a kernel there.
AiterMLAImpl
¶
Bases: MLACommonImpl[AiterMLAMetadata]
Methods:
-
decode_query_padded_num_heads–The asm decode's head count when this step pads the query with
-
forward_mha–Dispatch prefill to the FP8 ASM kernel when available.
_decode_num_heads
property
¶
Return the query-head count after DCP gathering.
_compute_prefill_context(q, kv_c_and_k_pe_cache, attn_metadata, k_scale)
¶
Attend the cached prefix, gathering and expanding it in one kernel
_concat_k_nope_k_pe(k_nope, k_pe)
¶
Build [k_nope | k_pe] with one kernel, fall back to the base copies.
_forward_segmented_dcp_verify(q_nope, q_pe, verify, kv_c_and_k_pe_cache, layer, out_dtype)
¶
Run segmented attention over this rank's shard of every verify row.
Each row's length already covers the current tokens this rank holds, so the cross-rank LSE merge in the MLA layer completes the causal block.
_forward_triton_dcp_verify(q_mla, verify, kv_c_and_k_pe_cache, layer, out_dtype)
¶
gfx942 causal verify using the LDS-safe generic split-KV kernel.
Each verify token is its own row whose causal window is its
row_lens entry. A zero-length row (an empty DCP shard or graph
padding) comes back as a NaN output with an LSE of -inf; the DCP
combine gives it zero weight and never reads the output.
_forward_triton_verify(q_mla, decode, kv_c_and_k_pe_cache, layer)
¶
Causal verify past qlen 4 where no asm decode kernel is correct.
One Triton program covers a block of a request's (verify token, head) rows, so each KV tile is read once for all of them, and each row is bounded by its own causal window. A varlen tail past its real length comes back as zeros.
_gather_context_chunk(chunk, kv_c_and_k_pe_cache, kv_indices, k_scale, out_dtype)
¶
Gather one context chunk and expand it into (k, v).
_mla_fp8_prefill_attn(q, k, v, attn_metadata, out)
¶
Run FP8 MLA prefill via mla_prefill_ps_asm_fwd + mla_reduce_v1.
Q, K, V are already decompressed (post-kv_b_proj), so K and V have
num_heads heads (same as Q) and gqa_ratio=1. Writes the
result in-place to out, which is the [total_q, nhead * v_head_dim]
output buffer supplied by forward_mha; no extra allocation or
copy is required.
decode_query_padded_num_heads(attn_metadata)
¶
The asm decode's head count when this step pads the query with get_mla_padded_q's head repeat. Gluon decode/verify and the DCP routes read the unpadded query; non-divisor counts are tiled, not repeated.
forward_mha(q, kv_c_normed, k_pe, kv_c_and_k_pe_cache, attn_metadata, k_scale, output, output_scale=None)
¶
Dispatch prefill to the FP8 ASM kernel when available.
Falls back to the parent (flash_attn_varlen_func) when FP8
MLA prefill is disabled, the selected prefill backend supersedes
it, PS metadata is missing, or chunked context requires two-pass
merge.
The annotation uses the base MLACommonMetadata to honour LSP
with MLACommonImpl.forward_mha; the AITER builder always
produces AiterMLAMetadata instances at runtime, so we narrow
with isinstance before reading the AITER-specific FP8 fields.
AiterMLAMetadataBuilder
¶
Bases: MLACommonMetadataBuilder[AiterMLAMetadata]
_build_context_chunk_kv_indices(prefill)
¶
Flatten each context chunk's block table into per-token KV indices.
Built here so the expansion runs once per step instead of once per MLA layer.
_build_dcp_verify_row_view(qlen, block_table, dcp_tot_seq_lens)
¶
Build one paged-KV row per verify token for DCP verification.
Every row is a single query (qo_indptr is an arange), so the whole
causal structure is carried by dcp_local_verify_row_lens and the
kernel applies no tail of its own.
_build_fp8_prefill_ps_metadata(metadata, common_attn_metadata)
¶
Build per-batch FP8 MLA prefill PS metadata and attach to metadata.
Called from build() when prefill tokens are present and
FP8 MLA prefill is enabled (auto-detected via
_fp8_mla_prefill_supported()).
_fill_dcp_verify_page_table(row_block_table, block_table, num_reqs, qlen, page_size)
¶
Fill one page list per request and broadcast it across qlen rows.
page_size is the unit the kernel indexes. Segmented MLA uses TILE
subpages; generic Triton uses the physical KV block. When those are
equal, pages_per_block is 1 and this copies block IDs as-is.
Every row of a request shares the request's shard, so the page list is
built once per request and repeated; only row_lens distinguishes the
rows. A DCP rank holds 1/dcp_world_size of the sequence, so the
request's block table is always wider than the pages a row can reach.
_flat_page_index_steps()
¶
Page and row steps for this layer's expanded page indices.
The KV cache is bound only after the builder is made.
_init_fp8_prefill_ps_buffers(max_num_reqs, max_prefill_qlen, max_num_batched_tokens, attn_out_dtype, device)
¶
Pre-allocate persistent buffers for FP8 MLA prefill PS metadata.
Uses get_ps_metadata_info_v1 with max values so the buffers are
large enough for any batch. get_ps_metadata_v1 fills them
per-batch in build(). The FP8 prefill forward path also uses the
global workspace manager for per-call scratch, so reserve its maximum
shape here before the workspace manager is locked after warmup.
Parameters:
-
(max_num_reqs¶int) –Maximum number of concurrent requests.
-
(max_prefill_qlen¶int) –Maximum Q-length for a single request in one prefill batch. Should be
min(max_model_len, max_num_batched_tokens)— a single request never exceedsmax_model_lentokens, nor the per-batch token budget. -
(max_num_batched_tokens¶int) –Maximum number of tokens scheduled in one batch. The
final_lsescratch is sized bytotal_q(the summed Q-length over all prefill requests in the batch), which is bounded by this budget rather than by a single request'smax_prefill_qlen— concurrent requests can sum to more thanmax_model_lenwhenmax_model_len < max_num_batched_tokens. -
(attn_out_dtype¶dtype) –Dtype of the attention output buffer, used to size the padded-head output scratch (small head counts only).
-
(device¶device) –Target device for the buffers.
_aiter_fp8_mla_decode_drops_v_scale(num_heads, qlen, q_fp8)
cached
¶
Whether AITER's FP8 persistent MLA decode leaves V unscaled at a shape.
gfx942's (gqa 16, qseqlen 4) FP8 kernel applies kv_scale to the scores but not to V, so its output is short by exactly kv_scale. Measured on a tiny causal block with kv_scale 2 rather than assumed per arch, so a fixed AITER build turns the correction off by itself (ROCm/aiter#6732).
_aiter_mla_native_h24_metadata_supported()
cached
¶
Whether AITER's fast MLA metadata planner accepts native H24.
The reducer and metadata planner have independent shape dispatch. Checking only the reducer can route H24 into a planner that rejects it before the attention kernel launches. Until AITER exposes a capability API, inspect the shipped JIT source for an explicit native-H24 planner branch.
_aiter_mla_native_h24_reducer_supported()
cached
¶
Whether AITER's JIT reducer supports the native H24/512 shape.
_aiter_mla_native_h24_supported()
¶
Whether the complete AITER decode path supports native H24.
_aiter_mla_non_causal_asm_kernels()
¶
Whether this arch ships non-causal MLA decode ASM kernels.
The Python causal= probe cannot see the per-arch manifest. gfx950 has
the kernels; gfx942 and everything else must fall through to another
backend.
_aiter_mla_small_head_mode()
¶
Small-head (<16) MLA decode kernel selection.
Controlled by VLLM_ROCM_AITER_MLA_ASM_PADDING:
"auto"(default): let the arch decide -- divisor head counts keep the Gluon decode where a build exists (gfx950), everything else (non-divisor counts and all counts on gfx942) uses the padded persistent-scheduling ASM decode."gluon": prefer the Gluon path wherever a build exists."asm": force the padded persistent-scheduling ASM decode.
On gfx942 (no Gluon build) the ASM path is always used regardless of this
setting; "gluon" there falls back to ASM with a one-time warning.
_asm_causal_verify_unsupported(decode_num_heads, mtp_decode_qlen, kv_cache_dtype, dcp_world_size)
¶
Whether CDNA3's asm decode has no correct kernel for causal verify past qlen 4: its persistent kernels stop at qseqlen 4 (bf16, and FP8 below 128 heads) and its non-persistent entries are not usable here (ROCm/aiter#5679). DCP verify keeps its row-view and cprr routes.
_asm_dcp_verify_configured(dcp_world_size, cp_interleave, multi_token_decode)
¶
Whether asm cprr DCP verify is reachable in this process.
Requires DCP with interleave size 1, speculative decoding, and gfx950 (the only arch AITER builds cprr kernels for).
_asm_dcp_verify_heads(decode_num_heads)
¶
Head count the cprr asm decode runs at, or 0 if it cannot serve this shape.
_asm_fp8_prefill_superseded(prefill_backend)
¶
Whether the selected MLA prefill backend supersedes the ASM FP8 prefill.
AiterFlyDSLFP8PrefillBackend runs FP8 attention itself and also serves
context-free prefills, so the assembly prefill kernel must not claim them.
_build_decode_indptr_kernel(seq_lens, query_start_loc, graph_seq_lens, kv_indptr, qo_indptr, num_reqs, buf_len, pad_qo_len, varlen_q_rows, varlen_o_rows, varlen_o_rows_len, PAD, VARLEN, BLOCK, ROW_W, ROW_P2)
¶
Single-program form of the eager full-graph decode indptr build.
Writes the per-request kernel KV lengths to graph_seq_lens[:num_reqs], their zero-prefixed cumsum to kv_indptr and the query indptr to qo_indptr. Both indptr tails, up to buf_len, repeat the last live entry.
With PAD the query indptr is uniform at pad_qo_len, and a request with no query tokens is a dummy row whose KV length is pad_qo_len. VARLEN instead extends every request's KV length by the query rows it is short of pad_qo_len, and also fills the varlen row maps: varlen_q_rows[r] is the packed query row of slot r = req * ROW_W + pos (0 for a padding slot), and varlen_o_rows maps each packed row back to its slot, 0 past the live tokens. The last varlen_o_rows entry is a dump slot nothing reads.
_expand_page_indices_kernel(page_indices, block_table, block_table_stride_0, block_table_stride_1, cu_num_tokens, num_real_tokens, KERNEL_BLOCK_SIZE, BLOCK_SIZE, CLAMP_TAIL=False, HAS_START_OFFSETS=False, start_offsets=None, PAGE_STRIDE=0, ROW_STRIDE=1)
¶
Expand block table entries into per-token flat page indices.
The aiter MLA kernel always operates with page_size=1 internally (kv_buffer is flattened via .view(-1, 1, 1, H)). This kernel converts block-level indices from the block table into individual token positions in the flattened KV buffer.
When KERNEL_BLOCK_SIZE=1: block_idx=t, offset=0, flat=block_id (equivalent to a direct copy -- no regression from the original kernel).
When KERNEL_BLOCK_SIZE=K: block table entry b (covering K tokens) is expanded to flat indices bK, bK+1, ..., b*K+(K-1).
PAGE_STRIDE and ROW_STRIDE replace K and 1 for a packed cache; 0 keeps both.
With CLAMP_TAIL, entries past num_real_tokens[req] (padding a request's page list out to its kernel length) repeat the request's first page, so they never index past its block-table row.
With HAS_START_OFFSETS the request's rows begin start_offsets[req] tokens into its sequence rather than at 0, which is what a chunked-context prefill needs.
_flat_mla_index_steps(cache)
¶
Page step and row step in the units that _flat_mla_kv_view indexes.
_flat_mla_kv_view(cache)
¶
Flat view of a KV cache whose pages may sit inside a wider block row.
Rows of this view overlap. Index it only with _flat_mla_index_steps.
_fp8_mla_prefill_supported()
cached
¶
Auto-detect FP8 MLA prefill via mla_prefill_ps_asm_fwd + mla_reduce_v1.
Requires gfx950 plus an AITER build that exports both kernels. When
either is missing we silently fall back to flash_attn_varlen_func.
_fused_mla_prefill_qkv_supported()
cached
¶
Gate fused_mla_prefill_qkv_fp8 to CDNA4 (gfx950).
Older AITER builds without the kernel fall back to the eager concat/cast.
_get_mla_gluon()
cached
¶
Load the small-head Gluon MLA entry point.
_get_segmented_mla_decode()
cached
¶
Load AITER's segmented MLA decode with unreduced partial output.
_gluon_kv_cache_in_bounds(kv_cache_bytes)
¶
Whether this layer's KV cache keeps Gluon on its bounds-checked path.
The cache is one flat tensor per layer, so the bound disqualifies the layer
outright; no batch shape brings an oversized cache back in range. None
is the not-yet-sized case during profiling, before any kernel runs.
_gluon_mla_decode_supported()
cached
¶
The small-head Gluon MLA decode kernel only has a gfx950 (CDNA4) build.
Its tiling needs ~160 KiB of LDS, which exceeds CDNA3's 64 KiB, so on
gfx942 there is no kernel to fall through to and selecting it asserts
(mla_gluon requires gfx950). Restrict Gluon decode to gfx950; other
archs use the asm persistent decode, which get_mla_padded_q makes
correct for any 1..15 heads.
_is_row_view_dcp_route(route)
¶
Routes that attend one causal window per verify token.
_max_mla_metadata_info(infos)
¶
Per-dimension maximum of several get_mla_metadata_info_v1 results.
_reserve_triton_dcp_verify_workspace(max_rows, num_heads, max_seq_len, kv_lora_rank, sm_count)
¶
Reserve split-KV scratch before warmup locks the workspace manager.
_reserve_triton_verify_workspace(max_num_reqs, qlens, num_heads, max_seq_len, kv_lora_rank, sm_count)
¶
Reserve the folded verify kernel's split scratch before warmup.
_segmented_dcp_verify_supported(dcp_world_size, cp_interleave)
¶
Whether this configuration can serve DCP verify on segmented MLA.
Configuration only -- whether a given batch takes the route additionally depends on its query length. Round-robin interleaving other than 1 is excluded because the per-row causal window has not been validated there.
_segmented_mla_decode_supported()
cached
¶
Whether AITER exposes the segmented MLA decode used by DCP verify.
_segmented_mla_fits_lds(num_heads, block_size, fp8_kv)
¶
Whether AITER segmented MLA compiles for this DCP verify shape.
On gfx942 it needs 128 KiB of LDS, over the 64 KiB available, for bf16 KV
at TILE-128 and for fp8 KV past 64 heads, where its query block widens to
128 heads. num_heads is the DCP-gathered count.
_segmented_mla_page_size(block_size)
¶
Largest supported power-of-two subpage dividing a physical KV block.
The subpage size becomes the segmented kernel's TILE_SIZE: the cache is
reinterpreted as (-1, page_size, 1, head_dim) before the call. It must
divide the physical block and be a power of two, since the kernel walks a
tile with tl.arange(0, TILE_SIZE). 128 is the largest tile that tiling
supports -- a tile holds TILE_SIZE x kv_lora_rank keys and its scores
are BLOCK_M x TILE_SIZE -- so a larger manager block is split into
several subpages instead of widening the tile.
_select_dcp_decode_route(*, supports_segmented, supports_triton, causal, max_qo_len, asm_selected)
¶
Select one DCP decode route for this batch; see DCP decode routing.
_supports_fused_dcp_prefill_config(kv_cache_dtype, query_dtype, kv_lora_rank, qk_nope_head_dim, qk_rope_head_dim, v_head_dim)
¶
Whether the configuration supports fused DCP context expansion.
_triton_dcp_verify_supported(dcp_world_size, cp_interleave)
¶
Whether gfx942 can fall back to generic split-KV causal target verify.
The batch shape is causal qlen>1 over a DCP shard. Eagle MTP and DSpark target verify are that same shape, so this is not limited to one speculative method. gfx942 has no cprr kernel, so this serves the shapes segmented MLA cannot (see _segmented_mla_fits_lds). qlen==1 and non-causal stay on the plain ASM decode.
_triton_verify_num_kv_splits(max_seq_len, num_reqs, qlen, num_heads, sm_count)
¶
KV split cap for the folded verify kernel at this launch shape.
Depends only on values that are static for a captured graph; the kernel lengthens the splits of shorter requests at runtime.