Skip to content

hc_gated: read the fused HC mix in whole sectors and prefetch its streams into L2 - #756

Merged
juliankang4 merged 3 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-prompt-hc
Oct 23, 2026
Merged

juliankang4 merged 3 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-prompt-hc

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Qwen3.8 prompt 처리에서 HC의 up과 mix를 합친 커널이 메모리를 읽고 쓰는 방식을 바꾸고 L2 prefetch를 더해, GB10에서 이 커널의 시간을 줄이는 작업입니다.

Part of #565.

What and why

Qwen3.8's prompt pass runs one fused HC up and mix kernel, hc_gated_up_mma_mix_4, for each HC block: 97 launches for a 4096-id prompt, 273.0 ms of GPU time on a GB10. After its MMA, the mix step had every lane read a different column, so one warp step touched 32 lines of xn and mixed and used 4 bytes of each. This PR changes that step and starts its reads earlier:

  • mma_mix (crates/gpu/src/hc_gated.rs): lane L mixes hidden index L % 8 of every fourth column from L / 8 (MIX_HIDDEN, MIX_STEP_COLS). A warp step now reads and writes four whole 32-byte sectors of xn and mixed. The gate scratch goes through mix_slot, which permutes the columns of each row so those 32 reads land in 32 banks. Each output still mixes its streams in the same order, so values and faults are the same bit for bit.
  • Before the MMA, each thread asks L2 for two lines of the tile's xn (XN_LINES, 512 lines a tile) with cores::l2_prefetch, so the mix finds them there. The prefetch is a hint: it changes no value, fault or launch.
  • The prefetch needs the tile's origin, so mma_tile_origin in crates/gpu/src/gemm/f32tile.rs now owns the tile raster for both callers: bf16_mma_tile (the dense MMA entries) and the HC kernel. The helper takes the weight type, so each caller uses its own weight's group size, and it keeps the let-else shape of the dense body's raster. MMA_ROWS and MMA_COLS become pub(crate) for the prefetch.
  • The new step keeps the addresses of four columns live next to the tile's waiting cells, so the kernel spills more: 492 bytes at sm_121 (260 before) and 176 driver-JIT local bytes (96 before). The four tools/ref pin rows move with PIN(2026-10-24) lines.

Release notes: Changed: the fused HC up and mix kernel of the Qwen3.8 prompt pass reads whole sectors and prefetches its normed streams into L2, which cuts that kernel's GPU time on GB10.

Code shape

  • No second copy. The tile raster has one owner, mma_tile_origin (crates/gpu/src/gemm/f32tile.rs:624), called by bf16_mma_tile and by the HC kernel. The mix order, slot map and prefetch each have one owner in hc_gated.rs.
  • 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. The HC block is replicated on every rank, so no rank constant appears.
  • Dead code is gone. Nothing was made unused.
  • No silent failure. No new input path and no Result change. The prefetch stays inside one stream of a live column, and the entry's launch check still validates sizes and shapes first.
  • Comments state what is true now. No dates or measurements in the new comments.
  • No relaxed gate. No band or tolerance moved. Four spill pin rows are re-pinned with PIN(2026-10-24) and their reason, and their earlier PIN lines are kept. No unsafe block was added and no test or gate case was removed.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up. cargo fmt --all -- --check passes on the GB10 machine, and cargo check --workspace --all-targets passes on a second machine. I did not run clippy for the GPU crate: the machine I edit on cannot build it. The helper carries one #[allow(clippy::question_mark, ...)] with its reason, because replacing the let-else with ? moves the dense entry's PTX.

Duplication found or removed (path:line), or "none": none. The tile raster has one owner, mma_tile_origin (crates/gpu/src/gemm/f32tile.rs:624), and the duplicate scan reports no other candidate.

What I ran

Output is unchanged

The new step visits every output in the same order as before, and the gate compares the stages with their rules bit for bit. The gate below ran on this branch's device code on a DGX Spark (GB10, driver 595.84, CUDA 13.3). The raster helper and prefetch it ran against are the ones in this head.

BLOOMERY_BOX_LOCAL=1 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 the rule; shapes, columns, planes and types refused by name; a NaN stream raises the site and leaves the other column clean; the wide arm's norm and combine the capped entries' bits, its glue the card rule, its mix within the error model's bands, each column its own, a NaN raised; a BF16 site the same over its widened values; no local depot

In the server A/B below, every 128-token greedy request gave the same tokens on main and head on both checkpoints. The HC wide arm runs only in the eager prompt pass, and the graph paths are not touched.

The three unit tests hc_mma_interleaved_rows_and_mix_order, hc_mix_slots_fill_the_scratch_and_every_bank and gemm::f32tile::tests::hc_mma_layout passed (tools/gate.sh --oxide -p bloomery-gpu --release --lib -- hc_m, test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 384 filtered out) on an earlier build of this branch. I did not rerun them on this head.

Dense entries

The three dense MMA entries that call bf16_mma_tile compile to the same PTX, with the same resources, as on upstream main (the just ptx-compare below), so their device code is unchanged and main's dense gate results hold for them.

FAIL-first

Two checks failed before the fix and pass after it.

hc_mix_slots_fill_the_scratch_and_every_bank checks that mix_slot fills the scratch exactly once and that each lane-wide read of the mix hits 32 distinct banks. With mix_slot returning row * MMA_WARP_COLS + col (no permutation), it fails:

thread 'hc_gated::tests::hc_mix_slots_fill_the_scratch_and_every_bank' (15398) panicked at crates/gpu/src/hc_gated.rs:2428:17:
Cargo test failed with exit code: Some(101)

A source check that the dense MMA and the HC kernel both reach the raster through mma_tile_origin::<W>(rows, n) fails on the version where the dense body kept its own copy, and passes on this head. It checks structure only:

AssertionError: dense MMA bypasses the weight-aware raster owner
raster owner: dense and HC share one calculation (worktree)

PTX and spill pins

just ptx-compare generate_ds41 --features gpu,deepseek41 of this branch's device code against upstream main (at 549df2ec7, before the later upstream commits) on a DGX Spark lists 398 entries on both sides and one changed entry, the fused mix, which is the one this PR changes:

  base entries  398
  head entries  398
  added         none
  removed       none
  changed       1: hc_gated_up_mma_mix_4 (spill:0->1352 jit_local:96->176 ptx-scan-md5:63864751->5e39d156)
ptx-compare: changed (rc 3)

The three dense entries have the same PTX md5 and the same resource columns on both sides:

Entry PTX md5 (main = head) JIT registers Spill Shared bytes
bf16_mma_gemm c06c8803d4e59db84f8d69bdce1243ad 128 0 49152
fp8_b128_mma_gemm 9e9ff1d05c116ff93946406c48610512 122 0 49152
mxfp8_mma_gemm 2ee3845c6477fbac1424f3c8469e03e0 126 0 49152

Upstream main later gained a commit that splits the MMA stage loop and adds a per-weight group size. After rebasing onto it, I reran the compare and the HC gate on the result. A git range-diff and an empty diff of cores.rs, hc_gated.rs, gemm/f32tile.rs and gemm/mod.rs show that the head differs from the checked build only in the pin files, so the device results above carry to this head.

The spill pin rows were checked on the GB10 with ptxas 13.3.73 and the driver JIT. The old pins failed the ratchet and the new ones pass it, replayed on the retained scans of the three architectures:

Arch (GB10, cc 12.1) Spill JIT local Registers Old pin before the fix Ratchet before / after
sm_86 1352 176 128 1112 FAIL / PASS
sm_121 492 176 128 468 FAIL / PASS
sm_121a 492 176 128 468 FAIL / PASS
generate_ds41 hc_gated_up_mma_mix_4 spill=492 pinned=468 ABOVE
ptx-spill bin=generate_ds41 entries=1 pinned=1 nonzero=hc_gated_up_mma_mix_4(spill=492,jit_local=176) violations=1 FAIL
ptx-spill bin=generate_ds41 entries=1 pinned=1 nonzero=hc_gated_up_mma_mix_4(spill=492,jit_local=176) violations=0 PASS

The sm_86 build is never launched. I did not measure the cc8.6 driver JIT, so that pin keeps its earlier stack-frame value. The full GPU gate suite was not run, only the HC gate above.

Speed

Qwen3.8 FP4-Local and NVFP4 on one DGX Spark (GB10), C = 1, draft off, three 4096-id code windows of 128 greedy tokens, median of three requests. Each checkpoint began with a discarded warm load, FP4-Local ran head and then main, NVFP4 ran main and then head, and the clock cap was lifted only around each arm's requests. Kernel times come from one profiled request an arm.

Metric (one DGX Spark, C@1, draft off) main head change
FP4-Local prefill tok/s, median of three 4096-id prompts 1311.0 1317.6 +0.5%
NVFP4 prefill tok/s, one profiled 4096-id request 1280.3 1372.1 +7.2%
NVFP4 hc_gated_up_mma_mix_4 GPU time, ms in one 4096-id prompt 273.0 145.0 -46.9%
NVFP4 HC wide arm GPU time, ms in one 4096-id prompt 679.9 551.9 -18.8%
NVFP4 whole prompt pass GPU time, ms 3131.5 2990.3 -4.5%

Delta (head against main): the fused HC mix kernel -46.9% (273.0 to 145.0 ms) and the whole 4096-id prompt pass -4.5% of GPU time (3131.5 to 2990.3 ms); FP4-Local end-to-end prefill +0.5% (1311.0 to 1317.6 tok/s), inside the spread between requests, from one server run per arm, so a single observation.

The kernel gain is well outside the noise. One mix launch takes 1.49 ms on average instead of 2.81 ms, and the HC wide arm (norm, down, inject, up and mix) shrinks by 128.0 ms of the pass. The head's mix kernel read 145.0, 146.7 and 145.2 ms in three separate profiled runs, while main's read 273.0, 323.1 and 289.5 ms. The other HC kernels and the dense and routed GEMMs move by 9.9 ms or less.

The end-to-end number does not show the saving. FP4-Local prefill is 1317.6 tok/s on head against 1311.0 on main (+0.5%), and the three requests of the head arm alone span 1296.7 to 1343.3 tok/s, so this pair cannot separate them. On NVFP4 the main arm ran while the host kernel was compacting memory (free memory 1.0 to 1.1 GiB), so I do not use the three-request medians of that pair. Between the profiled requests of the same two servers, prefill is 1280.3 tok/s on main and 1372.1 on head (+7.2%), one request each. I ran no second end-to-end pair, so the end-to-end gain is not established beyond this one observation.

Measured build against this head

The A/B and the profiled kernel times ran on an earlier build of this branch over upstream commit e4204ccbb. That build did not yet share the raster helper with bf16_mma_tile, and upstream has since split the MMA stage loop. This head's fused mix therefore compiles to different PTX than the measured build, with 492 spill bytes at sm_121 instead of 468. I did not rerun the timings on this head, so the speed rows above describe the measured build, and I make no speed claim for this exact head beyond the gate and PTX results above.

Hardware

DGX Spark (GB10), GPU, driver and CUDA lines from nvidia-smi of 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. The pins were read with ptxas 13.3.73. The workspace compile check ran on a separate machine.

What I ran into

None of this blocks the PR.

  • The end-to-end prefill difference on FP4-Local, +0.5%, is inside the spread between requests, even though the kernel saves 141.2 ms of GPU time in the NVFP4 pass. I have not found where the saving goes in the end-to-end number.
  • A server's first timed arm after loading a checkpoint often reads slower whichever binary it runs: on one FP4-Local pair main read 969.8, 1069.5 and 1001.4 tok/s and head read 1304.7, 1378.5 and 1373.3, with decode at 26.1 against 30.3 tok/s. A discarded warm load removed it on FP4-Local, but not on NVFP4, where the host compaction above remained. I did not find the cause.
  • The remaining up and mix time is 1.49 ms a launch, against about 0.9 ms that 210 MB of reads take at the DRAM rate, and hc_gated_norm_4w still takes 225.9 ms of the pass. Those are separate changes.
  • just ptx-compare exits 3 because the fused mix entry changed. That is the intended change, and its pin rows are updated here.

AI help

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

The epilogue of `hc_gated_up_mma_mix_4` gave each lane one column of the
warp's 32 and walked the hidden indices of a pass one at a time, so every
warp-wide load of the normed streams and every store of the mixed values
touched 32 cache lines for 4 bytes each. Each lane now takes one hidden
index of the pass and every fourth column, so a warp-wide step reads one
stream of eight consecutive hidden indices in four columns and stores the
same eight values of each: four whole 32-byte sectors. The gates go through
the same warp scratch, its columns permuted by the row's hidden index so a
step's 32 gate reads land in 32 banks.

Each output is mixed in the same order (stream 0 to 3, then the mean), so
the mixed values and the fault are unchanged bit for bit.
`hc_gated_up_mma_mix_4` computes a 128 by 128 tile of up-projection gates
with the BF16 MMA and only then mixes the four normed streams of the tile's
columns with them, so every block waited on its `xn` loads from DRAM after
its MMA finished. Each thread now first asks L2 for two of the tile's 512
`xn` lines (one stream of one column, the tile's 32 hidden indices each),
and the mix after the MMA finds them there.

The prefetch needs the tile's origin before the MMA starts, so the raster
`bf16_mma_tile` walks moves into `mma_tile_origin`, which both call. The
prefetch is a hint: no value, fault or launch changes.
hc_gated_up_mma_mix_4 reads eight hidden values of four columns a step and keeps those columns' addresses live beside the tile's waiting cells. ptxas 13.3.73 and the GB10 driver JIT (595.84) read 468 spill and 176 local bytes at sm_121 and sm_121a, and 1112 spill bytes with a 176-byte stack frame at sm_86.
@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: No reachable behavior defect or proven quality defect remains.

Non-blocking

⚠ Time this head: crates/gpu/src/hc_gated.rs:1838. The kernel adds up to 512 prefetch hints per 128×128 tile (256 threads × 2 hints) [derived]. The reported A/B precedes this head's device code; sm_121 spills changed from 468 to 492 bytes. The occupancy / geometry class in AGENTS.md calls for a timed base/head A/B. Supply that run on GB10 for the final device code.

Optimization headroom

  • E. Less work: crates/gpu/src/hc_gated.rs:1764. Skip gate computation and scratch stores for columns beyond m; the read guard already excludes them. At 37 prompt columns and hidden=2560 on GB10, this removes (128−37) × 4 streams × 2560 × 4 = 3,727,360 shared-store bytes per invocation, about 100,739 bytes per prompt token [derived].

Checked

Duplicates: six new named items inspected. Both raster callers use mma_tile_origin, which reuses raster32.
Complexity: the added prefetch loop has a fixed two-iteration bound and a live-column guard.
Silent failure: off-grid tiles return, padded columns are excluded from mix reads, and non-finite mix outputs raise HcMix.
Tests: scratch ownership and 32-bank reads verified arithmetically. Removing the XOR at hc_gated.rs:1712 reduces distinct banks to four. Rust and GPU tests were not run here.
Gates: the three architecture tables carry dated spill PINs and reasons; no accuracy band changed.
Escape catalog: C1–C4 do not match.

Notes

The body includes GB10 identification and HC gate output, but omits the gate run's exact build commit and model file paths. GPU evidence is incomplete.

Card check: needs-gpu. This pull request changes crates/gpu/src/cores.rs, crates/gpu/src/gemm/f32tile.rs, crates/gpu/src/gemm/mod.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 b66d38b 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.

1 participant