Skip to main content

FlashGDN

Gated DeltaNet written from scratch — first in plain C, then in CUDA, packaged as a PyTorch library. Built for Grace, BranchingNLP's family of small hybrid language models.

from flashgdn import gdn_chunk, gdn_recurrent

# training: chunked form, fused CUDA kernels (forward + backward)
o, state = gdn_chunk(q, k, v, beta, g, initial_state=None, output_final_state=True)
o.sum().backward()

# inference / decoding: recurrent form, state kept in registers
o, state = gdn_recurrent(q, k, v, beta, g, initial_state=state, output_final_state=True)

Shapes: q, k: [B, T, H, K] (L2-normalised), v: [B, T, H, V], beta, g: [B, T, H] (beta ∈ (0, 1), g ≤ 0 is the log-decay). Same convention as flash-linear-attention.

The gated delta rule

S_t = α_t S_{t-1} + β_t k_t (v_t − α_t S_{t-1}ᵀ k_t)ᵀ      α_t = e^{g_t}
o_t = scale · S_tᵀ q_t

The chunked form (blocks of C = 64 tokens) turns this into dense matrix products inside each block, plus a short sequential pass of the state between blocks:

γ = cumsum(g)    L_ij = e^{γ_i − γ_j}    A = −(diag(β) k kᵀ) ⊙ L  (strictly lower)    T = (I − A)⁻¹
u = T diag(β) v        w = T diag(β e^γ) k
v_new = u − w S        o = (q ⊙ e^γ) S + ((q kᵀ) ⊙ L) v_new        S ← e^{γ_last} S + (k ⊙ e^{γ_last − γ})ᵀ v_new

What's inside

file what
reference/flashgdn.c recurrent and chunked forms in plain C — they match to 4.5e-8
reference/flashgdn_bwd.c recurrent backward in C, checked with finite differences
flashgdn/flashgdn_kernel.cuh recurrent inference kernel (one thread per state column, state in registers)
flashgdn/flashgdn_opt.cuh recurrent training kernels: forward with checkpoints every 64 tokens + backward
flashgdn/flashgdn_chunk.cuh chunked form: state-passing kernel (forward/backward), cp.async pipelined
flashgdn/flashgdn_fused.cuh chunked form: all intra-chunk work fused (3 kernels forward, 6 backward), TF32 tensor cores
flashgdn/chunk.py gdn_chunk + a PyTorch fallback for shapes the fused kernels don't cover
tests/ correctness against a naive PyTorch reference + benchmarks

The backward of the fused chunked form (through the triangular inverse, the decays and the state pass) is derived by hand; every kernel's math was first checked against autograd in float64 (error ~1e-15).

Speed

One GDN layer of Grace (batch 4 × 2048 tokens, 12 heads, K = 64, V = 128), forward + backward, RTX 3070:

implementation inputs forward forward + backward
FlashGDN fused, MODE="bf16" bf16 1.4 ms 4.8 ms
fla chunk_gated_delta_rule (Triton) bf16 1.0 ms 3.6 ms
FlashGDN fused, MODE="bf16" fp32 1.6 ms 5.5 ms
FlashGDN fused, MODE="tf32" fp32 2.0 ms 8.2 ms
fla chunk_gated_delta_rule (Triton) fp32 2.2 ms 8.9 ms
FlashGDN fused, MODE="fp32" (exact) fp32 2.8 ms 10.6 ms
FlashGDN chunked, PyTorch + torch.compile + state kernel fp32 4.5 ms 13.4 ms
FlashGDN recurrent fp32 2.6 ms 44.9 ms

Recurrent decoding: 8.7 µs per token per layer.

How it got there: hand-derived chunked backward → fused kernels → blocked triangular inverse → bank-conflict-free shared memory (padded rows, warp-per-row reductions) → bf16 intermediates → bf16 tensor cores via hand-written mma.sync fragments → register accumulators → bf16 operand tiles.

Install and test

pip install -e .            # or just put the repo on PYTHONPATH
python3 tests/test_flashgdn_chunk.py
python3 tests/test_flashgdn_bwd.py
python3 tests/test_flashgdn.py

The CUDA code is compiled on first import with torch.utils.cpp_extension (needs nvcc and ninja) and cached afterwards.

Options:

  • flashgdn.chunk.MODE — what the fused kernels multiply with: "bf16" (default) bf16 tensor cores via mma.sync, like fla; "tf32" TF32 tensor cores via wmma; "fp32" no tensor cores and fp32 intermediates (slow, exact to ~1e-7, for checking). With "bf16"/"tf32" the large intermediates (u, w, T, attn, per-chunk states) are stored in bf16; all accumulation inside the kernels, and the recurrent state, stay in fp32.
  • Inputs q, k, v can be fp32, bf16 or fp16: the kernels read them directly and o, dq, dk, dv come back in the same dtype.
  • FLASHGDN_FUSED=0 — use the PyTorch + state-kernel path instead of the fused kernels
  • FLASHGDN_COMPILE=0 — disable torch.compile on that path

Limits

  • Fused chunked kernels: K = 64, V multiple of 32. Other shapes use the PyTorch path (K ≤ 64).
  • Recurrent kernels: K ≤ 128 (optimised path for V ≤ 128).
  • Computation is in fp32 (inputs in bf16/fp16 are converted; the output keeps the input dtype).
  • Tested on sm_86 (RTX 3070).

License

MIT

Metadata

Release files for flashgdn 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 flashgdn 0.1.0
File Size Uploaded Provenance Actions
flashgdn-0.1.0.tar.gz 43.1 kB Without provenance Details for flashgdn-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 84.3 kB

Release files / flashgdn-0.1.0.tar.gz

Download URL flashgdn-0.1.0.tar.gz
Size 43.1 kB
Tags Source
SHA-256 checksum
How to use checksums
22f3286272c16cad726219bc90b8fd1db0ba62dc33458aa89402bc3105ab7ea6
BLAKE2b-256 checksum
How to use checksums
f289260ef656a0a557c3d169684618c14c8a758f192c15f4f03fd918b3d02225
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release files / flashgdn-0.1.0-py3-none-any.whl

Download URL flashgdn-0.1.0-py3-none-any.whl
Size 41.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
112a9e8e0c037fbee95322d5d1e2a09b4a126c3d9ee65f24878429bf5bf4b82b
BLAKE2b-256 checksum
How to use checksums
c61d3709503b7b6d92d592287de167d80c0dc3526f2c2a74e7b0c722524e318b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Security for flashgdn 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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