det-attn

Bitwise-deterministic attention, invariant to KV sharding, loadable through kernels. With det-train's GEMM, the attention softmax combine is one of the two operations that make transformer training irreproducible under sequence and context parallelism; det-attn removes it. The reference contrast is the standard online-softmax combine, whose output bits depend on shard order.

Ring and context-parallel attention split the keys and values across devices and combine partial softmax states with an exp-rescale that is not associative in floating point, so the same attention over the same tokens returns different bits at different world sizes. This kernel makes the combine exact: composing KV shards is an integer addition, so the output is identical whether the sequence lives on one device or thirty-two, in any order.

Four KV shards orbit a query while the float32 output hash glitches every rotation and the det-attn hash holds still

Four shard rotations of the same attention. The float32 online-softmax output hashes to four different values (outputs differ by up to 1.5e-7); the det-attn output is torch.equal at every rotation, one hash, the same bytes.

Usage

import torch
from kernels import get_kernel

da = get_kernel("phanerozoic/det-attn", version=1, trust_remote_code=True)

# Q,K,V are [B, H, S, D]; GQA supported (K,V may have fewer heads).
out = da.det_attn(Q, K, V, causal=True)        # fused, deterministic

# context-parallel (ring) composition, bit-identical to the fused result:
out = da.det_attn_sharded(Q, K, V, kv_splits=[128, 128, 128, 128], causal=True)
assert torch.equal(out, da.det_attn(Q, K, V, causal=True))

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark.

API

Symbol Purpose
det_attn(Q, K, V, causal, scale) fused deterministic attention, f32 out
rowmax(Q, K, ..., kv_lo, kv_hi) per-query rowmax over a key range (combine by max)
accumulate(Q, K, V, rowmax, ...) long-accumulator NUM/DEN digits for a shard (combine by add)
finalize(NUM, DEN, shape) compose digits to the attention output
det_attn_sharded(Q, K, V, kv_splits, ...) reference KV-shard composition

Method

Two exact, order-invariant reductions replace the online-softmax rescale:

  1. a global row maximum rowmax_i = max_j (scale * Q_i . K_j), a max-reduce, order-invariant by construction; then
  2. exact Kulisch long-accumulator sums of the exp-weighted values num_id = sum_j exp(s_ij - rowmax_i) V_jd and the denominator den_i = sum_j exp(s_ij - rowmax_i).

Because the row maximum is global, every weight and every product is a fixed float, so the pass-2 sums are exact integer accumulations into fixed bins. Composing KV shards is an int64 add of digit arrays, bit-identical to the single-device result and identical across ring step counts, block sizes, and world sizes. Scores are per-(i, j), so nothing depends on how keys are sharded.

Correctness

  • Shard invariance: the sharded composition and any permutation of KV blocks are torch.equal to the fused single-call result (the property the hero measures live; the float32 online-softmax combine over the same shards produces order-dependent bits at the 1e-7 level).
  • Causal and non-causal, MHA and GQA, f32 and bf16 inputs, D <= 256.
  • Repeated runs are bitwise identical.

Requirements and limits

  • NVIDIA GPU with compute capability 8.0+.
  • Two-pass exact accumulation on fp64 scalar arithmetic, not a fused tensor-core flash kernel; built for reproducibility rather than peak throughput. The regime is context-parallel training reproducibility, debugging, and audit.
  • D <= 256; f32 output.

References

The exact order-invariant softmax reduction over a Kulisch long accumulator; ring attention and context parallelism as the setting; phanerozoic/det-train for the GEMM half.

License

Apache-2.0.

Downloads last month
-
apache-2.0
Supported hardwares new
CUDA
8.08.68.99.010.012.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64
Kernel Builder
2c516fc