Skip to content

hf: a ModelOpt NVFP4 shard opens, its U8 weights and F8_E4M3 scales held as stored bytes - #489

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-nvfp4-modelopt-reader
Oct 19, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-nvfp4-modelopt-reader

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

NVIDIA가 배포하는 NVFP4 체크포인트(ModelOpt) 파일을 bloomery의 safetensors 리더가 열 수 있게, U8과 F8_E4M3 tensor를 저장된 바이트 그대로 읽도록 하는 작업입니다.

Part of #458, section 7 (weight formats).

What and why

With this PR, hf::safetensors opens the shards of a ModelOpt NVFP4 checkpoint, such as NVIDIA's NVFP4 checkpoint of Qwen3.8-Flash-Next. ModelOpt stores the packed 4-bit weights as U8 and the block scales as F8_E4M3. On main the header parser knows neither dtype, so one such entry makes it refuse the whole file at open. 10 of the checkpoint's 11 safetensors files hold them.

The change is in crates/hf/src/safetensors.rs:

  • Dtype gains U8 and F8E4M3 (header names U8 and F8_E4M3), each 1 byte wide, so the open-time checks of shape, byte range and overlap cover them like every other dtype.
  • raw returns them as stored bytes with their dtype and shape. It slices the entry's byte range the same way for every dtype.
  • The f32 read tensor refuses both by name with Error::Dtype, as it already refuses I64 and BOOL.

Nothing decodes NVFP4 here. No GGUF code, GPU crate or engine path changes.

Release notes: Added: Safetensors files with U8 and F8_E4M3 tensors, such as ModelOpt NVFP4 checkpoints, now open, and those tensors read as stored bytes.

Code shape

  • No second copy. The two dtypes are new arms in the existing Dtype matches; no new function or type.
  • Model code holds only architecture facts. No model code changes.
  • Dead code is gone. Dtype::U8 and Dtype::F8E4M3 are what Dtype::parse returns for the two header names.
  • No silent failure. The f32 read of a U8 or F8_E4M3 entry is a named Error::Dtype, never a widened value.
  • Comments state what is true now.
  • No relaxed gate. The existing safetensors tests are unedited.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up. cargo fmt runs in the t0 check below; clippy is not part of that check.

Duplication found or removed (path:line), or "none": none.

What I ran

All results are on head 39d65073, based on b5ddb224.

The new test a_modelopt_shard_slice_opens_with_u8_and_f8_e4m3_held_raw opens the fixture crates/hf/tests/fixtures/modelopt-nvfp4.safetensors (14,216 bytes). The fixture holds the first 4 rows of layer 0's gate_proj for experts 0 and 1, taken by HTTP range reads from model-00001-of-00010.safetensors of nvidia/Qwen3.8-Flash-Next-NVFP4 at revision fc694b54fb0174e0913e6adf86691ef85a4ead47 and kept under their own header names, plus one small synthetic triple. The reference is the fixture's own header: for each entry the test reads, the stored dtype, shape and byte range.

Reference Read on this head Difference
Fixture header entry experts.0.gate_proj.weight: U8, shape [4, 1280], 5,120 bytes raw returns U8, [4, 1280], 5,120 bytes; tensor refuses it as U8 0 differences
Fixture header entry experts.0.gate_proj.weight_scale: F8_E4M3, shape [4, 160], 640 bytes raw returns F8_E4M3, [4, 160], 640 bytes; tensor refuses it as F8_E4M3 0 differences

A ModelOpt NVFP4 shard now opens: its U8 and F8_E4M3 entries read back as stored, and the f32 read refuses them by name.

Checks:

  • cargo test -p bloomery-hf --lib safetensors: test result: ok. 6 passed; 0 failed. That is the new test and the five existing safetensors tests.
  • On main code with only the fixture and a test that opens it added, the open fails with Dtype { name: "model.language_model.layers.0.mlp.experts.0.gate_proj.weight", dtype: "U8" }.
  • The existing dtype paths are unchanged. The diff touches only crates/hf/src/safetensors.rs and adds the fixture. In that file it changes three existing lines: the module comment, the Dtype::size arm that now lists the two 1-byte dtypes beside BOOL, and the tensor refusal arm that now lists them beside I64 and BOOL. The five existing safetensors tests pass unedited. No code outside safetensors.rs matches on Dtype, and git diff upstream/main -- crates/gguf is empty.
  • just contrib-check --tier t0 on this head: TBD(t0 /DONE tier=t0/)

To reproduce, from the repository root:

cargo test -p bloomery-hf --lib safetensors
git diff --stat upstream/main...HEAD

No GPU check was run, because no GPU crate, kernel or engine path changes, and the PR makes no speed claim.

Hardware (if the change is for a machine)

The change is not machine specific. The tests ran on macOS.

What I ran into

This does not block the PR. One ModelOpt triple can span two shard files: model-00002-of-00010.safetensors holds 9,643 U8 entries and 9,642 F8_E4M3 entries, and the expert stacks of layers 8, 14, 21, 27, 33, 40 and 46 cross a shard boundary. Code that reads a whole triple has to resolve each name through model.safetensors.index.json instead of reading one shard at a time.

AI help

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

…eld as stored bytes

safetensors holds U8 and F8_E4M3 as stored bytes, so a ModelOpt shard opens;
until now every shard of such a checkpoint was refused at open by its first U8
entry. raw returns both with their dtype and shape, and the f32 read refuses
both by name, as it refuses I64 and BOOL.

The test fixture is a small ModelOpt-layout shard: the first four rows of
layer 0's gate_proj of experts 0 and 1 of nvidia/Qwen3.8-Flash-Next-NVFP4,
range-read from shard 1 at fc694b54, plus one synthetic triple, each under its
own header entries.
@github-actions github-actions Bot added the model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja label Oct 19, 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: Both new dtypes use the validated raw-byte path and return named errors from unsupported typed reads.

Checked

  • Duplicates: 1 new test checked, none duplicated; both enum variants extend the existing dtype owner.
  • Complexity: the dtype matches gain ordinary cases.
  • Silent failure: 2 existing .ok() sites followed; failed conversions become Error::Format. U8 and F8_E4M3 typed reads propagate Dtype or Kind errors to callers.
  • Tests: static mutation check of crates/hf/src/safetensors.rs:63, crates/hf/src/safetensors.rs:87, and crates/hf/src/safetensors.rs:302: removing either parser arm or using size 2 fails the parse unwrap; returning f32 fails the error assertion.
  • Bounds: malformed lengths, overlap and shape overflow reach existing errors. The fixture's 12 tensor ranges have valid sizes and do not overlap.
  • Gates: no existing gate or pin changed. The PR reports the fixture failing on base with Error::Dtype for U8.
  • Escape catalog: no C1–C4 patterns matched. The change touches load-time parsing, with no hot-path work added.

Notes

Rust tests were not rerun during this review. The fixture was checked independently. The PR reports 6 passing safetensors tests; its t0 result remains TBD.

🔨 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.

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