Skip to content

gpu: retain both BF16 parts in the chunked delta products - #816

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-fix-fp4l-4128
Oct 25, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-fix-fp4l-4128

Conversation

@juliankang4

@juliankang4 juliankang4 commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

긴 프롬프트를 chunk로 처리하는 선형 어텐션 커널이 BF16 반올림 때문에 상태를 잘못 갱신하던 문제를 고칩니다.

What and why

The chunked prompt form of the Gated DeltaNet kernels (crates/gpu/src/linear/delta.rs) rounded each F32 operand of
its matrix products to one BF16 value. A state that sits between two BF16 values was therefore treated as if it had
moved, and the rule produced a false update on every chunk. With Q = K = e0, beta = decay = 1 and v = 1.00390625 the
state must stay at v, and it did not. On the FP4-Local checkpoint the error is large enough to flip the argmax at
position 4,128.

This change splits each F32 operand into a BF16 high part and a BF16 residual, and accumulates the high-high product
and the two cross products in F32 through the existing chunk_mma. Q and K, the inverse, and the solved update keep
their low parts. The already consumed Q tile is reused for the low K transpose, so the prepared kernel stays at
48,832 B of shared memory. The chunk record grows from 304 to 560 words per chunk; the short-walk reserve in
scratch38.rs and place.rs is recounted, and the 32 MiB reserve still holds it. Launch counts, carry, lanes, stamps
and the gate bands are unchanged. gate_linear gains two clauses for the fixed point above, one starting at v and one
starting at zero. tools/unsafe-ratchet.txt moves the GPU pin to 2621 blocks and 326 unsafe fns for the new blocks,
each with its SAFETY line.

Release notes: Fixed: Long prompts on Qwen3.8 linear-attention layers no longer accumulate BF16 rounding error in the chunked state update.

Code shape

  • No second copy. The BF16 pair split and the keys-by-time transpose each have one owner, and the products reuse chunk_mma.
  • Model code holds only architecture facts. No model code changes.
  • Dead code is gone. The old inline transpose body moved into the shared helper.
  • No silent failure. No new error path; the existing alignment and refusal clauses are unchanged.
  • Comments state what is true now. The new comments describe the current precision contract and tile bounds.
  • No relaxed gate. No band or tolerance moved; the one re-pin is the unsafe ratchet, dated with its reason.
  • cargo fmt is clean on what I touched, and cargo clippy is clean for bloomery-gpu-gates and bloomery-model. bloomery-gpu needs CUDA, so clippy did not run on it here; its code was built and exercised by the gate below.

Duplication found or removed (path:line), or "none": none.

What I ran

Format and lint on a Mac, on this head:

just mac-fmt-check
cargo clippy -p bloomery-gpu-gates -p bloomery-model
Finished `dev` profile [unoptimized + debuginfo] target(s) in 9.95s

The unsafe ratchet check accepts the new GPU counts.

The owning GPU gate, on a DGX Spark. The run used this branch's kernel code; the head that is posted differs from it only in SAFETY comment text.

| NVIDIA-SMI 595.84                 Driver Version: 595.84         CUDA Version: 13.3     |
|   0  NVIDIA GB10                    On  |   0000000F:01:00.0 Off |                  N/A |
just gate-gpu-linear
prompt m=4096 on lane 2 of 4: o rel_l2 6.636e-6, lane 2 at its last whole chunk rel_l2 5.164e-6 (band 2e-2)
prompt fixed point between BF16 values from 1.0039063: state rel_l2 0.000e0 PASS
prompt fixed point between BF16 values from 0: state rel_l2 0.000e0 PASS
shape kernel=gdn_delta_prepared local_depot=false ld_local=0 st_local=0 PASS
gate_linear: 132 clauses, 0 failed
gpu-gate.sh: gate_linear ran 25 s (exit 0)

The 4,096-row state error is 5.164e-6, inside the existing 2e-2 band, and the kernel has no local-memory spill.

On the FP4-Local checkpoint served by the feature server, the argmax at position 4,128 is token 364, the same token the f32 reference picks.

Hardware

DGX Spark with one GB10, driver 595.84, CUDA 13.3, sm_121a build. The gate above ran on it with the synthetic Qwen3.6-35B-A3B-shaped inputs that gate_linear uses; the argmax check ran against the FP4-Local checkpoint.

AI help

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

@github-actions github-actions Bot added gpu The card engine: crates/gpu*, crates/app model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja needs-gpu Changes code a card must check, and carries no gate run on a card labels Oct 25, 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: CLEAN: No must-fix defect was found in the changed code.

Non-blocking

⚠ Supply the precision-change proof: crates/gpu/src/linear/delta.rs:1545.
Prompt processing increases from 448 to 1,344 warp MMA instructions per value head per complete 16-token chunk: +56 per head per token [derived]. Prepared shared memory also grows from 45,760 to 48,832 B per block [derived].
Before this becomes the timing baseline, attach the error model, count-pin gate and timed A/B required by AGENTS' “float sum order or precision” class, plus the final affected Qwen class gates. Include resource counts, both tested hashes and the exact model file.

⚠ Exercise the remaining residual paths: crates/gpu-gates/src/bin/gate_linear.rs:3023.
With Q = K = e0, the Q/K, inverse and causal matrices have zero residuals. Dropping those residual paths leaves both new state assertions green.
Extend this clause with non-BF16, multi-key inputs and check output as well as state. Attach FAIL-first output. Identify a reusable state/argmax probe, or explain why the gate output supplies the fix's debugging layer.

Checked

Duplicates: 5 new items checked (3 helpers, 2 offsets); no duplicate owner found. Dryer reported 0 model pairs; GPU code was checked manually.
Complexity: prompt_clauses 35→41 adds fixture setup and two cases; gdn_delta_prepared 45→51 adds precision passes. The kernel was checked for correctness without proposing a split.
Silent failure: 0 new default/error-suppression sites; 2 new conversion/product paths followed. Non-finite output/state raises LinearDelta; token readback returns GpuError::Fault.
Tests: scalar mutation checks of the state-residual selection at delta.rs:1566 and solved-row cross product at delta.rs:623 turn the respective new assertions red. No device test was run here.
Gates: bands unchanged; unsafe ratchet has a dated PIN and reason. Record bounds and the 133,124 B extra scratch peak were checked [derived, Qwen3.8, m=1..4096, carry=0..15].
Escape catalog: C1–C4 do not match.

Notes

Dryer did not scan crates/gpu or crates/gpu-gates. The reported GB10 run lacks both tested commit hashes and an exact checkpoint path, so gpu.json records incomplete evidence.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_linear.rs, crates/gpu/src/arch/qwen3moe/scratch38.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)

@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 4d3ab0f into midagedev:main Oct 25, 2026
7 checks passed
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 model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja needs-gpu Changes code a card must check, and carries no gate run on a card

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant