Repository navigation
fix(quantize): replay each layer with its own calibration context (#4150) - #4153
Merged
Merged
Conversation
) Two replay paths reused a sibling layer's mask/kwargs: - Sequential TT block-float GPTQ pre-hooked only blocks[0] and replayed its attention_mask/position_embeddings/position_ids/cache_position through every block. On hybrid-attention models (Gemma2/3) a full-attention layer then calibrated on the sliding-window mask. Capture every block's side inputs from one dense forward and replay each block with its own; hidden states still propagate through the quantized blocks. - Parallel-branch calibration restored branch_first_layer_inputs only at a branch's first layer. Later layers read the top-level cache, which still held the first branch's kwargs, so e.g. vision_tower.layers.1 got the text decoder's attention_mask instead of its own cu_seqlens. Resolve the branch that owns layer_index (largest branch-first index <= layer_index) and take that branch's kwargs/masks/positions for every layer in it; only the first layer restarts from the branch's captured inputs. Tests assert on branch layer 1 (not just layer 0) and on a hybrid two-type model; single-stack and pure full-attention behavior is unchanged.
This was referenced Nov 16, 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 #4150
TL;DR
Two calibration replay paths gave a layer a sibling layer's mask/kwargs. Sequential TT block-float GPTQ replayed block 0's
attention_mask/rotary/positions through every block (hybrid attention); parallel-branch calibration restored a branch's cache only at its first layer, so later layers used the first branch's kwargs. Both now replay every layer with the context of the block/branch that owns it. 6 files changed (one new test file); single-stack and pure full-attention behavior is unchanged.1. The problem
A. Sequential TT block-float GPTQ reused block 0's context
_quantize_sequential(gptqmodel/utils/tt_blockfloat_quant.py:919, from #4009) pre-hooked onlyblocks[0], captured(args, kwargs), and then replayed that samekwargsthrough every block:transformers5 builds one causal mask perattention_typeand passescausal_mask_mapping[config.layer_types[i]]to each layer. Capturing only layer 0 therefore gave every full-attention layer the sliding-window mask (and vice versa), with no error. Pure full-attention models were unaffected because every layer receives the same mask.Observed on a tiny random Gemma2 (
layer_types=[sliding_attention, full_attention],sliding_window=4, seqlen 16), parent commit:B. Parallel-branch calibration restored context only at the branch's first layer
run_layer_stage(gptqmodel/looper/stage_layer.py:954, from #4126) looked upbranch_first_layer_inputsonly whenlayer_indexwas exactly a branch's first layer:receive_layer_inputs()refreshes only the top-levellayer_inputs;layer_input_kwargs,position_idsandattention_masksstay at the first branch's values. Later layers of a branch therefore replayed with the text decoder'sattention_maskinstead of the vision tower'scu_seqlens. The existing_VisionBlockignored kwargs, so this shipped.Observed on the repo fixture (
tests/module_tree/test_parallel_branch_calibration.py), parent commit:2. The new flow
A. Capture every block's side inputs, replay each block with its own
_quantize_sequentialnow pre-hooks every decoder block, stops at the last block, and keeps only the side inputs per block (gptqmodel/utils/tt_blockfloat_quant.py:950-999). Hidden states are propagated exactly as before: block 0's input is the only hidden tensor captured, and each quantized block's output becomes the next block's input. Shared tensors (mask, rotary tuple) are stored as references, not copied.flowchart TD W["calibration window"] --> F["dense backbone forward"] F --> H["pre-hook on every decoder block"] H --> C["captured extras i<br/>mask / rotary / position ids"] C --> L{"last block reached?"} L -->|no| F L -->|yes| STOP["raise _StopForward"] STOP --> Q["quantize block i"] Q --> R["replay block i with captured extras i<br/>hidden = previous quantized output"] R --> N{"more blocks?"} N -->|yes| Q N -->|no| DONE["report"] style STOP fill:#fff1b8,stroke:#ad8b00 style R fill:#d9f7be,stroke:#237804B. Resolve the branch that owns each layer
_branch_replay_cache()(gptqmodel/looper/stage_layer.py:79) returns the cache for the branch with the largest first-layer index at or beforemodel_layer_index. Every layer of that branch takes itslayer_input_kwargs,position_idsandattention_masks; only the branch's first layer restarts from the branch's capturedlayer_inputs, and later layers keep consuming the previous layer's output. Both the main replay path (stage_layer.py:958) and the packed catch-up path (stage_layer.py:808) use it.flowchart TD L["run_layer_stage<br/>flat layer_index"] --> OWN["_branch_replay_cache()<br/>largest branch-first index at or before layer_index"] OWN --> HAS{"branch cache?"} HAS -->|no| TOP["historical single-stack cache<br/>layer_inputs / kwargs / masks"] HAS -->|yes| FIRST{"layer_index equals<br/>the branch-first index?"} FIRST -->|yes| RESET["layer_inputs = branch captured inputs<br/>kwargs / masks / positions = branch cache"] FIRST -->|no| CONT["layer_inputs = previous layer output<br/>kwargs / masks / positions = branch cache"] TOP --> Q["replay the layer and collect Hessians"] RESET --> Q CONT --> Q style HAS fill:#fff1b8,stroke:#ad8b00 style RESET fill:#d9f7be,stroke:#237804 style CONT fill:#d9f7be,stroke:#237804Committed reference pages:
docs/parallel-module-trees.md(per-layer branch replay context) andhw/tenstorrent.md(sequential calibration context).3. Failure and ambiguity contract
sequential=Truesequential=True4. Change inventory
gptqmodel/utils/tt_blockfloat_quant.pygptqmodel/looper/stage_layer.py_branch_replay_cache(); use owning branch's context for every layer and in catch-up.tests/test_tt_blockfloat_sequential.py(new),tests/module_tree/test_parallel_branch_calibration.pydocs/parallel-module-trees.md,hw/tenstorrent.md5. Tests / guarantees
tests/test_tt_blockfloat_sequential.py::test_sequential_replay_uses_each_hybrid_layer_masktests/test_tt_blockfloat_sequential.py::test_sequential_replay_full_attention_context_unchangedtests/test_tt_blockfloat_sequential.py::test_sequential_replay_uses_each_layer_position_embeddingsposition_embeddings(RoPE), not layer 0's. Fails on parent.tests/module_tree/test_parallel_branch_calibration.py::test_parallel_branch_replays_each_layer_with_its_own_kwargsvision_tower.layers.1(index > 0) receivescu_seqlensand no textattention_mask. Fails on parent.tests/module_tree/test_parallel_branch_calibration.py::test_branch_replay_cache_resolves_the_owning_branchtests/module_tree/test_parallel_branch_calibration.py::test_single_branch_replay_context_is_unchanged6. Validation
All runs are CPU-only (no GPU used). Commands run from the clone root with
PYTHONPATH=$PWD.Part A, fail-before (parent
d62bc9afb):Part A, pass-after:
Part B, fail-before (parent
d62bc9afb):Part B, pass-after:
Related suites after merging
origin/main(git merge --no-edit origin/main: already up to date):ROCm venv (CPU execution,
HIP_VISIBLE_DEVICES=1):Common decoder path sanity: a tiny random Llama (
2layers,sequential=True) also quantizes cleanly; both layers captured and replayed their ownattention_mask,position_embeddingsandposition_ids(reporthas 14 entries).Full-attention no-regression (numerical): ran
quantize_model_blockfloat(sequential=True)on the same tiny full-attention Gemma2 on parent and on this branch and diffed the reports.diffreported no difference (FULL_ATTENTION_REPORTS_IDENTICAL), i.e. everynorm_l1/bits/on_gridentry is identical.Lint:
ruff check gptqmodel docs/eora tests setup.py # All checks passed!7. Cross-reference (not fixed here): AWQ token-row replay and all-zero additive masks
gptqmodel/looper/awq_processor.py:1521classifies an additive mask as:An all-zero additive
[B, 1, S, S]mask (0 = allowed, no negatives) takes the(m > 0)branch, which marks every position masked._token_row_source_structure()then returnsNone(flat_keep.sum() != row_count) and_token_row_replay_kwargs()falls back to a causal structure. That looks like a real bug for all-zero additive masks, but this file is owned by another worker in this batch, so it is only reported here.8. Reviewer notes / limitations
attention_typecapture could reduce it if a model ever needs it.