Repository navigation
CUDA gets FLASH_ATTN_BACK: an in-place lesson's attention backward keeps no scores - #166
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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