Repository navigation
fix(looper): capture calibration inputs per parallel branch (#4116) - #4126
Merged
Merged
Conversation
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.
Part of #4116 (item 1)
Root cause
#4111 made
module_tree="auto"return one variant per concurrent stack(
model.layers,vision_tower.layers), but the looper still treated theflattened variants as one sequential stack:
gptqmodel/looper/module_looper.py:1872callsget_layers_with_prefixes(),which concatenates every variant's layers into a single list.
gptqmodel/looper/stage_inputs_capture.py:155(first_layer = layers[0])hooked only the first TEXT layer and raised
StopForward, so no later branchever executed during capture.
gptqmodel/looper/stage_layer.py:911(and the catch-up path at:1293) thenreplayed each layer on the previous layer's output.
Result:
vision_tower.layers.0was calibrated on the text decoder's lasthidden state, with the text attention mask/position ids. No warning. A real
vision input was never captured. On a width mismatch the run died mid-quantize
with a shape error instead.
Fix
Capture calibration inputs per branch and restart the replay chain at each
branch boundary.
gptqmodel/looper/stage_inputs_capture.py_branch_first_layer_indices(layer_names)derives branch boundaries fromthe prefix change in the flattened layer names.
layers[0]— unchanged.branch's first layer, run the full calibration forward, capture each
branch's
layer_inputs/attention_masks/position_ids/layer_input_kwargs, and stop once all branches are captured.ValueErrornaming the branch and batch instead of calibrating it onanother branch's activations.
gptqmodel/looper/stage_layer.pyInputCache.branch_first_layer_inputs[layer_index]and restore thatbranch's captured inputs/kwargs/position ids/masks at a branch boundary;
subsequent layers in the branch continue from the branch's own output.
gptqmodel/looper/input_cache.pybranch_first_layer_inputsfield;cleanup()clears branch caches.docs/parallel-module-trees.mdcontract, cost, and unsupported cases.
docs/model-tree-resolution.mdtests/module_tree/test_parallel_branch_calibration.py(new)vision_tower.layerswithself_attn.{q,k,v,o}_projandmlp.fc1/fc2, 8 calibration rows,module_tree="auto"; parametrized overvision_hidden=[896, 512].Before / after repro
Reporter-style synthetic model: 2 text layers + 2 ViT blocks, 8 calibration
rows,
module_tree="auto".pixel_valuesshape[1,8,16]; vision hidden width896 vs 512 (text width 896).
4000d2ae5VISION_H=896error: None; vision inputs recorded 24; 24/24 equal a text last-layer output; 12 vision + 14 text modules quantizederror: None; vision inputs recorded 32; 0/32 equal a text last-layer output; 12 vision + 14 text modules quantizedVISION_H=512RuntimeError: mat1 and mat2 shapes cannot be multiplied (6x896 and 512x512); 1 vision input recorded (= text output); 0 vision modules quantizederror: None; 0/32 equal a text last-layer output; 12 vision + 14 text modules quantizedpixel_valuesValueError: ModuleLooper: the calibration forward never reached the first layer of module-tree branch(es) ['vision_tower.layers.0'] for calibration batch 1. Refusing to calibrate a branch on another branch's activations. ...Real output,
VISION_H=896(this branch):Real output,
VISION_H=512(this branch):Real output, unreachable branch (this branch):
The new test file fails 3/3 on
origin/mainand passes 3/3 here.Per-branch capture and replay
flowchart TD G["get_layers_with_prefixes()"] --> B["_branch_first_layer_indices(layer_names)"] B --> N{"branch count"} N -->|1| OLD["historical path:<br/>hook layers[0], early stop"] N -->|N| MAT["materialize every decoder layer"] MAT --> H["pre-hook each branch first layer"] H --> F["run full calibration forward"] F --> C{"all branch first layers captured?"} C -->|no| ERR["ValueError:<br/>branch never reached<br/>name branch + batch"] C -->|yes| CACHE["InputCache.branch_first_layer_inputs"] CACHE --> REPLAY["run_layer_stage()"] REPLAY --> R{"layer is a branch first layer?"} R -->|yes| RESET["restore branch layer_inputs / kwargs / masks"] R -->|no| PREV["use previous layer output"] RESET --> Q["quantize branch layers"] PREV --> Q style ERR fill:#ffa39e,stroke:#a8071a style CACHE fill:#d9f7be,stroke:#237804No-regression
Single-branch resolution is unchanged. I dumped
extract_layers_node,get_base_modules,simple_layer_modules,full_layer_modulesforLlamaQModelandQwen2QModelonorigin/mainand on this branch and diffedthem:
Registered multi-variant trees (
HrmTextQModel,MLlamaQModel,Qwen2_5_OmniGPTQ,Qwen3OmniMoeGPTQ):extract_layers_node+ModulePlan.from_tree(module_tree).to_dict()diff between main and this branch:These are layout alternatives with a single live stack, so they take the
single-stack path and are unaffected.
Tests
Pre-existing failure, unrelated and present on
origin/maintoo:tests/module_tree/test_definition_subset_ordering.py::test_consumer_is_quantized_after_its_producer[minimax-expected4].Hardware: CPU only. No GPU used.
Unsupported / limits
hard error. There is no silent fallback and no way to calibrate it without
calibration data that exercises that branch.
historical merged declaration rather than picking an alternative (fix(looper): resolve concurrent module-tree branches (#1998) #4111
behavior, unchanged).
are materialized and the full forward runs once per calibration batch, so
peak memory and per-step time are higher for concurrent models.
captured once per batch at the first invocation.
GPTQMODEL_*environment switch.