Skip to content

[MEM] AWQ token_rows copies every calibration token of every module to host and samples ~1% only after the layer forward: peak host RAM grows linearly with calibration size (OLMoE-2L 6.4 -> 17.8 GiB for 128 -> 512 docs, GPTQ 2.7 -> 3.8) and hits 28 GiB/layer with ExpertsRoutingOverride; retained rows stay 906 #5137

Closed

Description

@Qubitium

Found on main @ 7b8f127, RTX 4090 (CUDA 13.2, torch 2.15) and Radeon AI PRO R9700 (ROCm), Python 3.14, glibc.

What is wrong

With the default token_rows aggregation, AWQ keeps only a bounded sample of each module's input: _token_row_budget gives one largest-batch equivalent, 906 rows on 128 C4 docs. But _record_input_feature (looper/awq_processor.py:1108) first copies every calibration token of every captured module to host memory, and holds all of it until the end of the layer's forward. Only then does _layer_input_features (:2296-2398) run _pack_token_rows and drop the rest.

The scale search, x_mean, the replay loss and the clip search all read only the packed rows (inp in _search_best_scale is the packed [1, 906, hidden] tensor). So 98-99% of the captured host bytes are written and never read. Peak host RAM per layer therefore grows linearly with:

  • the number of calibration tokens. The retained sample does not grow: docs/awq-calibration-aggregation.md says "the bound does not grow with the number of calibration documents", which holds for the packed tensor but not for the capture;
  • the expert fan-out. With MoEConfig(routing=ExpertsRoutingOverride()), every expert captures every token.

GPTQ on the same looper folds each batch into a Hessian and stays flat.

Measurements (peak RssAnon sampled every 0.5 s during quantize(); default AWQConfig(bits=4, group_size=128) vs GPTQConfig(bits=4, group_size=128), bf16, batch_size=1, C4 rows of 2500 chars)

OLMoE-1B-7B-0924 truncated to 2 layers (num_hidden_layers=2 in a config copy), RTX 4090:

calibration tokens AWQ peak RssAnon GPTQ peak RssAnon AWQ rows retained per module (logged raw -> retained)
128 docs 64,652 6.39 GiB 2.69 GiB 64652 -> 906 (attention), 7449 -> 128 (an expert)
512 docs 256,655 17.78 GiB 3.80 GiB 256655 -> 906

4x the calibration data adds +11.4 GiB of AWQ host RAM, and the scale search still sees the same 906 rows.

OLMoE-1B-7B-0924 truncated to 4 layers, ExpertsRoutingOverride() (all 64 experts per token), 128 docs, R9700:

  • AWQ: every layer spikes from 3.6 GiB at the layer boundary to 25.9 / 23.6 / 27.8 / 25.4 GiB RSS (ru_maxrss 28.25 GiB). Every expert module logs raw 64652 -> retained 906.
  • GPTQ with the same override: peak 3.98 GiB.

Full OLMoE-1B-7B (16 layers, native routing, 128 docs), RTX 4090: AWQ quantize peak RSS 7.22 GiB, GPTQ 3.84 GiB.

The size matches a full-token capture. Per OLMoE layer it is about 64652 x 2 B x (2048 q/k/v + 2048 o_proj + 2048 MoE root + 8 x 2048 gate/up + 8 x 1024 down) = 3.9 GiB at top-8, and 26 GiB at 64-expert override. The same formula for Qwen3-30B-A3B (128 experts, 2048/768) with ExpertsRoutingOverride() and 128 docs gives ~45 GiB per layer. With a typical 1024-doc x 2048-token calibration set at native top-8, it gives ~90 GiB per layer, all to retain a few thousand rows per module.

Minimal repro

import json, gzip, torch, threading, time
from gptqmodel import GPTQModel
from gptqmodel.quantization.config import AWQConfig, GPTQConfig, CalibrationConfig
def anon():
    return next(int(l.split()[1]) for l in open("/proc/self/status") if l.startswith("RssAnon")) / 2**20
peak = [0.0]; stop = threading.Event()
def sample():
    while not stop.is_set(): peak[0] = max(peak[0], anon()); time.sleep(0.5)
texts = [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][:512]
m = GPTQModel.load("olmoe_2_layers", AWQConfig(bits=4, group_size=128), dtype=torch.bfloat16)   # or GPTQConfig
threading.Thread(target=sample, daemon=True).start()
m.quantize(CalibrationConfig(dataset=texts)); stop.set()
print(f"peak RssAnon {peak[0]:.2f} GiB")
AWQ  128 docs: 6.39   512 docs: 17.78
GPTQ 128 docs: 2.69   512 docs:  3.80

Suggested fix

Bound the capture, not only the packed result:

  • For token_rows modules, keep a per-module streaming sample while capturing. The per-batch quota can be derived from the calibration lengths, which the looper knows before the layer forward (largest_batch_rows and the batch count are known up front for dense modules). For routed experts, use a deterministic reservoir or a per-batch stride. Then drop the rest of each batch's capture immediately instead of appending it to entry["inputs"].
  • Where provenance is needed (_token_row_replay_kwargs), record (batch, row) for the kept rows at capture time, as _pack_token_rows already does.
  • If an exact match with the current deterministic sample is required, do a two-pass selection: the row indices depend only on per-batch row counts, so the indices for batch b can be fixed as soon as the counts are known.

Test that should have caught it

No test measures AWQ host memory against calibration size. Two checks would have caught this: a CPU test that captures N vs 4N batches on a tiny MoE and asserts the peak captured host bytes stay within ~2x of retained * hidden * dtype, and a check that entry["inputs"] never holds more than the budget.

Severity

Medium ([MEM]). No wrong output, but a large host-RAM waste that scales with calibration size and expert fan-out. AWQ on a large MoE with ExpertsRoutingOverride (the documented remedy for starved experts) or with a larger calibration set can run out of host RAM, while the same run with GPTQ stays at a few GiB.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    The ParoQuant processor has the same pattern, with a full concatenated copy on top.

    ParoProcessor._record_input_feature (looper/paro_processor.py:348) appends every batch's full input to entry["inputs"] on the host. _layer_input_features (:379-397) then calls torch.cat on all of them, so the capture plus a second full copy are live at the same time. The optimizer uses at most opt_train_samples + opt_validation_samples rows per module (_sample_activation_rows, quantization/paro/optimization.py:859): 2112 by default, 576 in the run below.

    Measured on the same host, OLMoE-1B-7B-0924 truncated to 2 layers, 128 C4 docs (64,652 tokens), bf16, R9700. Config: ParoConfig(bits=4, group_size=128, opt_rotation_epochs=2, opt_finetune_epochs=2, opt_train_samples=512).

    peak RssAnon during quantize()
    ParoQuant 9.35 GiB (from 1.18 at start)
    AWQ (same model, CUDA) 6.39 GiB
    GPTQ (same model, CUDA) 2.69 GiB

    Each expert module keeps a few hundred to a few thousand of the captured rows. Here too, a per-module streaming sample bounded by train + val rows at capture time (deterministic stride or reservoir) would remove both the capture and the concatenation peak.

    🤖 Generated with Claude Code

  2. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

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

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

    AWQ token_rows copies every calibration token of every module to host before sampling, so host RAM grows linearly with calibration size

  3. Prajwal7-2002 commented on Nov 18, 2026

    @Prajwal7-2002
    Contributor

    I am taking this up — implementing streaming bounded capture and reservoir sampling for AWQ (and ParoQuant) so calibration tokens are bounded at capture time rather than holding all raw tokens in host RAM.

  4. Prajwal7-2002 commented on Nov 18, 2026

    @Prajwal7-2002
    Contributor

    Submitted PR #5175 with the fix:

    • Precomputes deterministic token-row sampling plan from batch lengths upfront and bounds activations immediately during _record_input_feature
    • Provides deterministic streaming reservoir for dynamic expert routing
    • Records original unpadded batch lengths in batch_row_counts to preserve 100% fidelity for block-diagonal causal attention masks and position IDs
    • Maintains sibling projection host copy sharing (q is k is v and gate is up)
    • Bounds ParoQuant activations per-batch to per_batch_quota and layer features to target_rows
    • Added comprehensive unit tests in tests/test_awq_token_rows_bounded_capture_5137.py
  5. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

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

    Prajwal7-2002 claimed this issue at 2026-11-18T07:05:44Z and opened PR #5175, so that claim stands. Releasing ours; no code from our session will be pushed.

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