Skip to content

[PERF] #5236 (fix of #5233) incomplete: MoE AWQ quantize still 2.1-3.4x slower than pre-#5175 (OLMoE-2L 119-129 s vs 38-56 s) - per-capture ModulePlan policy rebuild (51k calls), ~160 us tensor merge per capture, and every routed expert now keeps the 906-row cap (5x clip-search rows) #5284

Closed

Description

@Qubitium

Found on main @ c1807f8, RTX 4090 (shared GPU; old and new runs interleaved), torch 2.15, transformers 5.22.

What is wrong

#5236 (fix of #5233) removed the copy of the whole kept buffer under the processor lock. That part works: the r21 base 396b5f0 spent 90.7 s in capture, main spends 22-35 s. But AWQ quantize of a MoE model is still 2.1-3.4x slower than before #5175, which is the level #5233 asked to restore. Three costs remain, all on the routed-expert path. They were measured with method wrappers (time.perf_counter, summed) on the same OLMoE run.

  1. Per-capture policy lookup. _record_input_feature calls _feature_aggregation_policy(module_name) (gptqmodel/looper/awq_processor.py:1200) on every capture. That calls BaseQModel.awq_input_feature_aggregation (gptqmodel/models/base.py:6062), which rebuilds ModulePlan.from_tree(...) and walks the whole module tree each time. One call costs about 36 us. Main makes 51,470 calls (3.4-3.5 s); pre-fix(awq): bound token-row activation capture to sampling budget (#5137) #5175 made 1,308 (0.1 s). The result is a pure function of the class and the module name.
  2. Per-capture tensor merge. _merge_reservoir_rows costs 7.2-8.6 s over 48,882 calls, about 160 us each. Every capture runs torch.cat of the priorities, topk, a bool mask, nonzero and two torch.arange, even when no new row survives.
  3. Clip search on about 5x more rows. Every routed expert now keeps the layer-wide cap: 906 rows for every expert projection (384 of 384). Before fix(awq): bound token-row activation capture to sampling budget (#5137) #5175, each expert's budget came from its own captures (max(largest capture, contributing batches)): mean 185 rows (min 128, max 548). _search_best_clip time rose accordingly: 10.0 s -> 22 s (cProfile run) and 12.9 s -> 68.5 s (a heavily contended run).

Measurements

OLMoE-1B-7B-0924 truncated to 2 layers (config copy), AWQConfig(bits=4, group_size=128), bf16, 128 C4 documents (2500 chars), one capture thread (single GPU, run_single). Each row is one quantize():

tree quantize wall _record_input_feature total of which policy / merge rows per expert at clip
0b1aea0f5^ (pre-#5175) 37.9 / 48.4 / 55.8 s 5.8 / 6.6 / 6.8 s 0.1 s / n.a. mean 185 (128-548)
396b5f0e1 (r21 base) 236.0 s 90.7 s
main c1807f8 129.3 / 119.7 / 118.6 s 34.5 / 24.2 / 21.7 s 3.4 s / 8.6 s 906 (all)

The pairs were interleaved (main, pre, main, pre). In every pair main took 2.1-3.4x as long. _search_best_scale is unchanged (1.5-3 s).

Repro

import time, gzip, json, torch
from gptqmodel import GPTQModel
from gptqmodel.quantization import config as C
cal = [t[:2500] for t in (json.loads(l)["text"] for l in gzip.open("c4-train.00000-of-01024.json.gz", "rt")) if len(t) >= 1500][:128]
m = GPTQModel.load("<OLMoE-1B-7B-0924 config copy with num_hidden_layers=2>", C.AWQConfig(bits=4, group_size=128), dtype=torch.bfloat16)
t = time.time(); m.quantize(C.CalibrationConfig(dataset=cal)); print(time.time() - t)
# 0b1aea0f5^: 38-56 s   main c1807f889: 119-129 s

Policy cost alone: OlmoeQModel.awq_input_feature_aggregation("mlp.experts.3.gate_proj") takes 36.5 us per call (2,000 calls, CPU).

Suggested fix

  • Cache the aggregation policy per (cls, module_name), or resolve it once per module per layer. It does not change between captures.
  • In _merge_reservoir_rows, once the buffer is full, compare priorities.min() with the current maximum kept priority (keep that maximum as a scalar). Skip the merge when no new row can survive; with a full 906-row buffer, most captures of a routed expert add nothing. Do small merges with a single topk on the new rows only.
  • Decide whether every routed expert should keep the dense layer's 906-row cap. Before fix(awq): bound token-row activation capture to sampling budget (#5137) #5175, an expert kept a budget sized from its own captures (about 185 rows here). If the larger sample is intended for accuracy, document the roughly 5x clip-search cost. Otherwise size the reservoir from the expert's own capture budget.

Test that should have caught it

The #5233 tests check buffer reuse and provenance but not time. A capture micro-benchmark asserting a per-capture cost bound, or a check that _feature_aggregation_policy is not called on every capture, would catch items 1 and 2.

Severity

Medium [PERF]: AWQ quantization of MoE models is still 2-3x slower end to end than before #5175, with no accuracy evidence offered for the larger per-expert sample. The cost grows with experts x layers x batches.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-fb5145b84190 finished this issue.

    Pull request: #5295

    Cache the AWQ feature-aggregation policy per (class, module name) and skip the reservoir merge when no new row can survive; measure both on CPU and add regression tests.

  2. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-4e3a0639ecaf is working on this issue now.

    Started 2026-11-18T23:50:03Z. The claim expires 4.0h after this comment is last edited (heartbeat). Other agents: do not start this issue while the claim is live.

    Scope: recover the remaining MoE AWQ quantize gap vs pre-#5175: cache the per-capture ModulePlan policy lookup, make the reservoir merge cheap when no row survives, and restore a per-expert row budget so the clip search is not run on 5x the rows; verify with OLMoE-2L wall time and unchanged checkpoint quality

  3. Qubitium commented on Nov 19, 2026

    @Qubitium
    CollaboratorAuthor

    Accuracy data for item 3 (the routed-expert cap), which #5295 left open as "an accuracy decision". Measured on main @ c03416c (includes #5295), RTX 4090, full 16-layer OLMoE-1B-7B-0924.

    Setup: AWQConfig(bits=4, group_size=128), bf16, 128 C4 documents (2500 chars, batch_size=1). Quantize, save, reload with AwqMarlin, then wikitext-2 PPL over 40 x 512 windows. The bf16 base is 10.6925. Runs were back to back on the same GPU.

    tree / expert budget rows kept per expert projection s / layer (layers 1-15) quantize() PPL
    main, layer-wide cap (current) 906 for all 3072 37-43 655 s / 690 s (2 runs) 11.2870 (both runs bitwise identical)
    main + per-expert budget (see below) mean 254, median 203 (120-784) 21-26 490 s 11.2814
    7b8f12785 (pre-#5175, round-20 base) mean 250, median 198 (119-787) 18-21 n/a (quant-only timing run) 11.2945 (earlier run)

    The 3.6x larger expert sample gives no measurable accuracy gain on the full model: PPL 11.2870 vs 11.2814. It costs about 15 s per MoE layer, roughly 1.4x end-to-end quantize(). The layer-wide cap is the dominant remaining gap to pre-#5175 speed. The last row's quantize time was not recorded because it was a quant-only run; its per-layer times are directly comparable.

    Per-expert budget used above. Monkeypatch only. Before _materialize_reservoir_provenance, keep the k = _cap_token_rows(min(sum(counts), max(max(counts), len(counts)))) lowest-priority rows of the reservoir, where counts are the expert's non-empty batch_row_counts. This is the pre-#5175 _token_row_budget formula applied to the expert's own captures. The lowest-k priorities of a uniform priority reservoir are themselves a uniform sample, so #5212's uniformity is kept. Memory stays O(906), and the kept set stays deterministic.

    def mrp(self, entry):
        used = int(entry.get("reservoir_used", 0))
        counts = [int(c) for c in entry.get("batch_row_counts") or [] if int(c) > 0]
        if entry.get("reservoir_buffer") is not None and counts:
            k = self._cap_token_rows(min(sum(counts), max(max(counts), len(counts))))
            if 0 < k < used:
                keep = torch.topk(entry["reservoir_priorities"][:used], k, largest=False).indices.sort().values
                for key in ("reservoir_buffer", "reservoir_priorities", "reservoir_sources", "reservoir_rows"):
                    entry[key][:k] = entry[key][:used].index_select(0, keep)
                entry["reservoir_used"] = k
        return orig_materialize(self, entry)

    Second model. Qwen3-30B-A3B truncated to 12 layers (128 experts, top-8), same settings. KL is measured against the bf16 logits of the same truncated model. Truncation makes PPL meaningless here.

    expert budget rows kept per expert projection s / layer quantize() KL vs bf16 (reloaded) starved-fallback records
    layer-wide cap (current) 906, or all raw rows when fewer 49-64 698 s 0.1010 1860
    per-expert budget mean 195 27-38 489 s 0.1013 1854

    Here too the larger sample is accuracy-neutral (+0.3% KL, within run-to-run noise) and costs about 1.4x quantize time.

    Checks I ran on the 906-cap main runs:

    🤖 Generated with Claude Code

  4. Qubitium commented on Nov 19, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-4e3a0639ecaf opened follow-up PR #5327.

    #5295 landed the policy memoization and the no-op merge. This PR carries the two remaining items from the accuracy data above: the per-expert reservoir budget (max(largest capture, capture count), mean 254 on the full model) and an incremental merge that writes only the surviving captured rows. A growing budget now carries the rows it holds instead of re-seeding from inputs (which silently dropped them).

    2-layer OLMoE, 128 C4 docs, batch_size=1, RTX 4090, alternating runs: 165.4 s -> 65.5 s (2.5x).

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