Repository navigation
fix(awq): key the sibling capture caches on live tensors, not bare ids (#5267) - #5293
Merged
Merged
Conversation
#5267) `_layer_input_features` dedupes sibling captures (q/k/v, gate/up) with `cat_cache` and `token_row_kwargs_cache`, keyed on `tuple(id(t) for t in tensors)`. Before #5236 the keyed tensors stayed alive in `entry["inputs"]` for the whole run; #5236 replaced a routed expert's replay input with a fresh temporary view (`buffer[:used].unsqueeze(0)`), which the loop drops when it rebinds `tensors`. CPython then reuses that address for the next module's view, so the `id()` key matched and one module received another's concatenated rows and packed mask/positions. When the widths matched (all gate/up share `hidden`) this was silent; a `down_proj` receiving an `up_proj` capture failed validation and the group fell back to plain quantization. Keep the `id()` fast path but store `(weakrefs, payload)` per key: a hit requires every weak reference to still resolve to the same live object. A dead reference proves the id was reusable, so it is a miss and the payload is recomputed. Only weak references are retained, so reservoir buffers are not pinned for the rest of the layer, and #5236's in-place merge is unchanged.
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.
Summary
Fixes #5267. One source file plus one regression test file; the AWQ capture
and replay flow is unchanged. #5236's in-place reservoir merge is preserved:
capture still copies only the surviving rows into the fixed buffer, and replay
still materializes one view per module.
Root cause
AWQProcessor._layer_input_featuresdedupes sibling captures (q/k/v,gate/up) with
cat_cacheandtoken_row_kwargs_cache, keyed ontuple(id(tensor) for tensor in tensors).Before #5236 the keyed tensors lived in
entry["inputs"]for the whole run.#5236 changed a routed expert's replay input to a fresh temporary view,
buffer[:used].unsqueeze(0), installed by_materialize_reservoir_provenance._layer_input_featuresdrops that viewwhen the next iteration rebinds
tensors, so CPython reuses its address forthe next module's view. The
id()key then matches and the earlier module'scat_cacherows andtoken_row_kwargs_cachepacked mask/positions/provenanceare handed to the wrong module;
entry["inputs"] = [features[name]]drops themodule's own rows.
When the widths match (all
gate_proj/up_projsharehidden) this issilent; a
down_projreceiving anup_projcapture fails validation and thegroup falls back to plain quantization (the visible symptom in the issue).
Fix
The caches keep the
id()fast path but store(weakrefs, payload)per key.A hit requires every weak reference to still resolve to the same live tensor
object:
id()was free to be reused, so the entry is amiss and the payload is recomputed;
id()and is rejected defensively.Only weak references are retained, so the reservoir buffers are not pinned
for the rest of the layer (a strong-reference cache would roughly double the
peak host memory of a large MoE replay pass). Genuine sharing is unchanged:
the plan-path sampled tensors and batch-mode host tensors are the same objects
across siblings, so those lookups still hit.
Before
flowchart TD A["for name in feature_names"] --> B["entry = tasks[name]"] B --> C{"reservoir_buffer?"} C -->|yes| D["materialize fresh view<br/>buffer[:used].unsqueeze(0)"] C -->|no| E["inputs = captured tensors"] D --> F["key = tuple of id tensor"] E --> F F --> G{"cache key present?"} G -->|"yes: previous view already freed,<br/>its id recycled"| H["reuse previous module's<br/>rows and replay kwargs"] G -->|no| I["cat and rebuild kwargs,<br/>store under key"] H --> J["entry.inputs = wrong rows<br/>own rows dropped"] I --> K["entry.inputs = own rows"]After
flowchart TD A["for name in feature_names"] --> B["entry = tasks[name]"] B --> C{"reservoir_buffer?"} C -->|yes| D["materialize fresh view<br/>buffer[:used].unsqueeze(0)"] C -->|no| E["inputs = captured tensors"] D --> F["key = tuple of id tensor"] E --> F F --> G["cached = cache key entry<br/>weakrefs plus payload"] G --> H{"every weakref still<br/>resolves to the same live tensor?"} H -->|yes| I["reuse rows and replay kwargs<br/>genuine sibling share"] H -->|"no: stale entry or recycled id"| J["cat and rebuild kwargs,<br/>store key with weakrefs"] I --> K["entry.inputs = own rows"] J --> KThis is a local cache-lifetime predicate fix: control flow, capture merging,
selection order and the public replay contract are unchanged, so no
docs/mechanism page is added.Tests
tests/test_awq_reservoir_sibling_rows_5267.pytest_reservoir_routed_and_shared_experts_keep_their_own_rowstest_reservoir_replay_kwargs_stay_aligned_with_each_modules_rowsposition_ids/attention_masklength matches the module's own retained rows.test_reservoir_cap_keeps_rows_aligned_with_their_provenancecaprows, each matching its(source, row)provenance, with no duplicate slot.test_plan_path_siblings_still_share_one_captureRed on pristine
0aa2d4b48(fix reverted,.scratch/red_tests_main.log):Issue repro (
.scratch/repro_5267.py, 5 runs on pristine main):2/16 modules got another module's rows; after the fix0/16(3 runs).Validation
Full
tests/test_awq_*.pyselection onCUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=1: TBD.Unverified
rerun; the deterministic CPU repro exercises the same cache path.
checkpoints are unverified, not qualified.