Skip to content

gguf: refuse F8 rows whose scaled values are not finite - #710

Merged
juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:fix-f8-row-overflow
Oct 23, 2026
Merged

juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:fix-f8-row-overflow

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

F8 행 디코더가 f32 범위를 넘는 값을 무한대로 내보내던 결함을 고쳐, 쓰기 전에 이름 있는 오류로 거부합니다.

Part of #565. Tracked in #699.

What and why

F8 row decoding could return Ok with infinite values. E4m3Rows::new multiplies each E4M3 byte by the tensor scale when it builds its table, and a normal byte times a large finite scale can leave the f32 range. decode refused only E4M3's NaN bytes, so the infinity went into the output. The per-layer embedding rows read through PleRows::F8 use the same decoder, so invalid values could reach inference. The decoder came in with #490.

E4m3Rows::check now reports the first byte whose scaled value is not finite, by index, before anything is written. A NaN byte keeps E4m3Nan. A non-finite product, or a non-finite scale, is the new QuantError::E4m3NonFinite. decode calls check, and a refused span leaves the output buffer bit for bit as it was. The arithmetic of finite values and the write order are unchanged.

The HF FP8 tensor normalization had its own copy of this check: it decoded the tensor 256 values at a time into a scratch array and scanned the result. It now calls E4m3Rows::check on the bytes once, so the decoder owns the rule and no scratch array is needed. The NVFP4, MXFP8 and FP8_B128 range checks already refuse the same class of input and are untouched.

Release notes: Fixed: F8 rows whose scaled values overflow f32 are refused with a named error instead of decoding to infinity.

Code shape

  • No second copy. E4m3Rows::check is the one owner. The HF normalization check that duplicated it is gone.
  • Model code holds only architecture facts. No model-specific branch was added. The per-layer embedding F8 path already calls the shared decoder.
  • Dead code is gone. The HF scratch array and its follow-up scan were removed.
  • No silent failure. A scaled value outside f32 is a named error, and the output is not written.
  • Comments state what is true now. The doc comments describe the refusal and the check before writing.
  • No relaxed gate. No band, tolerance or lint moved.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up. cargo fmt --all and git diff --check are clean; clippy has not been run on this head.

Duplication found or removed (path:line), or "none": the per-chunk decode and finiteness scan in crates/hf/src/modelopt.rs (plain) is replaced by the shared E4m3Rows::check.

What I ran

The tested head is f0705c9bae9e100c1271d411b84e87a6f41ed3ab, based on b9dfc4a748ea7d7b713ec831100761ef047b5472.

  • FAIL-first: the regression test alone, on the base, cargo test -p bloomery-gguf --lib quant::tests::e4m3_rows_refuse_nonfinite_products_before_writing -- --exact --nocapture. With scale f32::MAX and byte 0x40, expect_err panics because decode returned Ok: test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 50 filtered out; finished in 0.00s, exit 101.
  • RUSTFLAGS='-C target-cpu=native' cargo test -p bloomery-gguf --lib quant::tests::: test result: ok. 25 passed; 0 failed; 0 ignored; 0 measured; 26 filtered out; finished in 0.51s. It includes the new test, the existing all-byte oracle and the NVFP4, MXFP8 and FP8_B128 range tests.
  • RUSTFLAGS='-C target-cpu=native' cargo test -p bloomery-hf --lib modelopt::tests::the_table_writes_each_format_as_its_type -- --exact: test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 85 filtered out; finished in 0.00s. The NaN byte, non-finite scale and finite-scale overflow refusals still hold through the shared check.
  • On an RTX 5090 machine, cargo check --workspace --all-targets with the feature list just check uses exited 0 (Finished dev profile in 27.65s). nvidia-smi reported NVIDIA GeForce RTX 5090, 595.99.02, 12.0.

The new test covers four scales (f32::MAX, its negative and their halves) against positive and negative bytes. A product of f32::MAX times 1 or -1 is accepted, times 2 is refused, and half the scale times 2 is accepted. It also checks a NaN byte after a valid span, signed zero, an infinite and a NaN scale, and that the buffer keeps its original bits when a span is refused. Restoring the NaN-only check makes expect_err fail, and moving the write before the check makes the bit comparison fail.

Not run: clippy, t0 and the full gate set. No CUDA kernel or device arithmetic changed, so I ran no GPU gate and no timing comparison.

Reproduction

cargo test -p bloomery-gguf --lib quant::tests::e4m3_rows_refuse_nonfinite_products_before_writing -- --exact
cargo test -p bloomery-gguf --lib quant::tests::
cargo test -p bloomery-hf --lib modelopt::tests::the_table_writes_each_format_as_its_type -- --exact

The first command is the new regression test.

What I ran into

This does not block the PR: I found nothing else while preparing this change.

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 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: F8 overflow is rejected by the shared validator before any destination write.

Non-blocking

⚠ Debuggability: crates/gguf/src/quant.rs:329. The fix adds no retained probe, stats() output or diagnostic record, and the PR body gives no reason for omitting that layer. Explain why the named byte/index error and reproduction command provide sufficient diagnostics.

Checked

Duplicates: 3 new items checked, including the error variant; HF normalization now reuses the shared F8 validator.
Complexity: the validator scans for the first invalid value; HF normalization loses its chunk loop and scratch buffer.
Silent failure: 2 error paths followed. Row decoding and HF normalization propagate named errors before exposing decoded or normalized output. NVFP4, MXFP8 and FP8_B128 already reject overflowing products.
Tests: static mutation check of crates/gguf/src/quant.rs:994: restoring the NaN-only predicate fails the overflow expect_err assertion. Moving validation after writes at crates/gguf/src/quant.rs:1021 fails the destination-bit assertion.
Adversarial checks: late overflow cannot partially write; short input returns ShortSrc; trailing bytes outside the requested span are excluded. A NaN byte remains invalid at zero scale.
Gates: no pin, threshold or test clause moved. The PR supplies FAIL-first evidence for finite-scale overflow.
Escape catalog: no pattern matched.

Notes

The supplied test runs name f0705c9; the reviewed head is 2d64226. Tests were not rerun during review or validation.

🔨 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 648dd75 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

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