Skip to content

fix(awq): make the routed-expert capture a real uniform reservoir (#5204) - #5212

Merged
Qubitium merged 1 commit into
mainfrom
fix/awq-reservoir-tail-bias-5204
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/awq-reservoir-tail-bias-5204

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #5204.

Root cause

#5175 bounded routed-expert capture by resampling the kept buffer evenly together with each new batch:

all_tensors = [t.reshape(-1, hidden) for t in current_inputs] + [feat_2d]
combined = torch.cat(all_tensors, dim=0)
indices = torch.arange(M) * (total_cand - 1) // (M - 1)

That is not a reservoir. Once the buffer is full, the budget_cap kept rows — which stand for every earlier batch — weigh the same as the fresh batch, so each batch of L rows replaces about L / (cap + L) of the sample and earlier batches decay geometrically.

Measured before the fix

128 C4 rows, budget 906, routed-expert row counts varying per batch:

kept % per quarter:  [ 1,  1, 12, 86]
raw  % per quarter:  [25, 25, 25, 25]
batches contributing to kept: 75 of 128

After:

kept % per quarter:  [24, 25, 25, 25]
raw  % per quarter:  [25, 25, 25, 25]
batches contributing to kept: 128 of 128

Fix

Keep the budget_cap rows with the lowest deterministic uniform priority. That is the deterministic equivalent of Algorithm R: a uniform sample of every row seen, so a batch keeps a share proportional to its length wherever it falls in the stream.

  • Priorities come from a per-capture-slot seeded generator, so the sample is reproducible across processes (verified) and rows stay stable.
  • Survivors are re-sorted into capture order so replayed rows and provenance stay grouped for the mask/position rebuild.
  • Memory stays O(cap): 43,385 raw rows × hidden 4096 would be ~678 MiB unbounded; the kept buffer is ~14 MiB.
  • The dense-module / MoE-root plan path is untouched.

Tests

  • New test_awq_token_rows_streaming_reservoir_keeps_every_quarter_proportional asserts every quarter of the stream keeps 15–35% of rows. It fails on the pre-fix tree (quarter 0 kept 1.3%).
  • 11/11 in tests/test_awq_token_rows_bounded_capture_5137.py.
  • 117 passed, 1 skipped across the related AWQ/Paro suites.
  • format/check.sh clean.

I could not run a real MoE checkpoint here (no OLMoE on this host, and the CUDA toolkit is unavailable), so the numbers above come from the same capture path exercised directly with varying per-batch row counts.

)

#5175 bounded routed-expert capture by resampling the kept buffer evenly
together with each new batch. That is not a reservoir: once the buffer is
full, the `budget_cap` kept rows -- which stand for every earlier batch --
weigh the same as the fresh batch, so each new batch replaces about
`L / (cap + L)` of the sample and the surviving share decays geometrically.

On a 128-document calibration set this put 70-87% of every routed expert's
rows in the last quarter of the batches and left the first quarter at 1-5%,
so AWQ picked each expert's scales and clip ranges on a sample dominated by
the tail of the corpus. Dense modules and MoE roots were unaffected because
they match the precomputed plan.

Keep the `budget_cap` rows with the lowest deterministic uniform priority
instead. That is the deterministic equivalent of Algorithm R: a uniform
sample of every row seen, so a batch keeps a share proportional to its
length wherever it falls in the stream. Memory stays O(cap) and the sample
is reproducible across processes. Survivors are re-sorted into capture
order so replayed rows and provenance stay grouped.

Adds a regression test asserting every quarter of the calibration stream
keeps 15-35% of the rows (raw share 25%). It fails on the pre-fix tree
(quarter 0 kept 1.3%).
@Qubitium
Qubitium merged commit f58fcfc into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/awq-reservoir-tail-bias-5204 branch November 18, 2026 11:15
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