Skip to content

fix(granitemoehybrid): declare every checkpoint-owned decoder owner - #4674

Merged
Qubitium merged 1 commit into
mainfrom
fix/granitemoehybrid-plan-owners
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/granitemoehybrid-plan-owners

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

TL;DR

GraniteMoeHybridQModel.module_tree declared only mamba.in_proj/mamba.out_proj.
Every other checkpoint-owned tensor of the Mamba branch either had no plan entry
(mamba.conv1d, mamba.norm) or resolved through an untagged container
(mamba owning dt_bias/A_log/D), and the MoE router had no entry at all.
This PR declares those owners explicitly, adds a real-model plan/ownership test,
and removes the registry KNOWN_UNDECLARED exemption that hid the gap.

Two AWQ defects surfaced while validating the newly declared owners and are
fixed in the same declaration: a fold across the state-space path and a fold
whose replay boundary misses shared_mlp.

1. The problem

ModulePlan is the oracle for checkpoint-owned weights, but the registry sweep
in tests/module_tree/test_registry_leaf_coverage.py scanned supported
quantizable modules + expert parameters, not every state_dict() owner. It also
carried an explicit exemption:

tests/module_tree/test_registry_leaf_coverage.py:166 (before)
    "granitemoehybrid": "mamba.conv1d is checkpoint-owned but the definition is owned outside issue #4174",

A tiny real GraniteMoeHybridForCausalLM built on meta and converted through
_convert_model_with_defuser resolved owners as follows:

state_dict() item deepest owner before
mamba.conv1d.weight, mamba.conv1d.bias mamba.conv1d plan.find() → None
mamba.norm.weight mamba.norm plan.find() → None
mamba.dt_bias, mamba.A_log, mamba.D mamba entry exists, flags == () and quantize is True
block_sparse_moe.router.weight block_sparse_moe.router plan.find() → None
mamba.in_proj.weight, mamba.out_proj.weight projections declared and quantized

Old flow (gptqmodel/models/definitions/granitemoehybrid.py:29-42 on 42c5bca11):

flowchart TD
    A["granitemoehybrid module_tree"] --> B["mamba: in_proj, out_proj"]
    A --> C["post_attention_layernorm:!"]
    A --> D["block_sparse_moe:? → experts"]
    E["mamba.conv1d / mamba.norm modules"] --> F{"plan.find()"}
    G["mamba.dt_bias / A_log / D"] --> F
    H["block_sparse_moe.router.weight"] --> F
    F -- "no entry" --> X["undeclared owner<br/>no policy recorded"]
    F -- "container entry, flags ()" --> Y["reported as quantizable<br/>though it is a parameter owner"]
    style X fill:#f8d7da,stroke:#842029
    style Y fill:#fff3cd,stroke:#997404
Loading

Mechanisms replaced, with old locations:

Mechanism Location Effect
Mamba branch declared as projections only granitemoehybrid.py:29 conv1d/norm/container parameters never reached the plan
Untagged mamba container granitemoehybrid.py:29 dt_bias/A_log/D resolved with empty flags
No router declaration granitemoehybrid.py:32-38 router weight invisible to the plan
Registry exemption test_registry_leaf_coverage.py:166 the sweep never failed for this family

2. The new flow

flowchart TD
    T["module_tree"] --> M["mamba:!<br/>in_proj:0:nosrc, out_proj:1,<br/>conv1d:!, norm:!"]
    T --> N["post_attention_layernorm:!:nosrc"]
    T --> R["block_sparse_moe:moe:?<br/>router:!"]
    L["layer.state_dict()"] --> O["deepest owner"]
    O --> P["ModulePlan.find(owner)"]
    P --> Q{"policy flag?"}
    Q -- "quantize and in simple_layer_modules" --> OK["quantized projection"]
    Q -- "! or ?" --> DENSE["explicit dense owner"]
    Q -- "missing or no flag" --> FAIL["test fails with path + layers"]
    M --> P
    N --> P
    R --> P
    style FAIL fill:#f8d7da,stroke:#842029
    style DENSE fill:#d1e7dd,stroke:#0f5132
Loading

New declarations:

  • granitemoehybrid.py:34-45 — "mamba:!" container, in_proj:0:nosrc, out_proj:1, conv1d:!, norm:!.
  • granitemoehybrid.py:54 — post_attention_layernorm:!:nosrc.
  • granitemoehybrid.py:59 — "router:!": () under the :moe root.
  • test_registry_leaf_coverage.py:166 — exemption removed.

3. AWQ repairs found while validating the new owners

GraniteMoeHybridDecoderLayer.forward runs the Mamba mixer, then hands the
post-attention norm to block_sparse_moe and shared_mlp. The mixer splits
in_proj into gate / conv input / dt; the convolution and the SSM scan
consume those channels before the gated norm produces out_proj's input.

Measured with non-unit scales and no quantization, comparing full-model logits
against a fresh native baseline (tiny 2-layer model, FP32, eager Torch Mamba-2):

Group Before After
input_layernorm → mamba.in_proj 2.2e-7 kept (2.2e-7)
mamba.in_proj → mamba.out_proj 1.6e-1 across conv1d/scan dropped (in_proj:nosrc)
mamba.norm → mamba.out_proj unreachable (norm undeclared) kept, 1.6e-7
post_attention_layernorm → experts 1.5e-2, shared_mlp outside the MoE replay boundary dropped (post_attention_layernorm:nosrc)
experts.N.up_proj → experts.N.down_proj, shared_mlp.input_linear → output_linear ~1e-7 unchanged

The router already received the folded post-attention scale as a
_scale_consumers entry, so declaring it did not change that compensation. With
the source now nosrc, the routed/shared projections and the router are
quantized plainly.

docs/awq-scaling-groups.md records the same table and the flow diagram;
docs/module-tree-leaf-coverage.md records the removed exemption.

4. Failure and ambiguity contract

Situation Before After
conv1d / gated norm module plan.find() → None explicit conv1d:! / norm:!
dt_bias / A_log / D on the container empty flags, reported quantizable container declared mamba:!
MoE router weight no entry router:! under the :moe root
Registry sweep for this family xfail exemption runs and must pass
State-space fold emitted, changed logits by 1.6e-1 in_proj:nosrc; no group emitted
MoE-source fold emitted, boundary missed shared_mlp nosrc; projections quantize plainly
Unknown descendant (e.g. mamba.foo) — still rejected by plan.find()

5. Tests

tests/module_tree/test_granitemoehybrid_model_tree.py builds the installed
GraniteMoeHybridForCausalLM from a tiny config on meta, runs the same
_convert_model_with_defuser as the loader, and asserts:

Test node Invariant
test_every_checkpoint_tensor_owner_is_declared every state_dict() owner resolves, and dense owners carry :!/:?
test_mamba_branch_owners_are_explicit_and_dense mamba/conv1d/norm dense, projections quantized, container parameters are real nn.Parameters
test_moe_router_is_checkpoint_owned_and_unquantized router weight is a 2-D parameter, dense, inside the :moe root
test_mamba_projections_keep_their_declared_blocks simple_layer_modules still yields the two Mamba projection blocks
test_awq_does_not_fold_across_the_state_space_path exact emitted Mamba group set; no fold source on the SSM path
test_awq_post_attention_norm_is_not_a_scale_source no group from the post-attention norm; experts/shared projections still quantize
test_awq_folds_preserve_the_full_model_output non-unit fold into every usable source keeps full logits within 1e-5

The full-model fold test pins transformers' eager Torch Mamba-2 reference
(__wrapped__) because the optional fused kernels are CUDA-only; it does not
claim those kernels were exercised.

Also updated: tests/module_tree/fixtures/registered_module_plans.json,
tests/module_tree/test_moe_tags.py (router is now a declared child of the
:moe root), and .github/workflows/awq_cpu.yml (new test added to the AWQ CPU
suite).

6. Validation

Red evidence (only the definition reverted to 42c5bca11, new test kept):

python -m pytest tests/module_tree/test_granitemoehybrid_model_tree.py -q
6 failed, 1 passed
  test_every_checkpoint_tensor_owner_is_declared
  test_mamba_branch_owners_are_explicit_and_dense
  test_moe_router_is_checkpoint_owned_and_unquantized
  test_awq_does_not_fold_across_the_state_space_path
  test_awq_post_attention_norm_is_not_a_scale_source
  test_awq_folds_preserve_the_full_model_output   # 1.34e-2 > 1e-5

Green:

python -m pytest tests/module_tree/test_granitemoehybrid_model_tree.py \
                 tests/test_granitemoehybrid_monkeypatch.py -q
11 passed

python -m pytest tests/module_tree/test_registry_leaf_coverage.py \
                 tests/module_tree/test_registered_module_plans.py \
                 tests/module_tree/test_moe_tags.py \
                 tests/test_granitemoe_support.py -q
629 passed, 13 skipped, 3 xfailed

python -m pytest -q tests/module_tree
2363 passed, 192 skipped, 3 xfailed, 3 failed

The three failures (test_definition_subset_ordering.py::test_issue_4372_tiny_model_data_flow[nemotron_h],
test_fusion_kernel_backends.py::test_every_registered_gptq_awq_kernel_has_an_explicit_fusion_contract,
test_shared_input_cpu_forward.py::test_plan_matches_real_forward_inputs[jamba]) reproduce on
unmodified 42c5bca11 on the same host (CUDA-only mamba_ssm/Triton fallback
and an unregistered AWQ kernel), so they are not caused by this change.

AWQ baseline from the onboarding checklist: 577 passed.
Merged AWQ/fold/plan family run: 818 passed, 19 skipped, 3 xfailed.

A tiny end-to-end GPTQ quantize → save → reload run reproduced the defect
(mamba.conv1d stayed an unquantized Conv1d before the change) and, after the
change, keeps mamba.conv1d, mamba.norm and block_sparse_moe.router dense
while every declared projection is quantized.

7. Scope and limits

  • No checkpoint was downloaded. Ownership was checked against the architecture
    fixture plus the declared checkpoint conversion mappings
    (get_checkpoint_conversion_mapping("granitemoehybrid")), not against real
    checkpoint keys; runtime-only tensors are not treated as obligations.
  • The AWQ source/consumer conclusions cover this layout/config only. A different
    layer_types mix or a checkpoint with a different mamba_* layout is
    unverified.
  • The full-model fold invariant runs the eager Torch Mamba-2 reference; the
    fused mamba_ssm/causal_conv1d kernels are not qualified by this PR.

The module tree declared only mamba.in_proj/out_proj, so mamba.conv1d and
the gated mamba.norm failed ModulePlan.find(), and dt_bias/A_log/D resolved
through the untagged mamba container with no quantize/exclude policy. The
MoE router had no plan entry at all.

Declare the container, conv1d and norm as explicit dense owners and the
router under the :moe root. The new real-model plan test walks
state_dict() owners on a tiny GraniteMoeHybridForCausalLM and removes the
registry xfail for this family.

The declaration also records two AWQ repairs measured on the tiny model:
folding in_proj into out_proj crosses the convolution and SSM scan (1.6e-1
relative logit drift), and folding the post-attention norm for the routed
experts omits the shared_mlp consumer outside the MoE replay boundary
(1.5e-2). Both sources are marked nosrc so the projections quantize plainly;
out_proj folds the gated norm, which is output-neutral (1.6e-7).
@Qubitium
Qubitium merged commit 7395b40 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/granitemoehybrid-plan-owners branch November 17, 2026 09:30
Qubitium added a commit that referenced this pull request Nov 17, 2026
…4891) (#5019)

* fix(awq): replay a `**kwargs` block against the norm the layer owns (#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.

* ci(awq): run the Hymba AWQ regression in the unit suite

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.

* test(hymba): bind the required replay kwargs instead of bare reads (#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.

* test(module_tree): pin Hymba's corrected plan in the registered inventory

`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.

* test(hymba): validate the required replay kwargs explicitly

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.
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.

2 participants