Skip to content

[BUG] Regression from #4856 (fix of #4805): QQQ save() after any in-session forward writes a checkpoint with no B/s_group/s_channel (54 -> 12 tensors); reload hits illegal memory access / NaN #5010

Closed

Description

@Qubitium

Found on main @ c2b0bf6, RTX 5090 (sm_120), torch 2.15 / CUDA 13.2, transformers 5.22.

What is wrong

Since #4856 (fix of #4805), the first CPU forward of QQQTorchLinear drops the checkpoint buffers after it builds the AVX512 packed cache. _maybe_drop_checkpoint_tensors sets self._buffers["B"|"s_group"|"s_channel"] = None (gptqmodel/nn_modules/qlinear/qqq.py:1003-1005). Only QQQTorchLinear._save_to_state_dict (qqq.py:1090-1097) knows how to rebuild them.

GPTQModel.save() does not call state_dict(). get_state_dict_for_save walks module.named_buffers(recurse=False) (gptqmodel/utils/model.py:2898-2918, and the offload variant at :2777-2811). PyTorch skips None buffers there, so the three dropped buffers are not in the save manifest at all. _restore_checkpoint_layout_for_export (model.py:2816-2836) only restores modules that have a _fused_train_state snapshot. It never calls QQQTorchLinear._materialize_dropped_checkpoint().

After quantize(), the in-session model is on the CPU with QQQTorchLinear (with or without offload_to_disk). So the ordinary flow quantize → evaluate/generate → save writes a QQQ checkpoint that contains no quantized weights: only embeddings, norms and biases. save() raises nothing and logs no warning.

Loading does not catch it either. validate_checkpoint_qweights (model.py:3064-3108) checks only GPTQ/AWQ-family formats and the qweight key. A QQQ checkpoint with no B/s_group/s_channel therefore loads with uninitialized buffers:

Repro

Qwen2.5-0.5B-Instruct (2-layer copy; the full model behaves the same).

import torch
from gptqmodel import GPTQModel
from gptqmodel.quantization import QQQConfig
from gptqmodel.quantization.config import CalibrationConfig
from safetensors import safe_open

m = GPTQModel.load("Qwen/Qwen2.5-0.5B-Instruct", QQQConfig(bits=4, group_size=128), dtype=torch.float16)
m.quantize(CalibrationConfig(dataset=calib_32_c4_rows))
ids = m.tokenizer("The capital of France is Paris.", return_tensors="pt").input_ids
with torch.inference_mode():
    m.model(ids)                      # any in-session forward / generate / evaluation
m.save("qqq_out")
print(len(safe_open("qqq_out/model.safetensors", "pt").keys()))
r = GPTQModel.load("qqq_out", dtype=torch.float16)
r.model(ids.cuda())

Output (2-layer model):

                         saved keys  layer0 q_proj keys                         reload forward
no forward before save   54          B, bias, s_channel, s_group                finite logits
forward before save      12          bias                                       CUDA error: an illegal memory access was encountered

Bisect, with the same script and a forward before save:

With offload_to_disk=False the result is identical: 12 keys, and the reload crashes. Loading the 12-key checkpoint with backend=BACKEND.QQQ_TORCH gives all-NaN logits, not an error.

Suggested fix

  1. Materialize the QQQ checkpoint layout before collecting tensors. Either have _restore_checkpoint_layout_for_export call _materialize_dropped_checkpoint() on every module that has _torch_dropped_checkpoint, or give quantized kernels a generic "export my checkpoint tensors" hook that get_state_dict_for_save uses instead of the raw named_buffers walk. The next CPU forward drops the buffers again, so this costs nothing at steady state.
  2. Make the load fail closed. validate_checkpoint_qweights should check every quantized kernel's required checkpoint tensors (QQQ B/s_group/s_channel, and similar keys for FP8, EXL3, GGUF, NF12 and so on), not just qweight for GPTQ/AWQ. A truncated checkpoint should raise, not crash the CUDA context or return NaN.

Tests that should have caught it

tests/test_qqq_cpu_residency_4805.py checks state_dict() after the drop, but not GPTQModel.save(). Add an end-to-end test: QQQ quantize → one in-session forward → save() → assert that every B/s_group/s_channel key is present → reload and compare logits. The existing "forward before save is bytewise identical to save without forward" check should include QQQ.

Severity

High. A common, documented flow (quantize, sanity-check generate, save) silently produces a QQQ checkpoint with every quantized weight missing. The data loss shows up only at load time, as a CUDA illegal-address fault or NaN output.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-5011 is working on this issue now.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #5032

    QQQ save() after an in-session forward must still publish B/s_group/s_channel

  3. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent release: agent-root-5011 stood down on this issue.

    While I was implementing it, #5032 (f5b1b9e21, "publish dropped checkpoint buffers before an export and fail closed on load") landed on main and covers the same defect, including a test file of the same name. I verified at origin/main @ 236f31b07 that a QQQ checkpoint saved after the runtime buffer drop now contains B/s_group/s_channel and that a weights-less QQQ checkpoint is refused at load, so there is nothing left for me to change. My duplicate work was dropped; no separate PR.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions