Skip to content

CUDA gets FLASH_ATTN_BACK: an in-place lesson's attention backward keeps no scores - #166

Merged
joelteply merged 2 commits into
fable/context-memory-rowsfrom
fable/cuda-flash-attn-back
Nov 27, 2026
Merged

joelteply merged 2 commits into
fable/context-memory-rowsfrom
fable/cuda-flash-attn-back

Conversation

@joelteply

Copy link
Copy Markdown

ggml-cuda had no GGML_OP_FLASH_ATTN_BACK (only ggml-cpu and ggml-metal implement it), so on a CUDA
node opt_fa_back_native() is false and training composes the attention backward from tiles. On the
5090 (Codex's replay of continuum#6292, build 2161, 2026-11-27) a resident's in-place lesson over
her served window then measured 1.897 TB against 34.19 GB: learning waited on memory or was refused,
though in-place learning should cost the adapter's gradients, optimizer state and a reply-sized graph.

fa-back.cu ports Metal's per-row path (kernels/fa_back.metal kernel_flash_attn_back_{scale,q,kv}):
one warp per row, scores recomputed, memory the gradients alone. Pass Q (per query row and head):
online log-sum-exp, then D, A, B in one sweep, dq = scale (A - D B), lse and D to the scratch. Pass
KV (per key row >= kv0 and KV head): dk, dv over the group's query rows; keys before kv0 (a frozen
served prefix) get none. Pass scale records the dO power of two every backend writes. K/V F32, F16
or Q8_0; heads a multiple of 32 up to 512: Metal's support rule exactly, so either native backend
takes the same graph. ggml-cuda.cu gains only the include and the two one-line hooks.

Not compiled or run here (no NVIDIA on the M5): validation is on the 5090 with
test-backend-ops -o FLASH_ATTN_BACK -b CUDA0 (cases already cover F32/F16/Q8_0, D 128/256/512, kv0
splits and a 2,048-key window) and a re-measure of the over-capacity lesson. The tiled tensor-core
version follows once this is correct.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

…eps no scores

ggml-cuda had no GGML_OP_FLASH_ATTN_BACK (only ggml-cpu and ggml-metal implement it), so on a CUDA
node opt_fa_back_native() is false and training composes the attention backward from tiles. On the
5090 (Codex's replay of continuum#6292, build 2161, 2026-11-27) a resident's in-place lesson over
her served window then measured 1.897 TB against 34.19 GB: learning waited on memory or was refused,
though in-place learning should cost the adapter's gradients, optimizer state and a reply-sized graph.

fa-back.cu ports Metal's per-row path (kernels/fa_back.metal kernel_flash_attn_back_{scale,q,kv}):
one warp per row, scores recomputed, memory the gradients alone. Pass Q (per query row and head):
online log-sum-exp, then D, A, B in one sweep, dq = scale (A - D B), lse and D to the scratch. Pass
KV (per key row >= kv0 and KV head): dk, dv over the group's query rows; keys before kv0 (a frozen
served prefix) get none. Pass scale records the dO power of two every backend writes. K/V F32, F16
or Q8_0; heads a multiple of 32 up to 512: Metal's support rule exactly, so either native backend
takes the same graph. ggml-cuda.cu gains only the include and the two one-line hooks.

Not compiled or run here (no NVIDIA on the M5): validation is on the 5090 with
test-backend-ops -o FLASH_ATTN_BACK -b CUDA0 (cases already cover F32/F16/Q8_0, D 128/256/512, kv0
splits and a 2,048-key window) and a re-measure of the over-capacity lesson. The tiled tensor-core
version follows once this is correct.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…or an upstream PR)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply
joelteply merged commit 9f2477f into fable/context-memory-rows Nov 27, 2026
5 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant