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
Run FFPA CuTeDSL varlen backward with the caller’s globally merged output and LSE.
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.
Returns: torch.Tensor
A (dq, dk, dv) tuple in packed THD layout. Each gradient has the shape, dtype, and device of its
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.