Repository navigation
fix(checkpoint): preserve checkpoint buffer dtypes on re-save (#4379) - #4417
Merged
Merged
Conversation
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
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 #4379.
Problem
GPTQModel.load(out).save(out2)still narrowed some fp16 tensors to the model dtype:suh/svhtensors per layer went fp16 → bf16 (they are scaled values, so the narrowing is lossy).Qwen2.5-0.5B-Instruct-gptq-4bitcheckpoint (fp16 biases,torch_dtype: bf16) wrote all 72 biases as bf16.The export shell (
get_model_with_quantize) buildsfrom_config(dtype=model_dtype)and then loads withdtype=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:_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.jsonshard map;load_checkpoint_in_model_then_tie_weights()acceptsrestore_checkpoint_buffer_dtypes=Trueand extends the existingrestore_dtypesset 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.pyexport shell → fixes the bias case;gptqmodel/models/loader.pyforFORMAT.EXL3→suh/svhstay 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):BACKEND.EXL3_TORCH:suh/svhstay fp16. Fails pre-fix (became bf16).Verification:
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.pyand the wider dtype batches → no new failures; the only failures observed (missing local TinyLlama checkpoint, no AVX512-BF16 host) reproduce on pristinemain.ruff checkon all changed files → clean.Residual risk
.bin/.ptcheckpoints keep current behavior for mismatched buffers._checkpoint_tensor_dtypes()opens each shard once to read headers, adding one header pass to save/EXL3-load.