Skip to content

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

Merged
Qubitium merged 3 commits into
mainfrom
fix/qqq-runtime-state-4940
Nov 17, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/qqq-runtime-state-4940

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4940 and #4942 — two regressions from #4856.

#4940 — every QQQ quantize() crashed at the end

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'

#4942 — concurrent cold CPU forwards raced the checkpoint drop

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 (AttributeError: 'NoneType' object has no attribute 'device'). The whole build now runs under the lock, like the other CPU int4 kernels' _ensure_cpu_layout.

Tests

  • tests/test_qqq_runtime_state_4940.py (new, 4 tests): the real _switch_quant_linear_class swap installs state, clear_weight_cache()+post_init()+forward survive both the hooked and the bare class swap, and the _int_mm memo is per-instance. Red on parent: all 4 fail (AttributeError).
  • tests/test_qqq_cpu_cold_build_concurrency_4942.py (new, 2 tests): 3-4 barrier-synchronized threads, one cold forward each, with the window between the cache check and the buffer reads widened. Red on parent: both fail; green here, and every thread's output matches the serial reference.
  • Combined: new tests + test_qqq_cpu.py + test_qqq_cpu_residency_4805.py + test_qqq_kernel_memory_4387.py + test_quantize_in_session_4381.py -> 37 passed, 46 skipped, 1 failed. The failure is test_qqq_bf16_in_memory.py::test_qqq_bf16_in_memory_model_uses_the_torch_kernel, which fails identically on pristine main: the QQQ native JIT cannot build on this host, so the model selects QQQLinear.
  • End-to-end smoke (tiny 2-layer Llama, QQQConfig(device="cpu"), 8 rows): quantize() -> forward -> save() all succeed; logits finite.
  • ruff check gptqmodel tests: clean.

Residual risk

  • The e2e smoke does not reproduce the original crash on this host (CPU placement never enters the swap), so the swap itself is covered by the unit tests, which call the exact function from the traceback.
  • The lock is process-wide per class (_cpu_transform_lock); two threads building different QQQ modules now serialize their first build. That matches the other CPU int4 kernels' behaviour and the build is milliseconds.

…tate (#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`.
Addresses the Code Quality review notes: `_FakePackedKernel` is already the
callable `_qqq_cpu_kernel()` needs, so the lambda wrapper is unnecessary.
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