Skip to main content

FoldAttention

FoldAttention: Declared-Reference Softmax for Fast Decode and Deterministic Backward
Sriman Achanta
Paper: https://arxiv.org/abs/2609.33410

FoldAttention overview: declared-reference softmax, decode speedup, and deterministic backward throughput

FoldAttention declares softmax's reference before execution. Final weights make decode contributions additive, while a shared integer grid makes the backward deterministic without serializing its reductions.

Usage

Install FoldAttention from PyPI:

pip install fold-attention

Install the CUDA 13 dependencies with the cu13 extra:

pip install "fold-attention[cu13]"

FoldAttention requires Python 3.12 or newer and an NVIDIA SM90 GPU.

Code usage

Training

fold_attn_func follows FlashAttention's (batch, seqlen, heads, head_dim) layout. Keys and values may use fewer heads for GQA or MQA.

from fold_attention import fold_attn_func

out = fold_attn_func(q, k, v, causal=True)
out.backward(dout)

The forward uses FlashAttention-4. The FoldAttention backward produces bit-identical gradients across repeated runs, batching, and variable-length packing.

For packed self-attention, pass (total_tokens, heads, head_dim) tensors and a CUDA int32 cumulative-length vector:

from fold_attention import fold_attn_varlen_func

out = fold_attn_varlen_func(q, k, v, cu_seqlens, causal=True)

Decode

FoldKVCache owns one layer's paged cache. prefill runs FlashAttention-4 and writes the cache. fold_attn_with_kvcache optionally appends one token per request, then attends over the updated cache.

from fold_attention import FoldKVCache, fold_attn_with_kvcache

cache = FoldKVCache(
    batch=batch_size,
    n_heads=n_heads,
    n_kv_heads=n_kv_heads,
    head_dim=head_dim,
    max_len=max_len,
    depth=16,
)

prompt_out = cache.prefill(q, k, v, cu_seqlens)
step_out = fold_attn_with_kvcache(q_step, cache, k_step, v_step)

Set depth=None for dense decode. A finite depth cuts low-weight keys while retaining their normalization mass. Set v8=True to store values in two E4M3 planes.

Benchmarks

Decode speed and accuracy

Latency against FP32-relative error on seven real-model generations. Each FoldAttention curve sweeps the decode depth.

Decode latency against FP32-relative error on seven real-model generations

Deterministic backward

Causal backward throughput across MHA and GQA shapes. The rows below each panel report FoldAttention throughput relative to the fastest deterministic and nondeterministic kernel.

Causal attention backward throughput on MHA and GQA shapes

The benchmark suite and measurement protocol are documented in benchmarks/README.md.

License

FoldAttention is released under the Apache License 2.0.

Metadata

Release files for fold-attention 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fold-attention 0.2.0
File Size Uploaded
fold_attention-0.2.0.tar.gz 164.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fold-attention 0.2.0
File Interpreter ABI Platform
fold_attention-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 344.8 kB

Release files / fold_attention-0.2.0.tar.gz

Download URL fold_attention-0.2.0.tar.gz
Size 164.9 kB
Tags Source
SHA-256 checksum
How to use checksums
813098207e96ecb853de61fce499e38d06a3a5a85d2233d2d23deaeee4a4fc63
BLAKE2b-256 checksum
How to use checksums
2f7909bccfae10ef1b6a5401fcc94b1ed0c7bdec9422c450d1d863375ac3ff84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.20 {"installer":{"name":"uv","version":"0.12.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / fold_attention-0.2.0-py3-none-any.whl

Download URL fold_attention-0.2.0-py3-none-any.whl
Size 179.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be2ec067efe40107db224b155a4f33e4c264d72db3b0f6022cc845d975fa1c93
BLAKE2b-256 checksum
How to use checksums
8f732ffb489cdc950ede0be965dac559508ab4354865a9f12dda53f025cd5e33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.20 {"installer":{"name":"uv","version":"0.12.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page