Skip to content

fix(dtype): preserve checkpoint and packed tensor precision (#3976) - #3997

Merged
Qubitium merged 3 commits into
mainfrom
fix/issue-3976
Nov 14, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/issue-3976

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

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_inv now 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/main and pass on this branch.

Root causes and fixes

Finding Old behavior and location Fix Current location
1. Quantized resave dtype Loader and checkpoint load were forced to FP16 at gptqmodel/models/writer.py:2156-2168 and :2218-2225; BF16 norm 70144 became inf Resolve get_hf_config_dtype(config) once and pass it to both construction paths gptqmodel/models/writer.py:2170, :2174, :2231
2. GGUF BF16 gptqmodel/nn_modules/qlinear/gguf.py:1037-1039 and gptqmodel/utils/internal_gguf.py:386-388 value-converted bytes to uint16, corrupting values and keeping doubled width _decode_gguf_bf16 reinterprets contiguous byte pairs with .view(np.uint16) and rejects odd final dimensions gptqmodel/nn_modules/qlinear/gguf.py:1030-1051
3. GPTQ bias pack_block, pack_gpu, and pack_original forced bias to FP16 at gptqmodel/nn_modules/qlinear/__init__.py:1492, :1727, :1884; BF16 1e5 became inf Preserve source dtype, including detach semantics for the original packer gptqmodel/nn_modules/qlinear/__init__.py:1492, :1727, :1889
4. String dtype normalize_torch_dtype_kwarg passed strings through at gptqmodel/utils/hf.py:247-275, so the loader treated them as auto Resolve "bfloat16" / "torch.bfloat16" through torch, preserve auto/None, and fail closed on invalid strings gptqmodel/utils/hf.py:247-295
5. FP8 weight_scale_inv A tensor-wide magnitude heuristic at gptqmodel/quantization/dtype.py:270-282, :350-369 let one block flip the whole tensor to division; conversion defaulted to heuristic Add explicit multiplier, inverse, and heuristic semantics; default conversion to multiplier and honor configured inverse gptqmodel/quantization/dtype.py:270-291, :354-383; gptqmodel/utils/model_dequant.py:1044, :1810-1813
flowchart 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:#c00
Loading

The 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

Finding Regression
Loaded BF16 quantized resave tests/test_save_loaded_quantized_model_dtype.py::test_loaded_quantized_resave_preserves_bf16_norm_bits
GGUF BF16 byte pairs tests/test_internal_gguf.py::test_internal_gguf_bf16_reader_reinterprets_byte_pairs
All three GPTQ packers tests/test_quant_scale_overflow.py::test_packers_preserve_source_bfloat16_bias_bits
String dtype resolution tests/test_hf_config_compat.py::test_normalize_torch_dtype_kwarg_resolves_dtype_strings and ::test_normalize_torch_dtype_kwarg_rejects_invalid_dtype_string
FP8 default and inverse tests/test_model_dequant_fp8.py::test_fp8_weight_scale_inv_defaults_to_multiplier and tests/test_quant_dtype.py::test_dequantize_f8_e4m3_with_inverse_scale_semantics

The same nine new cases on unmodified origin/main (d18e2ba1) produced:

9 failed

Measured before:

  • BF16 norm 70144 became FP16 inf, and the saved config dtype was rewritten.
  • GGUF BF16 storage remained shape (2, 8) instead of logical (2, 4) and decoded to garbage values.
  • pack_block, pack_gpu, and pack_original each produced FP16 inf for a BF16 1e5 bias.
  • "bfloat16" and "torch.bfloat16" remained strings instead of becoming torch.bfloat16.
  • FP8 scale 1.5 produced 3.0 relative error and 3.3333 maximum absolute error.

After, the same nine cases on the pinned NVIDIA 4090 and AMD R9700 each produced:

9 passed

Measured after:

  • Both 32-element BF16 norm tensors round-tripped with zero uint16 bit mismatches.
  • GGUF BF16 values matched the independent NumPy/Torch byte oracle exactly and had logical shape (2, 4).
  • All three packers retained BF16 1e5 as finite BF16 with exact source bits.
  • Both dtype strings resolved to torch.bfloat16; an invalid string raised ValueError.
  • Default FP8 multiplication and explicit inverse both matched independent FP64 oracles exactly (rtol=0, atol=0).

FP8-adjacent regression selection:

tests/test_model_dequant_fp8.py tests/test_quant_dtype.py::test_dequantize_f8_e4m3_with_inverse_scale_semantics
7 passed, 30 deselected

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.
  • Mermaid validation: 4 diagram blocks, 8 fences.
  • Branch includes db1bba9c and current main (22fd6c56) through a normal merge; no force-push.
  • No RTX 5090 device was used.

Hardware used for the recorded runs:

  • NVIDIA: GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e (pinned 4090), CUDA ordinal 0 inside the restricted process.
  • AMD: 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.

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.

[BUG] dtype conversion: re-save forces fp16 (inf), GGUF BF16 decode garbage, packers force fp16 bias, string dtype ignored, FP8 scale_inv heuristic

1 participant