Repository navigation
fix(awq): replay a **kwargs block and fold the norm the layer owns (#4891) - #5019
Merged
Merged
Conversation
…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
force-pushed
the
root/hymba-awq-4891
branch
from
November 17, 2026 22:39
15b07ae to
de49a5b
Compare
**kwargs block, fold the norm the layer owns, and load in half precision (#4891, #4888)**kwargs block and fold the norm the layer owns (#4891)
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.
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.
This was referenced Nov 17, 2026
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.
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.
Fixes #4891
Hymba's AWQ path had two defects; the first hid the second. Both are fixed here.
Root cause
1.
_sanitize_kwargsemptied a**kwargsforwardAWQProcessor._sanitize_kwargskept only the keys that appear by name ininspect.signature(module.forward).HymbaBlock.forwardis(hidden_states, past_key_value=None, **kwargs)and readsattention_mask,position_ids,kv_last_layeranduse_swaout of the catch-all, so the filter handed it{}and the scale search aborted in layer 0:KeyErroris not an_AWQScaleGroupError, soquantize()aborted instead of falling back.2. The tree named a norm the layer does not own
definitions/hymba.pydeclaredpost_attention_layernorm, whichHymbaDecoderLayerdoes not have; the norm in front of the MoE ispre_moe_layernorm. With no producer of its own the routed-expert gate/up block was merged into the preceding norm run: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
gptqmodel/looper/awq_processor.py_forward_accepts_var_kwargs(): aVAR_KEYWORDparameter means the module accepts the captured kwargs._sanitize_kwargsthen passes them through, minus the processor's private bookkeeping (_awq_feature_kwargs); the retained-rows key_awq_rotary_position_idsstill survives so_prepare_module_forwardcan rebuild rotary._prepare_module_forwardalso treatsposition_idsas accepted for such a forward, so a 2-D activation is promoted to[1, seq, hidden]the way the block'spack(...)requires.gptqmodel/models/definitions/hymba.pypost_attention_layernorm->pre_moe_layernorm;mamba.in_proj:0:nosrc(the conv/SSM path betweenin_projandout_projis 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**kwargspassthrough and the 2-D promotion.tests/test_hymba_awq_4891.pytests/module_tree/fixtures/registered_module_plans.jsonHymbaQModel/gptqcase:pre_moe_layernorminstead of the absentpost_attention_layernorm, and the mixer's dense leaves in themamba.in_projblock..github/workflows/awq_cpu.ymlunitsuite (the workflow lists its files explicitly; the path filter alone would start the job without executing it).nosrcand the:!leaves follow the reporter's own suggested fix. Withoutnosrcthe fold-source resolver could skip the dense leaves and land onin_proj, scaling the mixer's output.Verification
Verification boundary — read this before trusting the numbers.
nvidia/Hymba-1.5B-Baseis not available on this host:transformers5.20.2 has notransformers/models/hymba(remote code only), there is no Hymba checkpoint in the HF cache,HF_HUB_OFFLINE=1blocks any download, andmamba_ssm/causal_conv1dare not installed. Real-weightquantize()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 aHymbaBlock-styleforward(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@ e7cb6adinput_layernorm -> [mamba.in_proj, gate_proj, up_proj],up_proj -> [down_proj]input_layernorm -> [in_proj],pre_moe_layernorm -> [gate_proj, up_proj](+moe.gateas a scale consumer),up_proj -> [down_proj]That is the group set the reporter measured at 1e-6 on the real weights, reproduced from the declared names. The
moe.gateconsumer is what keeps the fold output-preserving: the router reads the same normed input, soapply_scalehas to scale its input columns too.Red on unmodified
origin/main-> green heretests/test_hymba_awq_4891.pyorigin/main@ bfbb7a2, test copied inThe 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-**kwargsmodules and passes on both.Neighbouring suites — failure set identical to unmodified main
test_awq_scaling_groups.py,module_tree/test_awq_relationships.py,test_docs_contract_refs.py,test_hymba_bf16_load_4888.py(from #5021)module_tree/test_registered_module_plans.pySame nine failing nodes on both trees (
test_awq_scaling_groupsafmoe/flint,test_awq_relationshipsgranite), all pre-existing on this host; the only delta is the 7 new nodes.ruff checkis clean on every changed Python file.test_registered_module_plans.pyis 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 (theAWQ CPU (unit)job caught it on the first push).Residual risk
mamba_ssm/causal_conv1dkernels were unavailable, soquantize()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 byapply_scaleare the three above.in_proj:nosrc, but not Hymba's exact SSM math.