Skip to main content

FoldAttention

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.

Release files for fold-attention 0.1.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.1.0
File Size Uploaded
fold_attention-0.1.0.tar.gz 164.5 kB Details

Built distribution (wheel)

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

Total release size: 344.4 kB

Release files / fold_attention-0.1.0.tar.gz

Download URL fold_attention-0.1.0.tar.gz
Size 164.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6c63841ff106bbaaa0d4ad7d43e2d1da34be7691a7ad51313d35fed338fd25af
BLAKE2b-256 checksum
How to use checksums
ee18699f6affd4ebe1e4103b0e03ce577e2c6a0e413b75a99b665d7d5576d3ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","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.1.0-py3-none-any.whl

Download URL fold_attention-0.1.0-py3-none-any.whl
Size 179.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
142bf1846a56bc0b0af7b53592616c9ece8c8e7434e6cfecfb99e18d20deac73
BLAKE2b-256 checksum
How to use checksums
1557f6d81f8c3cb6c3b6ecff790c10af77e68b2feba2c68b6ac13cba648c29e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","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

0.1.1

2 release files

This release

0.1.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