Skip to content

fix(module-tree): order consumers after producers and declare missing linears (#4041) - #4048

Merged
Qubitium merged 3 commits into
mainfrom
fix/issue-4041
Nov 14, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/issue-4041

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4041

TL;DR

Module-tree subset digits are a quantization order: the looper captures one subset block, then installs its quantized weights. Five definitions put a consumer in the same (or an earlier) subset as its producer, silently discarding sequential compensation. Two definitions left called nn.Linear modules undeclared. Six dense families gained verified :in= tags so Hessian dedup is active. Net: 16 files, two commits, no numeric code changes.

1. The problem

BaseQModel._build_layer_modules_for_tree groups leaves by their subset digit. A subset is processed atomically, so a consumer that shares a subset with its producer is calibrated and quantized against the producer's unquantized output.

flowchart LR
    subgraph bad["old: one subset"]
        P0["q_a_proj:0"] --> C0["q_b_proj:0"]
    end
    subgraph good["new: producer first"]
        P1["q_a_proj:0"] --> C1["q_b_proj:1"]
    end
    bad -. "consumer sees dense producer output" .-> X["lost compensation"]
Loading

Audited declarations (all file:line from the branch):

Item File Old New
1 gptqmodel/models/definitions/deepseek_v4.py:1447-1451 q_b_proj:0, o_b_proj:1, kv_proj:2 q_b_proj:1, o_b_proj:2, kv_proj:0
1 gptqmodel/models/definitions/deepseek_v2.py:37 q_a/q_b/kv_a/kv_b all :0 q_a/kv_a :0, q_b/kv_b :1, o_proj:2
1 gptqmodel/models/definitions/lfm2.py:27 conv: (in_proj, out_proj) conv: (in_proj:0, out_proj:1)
1 gptqmodel/models/definitions/falcon_mamba.py:23 x_proj:0, out_proj:1 x_proj:1, out_proj:2
1 gptqmodel/models/definitions/minimax_m2.py:90-91 out_proj:1, output_gate:1 output_gate:1, out_proj:2
2 gptqmodel/models/definitions/jamba.py:47 mamba: (in_proj:0, out_proj:1) x_proj:1 added; out_proj:2
2 gptqmodel/models/definitions/axk2.py:53-60 gated-norm MLPs and DSA indexer implicit explicit :! leaves

layer_modules_strict = False (Jamba, AXK2) makes an undeclared called linear silently stay in the source dtype. Jamba's mamba.x_proj is now quantized like Falcon Mamba's; AXK2's gated-norm MLPs and indexer linears are intentionally excluded but declared, so the exclusion is reviewable.

Before (origin/main @ e6b19c5)

deepseek_v4  q_a_proj(0) -> q_b_proj(0)      ordered=False
deepseek_v4  kv_proj(2)  -> o_b_proj(1)      ordered=False
deepseek_v2  q_a_proj(0) -> q_b_proj(0)      ordered=False
deepseek_v2  kv_a(0)     -> kv_b(0)          ordered=False
lfm2         in_proj(0)  -> out_proj(0)      ordered=False
falcon_mamba in_proj(0)  -> x_proj(0)        ordered=False
minimax      output_gate(1) -> out_proj(1)   ordered=False
jamba        mamba.x_proj not in any subset
axk2         input_layernorm.mlp.fc1/fc2 and indexer.* not in plan

After

deepseek_v4  q_a_proj(0) -> q_b_proj(1)      ordered=True
deepseek_v4  kv_proj(0)  -> o_b_proj(2)      ordered=True
deepseek_v2  q_a_proj(0) -> q_b_proj(1)      ordered=True
deepseek_v2  kv_a(0)     -> kv_b(1)          ordered=True
deepseek_v2  kv_b(1)     -> o_proj(2)        ordered=True
lfm2         in_proj(0)  -> out_proj(1)      ordered=True
falcon_mamba in_proj(0)  -> x_proj(1)        ordered=True
falcon_mamba x_proj(1)   -> out_proj(2)      ordered=True
minimax      output_gate(1) -> out_proj(2)   ordered=True
jamba        in_proj(0) -> x_proj(1) -> out_proj(2) ordered=True
axk2         every called nn.Linear resolves; excluded leaves carry `:!`

2. The new flow

flowchart TD
    A["definition module_tree"] --> B["BaseQModel._build_layer_modules_for_tree"]
    B --> C["subset blocks in ascending digit order"]
    C --> D["BaseQModel.simple_layer_modules"]
    D --> E["ModuleLooper forward + Hessian capture"]
    E --> F["quantize and install block"]
    F --> G{"more blocks?"}
    G -- yes --> E
    G -- no --> H["packed checkpoint"]
    D --> I["BaseQModel.shared_input_plan"]
    I --> J["same-subset followers reuse leader Hessian"]
    J --> E
Loading
flowchart TD
    A["layer.named_modules yields nn.Linear"] --> B{"plan.find name"}
    B -- no --> C["undeclared: silent source dtype"]
    B -- yes --> D{"spec.quantize"}
    D -- yes --> E["must be in a subset block"]
    D -- no --> F{"flags include ! or ?"}
    F -- yes --> G["explicit exclusion"]
    F -- no --> H["contract test fails"]
Loading

The committed reference page is docs/module-tree-ordering.md, linked from README.md next to the model-tree resolution section. It carries the same diagrams, the before/after table, and the failure contract.

3. Shared-input tags (item 3, additive only)

The issue listed siblings with bitwise-identical input and no :in= tag, so Hessian dedup stayed off. This PR covers a verified subset:

Family Tags added Verified model_type
opt q/k/v in=x opt
starcoder2 q/k/v in=x starcoder2
gptj q/k/v in=x gptj
phi q/k/v in=x phi
jamba q/k/v, dense + expert gate/up in=x jamba
lfm2 q/k/v, w1/w3 in=x lfm2

Safety argument, both halves tested:

  1. build_layer_modules emits byte-identical blocks with and without the tags (test_in_tags_do_not_change_quantization_blocks), so subset membership and order cannot change.
  2. probe_shared_inputs runs a real tiny forward and requires every group's inputs to be bitwise identical (test_plan_matches_real_forward_inputs).

A skipped follower Hessian is therefore the leader's Hessian, and the packed tensors are unchanged.

Left out deliberately: families whose native layout needs remote code (deepseek_v2, deepseek_v4, kimi_k25, ...) and parallel attention/MLP blocks whose leaves sit under different parents (falcon, gpt_neox, ...); tags are parent-scoped, so those need a different mechanism or their own real-forward case first.

4. Change inventory

Area Files Purpose
Subset order deepseek_v4.py, deepseek_v2.py, lfm2.py, falcon_mamba.py, minimax_m2.py producer before consumer
Coverage jamba.py, axk2.py declare x_proj; explicit :! leaves
Shared input opt.py, starcoder2.py, gptj.py, phi.py, jamba.py, lfm2.py verified :in= tags
Tests test_definition_subset_ordering.py (new), test_shared_input_cpu_forward.py ordering, coverage, real-forward, tag safety
Inventory registered_module_plans.json 7 changed cases, 21 lines
Docs docs/module-tree-ordering.md (new), README.md flow, contract, scope

5. Validation

Hardware: CPU-only plan/forward tests. CUDA venv cu130t (RTX 4090 GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e); ROCm venv rocm101t with HIP_VISIBLE_DEVICES=1. No GPU kernels were exercised; no timing run was needed.

Command Result
ruff check gptqmodel docs/eora tests setup.py All checks passed
pytest tests/module_tree/test_registered_module_plans.py tests/module_tree/test_definition_subset_ordering.py tests/module_tree/test_jamba_model_tree_consumption.py -q (CUDA) 431 passed
pytest tests/module_tree/test_shared_input_cpu_forward.py tests/module_tree/test_shared_input.py -q (CUDA) 519 passed
same ordering + registry selection (ROCm) 424 passed
6 new real-forward cases (ROCm) 6 passed
new tag-safety + dedup tests (ROCm) 12 passed

The new test_definition_subset_ordering.py fails on origin/main: it asserts the exact subset index per producer/consumer pair and that every called AXK2 nn.Linear resolves through the plan. It passes on this branch.

6. Reviewer notes

  • The inventory update is limited to the 7 affected cases; the fixture's existing compact formatting is preserved (21 changed lines).
  • Item 1 is silent, so no numerical before/after is claimed beyond the plan ordering; the dependency itself was confirmed by the issue's perturbation audit.
  • Item 3 is deliberately a coherent verified subset, not all ~50 families.

@Qubitium
Qubitium merged commit 1549116 into main Nov 14, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4041 branch November 14, 2026 18:35
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