Skip to content

fix(qqq): accumulate Hessian in-place per device to reduce memory#2981

Merged
Qubitium merged 1 commit into
mainfrom
devin/qqq-memory-reduce
Jul 25, 2026
Merged

fix(qqq): accumulate Hessian in-place per device to reduce memory#2981
Qubitium merged 1 commit into
mainfrom
devin/qqq-memory-reduce

Conversation

@Qubitium

@Qubitium Qubitium commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

Summary

Reduce QQQ quantization peak memory by avoiding a separate columns x columns xtx output tensor for every calibration batch and by freeing per-device Hessian partials as they are merged.

Changes

QQQ.add_batch:

  • Keep the existing per-device Hessian partial design (required for calibration_data_device="balanced" / multi-GPU MoE).
  • Allocate the partial buffer once per device as float32.
  • Accumulate inp @ inp.t() directly into that buffer with addmm_ instead of inp.matmul(inp.t()) + add_.
    • This removes the transient xtx tensor that is the same size as the Hessian for each batch.

QQQ.materialize_hessian:

  • Merge per-device partials into the final Hessian on the target device.
  • popitem() and del partial each partial as it is added, so the peak during materialization is result + one partial instead of result + all partials.

QQQ.quantize:

  • Fixed the diag index tensor to live on H.device so the damping step is safe when H has been materialized on a different device from self.dev.

Validation

  • ruff check gptqmodel/quantization/qqq.py tests/test_qqq.py passes.
  • New TestQQQHessian unit tests in tests/test_qqq.py verify that add_batch + materialize_hessian match the closed-form 2/N * X^T X reference for:
    • single batch,
    • multiple batches,
    • multi-device (cpu + cuda:0) accumulation,
    • empty batches.
  • pytest tests/test_qqq.py::TestGroupSize::test_load_group_128 -v passed on GPU 4.

Closes memory-reduction follow-up for QQQ after #2115 / PR #2979.

@Qubitium Qubitium self-assigned this Jul 25, 2026
@devin-ai-integration

Copy link
Copy Markdown

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@Qubitium
Qubitium merged commit 581bfd9 into main Jul 25, 2026
6 checks passed
@Qubitium
Qubitium deleted the devin/qqq-memory-reduce branch July 25, 2026 09:36
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