Skip to content

hc_gated: read the normed streams once for an inject site's down and inject on GB10 - #759

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-hc-packdown
Oct 24, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-hc-packdown

Conversation

@juliankang4

@juliankang4 juliankang4 commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Qwen3.8 prompt 처리의 HC에서 inject가 있는 자리의 down과 inject를 BF16 tensor core 곱 한 번으로 합쳐, GB10에서 정규화된 스트림을 한 번만 읽게 하는 작업입니다.

Part of #565.

What and why

At each HC site that has an inject weight, Qwen3.8's prompt processing runs a BF16 down product of 320 rows (bf16_mma_gemm) and then a separate F32 inject product of 4 rows (f32_tile_gemm). Both read the normed streams xn from memory.
On FP4-Local, one 4096-id prompt spends 177.9 ms of GPU time in these two kernels on a GB10 (98.4 ms in the down product over 97 launches, 79.5 ms in the inject product over 96 launches).
This PR reads xn once for both. On GB10 an inject site's down rows and its inject rows are one BF16 tensor core product of 324 rows. A 128-row MMA tile makes 320 and 324 rows three row tiles each, so the grid stays the same size, and the separate inject launch is gone.

  • HcDownRows (crates/gpu/src/hc_gated.rs) is a tile weight that presents the down words and the inject rows as one 324-row weight. It reads both existing buffers in place, so nothing is copied at load. The down piece is fetched with cp.async; the inject piece reads eight F32 values and writes them to shared memory as BF16.
  • hc_gated_down_mma_4 is the new kernel entry. It runs bf16_mma_body over HcDownRows and writes [m][324], with each column's inject dot after its down dot.
  • hc_gated_lo_4x320 takes a packed argument and reads [m][324] when it is set, and [m][320] followed by [m][4] otherwise.
  • HcWideScratch drops its separate inj buffer and grows down by the same size, so the scratch keeps its total bytes.
  • enqueue_mix takes the packed path for a BF16 down with an inject on dense_mma (GB10). A Q8_0 down and every path off GB10 are unchanged.
  • land_values and mma_launch in crates/gpu/src/gemm/ become visible inside the crate for the new tile weight, and bf16_dot_rel moves out of check_mma in crates/gpu-gates/src/rounding.rs so the gate shares it.

The one numerical boundary this changes is that xn is rounded to BF16 in the inject dot, which the down product already does.
Both Qwen3.8 checkpoints store the inject weight as BF16 [4, 10240] (98 tensors each), so rounding the weight to BF16 for the tensor core is exact. The replay path in the gate keeps the F32 values.
This also supplies the base and head GB10 comparison that the review of #756 asked for with the next HC kernel change.

Release notes: Changed: Qwen3.8 prompt processing reads the normed streams once for an HC inject site's down and inject on GB10, which cuts GPU time in those two kernels.

Code shape

  • No second copy. HcDownRows is the only place that knows the packed layout. bf16_dot_rel is lifted out of check_mma so both gate checks call it, and the gate decides its BF16-and-GB10 condition in one place.
  • Model code holds only architecture facts. No model file changed, and no per-model branch, flag or trait method was added to a common owner.
  • Dead code is gone. HcWideScratch::inj is removed.
  • No silent failure. The packed path goes through mma_launch's shape check and the 16-byte alignment check of both buffers, and a failure is a named GpuError.
  • Comments state what is true now. No dates or measurements in the new comments.
  • No relaxed gate. No band was loosened. The weight band gains a BF16 inject bound with PIN(2026-10-24) and its reason, and the unsafe ratchet is re-pinned for the new blocks, each with a SAFETY line.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up. just mac-fmt-check and just mac-lint pass on a Mac. The CUDA crate's device code does not build there, so I did not run clippy on it.

Duplication found or removed (path:line), or "none": the duplicate scan flags hc_gated_down_mma_4 (crates/gpu/src/hc_gated.rs:2178) against the MMA entries q8f32.rs:1923 and gemm/kernels32.rs:1068. They share only the entry shape of one stage and one body call. Each entry is monomorphic in its weight type, and bf16_mma_gemm's weight type cannot carry two buffers, so they do not merge.

What I ran

Gate and greedy output

The gate below ran on this head's device code on a DGX Spark (GB10, driver 595.84, CUDA 13.3).

BLOOMERY_ARCH=sm_121 just gate-gpu-hc-gated
PASSED: gate_hc_gated the mix within its bands of the f64 rule at 4x2560 rank 320 (inject site and head); every stage its card rule bit for bit on the card's inputs, the down partials the q8f32 gemv of the view; eight columns each column's one-column mix and a rerun bit for bit; combine and init as 

It checks, among others, that hc_inject stays within its band (worst_err_over_band=2.245508e-2, kl=3.087345e-6, against q8_kl=7.706974e-5 for the Q8 representation), that a subnormal inject value in the out-of-range row replays bit for bit like the F32 tile, and that eager and graph runs give the same bits.
In the server A/B below, NVFP4 gave identical tokens on main and head in all three 128-token greedy windows.
On FP4-Local, two of three windows are identical. In the second window the output differs from token 115 of 128, the same on both head arms and the same on both main arms. After the split, the head output is ordinary Python code with no repetition or nonsense tokens. I did not measure a model-level KL: the serving harness here has no logit KL tool for Qwen3.8, so the site-level hc_inject KL above is the only KL number.

FAIL-first

Each of three changes to the new device code turns the gate red on this head.

  • Reading the packed inject index off by one in hc_gated_lo_4x320 fails wide[glue:wgt].
  • Swapping the two values of an inject piece in HcDownRows fetch makes dense_mma hc_inject leave its band by a large factor.
  • Breaking the inject replay staging in HcDownRows fails the new wide[inject-range] check.
wide[glue:wgt] m=37 the card rule on the readback bit_equal=false FAIL
FAIL-first gate-gpu-hc-gated mutant mglue rc=1 (want nonzero)
dense_mma hc_inject columns=37 q8_representable=true worst_err_over_band=8.841552e0 kl=2.059635e-1 q8_kl=7.706974e-5 error_l2=1.135812e1 q8_error_l2=1.997122e-1 FAIL
FAIL-first gate-gpu-hc-gated mutant mrows rc=1 (want nonzero)
wide[inject-range] m=37 the out-of-range row the F32 tile's bit_equal=false FAIL
FAIL-first gate-gpu-hc-gated mutant mstage rc=1 (want nonzero)

PTX and spill

The PTX comparison against upstream main b66d38b4e on a DGX Spark (generate_ds41, sm_86 declarations, JIT at cc 12.1), on an earlier build with this head's device code, lists one added entry and one changed entry, both the intended ones:

  base entries  398
  head entries  399
  added         1: hc_gated_down_mma_4
  removed       none
  changed       1: hc_gated_lo_4x320 (ptx-scan-md5:30762760->b64afbbe)
ptx-compare: changed (rc 3)

hc_gated_lo_4x320 changes because of its new packed argument. The dense MMA entries bf16_mma_gemm, fp8_b128_mma_gemm and mxfp8_mma_gemm and hc_gated_up_mma_mix_4 are not among the changed entries. The spill check on the compared build reports the new entry with no spill and no JIT local bytes at sm_121, sm_121a and sm_86:

generate_ds41 hc_gated_down_mma_4 unpinned spill=0 jit_local=0

That run printed the entry as unpinned, because the pin rows were not yet in that build. This head adds its four tools/ref/ptx-shapes rows at 0 0. I did not rerun the spill check on this head, and I did not measure the cc8.6 driver JIT, so the sm_86 row keeps the zero stack frame ptxas reported.

Speed

Qwen3.8 FP4-Local and NVFP4 on one DGX Spark (GB10), C = 1, draft off, a 4096-id code prompt of 128 greedy tokens, three requests per arm. Each checkpoint began with a discarded warm load, main and head alternated in one sitting, and the clock cap was lifted only around each arm's requests. FP4-Local ran two arms per tree, six requests in all; NVFP4 ran one arm per tree. Kernel times come from one profiled request per arm.

Metric (one DGX Spark, GB10, 4096-id prompt, C@1, draft off) main head change
FP4-Local prefill tok/s, median of six 4096-id requests 1412.8 1444.3 +2.2%
FP4-Local TTFT ms, median of six requests 2904.5 2843.5 -2.1%
NVFP4 prefill tok/s, median of three requests 1412.2 1312.2 within request spread, not used
NVFP4 prefill tok/s, one profiled request 1408.5 1419.8 one request each
FP4-Local down and inject GPU ms in one 4096-id prompt 177.9 99.6 plus about 1 about -77 ms
NVFP4 down and inject GPU ms in one 4096-id prompt 179.4 99.7 plus about 1 about -79 ms
FP4-Local whole prompt pass GPU ms 2877.8 2796.2 -2.8%
NVFP4 whole prompt pass GPU ms 2815.1 2791.4 -23.7 ms

Delta (head against main): the down and inject kernels of one 4096-id prompt take about 77 ms less GPU time on FP4-Local (177.9 to 99.6 ms plus about 1 ms) and about 79 ms less on NVFP4 (179.4 to 99.7 ms plus about 1 ms); FP4-Local end-to-end prefill +2.2% (1412.8 to 1444.3 tok/s) and TTFT -2.1% (2904.5 to 2843.5 ms), from one sitting, so a single observation whose request distributions overlap; the NVFP4 end-to-end requests varied by 15% inside one arm and are not a result.

The kernel saving is the same on both checkpoints, and the 61 ms drop in FP4-Local TTFT is about the size of the 77 ms kernel saving.
The whole prompt pass on NVFP4 shows -23.7 ms where the two kernels save about 79 ms, because an untouched routed GEMM (gemm_nvfp4p) ran 29.3 ms slower in the head arm.
On FP4-Local the end-to-end gain is small and the distributions overlap: two of the head arm's requests (1384.4 and 1377.1 tok/s) are below main's lowest request.
The three NVFP4 head requests read 1491.8, 1297.7 and 1312.2 tok/s from one server in one arm, a 15% spread that a kernel difference of about 2.8% cannot explain, so I do not read the NVFP4 end-to-end pair as a result.
On the profiled NVFP4 request, head reads 1419.8 and main 1408.5 tok/s, one request each.
Decode is the same on both sides for each checkpoint.
A kernel micro-benchmark on synthetic inputs (m = 4095, clock cap on) gives the same direction: one enqueue_mix call took 7.20 and 7.25 ms on main and 6.44 and 6.40 ms on head (-11%).

Measured build against this head

The A/B ran on an earlier build of this commit over b66d38b4e. This head is the same commit rebased onto the current upstream main, whose new commits add the MLX 4-bit format for the Metal backend; in crates/gpu they only add that format to the site list (site.rs) and the qwen35moe body (body35.rs), where no CUDA kernel reads it, and the HC kernels and the Qwen3.8 prompt path are unchanged. The final commit differs from the timed build only in the gate test and four new tools/ref/ptx-shapes rows. The device code is the same. The gate above, the FAIL-first runs and the Mac checks ran on this head, and the kernel micro-benchmark ran on an earlier build whose down, inject and glue code equals this head's.
just mac-check, just mac-lint (unsafe ratchet 3 = 3) and just mac-fmt-check pass on a Mac, as do tools/check-unsafe.sh and the PTX name check.
I ran only just gate-gpu-hc-gated among the GPU gates. The gates that the changed crates also touch, and the full test run, were not run here.

Hardware

DGX Spark (GB10); nvidia-smi lines from the check job on this head:

| NVIDIA-SMI 595.84                 Driver Version: 595.84         CUDA Version: 13.3     |
|   0  NVIDIA GB10                    On  |   0000000F:01:00.0 Off |                  N/A |

Models: Qwen3.8 FP4-Local and NVFP4.

What I ran into

None of this blocks the PR.

  • The NVFP4 end-to-end prefill requests varied by 15% inside one arm of one server. The kernel saving is clear in the profile, but I cannot show it in that pair of arms and ran no second pair.
  • FP4-Local's second window changes its output from token 115 because xn is rounded to BF16 in the inject dot. The output stays valid code. A model-level KL is not measured, as said 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 Oct 24, 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 reachable behavior defect or proven quality defect survived review.

Non-blocking

⚠ Complete the precision-change validation: crates/gpu/src/hc_gated.rs:2351. The inject now rounds activations to BF16, and the body reports a changed greedy window. AGENTS.md's precision class requires the gate-gpu-e2e count pin; only the HC gate ran. Supply that result and the affected Qwen3.8 gate outputs to check the composed prompt path before merge.

⚠ Complete the measurement evidence: crates/gpu/src/hc_gated.rs:2178. The GB10, 4096-id, C=1, draft-off A/B has summaries but lacks the timed head hash, exact model filenames and timing commands with output. Add those artifacts so the measurements can be reproduced. Also supply the final-head gate-ptx-spill result; the reported run predates the new pin rows.

Optimization headroom

  • G. Per-launch and per-sync fixed cost: crates/gpu/src/hc_gated.rs:2450. Folding the elementwise bottleneck and weight glue into the packed entry, using the existing MMA tile owner, would remove one launch per packed inject site per prompt batch, or 1/m launches per prompt token for a batch of m tokens [derived].

Checked

Duplicates: the packed MMA entry reuses bf16_mma_body; both precision checks use bf16_dot_rel. No duplicate finding.

Complexity: enqueue_mix adds packed dispatch; check_wide adds replay and precision checks; wide_mix adds layout unpacking.

Silent failure: 1 default site followed. wgt_ref.unwrap_or(&[]) receives Some from site.host(true). Non-finite dots reach the HC fault word and NaN outputs.

Tests: hc_gated.rs:2094 off-by-one → glue assertion; hc_gated.rs:1743 swapped lanes → MMA band; hc_gated.rs:1718 wrong replay staging → bit equality. The PR reports all three FAIL-first runs. The new entry is included in the local-depot check.

Gates: the BF16 weight-band extension and unsafe ratchet carry dated PIN reasons. No gate removed.

Escape catalog: C1–C4 do not match this diff.

Notes

Static validation; no GPU gates executed here. No ledger lessons are in force.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_hc_gated.rs, crates/gpu-gates/src/rounding.rs, crates/gpu/src/gemm/f32tile.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 ba0e299 into midagedev:main Oct 24, 2026
8 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.

1 participant