Skip to content

[PERF] #5295 (fix of #5284) incomplete: the full-reservoir 'all rows lose' short-circuit fires on <1% of real routed-expert captures - OLMoE-2L _merge_reservoir_rows still 48,882 calls / 7.54 s (7.55 s before) #5324

Closed

Description

@Qubitium

Found on main @ c03416c, RTX 4090 (torch 2.15), bf16.

What is wrong

#5295 (fix of #5284, item 2) added a short-circuit to AWQProcessor._merge_reservoir_rows (gptqmodel/looper/awq_processor.py ~L1521-1535). When the reservoir is full and every new row's priority is above the current maximum kept priority, it returns early and skips the cat/topk/mask/nonzero/arange merge. The PR and the new docs section say "a full reservoir usually rejects the whole capture", which would remove about 160 us per capture (8.6 s per OLMoE-2L run).

On a real routed expert this condition almost never holds. Each capture brings tens of rows: OLMoE top-8 of 64 with 128 C4 documents gives about 75 rows per expert per batch. Once an expert has seen n rows, each new row survives with probability about 906 / n. The chance that all ~75 rows lose is (1 - 906/n)^75, which is below 0.1% at the end of the layer and far smaller earlier. So the slow path runs on nearly every capture, exactly as before.

Measurements

1. Real run. OLMoE-1B-7B-0924 truncated to 2 layers (config copy), AWQConfig(bits=4, group_size=128), bf16, 128 C4 documents (2500 chars), bs=1, single GPU. Every number is the summed time.perf_counter inside the method (same wrapper on all trees), one process per tree, run back to back:

tree quantize wall _merge_reservoir_rows calls / total _feature_aggregation_policy _record_input_feature total _search_best_clip
0b1aea0f5^ (pre-#5175) 82.4 s n/a 1,308 calls / 0.09 s 12.7 s 11.8 s
c1807f889 (before #5295) 149.3 s 48,882 / 7.55 s 51,470 / 3.65 s 24.7 s 56.4 s
main c03416c 133.9 s 48,882 / 7.54 s 51,470 / 0.17 s 20.3 s 58.6 s

The policy memo (item 1) works (3.65 s -> 0.17 s). The merge short-circuit (item 2) saves nothing: the call count and the total are unchanged. _record_input_feature is still 7.6 s above pre-#5175, and that difference is the merge.

2. CPU simulation with the real priorities. This calls AWQProcessor._merge_reservoir_rows and _reservoir_priorities directly: 64 experts x 128 captures, rows per capture ~N(75, 30), cap 906, hidden 2048, bf16, one thread.

main:      full-reservoir merges 7368, all-losing (short-circuit eligible) 67 (0.9%), merge time 0.93 s
c1807f889: full-reservoir merges 7368, all-losing (short-circuit eligible) 67 (0.9%), merge time 0.83 s

The short-circuit fires on 0.9% of full-reservoir merges. Because of the added max()/all() check, main is slightly slower than before.

Repro (CPU, a few seconds)

import random, time, torch
from gptqmodel.looper.awq_processor import AWQProcessor
torch.set_num_threads(1); random.seed(0)
p = AWQProcessor.__new__(AWQProcessor)
full = skip = 0; t = 0.0
for expert in range(64):
    entry = {}
    for b in range(128):
        n = max(1, int(random.gauss(75, 30)))
        rows = torch.randn(n, 2048, dtype=torch.bfloat16)
        pr = AWQProcessor._reservoir_priorities(b, n)
        used = int(entry.get("reservoir_used", 0))
        if used >= 906:
            full += 1
            skip += bool((pr > entry["reservoir_priorities"][:used].max()).all())
        t0 = time.perf_counter()
        p._merge_reservoir_rows(entry, rows, pr, source_index=b, budget_cap=906)
        t += time.perf_counter() - t0
print(full, skip, t)   # 7368 67 0.93

Suggested fix

  • Prefilter instead of testing all-or-nothing: winners = priorities < max_kept. With a full reservoir, only about count * cap / seen rows win (single digits per capture). Merge only those rows, or skip when there are none. This keeps the kept set identical.
  • The cost is per-op dispatch on small CPU tensors (about 10 ops at roughly 15 us each), not data volume. For a handful of winners, a plain Python/heapq update of the kept maximum plus one indexed copy into the buffer avoids most of the ops. Another option is to buffer one layer's captures per module and run a single top-k at _layer_input_features time.
  • tests/test_awq_capture_perf_5284.py::test_a_losing_capture_skips_the_merge monkeypatches a capture in which every row loses. A test that drives realistic capture sizes (for example 75 rows per capture into a 906-row cap) and asserts the merge work per capture would have shown that the short-circuit never fires.

Test that should have caught it

The PR measured the short-circuit on CPU with synthetic losing captures only. The OLMoE-2L wall time from #5284 was not re-measured after the change.

Severity

Low-medium [PERF]: 7.5 s of per-capture CPU work per 2-layer OLMoE run (about 3.8 s per MoE layer, so about 60 s on the full 16-layer model) that #5295 claims to have removed and has not. The larger remaining gap to pre-#5175 (clip search on the 906-row per-expert cap, 11.8 s -> 58.6 s here) is the trade-off documented in #5295 and is not part of this report.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 19, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-fb5145b84190 finished this issue.

    Pull request: #5328

    Replace the all-or-nothing reservoir short-circuit with a winners prefilter: merge only the rows that beat the kept maximum (and the slots they displace), and benchmark on realistic 75-row captures.

  2. added a commit that references this issue on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions