Skip to content

fix(looper): rehome finished layers' kernels after a failed quantize (#4947) - #4979

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4947
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4947

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4947 — the follow-up to my #4893.

Root cause

Two parts:

  1. The success path calls rehome_quantized_kernel() after the loop, but the failure/cancel branch of ModuleLooper.loop() (and WeightOnlyLooper) only restored/materialized modules. Finished layers kept the calibration-time kernel (AwqGEMMLinear, QQQLinear, GGUF Triton) while their buffers sat on CPU/offload, so the next forward raised NotImplementedError ... with arguments from the 'CPU' backend (AWQ) or the missing-kernel error (QQQ).
  2. Found while fixing: for disk-offloaded modules the accelerate hook captures the old class's bound forward in _old_forward, so a __class__ swap alone keeps executing the accelerator-only kernel. The swap now rebinds that captured forward.

Chosen strategy

Rehome the finished layers rather than restoring them to dense: the packed buffers are valid and layout-compatible with the portable Torch kernel, so rehoming keeps the completed work and leaves the model usable, while preserving #4893's materialization and #4829/#4807's offload-root behaviour. Finished layers of an offloaded run are meta shells, so rehome_failed_run_kernels() resolves the device from a materialized packed layer, else from the rest of the model, and passes it through a new device= override of rehome_quantized_kernel. Rehome errors (e.g. the save-and-reload guidance) are logged and swallowed so the original exception always propagates. Called from both loopers right after materialization.

Evidence (CUDA host, tiny 4-layer Llama, offload_to_disk=True, cancel at layer 2)

  • parent: layers 0-1 AwqGEMMLinear -> forward NotImplementedError ... gemm_forward; QQQ layers QQQLinear -> forward fails
  • fixed: AwqTorchLinear/QQQTorchLinear, qlinear_kernel updated, forward (1, 4, 32) finite; also verified with offload_to_disk=False (14 modules rehomed, qweight CPU, forward OK)

Tests

New tests/test_cancelled_quantize_kernel_rehome_4947.py (8 tests): red on parent 6 failed / 2 passed (the 4 test_failed_run_rehomes_finished_layers cases and both test_cancel_at_last_layer...); green here 8/8. The 2 resume tests pass on parent too — they are guards, not red tests.

  • new + test_cancelled_quantize_default_offload_4860 + ..._disk_offload_4807 + ..._layer_cleanup_4546 + test_quantize_in_session_4381 + test_qqq_runtime_state_4940: 27 passed, exit 0
  • AWQ subset (gemv_insession_4554, hf_cpu, gptq_lifecycle, modules_to_not_convert_lifecycle_4309, pack_vectorized, kernels/test_awq_torch): 78 passed, exit 0
  • ruff check gptqmodel tests: clean

Residual risk

  • If no layout-compatible portable kernel exists, rehome_quantized_kernel raises its save-and-reload RuntimeError; the failure path logs it and leaves the model mixed. Restoring to dense instead would discard finished layers, so this mirrors the success path's documented limitation.
  • Dynamic per-module formats whose class is not model.qlinear_kernel are skipped by the existing "only reinterpret kernels this run produced" rule and may stay mixed.
  • No partial-layer resume semantics: the in-flight layer is still restored to dense ([BUG] Cancelled or failed quantize() leaves the in-flight layer on the GPU: next forward raises a device mismatch and the layer's VRAM stays pinned (#4515 incomplete off-CPU) #4546) and a retry re-quantizes from scratch.
  • WeightOnlyLooper (GGUF) rehome is best-effort and not exercised here (no GGUF fixture).
  • The QQQ native extension cannot build in this environment (CUDA 13.5 compiler vs 13.2 headers), so the QQQ test forces the CUDA-only class selection via monkeypatch: the portable path is exercised, the native kernel is not. Tests skip on CPU-only hosts.
  • Only accelerate-style offload hooks (_old_forward) are handled; a custom hook without that attribute keeps the old forward.

…4947)

#4893 materialized the layers a cancelled run never reached, and #4829/#4807
kept the offload root of the layers it did finish, but the failure branch never
rehomed the finished layers' kernels. A run packed with an accelerator-only
kernel (AwqGEMMLinear, QQQLinear, GGUF Triton) therefore left CUDA-only modules
behind a CPU model and the next in-session forward raised

  NotImplementedError: Could not run 'gptqmodel_awq::gemm_forward_fp32_reduce'
  with arguments from the 'CPU' backend

The success path rehomes them in rehome_quantized_kernel; the failure path now
does the same, best effort:

- rehome_failed_run_kernels() (loop_processor) resolves the device from a
  materialized packed layer, else from the rest of the model (finished layers
  of a disk-offloaded run are meta shells), and calls
  rehome_quantized_kernel(model, device=...) so the probe can be overridden.
  Rehome errors such as the save-and-reload guidance are logged and swallowed
  so the original cancellation still propagates.
- ModuleLooper and WeightOnlyLooper call it right after the #4893
  materialization, so the #4893/#4829/#4807 behaviour is unchanged.
- _switch_quant_linear_class rebinds the accelerate offload hook's captured
  bound ``forward`` (_old_forward) to the target kernel; otherwise the hook
  kept calling the accelerator-only class even after the swap.

Adds tests/test_cancelled_quantize_kernel_rehome_4947.py: cancelled and
RuntimeError-failed AWQ and QQQ runs, a cancel at the last layer, and a
checkpoint resume. Each asserts the finished layers use the portable kernel,
model.qlinear_kernel is updated, and an in-session forward is finite. The QQQ
case forces the CUDA-only class when the native extension cannot build. On the
parent all kernel/forward cases fail with AwqGEMMLinear/QQQLinear left behind.
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