Skip to content

[PERF] Regression from #4909 (fix of #4742): the desc_act sink-row cap does 4 host syncs per add_batch inside the forward hooks; desc_act quantize 2.1x slower (Qwen2.5-0.5B 38s -> 82s) #4950

Closed

Description

@Qubitium

Found on main @ 3867c82, RTX 4090 (CUDA 13.2, torch 2.15).

What is wrong

#4909 (fix of #4742) adds GPTQ.resolve_sink_row_cap_scale(), which compute_hessian_xtx() calls on every add_batch of every module whenever desc_act=True (gptqmodel/quantization/gptq.py:1171). It forces four device-to-host syncs per call:

  • gptq.py:1143: bool(torch.isfinite(norms).all())
  • gptq.py:1150: bool(torch.isfinite(threshold)) and float(threshold)
  • gptq.py:1157: bool((scale >= 1.0).all())

When a cap applies, it also moves the call from the single fp32 matmul fast path to the leased chunk-workspace path (chunk_size = rows). add_batch runs inside the layer forward hooks, so each sync drains the CUDA queue in the middle of the calibration forward. The "Forward" region grows about 30x and desc_act=True quantization is about 2x slower end to end. The cap's numerics do what #4742 asked for (see below); the cost comes from how they are computed.

Measurements (same box, old = 7edbca826^, new = main)

Full model, Qwen2.5-0.5B-Instruct, GPTQConfig(bits=4, group_size=128, desc_act=True), 128 C4 rows, fp16, quantize -> save -> reload, wikitext-2 80x512:

quantize region "Forward" (21504 calls) region "Pre-quant forward" PPL
old 38.2 s 0.915 s 27.5 s 21.010
new 81.8 s 30.894 s 60.0 s 21.094

2-layer Llama-3.1-8B-Instruct (the #4742 repro, bf16):

quantize "Forward" (1792 calls) "Pre-quant forward"
old desc_act 10 s 0.094 s 4.36 s
new desc_act 15 s 2.983 s 8.25 s
new default (no cap) 11 s 0.153 s 5.79 s

Microbenchmark of GPTQ.compute_hessian_xtx (fp16 input, 20 iterations; the GPU was shared, so absolute times are noisy):

cols=896  rows=512   desc_act cap=7.01ms  cap disabled=0.14ms  default=0.15ms
cols=4864 rows=512   desc_act cap=7.75ms  cap disabled=1.03ms
cols=4096 rows=2048  desc_act cap=5.67ms  cap disabled=1.38ms
cols=14336 rows=2048 desc_act cap=20.5ms  cap disabled=16.2ms

With the GPU idle, the torch profiler shows 4 aten::item/cudaStreamSynchronize per call, totalling 4.4 ms of 8.75 ms across 10 calls.

Suggested fix

Keep the whole cap on device. Compute threshold and scale with tensor ops only: torch.where(torch.isfinite(norms), ...), scale = clamp(threshold / norms, max=1), nan_to_num. Always multiply by scale (a no-op when everything is 1), and fold it into the fp32 GEMM ((X * s[:,None]).T @ X or X.T @ (s**2 * X)) on the existing fast path rather than switching to the chunk lease. That removes all four syncs and the path change. If a "nothing to cap" early-out is still wanted, do it once per module at finalize time, not per batch.

Quality check (the fix itself works)

2-layer Llama-3.1-8B layers.1.mlp.down_proj, relative error per column after dequantization:

old desc_act  overall 0.1718  worst [(5095, 4.95), (11717, 4.90), (11411, 4.31)]
new desc_act  overall 0.1480  worst [(6412, 1.587), (3174, 0.586), (591, 0.519)]   col 5095: 0.160
new default   overall 0.1172  worst [(6412, 1.284), (591, 0.789), (12657, 0.692)]

Test that should have caught it

tests/test_descact_sink_4742.py is CPU-only with synthetic Hessians, so it never sees the per-batch syncs. A GPU timing guard is needed, or at least an assertion that compute_hessian_xtx makes no host sync, for example under torch.cuda.set_sync_debug_mode("error").

Severity: perf regression. Every desc_act=True GPTQ quantize is about 2x slower on small models (1.5x on the 2-layer 8B truncation).

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4950 is working on this issue now.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

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

    Pull request: #4976

    Remove the four per-add_batch host syncs #4909 added to GPTQ.resolve_sink_row_cap_scale (desc_act 2x slower); keep the sink-row cap numerics, decide the cap on-device or once per module, and measure desc_act quantize time + PPL.

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-root-4950 finished this issue — #4976, merged as cdeb00e47.

    The four per-add_batch host reads are gone and the cap stays on the fp32 fast path; the cap formula and the resulting H are unchanged (bitwise vs main, same wikitext-2 PPL). Caching the threshold on the first batch was measured and rejected: it changed the quantized checkpoint (PPL 20.78 vs 20.32).

  4. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Fixed by #4976 (merged as cdeb00e47): the desc_act sink-row cap is decided entirely on device (0 host reads per add_batch), the cap formula and the resulting H are bitwise unchanged, and PPL is identical.

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