Repository navigation
fix: load FP8 checkpoints such as Hy3-FP8 by dequantizing them - #38
Merged
Merged
Conversation
tencent/Hy3-FP8 stores each FP8 weight matrix with one scale (`weight_scale`, plus a static `input_scale`), but Transformers only knows the names of per-block scales (`weight_scale_inv`), so it dropped the scales. On CPU the experts' weights stayed in FP8 and the first forward pass failed; on GPUs with FP8 support the weights were kept in FP8, which needs Triton (missing on Windows) and can't be modified by Heretic. Heretic now loads every checkpoint with `quant_method: "fp8"` with its weights dequantized to the dtype it loads the model with, and renames per-tensor scales to the names Transformers expects. It drops the weight conversions that Transformers keeps for saving: reversing them fails for MoE models and writes FP8 weights without a quantization configuration for others. Exported models are therefore unquantized, in the layout of the model's unquantized version. Transformers can't quantize FP8 checkpoints to 4 bits, so that combination is refused with a clear message. reset_model() reads the quantization configuration of the model it loads, which differs from the first one with evaluate_model. Verified with a tiny checkpoint built from tiny-random/hy3 in the format of tencent/Hy3-FP8, on CPU and on an RTX 5070 Ti. Roadmap: stage 3, Hy3 (p-e-w/heretic#436). p-e-w/heretic#182 isn't ported: no FP8 weights reach LoRA or merging anymore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
redchupa
added a commit
that referenced
this pull request
Dec 5, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
Roadmap stage 3, Hy3 (p-e-w/heretic#436). Transformers 5.18 implements Hy3 (
tiny-random/hy3already worked on master), but the official FP8 checkpoint,tencent/Hy3-FP8, could not be loaded. Its format was never checked because no small FP8 variant exists and the real model is about 300 GB. This PR builds a tiny checkpoint fromtiny-random/hy3in the same format and makes Heretic load, modify and export it correctly.The format (from the
config.jsonand the safetensors headers oftencent/Hy3-FP8at commitecc1d8e):quantization_config:{"activation_scheme": "static", "ignored_layers": ["lm_head", "model.embed_tokens"], "quant_method": "fp8", "kv_cache_scheme": "static"}, with noweight_block_size.<module>.weight(F8_E4M3),<module>.weight_scale(BF16, a single value with no dimensions, so weight = fp8 × weight_scale) and<module>.input_scale(F32, shape[1], a static activation scale).self_attn.{q,k,v,o}_proj, the dense MLP'smlp.{gate,up,down}_proj,mlp.shared_mlp.*and every expert'smlp.experts.<E>.{gate,up,down}_proj(stored per expert), including in the MTP layer. Not quantized: embeddings,lm_head, norms, the router,expert_biasand the MTP layer'seh_proj. There are no KV cache scales, althoughkv_cache_schemeis"static".Problem. Transformers only knows the names that checkpoints with one scale per block use (
weight_scale_inv,activation_scale). It dropped the per-tensor scales:RuntimeError: Expected mat_a to be Float32, BFloat16 or Float16 matrix, got Float8_e4m3fn.Failed to load model: No module named 'triton'on Windows. Where Triton is available, the weights would be loaded in FP8 with their scales missing, and Heretic can't modify FP8 weights anyway.Changes (
model.py):quantization_confighasquant_method: "fp8"is loaded withFineGrainedFP8Config(dequantize=True), so its weights arrive in the dtype Heretic loads the model with (bfloat16 for Hy3). For checkpoints withoutweight_block_size, akey_mappingrenamesweight_scaletoweight_scale_invandinput_scaletoactivation_scale, so Transformers applies the scales.save_pretrainedare dropped. Reversing the FP8 dequantization fails for MoE models (ValueError: Undefined Operation encountered!). For other models it writes FP8 weights and scales again, but without thequantization_configneeded to load them. Without the kept conversions, the model is saved like one that wasn't loaded from a checkpoint: unquantized, in the layout of the model's unquantized version (for Hy3, that oftencent/Hy3).quantization = "bnb_4bit"are refused with a clear message. Transformers can't combine the two and would otherwise fail with aValueErrorabout mismatched quantization config classes.reset_model()reads the quantization configuration of the model it actually loads. Withevaluate_model, it loads a different model than the first one, for example a BF16 export of an FP8 model.Decisions.
tencent/Hy3. Dropping the kept conversions relies on_weight_conversions, a private attribute of Transformers 5. The new save test fails if a Transformers update changes how it works.quant_method: "fp8"checkpoints, not only Hy3. Checkpoints with block scales (e.g. DeepSeek V3, Qwen3-FP8) had the same problems on FP8-capable GPUs and when saving. I checked tiny dense and MoE checkpoints with block scales: they load exactly and save correctly with this change, and the save fix is needed for both.tests/deepseek-v4*, block scales) is the only FP8 model in the reproducibility tests. On CPU its parameters are identical to master's (names, dtypes and values compared). Its merged save no longer crashes, but Transformers 5.18 can't reload any saved DeepSeek V4 correctly: a model created from its config, without FP8 or Heretic, fails with a dtype mismatch in its first forward pass after saving and reloading. Its tests therefore keep exporting the adapter.input_scaletensors as unexpected (3 lines per MoE layer). That is cosmetic.Linearlayers with block scales, and was written for Heretic before trials were merged into the weights (fix: score trials with the adapters merged into the weights #26). Its other commits are unrelated to FP8 (evaluator scoring with zero base refusals, NaN KL divergence, log_softmax in float32 with a clamp, themax_weightrange, target modules found by identity). The stage 5 roadmap item now lists them for a separate review.tencent/Hy3have the same limitation.Testing
tests/*/SHA256SUMS.*file changes, or the description explains why model outputs are meant to change.Windows 11, Python 3.12.13, torch 2.14.1, Transformers 5.18.0, PEFT 0.22.0.
ruff format --check,ruff check --extend-select I,ty check --error-on-warninganduv buildpass. The unit tests pass: 192 tests, OK with the 2 expected failures as on master.tests/run_tests.pyon this branch and on master b50e6f0, on the same machine.tests/*/model/*files, 66 are byte-identical between the two runs. They include the adapters of the two DeepSeek V4 tests, which load an FP8 checkpoint. The other 3 areadapter_config.jsonfiles with the sametarget_modulesin a different order (PEFT keeps them in a set).tests/test_model_loading.pyhas 5 new tests.save_hy3_fp8_checkpoints()builds a small random Hy3 model in thetencent/Hy3-FP8format, together with the exact dequantization of its FP8 weights in thetencent/Hy3format.quantization_config.reset_model()loads a different model correctly, and the other way round. On the code without thereset_model()fix, this test fails in both directions: the BF16 model gets aFineGrainedFP8Config, and the FP8 model's weights are loaded unscaled.tiny-random/hy3at1ea1658. It quantizes per tensor with the layout above, including the MTP layer, and calibrates the static input scales on 8 prompts.tiny-random/hy3(BF16), with Abliteration and with ARA. Both exports have the same 1781 tensors with the same shapes and dtypes, and noquantization_config. Abliteration changed the same 5 tensors in both runs (theo_projand shared-expertdown_projweights of layers 1–3). ARA changed none in either run, because the random model refuses nothing.No module named 'triton'). With this branch, the weights and logits equal the exact dequantization on CUDA, and Heretic runs with Abliteration and ARA to a merged export. Both exports load and generate oncuda:0.reset_model()now reads the configuration of the model it loads, which is the same configuration unless the model changes. Repeated on CPU with the final code, both FP8 runs export byte-identical files.AI use declaration
understand it, and can answer questions about it without relying on the AI.
🤖 Generated with Claude Code