Repository navigation
perf(awq): memoize the capture policy and skip losing reservoir merges (#5284) - #5295
Merged
Merged
Conversation
This was referenced Nov 19, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 19, 2026
…) (#5327) #5295 memoized the capture policy and skipped merges that cannot enter, but two costs remained on the routed-expert path: * every routed expert kept the layer-wide plan budget (906 rows on OLMoE) instead of the pre-#5175 per-expert rule (max(largest capture, captures); mean 254, median 203), so the clip search and the replay forward ran on ~3.6x the rows. The reservoir still keeps the lowest priorities, so the kept set stays a uniform sample of the stream. * a growing budget re-seeded from ``inputs``, which cannot work because provenance is materialized only at replay, so growth silently dropped every kept row. Carry the rows the buffer already holds. * the merge concatenated the kept priorities with the capture and wrote every surviving captured row into a slot. Select the drop set from the ``overflow`` largest kept priorities plus the capture and write only the surviving captured rows. Accuracy on full 16-layer OLMoE-1B-7B (128 C4 docs, batch_size=1, RTX 4090): PPL 11.2870 with the layer-wide cap vs 11.2814 with the per-expert rule, i.e. no measurable loss, and quantize 655-690 s -> 490 s (measured with the rule monkeypatched on main). On a 7-layer truncation the whole change is 997 s -> 541 s. Tests: tests/test_awq_moe_quant_speed_5284.py (6, CPU-only).
This was referenced Nov 19, 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.
Addresses #5284.
Problem
After #5236 removed the buffer copy, a 2-layer MoE AWQ run still spent about
3.4 s rebuilding the aggregation policy on every capture (51k calls) and 8.6 s
in
_merge_reservoir_rows(48.9k calls, ~160 us each), even when a capture couldnot displace a kept row.
Change
_feature_aggregation_policyis memoized per(class, selector, module_name).BaseQModel.awq_input_feature_aggregationrebuilds the model's
ModulePlanon every call, and the result is a purefunction of the class and the name. The key carries the selector's own
identity, so a monkeypatched/replaced selector is never answered from the
previous one's cache, and the memo keeps a strong reference to the model it
was resolved for (no
id()reuse). Callers still get a freshdictcopy.and every new priority is strictly greater than the worst kept priority, no
row can survive; the merge now returns without the
cat/topk/mask/nonzero/arangechain. Strict comparison keeps ties onthe old path, so the kept set is byte-identical.
Measurements (this host, CPU, interleaved old/new x3)
OlmoeQModelselectorThe reservoir runs end with an identical buffer, priority, source and row
tensors in every repetition (
identical=True), andtest_merge_matches_the_previous_algorithmpins that equivalence against theprevious merge body over randomized captures.
The routed-expert cap (item 3 of the issue)
Not changed here: sizing every routed expert from the dense layer-wide budget
(906 rows vs the pre-#5175 ~185) is an accuracy decision, and the issue asks for
that decision rather than a silent shrink.
docs/awq-calibration-aggregation.mdnow documents the cost (roughly 5x the clip-search rows;
_search_best_clip12.9 s -> 68.5 s in the issue's contended run) and the existing per-module knob:
awq_input_feature_aggregation(...)returning{"mode": "token_rows", "max_tokens": N}is honoured by both the plan and the reservoir.Tests
New
tests/test_awq_capture_perf_5284.py(13 nodes): policy memoization(including a replaced model and a patched selector), copy-on-return, the
non-mapping error, missing selector, the losing-capture short-circuit (asserts
torch.topkis not called and the buffer is unchanged), a winning capture, atied priority, row-index/provenance preservation, and the old-vs-new equivalence
over 40 randomized captures x 3 seeds. The memo and short-circuit nodes fail on
main.Also fixes a stale expectation in
tests/test_awq_weight_mean.py::test_awq_pack_token_rows_ignores_empty_captures_during_quota_allocation,which is red on
mainsince #5265 moved single-row quotas to the batch centre(the assertion now checks the centre row; the empty-capture guarantee it exists
for is unchanged).
Relevant suites: 190 passed, 1 skipped.