Skip to content

perf(awq): memoize the capture policy and skip losing reservoir merges (#5284) - #5295

Merged
Qubitium merged 1 commit into
mainfrom
fix/awq-moe-capture-perf-5284
Nov 19, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/awq-moe-capture-perf-5284

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

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 could
not displace a kept row.

Change

  1. _feature_aggregation_policy is memoized per
    (class, selector, module_name). BaseQModel.awq_input_feature_aggregation
    rebuilds the model's ModulePlan on every call, and the result is a pure
    function 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 fresh dict copy.
  2. A full reservoir short-circuits a losing capture. When the buffer is full
    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/arange chain. Strict comparison keeps ties on
    the old path, so the kept set is byte-identical.

Measurements (this host, CPU, interleaved old/new x3)

workload before after speedup
policy, 51,470 calls with the real OlmoeQModel selector 4.85 s 0.053 s 91x
reservoir, 48,882 captures x 30 rows into a 906-row cap 5.72 s 1.74 s 3.3x

The reservoir runs end with an identical buffer, priority, source and row
tensors in every repetition (identical=True), and
test_merge_matches_the_previous_algorithm pins that equivalence against the
previous 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.md
now documents the cost (roughly 5x the clip-search rows; _search_best_clip
12.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.topk is not called and the buffer is unchanged), a winning capture, a
tied 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 main since #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.

@Qubitium
Qubitium merged commit c03416c into main Nov 19, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/awq-moe-capture-perf-5284 branch November 19, 2026 00:02
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant