Repository navigation
CUDA: honor the F32 precision Inkling asks for on the banded attention op - #2
Draft
LeoBorcherding wants to merge 1 commit into
Conversation
Inkling sets GGML_PREC_F32 on every FLASH_ATTN_EXT_BANDED node, but ggml_cuda_flash_attn_ext_banded_use_mma never read the precision, so on Turing and newer with an F16/BF16 KV cache the op ran on the MMA kernel, whose V*softmax accumulator is half2 on NVIDIA. With a nonzero-mean V the dropped fp16 increments bias the output toward zero as the context grows. An F32 request now takes the FP32 banded kernel, which is what AMD and pre-Turing already run. test-backend-ops gets a banded case with V in U(900, 1100) at n_kv = 131072, held to the F32 tolerance; the existing cases use zero-mean V, where the accumulator stays near zero and the error can't show.
Owner
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Hooray! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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.
Audit finding
IK-2(P2, confirmed on an L4). Inkling's banded attention on NVIDIA.Base of this PR. The banded op only exists in the Inkling pin (
ggml-org/llama.cpp#25731, pinned inscripts/unsloth/pr-set.jsonat534c32182, which is still that PR's head and still the pin onunslothai/llama.cppmaster8dd697cf0), not in the fork's master tree. So this PR targetsnight-shift-tester/audit-pin-inkling-534c321(that pin commit) rather thannight-shift-tester/audit-base, which keeps the diff to the fix. Landing it means a commit on the Inkling PR branch and a repin, or a fix pin listed after it.What's wrong
Inkling sets
GGML_PREC_F32on everyFLASH_ATTN_EXT_BANDEDnode (src/models/inkling.cpp:466-468at534c32182):cur = ggml_flash_attn_ext_banded(ctx0, q_fa, k_fa, v_fa, mask, rel_fa, 1.0f/float(head_dim), rel_extent); ggml_prec_set_acc(cur, GGML_PREC_F32);ggml_cuda_flash_attn_ext_banded_use_mma(ggml/src/ggml-cuda/fattn.cu:541) never reads the precision, so with an F16/BF16 KV cache (and F32 caches, which Inkling casts to F16 first) on any Turing or newer card the op goes toBEST_FATTN_KERNEL_MMA_F16(fattn.cu:607), whose V*softmax accumulator ishalf2on NVIDIA (fattn-mma-f16.cuh:1138-1172). The FP32 banded kernel infattn-banded.cuonly ever runs on AMD and pre-Turing. With a nonzero-mean V, once the running sum is large each increment is under half an fp16 ulp and gets dropped while the fp32 row sum keeps growing, so the output is biased toward zero:Fix
ggml_cuda_flash_attn_ext_banded_use_mmareturns false when the node asks for F32 or better (prec != GGML_PREC_UNDEFINED && prec <= GGML_PREC_F32, the "at least F32" testggml.hdocuments), so the FP32 banded kernel runs. Both call sites (fattn.cukernel pick andfattn-banded.cu:219) go through this one function, so they stay in agreement. The other option in the audit, an fp32T_C_VKQfor the banded MMA instance, keeps the fast path but changes the shared MMA tile configs; this is the change that honors the documentedggml_prec_set_acccontract without touching the kernel.Tradeoff: Inkling always requests F32, so on NVIDIA the banded MMA path is now unused and long-context prefill runs on the simpler FP32 kernel. Speed isn't measured (
test-backend-ops perfhas no case for this op). If that cost turns out too high, the follow-up is the fp32T_C_VKQinstance, not dropping the precision request.Test
test-backend-opsgetstest_flash_attn_ext_banded_v_offset: the existing banded case with V drawn from U(900, 1100), atd=128, n_head=8, n_head_kv=1, n_q=512, n_kv=131072, causal, F16 K/V, held to the F32 tolerance (NMSE 2e-6). The existing cases use V in [-0.25, 0.25], where the accumulator stays near zero, and allow 1e-3 for fp16 accumulation.Testing
Colab L4 (NVIDIA L4, cc 8.9), driver 580.82.07, CUDA 13.0 (nvcc V13.0.88), gcc 13.3.0, cmake 3.31.10, Ubuntu 24.04.4.
534c32182(the pin) with only this PR'stests/test-backend-ops.cpphunk applied, so the new case exists on baseb58b65dc2(this branch)Same command on both:
v=U(900,1100)case534c32182ERR = 1.130658111 > 0.000002000b58b65dc2Base output:
Fix output:
The real-hardware error on base (NMSE 1.13) is far larger than the CPU emulation in the audit predicted (4e-4 to 0.46), so the fp16 accumulator loss is worse in practice than first sized.
Not tested