Skip to content

fix(hf): refuse unvalidatable kernels when the bridge caller omits in_features (#6149) - #6323

Merged
Qubitium merged 1 commit into
mainfrom
fix/6149-bridge
Nov 29, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/6149-bridge

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

hf_select_quant_linear{,_v2} is called once per checkpoint by
Transformers/Optimum before any layer exists, and neither caller passes
in_features/out_features. Without a width the bridge could not check a
grouped kernel's in_features % group_size == 0 contract. MarlinLinear has
the highest AUTO rank, so the shape-blind path returned it for SmolLM2 (hidden
576, group_size=128) and its constructor raised during layer replacement
with in_features: 576 must be divisible by group_size: 128 — a message that
never named the bridge decision.

The bridge now refuses that selection with an actionable ValueError instead
of returning a kernel it cannot validate.

Fixes #6149

What Changed

  • gptqmodel/utils/importer.py: both bridge entry points now call
    _reject_unvalidatable_shape_blind_selection(...) after selection. When
    in_features is absent, group_size != -1, the selected kernel subclasses
    PackedGroupedQuantLinear, and it does not auto-pad
    (SUPPORTS_AUTO_PADDING is not True), the bridge raises ValueError.
  • The message names in_features/out_features, the selected kernel, the
    group size, and the upstream caller (Transformers' GptqQuantizer via
    Optimum's GPTQQuantizer.select_quant_linear, or Transformers' AWQ layer
    replacement), and points callers that cannot supply the width at
    GPTQModel.load().
  • Behavior is unchanged when in_features is supplied, when
    group_size=-1 (no partial-group contract), or when the selected kernel
    auto-pads a partial group. Internal select_quant_linear callers
    (GPTQModel.load) and hf_check_packing_feasibility are untouched.
  • Updated bridge docstrings and docs: docs/hf-interop.md (section 7,
    failure contract, tests), docs/hf_packing_api.md example,
    docs/kernel-dispatch-cpu.md.
  • Updated the existing tests that intentionally drove the shape-blind path,
    and added tests/test_hf_bridge_missing_shape_6149.py.

Remaining upstream gap: plain Transformers loading is still blocked until
the caller forwards the width. Optimum's _replace_by_quant_layers already
reads layer.in_features and only needs to pass it to
hf_select_quant_linear_v2; Transformers' AWQ replace_with_awq_linear also
has module.in_features in its loop. Until then, a plain
AutoModelForCausalLM.from_pretrained of a GPTQ/AWQ checkpoint fails fast with
the new actionable ValueError instead of crashing inside the kernel.
GPTQModel.load is unaffected.

Tests

New CPU-only regression: tests/test_hf_bridge_missing_shape_6149.py.

Red (origin/main 89dbcf9 before the fix; re-confirmed on the pre-fix

0e0f0b5 checkout with the test copied outside the package root)

cd /tmp/red6149b
PYTHONPATH=<pre-fix-worktree>:<pre-fix-worktree>/tests CUDA_VISIBLE_DEVICES="" \
  /root/venv/bin/python -m pytest test_hf_bridge_missing_shape_6149.py -q -rs -p no:cacheprovider

Result: 4 failed, 6 passed; the four failures are the intended
Failed: DID NOT RAISE ValueError assertions for the two bridge entry points
(missing in_features, and out_features-only). The pre-fix bridge returned
MarlinLinear in both cases.

Green

cd tests
PYTHONPATH=<repo>:<repo>/tests CUDA_VISIBLE_DEVICES="" \
  /root/venv/bin/python -m pytest test_hf_bridge_missing_shape_6149.py -q -rs -p no:cacheprovider
# 10 passed

Affected suites

cd tests
PYTHONPATH=<repo>:<repo>/tests CUDA_VISIBLE_DEVICES="" \
  /root/venv/bin/python -m pytest \
    test_hf_bridge_missing_shape_6149.py test_hf_packing_api.py \
    test_hf_bridge_mixed_capability_5084.py test_hf_bridge_cpu_offload_5022.py \
    test_quant_linear_release_4847.py kernels/test_selection.py \
    test_packing.py test_packing_numeric_boundaries.py test_packing_speed.py \
    test_torch_int8_ragged_group_packing_5824.py \
    test_awq_cpu_int4pack_strided_5770.py test_awq_gemv_bias_pack.py \
    test_awq_gemv_fast_pack_vectorized.py test_awq_gemv_pack_vectorized.py \
    test_awq_gpt_neo_packed_replay_5085.py test_awq_pack_bias_representability.py \
    test_awq_pack_scale_representability.py test_awq_pack_vectorized.py \
    test_awq_qqq_pack_shape_4567.py \
    -q -rs -p no:cacheprovider
# 480 passed, 129 skipped in 86.20s

Skips are all environment gates: CUDA-only packing/repack/feasibility nodes
(CUDA_VISIBLE_DEVICES=""), CUDA-only kernels in the selection inventory, and
MLX nodes (No module named 'mlx' / macOS-only). tests/kernels/test_marlin_asymmetric_selection_5737.py passes with a GPU (23 passed); with
CUDA_VISIBLE_DEVICES="" its pre-existing
test_asymmetric_construction_rejects_activation_order_for_fallback fails
because MarlinLinear construction requires CUDA (reproduced on the pre-fix
checkout too).

/root/venv/bin/ruff check <changed files>
# All checks passed!

Real CUDA bridge reproduction (RTX 4090, before/after)

before: hf_select_quant_linear_v2(..., pack=False, no in_features) -> MarlinLinear
        MarlinLinear(in_features=576, group_size=128)
        -> NotImplementedError: `in_features`: 576 must be divisible by `group_size: 128`.

after:  hf_select_quant_linear_v2(..., pack=False, no in_features)
        -> ValueError: ... `in_features`/`out_features` were not both provided ...
           Pass `in_features=<layer input width>` ... `GptqQuantizer` currently omits ...
        hf_select_quant_linear_v2(..., in_features=576) -> TritonV2Linear
        hf_select_quant_linear_v2(..., in_features=896) -> MarlinLinear
        hf_select_quant_linear_v2(..., group_size=-1)   -> MarlinLinear

Measured Impact

Not applicable: this is a fail-closed selection guard. No kernel math,
packing layout, VRAM, or throughput path changed.

Model Support / Module Plan

Not applicable: no MODEL_MAP, definition, or module-plan change.

AWQ onboarding safety

Not applicable: no new model/alias, AWQ grouping, search, replay, or fold
change. The guard is shared with the AWQ bridge entry point but only rejects a
selection the bridge cannot validate.

Logic Flow Documentation

  • This change alters a major logic flow (kernel selection / fail-closed
    behavior); diagrams and a docs page are included.
  • New-flow Mermaid diagram uses shipped symbol names.
  • The removed mechanism is shown with before/after diagrams and a
    situation → before → after table in docs/hf-interop.md §7. The old
    mechanism returned select_quant_linear(...) directly from
    hf_select_quant_linear* (gptqmodel/utils/importer.py); the new guard
    is _reject_unvalidatable_shape_blind_selection in the same module.
  • Failure and ambiguity paths are marked red; success paths green.
  • docs/hf-interop.md repeats the diagrams and is linked from
    README.md (Hugging Face interop link).

New flow:

flowchart TD
    A["hf_select_quant_linear*"] --> B{"in_features supplied?"}
    B -->|"yes"| C["validate_quant_linear(in_features=...)"]
    C --> D{"in_features % group_size == 0?"}
    D -->|"yes"| E["MarlinLinear eligible"]
    D -->|"no"| F["skip Marlin; TritonV2Linear / TorchLinear"]
    B -->|"no"| G{"grouped, non-auto-padding, group_size != -1?"}
    G -->|"yes"| H["ValueError naming in_features / out_features and the upstream caller"]
    G -->|"no"| I["return the shape-safe kernel"]
    style H fill:#f8d7da,stroke:#b02a37
    style I fill:#d9f2d9,stroke:#2e7d32
Loading

Review Requirements

  • Reviewed this PR.
  • Code matches existing project structure, APIs, and conventions.
  • No new monkeypatching; the guard is a normal bridge-side check.
  • Impacted surface is limited to the two bridge entry points and docs.
  • Considered regressions: shape-supplied callers, group_size=-1,
    auto-padding kernels, explicit backends, and internal loaders are
    covered by tests or left unchanged.

Kernel Accuracy Requirements

  • Not applicable: selection-only change; no kernel forward, layout, or
    accuracy path changed.
  • No kernel inventory change.

Notes

  • The refusal also applies when a caller explicitly selects a grouped backend
    without in_features (for example backend="marlin"): the bridge still
    cannot prove that kernel can build the layers it returns. Pass the layer
    shape in that case.
  • hf_check_packing_feasibility keeps its documented shape-blind
    bool(candidates) contract; only the class-returning bridge refuses.

…_features (#6149)

Transformers/Optimum call hf_select_quant_linear{,_v2} once per checkpoint
before any layer exists, and they do not pass in_features/out_features. Without
a width the bridge cannot check a grouped kernel's `in_features % group_size == 0`
contract, so the shape-blind path returned MarlinLinear for SmolLM2 (hidden 576,
group_size=128) and its constructor raised during layer replacement.

The bridge now refuses that selection with a ValueError that names the missing
arguments, the selected kernel, the group size, and the upstream caller
(Transformers' GptqQuantizer via Optimum's GPTQQuantizer.select_quant_linear,
or Transformers' AWQ layer replacement). A caller that supplies in_features
keeps the previous selection; group_size=-1 and auto-padding kernels need no
shape and are unchanged. Internal select_quant_linear callers, including
GPTQModel.load, are not affected.

The load itself remains upstream-blocked: Optimum already reads
layer.in_features in _replace_by_quant_layers and only needs to forward it.

Fixes #6149
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 08:02
@Qubitium
Qubitium merged commit 339036d into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/6149-bridge branch November 29, 2026 08:02
Qubitium added a commit that referenced this pull request Nov 29, 2026
Rebased fix/writer-interop-consolidated onto origin/main cc10a7a.
The only conflict was the ..utils.model import block; keep main's
MODALITY alongside the PR's FUSED_PROJECTION_UNITS. No hunks dropped.

The four PR test files give 124 passed / 3 skipped / 1 failed; the
single failure (test_gptq_block_name_loads_through_plain_hf) fails
identically on pristine origin/main because #6323 now refuses
Optimum's shape-blind grouped-kernel selection.
Qubitium added a commit that referenced this pull request Nov 29, 2026
…layer shape (#6327)

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 added a commit that referenced this pull request Nov 29, 2026
Rebased fix/hf-bridge-layout-consolidated onto origin/main abde192.
importer.py was the only conflict. Main's #6326 replaced #6323's
shape-blind refusal with an AUTO fallback to the portable auto-padding
Torch kernel, while this PR adds _hf_pack_backend() and the legacy
floor-width symmetric qzeros deferral. Kept both: resolve the pack
backend, select with it, apply main's shape-safe Torch fallback when the
caller omits in_features, then wrap a legacy qzeros checkpoint.

Dropped the old _reject_unvalidatable_shape_blind_selection raise body,
superseded by main's _needs_shape_safe_gptq_kernel().

Adapted tests/test_hf_packing_api.py to the #6326 contract: shape-blind
selection now asserts the TorchLinear fallback instead of ValueError;
in_features=896 still selects Marlin and 576 still avoids it.

Tests: 96 passed, 11 skipped on CPU. Pristine main for the same files:
2 failed (the same two tests), 84 passed, 11 skipped.
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