Skip to content

gpu-gates: carry the GEMM contracts' exceptional range through the host transcriptions - #704

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:fix-gate-nvfp4-host-range
Oct 23, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:fix-gate-nvfp4-host-range

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

GEMM gate의 host 계산이 kernel의 예외 범위 처리를 따르게 해서, 올바른 유한 결과를 gate가 틀렸다고 판정하던 문제를 고칩니다.

What and why

The GEMM gates compare each kernel output bit for bit with a host transcription of the kernel's contract. Three of those transcriptions (dot32_nv, dot32 and gate_gemm::dot_ref) still walked the blocks in plain F32. The production epilogue (scaled_sum) leaves F32 when an intermediate leaves the normal range and finishes in F64, and for NVFP4 it keeps the sum in F64 through the expert scale.

Fixes #702. For K 64, weight codes 2, sub-block scales 0.5, activations 127 * 2^120 and expert scale 2^-120, the NVFP4 transcription overflowed to infinity while the kernel stores 8128, so the gate would have rejected a correct kernel. The same gap shows on the plain 32-value and K-quant paths where a step lands in the subnormal range: with d_a 2^-149 and an inner sum of 1.5 - 2^-24, the F32 transcription gives 2^-148 and the kernel gives 2^-149. The transcriptions came in with 2bf3a7d.

crates/gpu-gates/src/rounding.rs now holds one host scaled_sum with the production rule, and all three transcriptions walk with it. The NVFP4 one accumulates in F64 and stores scaled_sum([acc, 0], [scale, 0], 1, 0) as F32; the others keep an F32 accumulator and round each step. For inputs in the normal range the new walk gives the same bits as before, so existing gate rows keep their values. No kernel code changes.

g32_nvfp4_dense gets the boundary from the issue as one extra row (K 64, inputs 127 * 2^120, scale 2^-120): every output must store 8128 and match the contract bits. Three host tests pin the three transcriptions. The gate-gpu-gemm recipe's test filter now also names kquant_contract_keeps_a_subnormal_step_exact, which workers_must_complete did not select, so that test runs with the recipe.

Release notes: internal: the GEMM gates' host transcriptions follow the kernels' exceptional-range rule, so a correct finite NVFP4 result is no longer rejected. No user-visible change.

Code shape

  • No second copy. The three transcriptions share one owner, rounding::scaled_sum, and the old F32 epilogue copies are gone.
  • Model code holds only architecture facts. No model file or common owner is touched.
  • Dead code is gone. Nothing is left unused.
  • No silent failure. Input checks are unchanged.
  • Comments state what is true now. The comments describe the rule, with no dates or issue numbers.
  • No relaxed gate. No band, tolerance or lint moved; the change adds a boundary row and three tests.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line): the production scaled_sum (crates/gpu/src/gemm/grouped.rs:115) is written with device intrinsics that cannot run on the host, so the gate's host version in crates/gpu-gates/src/rounding.rs is its independent transcription and the only host copy.

What I ran

Host tests, before the fix (two of the three new tests fail):

cargo test -p bloomery-gpu-gates --lib gemm32::tests
left: (2139095040, 8128.0)  right: (1174274048, 8128.0)   # nvfp4: infinity
left: 2  right: 1                                          # plain: 2^-148 instead of 2^-149

After the fix:

cargo test -p bloomery-gpu-gates --lib && cargo check -p bloomery-gpu-gates --all-targets
test result: ok. 144 passed; 0 failed; 3 ignored; 0 measured; 0 filtered out

GPU, on the machine below:

just gate-gpu-gemm --case g32_nvfp4
gemm32 case=g32_nvfp4_dense range K=64 m=1 stored_8128=true band_off=0 contract_bits_differ=0 PASS
PASSED: gate_gemm

The recipe's first step runs workers_must_complete and passes 3 tests. The new K-quant test passes under the same gate command with its name as the filter, and fails when dot_ref goes back to the old F32 epilogue:

test gate::g32::tests::kquant_contract_keeps_a_subnormal_step_exact ... ok

Not run: cargo clippy and the contribution checks on this head, and the other g32_* cases.

Hardware

NVIDIA GeForce RTX 5090, 595.99.02
| NVIDIA-SMI 595.99.02              Driver Version: 595.99.02      CUDA Version: 13.2     |

The NVFP4 gate cases ran with BLOOMERY_ARCH=sm_86.

What I ran into

Nothing here blocks this PR. No gate case yet exercises the K-quant range boundary on the GPU; the K-quant fix is covered by the host test only.

AI help

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

…st transcriptions

The GEMM epilogues walk a block in F32 and walk it again with scaled_sum
when a step leaves the F32 normal range; the NVFP4 walk keeps that sum in
F64 until the expert scale. The host transcriptions (gemm32's dot32 and
dot32_nv, gate_gemm's dot_ref) still summed in F32 alone, so dot32_nv
returned infinity where the kernel stores a finite value (K 64, codes 2,
sub-block scales 0.5, d_a 2^120, expert scale 2^-120: 8128), and a step
whose product falls under the normal range rounded differently from the
kernel.

All three now walk with one host transcription of scaled_sum in
rounding.rs, the NVFP4 one in F64 as the kernel does. Its exceptional
step is one fused F64 multiply-add, as the kernel's `inner * scale + acc`
contracts to one; a separate product and sum would cancel two opposite
steps to 0 where the kernel keeps their residual (K 64, sub-block dots
(24384, 127) then their negation, sub-block scales (224, 2^-10), d_a
(1 + 2^-23)·2^120, expert scale 2^-120: 2^-33). g32_nvfp4_dense checks
both boundaries.
@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 Oct 23, 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: The host transcriptions preserve the device contracts' exceptional range and rounding.

Non-blocking

⚠ Complete the card evidence: crates/gpu-gates/src/bin/gate_gemm.rs:5304.
The pasted output uses stored_8128; this head emits stored_eq and adds the cancellation row. Attach both tested commit hashes and this head's just gate-gpu-gemm --case g32_nvfp4 output, including both boundary rows. This makes the card run traceable to the reviewed code.

Checked

Duplicates: 4 new functions checked by Grep. No duplicate host owner; the device implementation cannot run on the host. Dryer supplied no pairs.
Complexity: nvfp4_dense_case 48 → 69 adds a boundary-fixture stage. New scaled_sum at 20 implements one range decision in three stages: inner eligibility, F32 result, F64 fallback.
Silent failure: 0 new default/discard sites. Added fallible operations propagate errors; numeric and fault failures reach the case verdict.
Tests: mutation review covers dot32_nv's F64 accumulator, scaled_sum's fused fallback, and dot32/dot_ref's exceptional steps. Restoring F32 accumulation gives infinity; unfusing the cancellation gives zero; restoring either old subnormal epilogue gives bits 2 instead of 1. Assertions reject each mutant. The device rows also reject a dropped block or an unwritten output. The recipe selects the K-quant regression by name.
Gates: no threshold moved or case removed. Overflow, cancellation, subnormal, and zero-scale paths checked against the device rule. The GLM gate reuses the changed common transcription.
Escape catalog: no matching pattern.

Notes

Dryer did not run, as reported in diagnostics. Rust tests and card gates were not run during this review. Card evidence is incomplete because the PR body omits both tested commit hashes and output for the added cancellation row.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu-gates/src/gemm32.rs, crates/gpu-gates/src/rounding.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 c681eb0 into midagedev:main Oct 23, 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 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.

gpu-gates: NVFP4 host transcription overflows before expert scaling

1 participant