Repository navigation
fix(looper): resolve concurrent module-tree branches (#1998) - #4111
Merged
Merged
Conversation
This was referenced Nov 15, 2026
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 #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 completemodule_treevariant 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 passesmodule_tree="auto".1. The problem
BaseQModel._auto_detect_module_tree()(gptqmodel/models/base.py:6053onmain@c73793cea) probed a fixed candidate list and stopped at the first match: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 asmlp.fc1(gptqmodel/looper/module_looper.py:2125/gptqmodel/looper/stage_subset.py:665onmain).maingptqmodel/models/base.py:6095-6110named_children()gptqmodel/models/loader.py:2443-2450module_tree="auto"argumentgptqmodel/looper/stage_subset.py:665ModuleLooper.layer_modules_for_prefix():!hook-skip setgptqmodel/looper/stage_layer.py:529:!set2. 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:#237804A detected stack is a
ModuleList/list/tuple whose first element ownsnn.Linear/nn.Conv1d/nn.Conv2dleaves 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 expertModuleListcannot 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:#2378043. Before / after repro
Synthetic CPU model with two concurrent stacks:
model.layers(2 text blocks) andvision_tower.layers(1 vision block). Text leaves areq_proj/o_proj+up_proj/down_proj; vision leaves areq_proj/o_proj+fc1/fc2.Same script on
origin/main@c73793ceaand on this branch:On
mainthe 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):origin/maincannot run this path: it has nomodule_treeargument, and its detector emits onlymodel.layers.4. No-regression diff
Resolved
extract_layers_node(),get_base_modules(),simple_layer_modules()andfull_layer_modules()for two existing single-branch definitions, onorigin/main@c73793ceaand on this branch:Both definitions resolve identically:
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:#237804Default behavior is unchanged: an unknown model without
module_treestill raisesValueErrorand never guesses.module_tree="auto"is an explicit opt-in, not an environment switch.6. Files changed
gptqmodel/models/base.pygptqmodel/models/loader.pymodule_treeargument plumbing and explicit/auto tree attachment.gptqmodel/models/auto.pyGPTQModel.load/from_pretrained/from_quantizedforwardmodule_tree.gptqmodel/looper/module_looper.pylayer_modules_for_prefix()with per-branch caching.gptqmodel/looper/stage_subset.pysimple/fullleaf blocks.gptqmodel/looper/stage_layer.py:!hook-skip set.tests/module_tree/test_auto_detect.pydocs/parallel-module-trees.mddocs/model-tree-resolution.md,README.md7. Tests
tests/module_tree/test_auto_detect.pyfails onorigin/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 onorigin/main@c73793ceaas well (verified in a clean worktree).8. Not supported
module_tree="auto"requires the dense source model;from_quantizedneeds an explicit tree.self_attn,mlp, ...). Routed experts nested below those parents need an explicit:moedeclaration.nn.Sequentialis not detected; declare it explicitly.9. Validation hardware
CPU only on both venvs:
/opt/venvs/cu130t/bin/pythonand/opt/venvs/rocm101t/bin/python. No GPU was used.