Skip to content

gpu: count named file formats in weight censuses - #715

Merged
juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:fix-head-census
Oct 23, 2026
Merged

juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:fix-head-census

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

BF16 출력 헤드(output.weight)의 적재 크기를 실제로 올라가는 형식(MXFP8 또는 BF16 원본)으로 계산하도록 고치는 수정입니다.

Part of #565. Tracked in #699.

What and why

A standalone BF16 output.weight is uploaded as MXFP8 when K is a multiple of 128, and as raw BF16 otherwise. Three census paths and the load's staging-ring budget sized that tensor with resident_size, which takes only the tensor type and so counts the default F32 layout. A correct load then tripped its own resident-byte census, and the staging budget was larger than the bytes the upload moves. For a head with 64 rows and K=256 the old arithmetic gave 65,536 B where the upload takes 16,896 B; at K=320 it gave 81,920 B where the upload takes 40,960 B.

The defect came in with e53cabe (#665), which made the head's format depend on its name while these callers kept sizing by type alone.

The fix reuses weights::part_format, which already picks the format by tensor name and computes its resident bytes through CardFormat::resident_bytes, the arithmetic the uploads check against. No new helper or public item is added, and uploads, weight conversion, kernels and output bits are unchanged.

  • crates/gpu/src/weights.rs: upload_budget takes the bytes from part_format and falls back to the file bytes only when the tensor has no named format. The doc comment of resident_size now says it sizes the type's default format and points file tensors to part_format.
  • crates/gpu-gates/src/bin/gate_p10.rs: the file-tensor census loop uses part_format. The derived Q8_0 sizing, which is not a file tensor, keeps resident_size.
  • crates/gpu-gates/src/bin/gate_qwen35_dense.rs: the derived weight sum and the all-file sum use part_format.

I checked the other resident_size callers. gate_p2 tests the Q5_0 format itself. gpu-gemma-embedding2::card_bytes is limited by check_types to Q8_0 matrices and F32 vectors, so a standalone BF16 head cannot reach it. model::probe counts the resident variants that were actually loaded.

Release notes: Fixed: a BF16 output head is sized by the format it is loaded in, so the weight censuses and the upload staging budget agree with the uploaded bytes.

Code shape

  • No second copy. The census and the budget use the existing part_format owner. Duplication found or removed: none.
  • Model code holds only architecture facts. No model file changed, and no common owner gained a per-model branch.
  • Dead code is gone. Nothing was left unused.
  • No silent failure. A census that cannot size a tensor still counts it as a gate failure. The budget keeps its file-byte fallback, which is a sizing hint and not a contract, since the uploads check their own lengths.
  • Comments state what is true now.
  • No relaxed gate. No band, tolerance or lint moved.
  • cargo fmt and cargo clippy: rustfmt --edition 2024 --check on the three changed files reports no diff (rc 0), and cargo clippy was not run for this body.

What I ran

The test added in crates/gpu/src/weights.rs, file_upload_budget_counts_the_named_format, checks both part_format and upload_budget for six cases: the BF16 head at K=256 and K=320, the tied token embedding, an ordinary BF16 matrix, an F32 head and a Q8_0 head. A host-side harness compiles the functions and the byte arithmetic from the test-only baseline commit and from the fix commit against the real placement and GGUF crates, with only the GPU error wrapper replaced by a host stand-in. It runs four checks: that test, the P10 census loop, the Qwen derived-weight sum and the Qwen all-file sum. The census checks read a GGUF file with a BF16 output.weight and a blk.64.attn_q.weight tensor, each with 64 rows and K=256, and expect 16,896 B for the head, 65,536 B for the ordinary tensor and 82,432 B in total.

RUSTFLAGS='-C target-cpu=native' cargo test --offline --manifest-path target/headcensus-host/{base,head}/Cargo.toml --target-dir target/headcensus-host/build -- --nocapture
Commit Result
test-only baseline test result: FAILED. 0 passed; 4 failed
fix test result: ok. 4 passed; 0 failed

The baseline fails all four checks first, and the upload budget check computes 65,536 B where 16,896 B is expected. The fix passes the same four checks. The harness prints headcensus: PASS: base 4 assertion failures; head 4 passes and exits with rc 0.

Also run: git diff --check upstream/main...HEAD (rc 0) and the duplicate-code scan of the branch (rc 0, no candidates).

GPU evidence. The card, driver and CUDA lines from nvidia-smi on the DGX Spark that ran the GPU gate:

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

just gate-gpu-p10 and just gate-gpu-qwen38d need DeepSeek-V2-Lite Q3_K_M and Qwen3.8-27B Q4_K_M, which were not on the machine, so I did not run them. Instead I ran just gate-gpu-qwen3moe-types, which builds its own two-layer qwen3moe GGUF and loads it whole and placed onto the card through the same Weights load path. It ran on this fix and on upstream main without it:

BLOOMERY_BOX_LOCAL=1 BLOOMERY_ARCH=sm_121 just gate-gpu-qwen3moe-types
types: MXFP8 whole load resident=true PASS
types: MXFP8 placed load resident=true PASS
gate_qwen3moe_types: PASS
GATE head gate-gpu-qwen3moe-types rc=0

Both runs print the same four result lines and end with rc 0. This gate has no BF16 output head, so it shows that the real upload path still loads and passes its resident check with the new sizing. It is not a FAIL-first for this defect. The FAIL-first is the four host checks above.

What I ran into

This does not block the PR.

  • The census gates that read a real BF16-head checkpoint (gate-gpu-p10, gate-gpu-qwen38d) were not run. A run on a machine that has those models would confirm the fix end to end.
  • P10's expected_planes has no reference packing for an arbitrary BF16 file. Widening that support is a separate change from this arithmetic fix.
  • The change adds no speed claim. It makes an estimated staging size match the real format. The staging ring has an upper bound, so on large models the allocation may be the same.

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 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 censuses and staging budget now use the existing format selector and match the upload layouts.

Checked

Duplicates: 1 new item checked, the regression test; no duplicate contract found. Dryer reported 0 pairs in crates/gpu; crates/gpu-gates was not searched by dryer and its changed callers were checked manually.
Complexity: crapper flagged no function; the changed callers replace sizing expressions without adding branches.
Silent failure: 4 error/default sites followed. P10 rejects unknown formats, Qwen propagates errors or panics with context, and the budget fallback only sizes the bounded staging ring. Upload validation still rejects invalid layouts.
Tests: restoring type-only sizing at crates/gpu/src/weights.rs:1394 fails the new assertions for both BF16 head widths. The PR reports FAIL-first host checks for the budget and all three census paths; that harness is absent from the supplied tree.
Gates: no pins, thresholds or cases removed or relaxed. The assertions still compare predicted bytes with loaded bytes.
Escape catalog: no patterns matched.
Adversarial checks: aligned and unaligned BF16 heads select the uploaded layouts; tied embeddings retain F32 sizing. Empty or zero-sized tensors and odd-width BF16 heads reach explicit refusals.
Performance: load-time sizing and gate code only; no decode, prefill or kernel path changed.

Notes

The GB10 gate witness omits both tested commit hashes, so GPU evidence is marked false. The author did not run the P10 or Qwen dense gates. This review was static; no CUDA build or gate was run here.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_p10.rs, crates/gpu-gates/src/bin/gate_qwen35_dense.rs, crates/gpu/src/weights.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 3de5295 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant