Skip to content

fix(checkpoint): preserve checkpoint buffer dtypes on re-save (#4379) - #4417

Merged
Qubitium merged 1 commit into
mainfrom
fix/resave-dtype-4379
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/resave-dtype-4379

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4379.

Problem

GPTQModel.load(out).save(out2) still narrowed some fp16 tensors to the model dtype:

  • EXL3 — all 14 suh/svh tensors per layer went fp16 → bf16 (they are scaled values, so the narrowing is lossy).
  • fp16 biases under a bf16 config — re-saving the legacy Qwen2.5-0.5B-Instruct-gptq-4bit checkpoint (fp16 biases, torch_dtype: bf16) wrote all 72 biases as bf16.

The export shell (get_model_with_quantize) builds from_config(dtype=model_dtype) and then loads with dtype=None, assuming every buffer is already allocated in the checkpoint dtype. That holds for the #4303-covered methods, but not for EXL3's sign vectors, and the shell's dense biases are allocated in the model dtype.

Fix

gptqmodel/utils/model.py:

  • new _checkpoint_tensor_dtypes() — reads each safetensors tensor's stored dtype from the header only (no payload materialization), handling a single file, a directory, or an .index.json shard map;
  • load_checkpoint_in_model_then_tie_weights() accepts restore_checkpoint_buffer_dtypes=True and extends the existing restore_dtypes set with every floating buffer whose shell dtype differs from the checkpoint, so the existing _restore_checkpoint_buffers() path restores it at the checkpoint dtype.

Enabled at the two call sites that re-save rather than consume:

  • gptqmodel/models/writer.py export shell → fixes the bias case;
  • gptqmodel/models/loader.py for FORMAT.EXL3 → suh/svh stay fp16 on load, and therefore on re-save.

This reuses the existing #4303 dtype-restoration mechanism rather than adding a per-tensor special case.

Tests

New tests/test_resave_preserves_checkpoint_dtypes_4379.py (CPU, no downloads):

  • GPTQ Torch quantize → legacy fp16-bias checkpoint under a bf16 config → load → re-save: biases stay fp16. Fails pre-fix (written as bf16);
  • synthetic CPU EXL3 checkpoint loaded with BACKEND.EXL3_TORCH: suh/svh stay fp16. Fails pre-fix (became bf16).

Verification:

  • new file: 2 passed; pre-fix: 2 failed.
  • dtype/serialization batch (test_resave_preserves_buffer_dtypes_4303, test_checkpoint_scale_dtype, test_fp8_save_offload, test_checkpoint_bias_dtype_4324, test_exl3_buffer_dtype) → 16 passed, 1 skipped.
  • test_checkpoint_interop_4053.py and the wider dtype batches → no new failures; the only failures observed (missing local TinyLlama checkpoint, no AVX512-BF16 host) reproduce on pristine main.
  • ruff check on all changed files → clean.

Residual risk

  • Restoration is buffer-based. Quantized-module biases and EXL3 sign vectors are buffers, so the reported cases are covered; a floating parameter in a non-quantized module that differs from the model dtype would not be restored.
  • Dtype discovery covers safetensors only. Legacy .bin/.pt checkpoints keep current behavior for mismatched buffers.
  • EXL3 quantization could not run here (no CUDA toolkit / JIT build); only its load/save dtype handling was runtime-verified with a synthetic checkpoint.
  • _checkpoint_tensor_dtypes() opens each shard once to read headers, adding one header pass to save/EXL3-load.

The export shell allocates non-quantized buffers in the model dtype, so
accelerate's dtype=None copy narrowed a legacy checkpoint's fp16 biases to
bf16 on re-save. The EXL3 loader likewise cast suh/svh to the model dtype
before the save path republished them.

Read each tensor's stored dtype from the safetensors headers and restore any
floating buffer whose shell dtype differs at its checkpoint dtype. Apply the
mechanism to the export shell and, for EXL3, to the main loader so its sign
vectors also satisfy the kernel's fp16 contract.

Fixes #4379
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] Re-save still narrows some fp16 tensors to the model dtype: EXL3 suh/svh, and fp16 biases under a bf16 config (rest of the #4303 class)

1 participant