Skip to content

fix: compare FP8 checkpoints as dequantized in the tensor check - #39

Merged
redchupa merged 1 commit into
masterfrom
fix/tensor-check-fp8-sources
Dec 5, 2026
Merged

redchupa merged 1 commit into
masterfrom
fix/tensor-check-fp8-sources

Conversation

@redchupa

@redchupa redchupa commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to #37 (tensor check) and #38 (FP8 loading).

Problem

Since #38, checkpoints whose config.json has quantization_config.quant_method == "fp8" (such as tencent/Hy3-FP8) are loaded with their weights dequantized to the model's dtype and exported unquantized. The tensor check from #37 then compares the export with the FP8 source checkpoint and warns about every FP8 weight (F8_E4M3 -> BF16) and every scale tensor (missing). #38 intends both.

On a tiny Hy3-FP8, built like tencent/Hy3-FP8 from tiny-random/hy3 1ea1658 (with the builder from the #38 work), the merged export got:

* Differences from the 7052 tensors of the source checkpoint:
  * 5271 missing (such as model.layers.0.mlp.down_proj.input_scale)
  * 1756 with another dtype (such as model.layers.0.mlp.down_proj.weight: F8_E4M3 -> BF16)

The one real difference is the 593 tensors of the MTP layer, which Transformers doesn't load. It was buried in those 5271 missing tensors, and even the example shown was a scale.

Change

  • check_tensors() reads the source's config.json with get_fp8_quantization_config(source_model, source_revision). That is the function and revision the model loader uses (Model._read_fp8_quantization_config).

  • For an FP8 source, the new dequantize_tensors() turns its tensor list into what the export should contain:

    • the scale tensors (weight_scale, weight_scale_inv, input_scale, activation_scale) are dropped;
    • each F8_* weight takes the dtype it was saved in.

    Then the regular comparison runs, so missing and added tensors, shape changes and other dtype changes are still reported.

  • One line says so: * The FP8 weights of the source checkpoint are compared as dequantized, without their scales.

  • docs/configuration.md: FP8 checkpoints are added to the causes in "Checking that the model is saved faithfully", together with the line Heretic prints for them. Hy3 is added to the models whose MTP layer isn't loaded, since that is what a Hy3-FP8 export still reports.

The same export now gets:

* The FP8 weights of the source checkpoint are compared as dequantized, without their scales
* Differences from the 2374 tensors of the source checkpoint:
  * 593 missing (such as model.layers.4.eh_proj.weight)

That is exactly the report for the BF16 tiny-random/hy3 export. The two exports have identical tensor headers (1781 tensors).

Decisions

  • Only quant_method: "fp8", the format fix: load FP8 checkpoints such as Hy3-FP8 by dequantizing them #38 dequantizes. FP8 tensors in other formats (for example compressed-tensors) are not dequantized by Heretic, so they are still compared as stored. A test covers this.
  • Scales are recognized by the last part of their names, as the loader's key_mapping also renames them by name. If the export does contain a scale, it is reported as added.
  • The dtype of an FP8 weight is taken from the saved model. As a result, an export that still held FP8 weights would not show a dtype difference. That can't happen with fix: load FP8 checkpoints such as Hy3-FP8 by dequantizing them #38, which drops the weight conversions that would re-quantize on save. A re-quantized export would also bring its scales back, and those are reported as added.
  • A local directory without config.json counts as not FP8, because Transformers returns an empty config for it. Every other failure to read the config is reported like a failure to read the tensors ("Could not compare ..."), and the save is never blocked.
  • Not covered: DeepSeek V4. tiny-random/deepseek-v4 names its scales .scale (F8_E8M0) and packs its FP4 experts as I8. Its merged save fails in Transformers 5.18 anyway (its tests export adapters), so the check never runs on it.

Tests

Four new tests in tests/test_tensor_check.py. The FP8 fixture has F8_E4M3 weights, scales under both naming schemes and a config.json:

  • an FP8 source against its dequantized export finds no differences and prints the FP8 line;
  • other differences are still reported: a missing FP8 weight (without its scales), a scale in the export (added), an FP8 weight with another shape, and a non-FP8 tensor with another dtype;
  • a compressed-tensors source is compared as stored;
  • the config is read at the source revision.

On master's tensor_check.py, 3 of the 4 fail. The fourth checks the unchanged behavior.

Verification

  • ruff format --check, ruff check --extend-select I, ty check --error-on-warning, the unit tests (206, 4 new) and uv build pass locally.
  • End to end, the tiny Hy3-FP8 was loaded like Heretic loads it (get_fp8_loading_kwargs, _weight_conversions = None), saved and checked. The before and after output is shown above.
  • I also ran heretic itself on the tiny Hy3-FP8 (Abliteration, 2 trials, preflight_check = true, merged save). The preflight check and the check after saving both print the FP8 line and then 593 missing (such as model.layers.4.eh_proj.weight). The run exits with code 0.
  • I ran the reproducibility tests on this branch and on master 404bcb7, both on this Windows PC. Both have the same 5 known local failures: deepseek-v4, deepseek-v4-ara, gemma-3, gemma-4e and qwen3.5-moe. 66 of the 69 tests/*/model/* files are byte-identical between the two runs. The other 3 are adapter_config.json files that differ only in the order of target_modules. No test merges an FP8 model (the DeepSeek V4 tests export adapters), so none of them prints the new line.
  • No SHA256SUMS file changed.

🤖 Generated with Claude Code

Since FP8 checkpoints such as tencent/Hy3-FP8 are loaded dequantized and
exported unquantized (#38), the tensor check (#37) reported every FP8
weight "with another dtype" (F8_E4M3 -> BF16) and every scale as missing.
For a tiny Hy3-FP8, that buried the one real difference, the 593 tensors
of the MTP layer that Transformers doesn't load, under 5271 missing and
1756 dtype changes.

When the source checkpoint's config.json says that its weights are stored
in FP8 (read like the model loader does, at the same revision), the check
now leaves out the scale tensors (weight_scale, weight_scale_inv,
input_scale, activation_scale) and the dtype change of F8_* weights, and
prints one line saying that the FP8 weights were compared as dequantized.
Every other difference is still reported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@redchupa
redchupa merged commit 185fd7a into master Dec 5, 2026
5 checks passed
@redchupa
redchupa deleted the fix/tensor-check-fp8-sources branch December 5, 2026 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant