Skip to content

feat(hf): let the bridge validate a caller-supplied in_features (#6149) - #6187

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

Qubitium merged 1 commit into
mainfrom
fix/6149-bridge-in-features

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

hf_select_quant_linear{,_v2} return one kernel class for every layer they
replace, so they can only honour a kernel's width contract when the caller says
which width it is about to build. Neither entry point accepted that information,
so selection was shape-blind: MarlinLinear has the highest AUTO rank (90) and
its constructor rejects any in_features that is not a multiple of
group_size. A SmolLM2 checkpoint (hidden 576, group_size=128) therefore fails
inside Optimum's layer replacement:

NotImplementedError: <class 'gptqmodel.nn_modules.qlinear.marlin.MarlinLinear'>:
`in_features`: 576 must be divisible by `group_size: 128`.

Optimum already has the shape: _replace_by_quant_layers reads
layer.in_features to build each layer, then constructs the class selected
earlier. It just does not pass it to the bridge. Missing information cannot be
recovered by the selector, so the fix is to make the bridge accept it.

Refs #6149

What Changed

  • gptqmodel/utils/importer.py
    • hf_select_quant_linear, hf_select_quant_linear_v2 and
      select_quant_linear accept in_features / out_features and forward them
      to BaseQuantLinear.validate in every AUTO candidate pass (main, dynamic
      multi-select, portable fallback, explicit backend).
    • No behaviour change when the caller omits them: selection stays exactly as
      before, so existing loaders are unaffected.
  • docs/hf-interop.md: new section 7 documents the contract with a decision
    diagram, plus rows in the failure contract and test tables.
  • tests/test_hf_packing_api.py: new node covering both bridges.

This does not patch Transformers/Optimum (per #4620). The remaining step for
the end-to-end crash is the caller passing in_features; with that one-line
change Optimum gets a buildable kernel for partial-group layers and keeps Marlin
everywhere else.

Tests

  • Unit tests have been executed and passed.
  • I added a new simple/fast unit test for this change.

Behaviour, hf_select_quant_linear_v2(..., pack=False) on a CUDA device map,
bits=4, group_size=128, sym=True:

in_features Selected kernel
omitted MarlinLinear (unchanged)
896 (7 x 128) MarlinLinear
576 (SmolLM2 hidden) TritonV2Linear
960 (SmolLM2-360M hidden) TritonV2Linear

Red (importer reverted, new tests):

cd /root/openai-audit/review-6073 && PYTHONPATH=$PWD:$PWD/tests /root/venv/bin/python -m pytest '"\
  "tests/test_hf_packing_api.py::test_hf_bridge_forwards_in_features_to_kernel_validation" -q
# E   TypeError: hf_select_quant_linear() got an unexpected keyword argument 'in_features'
# E   TypeError: hf_select_quant_linear_v2() got an unexpected keyword argument 'in_features'
# 2 failed in 5.04s
"'```

Green (fix applied):

```bash
cd /root/openai-audit/review-6073 && PYTHONPATH=$PWD:$PWD/tests /root/venv/bin/python -m pytest '"\
  "tests/test_hf_packing_api.py::test_hf_bridge_forwards_in_features_to_kernel_validation" -q
# 2 passed in 2.94s
"'```

Bridge and selection families (no regressions):

```bash
cd /root/openai-audit/review-6073 && PYTHONPATH=$PWD:$PWD/tests /root/venv/bin/python -m pytest '"\
  tests/test_hf_packing_api.py tests/test_hf_bridge_mixed_capability_5084.py \\
  tests/kernels/test_marlin_asymmetric_selection_5737.py -q -rs
# 90 passed, 1 skipped (CPU fallback path only exercised without CUDA)

PYTHONPATH=$PWD:$PWD/tests /root/venv/bin/python -m pytest '"\
  tests/test_cpu_kernel_constraints_4181.py tests/test_lazy_kernel_registry_import.py \\
  tests/test_auto_dtype_cast.py -q -rs
# 93 passed in 169.73s
"'```

Lint: `ruff check` 0.14.2 on both changed Python files: all checks passed.

## Measured Impact

Functional; the extra validation is the existing `validate` call with one more
keyword, and it runs once per `quantize()`/bridge call.

| Case | Before | After |
| --- | --- | --- |
| Bridge, `in_features=576`, `group_size=128` | `MarlinLinear` -> constructor raises | `TritonV2Linear` |
| Bridge, `in_features=896` | `MarlinLinear` | `MarlinLinear` |
| Bridge, no `in_features` | `MarlinLinear` | `MarlinLinear` (unchanged) |

## Model Support / Module Plan

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

## AWQ onboarding safety

Not applicable: the parameter is method-agnostic and only feeds the existing
`validate` contract; AWQ grouping, search, replay and fold are untouched.

## Logic Flow Documentation

- [x] The required diagrams and docs page are included.

`docs/hf-interop.md` section 7 has the decision diagram for
shape-blind vs shape-validated selection, with the failure path marked.

## Review Requirements

- [x] Reviewed this PR.
- [x] Checked that the code matches existing project structure, APIs, and conventions.
- [x] Avoided unnecessary monkeypatching and used the project'"'s normal extension points where possible.
- [x] Minimized and compacted the impacted code surface.
- [x] Considered and eliminated potential regressions.

## Kernel Accuracy Requirements

Not applicable: no kernel change. Kernel choice changes only when a caller
supplies a width the previously selected kernel could not construct.

## Notes

`in_features` is intentionally optional: requiring it would break every existing
caller, and a caller that omits it is asking for a shape-blind answer. The
documented contract makes that explicit rather than guessing on the caller'"'s
behalf.

The HF bridge returns one kernel class for every layer it replaces, so it can
only honour a kernel's width contract when the caller passes the width it is
about to build. Neither bridge entry point accepted that information, so
selection was shape-blind: MarlinLinear has the highest AUTO rank and its
constructor rejects any `in_features` that is not a multiple of `group_size`,
which is why a SmolLM2 checkpoint (hidden 576, group_size 128) failed during
Optimum's layer replacement.

Accept `in_features`/`out_features` in `hf_select_quant_linear`,
`hf_select_quant_linear_v2` and `select_quant_linear`, and forward them to
`BaseQuantLinear.validate` in every AUTO candidate pass (the same check
`GPTQModel.load` applies per module). A caller that supplies the width now gets
a kernel that can build it; a caller that does not keeps the previous
shape-blind answer, which the docs now state explicitly.

Optimum already reads `layer.in_features` in `_replace_by_quant_layers` and
only needs to pass it here; GPT-QModel does not patch Transformers/Optimum.

Refs #6149
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 01:02
@Qubitium
Qubitium merged commit 454d0c2 into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/6149-bridge-in-features branch November 29, 2026 01:03
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