Skip to content

perf(awq): merge routed-expert reservoir captures in place (#5233) - #5236

Merged
Qubitium merged 1 commit into
mainfrom
perf/awq-expert-reservoir-incremental-5233
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
perf/awq-expert-reservoir-incremental-5233

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

What

The dynamic token-row reservoir (routed MoE experts, cache-less runs) did all of this on every capture, under the processor-wide self.lock:

  • torch.cat of the whole kept buffer (up to budget_cap rows x hidden) plus the new rows,
  • an index_select copy of the result,
  • a rebuild of the Python provenance list of (source, row) tuples,
  • keep.sort(key=lambda index: provenance[index]) once the buffer was full.

That is O(budget x hidden) copying plus O(budget) Python work per expert, per module, per batch, serialized on one lock. On OLMoE-2L AWQ it took capture from 8.6 s to 73-123 s (quantize 43-54 s -> 164-312 s).

Change

  • Each module owns a fixed slot buffer (reservoir_buffer) plus parallel reservoir_priorities / reservoir_sources / reservoir_rows tensors.
  • A capture computes its priorities, runs one topk over used + new priorities, and writes only the surviving new rows into the slots freed by the rows it displaces (index_copy-style scatter). The kept buffer is never concatenated, copied or re-sorted.
  • Provenance stays in integer tensors during capture and is materialized once per layer in _layer_input_features, so no per-capture Python tuple list is built.
  • The merge runs under a per-module lock; the processor-wide lock now only covers O(1) bookkeeping. self.lock is not reentrant, so the module lock is created inside that critical section.
  • Rows captured before a module fell out of the token-row plan are seeded into the buffer, so a mixed plan/dynamic capture still keeps every batch.
  • Per-module locks are dropped when the layer is finalized.

The retention rule is unchanged: lowest uniform priority wins, so the sample stays a uniform reservoir of every row seen and memory stays O(budget).

Measurements

Synthetic capture benchmark (128 batches, 906-row cap, hidden 2048, CPU), same script on both trees:

before after
single module, 128 captures 448-500 ms 57-60 ms
8 modules concurrently, 128 captures each 6.9 s 0.79 s

Tests

  • New tests/test_awq_reservoir_incremental_5233.py: the buffer is reused in place and inputs is a view of it (no per-capture copy or provenance list); retained rows stay aligned with their provenance; a batch larger than the cap is handled; plan-captured rows survive the switch to the reservoir.
  • tests/test_awq_token_rows_bounded_capture_5137.py: provenance is now materialized for replay before the test reads it (assertions unchanged, 11 passed).
  • tests/test_awq_hash_moe_kept_rows_5015.py: fixed the spy signature so the test passes row_counts through; the 3 tests were failing on main with unexpected keyword argument 'row_counts' and now validate the provenance -> input_ids alignment against the new layout.
  • Broader CPU AWQ sweep (token rows, hash MoE, GPT-Neo replay, MoE root capture, position kwargs, shift capture, balanced/starved/shared MoE, HF CPU, GPTQ lifecycle): 79 passed, 4 skipped. The only failures are pre-existing CUDA-JIT tests (CUDA_HOME unset on this host).
  • ruff check clean.

Fixes #5233

The dynamic token-row reservoir concatenated, re-sorted and re-listed the
whole kept buffer on every capture while holding the processor-wide lock, so
AWQ quantize of MoE models was 3-6x slower than before #5175 (capture
bookkeeping 8.6 s -> 73-123 s on OLMoE-2L).

Each module now owns a fixed slot buffer with parallel priority/source/row
tensors. A capture writes only the surviving new rows into the slots freed by
the rows it displaces, under a per-module lock, and provenance is materialized
once per layer. Rows captured before a module fell out of the token-row plan
are seeded into the buffer so mixed plan/dynamic captures keep every batch.

Synthetic capture benchmark (128 batches, 906-row cap, hidden 2048):
448-500 ms -> 57-60 ms; 8 concurrent modules 6.9 s -> 0.79 s.
@Qubitium
Qubitium merged commit 0cf4d8e into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the perf/awq-expert-reservoir-incremental-5233 branch November 18, 2026 12:58
Qubitium added a commit that referenced this pull request Nov 18, 2026
#5267) (#5293)

`_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