Skip to content

[MEM] Quantize memory: EXL3 tile scratch never released (2-8 GiB pinned after quantize), VPTQ distance temporaries 3x budget and 8 fp64 Hessian copies + duplicate inverse, GPTAQ/FOEM bs>1 duplicate sibling inputs and Hessians #4391

Closed

Description

@Qubitium

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_hessian once in quantize_weight and pass it to both _vptq_pass calls; that drops one fp64 factorization (~2–3 s at 8192).
  • Hand H2 over 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.qr warmup (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 the macaq_quantize_weight frame 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() until gc.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

  1. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed by an AI agent: items 2-3 (VPTQ distance temporaries and fp64 Hessian/inverse duplication). Item 1 is already merged.

  2. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed by an AI agent: item 4 first half (reuse bitwise-identical native sibling inputs at bs>1). Items 1-3 are already merged.

  3. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed by an AI agent: item 4 second half (GPTAQ/FOEM adopt leader H and dXXT at bs>1).

  4. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    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-4401 also carries the #4401 item 3 dedup.

  5. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    All four items are now fixed and merged:

    1. EXL3 tile scratch release — fix(exl3): release per-stream tile scratch at end of run (#4391) #4432
    2. VPTQ distance temporaries — fix(vptq): bound distance and Hessian temporaries (#4391) #4438
    3. VPTQ fp64 Hessian/inverse duplication — fix(vptq): bound distance and Hessian temporaries (#4391) #4438
    4. GPTAQ/FOEM sibling input + H/dXXT dedup — fix(looper): share bitwise-identical native sibling inputs (#4391) #4443 (inputs) and fix(looper): dedup shared-input Hessians for QQQ/VPTQ/MACaQ/EXL3 and GPTAQ/FOEM (#4401, #4391) #4514 (H/dXXT)
      Tests assert bitwise-identical output versus the non-dedup path and actual storage sharing. Closing.
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