Skip to content

gpu: Qwen3.8's ubatch walk allocates its BF16 streams and normed streams at their width - #1001

Merged
juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-next-state-scratch
Oct 30, 2026
Merged

juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-next-state-scratch

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Qwen3.8의 GDN 상태를 슬롯당 레인 하나로 줄이고, ubatch 걸음의 BF16 스트림 버퍼를 BF16 폭으로 잡아 GPU 메모리를 줄인 PR입니다.

What and why

A Qwen3.8 GDN layer kept four state lanes per slot so a verify could write each row's result beside the live state. It now keeps one lane, and a verify records each row's key, rounded update and head decay in a compact record buffer beside it (DELTA_VERIFY_ROWS = 4 rows). The ubatch walk also allocates its BF16 stream buffer, and on a dense MMA card with BF16 HC downs the wide HC normed streams, at their BF16 width instead of F32. Per slot the stores shrink by 329,122,224 B (36 GDN layers), and at ubatch 4,096 the load holds 496,894,096 B less. Throughput does not change and greedy output stays equal.

Release notes: Under the hood: Qwen3.8 keeps one GDN state lane per slot and allocates its BF16 stream buffers at BF16 width, freeing 329,122,224 B of stores per slot; output is unchanged.

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-gates/src/bin/gate_qwen4exp_e2e.rs:681 and crates/gpu-glm5next/src/pair.rs:72: different things. The gate's row count is the Qwen3.8 delta record's, held equal to runtime::stores::DELTA_VERIFY_ROWS by a const assert; GLM's LOAD_ROWS is an unrelated load constant.
  • crates/gpu/src/arch/qwen3moe/program38.rs:494 and :507: rec_of borrows a delta store's state, stamp and record, qsa_of a selecting store's planes, ring and pooled keys; each refuses the other variant by name, as the rest of the file does.
  • crates/gpu/src/gemm/f32tile.rs:2566 and :2581: reused. mma_launch_words counts BF16 words and calls the same tile_spans that tile_launch calls.

What I ran

Memory, real binary bloomery-serve-qwen38, Qwen3.8 FP4-Local with its MTP draft, upstream base 977e8e74 against the PR head, 6 rounds per arm in the order base, head, head, base, base, head, prompt 4,096 tokens, 128 new tokens. store_bytes and resident_bytes are the server's own load line:

Arm Metric Base Head Difference
1 slot, ctx 16,896 store_bytes 721,408,640 392,286,416 -329,122,224
1 slot, ctx 16,896 resident_bytes 79,741,229,384 79,244,335,288 -496,894,096
4 slots, slot ctx 4,352 store_bytes 548,000,384 218,878,160 -329,122,224
4 slots, slot ctx 4,352 resident_bytes 79,550,921,992 79,054,027,896 -496,894,096

greedy_equal: True, and no server error in the 12 rounds. This PR makes no speed claim.

Gates on a GB10 (commands from the repository root):

just gate-gpu-linear
gate_linear: 186 clauses, 0 failed — PASS
recovery refuses rows=1 lanes=4 lane_at=0: Err(Shape { what: "enqueue_delta_recovery", detail: "record size overflows usize" }) PASS

just gate-gpu-hc-gated
PASSED: gate_hc_gated ...
wide[bf16_scratch] m=37 the half scratch's mix the full one's bits PASS
wide[refuse:bf16_scratch] m=37 an F32 norm on a BF16 scratch -> hc_gated::enqueue_mix_wide: the scratch's normed streams hold 189440 f32, made for BF16 words; ...  PASS

just gate-gpu-qwen4exp-mtp-input
(iv) first 8 drafted greedy ids = plain ids PASS; draft kept 11 ids
test result: ok. 1 passed; 0 failed

just gate-gpu-qwen4exp-nvfp4
(p) the batch set by one pass: last argmax ours=11751 oracle=11751 ... PASS
(g) the batch set by one ubatch: last argmax ours=11751 oracle=11751 ... PASS
gpu-gate.sh: gate_qwen4exp_nvfp4 ran 60 s (exit 0)

BLOOMERY_TIER=fixture just gate-gpu-qwen4exp-mtp
gpu-gate.sh: gate_qwen4exp_mtp ran 329 s (exit 0)

BLOOMERY_TIER=fixture just gate-gpu-qwen4exp-e2e
verify T=4 k=4: rows [47835, 32521, 34727, 48499] vs steps [...] (4 compared), logits bit for bit: true; ... lane 0 (want 0, computed here) ... PASS
slots rollback isolation: slot 1's verify committed 2 of its 4 rows; slot 0's next 4 ids equal its solo run's continuation PASS
card leg plan: 4 of 4 layers hold routed experts on the card, a layer's count Some(254)..Some(255) (want 256 on every layer: half the experts, the header's budget) FAIL

The one FAIL in the e2e gate is a card-leg plan count that the lane change does not touch; every verify and slot clause the lane change reaches passes.

The plan pins this change moves, red before the re-pin and green after:

gate-qwen4exp-meta (before the re-pin)
gate-qwen4exp-meta rc: lib 101 reserve 0 meta 101 mtp 101 host 0 bp 0
gate-qwen4exp-meta (after)
gate-qwen4exp-meta rc: lib 0 reserve 0 meta 0 mtp 0 host 0 bp 0

Unit tests on a Linux CPU node:

cargo test -p bloomery-model --lib        test result: ok. 599 passed; 0 failed; 5 ignored
cargo test -p bloomery-runtime --lib      test result: ok. 300 passed; 0 failed
cargo test -p bloomery-placement          test result: ok. 220 passed; 0 failed

Hardware (if the change is for a machine)

Card Driver CUDA
NVIDIA GB10 (DGX Spark) 595.84 13.3

AI help

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

…eside it

A verify of a GDN layer (MTP's rows and every slot of a joint pass) records
each row's key, rounded update scalar and head decay in a record buffer of
its own instead of two spare state lanes, so its replay at the commit reads
the record and writes the kept state into the one lane in place. Each of the
36 delta layers of a slot holds 3 MiB of state and a 288 KiB record of four
rows, where it held 12 MiB of four lanes: 314 MiB less a slot, draft on or
off. The replay table carries the record's address beside the state, stamp
and carry; its overlap check covers the record. The verify's row count is
its own constant (`DELTA_VERIFY_ROWS`), apart from the state's lane count,
and the plan counts the lane's stamp and the record.
…ams at their width

The ubatch walk carries its residual streams as BF16 values in the stream
buffer other than the final one, so that buffer is allocated at a halfword a
value: 20,480 B a ubatch position less, 80 MiB at 4,096 positions, which the
plan's arena count follows. On a dense MMA card where every HC mix has a
BF16 down, the wide mixes norm their streams into BF16 words alone, so the
wide HC scratch holds the normed streams at a halfword a value too, another
80 MiB at 4,096 positions: the BF16 down's span check counts those words two
values a word, and a mix that norms into F32 values on such a scratch is
refused by name before any launch. The Qwen3.8 file's plan figures (kv,
scratch, each card's experts and the largest context) move with the delta
stores' record and this buffer.
@github-actions github-actions Bot added documentation Improvements or additions to documentation gpu The card engine: crates/gpu*, crates/app host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset 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 placement Placement planning: crates/placement labels Oct 30, 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 behavior or quality defect was established in the changed code.

Non-blocking

⚠ Establish the failing gate's baseline: crates/model/src/arch/qwen35moe/place.rs:1923 changes the planner's recurrent bytes. The PR reports the fixture e2e plan holding 254–255 experts per layer instead of 256. Attach this clause's base and head results on the same card and fixture to establish whether it regressed.

⚠ Supply throughput evidence: crates/gpu/src/linear/delta.rs:2680 changes verify-record stores. The introduction claims unchanged throughput without timing output. Attach the card/driver line, exact base and head hashes, model path, and timed A/B commands with output. Include PTX resource columns; an occupancy change requires the owning gate and one timed A/B under the base contract.

Checked

Duplicates: 10 new named items, including renames, checked by Grep; no qualifying duplicate. The new record sizing delegates to runtime::stores; tile span checks remain shared. Dryer reported zero pairs; its GPU exclusions were searched manually.

Complexity: check_wide adds BF16 cases; recovery_layers_clauses adds overlap cases; recovery_clauses adds one-lane coverage; misaligned_refused adds record windows. RecoveryTable::new and enqueue_mix add validation; delta_carried, load_placed and Arena38::alloc extend allocation. No kernel split.

Silent failure: 3 added fallback/bound sites followed. Saturating slot multiplication and the row cap retain their existing semantics. A failed BF16 eligibility lookup selects full scratch; the launch still validates the weight and returns its named error.

Tests: static mutation checks only. Dropping the update store linked above fails scalar recovery-state comparisons. Removing the F32-capacity guard (hc_gated.rs, line 2843) fails the new refusal clause. Restoring full-width streams at scratch38.rs:438 fails the arena byte assertion. Removing the record term linked above fails layout and plan assertions. These cover the changed recovery, HC, arena and byte/plan contracts; no tests ran locally.

Gates: byte, placement and context pins carry dated reasons; obsolete lane-rotation and planted-lane cases have a dated removal reason. The body reports red-before/green-after plan pins, but no FAIL-first run for the new HC clauses.

Adversarial: four verify rows on one lane, a parked-slot commit after another slot changes position, and split ubatches are bounded by record lengths, saved record metadata, per-slot stores/tables and BF16 windows. Reset and checkpoint restore restamp the live lane.

Escape catalog: no C1–C4 pattern matched. No ledger lessons were in force.

Notes

The gate evidence lacks the required nvidia-smi line and exact head commit hash; gpu.json records evidence=false. CUDA gates were reviewed statically.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_hc_gated.rs, crates/gpu-gates/src/bin/gate_linear.rs, crates/gpu-gates/src/bin/gate_qwen4exp_e2e.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 c245910 into midagedev:main Oct 30, 2026
7 checks passed
@midagedev

Copy link
Copy Markdown
Owner

@juliankang4 FYI: c245910 changed the f32_tile short-x refusal to "x holds … values". gate-gpu-gemm still looked for "x.len() ", so the 0.3.12 release batch read it red:

gemm32 case=g32_refuse f32_tile_short_x=REFUSED-OTHERWISE(... x holds 320 values, want n*k ...) FAIL

I changed the clause to look for "x holds " (b5d08f3, on main). Same commit: the Silu nan_o case's graph replay raised the fault again after its take, so the next case read it; the case now clears the word. gate-gpu-gemm is green on the 3090 (278 s). Nothing for you to do; tell me if you want the wording checked another way.

@juliankang4

Copy link
Copy Markdown
Collaborator Author

@midagedev Thanks for catching the wording change and fixing the clause and the nan_o replay case. "x holds " reads fine here, nothing more needed on this side.

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

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

Labels

documentation Improvements or additions to documentation gpu The card engine: crates/gpu*, crates/app host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset 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 placement Placement planning: crates/placement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants