Skip to content

fix(embed): refuse fresh endpoint quantize before calibration (#5111) - #5168

Merged
Qubitium merged 1 commit into
mainfrom
fix/5111-embed-quant-fresh-quantize
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5111-embed-quant-fresh-quantize

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5111. quantize(embed_quant_config=...) / embed_quant_mode=... is an
endpoint-requantize flow, but quantize() accepted it on an unquantized
LazyTurtle model and then either crashed at input capture or spent a full run
before failing to re-tie a tied lm_head. Both unsupported fresh paths are now
refused before calibration, with the model left dense and
model.quantized=False. requantize() on a loaded quantized checkpoint
(including a tied one) is unchanged.

Net: one runtime file (gptqmodel/models/base.py, +42/−5), one regression
test file, and a mechanism page plus its links. No checkpoint format, kernel,
or requantize path changes.

Root cause

quantize() only refused a model that was already quantized
(gptqmodel/models/base.py:2415 at e2f9676c6), and the tied-head guard in
ModuleLooper covered only quantize_config.lm_head
(gptqmodel/looper/module_looper.py:2056-2062), never embed_quant_config.
The endpoint flow assumes a loaded quantized endpoint; a fresh quantize is a
LazyTurtle shell.

Removed / replaced mechanism (before, at e2f9676c6)

Flow Before Evidence
Fresh INPUT/BOTH StageInputsCapture.cache_inputs skipped materializing the input embedding for INPUT/BOTH (correct for requantize, wrong for a fresh shell). The capture forward ran the embedding on meta; transformers' create_causal_mask called .item() on it. gptqmodel/looper/stage_inputs_capture.py:410-414; gptqmodel/models/base.py:3555 (run_input_capture)
Fresh OUTPUT/BOTH, tied head No tied check for embed_quant_config; every decoder layer was quantized, the tied lm_head became TorchLinear, and only the final restore failed while re-tying. gptqmodel/looper/module_looper.py:2056-2062 (lm_head-only check); gptqmodel/utils/offload.py:255-264

Measured on GPU 1 with Qwen/Qwen2.5-0.5B-Instruct (tied), bf16, 4 rows:

INPUT  -> RuntimeError: Tensor.item() cannot be called on meta tensors
          (raised in run_input_capture; model.quantized=False)
OUTPUT -> KeyError: "attribute 'weight' already exists"
          RuntimeError: Offload: base modules remain on the meta device after
          quantization (<tie_weights>). The in-session model cannot run inference;
          reload the saved checkpoint before using it.
          (model.quantized=True, lm_head=TorchLinear, tied=False)

New flow

flowchart TD
    A["BaseQModel.quantize(calibration, *, embed_quant_config / embed_quant_mode)"] --> N["_normalize_embed_quant_config"]
    N --> Q{"self.quantized?"}
    Q -- "yes: requantize on a loaded checkpoint" --> R["existing endpoint requantize flow"]
    Q -- "no: fresh quantize" --> T{"mode is OUTPUT or BOTH<br/>and lm_head is tied?"}
    T -- "yes" --> X2["NotImplementedError<br/>tied output head"]
    T -- "no" --> I{"mode is INPUT or BOTH?"}
    I -- "yes" --> X1["NotImplementedError<br/>input endpoint is requantize-only"]
    I -- "no: OUTPUT, untied" --> OK["capture, pack, finalize"]
    style X1 fill:#ffe8e8,stroke:#cc3333
    style X2 fill:#ffe8e8,stroke:#cc3333
    style OK fill:#d7f0d7,stroke:#2f7d32
Loading

The guards are at gptqmodel/models/base.py:2386-2418, before fuse, backend
selection, dataset preparation, rotation, or any looper work. OUTPUT on an
untied head keeps its existing working path.

Failure paths

stateDiagram-v2
    state "fresh model, dense" as Dense
    state "refused before calibration" as Refused
    state "quantizing every layer" as Running
    state "half-quantized, quantized=True" as Half
    state "requantize on loaded checkpoint" as Requant

    Dense --> Refused: INPUT or BOTH
    Dense --> Refused: OUTPUT or BOTH with a tied head
    Dense --> Running: OUTPUT with an untied head
    Running --> Half: tied re-tie fails (old behavior)
    Refused --> Dense: model untouched
    Requant --> Requant: allowed
Loading

Why refuse rather than materialize the capture embedding

Materializing the input embedding for capture was implemented and measured
first. It fixed the meta .item() crash, but the packed endpoint stores FP16
scales and emits FP16 while the dense decoder remains BF16, so the completed
model could not forward:

RuntimeError: mat1 and mat2 must have the same dtype, but got Float and BFloat16

The endpoint-only result leaves a dense decoder behind a packed endpoint;
making that consistent needs an endpoint output-dtype contract, not a one-line
materialization. The flow is therefore refused up front instead of leaving a
non-forwardable in-session model.

Changes

File Change
gptqmodel/models/base.py Normalize embed_quant_config before any mutation and refuse fresh INPUT/BOTH and tied OUTPUT/BOTH (lines 2382-2418). OUTPUT on an untied head and all requantize() calls are unchanged.
tests/test_fresh_embed_quant_5111.py New regression file: refusal before calibration for INPUT/BOTH and tied OUTPUT/BOTH (no quantized module, quantized=False, capture never called); untied OUTPUT control; tied-checkpoint requantize(BOTH) control.
docs/embed-endpoint-quantize.md Mechanism page with the same two diagrams, before/after table, failure paths, and tests.
docs/quantization-features.md, docs/calibration-config.md, docs/README.md, README.md Link and state the requantize-only boundary.

Validation

Environment: /root/venv/bin/python (torch 2.15.0+cu132, transformers 5.22.1), CUDA_VISIBLE_DEVICES=1, PYTHONPATH=/root/work/issue-5111, HF_HUB_OFFLINE=1.

Command Result
pytest tests/test_fresh_embed_quant_5111.py -q -ra 6 passed in 6.76s
pytest tests/test_fresh_embed_quant_5111.py tests/test_embed_quant_api.py tests/test_requant_embed_lm_head.py tests/test_weight_only_looper.py tests/test_calibration_config.py tests/test_checkpoint_method_compatibility.py tests/test_prepare_dataset.py -q -ra 147 passed, 95 skipped (opt-in checkpoint_recovery marker)
pytest tests/test_stage_inputs_capture.py tests/test_stage_modules.py -q -ra 128 passed
pytest on the 18 other stage_inputs_capture/run_input_capture files 258 passed, 4 skipped (needs 2 CUDA devices), 1 pre-existing error (test_locateanything_prepare_dataset_uses_remote_processor, reproduced on e2f9676c6)
uvx [email protected] check gptqmodel docs/eora tests setup.py All checks passed

Red evidence: on e2f9676c6 the new file gives 4 failed, 2 passed — the
refusal tests reach calibration (or finish the tied run) instead of refusing,
while the two controls pass. format/check.sh cannot run its pip install
step in this externally-managed environment; the same pinned ruff version was
run directly.

Reviewer notes

  • The fresh INPUT/BOTH refusal is a deliberate behavior change: that flow
    never completed successfully, and materializing it alone produced a
    non-forwardable model (evidence above).
  • OUTPUT on an untied fresh model remains supported and is covered by a
    passing forward control.
  • Unverified: the real /monster/data/model/Qwen2.5-0.5B-Instruct-gptq-4bit
    end-to-end requantize accuracy test (tests/test_requant_embed_lm_head_e2e.py)
    is unavailable on this host; the tied-checkpoint requantize(BOTH) regression
    covers the same mechanism on a tiny model.
  • tests/test_locateanything_support.py::test_locateanything_prepare_dataset_uses_remote_processor
    errors on the unmodified base revision too; unrelated to this change.

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