Skip to content

fix(hymba): pass replayed kwargs to a **kwargs forward and name the real MoE norm (#4891) - #5024

Closed
Qubitium wants to merge 2 commits into
mainfrom
fix/issue-4891
Closed

Qubitium wants to merge 2 commits into
mainfrom
fix/issue-4891

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4891

Two defects in Hymba AWQ; the first hid the second.

1. _sanitize_kwargs dropped every kwarg of a **kwargs-only forward

AWQProcessor._sanitize_kwargs kept only keys that appear by name in inspect.signature(module.forward).parameters. HymbaBlock.forward(self, hidden_states, past_key_value=None, **kwargs) reads kwargs["attention_mask"], kwargs["kv_last_layer"], kwargs["position_ids"], kwargs["use_cache"] and kwargs["use_swa"], so it got an empty dict:

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

That is not an _AWQScaleGroupError, so quantize() aborted in layer 0. A VAR_KEYWORD parameter now passes the captured kwargs through unchanged; a forward with an explicit signature still filters (both pinned by tests).

2. The tree named a norm the decoder layer does not have

gptqmodel/models/definitions/hymba.py declared "post_attention_layernorm": ("post_attention_layernorm:!",) before moe:moe. HymbaDecoderLayer has no such module — the norm in front of the MoE is pre_moe_layernorm, applied to residual + mamba_out (verified against nvidia/Hymba-1.5B-Base's modeling_hymba.py: self.pre_moe_layernorm = HymbaRMSNorm(...) and hidden_states = self.pre_moe_layernorm(hidden_states) right before self.moe(...)).

With no producer of its own, the experts block was merged into the preceding norm run:

input_layernorm -> [mamba.in_proj, moe.experts.0.gate_proj, moe.experts.0.up_proj]     # before
input_layernorm -> [mamba.in_proj]                                                      # after
pre_moe_layernorm -> [moe.experts.0.gate_proj, moe.experts.0.up_proj]
moe.experts.0.up_proj -> [moe.experts.0.down_proj]

The experts read pre_moe_layernorm's output, so the old fold was not output-preserving (the report measured 5.8e-2 / 8.2e-2 relative logit change). mamba.in_proj is now marked :nosrc too, because it feeds a conv/SSM/attention path that is not positively homogeneous — the old tree only avoided that fold through a width mismatch, which is not a guarantee.

Verification

New tests/test_hymba_awq_groups_4891.py (8 tests), using a synthetic HymbaDecoderLayer-shaped module because the real model needs remote code with mamba_ssm/causal_conv1d/flash_attn:

  • tree declares pre_moe_layernorm and no post_attention_layernorm; in_proj:0:nosrc present;
  • the group builder produces exactly the three groups above and no input_layernorm → experts or in_proj → out_proj fold;
  • folding a random 0.5-1.5 scale into each of the three groups changes the layer output by < 1e-5;
  • _sanitize_kwargs passes everything to a **kwargs forward and still filters an explicit signature.

Red on parent (git checkout origin/main -- gptqmodel/models/definitions/hymba.py gptqmodel/looper/awq_processor.py, then restored): 5 failed, 3 passed — including test_folding_a_scale_is_output_preserving[0], i.e. the old input_layernorm → [in_proj, gate, up] fold does not preserve the layer output.

Green after merging current main: 8 passed. Neighbouring suites — tests/test_awq_scale_folding.py tests/test_awq_scale_search_safety.py tests/module_tree/test_awq_relationships.py tests/test_hymba_awq_groups_4891.py → 114 passed; tests/test_awq_conv1d_gpt2_4887.py and tests/test_awq_scale_search_memory.py/test_awq_deepseek_v4_rotary_4695.py pass too. ruff check gptqmodel tests clean.

One note: tests/test_awq_weight_mean.py::test_awq_weight_mean_matches_legacy_impl_2_cpu_gs128 asserts a wall-clock ratio and failed once under a parallel suite run (59.2 ms vs 25.7 ms); it passes in isolation and does not touch this change.

Residual risk

  • The synthetic layer is not the real Hymba: the conv/SSM internals are replaced by in_proj → relu → out_proj. The claim being tested is the fold topology (which module reads which norm, and homogeneity), which the real forward shares. A real-model AWQ run on nvidia/Hymba-1.5B-Base was not possible here (no mamba_ssm/flash_attn).
  • _sanitize_kwargs now forwards unknown keys to any **kwargs forward. That is what **kwargs means, but a remote-code forward that re-dispatches **kwargs to a stricter child would now see the extra keys; the previous behaviour silently broke such a forward instead.
  • Dense Hymba layers (intermediate_size == 0) have neither pre_moe_layernorm nor moe; the tree tolerates absent entries exactly as it did for the previously declared (also absent) norm.

… real MoE norm (#4891)

Two defects, the first hiding the second.

1. `AWQProcessor._sanitize_kwargs` kept only the keys that appear by name in
   `inspect.signature(module.forward).parameters`. `HymbaBlock.forward(self,
   hidden_states, past_key_value=None, **kwargs)` indexes
   `kwargs["attention_mask"]`, `kwargs["kv_last_layer"]`,
   `kwargs["position_ids"]`, `kwargs["use_cache"]` and `kwargs["use_swa"]`, so
   it received an empty dict and the replay raised `KeyError: 'attention_mask'`
   -- not an `_AWQScaleGroupError`, so `quantize()` aborted in layer 0.
   A `VAR_KEYWORD` parameter now passes the captured kwargs through unchanged;
   an explicit signature still filters.

2. `gptqmodel/models/definitions/hymba.py` declared
   `post_attention_layernorm`, which `HymbaDecoderLayer` does not have: the
   norm in front of the MoE is `pre_moe_layernorm`, applied to
   `residual + mamba_out`. With no producer of its own, the experts block was
   merged into the preceding norm run, so AWQ built
   `input_layernorm -> [mamba.in_proj, moe.experts.0.gate_proj,
   moe.experts.0.up_proj]`. The experts read `pre_moe_layernorm`'s output, so
   that fold was not output-preserving (measured 5.8e-2 / 8.2e-2 relative logit
   change in the report). The tree now declares `pre_moe_layernorm` and marks
   `mamba.in_proj` `:nosrc`, because `in_proj` feeds a conv/SSM/attention path
   that is not positively homogeneous; the previous tree only avoided that fold
   through a width mismatch.

Fixes #4891
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Closing as a duplicate of #5019, which is the maintainer's version of the same two fixes (created 22:27, before this one) and already builds on #5021. Same root causes and same corrected groups (input_layernorm -> [mamba.in_proj], pre_moe_layernorm -> [gate_proj, up_proj], up_proj -> [down_proj]); no reason to keep two branches for it.

@Qubitium Qubitium closed this Nov 17, 2026
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