Skip to content

fix(awq): size scale-search replay by sequence rows and inherit fallback threshold (#4087) - #4089

Merged
Qubitium merged 3 commits into
mainfrom
fix/issue-4087-replay
Nov 15, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/issue-4087-replay

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4087

TL;DR

Two localized defects, one per issue item:

  1. AWQProcessor._scale_search_replay_batch_size sized a 3-D capture by its hidden width instead of one leading-dimension row, so [128, 2048, H] captures returned 0 ("one forward over everything") and 7-8B AWQ OOMed.
  2. fallback= given as a dict defaulted the threshold to 1.0% instead of the Fallback dataclass's 0.5%, silently moving modules from GPTQ to RTN.

Net diff: 7 files, +259/-8 (two source files, one override-path unification, two test files, README.md, and a new reference page).

1. The problem

1a. Replay sizing ignored the sequence dimension

AWQProcessor._scale_search_replay_batch_size (gptqmodel/looper/awq_processor.py:2992 on 1de03752d; :3010 on 701e58b) did:

hidden = int(x.shape[-1]) if x.dim() > 1 else 1
bytes_per_row = max(1, hidden) * element_size * 3

A 3-D capture is [batches, seq, hidden]; one leading-dimension row is seq * hidden elements. For [128, 2048, 4096] the estimate was 24 KiB/row instead of 48 MiB/row, so budget_rows >= rows and the helper returned 0. The MLP group then ran 262,144 tokens in one forward: 2 GiB input and a 7 GiB LlamaMLP output.

1b. A dict fallback= doubled the threshold

_normalize_fallback (gptqmodel/quantization/config.py:3533 on 1de03752d) used fallback.get("threshold", "1.0%"), while Fallback.threshold (:1474) and docs/quantization_protocol.md say 0.5%. A dict that only set strategy or smooth therefore got 1.0%. The decision in AWQProcessor._should_fallback_group / GPTQ compares observed calibration tokens against that percentage, so the doubled default moved 428 more Qwen3-30B-A3B experts from GPTQ to RTN (issue measurement: 1458 -> 1886).

2. The fix

2a. Size a row from the leading dimension

numel = int(x.numel())
if numel == 0:
    return None
elements_per_row = numel // rows
bytes_per_row = max(1, elements_per_row) * element_size * 3

rows is still x.shape[0]; the per-row element count is numel // rows, so the sequence dimension is included for any rank. A zero-element capture returns None (one forward over nothing) instead of dividing by an empty row; a 1-D input has one element per row; rows <= 1 returns None because one row is the minimum chunk.

2b. One threshold default

The dict path now defaults to Fallback.threshold. gptqmodel/utils/fallback.py:normalize_fallback (the per-module override path used by GPTQProcessor, AWQProcessor, VPTQProcessor, QQQProcessor, and ParoQuantProcessor) merges its override onto the config default and delegates construction to the same _normalize_fallback, so the two entry points cannot drift again.

3. Flow

Replay sizing

flowchart TD
    A["_search_best_scale: captured inp"] --> B["_scale_search_replay_batch_size"]
    B --> C{"x.dim == 0 or<br/>rows <= 1?"}
    C -- yes --> D["return None<br/>one forward"]
    C -- no --> E{"x.numel == 0?"}
    E -- yes --> D
    E -- no --> F["rows = x.shape[0]<br/>elements_per_row = x.numel // rows"]
    F --> G["bytes_per_row = elements_per_row<br/>* element_size * 3"]
    G --> H{"budget_rows = max_chunk_memory // bytes_per_row<br/>budget_rows >= rows?"}
    H -- yes --> I["return 0<br/>whole replay fits"]
    H -- no --> J["return max(quant_batch_size, budget_rows)<br/>chunked replay"]
    J --> K["_iter_prepared_forward_outputs<br/>torch.split(x, batch_size, dim=0)"]
Loading

Fallback normalization

flowchart TD
    A["GPTQConfig / AWQConfig / EXL3Config<br/>__post_init__"] --> B["_normalize_fallback"]
    C["per-module override:<br/>utils.fallback.normalize_fallback"] --> B
    B --> D{"fallback type"}
    D -- "None" --> E["None"]
    D -- "Fallback" --> F["validate strategy + smooth"]
    D -- "str, int, float" --> G["Fallback(strategy=RTN, threshold=value)"]
    D -- "dict" --> H["threshold = fallback.get(<br/>'threshold', Fallback.threshold)"]
    H --> I["Fallback(strategy, threshold, smooth)"]
    I --> F
Loading

4. Helper before / after

Input Before After
[128, 2048, 4096] fp16 0 (one 6.44 GiB chunk) 21 (1.06 GiB chunk)
[128, 2048, 14336] fp16 0 (one 22.5 GiB chunk) 6 (1.06 GiB chunk)
[262144, 4096] fp16 43690 43690 (unchanged)
[4, 0, 8] 0 None
1-D [N] 0 0

Config thresholds:

Config Before After
GPTQConfig(...).fallback.threshold 0.5% 0.5%
GPTQConfig(..., fallback={"strategy": "rtn"}) 1.0% 0.5%
GPTQConfig(..., fallback={"smooth": "mad"}) 1.0% 0.5%
AWQConfig(..., fallback={"strategy": "rtn"}) 1.0% 0.5%
EXL3Config(..., fallback={"strategy": "rtn"}) 1.0% 0.5%
fallback={"threshold": "2.0%"} 2.0% 2.0%

5. Real-model before / after

Hardware: RTX 4090 (48 GiB), CUDA_VISIBLE_DEVICES=GPU-d458708b-c760-bbf1-08fb-8b8803a2157d. The RTX 5090 was never used. Calibration: 128 x 2048 C4 token windows, AWQ 4-bit group size 128, batch_size=1.

Llama-3.1-8B-Instruct, 32 GiB allocator cap

Before (1de03752d, 3-D batch capture):

TRACE: inp_shape=(128, 2048, 4096) replay_batch_size=0
OOM: CUDA out of memory. Tried to allocate 7.00 GiB.
32.00 GiB allowed; 27.52 GiB in use
PEAK_ALLOC_GIB: 27.87 PEAK_RESERVED_GIB: 31.95
RESULT mode=awq oom=1 ... seconds=29.9

After:

TRACE: inp_shape=(128, 2048, 4096) replay_batch_size=21
TRACE: inp_shape=(128, 2048, 14336) replay_batch_size=6

The after run passed the layer where the before run OOMed; it was stopped manually for time. The same 8B model with the default token-row aggregation completed AWQ end to end under the same 32 GiB cap (PEAK_ALLOC_GIB: 5.33, PEAK_RESERVED_GIB: 31.96, 2 allocator retries).

Llama-3.2-1B-Instruct, 12 GiB allocator cap

Before (1de03752d):

TRACE: inp_shape=(128, 2048, 2048) replay_batch_size=0
OOM: CUDA out of memory. Tried to allocate 4.00 GiB.
PEAK_ALLOC_GIB: 10.82 PEAK_RESERVED_GIB: 11.79
RESULT mode=awq oom=1 ... seconds=69.4

After:

TRACE: inp_shape=(128, 2048, 2048) replay_batch_size=42
TRACE: inp_shape=(128, 2048, 8192) replay_batch_size=10

The after-fix 1B quantize is still running; the trace above is from that run and it passed the first MLP group that OOMed before. I will update this section with its final peak/PPL when it completes.

Note on the current default path

Current main's default awq_input_feature_aggregation token-row policy caps a dense capture at one largest-batch equivalent (2048 tokens for 128 x 2048), so the 3-D [batches, seq, hidden] shape is only reached when a model selects mode=batch (or a model without the policy). The harness pins that mode to exercise the exact capture shape from the issue; the shipped sizing helper and forward are unchanged.

6. Tests

Regression tests added:

  • tests/test_awq_scale_search_memory.py::test_scale_search_replay_batch_size_accounts_for_the_sequence_dimension — 3-D [128, 2048, 4096] returns > 0 and batch_size * per_row_bytes <= budget.
  • tests/test_awq_scale_search_memory.py::test_scale_search_replay_batch_size_keeps_the_2d_row_size — 2-D behavior unchanged.
  • tests/test_awq_scale_search_memory.py::test_scale_search_replay_batch_size_handles_empty_and_1d_inputs — 0-element -> None, 1-D -> one element per row.
  • tests/test_fallback.py::test_dict_fallback_keeps_the_dataclass_threshold_default — plain, dict-strategy, and dict-smooth forms for GPTQ + AWQ, explicit threshold still wins, per-module override path shares the default.

Before the fix (source reverted, tests kept): 3 failed, 1 passed.
After the fix: 4 passed.

Commands run after merging origin/main (d3b5e0b7a):

  • ruff check gptqmodel docs/eora tests setup.py -> All checks passed!
  • CUDA: pytest -q tests/test_awq_scale_search_memory.py tests/test_fallback.py -k "replay_batch_size or dict_fallback_keeps or should_use_fallback" -> 20 passed, 68 deselected
  • ROCm (HIP_VISIBLE_DEVICES=2): same selection -> 20 passed, 108 deselected
  • tests/test_awq_weight_mean.py -k "coalesces_cpu_activation_replay or disable_chunked_activation_streaming" could not collect on the CUDA venv (pre-existing missing parameterized); it collects and runs on the ROCm venv.

7. Reviewer notes / limitations

  • The 3-D sizing path is currently masked by the default token-row aggregation; the defect is still reachable for mode=batch aggregation and for any other caller of the helper. The fix is rank-robust rather than specific to the batch path.
  • The PPL table from the issue (Llama-3.1-8B: fp 6.778, RTN 7.478, GPTQ 7.124, AWQ 7.215) was not re-measured in this shortened verification; the 8B AWQ default-path run completed but was not evaluated. The regression tests pin the sizing/threshold behavior, and the real-model runs above pin the OOM -> success transition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] AWQ scale-search replay ignores seq dim (7-8B OOM with fixed-length calibration); dict fallback= doubles threshold 0.5% -> 1.0%

1 participant