Skip to content

[QUALITY] Regression from #5175 (fix of #5137): AWQ routed-expert 'streaming reservoir' re-strides the kept buffer with each new batch, so 70-87% of every expert's token rows come from the last quarter of the calibration set (first quarter 1-5%) #5204

Closed

Description

@Qubitium

Found on main @ 396b5f0, RTX 4090 (CUDA 13.2, torch 2.15, transformers 5.22).

What is wrong

#5175 (fix of #5137) bounds AWQ token-row capture at capture time. Dense modules and MoE roots get a precomputed per-batch plan, and their sample stays proportional. Routed experts do not match the plan because their row count per batch varies, so they go through the "Dynamic / streaming reservoir" branch of AWQProcessor._record_input_feature (gptqmodel/looper/awq_processor.py:1303-1353):

all_tensors = [t.reshape(-1, hidden) for t in current_inputs] + [feat_2d]
combined = torch.cat(all_tensors, dim=0)          # <= budget_cap kept rows + this batch
...
indices = torch.arange(M) * (total_cand - 1) // (M - 1)   # evenly spaced over `combined`

This is not a reservoir. Once the buffer is full, every new batch is resampled evenly together with the kept buffer. The 906 kept rows, which stand for all earlier batches, then weigh the same as 906 fresh rows. Each batch of L rows therefore replaces about L/(906+L) of the sample, and the contribution of earlier batches decays geometrically. The final sample is mostly the last few dozen calibration batches.

The code this replaced (_pack_token_rows at 0b1aea0^) gave every batch a share proportional to its length. Its docstring states it "avoids the previous latest-batch-only bias". For routed experts, #5175 brings that bias back.

Measured

OLMoE-1B-7B-0924 truncated to 2 layers, bf16, default AWQConfig(bits=4, group_size=128), 128 C4 rows. Each line compares the provenance of the kept rows (entry["provenance"] source slot) with the raw token count per quarter of the capture slots. Read at _layer_input_features:

module                 slots raw   kept  kept% per quarter of batches   raw% per quarter
mlp (MoE root, plan)   128  64652  906   [29, 27, 24, 20]               [30, 27, 24, 19]   <- plan path: proportional
mlp.experts.1.*        128   7449  906   [ 2,  4, 21, 73]               [32, 29, 22, 17]
mlp.experts.11.*       128   9855  906   [ 1,  2, 15, 82]               [42, 21, 21, 17]
mlp.experts.12.*       128   8812  906   [ 1,  1, 14, 84]               [27, 24, 28, 21]
mlp.experts.19.*       128   9969  906   [ 1,  1, 11, 87]               [30, 26, 24, 20]
(every expert with > 906 tokens looks like this)

So 70-87% of each expert's scale-search and clip rows come from the last 32 calibration documents, and the first 32 documents contribute 1-5%. The skew grows with the calibration size: the more batches there are, the smaller the fraction of the run that survives.

Repro

from gptqmodel import GPTQModel, CalibrationConfig
from gptqmodel.quantization.config import AWQConfig
from gptqmodel.looper import awq_processor as AP
orig = AP.AWQProcessor._layer_input_features
def patched(self, state):
    for name, e in sorted(self.tasks.items()):
        if not isinstance(e, dict) or not e.get("provenance") or len(e.get("batch_row_counts", [])) < 4:
            continue
        counts, prov = e["batch_row_counts"], e["provenance"]; n = len(counts)
        kept = [0] * 4; raw = [0] * 4
        for s, c in enumerate(counts): raw[min(3, 4 * s // n)] += c
        for s, _ in prov: kept[min(3, 4 * s // n)] += 1
        print(name, [round(100 * k / len(prov)) for k in kept], [round(100 * r / sum(counts)) for r in raw])
    return orig(self, state)
AP.AWQProcessor._layer_input_features = patched
m = GPTQModel.load("olmoe_2_layers", AWQConfig(bits=4, group_size=128), dtype=torch.bfloat16)
m.quantize(CalibrationConfig(dataset=c4_rows[:128]))

Suggested fix

Either:

  • use a real reservoir: Algorithm R, or a deterministic equivalent that keeps each row with probability cap / rows_seen. It is unbiased and still bounded at cap rows; or
  • keep a cheap per-batch over-sample (for example cap rows per batch, stride-sampled) and run the old proportional _pack_token_rows allocation over all batches once the per-batch row counts are known at the end of the layer. The memory stays O(batches x cap / k) instead of O(tokens).

Test that should have caught it

test_awq_token_rows_streaming_reservoir_dynamic_moe (tests/test_awq_token_rows_bounded_capture_5137.py:174) only checks the bound. Feeding batches whose rows are tagged with the batch id and asserting that each half of the batches keeps about half of the rows would fail today.

Severity

Medium-high ([QUALITY], regression from #5175). Every routed expert of every AWQ MoE quantization now picks its scales and clip ranges on a sample dominated by the tail of the calibration set. There is no error, and dense layers are unaffected. Large calibration sets, where the bias is strongest, are what #5137 was fixed for.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-fb5145b84190 is no longer working on this issue.

    The issue is free to claim.

    Fixed by #5212 (merged f58fcfc). Routed-expert capture is now a deterministic uniform priority reservoir; verified proportional across the calibration stream.

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