Skip to content

fix(qqq): replace the CPU checkpoint layout with the packed cache (#4805) - #4856

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4805
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4805

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4805.

Change

QQQTorchLinear's first CPU forward builds an INT4 cache for the AVX512 qqq_int4 kernel while the checkpoint B/s_group/s_channel stayed resident, so the packed weight was stored twice and the group scales were widened to FP32.

  • The cache now rebuilds the checkpoint tensors bit for bit, verifies shape/dtype/values, and only then replaces the checkpoint layout (_torch_dropped_checkpoint). Any mismatch keeps both layouts (fail closed).
  • state_dict(), save, load and _apply/.to() rebuild the checkpoint tensors on demand; the dequantized fallback rebuilds them transiently.
  • The cache keeps the checkpoint scale dtype; the kernel call widens group scales to FP32, so an FP32 cache no longer doubles scale residency.

Tests

  • new tests/test_qqq_cpu_residency_4805.py: 9 passed (cache replaces layout, state_dict bit-identical, load/move restore, fallback rebuild, fail-closed inverse)
  • tests/test_qqq_cpu.py + test_qqq_kernel_memory_4387.py: all pass; test_qqq_jit.py 2 failures are the pre-existing native JIT build failure on this box (sm_121 build of qqq_gemm.cu), identical on parent
  • ruff check gptqmodel tests: clean

Residual risks

  • A mutation of the dropped buffers must materialize them first (covered by the updated mutation tests).
  • The AVX512 kernel is x86-only; on this ARM host the residency tests use a deterministic stand-in kernel. The native kernel path is unchanged.

)

QQQTorchLinear's first CPU forward built the AVX512 INT4 cache while the
checkpoint B/s_group/s_channel stayed resident, so the packed weight was
stored twice and the group scales were widened to FP32. The cache now
rebuilds the checkpoint tensors bit for bit, verifies them, and only then
replaces the checkpoint layout; state_dict/save/load/move rebuild them on
demand. Any verification mismatch keeps both layouts.
@Qubitium
Qubitium merged commit 57a6055 into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4805 branch November 17, 2026 17:29
Qubitium added a commit that referenced this pull request Nov 17, 2026
…tate (#4940, #4942) (#4951)

* fix(qqq): make the class-swapped CPU kernel independent of __init__ state (#4940, #4942)

Two regressions from #4856:

* `rehome_quantized_kernel` reinterprets a packed `QQQLinear` as
  `QQQTorchLinear` by assigning `__class__`, which never runs the target's
  `__init__`. The runtime attributes #4856 added
  (`_torch_packed_cache`, `_torch_packed_cache_key`, `_torch_dropped_checkpoint`)
  therefore did not exist, and the following `clear_weight_cache()` raised
  `AttributeError: 'QQQTorchLinear' object has no attribute
  '_torch_dropped_checkpoint'` at the end of every QQQ `quantize()` (#4940).
  Install that state through a `_init_runtime_state` hook the swap calls, keep
  class-level defaults for the reads, and create the `_int_mm` failure memo on
  demand instead of sharing a class-level `set`.

* The cold path of `_packed_weight_for_cpu` read `B`/`s_channel`/`s_group`
  without `_cpu_transform_lock`, so a second thread that passed the cache check
  while the first was still building could read the buffers after
  `_maybe_drop_checkpoint_tensors` had dropped them (#4942). The whole build now
  runs under the lock, like the other CPU int4 kernels' `_ensure_cpu_layout`.

* test(qqq): pass the stand-in kernel class instead of a wrapping lambda

Addresses the Code Quality review notes: `_FakePackedKernel` is already the
callable `_qqq_cpu_kernel()` needs, so the lambda wrapper is unnecessary.
Qubitium added a commit that referenced this pull request Nov 17, 2026
…l closed on load (#5010) (#5032)

Since #4856 the first AVX512 CPU forward of `QQQTorchLinear` drops the
checkpoint `B`/`s_group`/`s_channel` buffers once the packed cache can rebuild
them. `GPTQModel.save()` does not call `state_dict()`; it walks
`named_buffers(recurse=False)` and PyTorch skips a `None` buffer, so the
ordinary quantize -> generate -> save flow wrote a checkpoint with no
quantized weights at all (54 keys -> 12 on a 2-layer model). The reload then
crashed the CUDA context with an illegal memory access, or returned all-NaN
logits on `QQQ_TORCH`.

Two changes:

1. `BaseQuantLinear.materialize_checkpoint_layout_for_export()` is a new
   no-op hook, overridden by `QQQLinear` to restore the dropped buffers from
   the cache. `_restore_checkpoint_layout_for_export()` -- which
   `get_state_dict_for_save()` already runs -- calls it, so the export
   manifest sees the tensors. The packed cache is kept, so the next forward
   neither rebuilds nor changes output.

2. `validate_checkpoint_qweights()` now takes the required payload from the
   kernel instead of a format allow-list: `BaseQuantLinear.CHECKPOINT_REQUIRED_BUFFERS`
   is `("qweight",)` for the GPTQ/AWQ/Marlin/BitBLAS family (inherited from
   `PackedGroupedQuantLinear`) and `("B", "s_group", "s_channel")` for QQQ.
   A checkpoint missing any of them raises instead of loading uninitialized
   buffers. Every other format keeps its previous skip.

Fixes #5010
sLaffey pushed a commit to sLaffey/GPTQModel that referenced this pull request Nov 18, 2026
…lCloud#4978)

Seven tests went red because their doubles/oracles predate library changes
that behave as intended. This is test-only; no product code changes.

- test_fusion_kernel_backends: gc.collect() before the weakref check; ModelCloud#4871
  leaves transient Dynamo trace garbage that keeps the pre-fusion weight
  alive. The refs are still asserted dead afterwards.
- test_failed_run_fence_4595: del qmodel + gc.collect() so ModelCloud#4829's finalizer
  runs before the no-leak assertion; a late finalizer must not recreate the
  released run dir.
- test_exl3_projection_resume: add _release_deferred_hessian_sources() to the
  _Capture double (ModelCloud#4837).
- test_kernel_audit_regressions: stub _needs_torch_fallback=lambda: False on
  the SimpleNamespace self (ModelCloud#4835).
- test_moe_resave_preserves_experts_4589: accept the earlier RuntimeError
  "does not provide tensors" refusal (ModelCloud#4810); still assert no safetensors.
- test_positional_mask_capture_4660: build the native oracle with the same
  mask-derived position_ids the capture uses (ModelCloud#4930).
- test_qqq_kernel_memory_4387: snapshot the oracle weight/s_channel before
  the first forward, which may drop the checkpoint layout (ModelCloud#4856).
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