Repository navigation
Description
Audit of main @ 5065bf1 (first measured at 27c6b7b), RTX 4090, fp16, 64 C4 samples. Attribution comes from torch.cuda.memory._record_memory_history snapshots replayed to the peak.
1. EXL3 tile scratch is cached process-wide and never released: 2 GiB (4-bit) to 8 GiB (2-bit) stays allocated after quantize() and del model (medium)
exllamav3/modules/quant/exl3_lib/quantize.py:147-187: _allocate_temp_buffers / _stream_temp_buffers store each worker stream's scratch (batch × 256 × (65536>>K) int16 + fp16; 512 MiB at K=4, 2 GiB at K=2) in the module-global _workspace_buffers = {} (:164), keyed by (device, K, stream, batch). Nothing clears it, so the memory survives quantize(), del model, gc.collect() and empty_cache(). With 4 CUDA workers, every K used adds 4 more entries:
after K=4: allocated=2064 MiB registry_entries=4
after K=2: allocated=10320 MiB registry_entries=8 (K=4,3,2 -> 14448 MiB, 12 entries)
pipeline Qwen2.5-0.5B exl3 2-bit: peak_alloc 9204 MiB, after del+gc+empty_cache: 8256 MiB still allocated
Fix: clear the entries when the EXL3 processor finalizes or quantize() exits (add a clear_tile_workspace() to the looper's end-of-run cleanup), or hold the scratch on the processor/task instead of a module global, sized to min(batch, tiles). tests/test_exl3_workspace.py checks per-stream privacy but not release.
2. VPTQ _chunked_assign keeps three distance temporaries per chunk, 3× its stated 256 MiB budget (medium-low)
quantization/vptq.py:152: distance = centroids_norm.unsqueeze(0) - 2.0 * (block @ centroids.t()) keeps the matmul result, the 2.0* product and the difference alive at once, plus the previous chunk's distance until it is reassigned. The budget is _DISTANCE_CHUNK_BYTES = 256 MiB (:50). This runs for every Lloyd iteration (100 by default). In the Qwen2.5-0.5B VPTQ snapshot, 1536 of the 1818 MiB peak is 6 × 256 MiB blocks from this line (gate and up quantized concurrently).
4864 x 896 module: current peak_extra 899 MiB -> addmm(+del) 388 MiB; quantized_weight, indices, centroids bitwise equal
Fix: distance = torch.addmm(centroids_norm.unsqueeze(0), block, centroids.t(), alpha=-2.0); best_distance, best_index = distance.min(1); del distance. This saves ~511 MiB per concurrent module and removes most of the exact-256 MiB allocation churn that feeds the reserved growth in #4389.
3. VPTQ holds up to 8 fp64 n×n Hessian copies at the peak, and runs the same fp64 inverse Cholesky twice per module (medium-low; dedup)
On Llama-3.2-1B, 4096 of the 4282 MiB VPTQ peak is 8 × 512 MiB fp64 copies of the 8192² down_proj Hessian:
- materialize:
:383 - normalized:
:500 - damped:
:659 - cholesky:
:663 - 3 blocks from
:669
vptq.py:431 also keeps self.H2 alive while :473 makes a private hessian.to(fp64).clone(). _inverse_hessian_cholesky (:655-670) clones, factorizes and inverts out of place. It is called from _vptq_pass (:707) twice per module (:576, and :608 for the residual pass, which is on by default) with the same pass_hessian. At 8B (14336 columns) each copy is 1.5 GiB, about 12 GiB for 8 copies.
8192 cols: current extra_peak 2560 MiB -> in-place (free damped after factorization, cholesky_inverse(out=chol)) 1536 MiB, bitwise equal
Fix:
- Factorize in place as above.
- Compute
inv_hessianonce inquantize_weightand pass it to both_vptq_passcalls; that drops one fp64 factorization (~2–3 s at 8192). - Hand
H2over instead of cloning (free()it immediately).
Expected Llama-1B peak: ~4.3 → ~2.3–2.8 GiB.
4. GPTAQ/FOEM at batch_size>1 store bitwise-identical native inputs once per sibling (q/k/v ×3, gate/up ×2), and accumulate identical H/dXXT per sibling (low-medium; dedup)
looper/native_processor.py:77-101 (_split_batch_samples: inp[i][mask[i]].to(...) builds a new tensor for each module) and :162-170. At batch_size=1 the entries alias the shared input. With a keep-mask, each sibling gets its own masked copy, held on GPU (gptaq.device="auto") until it is consumed.
Llama-3.2-1B gptaq4, 64 samples, bs=4: native cache logical 1199.0 MiB, distinct storage 1199.0 MiB, bitwise-duplicate sibling copies 359.7 MiB
same at bs=1: distinct 839.3 MiB, duplicates 0
That is 30% of the native cache (+34% at the layer boundary), and it scales with samples × tokens × hidden: about 6 GiB at 8B with 128 × 2048 tokens. Separately, gptq_processor.py:443-473 excludes GPTAQ/FOEM from the :in= shared-input Hessian dedup because they "also consume outputs". In fact GPTAQ.add_batch (gptaq.py:71) and FOEM.add_batch (foem.py:147) ignore out, so q/k/v each build their own identical H and dXXT: 3 × 2 × cols² × 4 B ≈ 384 MiB at 4096 columns, plus 3× the accumulation matmuls.
Fix: reuse per-sample entries for siblings fed by the same source tensor in the same batch (key on batch index + source tensor, or use the :in= shared-input groups). Let GPTAQ/FOEM followers adopt both H and dXXT from the leader, and correct the comment. tests/test_gptaq_foem_batch.py covers correctness only.
Peak memory per method (allocated / reserved MiB, for reference)
Reserved values from EXL3 onward are under a 10 GiB per-process cap. Llama reserved values for GPTQ-family runs are inflated by #4389.
| method | Qwen2.5-0.5B | Llama-3.2-1B |
|---|---|---|
| gptq4 | 459 / 660 | 1324 / 7688 |
| gptaq4 | 773 / 1462 | 2092 / 4334 |
| foem4 | 599 / 780 | 1772 / 10236 |
| awq4 | 433 / 522 | 960 / 1046 |
| qqq4 | 488 / 598 | 1440 / 2326 |
| vptq | 1853 | 4400–4520 |
| exl3_4 | 3065 / 5484 | 4924 / 9790 |
| macaq | 1358 / 1896 | 5256 / 10222 |
| paro4 | 385 / 452 | 915 / 2170 |
| rtn4 / fp8 / nf12 / gguf | ~412–421 | ~489–538 |
Plausible, not verified
- Weight-only peak from the QR warmup: for RTN/FP8/NF12/GGUF, 384 of the 414 MiB peak (Qwen) is the fp64 4×4
linalg.qrwarmup (utils/linalg_warmup.py:108). Plain torch takes +384 MiB transiently for that call. Warming QR lazily, only when rotation is used, would cut those peaks ~10×. - MACaQ
compile_flip: the first compiled call leaves themacaq_quantize_weightframe in a reference cycle (~367 MiB, freed only by gc). - GPTAQ/AWQ: the last layer's replay outputs (53–120 MiB) stay alive after
quantize()untilgc.collect().
Checked, clean
- Per-layer allocated memory is flat for all 16 configs on both models; no live-memory leak.
- After
del model+ gc, every method returns to 0 MiB allocated except EXL3 (item 1). - Repeated quantize → save → reload in one process is clean on GPU.
- GPTQ
:in=Hessian dedup works for plain GPTQ.
🤖 Generated with Claude Code
Activity
fix(exl3): release per-stream tile scratch at end of run (#4391) (#4432)
Qubitium commentedon Nov 16, 2026
Claimed by an AI agent: items 2-3 (VPTQ distance temporaries and fp64 Hessian/inverse duplication). Item 1 is already merged.
fix(vptq): bound distance and Hessian temporaries (#4391) (#4438)
Qubitium commentedon Nov 16, 2026
Claimed by an AI agent: item 4 first half (reuse bitwise-identical native sibling inputs at bs>1). Items 1-3 are already merged.
fix(looper): share bitwise-identical native sibling inputs (#4391) (#…
Qubitium commentedon Nov 16, 2026
Claimed by an AI agent: item 4 second half (GPTAQ/FOEM adopt leader H and dXXT at bs>1).
Qubitium commentedon Nov 16, 2026
Claimed by an AI agent: item 4 second half (GPTAQ/FOEM adopt the leader's H and dXXT at bs>1). Branch
fix/sibling-hessian-dedup-4401also carries the #4401 item 3 dedup.fix(looper): dedup shared-input Hessians for QQQ/VPTQ/MACaQ/EXL3 and …
Qubitium commentedon Nov 17, 2026
All four items are now fixed and merged:
Tests assert bitwise-identical output versus the non-dedup path and actual storage sharing. Closing.