Skip to content

fix(vptq): bound distance and Hessian temporaries (#4391) - #4438

Merged
Qubitium merged 1 commit into
mainfrom
fix/vptq-memory-4391
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/vptq-memory-4391

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Two remaining VPTQ memory findings from #4391 (item 1, EXL3 tile scratch, already shipped in #4432):

  1. _chunked_assign kept three distance temporaries per chunk alive at once (the matmul result, the 2.0* product and the difference), tripling its stated 256 MiB budget for every Lloyd iteration (100 by default).
  2. VPTQ held up to 8 fp64 n x n Hessian copies at the peak (materialize, normalized, damped, cholesky, and three from the factorization) and ran the same fp64 inverse Cholesky twice per module — once for the main pass and once for the default residual pass.

Addresses items 2 and 3 of #4391. Item 4 (GPTAQ/FOEM sibling inputs and H/dXXT dedup) remains open.

What Changed

  • vptq.py::_chunked_assign: compute distance with one torch.addmm(..., alpha=-2.0), take .min(1), then del distance.
  • vptq.py::_inverse_hessian_cholesky: free damped after factorization, factorize/invert in place (torch.linalg.cholesky(..., upper=True, out=...), torch.cholesky_inverse(cholesky, out=cholesky)).
  • quantize_weight factorizes once and passes inv_hessian to both _vptq_pass calls.
  • quantize() calls free() and passes owns_hessian=True, so the normalized Hessian reuses H2 in place instead of cloning; H2 is released afterwards.
  • New tests/test_vptq_memory_4391.py (8 fast CPU tests) pins the temporaries, single factorization, in-place identity and H2 release; tests/test_vptq.py spy retargeted to the single factorization.

Tests

PYTHONPATH=/root/work-4391b /root/venv/bin/python -m pytest tests/test_vptq_memory_4391.py -q
8 passed
PYTHONPATH=/root/work-4391b /root/venv/bin/python -m pytest tests/test_vptq.py -q -k "not cuda"
199 passed, 103 deselected
/root/venv/bin/python -m ruff check gptqmodel/quantization/vptq.py tests/test_vptq.py tests/test_vptq_memory_4391.py
All checks passed!

Baseline tests/test_vptq.py -k "not cuda" on pristine main is identical. CUDA-extension tests fail on both trees only with CUDA_HOME environment variable is not set.

Bitwise A/B on the GB10 (pre-fix algorithm vs committed code) at 4864x896 and 8192 columns: quantized weight, loss, centroids, indices, res_centroids, res_indices all torch.equal.

Measured peaks (torch.cuda.max_memory_allocated): item 2 4864x896 extra peak 853 -> 336 MiB (issue: 899 -> 388); item 3 at 8192 columns extra beyond the input Hessian 3081 -> 1545 MiB (issue target 1536), H2 is None afterwards.

Logic Flow Documentation

Not applicable — no decision mechanism or selection order changed; the change removes redundant copies while the factorization and assignment stay numerically identical (pinned bitwise).

Review Requirements

  • Reviewed; ownership of the caller's Hessian is documented on quantize_weight/_vptq_pass.

Item 2: `_chunked_assign` kept the matmul result, its `2.0 *` product and
their difference live at once, so each chunk used three times the 256 MiB
`_DISTANCE_CHUNK_BYTES` budget. Use `torch.addmm(..., alpha=-2.0)` to write
the fused distance directly and `del` it before the residual allocation.
`alpha=-2.0` is exact, so assignments and residuals stay bitwise identical.

Item 3: `_inverse_hessian_cholesky` cloned, factorized, inverted and
re-factorized out of place, and `_vptq_pass` recomputed the same fp64
inverse for the residual pass. Free the damped copy after factorization,
invert and re-factor in place with `out=`, factorize once in
`quantize_weight` and share the result between both passes, and let
`quantize()` hand `H2` over (`owns_hessian=True`) instead of cloning it.

Measured on the GB10 CUDA device:

- 4864x896 module, 4096 centroids: peak extra 853 -> 336 MiB (issue:
  899 -> 388 MiB); quantized weight, indices, centroids and residuals
  bitwise equal.
- 8192 columns, fp64 Hessian 512 MiB: peak extra beyond the input Hessian
  3081 -> 1545 MiB (issue expected 1536 MiB); quantized output and loss
  bitwise equal. `quantize()` peaks at 1545 MiB including `H2`.

Add `tests/test_vptq_memory_4391.py`, which pins the pre-fix packed result
bitwise, asserts a single inverse factorization per module, and bounds the
live full-size temporaries with a CPU dispatch-mode counter.

Co-Authored-By: Claude <[email protected]>
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.

1 participant