Repository navigation
fix(hf): refuse unvalidatable kernels when the bridge caller omits in_features (#6149) - #6323
Merged
Merged
Conversation
…_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
marked this pull request as ready for review
November 29, 2026 08:02
This was referenced Nov 29, 2026
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.
This was referenced Nov 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
hf_select_quant_linear{,_v2}is called once per checkpoint byTransformers/Optimum before any layer exists, and neither caller passes
in_features/out_features. Without a width the bridge could not check agrouped kernel's
in_features % group_size == 0contract.MarlinLinearhasthe highest AUTO rank, so the shape-blind path returned it for SmolLM2 (hidden
576,
group_size=128) and its constructor raised during layer replacementwith
in_features: 576 must be divisible by group_size: 128— a message thatnever named the bridge decision.
The bridge now refuses that selection with an actionable
ValueErrorinsteadof 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. Whenin_featuresis absent,group_size != -1, the selected kernel subclassesPackedGroupedQuantLinear, and it does not auto-pad(
SUPPORTS_AUTO_PADDING is not True), the bridge raisesValueError.in_features/out_features, the selected kernel, thegroup size, and the upstream caller (Transformers'
GptqQuantizerviaOptimum's
GPTQQuantizer.select_quant_linear, or Transformers' AWQ layerreplacement), and points callers that cannot supply the width at
GPTQModel.load().in_featuresis supplied, whengroup_size=-1(no partial-group contract), or when the selected kernelauto-pads a partial group. Internal
select_quant_linearcallers(
GPTQModel.load) andhf_check_packing_feasibilityare untouched.docs/hf-interop.md(section 7,failure contract, tests),
docs/hf_packing_api.mdexample,docs/kernel-dispatch-cpu.md.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_layersalreadyreads
layer.in_featuresand only needs to pass it tohf_select_quant_linear_v2; Transformers' AWQreplace_with_awq_linearalsohas
module.in_featuresin its loop. Until then, a plainAutoModelForCausalLM.from_pretrainedof a GPTQ/AWQ checkpoint fails fast withthe new actionable
ValueErrorinstead of crashing inside the kernel.GPTQModel.loadis 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)
Result:
4 failed, 6 passed; the four failures are the intendedFailed: DID NOT RAISE ValueErrorassertions for the two bridge entry points(missing
in_features, andout_features-only). The pre-fix bridge returnedMarlinLinearin both cases.Green
Affected suites
Skips are all environment gates: CUDA-only packing/repack/feasibility nodes
(
CUDA_VISIBLE_DEVICES=""), CUDA-only kernels in the selection inventory, andMLX nodes (
No module named 'mlx'/ macOS-only).tests/kernels/test_marlin_asymmetric_selection_5737.pypasses with a GPU (23 passed); withCUDA_VISIBLE_DEVICES=""its pre-existingtest_asymmetric_construction_rejects_activation_order_for_fallbackfailsbecause
MarlinLinearconstruction requires CUDA (reproduced on the pre-fixcheckout too).
Real CUDA bridge reproduction (RTX 4090, before/after)
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
behavior); diagrams and a docs page are included.
situation → before → after table in
docs/hf-interop.md§7. The oldmechanism returned
select_quant_linear(...)directly fromhf_select_quant_linear*(gptqmodel/utils/importer.py); the new guardis
_reject_unvalidatable_shape_blind_selectionin the same module.docs/hf-interop.mdrepeats the diagrams and is linked fromREADME.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:#2e7d32Review Requirements
group_size=-1,auto-padding kernels, explicit backends, and internal loaders are
covered by tests or left unchanged.
Kernel Accuracy Requirements
accuracy path changed.
Notes
without
in_features(for examplebackend="marlin"): the bridge stillcannot prove that kernel can build the layers it returns. Pass the layer
shape in that case.
hf_check_packing_feasibilitykeeps its documented shape-blindbool(candidates)contract; only the class-returning bridge refuses.