API Reference¶
ffpa_attn.ffpa_attn_func ¶
ffpa_attn_func(query: Tensor, key: Tensor, value: Tensor, attn_mask: Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, **kwargs: object) -> torch.Tensor
FFPA: Faster Flash Prefill Attention for large headdims (D > 256).
Signature aligned with torch.nn.functional.scaled_dot_product_attention.
Dispatches by query.dtype (fp16 / bf16) and acc through a single
registered torch op (torch.ops.ffpa_attn.attn), keeping the
Python layer minimal and fully compatible with torch.compile.
Supports cross-attention where query seqlen (Nq) differs from
key/value seqlen (Nkv) and grouped-query attention where
query has more heads than key/value (MQA is the
Nh_kv == 1 special case). key and value must share the same
Nh_kv and the same Nkv. Causal masking is supported via
is_causal=True with queries aligned to the tail of the KV sequence
(Nkv >= Nq required).
Backward pass is supported via :class:FFPAAttnFunc. The public API falls
back to SDPA for cases FFPA does not currently support directly (small-D,
D > 1024, and unsupported large-D dropout), and otherwise keeps the
existing FFPA forward plus SDPA/FFPA backward routing. Large-D Triton forward
and backward support explicit additive attn_mask gradients.
forward_backend only affects the large-D path and is accepted as an
FFPA-specific keyword inside **kwargs so the explicit signature remains
aligned with :func:torch.nn.functional.scaled_dot_product_attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Tensor
|
Query tensor with layout |
required |
key
|
Tensor
|
Key tensor with layout |
required |
value
|
Tensor
|
Value tensor with layout |
required |
attn_mask
|
Tensor | None
|
Optional attention mask broadcastable to
|
None
|
is_causal
|
bool
|
When |
False
|
scale
|
float | None
|
Pre-softmax scaling factor applied to |
None
|
enable_gqa
|
bool
|
Grouped-query attention mode. Defaults to |
False
|
kwargs
|
object
|
FFPA-specific extension kwargs. Supported keys are
|
{}
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor |
Raises:
| Type | Description |
|---|---|
TypeError
|
if |
ValueError
|
if the selected backend config is invalid, if |
NotImplementedError
|
propagated from SDPA or FFPA backends for unsupported backend-specific combinations. |
ffpa_attn.ffpa_attn_varlen_func ¶
ffpa_attn_varlen_func(q: Tensor, k: Tensor, v: Tensor, cu_seqlens_q: Tensor, cu_seqlens_k: Tensor | None, max_seqlen_q: int, max_seqlen_k: int, *, dropout_p: float = 0.0, softmax_scale: float | None = None, causal: bool = False, enable_gqa: bool = False, return_lse: bool = False, **kwargs: object) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]
FFPA variable-length attention (packed THD, FlashAttention-style).
Signature aligned with Dao-AILab flash_attn_varlen_func. Inputs are
packed THD: q is [T_q, H_q, D] and k / v are
[T_k, H_kv, D]. Sequence boundaries are described by cu_seqlens_q
and cu_seqlens_k (int32 CUDA tensors of length B+1 starting at 0).
When cu_seqlens_k is None it defaults to cu_seqlens_q (self-attention).
Only the CuTeDSL backend is supported: SM8x/SM90, large 64-aligned head dims,
fp16 / bf16. Any unsupported case raises an
actionable error immediately — there is no silent fallback to dense /
per-sequence paths. Callers needing other shapes / backends should
unpack the batch and call :func:ffpa_attn_func per sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Tensor
|
Query tensor of shape |
required |
k
|
Tensor
|
Key tensor of shape |
required |
v
|
Tensor
|
Value tensor of shape |
required |
cu_seqlens_q
|
Tensor
|
|
required |
cu_seqlens_k
|
Tensor | None
|
Same convention for keys; defaults to
|
required |
max_seqlen_q
|
int
|
Maximum per-sequence query length across the batch. |
required |
max_seqlen_k
|
int
|
Maximum per-sequence key length across the batch. |
required |
dropout_p
|
float
|
FlashAttention-compat; must be |
0.0
|
softmax_scale
|
float | None
|
Pre-softmax scaling factor; defaults to
|
None
|
causal
|
bool
|
Apply a lower-right (tail-aligned) causal mask. |
False
|
enable_gqa
|
bool
|
Opt-in to GQA/MQA ( |
False
|
return_lse
|
bool
|
When |
False
|
kwargs
|
object
|
Most kwargs are recognized-and-rejected by
:func: |
{}
|
Returns:
| Type | Description |
|---|---|
Tensor | tuple[Tensor, Tensor]
|
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
for unsupported CuTeDSL head dims or hardware,
|
ValueError
|
for shape mismatches between |
Backend Configuration¶
ffpa_attn.Backend
dataclass
¶
Base backend configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Backend identifier (e.g. "triton", "cutedsl"). |
forward |
bool | None
|
Whether this instance configures the forward pass. |
backward |
bool | None
|
Whether this instance configures the backward pass.
Same |
ffpa_attn.TritonBackend
dataclass
¶
Bases: Backend
Triton forward + backward backend (default).
Attributes:
| Name | Type | Description |
|---|---|---|
autotune |
bool
|
Enable Triton autotuning for kernel parameters. |
autotune_mode |
str
|
Autotune search granularity ( |
enable_tma |
bool
|
Enable experimental SM90+ TMA hardware acceleration. |
enable_ws |
bool
|
Force warp-specialized configs (requires enable_tma). |
persist_dkdv |
bool
|
Keep |
split_launch |
bool
|
Issue separate backward launches for |
preprocess_d_chunk |
bool
|
Split the |
grad_kv_storage_dtype |
dtype | str | None
|
Optional |
grad_q_storage_dtype |
dtype | str | None
|
Optional |
ffpa_attn.CUDABackend
dataclass
¶
Bases: Backend
Hand-written CUDA forward-only backend.
Attributes:
| Name | Type | Description |
|---|---|---|
acc |
str
|
MMA accumulator precision ( |
stages |
int
|
Pipeline stages for the CUDA kernel (3 on Ampere/Ada, 4 on Hopper+). |
ffpa_attn.CuTeDSLBackend
dataclass
¶
Bases: Backend
CuTeDSL SM90-specialized backend (Hopper only, dense 320<D<=512, fp16/bf16 training).
Attributes:
| Name | Type | Description |
|---|---|---|
grad_kv_storage_dtype |
dtype | str | None
|
Optional |
ffpa_attn.SDPABackend
dataclass
¶
Bases: Backend
PyTorch native scaled_dot_product_attention backend.
Forward always short-circuits via :meth:FFPAAttnMeta.fallback.
When used as backward_backend it delegates to
:func:_efficient_attn_backward_aten.
Attributes:
| Name | Type | Description |
|---|---|---|
high_precision_grad |
bool
|
When |