nemo_automodel.components.models.step5.fa4
nemo_automodel.components.models.step5.fa4
Step-5 attention through FA4, with compact region membership metadata.
Module Contents
Classes
Functions
API
Bases: NamedTuple
Independent int32 lists in the original provider/head layout.
Tensor with the partial_counts layout.
Tensor with the partial_indices layout.
Tensor [provider_batches, heads, key_tiles].
Tensor [provider_batches, heads, key_tiles, query_tiles].
Copy singleton-key attention while retaining explicit zero Q/K gradients.
Parameters:
Tensor [batch, local_queries, query_heads, head_dim].
Tensor [batch, global_keys, kv_heads, head_dim].
Tensor shaped like key, potentially a noncontiguous KV-unbind view.
The only allowed global key, proven by shared metadata. query_heads must be divisible by kv_heads.
Returns: torch.Tensor
Materialized tensor [batch, local_queries, query_heads, head_dim].
Build block lists without allocating a dense block grid.
Parameters:
Local query length.
Global key length.
Physical global offset of the query shard.
Sliding window size, or None for all causal blocks.
Query block size.
Key block size.
Return key-to-query lists used by backward.
CUDA metadata device.
Returns: torch.Tensor
Int32 counts [1, 1, block_rows] and indices [1, 1, block_rows, width].
Run the existing B1 causal prefix or proven singleton-padding operation.
Parameters:
BF16 tensor [1, local_queries, query_heads, head_dim].
BF16 tensor [1, global_keys, kv_heads, head_dim], possibly strided.
Tensor with the same shape/dtype as key; global storage is shared.
Proven prefix metadata for this one batch row. A zero query count denotes a fully padded row with a valid singleton padding key.
Returns: torch.Tensor
Tensor [1, local_queries, query_heads, head_dim], preserving B1 arithmetic.
Compact a boolean block grid into FA4 partial-block counts and indices.
Load FA4 and cache a mask, optionally specializing a proven document.
canonical_region_size>0 requires one unpadded B1 CP1 document with Q=K, zero query_start and positions exactly 0..K-1. Its token_regions are physical_key // canonical_region_size; learned membership is unchanged.
Load native FA4 causal varlen attention without a custom mask.
Copy provider-specific block metadata into FA4’s query-head axis.
Parameters:
Read-only int32 tensor [batch * groups, 1, query_tiles] for counts, or [batch * groups, 1, query_tiles, key_tiles] for indices. Backward metadata swaps query_tiles and key_tiles. Each provider owns one KV group, with a common number of query heads per group.
Original attention batch size before provider folding.
Number of consecutive query heads sharing a KV group.
Returns: torch.Tensor
Independent contiguous int32 metadata [batch, query_heads, …],
Union selected regions for bounded chunks of 128-query tiles.
Parameters:
Sorted region IDs [batch, groups, queries, slots].
Monotonic region IDs [batch, keys].
Number of global regions; also the unused-route sentinel.
Forward key tile width.
Backward key tile width.
Returns: torch.Tensor
Forward and backward tile-presence grids [batch, groups, query_tiles,
Create route-derived forward/reverse FA4 lists with bounded scratch space.
Parameters:
Sorted region IDs [batch, groups, queries, slots] and token-region IDs [batch, keys] with monotonic key-region order.
Number of global regions.
Global token offset of the local query shard.
Key tokens represented by each forward metadata block.
Key tokens represented by each backward metadata block.
Query tokens per forward metadata block, either 128 or 256.
Returns: torch.Tensor
Forward counts [batch * groups, 1, ceil(queries / fwd_q)] and
Classify complete causal rectangles using SM90 D192 backward tiles.
Parameters:
Int32 counts [provider_batches, heads, key_tiles].
Ascending unique nonnegative query-block IDs of shape [provider_batches, heads, key_tiles, query_tiles]. Entries beyond counts are ignored. Provider-folded tensors have heads=1; native layout has provider_batches=batch and heads=query_heads.
Proven all-selected query prefix in one unpadded B1 CP1 document whose positions equal physical token indices.
Returns: _BackwardBlockPartition
Independent partial/full counts and index lists with the input layouts.
Separate a low contiguous full-block prefix without changing K order.
Parameters:
Int32 selected K-block counts [provider_batches, heads, query_tiles]. Provider-folded layout uses provider_batches=batch*groups and heads=1; native head layout uses provider_batches=batch and heads=query_heads.
Sorted unique nonnegative K-block indices [provider_batches, heads, query_tiles, key_tiles], with ignored trailing slots. Both tensors use SM90 D192 tiles: 128 queries and 112 keys.
Proven all-selected initial query count in a single unpadded CP1 document with physical positions starting at zero.
Returns: torch.Tensor
Partial-block counts, shifted partial-block indices, and full-block
Run FA4 forward/backward with compact CSA metadata on SM90 or SM100.
Parameters:
BF16 CUDA tensor [batch, local_queries, query_heads, head_dim].
BF16 CUDA tensor [batch, global_keys, kv_heads, head_dim].
Same shape/dtype/device as key.
Int tensor [batch, local_queries], document-local positions.
Int tensor [batch, global_keys], document-local positions.
Boolean tensor [batch, global_keys], true for padding.
Int tensor [batch, global_keys], document IDs.
Physical global offset of the contiguous query shard.
Sliding window size, or None for CSA.
Compact sorted region metadata, or None for sliding attention.
Opt-in valid SWA document segments in global token coordinates. The existing halo may contain local alignment padding.
Returns: torch.Tensor
BF16 tensor [batch, local_queries, query_heads, head_dim].