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

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.1
File Size Uploaded
fold_attention-0.1.1.tar.gz 164.5 kB Details

Built distribution (wheel)

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

Total release size: 344.4 kB

Release files / fold_attention-0.1.1.tar.gz

Download URL fold_attention-0.1.1.tar.gz
Size 164.5 kB
Tags Source
SHA-256 checksum
How to use checksums
39045ac193db78e6c2a2d48fa2dd324b0413ff20348c0eaaff074af268b23916
BLAKE2b-256 checksum
How to use checksums
1a2e13e2d3ed57ea342a61bca56165a90e4197c060bb5bbd6c4936378cb0df0c
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.1-py3-none-any.whl

Download URL fold_attention-0.1.1-py3-none-any.whl
Size 179.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d53248f41b3c7a6b99e635c7ba17e018f9d58f1defe1c0890f18feca7f4abcaa
BLAKE2b-256 checksum
How to use checksums
44bc4875285c43af53edf164446edc4da252868d8f1610d19f9d563c17765b51
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

This release

0.1.1 This release

2 release files

0.1.0

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