Repository navigation
fix(qqq): make the class-swapped CPU kernel independent of __init__ state (#4940, #4942) - #4951
Merged
Merged
Conversation
…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.
This was referenced Nov 17, 2026
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 #4940 and #4942 — two regressions from #4856.
#4940 — every QQQ
quantize()crashed at the endrehome_quantized_kernelreinterprets a packedQQQLinearasQQQTorchLinearby 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 followingclear_weight_cache()raisedQQQTorchLineargains a_init_runtime_state(module)hook plus class-level defaults, and_switch_quant_linear_classcalls the hook after the swap — so the next attribute added to the kernel cannot repeat [BUG] Regression from #4381 fix: QQQquantize(offload_to_disk=False)crashes at loop end withAttributeError: 'QQQTorchLinear' object has no attribute '_torch_int_mm_failed_shapes', so nothing can be saved #4451/fix(qqq): replace the CPU checkpoint layout with the packed cache (#4805) #4856.__init__delegates to the same hook;clear_weight_cache/_materialize_dropped_checkpoint/_maybe_drop_checkpoint_tensors/_packed_cpu_weight_deviceread throughgetattrwith the class default.torch._int_mmfailure memo is created on demand (_failed_int_mm_shapes()): a class-levelsetwould have shared one module's failures with every other module.#4942 — concurrent cold CPU forwards raced the checkpoint drop
The cold path of
_packed_weight_for_cpureadB/s_channel/s_groupwithout_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_tensorshad 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_classswap installs state,clear_weight_cache()+post_init()+forward survive both the hooked and the bare class swap, and the_int_mmmemo 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.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 istest_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 selectsQQQLinear.QQQConfig(device="cpu"), 8 rows):quantize()-> forward ->save()all succeed; logits finite.ruff check gptqmodel tests: clean.Residual risk
_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.