Skip to content

fix(qlinear): size GPTQ buffers by pack_factor and skip Laguna g_proj quantization - #2983

Merged
Qubitium merged 1 commit into
mainfrom
upstream-laguna-gproj
Jul 26, 2026
Merged

Qubitium merged 1 commit into
mainfrom
upstream-laguna-gproj

Conversation

@Qubitium

@Qubitium Qubitium commented Jul 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes two related issues for Laguna S-2.1:

  1. GPTQQuantLinear._register_gptq_buffers (and the fallback PackableQuantLinear.pack) sized packed buffers with // pack_dtype_bits * bits. This only equals // pack_factor when the dimension is a multiple of pack_dtype_bits (32), so layers like self_attn.g_proj with out_features=48 or 72 got a qzeros buffer with too few columns and load_checkpoint failed with a shape mismatch.
  2. Laguna's per-head g_proj is tiny (0.28–0.42 MB dense) and not worth quantizing. Mark it as :! in LagunaQModel.module_tree so it is bypassed during dense → quantization.

What Changed

  • gptqmodel/nn_modules/qlinear/__init__.py:
    • _register_gptq_buffers now allocates qweight rows and qzeros columns with math.ceil(features / self.pack_factor).
    • PackableQuantLinear.pack uses the same pack_factor-based sizing for qweight/qzeros.
  • gptqmodel/models/definitions/laguna.py:
    • self_attn.g_proj changed from :0 to :! so simple_layer_modules excludes it from quantization.
  • tests/kernels/test_qlinear_hierarchy.py:
    • Added test_gptq_quant_linear_buffer_shapes_for_non_word_aligned_out_features covering out_features=48 and 72.

Tests

  • Added targeted unit test for the buffer-sizing regression.
  • Ran the test locally:
python -m pytest tests/kernels/test_qlinear_hierarchy.py::test_gptq_quant_linear_buffer_shapes_for_non_word_aligned_out_features -q
# 1 passed
  • ruff check gptqmodel/nn_modules/qlinear/__init__.py gptqmodel/models/definitions/laguna.py tests/kernels/test_qlinear_hierarchy.py --config format/ruff.toml passes.
  • git diff --check passes.

Notes

  • The g_proj:! change applies to the dense → quantization path. Existing checkpoints quantized with g_proj.qweight will treat it as unexpected unless the loader is taught to dequantize them, but the primary goal is to avoid quantizing these tiny layers going forward.

… quantization (#84)

* fix(qlinear): use pack_factor for qweight/qzeros buffer sizing and mark Laguna g_proj as non-quantizable

* test(qlinear): add buffer shape test for out_features=48/72
@Qubitium Qubitium self-assigned this Jul 26, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@Qubitium
Qubitium merged commit 079edcc into main Jul 26, 2026
6 checks passed
@Qubitium
Qubitium deleted the upstream-laguna-gproj branch July 26, 2026 19:59
Qubitium added a commit that referenced this pull request Nov 29, 2026
…6409)

* fix(hf): defer a legacy qzeros only on a real floor-width mismatch

`_hf_legacy_qzeros_quant_linear` wraps every symmetric checkpoint from a
pre-7.3.4 producer with `_HF_DEFER_SYM_QZEROS = True`. The deferral hides
`qzeros` from the loader's expected-key set, which is only needed when the
checkpoint stores the pre-#2983 floor width (`out_features // 32 * bits`).
The two widths are identical for a 32-aligned projection, but the buffer was
still hidden: the module kept its constructor (meta) `qzeros`, and
`_materialize_hf_sym_qzeros` only runs from `hf_gptqmodel_post_init`, which
Optimum calls *after* `accelerate.dispatch_model`. A GPU/disk placement then
failed with

    ValueError: qzeros is on the meta device, we need a `value` to put in on 0.

Measured on the published
`ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1`
(`meta.quantizer: gptqmodel:1.7.4`; all 196 `qzeros` already ceil-width) with
`max_memory={0: "3GiB", "cpu": "14GiB"}`: the load fails before this change
and loads with finite logits after it.

The constructor now clears the deferral for a 32-aligned layer, so the
checkpoint tensor is loaded normally; a genuine floor-width layer keeps the
deferral and is still regenerated after loading.

`tests/test_deferred_qzeros_dispatch_6396.py` pins both branches, including a
GPU-gated mixed-map load+forward on the real checkpoint. Two
`test_hf_bridge_cpu_offload_5022.py` fixtures that hand-build a legacy Marlin
layer use a floor-width shape (or re-apply the loader marker), because a
32-aligned layer no longer defers.

* test(hf): pin the matching-width offload path and bound the GPU node

A 32-aligned legacy checkpoint no longer defers `qzeros`, so the old
"legacy deferred qzeros on CPU offload still raises" node no longer describes
it: the refusal only guards a genuine floor-width buffer. Rename it to pin the
new outcome (the checkpoint loads), and keep the floor-width deferral covered
by the resident-CPU node in `test_deferred_qzeros_dispatch_6396.py`.

The GPU-gated real-checkpoint node keeps only the 3GiB map: a 1GiB cap
reselected all 196 layers to the portable kernel, whose optional inductor
compile makes the node a multi-minute run without adding coverage of the
dispatch failure (the 3GiB map still exercises it).

---------

Co-authored-by: agent <agent@local>
Qubitium added a commit that referenced this pull request Nov 29, 2026
* test(qzeros): pin the symmetric-qzeros state machine invariants

Six changes landed on the symmetric ``qzeros`` path in one day (#6388,
#6392, #6391, #6395, #6409 and the pending #6157 normalization). Each was
verified on its own; nothing pinned the combination, and a later edit can
satisfy one fix while breaking another.

The state machine has three markers (``_HF_LEGACY_QZEROS``,
``_HF_CHECKPOINT_SYM``, ``_HF_DEFER_SYM_QZEROS``) and two word encodings,
selected by ``_load_target_qzeros_format()``. The tests assert:

- a 32-aligned legacy projection never defers, and a real floor-width
  mismatch always does (#6409 / #6396);
- an asymmetric floor-width checkpoint is refused, never silently wrong;
- a deferred module materializes to the standard shape with a resolved
  layout that agrees with the one the kernel decodes;
- materialization is idempotent across 2/3/4/8-bit;
- the deferral marker and a materialized buffer are mutually exclusive;
- a float ``qzeros`` buffer (int4pack affine offsets) is never filled with
  a packed word -- skipped until the #6157 normalizer exists;
- a clean checkpoint's word survives the load, and every pre- and
  post-#2983 producer lands on the same decoded zero point.

``qzero_format()`` is deliberately not read as a decode answer before
``_qzeros_format_established``: every kernel is ``REQUIRES_FORMAT_V2``, so
the constructor placeholder ``1`` disagrees with the decoded ``2`` until a
load resolves it (#4377). That distinction is the whole reason the first
#6157 draft corrupted clean checkpoints.

Test-only. Verified 49 passed / 13 skipped on main (a4471c5) and
50 / 12 with #6419 applied; 68 passed / 17 skipped together with the four
existing qzeros test files, so there is no global-state pollution. Every
skip is a kernel-geometry or environment gate with a precise reason.

* test(qzeros): spell out the unreachable return after a pytest.skip

``_legacy_module_or_skip`` ended with a bare ``pytest.skip(...)`` call, which
reads as an implicit ``return None`` to static analysis and made CodeQL flag
the function for mixing explicit and implicit returns. ``pytest.skip``
raises, so the added return is unreachable; it exists only to make every
non-exceptional path explicit, and the comment says so.

Behaviour unchanged: 49 passed, 13 skipped, identical to before.

---------

Co-authored-by: agent <agent@local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant