Skip to content

fix(awq): replay a **kwargs block and fold the norm the layer owns (#4891) - #5019

Merged
Qubitium merged 6 commits into
mainfrom
root/hymba-awq-4891
Nov 17, 2026
Merged

Qubitium merged 6 commits into
mainfrom
root/hymba-awq-4891

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4891

Hymba's AWQ path had two defects; the first hid the second. Both are fixed here.

Scope note: #4888 (half-precision load: root-level meta parameters and the float32 A_log) is not part of this PR. It landed separately in #5021 (get_modules_with_direct_meta_tensors now detects any module-tree node owning a direct meta tensor, plus the construction-dtype record). This branch is rebased on that merge and touches none of it.

Root cause

1. _sanitize_kwargs emptied a **kwargs forward

AWQProcessor._sanitize_kwargs kept only the keys that appear by name in inspect.signature(module.forward). HymbaBlock.forward is (hidden_states, past_key_value=None, **kwargs) and reads attention_mask, position_ids, kv_last_layer and use_swa out of the catch-all, so the filter handed it {} and the scale search aborted in layer 0:

File ".../modeling_hymba.py", line 1744, in forward
    res, attn_key_value = self.mixer_forward(hidden_states, ..., attention_mask=kwargs['attention_mask'], ...)
KeyError: 'attention_mask'

KeyError is not an _AWQScaleGroupError, so quantize() aborted instead of falling back.

2. The tree named a norm the layer does not own

definitions/hymba.py declared post_attention_layernorm, which HymbaDecoderLayer does not have; the norm in front of the MoE is pre_moe_layernorm. With no producer of its own the routed-expert gate/up block was merged into the preceding norm run:

input_layernorm -> [mamba.in_proj, moe.experts.0.gate_proj, moe.experts.0.up_proj]

The experts read pre_moe_layernorm(residual + mamba_out), so that fold is not output-preserving. This is the defect that a kwargs-only fix would have turned into silent quality loss — which is why both are in this PR.

Fix

File Change
gptqmodel/looper/awq_processor.py _forward_accepts_var_kwargs(): a VAR_KEYWORD parameter means the module accepts the captured kwargs. _sanitize_kwargs then passes them through, minus the processor's private bookkeeping (_awq_feature_kwargs); the retained-rows key _awq_rotary_position_ids still survives so _prepare_module_forward can rebuild rotary. _prepare_module_forward also treats position_ids as accepted for such a forward, so a 2-D activation is promoted to [1, seq, hidden] the way the block's pack(...) requires.
gptqmodel/models/definitions/hymba.py post_attention_layernorm -> pre_moe_layernorm; mamba.in_proj:0:nosrc (the conv/SSM path between in_proj and out_proj is not homogeneous — the same guard as Falcon-H1's mixer in #4674); conv1d:!, x_proj.0:!, dt_proj.0:! declared dense.
docs/awq-calibration-aggregation.md Document the **kwargs passthrough and the 2-D promotion.
tests/test_hymba_awq_4891.py New: synthetic replica of the declared layer (see boundary below).
tests/module_tree/fixtures/registered_module_plans.json Repin the HymbaQModel/gptq case: pre_moe_layernorm instead of the absent post_attention_layernorm, and the mixer's dense leaves in the mamba.in_proj block.
.github/workflows/awq_cpu.yml Run the new file in the unit suite (the workflow lists its files explicitly; the path filter alone would start the job without executing it).

nosrc and the :! leaves follow the reporter's own suggested fix. Without nosrc the fold-source resolver could skip the dense leaves and land on in_proj, scaling the mixer's output.

Verification

Verification boundary — read this before trusting the numbers. nvidia/Hymba-1.5B-Base is not available on this host: transformers 5.20.2 has no transformers/models/hymba (remote code only), there is no Hymba checkpoint in the HF cache, HF_HUB_OFFLINE=1 blocks any download, and mamba_ssm/causal_conv1d are not installed. Real-weight quantize() was NOT validated, and nothing here claims it was. The test builds a synthetic replica from the exact module names the issue quotes: input_layernorm -> mamba (in_proj, conv1d, x_proj.0, dt_proj.0, out_proj) -> pre_moe_layernorm -> moe.experts.0.{gate,up,down}_proj, with a HymbaBlock-style forward(self, hidden_states, past_key_value=None, **kwargs) that raises if the mask/positions/kv keys are dropped.

Group set and fold invariant match the reporter's expectation

origin/main @ e7cb6ad this PR
groups input_layernorm -> [mamba.in_proj, gate_proj, up_proj], up_proj -> [down_proj] input_layernorm -> [in_proj], pre_moe_layernorm -> [gate_proj, up_proj] (+ moe.gate as a scale consumer), up_proj -> [down_proj]
relative logit change from folding a random scale 3.3e-2 (layer 0), 2.5e-2 (layer 1) < 1e-7

That is the group set the reporter measured at 1e-6 on the real weights, reproduced from the declared names. The moe.gate consumer is what keeps the fold output-preserving: the router reads the same normed input, so apply_scale has to scale its input columns too.

Red on unmodified origin/main -> green here

tests/test_hymba_awq_4891.py result
unmodified origin/main @ bfbb7a2, test copied in 6 failed, 1 passed
this PR 7 passed

The six red nodes are the kwargs passthrough, the 2-D promotion, the declared tree (no post_attention_layernorm, nosrc, dense leaves), the group set, the gate/up consumer wiring and the fold invariant. The seventh (test_sanitize_kwargs_still_filters_a_named_signature) is a guard for non-**kwargs modules and passes on both.

Neighbouring suites — failure set identical to unmodified main

suite main this PR
test_awq_scaling_groups.py, module_tree/test_awq_relationships.py, test_docs_contract_refs.py, test_hymba_bf16_load_4888.py (from #5021) 9 failed, 217 passed, 2 skipped 9 failed, 224 passed, 2 skipped
module_tree/test_registered_module_plans.py 423 passed 423 passed (with the repinned Hymba case)

Same nine failing nodes on both trees (test_awq_scaling_groups afmoe/flint, test_awq_relationships granite), all pre-existing on this host; the only delta is the 7 new nodes.

ruff check is clean on every changed Python file.

test_registered_module_plans.py is the always-run inventory contract that pins each definition's compiled blocks; it fails on the tree change until the Hymba case is updated, which is what the last commit does (the AWQ CPU (unit) job caught it on the first push).

Residual risk

  • Real Hymba weights, remote code and the CUDA mamba_ssm/causal_conv1d kernels were unavailable, so quantize() was validated structurally, not on the real checkpoint. A host with the model should re-run the issue's repro (AWQConfig(bits=4, group_size=32, offload_to_disk=False) over the four calibration rows) and confirm the groups logged by apply_scale are the three above.
  • The fixture reproduces the conv/SSM non-homogeneity that motivates in_proj:nosrc, but not Hymba's exact SSM math.

Comment thread tests/test_hymba_awq_4891.py Fixed
Comment thread tests/test_hymba_awq_4891.py Fixed
…4891)

Two defects in Hymba's AWQ path; the first hid the second.

Replay: `AWQProcessor._sanitize_kwargs` kept only the keys that appear by name
in `inspect.signature(module.forward)`. `HymbaBlock.forward` is
`(hidden_states, past_key_value=None, **kwargs)` and reads `attention_mask`,
`position_ids`, `kv_last_layer` and `use_swa` out of the catch-all, so the
filter handed it an empty dict and the scale search aborted in layer 0 with
`KeyError: 'attention_mask'`. A `VAR_KEYWORD` parameter now means the module
accepts the captured kwargs, minus the processor's own private bookkeeping
(`_awq_feature_kwargs`); `_prepare_module_forward` also treats `position_ids` as
accepted so a 2-D activation is promoted to `[1, seq, hidden]` the way the
block's `pack(...)` requires.

Scaling groups: the tree declared `post_attention_layernorm`, which
`HymbaDecoderLayer` does not own -- the norm in front of the MoE is
`pre_moe_layernorm`. With no producer of its own, the routed-expert gate/up
block was merged into the preceding norm run and folded into
`input_layernorm`:

    input_layernorm -> [mamba.in_proj, moe.experts.0.gate_proj,
                        moe.experts.0.up_proj]

The experts read `pre_moe_layernorm(residual + mamba_out)`, so that fold is not
output-preserving: 3.3e-2 (layer 0) and 2.5e-2 (layer 1) relative logit change
on the synthetic fixture. The tree now declares `pre_moe_layernorm`, marks
`mamba.in_proj` `:nosrc` (the conv/SSM path between `in_proj` and `out_proj` is
not homogeneous, the same guard as Falcon-H1's mixer in #4674) and declares the
mixer's dense leaves `conv1d`, `x_proj.0` and `dt_proj.0` `:!`. Groups become

    input_layernorm     -> [mamba.in_proj]
    pre_moe_layernorm   -> [moe.experts.0.gate_proj, moe.experts.0.up_proj]
                           (+ moe.gate as a scale consumer)
    moe.experts.0.up_proj -> [moe.experts.0.down_proj]

and every fold measures < 1e-7.

`tests/test_hymba_awq_4891.py` synthesises the layer from the exact module names
the tree declares (a `**kwargs` block, `input_layernorm` -> `mamba` ->
`pre_moe_layernorm` -> `moe`) and asserts the sanitisation, the promotion, the
declared tree, the group set and the fold invariant. Seven nodes: 7 failed on
unmodified `origin/main`, 7 pass here.

Real `nvidia/Hymba-1.5B-Base` weights are not available on this host (no
checkpoint, no remote code, no `mamba_ssm`/`causal_conv1d`), so the fixture is
the verification boundary for this commit.
The new file is the only coverage for #4891 on a CPU host. The workflow lists
its files explicitly, so the path filter alone would start the job without ever
executing it.
@Qubitium
Qubitium force-pushed the root/hymba-awq-4891 branch from 15b07ae to de49a5b Compare November 17, 2026 22:39
@Qubitium Qubitium changed the title fix(hymba): replay a **kwargs block, fold the norm the layer owns, and load in half precision (#4891, #4888) fix(awq): replay a **kwargs block and fold the norm the layer owns (#4891) Nov 17, 2026
The only conflict was the AWQ CPU unit-test list: main added
tests/test_smooth_percentile_bounds_5005.py and this branch added
tests/test_hymba_awq_4891.py. Keep both.
…4891)

Two bare `kwargs[...]` expressions in the fake block's forward were
statements with no effect. Bind them explicitly; a missing key still raises
KeyError, which is the behaviour the test pins.
…tory

`tests/module_tree/test_registered_module_plans.py` compares every definition's
compiled blocks against `fixtures/registered_module_plans.json`, so the Hymba
case has to follow the tree fix: `post_attention_layernorm` becomes
`pre_moe_layernorm`, and the mixer's dense leaves (`conv1d`, `x_proj.0`,
`dt_proj.0`) now appear in the `mamba.in_proj` block instead of being undeclared.

The `AWQ CPU (unit)` job caught this; the pinned case is the only place the
declared block order is written down outside the definition itself.
Comment thread tests/test_hymba_awq_4891.py Fixed
Comment thread tests/test_hymba_awq_4891.py Fixed
The stub block read `kv_last_layer`/`position_ids` into underscore locals to
assert the `**kwargs` replay forwards them; both bindings were unused, so the
code-quality review flagged them. Check the whole required set up front and
raise `KeyError` with the missing key, which is the behaviour the test pins.
@Qubitium
Qubitium merged commit 7744cb8 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the root/hymba-awq-4891 branch November 17, 2026 23:02
Qubitium added a commit that referenced this pull request Nov 18, 2026
…5142)

`AWQProcessor._all_experts_replay_forward` installs an instance-level
`all_experts_forward(hidden_states, **kwargs)` on the MoE block. Since #5019,
`_forward_accepts_var_kwargs` read that catch-all as a real declaration, so
`_sanitize_kwargs` kept every captured layer kwarg and
`_prepare_module_forward` promoted the 2-D replay and synthesized
`position_ids`. The wrapper then forwarded those keys to the original
`SparseMoeBlock.forward(hidden_states)`, which raised
`TypeError: ... unexpected keyword argument 'position_ids'` in layer 0.

Decorate the wrapper with `functools.wraps(original_forward)`: `inspect.signature`
follows `__wrapped__`, so the replay path sees the block's declared signature
again while Hymba's genuine `(hidden_states, past_key_value=None, **kwargs)`
block still reports `VAR_KEYWORD`. The wrapper also calls the original forward
through `safe_kwargs_call`, so a caller cannot widen that contract.

Regression coverage in `tests/test_awq_moe_bypass_4223.py` drives the real
instance wrapper through `_sanitize_kwargs` and `_prepare_module_forward` with
`position_ids`/`attention_mask` and asserts the original block receives only
the keys it declares. The existing end-to-end bypass replay test now passes.

OLMoE-1B-7B-0924 truncated to 2 layers with the issue's C4 sample:
e2f9676 raises the TypeError; this commit quantizes 392 modules and captures
1,817,856 expert rows, matching the pre-#5019 baseline.
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