Skip to content

perf(awq): merge only a full reservoir capture's winners (#5324) - #5328

Closed
Qubitium wants to merge 1 commit into
mainfrom
fix/awq-reservoir-winners-prefilter-5324
Closed

Qubitium wants to merge 1 commit into
mainfrom
fix/awq-reservoir-winners-prefilter-5324

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #5324.

Problem

#5295's all-or-nothing short-circuit only skips a full-reservoir merge when every new row loses to the worst kept priority. On a real routed expert that is rare — each of a capture's ~75 rows survives with probability about cap / seen, so all-losing is under 1% of full-reservoir merges. OLMoE-2L stayed at 48,882 merges / 7.54 s.

#5327 (merged after this branch started) already replaced the full cap + count concat/topk with a drop-set search, but it still ranks the whole capture on every merge. This branch is rebased onto current main (67de3469) and keeps #5327's drop-set merge as the fallback path.

Fix

Build on #5327's drop-set merge with the per-row prefilter the issue asks for:

  • A full reservoir computes winners = priorities < kept.max() (strict, so a tie keeps the kept row) and skips the merge when there are none.
  • Exactly len(winners) rows can leave, and they are the largest of (len(winners) largest kept rows + winners). The drop/insert therefore runs on 2 * len(winners) candidates instead of 2 * count, and only the surviving new rows are copied into the freed slots.
  • used < budget_cap (the one-time crossing) and a capture with at least as many winners as kept rows keep perf(awq): size routed-expert reservoirs from their own captures (#5284) #5327's generalized drop-set path unchanged.
  • The kept set is identical to the full concat/topk merge: every non-winner loses to cap kept rows. Ties at the boundary keep the kept row (the old topk tie order was unspecified).

CPU-side only; device and provenance handling are unchanged.

Measurements

The issue's CPU repro (64 experts x 128 captures, rows ~N(75, 30), cap 906, hidden 2048, bf16, torch.set_num_threads(1)), time.process_time(), one process per tree, this container's CPU (the reporter's CPU is ~1.8x faster; the ratio is the point). full_few/full_many split by the number of winners (<16 / >=16) before the call:

tree full merges all-losing skips full_few total / median full_many total / median full total
35d97a79 (pre-#5327) 7,368 67 0.864 s / 206.6 us 0.791 s / 241.5 us 1.655 s
92276f43 (current main, #5327) 7,368 67 0.574 s / 134.5 us 0.588 s / 172.4 us 1.162 s
this PR 7,368 67 0.469 s / 109.5 us 0.534 s / 154.9 us 1.003 s

That is -14% vs current main and -39% vs pre-#5327, with the same call count and the same all-losing skips. The winning path's remaining dominant cost is the copy of the surviving rows (a 5-row bf16 scatter is ~13 us against ~132 us for 75 rows).

I did not re-run the OLMoE-2L wall clock in this environment; the CPU simulation is the same measurement the issue used to size the cost.

Also repairs current main's capture-perf tests

#5327's merge makes two torch.topk calls and fills the same kept set into different buffer slots, so on current main these fail:

  • test_a_winning_capture_still_merges (asserts exactly one topk call)
  • test_tied_priorities_take_the_merge_path (asserts exactly one topk call)
  • test_merge_matches_the_previous_algorithm[2] (compares the buffer elementwise)

This PR updates them: assert calls, a tie now skips the merge and keeps the kept row, and the equivalence oracle canonicalizes by (source, row) before comparing (the method docstring says the buffer layout is deliberately not a contract). The kept rows, priorities, sources and row indices are still compared exactly.

Tests

Docs: docs/awq-calibration-aggregation.md now describes the per-row prefilter and pins the new test nodes.

@Qubitium

Copy link
Copy Markdown
Collaborator Author

Superseded by #5327 (merged).

#5327 landed the same winners-only merge for _merge_reservoir_rows (drop set = top overflow of the largest kept rows + the capture; write only the surviving captured rows) together with the per-expert reservoir budget, and it is now on main. That makes this branch redundant and it is currently dirty against main.

Closing as superseded; the behaviour is covered by tests/test_awq_moe_quant_speed_5284.py and docs/moe-awq-calibration.md section 4. Reopen if you think a piece here is not in #5327.

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