Skip to content

hf: FP8 weights of one scale a row, a tensor or a 32 by 32 E8M0 block read onto FP8_B128 and MXFP8 - #579

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-fp8-scale-forms
Oct 21, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-fp8-scale-forms

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

F8_E4M3 가중치의 스케일이 행마다 하나(채널), 텐서에 하나, 32x32 블록마다 E8M0 하나인 체크포인트를 기존 카드 형식 FP8_B128과 MXFP8로 읽습니다. 값은 그대로 두고 스케일만 각 카드 블록에 복사하므로 새 커널과 새 GGUF 타입은 없습니다. 바뀐 파일은 crates/hf의 두 소스와 시험 fixture뿐입니다.

Part of #495.

What and why

FP8 checkpoints store the scale of an E4M3 weight in three more layouts than the 128 by 128 block FP8_B128 already reads: one scale for each row (compressed-tensors channel), one scale for the whole tensor (ModelOpt FP8), and one E8M0 scale for each 32 by 32 block. bloomery gave none of these a card type, so a checkpoint in these layouts could not be placed. This PR reads all three onto the two types the card walks already, with the values unchanged.

  • A 128 value row block of a row scale or a tensor scale has one scale, so it fits FP8_B128 exactly. A 32 value row block of a 32 by 32 E8M0 block has one power of two, so it fits MXFP8 exactly. Each card block copies the scale of the checkpoint block it lies in. The cost is the same 1/32 byte a value that both types already carry.
  • Format::card_type in crates/hf/src/quant_config.rs is the one rule from a format to its card type. A block of width 128 or more in multiples of 128 is FP8_B128, a block of width 32 or more in multiples of 32 is MXFP8, and any other FP8 block has no card type and is refused by name.
  • Format::fp8_scale_block is the one owner of the area a scale covers and of the stored grid shapes it accepts ([r, c], ModelOpt's [r, 1, c, 1], a one-column grid as [r], a single scale as []). The plan's layout and the reader used to keep two copies of this rule.
  • crates/hf/src/modelopt.rs has one reader, fp8, in place of mxfp8, fp8_b128 and fp8_b128_append. It finds each block's scale with (row / a) * c + (j * blck) / b and writes an f32 (widened from F32, BF16 or E8M0) for FP8_B128 or the E8M0 byte for MXFP8. Routed experts of a row or a tensor scale stack as [k, rows, n_expert], as the 128 by 128 experts do.
  • Format::held counts every format by the bytes of its card type, so the old scaled_bytes count by squares is gone. A block the card cannot hold returns None.

A row or tensor scale FP8 weight whose row length is not a multiple of 128 is refused by name (in the Nemotron shard below that is 21 of 41 weights, for example in_proj [17504, 3136]). Also refused by name: a 32 by 32 block whose scale is F32 or BF16 (MXFP8 holds only E8M0), a grid whose shape does not match the weight, an MXFP8 expert stack, and a scale or value that is not finite.

Two small behavior changes sit on the same path: an MXFP8 weight placed together with an input_scale now reads its first companion (the scale) and leaves input_scale alone, as FP8_B128 does, where it used to be refused; and FP8_B128 accepts an E8M0 scale stored as U8, as MXFP8 does. No real checkpoint in the plan changes under either.

Release notes: Added: FP8 weights with one scale a row, a tensor or a 32 by 32 E8M0 block load onto the FP8_B128 and MXFP8 card formats.

Code shape

  • No second copy. The scale-grid shape rule moved into Format::fp8_scale_block, the three FP8 readers became fp8, and the format-to-card-type rule is Format::card_type. python3 tools/dup-scan.py lists no candidate.
  • Model code holds only architecture facts. No model file changed. The common owners gained no per-model branch.
  • Dead code is gone. scaled_bytes, mxfp8, fp8_b128 and fp8_b128_append are removed.
  • No silent failure. Every refusal above names the tensor and the format. The scale count and the block bytes use checked_mul, and the block buffer uses try_reserve.
  • Comments state what is true now.
  • No relaxed gate. Two expectations changed with the new rule: the held test's 32 by 32 value and the "no GGUF type" refusal case for a 32 by 32 block, which now has a type. Both carry PIN(2026-10-21): and the reason. No unsafe was added.
  • cargo fmt is clean on bloomery-hf. The lint count is checked by the PR check, not here.

Duplication found or removed: the scale-grid shape rule in the plan's layout and in the reader, and the writers mxfp8 and fp8_b128, which now live in fp8 and Format::fp8_scale_block (crates/hf/src/modelopt.rs, crates/hf/src/quant_config.rs).

What I ran

Host tests and a compile check on an x86 Linux host, at head 98908fe7eb26d1f920ad226db6f3577f58e5a4cf. The PR head differs from it only in the Python reference generator (crates/hf/tests/fixtures/fp8_scales.py), which now refuses non-finite scales; the Rust code and the reference file are the same bytes, and the full host suite passed again at the PR head:

cargo test -p bloomery-hf -p bloomery-model-hf --lib
cargo check -p bloomery-model-hf --all-targets
test result: ok. 90 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.65s
test result: ok. 21 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.58s

The 21 bloomery-model-hf tests include the pinned GLM FP4-Local plan bytes, so the MXFP8 count by card_type instead of scaled_bytes gives the same numbers.

Real rows against the producer's rule

quant_config::tests::real_fp8_rows_decode_to_their_producers_rule places rows of three real checkpoints by their own config.json, normalizes them, decodes each row with dequant_row and compares its sha256 with crates/hf/tests/fixtures/fp8_scales.py ref. That script decodes from the checkpoint definition with the Python standard library only: it widens E4M3, BF16 and E8M0 in f64, multiplies, and rounds to f32 once. The fixture slices come from HTTP range reads of the pinned revisions.

Checkpoint and tensor Card type Rows equal Result
MiniCPM-V-4.7-35B-A3B-FP8, expert 148 and 149 down_proj, one scale a row FP8_B128 8/8 PASS
NVIDIA-Nemotron-3-Nano-4B-FP8, out_proj, one scale a tensor FP8_B128 2/2 PASS
DeepSeek-V4.1-Flash, shared expert w2, E8M0 a 32 by 32 block MXFP8 40/40 PASS

All 50 checked rows equal the producer's rule bit for bit (difference 0). The same test checks that Nemotron's in_proj row, whose length 3136 is not a multiple of 128, is refused by name.

Synthetic layouts

modelopt::tests::fp8_weights_take_their_block_s_scale_a_row_block builds a 130 by 256 weight with a 128 by 128 grid (F32 [2, 2], BF16 [2, 1, 2, 1], E8M0), a row scale (F32 [130], BF16 [130, 1]), a tensor scale (F32 [], BF16 [1]) and a 32 by 32 E8M0 grid [5, 8]. For each it checks the type, the dims, the byte count, the scale of every block, the unchanged value bytes and a dequant_row that matches bit for bit. It also covers the refusals above and a row scale expert stack of [256, 130, 2].

The tests catch a defect

Each of seven one-line defects was planted in the test host's work tree and reverted afterwards. Before the defects, the 17 selected tests passed. Each defect made at least two of them fail with exit 101:

Planted defect Failing tests
no card type for row and tensor scales (as before this PR) fp8_weights_take_their_block_s_scale_a_row_block, each_format_holds_its_values_and_plain_scales, real_fp8_rows_decode_to_their_producers_rule
no card type for 32 wide blocks the same three
scale lookup without the column term the block test, mxfp8_weights_move_into_the_mxfp8_block_unchanged, the real rows test
rows grouped by block width the same three
any grid shape accepted the block test, the MXFP8 test
MXFP8 and plain stacks accepted the block test, the MXFP8 test, the_table_writes_each_format_as_its_type
rows that do not fit a block accepted the block test, the MXFP8 test, the real rows test

What the change holds in real checkpoints [derived]

held-count.py applies this PR's rule to the safetensors headers of one shard of each checkpoint. Before this PR none of these weights had a card type.

Checkpoint shard F8_E4M3 weights Held Refused Held bytes
MiniCPM-V-4.7-35B-A3B-FP8, shard 2 (row scale) 14936 14936 0 16,269,901,824 for 15,776,874,496 values
NVIDIA-Nemotron-3-Nano-4B-FP8, shard 1 (tensor scale) 41 20 21 669,774,336 for 649,478,144 values
DeepSeek-V4.1-Flash, shard 24 (32 by 32 E8M0) 8 8 0 167,067,648 for 162,004,992 values

The end-to-end number this opens is the first decode of these checkpoints with FP8 weights on the card, once a model loader maps their tensors. The existing callers of these types (prepare for Qwen3.8 and the GLM plan) keep their results, which the tests above show.

No device code changed, so no GPU gate ran and this PR makes no speed claim. The normalized weights are the FP8_B128 and MXFP8 blocks whose card walks gate_mxfp8 already pins bit for bit against the same host dequant_row.

Hardware

Host tests ran on an x86 Linux machine. The change touches no device path.

What I ran into

This does not block the PR.

  • The three fixtures are slices of real checkpoints, so the rows above come from layer 20, layer 0 and layer 21 only. The other layers of these checkpoints were not decoded row by row.
  • just lint (Clippy) did not run on this host. The PR check runs it.

AI help

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

… read onto FP8_B128 and MXFP8

A checkpoint's F8_E4M3 weight keeps its values on the card whatever its
scale layout: each row block of the card type holds the scale of the
checkpoint's block it lies in. quant_config's Format::card_type is the
one rule: a scale covering whole 128-value blocks of a row (one a
tensor, as ModelOpt FP8 stores it; one a row, as compressed-tensors
strategy channel does; a block 128 values wide) is held as FP8_B128, an
f32 a 128 values; one covering whole 32-value blocks (MXFP8, and the
E8M0 32 by 32 blocks DeepSeek-V4.1-Flash stores its attention and shared
experts in) as MXFP8, an E8M0 byte a 32 values. Format::held reads that
type's row for every format, so the square count for other FP8 blocks
goes; a block no card type holds has no held form.

Format::fp8_scale_block is the one owner of a scale grid's stored shapes
([r, c], ModelOpt's [r, 1, c, 1], a channel's [rows], a tensor's []),
read by the plan's layout and by modelopt's reader. modelopt::type_of
reads card_type for MXFP8 and the FP8 formats, and one reader (fp8)
writes all of them: MXFP8 and FP8_B128 blocks with each block's scale
looked up in the grid, routed stacks of FP8_B128 for channel and tensor
experts as for 128 by 128 blocks. An MXFP8 stack, an F32 or BF16 scale
on an MXFP8 block and a row that is not whole blocks of the type (a
tensor or row scale over 3136 values) are refused by name.

The oracle reads real rows: two MiniCPM-V 4.7 FP8 experts (BF16 scale
a row), a Nemotron 3 Nano 4B FP8 out_proj (one F32 scale) and a
DeepSeek-V4.1-Flash shared expert block (E8M0 32 by 32), each placed by
its own checkpoint's config; every row decodes to the f32 bits of its
producer's rule (fp8_scales.py ref).
@github-actions github-actions Bot added the model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja label 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: The added FP8 scale layouts preserve weight values and reject unsupported inputs by name.

Checked

  • Duplicates: 14 new or renamed items checked; no duplicated owner. Existing fixture helpers are reused.
  • Complexity: fp8_append consolidates the existing readers; scale preparation and block emission are its stages. card_type adds format-selection cases.
  • Silent failure: 4 production fallbacks traced. Unsupported scale dtype, format, and grid reach named refusals. The writer's default is reachable only for FP8_B128.
  • Tests: Static mutation check: table and MXFP8 assertions reject removal of the stack guard (modelopt.rs:216); synthetic and real-row assertions reject removal of the column term (modelopt.rs:541); held-size assertions reject the former byte count (quant_config.rs:181); the Python self-test rejects removal of the finite-scale check (fp8_scales.py:125).
  • Verification: The Python self-test passed with temporary files held in memory, including fixture hashes, range slices, and 21 undefined-scale refusals. All 51 fixture row hashes matched the checked-in reference. The Rust test covers 50 accepted rows and one named refusal.
  • Adversarial checks: Empty and misaligned dimensions, boundary scale blocks, non-finite values/products, and mismatched expert shapes reach guards or named errors.
  • Gates: Both changed expectations carry dated PIN comments and reasons. The PR supplies FAIL-first mutation results.
  • Escape catalog: No matching patterns.

Notes

Rust tests were not rerun under the output-only write restriction. The PR reports passing host tests. No GPU gate evidence is supplied.

🔨 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 613fb90 into midagedev:main Oct 21, 2026
6 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