Skip to content

hf: normalize NVFP4 expert stacks from bounded input batches - #981

Merged
juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-sa-q6-load-time
Oct 29, 2026
Merged

juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-sa-q6-load-time

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Qwen3.8 폴더 로드에서 ModelOpt 선언 조회와 NVFP4 expert stack 정규화를 고쳐 최대 host 메모리를 26.23%와 27.58% 줄였습니다.

What and why

Loading an NVIDIA ModelOpt checkpoint folder did two wasteful things on the host. Every weight was checked against all 545 (or 50) quant declarations, and NVFP4 expert stacks were normalized with the whole input held at once next to the result.

This PR indexes the declarations in a BTreeMap and looks up the module and its dot-separated ancestors, so the longest complete module prefix still wins. It also reserves the stack result once and reads the inputs in batches of at most 32 MiB (a single expert larger than that is read whole), releasing the CSF scale of each expert after use. read_bytes now counts the input batch, the current expert's scratch and the result stack. The direct-read and mapping paths share Checkpoint::raw.

The measured gain is host memory, not time: the two real folders load with identical resident bytes and identical output, and peak host RSS drops by 26.23% and 27.58%. This PR claims no start-up speedup.

Release notes: Under the hood: lower peak host memory when loading ModelOpt NVFP4 checkpoint folders.

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/model-hf/src/part.rs:223 ~ crates/model-hf/src/part.rs:408 reused. The two direct-read lookups now go through Checkpoint::raw.

What I ran

Real folder load on a DGX Spark GB10 (NVIDIA GB10, driver 595.84), base 4c549a477835d471929aa3c04718062488864d3b against head 5a771d7ed191ec33ab9900ea93ad67fb618df617. Base and head were built into separate target directories and each arm ran once, FP4-Local first, then NVFP4:

cargo oxide build --arch sm_121a -- -p bloomery-gpu-gates --features deepseek41,gemma-embedding2,hf,checkpoint --release --bin bloomery-serve-qwen38
BLOOMERY_REF_MODEL=<folder> BLOOMERY_DRAFT=off <base-serve or head-serve> --host 127.0.0.1 --port 18736 --parallel 1 --ctx-size 16896 --cache-ram 0 --no-mmproj

Folders: nvidia/Qwen3.8-Flash-Next-FP4-Local at b9ce18a6887e18c089fdea8509a33e63429a3300 and nvidia/Qwen3.8-Flash-Next-NVFP4 at fc694b54fb0174e0913e6adf86691ef85a4ead47. Peak host RSS is the server process wait4 rusage, including one short completion and shutdown. Start to health is process spawn to the first HTTP 200 on /health.

Metric Base Head Difference
FP4-Local peak host RSS (bytes) 14,625,361,920 10,789,302,272 -3,836,059,648 (-26.23%)
FP4-Local resident bytes 78,529,707,260 78,529,707,260 0
FP4-Local engine load (s) 12.5 11.8 -0.7
FP4-Local start to health (s) 16.843 33.815 +16.972
NVFP4 peak host RSS (bytes) 13,300,940,800 9,631,911,936 -3,669,028,864 (-27.58%)
NVFP4 resident bytes 81,354,368,252 81,354,368,252 0
NVFP4 engine load (s) 13.5 13.7 +0.2
NVFP4 start to health (s) 17.228 17.423 +0.195

Each arm ran once, so no timing difference is claimed.

LOAD-PASS fp4l resident bytes and completion identical
LOAD-PASS nvfp4 resident bytes and completion identical

All four arms completed The capital of France is (n_predict=8 temperature=0 seed=42) with the same text, Paris. The capital of Germany is Berlin.

Host unit tests for the changed code, on macOS:

cargo test -p bloomery-hf --lib modelopt::      # 20 passed
cargo test -p bloomery-hf --lib quant_config::  # 15 passed
cargo test -p bloomery-model-hf --lib part::    # 7 passed

The new scratch bound was written to fail first: with the assertion added to base, cf.weight: 2504732 > one expert's scale bound 2439196 and FAIL-FIRST PASS: base exceeds one expert scale scratch; on the head it passes.

cargo fmt --all is clean and git diff --check passes.

Hardware (if the change is for a machine)

Card: NVIDIA GB10, 595.84, built with --arch sm_121a --release.

AI help

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

@github-actions github-actions Bot added the model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja label Oct 29, 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 changed loading paths preserve the normalization and error contracts.

Checked

  • Duplicates: normalization and bounded reads share Nvfp4Stack; direct-read fallbacks share Checkpoint::raw. Dryer reported no pairs.
  • Complexity: append_plain (24) moves the existing append body unchanged. Checkpoint::read (34 → 37) adds the streaming branch. Config::declared (18 → 20) replaces the prefix scan with ancestor traversal. No complexity finding.
  • Silent failure: missing tensors, malformed scales and byte-count overflow retain named errors. Unsupported direct reads use the mapping.
  • Tests, static mutation check: removing the extra-expert refusal at crates/hf/src/modelopt.rs:710 makes the new unwrap_err fail. Removing ancestor traversal at crates/hf/src/quant_config.rs:434 fails the descendant lookup assertions.
  • Tests, memory mutations: changing > to >= at crates/model-hf/src/part.rs:209 fails the exact-limit assertion. Dropping decoded-scale scratch at crates/model-hf/src/part.rs:380 fails the measured-peak bound. Replacing the maximum with the last batch at crates/model-hf/src/part.rs:383 fails the three-expert case. Other test edits adapt fixtures or comments.
  • Gates: no tolerance, pin, lint level or existing assertion was relaxed or removed. The PR supplies FAIL-first output for the scratch bound.
  • Escape catalog: no C1–C4 pattern matched.
  • Adversarial checks: oversized single experts, missing or mismatched experts, invalid compressed scales and unsupported direct reads have explicit paths. Ancestor traversal checks complete module prefixes; batch ranges preserve source order.

Notes

Rust suites were not executed in this source-only review. The PR includes load checks, but no nvidia-smi line or just gate-* output; GPU evidence is false.

🔨 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 4563e4f into midagedev:main Oct 29, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant