Skip to content

fix(awq): key the sibling capture caches on live tensors, not bare ids (#5267) - #5293

Merged
Qubitium merged 1 commit into
mainfrom
fix/5267-awq-sibling-key
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5267-awq-sibling-key

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

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_features dedupes sibling captures (q/k/v,
gate/up) with cat_cache and token_row_kwargs_cache, keyed on
tuple(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_features drops that view
when the next iteration rebinds tensors, so CPython reuses its address for
the next module's view. The id() key then matches and the earlier module's
cat_cache rows and token_row_kwargs_cache packed mask/positions/provenance
are handed to the wrong module; entry["inputs"] = [features[name]] drops the
module's own rows.

When the widths match (all gate_proj/up_proj share hidden) this is
silent; a down_proj receiving an up_proj capture fails validation and the
group 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:

  • a dead reference proves the id() was free to be reused, so the entry is a
    miss and the payload is recomputed;
  • a live reference that is not the current tensor is impossible for a live
    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"]
Loading

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 --> K
Loading

This 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.py

Test Invariant
test_reservoir_routed_and_shared_experts_keep_their_own_rows 32 routed + shared expert modules on the dynamic reservoir replay exactly their own rows and row count.
test_reservoir_replay_kwargs_stay_aligned_with_each_modules_rows Packed position_ids/attention_mask length matches the module's own retained rows.
test_reservoir_cap_keeps_rows_aligned_with_their_provenance Over-cap reservoir retains exactly cap rows, each matching its (source, row) provenance, with no duplicate slot.
test_plan_path_siblings_still_share_one_capture q/k/v recorded from one activation object still share one tensor and one kwargs dict (control).

Red on pristine 0aa2d4b48 (fix reverted, .scratch/red_tests_main.log):

2 failed, 2 passed
AssertionError: 3/32 modules got another module's rows:
  [('mlp.experts.1.gate_proj', [1.0], 12, 13),
   ('mlp.experts.5.gate_proj', [13.0], 13, 9),
   ('mlp.experts.9.up_proj', [26.0], 12, 13)]
AssertionError: 1/12 modules got another module's replay kwargs:
  [('mlp.experts.4.gate_proj', 'rows', [6.0])]

Issue repro (.scratch/repro_5267.py, 5 runs on pristine main):
2/16 modules got another module's rows; after the fix 0/16 (3 runs).

Validation

uvx [email protected] check gptqmodel docs/eora tests setup.py   # All checks passed
python -m pytest -q -rs tests/test_awq_reservoir_sibling_rows_5267.py \
  tests/test_awq_reservoir_incremental_5233.py \
  tests/test_awq_token_rows_bounded_capture_5137.py \
  tests/test_awq_hash_moe_kept_rows_5015.py                # 22 passed
python -m pytest -q -rs tests/test_awq_moe_shared_expert_group_4760.py \
  tests/test_awq_balanced_moe_5104.py \
  tests/test_awq_balanced_moe_group_5104.py                # 8 passed, 5 skipped

Full tests/test_awq_*.py selection on CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=1: TBD.

Unverified

  • Real-checkpoint Repro 2 from the issue (OLMoE, Qwen1.5-MoE, MiniMax) is not
    rerun; the deterministic CPU repro exercises the same cache path.
  • Tests skipped by the two-device requirement or unavailable offline
    checkpoints are unverified, not qualified.

#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant