Skip to content

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

Closed

Description

@Qubitium

Audit of main @ a16977e. Silent precision loss or corruption where tensors cross dtype boundaries. Related, but still reproducible on HEAD: #3519.

1. Re-saving a loaded quantized model converts every non-quantized tensor to fp16 (high)

gptqmodel/models/writer.py:2149-2151 (self.loader.from_config(config, dtype=torch.float16)) and :2206-2208 (load_checkpoint_in_model_then_tie_weights(model, dtype=torch.float16, ...)). Reached from save_quantized (:1861-1865) whenever load_quantized_model is set and the format isn't EXL3.

Embeddings, norms, lm_head and biases are rewritten as fp16, and config.json dtype becomes float16. No warning is logged. self.model is replaced by the fp16 model too.

Repro: tiny bf16 Llama → quantize → GPTQModel.load → .save():

  • model.norm.weight bf16 70144 → fp16 inf
  • input_layernorm 9.98e-7 → 1.013e-6
  • config.dtype float16

Fix: use the loaded model's dtype (e.g. get_hf_config_dtype(self.model.config)) in both calls.

2. GGUF BF16 tensors decode to garbage (high for BF16 GGUFs)

gptqmodel/utils/internal_gguf.py:386-388 and gptqmodel/nn_modules/qlinear/gguf.py:1037-1039 use np.asarray(data, dtype=np.uint16). GGUFReader._read_tensors (internal_gguf.py:668-688) returns BF16 as uint8 bytes of shape (..., 2*cols), so each byte is value-converted instead of byte-pairs being reinterpreted.

Repro: write a real 2×4 BF16 GGUF containing [1.0, -2.5, 3.14, 0.001]. It reads back as [1.1755e-38, 5.79e-39, ...] with shape (2,8). This affects non-quantized tensors (norms, embeddings) via loader.py:1217 and hf.py:545.

Fix: np.ascontiguousarray(data).view(np.uint16) (when the dtype is uint8), then halve the last dim.

3. GPTQ packers force the bias to fp16 (inf for large bf16/fp32 biases)

gptqmodel/nn_modules/qlinear/__init__.py:

  • :1492 (pack_block)
  • :1727 (pack_gpu)
  • :1884 (pack_original)

All three use linear.bias...to(dtype=t.float16). A bf16 bias of 1e5 becomes inf with all three packers. torch_awq.py:125-128 deliberately keeps the source dtype, and _preserve_checkpoint_buffer_dtypes in packing.py exists to prevent exactly this.

Fix: keep the source dtype, or go through a range-checked helper like _scales_for_storage.

4. A string dtype passed to load/quantize is silently replaced by auto

gptqmodel/models/loader.py:1330, 1493, 1812: elif dtype is None or dtype == "auto" or not isinstance(dtype, torch.dtype): dtype = auto_dtype(...). The signatures accept str | torch.dtype, but dtype="float16" / "bfloat16" is ignored without a warning. Only the SGLang path normalizes strings (_normalize_sglang_load_dtype).

For example, "bfloat16" on MPS/XPU silently becomes fp16, with overflow risk for large-norm models.

Fix: resolve strings via getattr(torch, s.removeprefix("torch.")) before the auto branch.

5. The FP8 weight_scale_inv multiply-vs-divide decision is a per-tensor max heuristic (latent)

gptqmodel/quantization/dtype.py:270-282 (_fast_scale_arg) and :395-399 (_dequantize_f8_reference) multiply if max|scale_inv| <= 1, else divide. This is the default for convert_fp8_shard without weight_scale_semantics (model_dequant.py:1792). DeepSeek/Qwen-style checkpoints store the multiplier (block amax/448) under weight_scale_inv. One block with amax > 448 flips the whole tensor to division.

Repro: 256×256 weight, 128×128 blocks. Max scale 0.9 gives error 0. One block at 1.5 gives relative error 666×.

Fix: default to the multiplier semantics for weight_scale_inv unless the config says inverse, or at least warn when the heuristic picks divide.

🤖 Generated with Claude Code

Activity

  1. added
    bug
    on Nov 14, 2026
  2. Qubitium commented on Nov 14, 2026

    @Qubitium
    CollaboratorAuthor

    Related: #3985 item 3 covers the same re-save path (writer.py:1861-1865). It rebuilds the whole model from disk (5.1 s / +2.6 GB, against 0.65 s / +0.5 GB with streaming) and replaces self.model with a CPU copy after save(). The fp16 cast in item 1 here and that rebuild are best fixed together.

  3. Qubitium commented on Nov 14, 2026

    @Qubitium
    CollaboratorAuthor

    Follow-up review of the merged fix found remaining problems; see #4003.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions