nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa
nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa
SM90 and SM100-family QSA using Triton preprocessing and FlashAttention 4.
Optional FA4/CuTe dependencies are loaded on the first CUDA call. The only CuTe code owned here is the mask callback compiled into FA4’s kernels. Three Triton kernels build FA4’s byte membership table and block lists from the routes: they need no recompilation when the route width changes, and one read of the table classifies both the forward and the backward tiles.
Module Contents
Functions
Data
API
Return the forward and backward key-block widths of FA4’s head_dim=256 QSA kernels.
FA4 checks block-sparse lists against its KV tiles; query blocks are always 128 rows. SM90 runs 128x80 forward and 64x64 backward tiles. The SM100 family runs 128x128 forward tiles and a 2CTA backward whose 64-key tiles must pair up, so both directions use 128 keys there.
Parameters:
CUDA device that runs the attention.
Returns: tuple[int, int]
Keys per forward block and keys per backward block.
Raises:
RuntimeError: When the device is neither SM90 nor compute capability 10.x/11.x.
Classify 128-query x CHUNK_KEYS-key chunks of the membership table.
Parameters:
Uint8 tensor [batch * queries, membership_row_stride] of zeros and ones whose row padding past the last key is zero.
Int8 tensor [batch, row_blocks, chunks] receiving absent (0), partial (1) or full (2). Chunks that reach past the last query or key are never full.
Merge chunk kinds into FA4 block kinds and write one sorted block list per program.
A list’s column c covers query block list * LIST_ROW_BLOCK + c * COLUMN_ROW_BLOCK and
chunks list * LIST_CHUNK + c * COLUMN_CHUNK + [0, SPAN): lists are query blocks and
columns key blocks in the forward direction, and the reverse in the backward direction.
Parameters:
Int8 tensor [batch, row_blocks, chunks] from _chunk_kinds_kernel.
Int32 tensor [batch, 1, lists] receiving the partial-block counts.
Int32 tensor [batch, 1, lists, columns] receiving the partial columns in
ascending order, then columns as padding.
Int32 tensor [batch, 1, lists] receiving the full-block counts.
Int32 tensor [batch, 1, lists, columns] laid out like partial_ids.
Cache optional FA4 entry points and the mask callback, never tensors.
Build a byte membership table and FA4 forward/reverse block lists with Triton.
Parameters:
Signed int32/int64 CUDA tensor [batch, queries, routes_per_query] with arbitrary strides. Duplicate IDs collapse; invalid IDs, including large int64 values, are discarded. routes_per_query may differ between calls without compiling new kernels.
Positive number of physical key/value positions.
Keys per forward block (see _block_keys).
Keys per backward block (see _block_keys).
Returns: torch.Tensor
Uint8 membership [batch, queries, kv_length], with each row padded to a
Raises:
ImportError: When Triton is not installed.
Write one query row of the membership table: zeros, then ones at every valid route.
The row is zeroed by this program right before its routes are marked, so its cache lines are still in L2 when the scattered stores land; they merge there instead of becoming partial-sector writes to DRAM. The indexer emits each selected compressed block as four consecutive keys, and packed documents start at multiples of 64. A route group that covers one aligned 4-key block is stored as a single 32-bit word of ones; every other valid ID is stored as one byte.
Parameters:
Signed int32/int64 tensor [batch, queries, width] with arbitrary strides.
Uninitialized uint8 tensor [batch * queries, membership_row_stride] with a row stride that is a multiple of four; one program writes every byte of one row. IDs outside [0, kv_length) are skipped, and duplicates store one twice.
Evaluate exact route-set GQA using FA4 on SM90 or SM100-family GPUs.
Duplicate routes select a token once. Invalid IDs are ignored. Empty query rows have zero output and query gradients. Routes encode causality, documents and padding; no additional triangular mask is imposed. FA4 owns first-order autograd; higher-order gradients and deterministic backward are unsupported. Discrete preprocessing runs in Triton kernels.
Parameters:
BF16 CUDA tensor [batch, queries, query_heads, 256]. Arbitrary strides are accepted; non-unit final strides are copied.
BF16 CUDA tensor [batch, keys, kv_heads, 256]. May contain gathered global K/V while query is a local CP slice. query_heads must be a positive multiple of kv_heads.
BF16 CUDA tensor with key’s shape and device.
Signed int32/int64 CUDA tensor [batch, queries, routes_per_query] in physical K/V coordinates. Noncontiguous inputs are accepted. Dimensions must be nonempty.
Finite positive score multiplier; defaults to 1/sqrt(256).
Returns: torch.Tensor
Independent BF16 CUDA tensor [batch, queries, query_heads, 256].