Skip to content

fix(hf): fall back to the auto-padding kernel when the bridge has no layer shape (#6326) - #6327

Merged
Qubitium merged 1 commit into
mainfrom
fix/hf-shape-blind-fallback-6326
Nov 29, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/hf-shape-blind-fallback-6326

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #6326.

Problem

#6323 made hf_select_quant_linear{,_v2} raise ValueError whenever in_features is omitted and the AUTO-selected kernel is a PackedGroupedQuantLinear without auto-padding. Transformers/Optimum always call the bridge before any layer exists and never pass in_features, so the guard refuses every GPTQ checkpoint — including group-aligned models whose widths are valid — and AutoModelForCausalLM.from_pretrained fails on CPU (TorchCpuAvx512Linear) and CUDA (MarlinLinear).

The repo's own tests/test_checkpoint_compat.py::test_gptq_block_name_loads_through_plain_hf is red on current main and green at 89dbcf91.

Fix

Resolve the missing shape in the safe direction instead of refusing:

  • An explicit backend keeps the old refusal contract (the caller chose that kernel; its per-layer validate() is the authority).
  • An AUTO selection with no in_features now falls back to BACKEND.GPTQ_TORCH (TorchLinear), which supports auto-padding and every device family. It builds both a group-aligned layer (unchanged codes) and a partial-group layer (padded), so the plain-HF path works for all widths.

This preserves #6323's intent (never hand a partial-group width to Marlin) while restoring the interop path.

Tests

cd tests && CUDA_VISIBLE_DEVICES="" PYTHONPATH=<clone>:<clone>/tests /root/venv/bin/python -m pytest \
  test_hf_bridge_missing_shape_6149.py test_checkpoint_compat.py -q -rs -p no:cacheprovider
# 34 passed, 3 skipped

Updated test_hf_bridge_missing_shape_6149.py to assert the auto-padding fallback (the two tests that asserted pytest.raises(ValueError) now assert TorchLinear).

Manual red→green:

# tiny GPTQ checkpoint, hidden 64 / group 16 (group-aligned)
from transformers import AutoModelForCausalLM
AutoModelForCausalLM.from_pretrained("/tmp/tiny_gptq")
# main 29f58fb1: ValueError hf_select_quant_linear_v2() cannot validate ...
# 89dbcf91 and this branch: loads, projections carry qweight

…layer shape

Fixes #6326.

#6323 refused every shape-blind grouped-kernel selection, which also
rejected every group-aligned GPTQ checkpoint and broke
AutoModelForCausalLM.from_pretrained. Keep the refusal for an explicit
backend, but for an AUTO selection with no in_features return the
portable Torch kernel, which pads a partial group and builds every
width.
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 08:32
@Qubitium
Qubitium merged commit f6ae416 into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/hf-shape-blind-fallback-6326 branch November 29, 2026 08:32
Qubitium added a commit that referenced this pull request Nov 29, 2026
Fixes #6332, #6334

- test_hf_packing_api: the bridge now forwards in_features/out_features to
  validate_quant_linear and, for a shape-blind AUTO selection, falls back to
  the portable auto-padding TorchLinear instead of the ValueError removed by
  #6327. Spy on validate_quant_linear (covering both candidates) and assert
  the fallback plus the forwarded widths.
- docs/backend-selection: import BaseQuantLinear so the registration example
  is self-contained and runnable.
- test_docs_contract_refs: refresh the hf_packing_api block 1 digest. It is
  still a continuation snippet (`linear`, `scales`, `zeros`, `g_idx` come from
  the prose), so the skip remains correct.
Qubitium added a commit that referenced this pull request Nov 29, 2026
The fenced example referenced `MyKernelBase` without defining it, so
`test_doc_python_example_runs` raised `NameError`. It now subclasses
`BaseQuantLinear`, the real base `register_kernel` requires, so the block
executes. Also refreshes the reviewed `docs/hf_packing_api.md::1` skip digest,
whose snippet changed.

Carried over from #6340, which is superseded by this PR for the
`test_hf_packing_api.py` half: the per-layer `ShapeAware` contract here
replaces #6327's whole-checkpoint fallback that #6340 asserted.
Qubitium added a commit that referenced this pull request Nov 29, 2026
#6327's `_shape_safe_gptq_selection` replaced the AUTO kernel with
`TorchLinear` for the whole checkpoint, because
`TorchCpuAvx512Linear.SUPPORTS_AUTO_PADDING` and
`MarlinLinear.SUPPORTS_AUTO_PADDING` are both False. Group-aligned layers
therefore lost their real kernel, and on CPU `TorchLinear.post_init` runs an
eager `torch_compile` per layer: cold 0.87 s -> 1.52 s, warm 0.365 s -> 0.440 s.

`_shape_aware_gptq_kernel()` now returns `ShapeAware<AutoKernel>`, a subclass
of the already-selected AUTO kernel whose `__new__` dispatches on
`in_features`: aligned widths keep the AUTO kernel, partial groups get the
auto-padding `TorchLinear`. A metaclass `__instancecheck__` keeps Optimum's
`isinstance`/`get_layers` working, and the padding kernel is selected once per
checkpoint rather than per module.

Rebased onto #6340, whose whole-checkpoint fallback assertions this replaces
with the per-layer contract.

Fixes #6336
Qubitium pushed a commit that referenced this pull request Nov 29, 2026
…he HF bridge (#6191, #6178)

Rebased onto be14824; keeps both the #6344
g_idx repair and the deferred symmetric qzeros materialization in
hf_gptqmodel_post_init, and keeps main's #6327 shape-blind fallback
contract in test_hf_packing_api.
Qubitium added a commit that referenced this pull request Nov 29, 2026
) (#6353)

* fix(hf): align portable GPTQ kernel positional order with Optimum

Optimum builds the bridge-selected class positionally as
(bits, group_size, desc_act, sym, ...). The portable GPTQ kernels declared
(bits, group_size, sym, desc_act, ...), so Optimum-built layers got the two
flags swapped. Since #6327 the bridge selects these kernels for shape-blind
HF loads, and #6344 then rejected the swapped desc_act=True instead of
repairing the missing g_idx.

Reorder TorchLinear, TorchAtenLinear (and TorchCpuAvx512Linear via
inheritance), TorchFusedLinear and TorchInt8Linear to match GPTQQuantLinear
and the other GPTQ kernels. No positional callers existed in the repo; all
internal construction is keyword-based.

Fixes #6346

* fix(hf): align W4A GPTQ kernel positional order with Optimum

`hf_select_quant_linear_v2` returns `W4AFP8Linear` (and its
`W4ANVFP4Linear` subclass) when a checkpoint names the saved
`gptq_w4afp8`/`gptq_w4a_nvfp4` backend, and Optimum builds every
returned class positionally as `(bits, group_size, desc_act, sym,
in_features, out_features, bias, ...)`. Both kernels declared
`(bits, group_size, sym, desc_act, ...)`, so a W4A load through
Optimum swapped the two flags. They only support `sym=True,
desc_act=False`, so the swap produced `sym=False` and the constructor
rejected the layer with `NotImplementedError: only supports symmetric
[True] quantization`.

Reorder both (the subclass forwards `*args`) and cover them in the
#6346 positional-order test. All in-repo call sites already pass
keywords, so the reorder is source-compatible.

---------

Co-authored-by: agent <agent@local>
Qubitium added a commit that referenced this pull request Nov 29, 2026
…os in the HF bridge (#6191, #6178, #6336) (#6274)

* fix(hf): packable kernel selection and legacy floor-width qzeros in the HF bridge (#6191, #6178)

Rebased onto be14824; keeps both the #6344
g_idx repair and the deferred symmetric qzeros materialization in
hf_gptqmodel_post_init, and keeps main's #6327 shape-blind fallback
contract in test_hf_packing_api.

* fix(hf): consolidate packable selection, per-layer shape dispatch and legacy qzeros (#6191, #6178, #6336)

Bring #6345's ShapeAware<AutoKernel> dispatcher into #6274's HF bridge
selection path:

- _shape_safe_gptq_selection() now takes the pack flag and selects the
  auto-padding fallback with the same pack gate, so a pack=True call still
  resolves to a packable kernel.
- _hf_pack_backend() runs first and resolves the packing backend; the
  shape-aware decision is layered on the resolved backend.
- the legacy floor-width qzeros wrapper is applied to both concrete kernels
  of the dispatcher (aligned AUTO and partial-group fallback), so #6178's
  deferral survives the per-layer split.
- the plain-HF pack=False AUTO path keeps its constant two selections.

Merged test_hf_packing_api.py keeps #6345's per-layer assertions and
#6274's pack/inference assertions.

* fix(hf): dispatch layer shapes the AUTO kernel cannot build

The #6336 per-layer dispatcher only checked the partial-group rule, so an
AUTO kernel that passed model-wide validation (in_features/out_features
unknown) could still reject the real layer shape. The CPU AVX512 kernel
requires out_features % 32 == 0, so a plain-HF load of a model with a
1744-wide intermediate crashed during layer replacement; route those
layers to the pre-selected TorchLinear fallback.

TorchAtenLinear also advertised SUPPORTS_AUTO_PADDING=True although its
CPU int4pack validation rejects a partial last group. That made the
shape-blind bridge skip the dispatcher for group_size=32 checkpoints and
crash on a 48-wide layer. Declare the contract the op actually enforces.

Update the #6169 dtype tests for the dispatcher's second (padding)
selection and add regression tests for both paths.

* fix(hf): materialize legacy qzeros only for kernels that own the buffer

`_materialize_hf_sym_qzeros` rebuilt every deferred symmetric `qzeros` with
`register_buffer`, but Marlin keeps its zero point in a packed `Parameter`
and never reads the words. An AUTO load of a legacy floor-width checkpoint on
CUDA therefore raised `KeyError: attribute 'qzeros' already exists` before
`gptqmodel_post_init` could run; the portable Torch kernel was unaffected,
which is why the CPU-only coverage missed it.

The deferral only hides the legacy width from the core loader, so a kernel
that does not own a `qzeros` buffer has nothing left to repair. Skip it, and
replace the constructor's meta placeholder with the empty parameter the
kernel's own runtime state installs, so a resident layer is not mistaken for
an offloaded one.

`tests/test_hf_bridge_legacy_qzeros_marlin_6274.py` covers the Marlin load on
CUDA, the portable path on CPU, and both branches of the skip directly.

---------

Co-authored-by: pr-keeper <pr-keeper@localhost>
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

1 participant