Skip to content

[BUG] Regression from #4703: subset early-stop flag is never reset between batches, so every routed MoE expert gets calibration from only 1 batch per layer (GPTQ/AWQ/EXL3/Paro) #4738

Closed

Description

@Qubitium

Found on main @ b225a43, RTX 4090 (sm_89, CUDA 13.2, torch 2.15, transformers 5.22.1). A/B against 12c1273c2 (before #4703) on the same card.

What is wrong

#4703 (fix for #4679) replaced the declared-last forward_hook_last with _mark_subset_member (gptqmodel/looper/stage_subset.py:1728-1743, set up at :1069-1072). There are two problems:

  1. pending_members = set(subset_names) is built once per subset pass. It is shared by every calibration batch and is never refilled.
  2. The member whose hook empties the set gets module.forward_hook_last = True. The flag stays set for the rest of the pass and is only cleared in the cleanup at :1165.

From the next batch onward, the forward therefore raises StopForward as soon as that one member fires. That is correct only if the member is also the last one to fire in every later batch. In a MoE subset it is not:

  • routing differs per batch;
  • the expert loop runs experts in index order;
  • the member that happened to complete the set (on OLMoE it ends up being experts.0.gate_proj) fires first in later batches, so every other expert projection is cut off.

Every processor with subset_early_stop=True / replay_after_process=True is affected: GPTQ, AWQ, EXL3, ParoQuant and MACaQ.

Result: each routed expert (except the one that holds the flag) receives calibration from only one batch per layer. The tokens are silently dropped. GPTQ builds expert Hessians from about 1/N of the data. AWQ sees most experts as "starved" and sends them to the RTN fallback.

Evidence

OLMoE-1B-7B-0924 truncated to 4 layers, 16 C4 rows (t[:2500]), batch_size=1.

GPTQ: GPTQ.add_batch calls per expert module, summed over the 4 layers (names are layer-relative):

                         main @ b225a4383        12c1273c2 (before #4703)
experts.0.gate_proj      64 calls / 3359 tok     64 / 3442
experts.0.up_proj         4 calls /  137 tok     64 / 3442
experts.1.gate_proj       4 calls /  163 tok     64 / 4055
experts.10.gate_proj      4 calls /  444 tok     60 / 2506
median over all experts   4 calls (1 per layer)  64 calls (16 per layer)

AWQ: captured feature stats for layer 0 (_feature_stats at _should_fallback_group):

main:      ('mlp.experts.0.gate_proj', 'token_rows', raw_tokens=826, batches=16)
           ('mlp.experts.0.up_proj',   'token_rows', raw_tokens=26,  batches=1)
           ('mlp.experts.1.gate_proj', 'token_rows', raw_tokens=47,  batches=1)
           ('mlp.experts.21.gate_proj','token_rows', raw_tokens=1,   batches=1)
12c1273c2: ('mlp.experts.0.up_proj',   'token_rows', raw_tokens=826, batches=16)
           ('mlp.experts.1.gate_proj', 'token_rows', raw_tokens=753, batches=16)

On main, the default AWQConfig(bits=4, group_size=128) now logs the following for layers 0-3. At 12c1273c2, the same run has no fallback at all:

AWQProcessor: layer 3 keeps AWQ for 88 of 128 members of a scaling group; 40 starved member(s) are scale-folded then plain-quantized
AWQProcessor: layer 3 fallback quant 60 modules

(L0/L1/L2: 7 / 19 / 25 modules fall back.) Even experts.0.gate_proj and experts.0.up_proj disagree (826 vs 26 tokens) although they read the same input.

Quality (quantize, save, reload; mean token KL vs bf16 on 16x256 wikitext-2 tokens):

main 12c1273
AWQ, OLMoE-L4, n=16 0.07233 0.06972
GPTQ, OLMoE-L4, n=16 0.0690 0.0709

On this 4-layer probe, the quality effect is small: AWQ is 3.7% worse, and GPTQ is within run-to-run noise. The mechanism is still a silent drop of about 15/16 of each expert's calibration data, together with spurious AWQ RTN fallback. The effect should grow with the expert count (Qwen3-MoE: 128 experts) and with skewed routing.

Dense subsets (q/k/v, gate/up) are not affected, because the member that completes the set in batch 0 is also the last to fire in later batches.

Minimal repro

import collections, torch
from gptqmodel import GPTQModel
from gptqmodel.quantization.config import GPTQConfig, CalibrationConfig
from gptqmodel.quantization import gptq as G
calls = collections.Counter()
orig = G.GPTQ.add_batch
def add_batch(self, inp, out, *a, **k):
    calls[self.name] += 1
    return orig(self, inp, out, *a, **k)
G.GPTQ.add_batch = add_batch
m = GPTQModel.load("OLMoE-1B-7B-0924 (num_hidden_layers=4)", GPTQConfig(bits=4, group_size=128), dtype=torch.bfloat16)
m.quantize(CalibrationConfig(dataset=c4_rows[:16], batch_size=1))
print({k: calls[k] for k in ["mlp.experts.0.gate_proj", "mlp.experts.0.up_proj", "mlp.experts.1.gate_proj"]})
# main:      {'mlp.experts.0.gate_proj': 64, 'mlp.experts.0.up_proj': 4,  'mlp.experts.1.gate_proj': 4}
# 12c1273c2: {'mlp.experts.0.gate_proj': 64, 'mlp.experts.0.up_proj': 64, 'mlp.experts.1.gate_proj': 64}

Suggested fix

Make the completion state per batch. At the start of each calibration batch's forward:

  • refill pending_members;
  • clear forward_hook_last on every member.

Only then let the member that completes the set for this batch raise StopForward. Alternatively, keep the declared-order stop for dense subsets and never early-stop a subset with conditionally-executed members (MoE experts), as before #4703. Note that a MoE batch in which some expert is not routed never completes the set, so it runs to completion. That is correct and costs only the rest of the layer forward.

Test that should have caught it

tests/test_subset_declared_order_4679.py runs the out-of-order MLP with one batch only. Add a case with two or more batches where the member that completes the set in batch 0 fires first in batch 1, for example a toy MoE whose routing flips between batches. Assert that every member's hook (and GPTQ.add_batch) fires in every batch that routes to it. An end-to-end check on a tiny Qwen3-MoE with batch_size=1 and 8 rows that asserts per-expert nsamples would also catch it.

Severity

High (silent calibration-data loss, regression). Every MoE model quantized with GPTQ/AWQ/EXL3/ParoQuant on main calibrates its routed experts on about one batch per layer instead of the full dataset. AWQ additionally sends many experts to RTN. Nothing errors.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4752

    Resetting the subset early-stop flag between batches so routed MoE experts keep all calibration

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Dense models are affected as well, and badly. The statement "Dense subsets (q/k/v, gate/up) are not affected" does not hold. In batch 1 and every later batch, pending is already empty, so marked() sets forward_hook_last = True on whichever member fires first, and StopForward is raised right after it. In a q/k/v subset that member is q_proj; in gate/up it is gate_proj. The other members are cut off. As a result, k_proj, v_proj and up_proj see only batch 0 of the calibration set on every dense Llama/Qwen model.

    Measured on main @ b225a43, with 12c1273c2 as the A/B base. Hook calls per layer-0 module are counted by wrapping pre_process_fwd_hook. The model is Qwen2.5-0.5B-Instruct truncated to 3 layers, with 8 C4 rows at batch_size=1. Numbers are summed over the 3 layers:

                     q_proj k_proj v_proj o_proj gate_proj up_proj down_proj
    main  AWQ          24      3      3     24      24        3       24
    main  GPTQ         24      3      3     24      24        3       24
    12c1273c2 AWQ/GPTQ 24     24     24     24      24       24       24
    

    For AWQ this changes the checkpoint through the clip search. v_proj and up_proj are clipped from one sample (q/k are excluded from clipping). In the diff of the 3-layer checkpoint, only v_proj and up_proj qweight/scales differ, already in layer 0. MACaQ is hit the same way. GPTQ, GPTAQ, FOEM, QQQ, EXL3 and ParoQuant checkpoints remain byte-identical here, because the siblings share the leader's Hessian/capture. git bisect run on the 3-layer AWQ checkpoint hash lands on 219a528 (#4703). For MACaQ, the hash at 219a52803~1 equals 12c1273c2, and the hash at 219a52803 equals main.

    Full-model quantize, save and reload. Wikitext-2 PPL, 80x512 windows, 128 C4 rows, default configs, fp16:

    model method CUDA 4090: 12c1273 -> main ROCm R9700: 12c1273 -> main
    Qwen2.5-0.5B-Instruct AWQ 4-bit g128 24.504 -> 26.689 (+8.9%) 24.374 -> 26.145 (+7.3%)
    Qwen2.5-0.5B-Instruct AWQ 4-bit g128 asym 22.063 -> 22.464 (+1.8%) 22.095 -> 22.696 (+2.7%)
    Llama-3.2-1B-Instruct AWQ 4-bit g128 21.884 -> 22.261 (+1.7%) 21.829 -> 22.322 (+2.3%)
    Llama-3.2-1B-Instruct AWQ 4-bit g128 asym 20.982 -> 21.552 (+2.7%) 20.928 -> 21.576 (+3.1%)
    Qwen2.5-0.5B-Instruct MACaQ default 22.528 -> 23.004 (+2.1%) 22.652 -> 23.285 (+2.8%)
    Llama-3.2-1B-Instruct MACaQ default 22.342 -> 22.719 (+1.7%) (running)

    Every one of these was byte-identical between rounds before #4703. So on main, the default AWQ and MACaQ path silently loses quality on every dense model, not only MoE. The alternative fix in the issue ("keep the declared-order stop for dense subsets, never early-stop MoE subsets") would therefore not be enough by itself. The per-batch reset is required: refill pending and clear forward_hook_last on every member before each batch's forward.

    Test addition: also assert the per-module hook count for a dense q/k/v + gate/up layer with 2+ batches. For example, on a tiny Llama with batch_size=1 and 4 rows, every member's hook should fire 4 times per layer. Today k_proj/v_proj/up_proj fire once.

    🤖 Generated with Claude Code

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Additional data: gpt-oss-20b-BF16 truncated to 2 layers (real weights), default AWQConfig(bits=4, group_size=128), 32 C4 rows, batch_size=1, RTX 4090, main @ b225a43. #4723 restores the post_attention_layernorm -> 64 expert gate/up group here. Because of this bug, most of that group's members then fall back:

    main:
    AWQProcessor: layer 0 keeps AWQ for 19 of 64 members of a scaling group; 45 starved member(s) are scale-folded then plain-quantized
    AWQProcessor: layer 1 keeps AWQ for 15 of 64 members of a scaling group; 49 starved member(s) are scale-folded then plain-quantized
    
    main with only the `module.forward_hook_last = True` line in `_mark_subset_member` removed (no subset early stop, so every batch is captured):
    (no "keeps AWQ" / "fallback quant" lines; all 64 members keep AWQ in both layers)
    

    So on gpt-oss, 70-77% of the routed-expert gate/up projections become RTN only because their calibration rows are cut off after the first batch.

    🤖 Generated with Claude Code

  4. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    One extra data point. An existing repo test already catches this, and it has been red on main since #4703:

    tests/test_moe_batch_equivalence.py::test_moe_expert_weights_match_across_calibration_batch_sizes (CPU, tiny Qwen3-MoE, 2 calibration rows, compares batch_size=1 vs batch_size=2 dequantized expert weights):

    E   AssertionError: model.layers.0.mlp.experts.1.gate_proj
    E   assert 0.13589726388454437 < 1e-06
    

    Bisect (CPU, this test file at each commit): passes at 04be2e62b and at 12c1273c2; first failing commit is 219a52803 (#4703). Counting GPTQ.add_batch rows per expert in that fixture:

    219a52803^  bs=1: experts.1=12 experts.2=17 experts.3=11   bs=2: 12 / 17 / 11
    219a52803   bs=1: experts.1=7  experts.2=11 experts.3=8    bs=2: 12 / 17 / 11
    

    At a larger scale, on OLMoE-1B-7B truncated to 1 layer with 32 C4 rows (t[:800]), batch_size=1, GPTQ, the sum of add_batch rows over all 64 experts.*.gate_proj is:

    • main: 2,962 (min 5, median 30 per expert);
    • 219a52803^: 44,768 (min 331, median 644).

    q_proj gets 5,596 rows in both runs. So with the default batch_size=1, experts receive about 6.6% of their calibration tokens.

    The suite had a red test for this regression, but it was not run before #4703 merged.

    🤖 Generated with Claude Code

  5. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Follow-up: dense GPTQ is affected too once the shared-input Hessian dedup is off. With the default HessianConfig(dedup_shared_inputs=True), k_proj/v_proj/up_proj reuse the leader's (q_proj/gate_proj) Hessian, and that masks the bug. With GPTQConfig(bits=4, group_size=128, hessian=HessianConfig(dedup_shared_inputs=False)), each sibling builds its own Hessian. Those Hessians now come from batch 0 only.

    Llama-3.2-1B-Instruct, fp16, 128 C4 rows, batch_size=1, quantize, save and reload. Wikitext-2 PPL over 80x512 windows, RTX 4090:

    commit ckpt PPL
    12c1273c2 (pre-#4703) 5f47d11d 25.060 (same as the dedup-on run)
    main @ b225a43 f22995ee 26.281 (+4.9%)

    In the 3-layer Qwen2.5-0.5B checkpoint, the hash changes exactly at 219a528: 219a52803~1 gives 8306848e and 219a52803 gives 11058ba7.

    🤖 Generated with Claude Code

  6. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    A correction to "Dense subsets (q/k/v, gate/up) are not affected". That holds only while HessianConfig(dedup_shared_inputs=True) (the default) collapses q/k/v and gate/up onto one hooked leader. With dedup_shared_inputs=False, the dense followers are starved the same way.

    Repro: tests/test_shared_input_hessian_dedup_e2e.py::test_tiny_llama_dedup_matches_independent_hessians. It is a CPU test: tiny Llama, 2 layers, 3 calibration rows of 15/12/9 tokens, batch_size=1.

                             main (b225a4383)   219a52803^
    self_attn.q_proj               36               36
    self_attn.k_proj               15               36
    self_attn.v_proj               15               36
    self_attn.o_proj               36               36
    mlp.gate_proj                  36               36
    mlp.up_proj                    15               36
    mlp.down_proj                  36               36
    

    (Both layers show the same counts.) On main, k/v/up see only the first batch, which is the 15-token row. So with dedup off, every dense shared-input follower is calibrated on one batch. The fix should cover both cases: re-arm pending_members and clear forward_hook_last per batch.

    🤖 Generated with Claude Code

  7. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Full-model data point for checking #4752. Qwen1.5-MoE-A2.7B (full, 24 layers x 60 experts, top-4), default GPTQConfig(bits=4, group_size=128), 128 C4 docs, bf16, RTX 4090. Quantize, save, reload (Marlin), then wikitext-2 40x512:

    bf16 main @ b225a43 (before #4752) 12c1273c2 (before #4703)
    PPL 8.236 8.844 (+7.4%) 8.660 (+5.2%)
    mean token KL vs bf16 (256-token probe) - 0.128 0.086
    expert modules logged Using rtn fallback quantization - 4293 / 4320 (observed 22-104 token rows each, threshold 314) 0
    quantize time - 451 s 635 s

    On the full model, almost every routed expert projection silently became RTN. The fix should bring main back to about 8.66 with 0 expert fallbacks; a quick check is to grep the quantize log for rtn fallback on any MoE model.

    🤖 Generated with Claude Code

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