Repository navigation
fix(vptq): bound distance and Hessian temporaries (#4391) - #4438
Merged
Merged
Conversation
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]>
This was referenced Nov 16, 2026
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
Two remaining VPTQ memory findings from #4391 (item 1, EXL3 tile scratch, already shipped in #4432):
_chunked_assignkept three distance temporaries per chunk alive at once (the matmul result, the2.0*product and the difference), tripling its stated 256 MiB budget for every Lloyd iteration (100 by default).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: computedistancewith onetorch.addmm(..., alpha=-2.0), take.min(1), thendel distance.vptq.py::_inverse_hessian_cholesky: freedampedafter factorization, factorize/invert in place (torch.linalg.cholesky(..., upper=True, out=...),torch.cholesky_inverse(cholesky, out=cholesky)).quantize_weightfactorizes once and passesinv_hessianto both_vptq_passcalls.quantize()callsfree()and passesowns_hessian=True, so the normalized Hessian reusesH2in place instead of cloning;H2is released afterwards.tests/test_vptq_memory_4391.py(8 fast CPU tests) pins the temporaries, single factorization, in-place identity andH2release;tests/test_vptq.pyspy retargeted to the single factorization.Tests
Baseline
tests/test_vptq.py -k "not cuda"on pristine main is identical. CUDA-extension tests fail on both trees only withCUDA_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 Noneafterwards.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
quantize_weight/_vptq_pass.