Repository navigation
perf(gptq): resolve the desc_act sink-row cap without syncing per add_batch (#4950) - #4976
Merged
Merged
Conversation
`resolve_sink_row_cap_scale` ran on every `add_batch` of every module when `desc_act=True`, and it read the host four times per call: `bool(isfinite(norms).all())`, `bool(isfinite(threshold))`, `float(threshold)` and `bool((scale >= 1).all())`. `add_batch` runs inside the layer forward hooks, so each read drained the CUDA queue mid-calibration. When a cap applied it also left the single fp32 `X^T X` GEMM for the leased chunk-workspace path (`chunk_size = rows`). Compute the whole cap decision with tensor ops and fold the per-row scale into the existing fp32 fast path: * `resolve_sink_row_cap_threshold` derives `min(p99.9, N x median)` from one device sort of the row norms and turns a non-finite/non-positive threshold (where the old code returned `None`) into `+inf` through `torch.where`; * `_sink_row_cap_scale_from_threshold` builds the per-row clamp on device; * `compute_hessian_xtx` multiplies the fp32 staging copy by the scale instead of switching to the chunk lease, so a capped batch keeps the single-GEMM path. The multiply is out of place when the input is already fp32, because `matrix.to(float32)` then returns the caller's (possibly shared) tensor. The cap is still decided per batch, so the Hessian is bitwise identical to the pre-#4950 formula for one batch and for a batch sequence. Caching the threshold on the first batch was tried and rejected: it changed the quantized Qwen2.5-0.5B checkpoint (wikitext PPL 20.78 vs 20.32). `test_descact_cap_sync_4950.py` pins the absence of per-`add_batch` host reads, the fast-path choice, the first-batch cap and the bitwise H. It fails on `main` (16 host reads for 4 batches, no device-only threshold resolver, one chunk lease per capped batch) and passes here. Qwen2.5-0.5B-Instruct, 4-bit/gs128/desc_act, 128 C4 rows, fp16, shared PG506 (two runs each, the box is 80-92 GiB of 98 GiB busy): region "Forward" over 21504 calls 12.2-23.1 s -> 8.5-8.7 s, `quantize()` 79.9-174.8 s -> 45.7-60.2 s, wikitext-2 PPL unchanged at 20.319 (pre-#4909: Forward 2.53 s, PPL 20.278).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
#4909 fixed #4742 by capping each calibration row's norm before
X^T X, but itcomputed the cap in
resolve_sink_row_cap_scale, whichcompute_hessian_xtxcalled on every
add_batchof every module withdesc_act=True. That didfour device-to-host reads per call (
bool(isfinite(norms).all()),bool(isfinite(threshold)),float(threshold),bool((scale >= 1).all())),and whenever a cap applied it moved the GEMM off the fp32
matmulfast pathonto the leased chunk-workspace path (
chunk_size = rows).add_batchrunsinside the layer forward hooks, so every read drained the CUDA queue in the
middle of the calibration forward:
desc_act=Truequantization was ~2x slower.The cap's numerics are unchanged. Only how they are computed changed.
What Changed
GPTQ.resolve_sink_row_cap_threshold(new) derivesmin(p99.9, N x median)from onetorch.sortof the row norms, entirely ondevice. The old early-return cases (a non-finite row norm, a non-finite or
non-positive threshold) now fold into the tensor as
threshold = +infthrough
torch.where, which makes every scale 1 — the same Hessian the oldNoneproduced, without a host read.GPTQ._sink_row_cap_scale_from_threshold(new) builds the per-row clamp ondevice;
resolve_sink_row_cap_scalekeeps its old signature as a purefunction of the matrix (the [QUALITY] GPTQ desc_act=True on Llama-3.1-8B is worse than RTN (PPL 11.62 vs 10.22): layers.1.mlp.down_proj columns saturate on BOS-sink Hessian, 10/160 wikitext windows reach PPL 800-12000 (#4393 unsolved for desc_act) #4742 tests use it).
GPTQ.compute_hessian_xtxmultiplies the fp32 staging copy by the scale andstays on the single-GEMM fast path instead of switching to the chunk lease.
The multiply is out of place when the input is already fp32, because
matrix.to(torch.float32)then returns the caller's tensor and activationscan be shared by several projections.
sink_row_cap_median_multipletolerates a capture built withoutqcfg(the
GPTQ.__new__accumulators intests/test_rocm_quant_blas.py), whichfixes two tests that fail on
mainfor the same reason.docs/gptq-hessian-accumulation.mdgains adesc_actsink-row cap sectionwith the shipped flow diagram;
docs/rocm-default-environment.mdandtests/test_docs_contract_refs.pyfollow the moved line numbers.Deliberately not done: caching the threshold on the first batch. It is the
cheapest way to skip the per-batch sort, and it was implemented and measured,
but it decides later batches against a stale bound. On Qwen2.5-0.5B-Instruct it
changed the quantized checkpoint (wikitext-2 PPL 20.78 vs 20.32) for a ~2 s
gain on the whole run. The per-batch decision is kept so the Hessian stays
bitwise identical to
main.Tests
New test (
tests/test_descact_cap_sync_4950.py, CPU-only, no checkpoint):torch.Tensor.item/__bool__/__float__/__int__on the device path for4 and 12
add_batchcalls: 16 reads onmain(4 per batch), 0 here;batches with different norm distributions must reproduce
main's per-batchcapped
Hbitwise (torch.equal);borrow_materialized_chunk_fp32;His bitwise equal tomain's formula.Numerics
His bitwise identical tomainfor the #4742 fixture (single batch) and fora 3-batch sequence (
torch.equal, 0 tolerance). End-to-end, the quantizedQwen2.5-0.5B checkpoint gives the same wikitext-2 PPL as
main:7edbca826^mainrun Amainrun BQwen2.5-0.5B-Instruct,
GPTQConfig(bits=4, group_size=128, desc_act=True),128 C4 rows, fp16,
CUDA_VISIBLE_DEVICES=5on a shared 8x PG506 (80-92 GiB of98 GiB already in use by other tenants). Wall-clock and the forward region
move a lot with that contention — the sync-heavy
mainpath suffers most,which is the bug — so the paired region totals matter more than the absolute
seconds.
GPTQ.compute_hessian_xtxmicrobenchmark (fp16, cols=896, rows=512, 20iterations, same box):
mainOf the remaining 0.61 ms, 0.25 ms is the row-norm + sort decision and the rest
is the fp32 GEMM plus the row scale. The cap disabled/default paths are
untouched.
Model Support / Module Plan
Not applicable: no
MODEL_MAP, definition, alias, module-plan or fusion change.Logic Flow Documentation
desc_actsink-row cap flow is documented with a Mermaid diagram anda before/after table in
docs/gptq-hessian-accumulation.md#desc_act-sink-row-cap,linked from
README.md. The removed mechanism (four host reads plus thechunk-lease switch) is tabulated with
file:lineevidence.flowchart TD A["compute_hessian_xtx(matrix)"] --> B["resolve_hessian_chunk_size"] B --> C["resolve_sink_row_cap_threshold(matrix, chunk_size)"] C --> D{"sink_row_cap_median_multiple()"} D -- None --> E["no cap"] D -- "N > 0" --> F["_sink_row_norms: fp32 row L2, chunk-staged"] F --> G["torch.sort(norms) once"] G --> H["threshold = min(ordered[p99.9], N x ordered[median])"] H --> I{"every norm finite and threshold > 0?"} I -- No --> J["threshold = +inf, so every scale is 1"] I -- Yes --> K["keep threshold"] K --> L["_sink_row_cap_scale_from_threshold: clamp(threshold / norm, max=1)"] J --> L L --> M{"chunk_size is None?"} M -- Yes --> N["fp32 fast path: (X * s)^T (X * s)"] M -- No --> O["chunk lease: (X_c * s_c)^T (X_c * s_c) per chunk"] classDef bad fill:#fdd,stroke:#c33 class J badAWQ onboarding safety
Not applicable: no model/alias onboarding and no AWQ grouping, search, replay
or fold change.