Skip to content

fix(looper): resolve concurrent module-tree branches (#1998) - #4111

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-1998-parallel-modules
Nov 15, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-1998-parallel-modules

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #1998

TL;DR

Models with concurrent decoder siblings (model + vision_tower, thinker/talker pairs) can now be discovered and quantized in one run. The structural auto-detector returns one complete module_tree variant per stack instead of the first match, and the looper resolves each layer against the variant that owns its prefix. Single-branch model definitions resolve byte-identically. Unknown models stay fail-closed unless the caller explicitly passes module_tree="auto".

1. The problem

BaseQModel._auto_detect_module_tree() (gptqmodel/models/base.py:6053 on main @ c73793cea) probed a fixed candidate list and stopped at the first match:

for c in candidates:
    m = _get(c)
    if isinstance(m, (nn.ModuleList, list, tuple)) and ...:
        chosen = c
        break

The rest of the planner already understood multiple complete tree variants (extract_layers_node(), get_layers_with_prefixes(), get_base_modules()), but discovery could only ever produce one. The looper also applied one flat union of every variant's leaves to every layer, so a text layer was asked to resolve a vision-only leaf such as mlp.fc1 (gptqmodel/looper/module_looper.py:2125 / gptqmodel/looper/stage_subset.py:665 on main).

Old mechanism Location on main Replacement
Fixed candidate list, first match wins gptqmodel/models/base.py:6095-6110 Recursive structural walk over named_children()
No opt-in for an unregistered model gptqmodel/models/loader.py:2443-2450 Explicit module_tree="auto" argument
Flat union of all variant leaves gptqmodel/looper/stage_subset.py:665 ModuleLooper.layer_modules_for_prefix()
Global :! hook-skip set gptqmodel/looper/stage_layer.py:529 Per-branch :! set

2. The new flow

flowchart TD
    L["GPTQModel.load(..., module_tree='auto')"] --> FP["ModelLoader.from_pretrained()"]
    FP --> ML["HF model loaded on CPU"]
    ML --> A["_apply_requested_module_tree()"]
    A --> D["BaseQModel._auto_detect_module_tree()"]
    D --> W["_walk(named_children)"]
    W --> SEQ{"ModuleList / list / tuple?"}
    SEQ -->|no| DESC["descend into child"]
    DESC --> W
    SEQ -->|yes| LOOK{"known stack name<br/>or transformer-like block?"}
    LOOK -->|no| DESC
    LOOK -->|yes| LIN{"first block has<br/>Linear / Conv leaves?"}
    LIN -->|no| SKIP["log and skip this stack"]
    LIN -->|yes| ADD["record branch prefix"]
    ADD --> STOP["do not descend into the stack"]
    STOP --> W
    W --> DONE["branches collected"]
    DONE --> ONE{"branch count"}
    ONE -->|1| FLAT["return flat tree"]
    ONE -->|N| VAR["return N complete variants"]
    FLAT --> SET["model.gptqmodel_effective_module_tree"]
    VAR --> SET
    SET --> RES["_resolve_effective_module_tree()"]
    RES --> FREEZE["freeze tree per instance"]
    FREEZE --> PLAN["ModulePlan.from_tree()"]
    style SKIP fill:#fff1b8,stroke:#ad8b00
    style VAR fill:#d9f7be,stroke:#237804
Loading

A detected stack is a ModuleList/list/tuple whose first element owns nn.Linear/nn.Conv1d/nn.Conv2d leaves and either uses a known stack name (layers, h, blocks, block) or looks like a transformer block (attn, mlp, ffn, mixer, ...). The walk does not descend into a detected stack, so a nested expert ModuleList cannot become a false second decoder.

Each layer then uses its own branch declaration:

flowchart TD
    LN["layer name<br/>model.layers.0 / vision_tower.layers.0"] --> RES["ModuleLooper.layer_modules_for_prefix()"]
    RES --> V["iterate effective_module_tree variants"]
    V --> P{"layer name starts with<br/>variant prefix + '.'?"}
    P -->|no| NEXT["next variant"]
    NEXT --> V
    P -->|yes| LONG{"longest matching prefix"}
    LONG -->|one| SEL["select that variant"]
    LONG -->|several same prefix| UNION["return None:<br/>keep historical merged declaration"]
    SEL --> CTX["definition._module_tree_context(variant)"]
    CTX --> SIMPLE["simple_layer_modules()<br/>full_layer_modules()"]
    SIMPLE --> CACHE["cache by (prefix, planning)"]
    CACHE --> SUBSET["build_layer_subset_plans()"]
    SUBSET --> FULL["full names:<br/>layers_prefix + '.' + leaf"]
    FULL --> DYN["dynamic patterns / ignore globs resolve"]
    style UNION fill:#fff1b8,stroke:#ad8b00
    style DYN fill:#d9f7be,stroke:#237804
Loading

3. Before / after repro

Synthetic CPU model with two concurrent stacks: model.layers (2 text blocks) and vision_tower.layers (1 vision block). Text leaves are q_proj/o_proj + up_proj/down_proj; vision leaves are q_proj/o_proj + fc1/fc2.

Same script on origin/main @ c73793cea and on this branch:

$ diff -u detect_main.json detect_branch.json
--- detect_main.json
+++ detect_branch.json
@@
   "layer_names": [
     "model.layers.0",
-    "model.layers.1"
+    "model.layers.1",
+    "vision_tower.layers.0"
   ],
   "paths": [
-    "model.layers"
+    "model.layers",
+    "vision_tower.layers"
   ],

On main the detector returns the flat ["model", "layers", "#", {...}] tree. On this branch it returns two variants with full prefixes and branch-specific leaf maps.

Full CPU GPTQ quantization through the new module_tree="auto" entry point (CUDA and ROCm venvs both):

$ RUN_Q=1 PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python /tmp/tiny_parallel_model.py
QUANTIZED total=20 text=14 vision=6
model.layers.0.self_attn.q_proj
...
model.layers.1.mlp.down_proj
vision_tower.layers.0.self_attn.q_proj
...
vision_tower.layers.0.mlp.fc2

origin/main cannot run this path: it has no module_tree argument, and its detector emits only model.layers.

4. No-regression diff

Resolved extract_layers_node(), get_base_modules(), simple_layer_modules() and full_layer_modules() for two existing single-branch definitions, on origin/main @ c73793cea and on this branch:

$ diff -u no_regression_main.json no_regression_branch.json
$ echo $?
0

Both definitions resolve identically:

llama  extract_layers_node = ["model.layers"]
       get_base_modules    = ["model.embed_tokens", "model.norm", "model.rotary_emb"]
       simple_layer_modules = [
         ["self_attn.q_proj", "self_attn.k_proj", "self_attn.v_proj"],
         ["self_attn.o_proj"],
         ["mlp.gate_proj", "mlp.up_proj"],
         ["mlp.down_proj"],
       ]
qwen2  identical to llama

The same script was run on the ROCm venv; the only diff was a trailing newline.

5. Failure and ambiguity contract

flowchart TD
    A["module_tree='auto'"] --> B{"quantize_config present?"}
    B -->|no| E1["ValueError:<br/>method unknown"]
    B -->|yes| C{"unquantized from_pretrained path?"}
    C -->|no| E2["ValueError:<br/>explicit tree required for from_quantized"]
    C -->|yes| D["_auto_detect_module_tree()"]
    D --> F{"any branch resolved?"}
    F -->|no| E3["ValueError:<br/>no quantizable decoder branch"]
    F -->|yes| OK["attach multi-variant tree"]
    D --> G{"stack has no Linear/Conv?"}
    G -->|yes| SKIP["warn and skip that stack"]
    style E1 fill:#ffa39e,stroke:#a8071a
    style E2 fill:#ffa39e,stroke:#a8071a
    style E3 fill:#ffa39e,stroke:#a8071a
    style SKIP fill:#fff1b8,stroke:#ad8b00
    style OK fill:#d9f7be,stroke:#237804
Loading

Default behavior is unchanged: an unknown model without module_tree still raises ValueError and never guesses. module_tree="auto" is an explicit opt-in, not an environment switch.

6. Files changed

File Purpose
gptqmodel/models/base.py Multi-branch structural discovery; flat tree for one stack, N variants for N stacks.
gptqmodel/models/loader.py module_tree argument plumbing and explicit/auto tree attachment.
gptqmodel/models/auto.py GPTQModel.load / from_pretrained / from_quantized forward module_tree.
gptqmodel/looper/module_looper.py layer_modules_for_prefix() with per-branch caching.
gptqmodel/looper/stage_subset.py Use the owning branch's simple/full leaf blocks.
gptqmodel/looper/stage_layer.py Per-branch :! hook-skip set.
tests/module_tree/test_auto_detect.py Multi-branch discovery, prefixed layer resolution, per-branch leaves, fail-closed paths.
docs/parallel-module-trees.md New reference page with the discovery and per-branch diagrams.
docs/model-tree-resolution.md, README.md Document the opt-in and link the new page.

7. Tests

$ ruff check gptqmodel docs/eora tests setup.py
All checks passed!

$ PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
    tests/module_tree/test_auto_detect.py \
    tests/module_tree/test_module_tree_variants.py \
    tests/module_tree/test_explicit_module_plan.py \
    tests/module_tree/test_registered_module_plans.py \
    tests/test_tiny_moe_quant_smoke.py -q
458 passed, 1 warning

$ PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest \
    tests/module_tree/test_auto_detect.py -q
8 passed, 1 warning

tests/module_tree/test_auto_detect.py fails on origin/main (5 failures: the detector returns one branch and the new loader helper does not exist) and passes on this branch.

Pre-existing, not caused by this PR: tests/module_tree/test_definition_subset_ordering.py::test_consumer_is_quantized_after_its_producer[minimax-expected4] fails on origin/main @ c73793cea as well (verified in a clean worktree).

8. Not supported

  • Quantized checkpoints: module_tree="auto" requires the dense source model; from_quantized needs an explicit tree.
  • MoE leaf discovery: auto-detection maps direct transformer parents (self_attn, mlp, ...). Routed experts nested below those parents need an explicit :moe declaration.
  • Same-prefix alternatives: if several variants declare the same decoder prefix, the looper keeps the historical merged declaration rather than picking one alternative per layer.
  • Non-sequence stacks: a decoder exposed as a single module or nn.Sequential is not detected; declare it explicitly.

9. Validation hardware

CPU only on both venvs: /opt/venvs/cu130t/bin/python and /opt/venvs/rocm101t/bin/python. No GPU was used.

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.

How to quantize both vision encoder and llm together?

1 participant