nemo_automodel.components.attention.fa4
nemo_automodel.components.attention.fa4
FlashAttention 4 (flash_attn.cute) helpers for models that support BackendConfig.attn = "fa4".
-
import_fa4_modulesimports the optional FA4/CuTe modules with one clear error when they are missing. -
causal_fa4_attentionruns plain causal attention. -
document_causal_fa4_attentionruns causal, per-document attention of a contiguous query shard against the global keys (packed sequences, and context parallelism with contiguous shards). It is expressed as FA4 varlen segments, so no mask is materialized or evaluated per element and unequal QK/V head dims (e.g. MLA’s 192/128) run without padding:- Every document run of a row (padding runs included) is split at the end of the local query shard.
- A piece that overlaps the shard contributes its overlapping queries and all of its keys; FA4’s causal mask is aligned to the bottom-right corner of each segment, which is exactly “query at global position p sees keys of its document up to p”.
- Pieces without local queries (other ranks’ later tokens, earlier documents) become zero-query segments, so the
key segments tile the flattened
[batch * key_sequence]tensor and no gather is needed.
Optional FA4/CuTe dependencies are imported on first use.
Module Contents
Functions
Data
API
Import the FA4 dense and varlen entry points once.
Build (and cache for the step) FA4 varlen cu_seqlens and max lengths.
Parameters:
Integer tensor [batch, query_sequence] of 1-based document ids (0 = padding) for the local queries;
equal to kv_doc_ids[:, q_global_start : q_global_start + query_sequence].
Integer tensor [batch, key_sequence] of document ids for all keys, on the attention device. Its storage identifies the step: a new tensor invalidates the cache.
Global sequence offset of the first local query.
Returns: torch.Tensor
(cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k): two int32 tensors of shape [segments + 1] on
Return per-segment query and key lengths describing the document-causal pattern.
Parameters:
CPU tensor of shape [batch, key_sequence] with 1-based document ids, 0 for padding.
Local query length.
Global sequence offset of the first local query.
Returns: tuple[list[int], list[int]]
Query lengths and key lengths of every segment, in flattened row-major order.
Run plain causal attention with FA4 when queries and keys cover the same single-document sequence.
Parameters:
Tensor of shape [batch, heads, sequence, qk_head_dim].
Tensor of shape [batch, heads, sequence, qk_head_dim].
Tensor of shape [batch, heads, sequence, v_head_dim].
Softmax scale applied to the query-key product.
Returns: torch.Tensor
Tensor of shape [batch, heads, sequence, v_head_dim].
Run causal, per-document attention of local queries against global keys with FA4 varlen.
Parameters:
Tensor of shape [batch, heads, query_sequence, qk_head_dim].
Tensor of shape [batch, heads, key_sequence, qk_head_dim].
Tensor of shape [batch, heads, key_sequence, v_head_dim].
Tensor of shape [batch, query_sequence] with 1-based document ids.
Tensor of shape [batch, key_sequence] with 1-based document ids.
Global sequence offset of the first query token.
Softmax scale applied to the query-key product.
Returns: torch.Tensor
Tensor of shape [batch, heads, query_sequence, v_head_dim]; padding queries are zero.
Import optional FlashAttention 4 / CuTe DSL modules, raising one clear error if any is missing.
Parameters:
Fully qualified module names, e.g. "flash_attn.cute.interface" or "cutlass.cute".
Returns: tuple[ModuleType, ...]
The imported modules, in the order requested.