Skip to content

gpu, placement, model: BF16 dense weights in their words, picked per tensor (Qwen3.8) - #592

Merged
juliankang4 merged 9 commits into
midagedev:mainfrom
juliankang4:dgx-spark-bf16-dense
Oct 21, 2026
Merged

juliankang4 merged 9 commits into
midagedev:mainfrom
juliankang4:dgx-spark-bf16-dense

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

체크포인트가 BF16으로 담은 dense 가중치를 Q8_0으로 바꾸지 않고 BF16 그대로 GPU에서 읽는 공통 경로를 추가하고, Qwen3.8이 텐서마다 Q8_0과 BF16 중 하나를 고르게 합니다.

Part of #495.

What and why

This adds BF16 as a resident weight format on the card. A BF16 dense matrix is uploaded as it sits in the file (two values per u32 word) and read by one BF16 site: gemv for 1 to 8 columns, a wide tile, a row-list gemv, the output head and the token embedding. Qwen3.8 chooses per tensor between the Q8_0 site and the BF16 site. prepare --keep-bf16 already leaves every BF16 matrix that would become Q8_0 (head and embedding included) in BF16, so this is the reader for what that option writes.

Every BF16 output equals the F32 rule over the widened values bit for bit, because the BF16 kernels walk the same lanes in the same order as the F32 kernels. Models that do not turn the new placement flag on keep their placement byte for byte.

  • Card table (crates/gguf/src/qtype.rs, crates/gguf/src/card.rs): the BF16 row of the shared card table names Bf16 in its dense, head and embedding columns, so the entry definitions, the file type and the activation rule are table reads like every other format. Its default and routed columns stay Bf16AsF32.
  • Placement (crates/placement/src/placement.rs): ModelTensors::bf16_words and ModelTensors::card_format. On a model that turns the flag on, a BF16 tensor of two or more dimensions in attention, hyper-connection, shared expert, dense FFN, engram dense, head or token embedding gets CardFormat::Bf16Raw. One-dimensional tensors, routers and norms stay Bf16AsF32. card_format_of_role remains the role rule, and card_format is the one entry every caller uses (card_segment, place_whole, whole_on_card, the plan rules and whole_need).
  • Weights (crates/gpu/src/weights.rs): DevWeight::Bf16, a raw upload that copies the file bytes, Weights::bf16, and one join_plane_rows for joined F32 and BF16 rows. An odd k or a length mismatch is refused by tensor name.
  • Kernels (crates/gpu/src/q8f32.rs, gemm/f32tile.rs, q38.rs, hc_gated.rs): the F32 lane walk is generalized over RowValues (F32 values or BF16 words). New entries are bf16_gemv, bf16_gemv_ids, bf16_tile_gemm, embed_rows_bf16, hc_gated_down_bf16_4x320 and hc_gated_up_mix_bf16_4x320.
  • Site (crates/gpu/src/site.rs): SiteTy::Bf16, BF16 arms in gemv, gemm and resident_gemv, and three entry points that dispatch by resident form: resident_gemv_cols, resident_gemm and resident_quantize. GLM's body::gemv calls site::resident_gemv, so GLM projections get the BF16 site as soon as its ModelTensors turns the flag on.
  • Head (crates/gpu/src/head.rs): OutW::Bf16 shares argmax_finite with Q8_0. The K-quant launches are unchanged.
  • Qwen3.8 wiring: plan38, program38, wide38, mtp38, body38's head_form and the embedding accept BF16 next to Q8_0 and F32. The hyper-connection mix and the embedding lookup read one resident matrix view, HcMatrix in hc_gated.rs (named EmbedRows in q38.rs), built from Weights::resident and checked by one shape. The MTP draft head can borrow a BF16 target head and embedding (bf16_gemv_ids, BorrowedHead::Bf16, HeadOut::Bf16). The draft's own eh_proj and its store-walk q, k and v use the same common site.
  • Non-finite sums: in hc_gated.rs, every sum that feeds SiLU, sigmoid or a square root is checked before the nonlinearity (norm sum and epsilon sum, the four down streams, the four inject streams, in the capped and wide paths). A non-finite sum writes NaN and raises the site fault instead of a finite value such as sigmoid(infinity) = 1.
  • Tables and docs: BF16 rows in model/src/arch/coverage.rs, placement/src/kernels.rs and placement/src/accepts.rs; the BF16 mapping check in tools/hf-arch-check.py; PTX shape rows for the new entries in tools/ref/ptx-shapes*; a gate-gpu-bf16 recipe in the justfile; the --keep-bf16 usage note for prepare.

Release notes: Added: BF16 dense weights, head and embedding run on the card as BF16 words through one shared site, chosen per tensor by Qwen3.8 placement. Output matches the F32 rule bit for bit.

Code shape

  • No second copy. The BF16 and F32 lane walks, tiles, hyper-connection stages and embedding lookups share one generic body per operation, with one kernel entry per weight form. The hyper-connection mix and the embedding share one matrix view. Qwen3.8's wide walk and the MTP draft's store walk call the same site::resident_gemm and site::resident_quantize.
  • Model code holds only architecture facts. The format choice lives in ModelTensors, the card table and the common site. Qwen3.8 supplies tensor roles and shapes. No common owner gained a per-model branch.
  • Dead code is gone. The duplicate adapters in wide38 and mtp38 and the embedding's own BF16 reader are removed.
  • No silent failure. A BF16 site over an F32 resident, an odd k, a tile k that is not a multiple of 64, an out-of-range row id and a non-finite sum are named refusals, faults or NaN outputs.
  • Comments state what is true now.
  • No relaxed gate. No band or tolerance moved. The new tools/model-tax.tsv row is the gate_bf16.rs PIN.

The synthetic-file helpers (Decl, decl, q8_0_bytes, host_rows, columns) that gate_bf16 and gate_mxfp8 both needed live once in crates/gpu-gates/src/bin/shared/dense_file.rs. The store-walk helpers are merged into site::resident_gemm and site::resident_quantize, the down-row end of the hyper-connection kernels into hc_gated::down_row_end, and the head tensor name is weights::HEAD_TENSOR.

What I ran

The PR head is 0e8644f60c4c74067f32aa2a23e493bb470b6c9b on upstream main 3da32e779a76dd2f1c9c4f3a0746865fb7521586. gate_bf16 ran at 0e691b61bc101d1c80126078b17254f7d0394d92, which differs from the PR head in one source-tool path (tools/hf-arch-check.py) and no crate, manifest or recipe, so that result carries to the PR head. gate_mxfp8 and the hf-arch-check.py self-test ran at the PR head. gate_hc_gated ran at 9c0b0c22160cdb100f053ad6f543dae5087bd32a; the later commits change host code in hc_gated.rs and q38.rs (the shared matrix view) and no device function, and gate_hc_gated was not rerun after them. just lint and a cargo check on the card were still running on the PR head when this was written.

The oracle is the F32 rule over the widened BF16 values, plus the derived error band, on synthetic tensors with the shapes of Qwen3.8's attention, head and embedding, loaded through the placed path.

GPU card, a DGX Spark (GB10) in the dev container image bloomery-dev:cuda13.3.1. The gate run's own record of the card is the query line NVIDIA GB10, 595.84, 12.1 (name, driver, compute capability). Raw nvidia-smi output from the same node and image, taken at the gate_hc_gated run:

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

Gate commands on that card, each recipe run with local execution enabled, and their exit lines:

just gate-gpu-bf16
gpu-gate.sh: gate_bf16 ran 11 s (exit 0)
just gate-gpu-mxfp8
gpu-gate.sh: gate_mxfp8 ran 6 s (exit 0)
just gate-gpu-hc-gated
gpu-gate.sh: gate_hc_gated ran 10 s (exit 0)
Check Result
gate_bf16: a 64-column GEMM on 8192x2560 against the F32 rule 524288/524288 outputs equal, PASS
bloomery-placement tests (CPU host) 165 passed

The BF16 64-column GEMM equals the F32 rule in all 524288 outputs of the shape shown.

The gate_bf16 log also shows the BF16 gemv, the head (4099 rows, k=2560, tokens equal to the argmax) and the embedding lookup equal to the F32 rule, and gate_mxfp8 shows its 16384-row tile at K=1536 equal in 1048576/1048576 outputs. gate_hc_gated checks both hyper-connection sites against the f64 rule within their bands and every stage bit for bit against the card rule. With the norm boundary reverted to its earlier code, gate_hc_gated exits 1 on the new norm clauses, and with the fix restored it exits 0:

gate_hc_gated: FAILED: one or more checks above did not pass
gpu-gate.sh: gate_hc_gated ran 10 s (exit 1)

tools/hf-arch-check.py --self-test passes at the PR head, including the check that SiteTy::Bf16 resolves to the BF16 row of the card table:

ok BF16 is a dense family in the card table

To reproduce, from the repository root with CUDA configured:

just gate-gpu-bf16
just gate-gpu-hc-gated
just gate-gpu-mxfp8
cargo test -p bloomery-placement --lib

This PR makes no speed claim and runs no PTX comparison at this head. BF16 dense reads more bytes per token than Q8_0, so decode is expected to be slower than on a Q8_0 split.

What I ran into

The following does not block this PR. I did not run a real-model BF16 oracle: the BF16 branches of program38, wide38 and mtp38 are checked through the kernels' gates and through placement and admission tests, not through a model run. A same-lease prefill A/B for the existing GGUF path is also not part of this PR.

Hardware

A DGX Spark (GB10) for the GPU gates (driver 595.84, CUDA 13.3 in the dev container). A separate CPU host for the placement tests and the hf-arch-check.py self-test.

AI help

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

@github-actions github-actions Bot added 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 tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels Oct 21, 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 established; the remaining items are advisory.

Non-blocking

⚠ Complete the code-generation proof: crates/gpu/src/q8f32.rs:203.
The shared F32 walk was generalized, and hyper-connection bodies changed. The PR reports no PTX comparison. Attach the move proof for the F32 refactor and existing-entry resource and new-entry spill checks for BF16, with FAIL-first evidence per new clause. Rerun gate-gpu-hc-gated at head.

⚠ Validate the model wiring: crates/model/src/arch/qwen35moe/roles.rs:211.
The raw-BF16 placement choice enables branches in program38, wide38 and mtp38. The PR reports synthetic operation gates but no real-model BF16 oracle run. Add mixed Q8_0/BF16 prefill, decode and borrowed-head MTP validation with output and fault comparisons.

Optimization headroom

  • G. Per-launch and per-sync fixed cost: crates/gpu/src/arch/qwen3moe/program38.rs:648. Same-format shared gate/up weights can use the existing Weights::join_rows owner and one projection, removing one launch per layer per decode token [derived].

Checked

Duplicates: F32/BF16 share RowValues and join_plane_rows. Dryer reports zero pairs in its searched crates; GPU crates were excluded.
Complexity: diagnostics flag new gate functions and growth in existing dispatch, validation and model walks; complexity alone is no finding.
Silent failure: resident-format mismatches return named errors. bf16_gemv_ids rejects IDs at or past the matrix's row count, raises TokenId and writes NaN.
Tests, static mutation: swapping BF16 halves breaks the F32-reference bit checks. Accepting id == rows bypasses the fault required by gate_bf16's invalid-ID clause.
Gates: the BF16 head/embedding admission change carries PIN(2026-10-20). The PR reports FAIL-first for norm overflow.
Escape catalog: C1–C4 do not match the retained items.

Notes

Tests were not rerun during validation. The PR reports synthetic GB10 gate passes and identifies the earlier hyper-connection run and missing PTX/model evidence.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/Cargo.toml, crates/gpu-gates/src/bin/gate_bf16.rs, crates/gpu-gates/src/bin/gate_deepseek41_chain_glue.rs … and carries a gate run on the author's card, which counts as verified.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@github-actions github-actions Bot added gpu-evidence Verified on the author's card: the pull request carries the gate run and its output and removed needs-gpu Changes code a card must check, and carries no gate run on a card labels Oct 21, 2026
@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 c585fdf into midagedev:main Oct 21, 2026
6 checks passed
midagedev added a commit that referenced this pull request Oct 21, 2026
…ions held in two-byte words

#592 holds the 24 BF16 indexer projections of Qwen3.8 (q_proj 2,560×512 and k_proj 2,560×128 on each of the 12
selecting layers, 19,660,800 values) on the card at 2 B a value, not as f32. Card dense falls 39,321,600 B and the heap
18 granules, so the spread holds more experts under the same room. The sum is right; the pins were stale. Every moved
pin carries PIN(2026-10-21) and its derivation: qwen4exp_meta's dense, rounding, max ctx and card plans (Q4 and Q3) and
the bp plain row; qwen4exp_mtp_meta's drafted bp row and max ctx; the Mac replica qwen38_cards follows the same word
rule. The qwen38 fixture's recorded budget moved 9,490,661,376 -> 9,482,272,768 (4 granules) in both header files, so
the plan under it is [256; 4] again.

Fixes #594.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 gpu-evidence Verified on the author's card: the pull request carries the gate run and its output 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 placement Placement planning: crates/placement 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