Skip to content

fix: keep packed and scale buffer dtypes on checkpoint re-save (#4303) - #4334

Merged
Qubitium merged 1 commit into
mainfrom
pr/4303
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
pr/4303

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Re-saving a loaded quantized checkpoint no longer casts packed/scale buffers to the model dtype. Fixes the FP8 regression (re-saved checkpoint became unloadable) and the GPTQ/QQQ regression (FP16/FP32 scales were silently narrowed to BF16) introduced by #4235 (08f1b2b).

What Changed

gptqmodel/models/writer.py — get_model_with_quantize (export-shell path) now loads the rebuilt shell with dtype=None instead of dtype=model_dtype.

With a device_map, accelerate routes every tensor through set_module_tensor_to_device(value=param, dtype=dtype). Passing the model dtype therefore cast every floating tensor to bf16/fp16:

  • packed FP8 weight (float8_e4m3fn) → bf16, and weight_scale_inv fp32 → bf16. The re-saved checkpoint then raised RuntimeError: Checkpoint buffer '...weight' must have packed dtype torch.float8_e4m3fn, got torch.bfloat16, and roughly doubled in size.
  • GPTQ scales fp16 → bf16 (112/112 tensors on a real checkpoint, up to 3.9e-3 relative error).
  • QQQ s_channel fp32 → model dtype.

dtype=None makes accelerate copy each checkpoint tensor into the existing shell buffer dtype (value.to(old_value.dtype)). Every quantized module allocates its buffers in the checkpoint's storage dtype (packed FP8 weight, FP16 GPTQ scales, FP32 QQQ s_channel), so the round trip is byte-identical. This is the same semantics the pre-08f1b2b set_model_state_dict path had.

The device_map={"": "cpu"} argument is intentionally kept: it is what makes each shard load exactly once, which #4166 depends on. Normal (non-export) loading is untouched.

Tests

New file tests/test_resave_preserves_buffer_dtypes_4303.py:

  • test_export_shell_load_does_not_cast_dtypes — spies on the writer's load_checkpoint_in_model_then_tie_weights and asserts dtype is None; asserts every stored *.scales stays fp16 with identical bytes across the re-save.
  • test_gptq_resave_is_byte_identical — tiny bf16 Llama, GPTQ 4-bit g32: quantize → save → load → save; every tensor keeps its dtype and bytes, and the total checkpoint size is unchanged.
  • test_fp8_resave_keeps_packed_weight_and_scales — tiny bf16 Llama, FP8: quantize → save → load (FP8_TORCH) → save; weight stays float8_e4m3fn, weight_scale_inv stays fp32, bytes identical, file size identical, and the re-saved checkpoint loads.

All three fail on the parent commit and pass with the fix.

$ python3 -m pytest tests/test_resave_preserves_buffer_dtypes_4303.py -q
3 passed

$ python3 -m pytest tests/test_resave_preserves_buffer_dtypes_4303.py \
    tests/test_save_loaded_quantized_model_dtype.py \
    tests/test_checkpoint_interop_4053.py \
    tests/test_fp8_packed_weight_lifecycle.py \
    tests/test_writer_instance_attrs.py \
    tests/test_fp8_bf16_scale_reload.py \
    tests/test_model_load_lock_4204.py -q
58 passed, 4 skipped

$ ruff check gptqmodel/models/writer.py tests/test_resave_preserves_buffer_dtypes_4303.py
All checks passed!

Not run: QQQ end-to-end re-save — the QQQ JIT extension does not build on this CPU-only box (qqq_runtime_available() is False), so the QQQ case is covered by the dtype=None guarantee plus the fp32 s_channel shell allocation rather than a round-trip test.

Fixes #4303

The re-save export shell loaded its checkpoint with
load_checkpoint_in_model_then_tie_weights(dtype=model_dtype,
device_map={"": "cpu"}). With a device_map, accelerate sends every tensor
through set_module_tensor_to_device(value=param, dtype=dtype), which casts
every floating tensor to the model dtype: packed FP8 weights and their FP32
scales became bf16 (the re-saved checkpoint then failed to reload) and FP16
GPTQ/QQQ scales became bf16 (a lossy re-save).

Load the export shell with dtype=None so accelerate copies each checkpoint
tensor into the existing shell buffer dtype instead. Every quantized module
allocates its buffers in the checkpoint's storage dtype (packed FP8 weight,
FP16 GPTQ scales, FP32 QQQ s_channel), which is also what the pre-08f1b2b
set_model_state_dict path did. The device_map stays: it is what makes each
shard load exactly once (#4166).

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

Labels

None yet

Projects

None yet

1 participant