nemo_automodel.components.attention.ffpa_attention
nemo_automodel.components.attention.ffpa_attention
FFPA attention bindings for HF ALL_ATTENTION_FUNCTIONS / ALL_MASK_ATTENTION_FUNCTIONS.
Routes head_dim=512 bf16/fp16 layers through the CuTeDSL FFPA kernel,
sliding-window (flex BlockMask) layers through FlexAttention, and falls back
to SDPA (or eager for softcap) for other unsupported configurations.
Module Contents
Functions
Data
API
Run FFPA CuTeDSL dense backward with the caller’s globally merged output and LSE.
Parameters:
Output-gradient tensor of shape [batch, query_heads, query_sequence, value_head_dim] in SDPA layout.
Float16 or bfloat16 CUDA tensor of shape [batch, query_heads, query_sequence, head_dim] in SDPA
layout. QKV, grad_out, and out share dtype and device.
Key tensor of shape [batch, key_value_heads, key_value_sequence, head_dim] in SDPA layout.
Value tensor of shape [batch, key_value_heads, key_value_sequence, value_head_dim] in SDPA layout.
Caller’s globally merged output tensor of shape [batch, query_heads, query_sequence, value_head_dim] in SDPA layout.
Caller’s globally merged float32 LSE tensor of shape [batch, query_heads, query_sequence] on the
same device as q.
Pre-softmax scaling factor applied to query-key scores.
Whether to apply the causal mask.
Returns: torch.Tensor
A (dq, dk, dv) tuple in SDPA layout. Each gradient has the shape, dtype, and device of its
Run FFPA CuTeDSL dense forward on SDPA-layout tensors.
The returned per-chunk LSE supports the ring online-softmax merge.
Parameters:
Float16 or bfloat16 CUDA tensor of shape [batch, query_heads, query_sequence, head_dim] in SDPA layout. QKV share dtype, device, and head dimension.
Key tensor of shape [batch, key_value_heads, key_value_sequence, head_dim] in SDPA layout.
Value tensor of shape [batch, key_value_heads, key_value_sequence, value_head_dim] in SDPA layout.
Pre-softmax scaling factor applied to query-key scores.
Whether to apply the causal mask.
Returns: torch.Tensor
A tuple containing an output tensor of shape [batch, query_heads, query_sequence, value_head_dim] with
Whether ffpa-attn runs its generic SM80 Split-D backward for this device and head dims.
That kernel accumulates dK/dV across tiles through an HBM buffer in the activation dtype, so
every partial sum loses bits to a bf16 round trip and the error grows with the chunk length
(measured on the SM80 path, head_dim 512: ~4e-3 relative L2 at 256 tokens, ~1e-2 at 2K, ~1.5e-2 at 4K,
three to four times the Flex ring). Its grad_kv_storage_dtype option widens that buffer
to fp32; the SM90/SM100 kernels accumulate in registers/TMEM and reject the option. Mirrors
the library’s own dispatch so the two cannot disagree; if that dispatch cannot be imported,
the backward keeps the library defaults and says so once.
Run FFPA CuTeDSL varlen backward with the caller’s globally merged output and LSE.
On the generic SM80 path the per-document segments run through the dense op with fp32
dK/dV storage (see _ffpa_sm80_split_d_backward).
Parameters:
Output-gradient tensor of shape [total_query_tokens, query_heads, value_head_dim] in packed THD layout.
Float16 or bfloat16 CUDA tensor of shape [total_query_tokens, query_heads, head_dim] in packed
THD layout. QKV, grad_out_pack, and out_pack share dtype and device and are made contiguous
before dispatch.
Key tensor of shape [total_key_value_tokens, key_value_heads, head_dim] in packed THD layout.
Value tensor of shape [total_key_value_tokens, key_value_heads, value_head_dim] in packed THD layout.
Caller’s globally merged output tensor of shape [total_query_tokens, query_heads, value_head_dim] in packed THD layout.
Caller’s globally merged float32 LSE tensor of shape [query_heads, total_query_tokens] on the
same device as q_pack.
Int32 CUDA tensor of shape [batch + 1] on the same device as q_pack, containing cumulative
query-token offsets.
Int32 CUDA tensor of shape [batch + 1] on the same device as q_pack, containing cumulative
key/value-token offsets.
Maximum query sequence length represented by cu_q.
Maximum key/value sequence length represented by cu_k.
Pre-softmax scaling factor applied to query-key scores.
Whether to apply the tail-aligned causal mask within each packed sequence.
Host-side copy of cu_q when the caller already holds one; spares the SM80 path a
device-to-host sync.
Host-side copy of cu_k, likewise.
Returns: torch.Tensor
A (dq, dk, dv) tuple in packed THD layout. Each gradient has the shape, dtype, and device of its
SM80 varlen backward as per-document dense calls, so every segment gets the fp32 dK/dV
storage of _ffpa_dense_bwd.
ffpa-attn’s SM80 varlen backward is this same per-segment decomposition, but it allocates the segment buffers in the activation dtype, so its storage-dtype option never reaches them.
Run FFPA CuTeDSL varlen forward on packed THD inputs.
Windowing, softcap, and packed GQA are disabled at this seam.
Parameters:
Float16 or bfloat16 CUDA tensor of shape [total_query_tokens, query_heads, head_dim] in packed THD layout. QKV share dtype and device and are made contiguous before dispatch.
Key tensor of shape [total_key_value_tokens, key_value_heads, head_dim] in packed THD layout.
Value tensor of shape [total_key_value_tokens, key_value_heads, value_head_dim] in packed THD layout.
Int32 CUDA tensor of shape [batch + 1] on the same device as q_pack, containing cumulative
query-token offsets from 0 through total_query_tokens.
Int32 CUDA tensor of shape [batch + 1] on the same device as q_pack, containing cumulative
key/value-token offsets from 0 through total_key_value_tokens.
Maximum query sequence length represented by cu_q.
Maximum key/value sequence length represented by cu_k.
Pre-softmax scaling factor applied to query-key scores.
Whether to apply the tail-aligned causal mask within each packed sequence.
Returns: torch.Tensor
A tuple containing an output tensor of shape [total_query_tokens, query_heads, value_head_dim] with the
Whether the FFPA CuTeDSL varlen ops are importable and registered.
True for a flex BlockMask — the marker that a layer should run on FlexAttention.
Route the Hugging Face attention interface through FFPA or an eligible fallback.
Parameters:
Attention module providing the training state and head_dim used for FFPA eligibility.
Query tensor of shape [batch, query_heads, query_sequence, head_dim] in SDPA layout.
Key tensor of shape [batch, key_value_heads, key_value_sequence, head_dim] in SDPA layout.
Value tensor of shape [batch, key_value_heads, key_value_sequence, value_head_dim] in SDPA layout. QKV share dtype and device. FFPA requires float16 or bfloat16 CUDA tensors with head dimension 512.
Optional boolean padding tensor of shape [batch, key_value_sequence] or additive tensor
of shape [batch, 1, query_sequence, key_value_sequence]. A Flex BlockMask produced by
ffpa_mask may also be supplied for the FlexAttention route.
Dropout probability. Nonzero training dropout makes the FFPA route ineligible.
Optional pre-softmax scaling factor applied to query-key scores.
Optional score cap; when present, the eager fallback owns execution.
Additional Hugging Face attention arguments forwarded to the selected backend.
Returns: torch.Tensor
A tuple containing an output tensor of shape [batch, query_sequence, query_heads, value_head_dim] with
Build the attention mask used by the registered FFPA backend.
Parameters:
Number of sequences in the batch.
Query sequence length.
Key/value sequence length.
Global offset of the first query token.
Global offset of the first key/value token.
Optional index-based mask predicate.
Optional padding mask tensor of shape [batch, kv_sequence]
or a prebuilt mask tensor of shape [batch, 1, query_sequence, kv_sequence].
Floating-point dtype used when a backend needs an additive mask.
Mask-factory metadata forwarded by Transformers.
Returns: 'torch.Tensor | BlockMask | None'
A boolean tensor of shape [batch, kv_sequence] in padding layout or
Register "ffpa" in HF attention/mask registries. Idempotent.
Validate FFPA preconditions, then register the HF "ffpa" backend.
The "ffpa" backend calls the FFPA op directly, bypassing both the ring-CP
SDPA swap and packed-sequence masking; for CP or packing use
attn_implementation="sdpa" + text_config.cp_full_attn_backend="ffpa" instead.