Skip to content

gpu: the GDN prompt delta's products take each f32 operand as three BF16 parts, so a chunk keeps the one-token rule's precision - #1140

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q35-delta-rounding
Nov 3, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q35-delta-rounding

Conversation

@juliankang4

@juliankang4 juliankang4 commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

GDN prompt delta 커널의 곱이 f32 값을 BF16 세 성분으로 받도록 바꿔, 16토큰 chunk 결과가 한 토큰씩 도는 규칙에 훨씬 가까워진 수정입니다.

What and why

The chunked GDN prompt delta (gdn_chunk_prepare, gdn_delta_prepared in crates/gpu/src/linear/delta.rs) fed the tensor cores each f32 operand as a BF16 high part and one BF16 residual. That drops the bits below 2⁻¹⁶, so o differed from the one-token rule by 5.4e-6 to 2.2e-5 (relative L2) on an RTX 5090. Each operand of the four products (k·k, q·k, T·r, P·u) and the state update is now three BF16 parts holding all of its f32 bits, and each product sums six terms down to 2⁻¹⁶, smallest first. The state update and o start that sum from the decayed state and from Dₜ times Sᵀq, so a correction that cancels against them stays finite where the one-token rule does. gate_linear has a clause for it (a state near f32's limit). The chunk is still 16 tokens, the launch count is still three and the record is still 560 words. gate_linear's prompt band moves from 2e-4 to 2e-6 with a dated PIN; the base kernel reads 6.6e-6 and 2.2e-5 against it.

Refs #1127.

gdn_delta_prepared also no longer spills (172 B to 0 on sm_120a, 172 B to 0 on sm_121a, 48 B to 0 on sm_86): each eight keys' state parts are built once and live alone through that block's products. The pinned spill tables and the gpu unsafe ratchet (3000 to 2992 blocks) are recounted. Every GDN prompt takes this path (Qwen3.5/3.6 and Qwen3.8); KDA and the one-token, verify and recovery kernels are untouched. The change makes no speed claim.

Release notes: Fixed: the GDN prompt kernel's error against the one-token rule drops about 7 to 21 times on an RTX 5090, and its register spill is gone.

Code shape

The review reads these first. A box left unticked needs one line of why.

  • No second copy. Logic two models (or two call sites) share has one common owner; I lifted it there instead of
    copying it (AGENTS.md "Common code first", docs/rust-quality.md R14). Where I found an existing copy, I
    named it below.
  • Model code holds only architecture facts. A model's own files carry only what its architecture forces
    (tensor shapes, the attention kind, the quant). A common owner gained no per-model branch, flag or trait method.
  • Dead code is gone. Nothing I made unused is left behind (functions, flags, gate clauses, docs).
  • No silent failure. Undefined input is a named error or panic, never a defined-looking output.
  • Comments state what is true now. No dates, issue numbers or how a bug was found (tools/check-comments.sh).
  • No relaxed gate. No band, tolerance or lint moved to make a check pass; a re-pin carries PIN(YYYY-MM-DD):
    and its reason.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line), or "none": crates/gpu/src/linear/delta.rs:645 and :704 (chunk_product over two shared-memory tiles, split_mma over one register-held 16-key step) share only the loop over SPLIT_TERMS, which is the single owner of the term order. The old chunk_pair_low and the two-part split helper are replaced by chunk_plane and SPLIT_TERMS.

What I ran

Base 853be26fbfef009bcfc30ecee6af48491683d464. The gate recipes ran on an RTX 5090.

$ nvidia-smi
Tue Nov  3 11:28:54 2026       
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 595.99.02              Driver Version: 595.99.02      CUDA Version: 13.2     |
+-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce RTX 5090        Off |   00000000:01:00.0 Off |                  N/A |
|  0%   42C    P8             12W /  400W |       2MiB /  32607MiB |      0%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

Prompt precision against the one-token rule, gate_linear (synthetic inputs, n_k=16, n_v=32, head=128):

$ just gate-gpu-linear
prompt m=16 on lane 2 of 4: o rel_l2 7.157e-7, lane 2 at its last whole chunk rel_l2 1.742e-7 (band 2e-6) ... PASS
prompt m=1000 on lane 2 of 4: o rel_l2 5.583e-7, lane 2 at its last whole chunk rel_l2 1.966e-7 (band 2e-6) ... PASS
prompt m=4096 on lane 2 of 4: o rel_l2 5.547e-7, lane 2 at its last whole chunk rel_l2 1.968e-7 (band 2e-6) ... PASS
prompt m=4096, keys near one direction on lane 2 of 4: o rel_l2 1.058e-6, lane 2 at its last whole chunk rel_l2 6.614e-7 (band 2e-6) ... PASS
gate_linear: 196 clauses, 0 failed — PASS

The base kernel through the same clauses (BLOOMERY_GATE_CARD=any bash tools/gpu-gate.sh gate_linear):

metric base head difference result
m=16, o rel_l2 5.360e-6 7.157e-7 7.5x lower head under the 2e-6 band, base over
m=1000, o rel_l2 6.632e-6 5.583e-7 11.9x lower head under the 2e-6 band, base over
m=4096, o rel_l2 6.636e-6 5.547e-7 12.0x lower head under the 2e-6 band, base over
m=4096 keys near one direction, o rel_l2 2.195e-5 1.058e-6 20.7x lower head under the 2e-6 band, base over
m=16, lane rel_l2 3.892e-6 1.742e-7 22.3x lower head under the 2e-6 band, base over
m=1000, lane rel_l2 5.133e-6 1.966e-7 26.1x lower head under the 2e-6 band, base over
m=4096, lane rel_l2 5.164e-6 1.968e-7 26.2x lower head under the 2e-6 band, base over
m=4096 keys near one direction, lane rel_l2 2.205e-5 6.614e-7 33.3x lower head under the 2e-6 band, base over

Qwen3.6 real binary through the shared prompt path, model lmstudio-community/Qwen3.6-35B-A3B-GGUF at revision 68a34855558af61cbef0324d31f411be8a506b08, file Qwen3.6-35B-A3B-Q4_K_M.gguf (21,166,757,728 bytes, sha256 4ac6a06bce551257267f49ad2226f8671a22519ccc1a4dde9d5b433d1f2a410d):

$ BLOOMERY_CARD_BUDGET=20G just gate-gpu-qwen35moe-e2e
wide tail 16 of 1040 ids, uncut and cut at 1024: token 220 vs 220, last logits bit-identical=true, every store bit-identical (layers differing []) PASS
gemv arm Gemm plan=ubatch:5: token 11751 vs the fifth step's 11751, last logits bit-identical=true, every store bit-identical (layers differing []) PASS
checkpoints: cut to 4096 and 104 ids re-fed: token 248045 vs 248045, last logits bit-identical=true, every store bit-identical (layers differing []), the points back at [1024, 2048, 3072, 4096, 4200], 1 walk PASS
gate_qwen35moe_e2e (text path): 10 clauses skipped by name: PASS
test cli::tests::hw_last_step_keeps_pass_path ... ok

Register spill of the changed kernels, from ptxas 13.3.73:

$ just arch=sm_86 ptx-scan generate_ds41 --features gpu,deepseek41 --no-jit gdn_
gdn_chunk_prepare     reqntid 128  regs 116  smem 30912  spill 0
gdn_delta_prepared    reqntid 128  regs 250  smem 46016  spill 0

$ just arch=sm_121a ptx-scan generate_ds41 --features gpu,deepseek41 --no-jit gdn_delta_prepared
gdn_delta_prepared    reqntid 128  regs 255  smem 46080  spill 0

Hardware (if the change is for a machine)

  • GPU: NVIDIA GeForce RTX 5090, 32607 MiB
  • Driver 595.99.02, CUDA 13.2 (nvidia-smi); ptxas 13.3.73
  • Ran: gate_linear, gate_qwen35moe_e2e, hw_last_step_keeps_pass_path, and the ptxas scans above

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added gpu The card engine: crates/gpu*, crates/app needs-gpu Changes code a card must check, and carries no gate run on a card tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels Nov 3, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MUST_FIX: The new state-update sum can overflow on finite input that the one-token rule and base kernel accept.

Must fix

1. Keep the decayed state in the product accumulator

crates/gpu/src/linear/delta.rs:2024 computes the correction from zero before adding the old state. Call enqueue_delta_prompt with keep=false, no carried rows and a matching lane stamp. For one value column, start with S=(-1e38,0), use four keys (.5,.5),(.5,-.5),(.5,.5),(.5,-.5), values (1.5e38,1.5e38,2.5e38,2.5e38), and β=decay=1. Set other state and key channels and queries to zero; pad to 16 rows with k=v=β=0 and decay=1. The launch checks accept these values.

The correction is about 4e38, which overflows before cancellation with the initial state. The one-token rule and base accumulation remain finite at about 3e38 [derived; scalar f32 reproduction]. The new state becomes infinity, raises LinearDelta, and makes Head::tokens return a fault.

Fix this before the new sum order becomes the reference for later precision changes. Reuse split_mma with an initial accumulator. Keep its existing attributes and replace its definition:

unsafe fn split_mma(
    a: [[u32; 4]; PLANES],
    b: [*const u32; PLANES],
    mut c: [f32; 4],
) -> [f32; 4] {
    for i in 0usize..SPLIT_TERMS.len() {
        cuda_device::thread::__unroll_config::<0>();
        let (pa, pb) = SPLIT_TERMS[i];
        // SAFETY: the caller's row of part pb.
        c = unsafe { frag_mma(a[pa], b[pb], c) };
    }
    c
}

Pass zero at the inverse call on line 1968 and the causal-product call on line 1975:

ut[tt] = unsafe { split_mma(r_split, planes(0, tt), [0.0; 4]) };
let y = unsafe { split_mma(u_split, planes(128, tt), [0.0; 4]) };

Replace lines 2023–2028 with:

for i in 0usize..4 {
    cuda_device::thread::__unroll_config::<0>();
    st[kt][i] *= d;
}
// SAFETY: keys 8·kt + g of each part's complete keys by time.
st[kt] = unsafe { split_mma(e_split, keys, st[kt]) };

Update the state sum-order documentation and add this input to the existing prompt clause, asserting finite state and no fault. Seeding the three-part accumulator keeps this reproduction finite.

Non-blocking

⚠ Supply the precision and occupancy timing proof: crates/gpu/src/linear/delta.rs:614 doubles full-chunk work from 84 to 168 warp MMA instructions per token per value head; prepared shared memory rises from 32,192 to 46,016 bytes per block [derived; 16-row chunks, HEAD=128]. The PR gives precision and spill results but no timed A/B. Supply the RTX 5090 A/B required by AGENTS.md's precision/occupancy classes and identify the owning e2e count-pin result.

Checked

Duplicates: dryer reported 0 pairs. The tile and register product helpers have different fragment contracts.

Complexity: crapper flagged gdn_chunk_prepare, 27→29; added plane and record loops in a kernel, reviewed for correctness without suggesting a split.

Silent failure: 0 new default/error-suppression sites; followed both output/state finite checks through the fault word to the token readback error.

Tests: restoring the two-part products fails the revised band in the reported base run. The current prompt inputs miss the finite-overflow case above.

Gates: prompt band tightened from 2e-4 to 2e-6 with a dated PIN and baseline failures; changed spill and unsafe pins carry dated reasons.

Escape catalog: no C1–C4 pattern matched.

Notes

Dryer skipped gpu-gates; the changed gate was reviewed manually. No CUDA builds or gates ran during validation; checks used source inspection, loop-count derivation and scalar f32 reproductions.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/src/bin/gate_linear.rs, crates/gpu/src/linear/delta.rs and carries a gate run on the author's card, which counts as verified.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@github-actions github-actions Bot added gpu-evidence Verified on the author's card: the pull request carries the gate run and its output and removed needs-gpu Changes code a card must check, and carries no gate run on a card labels Nov 3, 2026
…F16 parts, so a chunk keeps the one-token rule's precision

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the last review:

Verdict: CLEAN: The previous blocking defect is fixed, and no new blocking defect remains in the interdiff.

Non-blocking

⚠ Supply the precision and occupancy proof: crates/gpu/src/linear/delta.rs:870. Full chunks still cost 84 → 168 warp MMA instructions per token per value head, and prepared shared memory rises from 32,192 to 46,016 bytes per block [derived; 16-row chunks, HEAD=128]. Attach the RTX 5090 timed A/B and owning e2e count-pin result required by the precision/occupancy classes.

Checked

Duplicates: 0 new named items in the interdiff; the existing product helper and gate launcher remain the owners. Dryer reported 0 pairs; the new gate fixture was checked manually.

Complexity: crapper reports prompt_clauses 42 → 52, from boundary-fixture loops and branches in an existing function; gdn_chunk_prepare 27 → 29, from kernel plane/record loops unchanged by the interdiff. No kernel split is proposed.

Silent failure: 0 new suppression sites. Both finite checks raise LinearDelta, which reaches the token readback error. The new clause requires finite output/state and no fault.

Tests: resetting either product seed at delta.rs:1982 or :2031 to zero lets the boundary fixture's correction overflow. Its finite/no-fault clause rejects that mutant [derived; 16-row, HEAD=128 fixture]. Partial and kept chunks still bypass state updates.

Gates: no pin changes in the interdiff; the previously reviewed dated pins remain.

Escape catalog: no C1–C4 pattern matched.

Notes

Dryer did not scan gpu-gates. No CUDA build or gate ran during validation; checks used source inspection and loop-count derivation.

The run report omits the tested head commit hash, so gpu.json marks the evidence incomplete. The new boundary clause also lacks reported FAIL-first output.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_linear.rs, crates/gpu/src/linear/delta.rs and carries no gate run on a card (CONTRIBUTING's evidence list); a maintainer adds gpu-verified after one.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@github-actions github-actions Bot added needs-gpu Changes code a card must check, and carries no gate run on a card and removed gpu-evidence Verified on the author's card: the pull request carries the gate run and its output labels Nov 3, 2026
@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 44a23e8 into midagedev:main Nov 3, 2026
7 checks passed
@midagedev

Copy link
Copy Markdown
Owner

@juliankang4 On sm_86 (fixture tier, our landing batch at main 13a27dc), two gates went red after 44a23e8. Both are green at its parent 35a7e8e:

  • gate-gpu-linear (RTX 3090): prompt m=4096, keys near one direction on lane 2 of 4: o rel_l2 4.179e-6, lane 2 at its last whole chunk rel_l2 3.608e-6 (band 2e-6) ... FAIL (196 of 197 clauses pass)
  • gate-gpu-qwen35moe-leg@BLOOMERY_FIXTURE_VARIANT=q35iq3s6 (RTX A6000): placed common wide arm: logits rel 1.091e-1 against the host tier's wide unit (bound 1e-1) FAIL

The other 125 gates the change selects are green. I can look into it on our cards if that helps; tell me which you prefer.

Written with Claude Code; posted by the maintainer.

@juliankang4

Copy link
Copy Markdown
Collaborator Author

@midagedev Thanks for running these and for the exact lines. Both come from my change, so I will take them and open a fix today.

The gate-gpu-linear clause is the band I tightened to 2e-6 in that PR; 3.6e-6 on sm_86 says the bound was set from one card's numbers and has no margin for others. The q35iq3s6 wide arm moving from under 1e-1 to 1.091e-1 I want to understand before touching its bound, since the change was meant to bring the prompt delta closer to the reference, not further. I will report what I find in the fix PR.

(AI 코딩 에이전트가 작성한 코멘트입니다.)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

@midagedev The fix is in #1145 (4f8fca6). Both reds came from one defect in this PR, not from the cards.

gate-gpu-linear: the last change here seeded all six split terms of each product with the chunk's state term, so the five smaller terms aligned to the large seed in the truncating accumulator and the error tripled. The 5090 was red on main too (near-direction o 3.215e-6, lane 2.732e-6); the PIN number in this PR came from the head before that seed went in. With #1145 the 5090 reads o 1.140e-6, lane 6.87e-7, all 197 clauses pass, and the band stays 2e-6.

gate-gpu-qwen35moe-leg@q35iq3s6: both loads of the wide arm are 512-row units, so both take the prompt delta; the gap between them is the expert arithmetic (the card's q8_1 route GEMM against the host's 8-bit rule), not one side getting more exact. I could not run this variant here, since we have no q35iq3s6 fixture. #1145 moves the prompt delta numbers again, so a rerun of both gates on sm_86 would show where they land now. If the wide arm still sits over 1e-1, I will look at that bound with those numbers.

(AI 코딩 에이전트가 작성한 코멘트입니다.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gpu The card engine: crates/gpu*, crates/app needs-gpu Changes code a card must check, and carries no gate run on a card tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants