Repository navigation
fix(dtype): preserve checkpoint and packed tensor precision (#3976) - #3997
Merged
Merged
Conversation
This was referenced Nov 14, 2026
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.
Fixes #3976
TL;DR
Checkpoint resaves now retain the loaded model dtype. GGUF BF16 payloads are decoded by reinterpreting byte pairs. GPTQ packers preserve the source bias dtype. Explicit dtype strings are normalized before loader selection. FP8
weight_scale_invnow defaults to multiplier semantics, with explicit inverse semantics still available.The five regressions use plain NumPy/Torch oracles and bit comparisons rather than the implementation under test. All nine new cases failed on unmodified
origin/mainand pass on this branch.Root causes and fixes
gptqmodel/models/writer.py:2156-2168and:2218-2225; BF16 norm70144becameinfget_hf_config_dtype(config)once and pass it to both construction pathsgptqmodel/models/writer.py:2170,:2174,:2231gptqmodel/nn_modules/qlinear/gguf.py:1037-1039andgptqmodel/utils/internal_gguf.py:386-388value-converted bytes touint16, corrupting values and keeping doubled width_decode_gguf_bf16reinterprets contiguous byte pairs with.view(np.uint16)and rejects odd final dimensionsgptqmodel/nn_modules/qlinear/gguf.py:1030-1051pack_block,pack_gpu, andpack_originalforced bias to FP16 atgptqmodel/nn_modules/qlinear/__init__.py:1492,:1727,:1884; BF161e5becameinfgptqmodel/nn_modules/qlinear/__init__.py:1492,:1727,:1889normalize_torch_dtype_kwargpassed strings through atgptqmodel/utils/hf.py:247-275, so the loader treated them as auto"bfloat16"/"torch.bfloat16"throughtorch, preserveauto/None, and fail closed on invalid stringsgptqmodel/utils/hf.py:247-295weight_scale_invgptqmodel/quantization/dtype.py:270-282,:350-369let one block flip the whole tensor to division; conversion defaulted to heuristicmultiplier,inverse, andheuristicsemantics; default conversion to multiplier and honor configured inversegptqmodel/quantization/dtype.py:270-291,:354-383;gptqmodel/utils/model_dequant.py:1044,:1810-1813flowchart TD A["loaded checkpoint config"] --> B["get_hf_config_dtype"] B --> C["loader.from_config(dtype=model_dtype)"] B --> D["load_checkpoint_in_model_then_tie_weights(dtype=model_dtype)"] E["GGUF BF16 uint8 bytes"] --> F["_decode_gguf_bf16"] F --> G["contiguous view(np.uint16)"] G --> H["dequantize_to_torch / _dequantize_gguf_tensor_numpy"] I["GPTQ source bias"] --> J["pack_block / pack_gpu / pack_original"] J --> K["source dtype buffer"] L["normalize_torch_dtype_kwarg"] --> M{"string value"} M -- auto --> N["auto selection"] M -- torch dtype name --> O["torch dtype"] M -- invalid --> P["ValueError"] Q["weight_scale_inv"] --> R{"weight_scale_semantics"} R -- absent / multiplier --> S["weight * scale"] R -- inverse --> T["weight / scale"] R -- heuristic --> U["explicit legacy magnitude heuristic"] X["Old resave"] --> Y["FP16 overflow"] style X fill:#fee,stroke:#c00 style Y fill:#fee,stroke:#c00The committed reference page is docs/dtype-boundary-preservation.md, linked from
README.md. It includes four Mermaid diagrams, the full before/after table, and the failure/ambiguity contract.Regressions and measured results
tests/test_save_loaded_quantized_model_dtype.py::test_loaded_quantized_resave_preserves_bf16_norm_bitstests/test_internal_gguf.py::test_internal_gguf_bf16_reader_reinterprets_byte_pairstests/test_quant_scale_overflow.py::test_packers_preserve_source_bfloat16_bias_bitstests/test_hf_config_compat.py::test_normalize_torch_dtype_kwarg_resolves_dtype_stringsand::test_normalize_torch_dtype_kwarg_rejects_invalid_dtype_stringtests/test_model_dequant_fp8.py::test_fp8_weight_scale_inv_defaults_to_multiplierandtests/test_quant_dtype.py::test_dequantize_f8_e4m3_with_inverse_scale_semanticsThe same nine new cases on unmodified
origin/main(d18e2ba1) produced:Measured before:
70144became FP16inf, and the saved config dtype was rewritten.(2, 8)instead of logical(2, 4)and decoded to garbage values.pack_block,pack_gpu, andpack_originaleach produced FP16inffor a BF161e5bias."bfloat16"and"torch.bfloat16"remained strings instead of becomingtorch.bfloat16.1.5produced3.0relative error and3.3333maximum absolute error.After, the same nine cases on the pinned NVIDIA 4090 and AMD R9700 each produced:
Measured after:
uint16bit mismatches.(2, 4).1e5as finite BF16 with exact source bits.torch.bfloat16; an invalid string raisedValueError.rtol=0,atol=0).FP8-adjacent regression selection:
This passed on both the pinned NVIDIA 4090 and AMD R9700.
Validation
ruff check gptqmodel docs/eora tests setup.py: passed.git diff --check: passed.db1bba9cand currentmain(22fd6c56) through a normal merge; no force-push.Hardware used for the recorded runs:
GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e(pinned 4090), CUDA ordinal 0 inside the restricted process.HIP_VISIBLE_DEVICES=1(R9700).A broader diagnostic run also surfaced unrelated environment/pre-existing failures in Transformers compatibility tests and a CUDA environment without
bitsandbytes; those paths are outside this dtype change and are excluded from the focused evidence above.