Skip to content

fix(weight_loader): write counterless tensors in file-backed offload (RTN quantize crash, #6539) - #6564

Merged
Qubitium merged 1 commit into
mainfrom
fix/filebacked-counterless-tensors
Dec 1, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/filebacked-counterless-tensors

Conversation

@Qubitium

@Qubitium Qubitium commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Fixes #6539

Problem

Weight-only finalize (RTN, and other direct-pack methods) passes file_back_module tensors that were created under inference_mode and later rebound with param.data = ... (the #4572 state: is_inference() == False, no version counter). The packed module's bias aliases such a source tensor. The file-backed writer added in #6473 serializes through reshape(-1).view(torch.uint8), and any view of a counterless tensor raises RuntimeError: Inference tensors do not track version counter. So quantize() with the default offload_to_disk=True crashed for every model with biased linears (verified: opt-125m, Qwen2.5-0.5B-Instruct). Llama (no linear bias) was unaffected. Before #6473 (0043349bf^), the same script quantized fine.

Fix

gptqmodel/weight_loader/filebacked.py: _viewable() copies a tensor once when it has no version counter, before the writer takes its byte view. This covers both the normal path (_host_bytes) and the raw-U8 path (dtypes without a safetensors name). Tensors that already have a counter take the existing path unchanged, with no extra copy. file_back_module already rebinds every tensor to a fresh file-backed view, so the in-session model ends up with normal, counter-tracking tensors.

Tests

tests/test_weight_loader_filebacked.py::test_counterless_tensors_are_written_and_rebound[False|True] builds the exact producer state: a Parameter created under inference_mode, then .data = cpu_tensor. The test asserts that the counter is really missing, and covers both an fp16 bias and a raw-U8 float8_fnuz tensor. It file-backs the module from a normal caller and from an inference_mode caller. It then checks the values in memory and on disk, and that an in-place update works afterwards.

origin/main c649fcb this PR
new regression (2 cases) 2 failed: RuntimeError: Inference tensors do not track version counter at filebacked.py:104 2 passed
tests/test_weight_loader_filebacked.py n/a 13 passed, 1 skipped (CUDA-only case, run CPU-only)
46 offload / filebacked / weight_loader / weight-only / RTN test files (tests/test_*{offload,filebacked,weight_only,weight_loader,rtn,insession}*) n/a 600 passed, 5 skipped
ruff gate (uvx [email protected] check ...) n/a All checks passed

End-to-end (GPTQModel.load → RTNConfig(bits=4, group_size=128) → quantize() → save → GPTQModel.load(backend=TORCH) → generate → wikitext2 PPL, ctx 2048, RTX 4090):

model origin/main this PR fp16 PPL save/reload logits
opt-125m crash in quantize() PPL 31.88 27.57 bit-identical
Qwen2.5-0.5B-Instruct crash in quantize() PPL 17.80 13.24 bit-identical
pre-#6473 tree (0043349bf^), opt-125m quantizes OK n/a n/a n/a

No control flow change, so there is no diagram.

🤖 Generated with Claude Code

…6539)

Weight-only finalize (RTN/SINQ/GGUF ...) hands file_back_module tensors that
were created under inference_mode and rebound with param.data = ... (the #4572
state: not inference, but no version counter). The #6473 writer serialized them
through reshape/view, which raises 'Inference tensors do not track version
counter', so quantize() crashed with the default offload_to_disk=True.
Copy such a tensor once before taking its byte view.

Fixes #6539

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium marked this pull request as ready for review December 1, 2026 02:51
@Qubitium
Qubitium merged commit 297869c into main Dec 1, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/filebacked-counterless-tensors branch December 1, 2026 02:52
@Qubitium

Qubitium commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

Release-sweep note: this PR does not cover a second consumer of the same counterless weight.

RTNConfig(..., format=FORMAT.GEMM) with the default offload_to_disk=True still fails on this PR's head, earlier than the writer, in the AWQ packer:

cfg = RTNConfig(bits=4, group_size=128, sym=False, format=FORMAT.GEMM, device="cuda:0")  # offload_to_disk default True
m = GPTQModel.load("Qwen/Qwen2.5-0.5B-Instruct", quantize_config=cfg, dtype=torch.float16)
m.quantize(CalibrationConfig(dataset=[...], batch_size=1))
# weight_only_looper.py:506 _finalize_quantized_module -> weight_only_processor.py:389 submodule_finalize
# -> utils/model.py:1692 pack_module -> nn_modules/qlinear/torch_awq.py:189 pack: weight.t()
# RuntimeError: Inference tensors do not track version counter.
  • At pack_module entry for the first module (model.layers.0.self_attn.q_proj), layer.weight is a CPU Parameter with is_inference() == False, and layer.weight.detach().t() already raises. The Parameter has been rebound to a counterless tensor before pack.
  • Same config with offload_to_disk=False: quantize, save, reload and generate all succeed.
  • Same result on origin/main c649fcb and on this PR head.
  • End-to-end: tests/test_awq_variable_length_ppl.py::test_awq_beats_rtn_with_variable_length_calibration (RTN baseline with format=GEMM) fails on both.

Suggestion: fix the producer that rebinds the weight with an inference-mode tensor on the offload path, so every consumer (writer, GPTQ/AWQ packers, later forwards) gets a normal tensor, rather than patching each consumer.

Qubitium added a commit that referenced this pull request Dec 1, 2026
#6595) (#6596)

The lazy source materializes a layer under inference_mode and the weight-only
CPU move rebinds it with .data =, leaving parameters that are not inference
tensors yet have no version counter (#4572). The AWQ packer (weight.t()) then
raised for RTNConfig(format=GEMM) with the default offload_to_disk=True. #6564
only hardened the file-backed writer; fix the producer state once in
WeightOnlyLooper._finalize_quantized_module with replace_inference_parameters.

Fixes #6595

Co-authored-by: Claude Opus 5.5 <[email protected]>
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