Skip to navigation

nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa

View as MarkdownOpen in Claude

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

NameDescription
_block_keysReturn the forward and backward key-block widths of FA4’s head_dim=256 QSA kernels.
_chunk_kinds_kernelClassify 128-query x CHUNK_KEYS-key chunks of the membership table.
_compact_block_lists_kernelMerge chunk kinds into FA4 block kinds and write one sorted block list per program.
_load_fa4Cache optional FA4 entry points and the mask callback, never tensors.
_preprocessBuild a byte membership table and FA4 forward/reverse block lists with Triton.
_scatter_routes_kernelWrite one query row of the membership table: zeros, then ones at every valid route.
fa4_sparse_gqa_attentionEvaluate exact route-set GQA using FA4 on SM90 or SM100-family GPUs.

Data

HAVE_TRITON

_KEYS_PER_CHUNK_PROGRAM

_QUERY_BLOCK

API

nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa._block_keys(
device: torch.device
) -> tuple[int, int]

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:

device
torch.device

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.
nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa._chunk_kinds_kernel(
membership,
kinds,
queries,
membership_row_stride,
row_blocks,
chunks,
CHUNK_KEYS: tl.constexpr,
CHUNKS_PER_PROGRAM: tl.constexpr,
ROWS: tl.constexpr
)

Classify 128-query x CHUNK_KEYS-key chunks of the membership table.

Parameters:

membership

Uint8 tensor [batch * queries, membership_row_stride] of zeros and ones whose row padding past the last key is zero.

kinds

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.

nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa._compact_block_lists_kernel(
kinds,
partial_counts,
partial_ids,
full_counts,
full_ids,
lists,
columns,
row_blocks,
chunks,
LIST_ROW_BLOCK: tl.constexpr,
COLUMN_ROW_BLOCK: tl.constexpr,
LIST_CHUNK: tl.constexpr,
COLUMN_CHUNK: tl.constexpr,
SPAN: tl.constexpr,
BLOCK_COLUMNS: tl.constexpr,
COLUMN_TILES: tl.constexpr
)

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:

kinds

Int8 tensor [batch, row_blocks, chunks] from _chunk_kinds_kernel.

partial_counts

Int32 tensor [batch, 1, lists] receiving the partial-block counts.

partial_ids

Int32 tensor [batch, 1, lists, columns] receiving the partial columns in ascending order, then columns as padding.

full_counts

Int32 tensor [batch, 1, lists] receiving the full-block counts.

full_ids

Int32 tensor [batch, 1, lists, columns] laid out like partial_ids.

nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa._load_fa4() -> tuple[collections.abc.Callable, type, collections.abc.Callable]

Cache optional FA4 entry points and the mask callback, never tensors.

nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa._preprocess(
routes: torch.Tensor,
kv_length: int,
forward_block_keys: int,
backward_block_keys: int
) -> tuple[torch.Tensor, ...]

Build a byte membership table and FA4 forward/reverse block lists with Triton.

Parameters:

routes
torch.Tensor

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.

kv_length
int

Positive number of physical key/value positions.

forward_block_keys
int

Keys per forward block (see _block_keys).

backward_block_keys
int

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.
nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa._scatter_routes_kernel(
routes,
membership,
queries,
width,
kv_length,
route_batch_stride,
route_query_stride,
route_width_stride,
membership_row_stride,
BLOCK: tl.constexpr,
ROW_BLOCK: tl.constexpr
)

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:

routes

Signed int32/int64 tensor [batch, queries, width] with arbitrary strides.

membership

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.

nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa.fa4_sparse_gqa_attention(
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
selected_token_ids: torch.Tensor,
softmax_scale: float | None = None
) -> torch.Tensor

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:

query
torch.Tensor

BF16 CUDA tensor [batch, queries, query_heads, 256]. Arbitrary strides are accepted; non-unit final strides are copied.

key
torch.Tensor

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.

value
torch.Tensor

BF16 CUDA tensor with key’s shape and device.

selected_token_ids
torch.Tensor

Signed int32/int64 CUDA tensor [batch, queries, routes_per_query] in physical K/V coordinates. Noncontiguous inputs are accepted. Dimensions must be nonempty.

softmax_scale
float | NoneDefaults to None

Finite positive score multiplier; defaults to 1/sqrt(256).

Returns: torch.Tensor

Independent BF16 CUDA tensor [batch, queries, query_heads, 256].

nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa.HAVE_TRITON: bool = bool(_HAVE_TRITON and _HAVE_TRITON_LANGUAGE)
nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa._KEYS_PER_CHUNK_PROGRAM = 256
nemo_automodel.components.models.qwen3_8_flash_next.fa4_qsa._QUERY_BLOCK = 128