Skip to content

[BUG] Follow-up on #4103/#4110: HF save_pretrained on ExLlamaV2/Marlin no longer refuses and writes runtime layout (reload = garbage, regression), AWQ CPU kernels still serialize int4pack, v1<->v2 copy lost via safetensors, per-document mask never built #4125

Closed

Description

@Qubitium

Audit of main @ ffa0502, RTX 4090 and CPU. Checks the fixes for #4091 (#4101, #4103, #4110) and #4095 (#4096, #4104). Before/after comparisons use git archive trees of 0ff46b6^ and 1a3943e^.

1. Regression from #4103: HF save_pretrained() on ExLlamaV2 / Marlin / AWQ-Marlin / AWQ-ExLlamaV2 no longer raises; it writes the runtime layout, and ExLlamaV2 files reload as garbage with no error (high)

The refusals in qlinear/exllamav2.py:193, marlin.py:484, marlin_awq.py:363 and exllamav2_awq.py:157 now fire only inside checkpoint_serialization(), which only models/writer.py:744 enters. model.save_pretrained() (transformers) and torch.save(model.state_dict()) therefore serialize shuffled/repacked tensors under an ordinary GPTQ/AWQ config. docs/checkpoint-interop.md:451 still promises "save_pretrained() on a Marlin/ExLlamaV2 model → RuntimeError". AUTO on CUDA selects these kernels by default: ExLlamaV2 for asym GPTQ, Marlin for sym GPTQ and AWQ.

Llama-3.2-1B-Instruct GPTQ 4-bit g128 asym, default load:
  HEAD:    kernel ['ExllamaV2Linear']; before ' Paris.\nThe capital of France is Paris'
           model.model.save_pretrained(...): OK (no error)
           reloaded: ' flags flags flags flags flags flags flags flags'
  pre-4103: save_pretrained raised RuntimeError: ExLlamaV2 shuffles `qweight` ...
Marlin (GPTQ-4bits-gp128):   save OK -> reload ValueError: Trying to set a tensor of shape [0] in "g_idx"
AWQ-Marlin:                  save OK -> reload qweight shape error   (pre-4103: raised at save)
state_dict() of ExLlamaV2 model -> fresh TORCH model: max logit diff 24.6-33.7 (v1, v2, asym, desc_act, asym-v2)

Fix: keep refusing by default, with an explicit read-only scope for PEFT, or have GPTQModel.load run the HF model's save_pretrained inside checkpoint_serialization(). At minimum, record the runtime layout in the state-dict metadata so a reload fails loudly. tests/test_checkpoint_state_invariant.py:327-346 only asserts the raise inside an explicit scope; add a test that calls model.save_pretrained().

2. #4103 incomplete: AWQ CPU kernels (CPU AUTO default for AWQ) still serialize int4pack runtime tensors (medium)

Only GPTQQuantLinear._save_to_state_dict (qlinear/__init__.py:1346) emits the snapshot. AWQuantLinear (:2304) has no hook. torch_fused_awq.py:395, torch_aten_kernel_awq.py:282 and torch_cpu_avx512_awq.py:279 capture _fused_train_state, but nothing writes it out, although checkpoint-interop.md:96 says these kernels "emit the captured checkpoint snapshot".

CPU, Llama-3.2-1B-Instruct-AWQ, AUTO -> ['TorchCpuAvx512AwqLinear'], after an eval forward:
  save_pretrained: OK (no error)
  reload: ValueError Trying to set a tensor of shape torch.Size([2048, 4096]) in "qweight" (which has shape torch.Size([8192, 256]))
same for AWQ_TORCH_FUSED and AWQ_TORCH_ATEN (pre-4103 identical -> incomplete fix, not a regression)

The AWQ cases in test_checkpoint_state_invariant.py never reach the transform: the modules stay training=True and qweight stays (128, 8). Fix: move _write_fused_train_state_snapshot up to PackedGroupedQuantLinear (or add it to AWQuantLinear).

3. #4103 incomplete (#4091 item 3): the v1↔v2 copy is exact only while state_dict._metadata survives (low-medium)

The qzeros layout now lives only in non-tensor metadata (_write_state_dict_layout_metadata / _incoming_qzeros_format). A safetensors round trip (Trainer/accelerate checkpoints) or any {k: v.to(dev) for k, v in sd.items()} drops it:

A = Llama-1B GPTQ v1, B = HF-GPTQV2 checkpoint, TORCH kernel, max logit diff after A.load_state_dict(B's sd):
  torch.save path 0.0 | safetensors round trip 25.646 | dict copy 25.646 | B<-A safetensors 26.656   (same as the pre-fix numbers in #4091)

Fix: store the layout in a tensor, either a persistent qzeros_format int buffer or a tensor-encoded _extra_state.

4. #4110 incomplete: the per-document mask is never built for real padded bs>1 SDPA bool masks (Qwen2, Llama) (low; no measured PPL loss)

HF bool masks keep pad keys masked, but pad query rows still see real keys. So allowed_grid.any(-1) (awq_processor.py:1372-1373, :1387) counts all B·S rows, the != row_count check fails, and the code always takes the fallback at :1458: one causal sequence across the whole batch.

right-padded mask (2,1,8,8) bool: real rows 11, B*S 16; a pad query row sees keys [0, 1, 2]; structure: None
bs=4 calibration: structure recovered in 0/56 calls on Qwen2.5-0.5B and Llama-3.2-1B (keep_sum 1124 vs rows 993); Gemma-3: 56/56

Fix: derive the keep mask from the diagonal, since a real token always attends to itself. With that fix PPL doesn't improve (Qwen bs4 24.21 → 24.62, Llama 21.80 → 21.89), so the cost is the dead code path and an inaccurate claim in the PR, not accuracy. tests/test_awq_weight_mean.py::test_awq_token_rows_replay_mask_stays_inside_each_document uses a hand-built mask with fully masked pad rows, unlike real HF masks.

Plausible, not verified

  • fix(awq): replay packed calibration rows with their own mask and positions (#4091) #4110 positions are dead code for HF attention. _sanitize_kwargs (awq_processor.py:3684) drops position_ids (Qwen2/Llama/Gemma3 attention doesn't take it), and _prepare_module_forward:3269-3272 rebuilds RoPE from arange(packed_len). Restoring the positions gives mixed PPL results (gemma bs1 45.39 → 43.03, bs4 45.59 → 47.03; qwen bs1 24.03 → 24.50).
  • MACaQ Triton vs Torch: max logit diff 5.84 on the same Qwen-0.5B checkpoint (top-1 identical).
  • MLX pad_token_id fallback: it strips leading and trailing pad ids, which can remove a BOS when bos == pad (static review).

Checked, clean

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 15, 2026

    @Qubitium
    CollaboratorAuthor

    All four items are now on main:

    Item 1 was the serious one: #4103's refusals had been scoped to checkpoint_serialization(), which only models/writer.py enters, so a plain model.save_pretrained() wrote shuffled tensors and the reload silently produced ' flags flags flags flags flags flags flags flags' instead of raising. The reason it shipped: the regression test only asserted the raise inside an explicit scope and never called model.save_pretrained(). That gap is now covered directly.

    Item 4's root cause is worth recording because it was a wrong-shape check rather than a wrong idea: HF bool masks keep pad keys masked but pad query rows still see real keys, so allowed_grid.any(-1) counted all B·S rows, the row-count check never matched, and the code silently fell back to one causal sequence across the whole batch. The keep-set now comes from the grid diagonal. Measured side effect: AWQ batch_size=4 PPL on Qwen2.5-0.5B moved 25.52 → 25.13 (bs=1 unchanged at 22.09, RTN 22.77).

    Closing as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions