Repository navigation
perf(awq): merge routed-expert reservoir captures in place (#5233) - #5236
Merged
Merged
Conversation
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.
This was referenced Nov 18, 2026
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.
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.
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.catof the whole kept buffer (up tobudget_caprows x hidden) plus the new rows,index_selectcopy of the result,provenancelist 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
reservoir_buffer) plus parallelreservoir_priorities/reservoir_sources/reservoir_rowstensors.topkoverused + newpriorities, 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._layer_input_features, so no per-capture Python tuple list is built.self.lockis not reentrant, so the module lock is created inside that critical section.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:
Tests
tests/test_awq_reservoir_incremental_5233.py: the buffer is reused in place andinputsis 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 passesrow_countsthrough; the 3 tests were failing on main withunexpected keyword argument 'row_counts'and now validate the provenance ->input_idsalignment against the new layout.CUDA_HOMEunset on this host).ruff checkclean.Fixes #5233