Skip to content

[MEM] CUDA default alloc conf (expandable_segments + max_split_size_mb:256) grows reserved VRAM per layer during GPTQ (1B: 5.4 GiB reserved for 1.23 allocated; 8B: ~45 GiB), AllSpark keeps both weight layouts resident, CPU fp16 TorchLinear ~120x slow GEMM #4389

Closed

Description

@Qubitium

Audit of main @ 5065bf1 (first measured at 27c6b7b), RTX 4090, plus R9700 for item 2. All numbers are per-process.

1. CUDA default allocator config makes reserved VRAM grow per layer during GPTQ: 4.4× the allocated peak, and ~45 GiB reserved for an 8B model (high; CUDA sibling of #4313)

gptqmodel/_alloc_conf.py:35 sets the CUDA default PYTORCH_ALLOC_CONF to expandable_segments:True,max_split_size_mb:256,garbage_collection_threshold:0.7. With max_split_size_mb capping block splitting on top of expandable segments, GPTQ's per-layer Hessian/workspace pattern keeps mapping new pages instead of reusing them. #4313's fix covered only ROCm/AWQ.

Llama-3.2-1B GPTQ 4-bit (same calibration), peak allocated 1.23 GiB in every case:
  default conf                                   peak reserved 5.40 GiB  (end of quantize: 5.40 GiB still reserved)
  expandable_segments:True only                  peak reserved 1.67 GiB
  no expandable segments                         2.55 GiB
  default conf, cuda_workers=1                   2.16 GiB
Llama-3.1-8B bf16, n=32, default conf, 48 GB card:
  peak allocated 5.41 GiB, peak reserved 45.46 GiB (reserved per layer 0.39 -> 45 GiB while active stays ~0.12 GiB),
  1 alloc retry, 15.6 GiB still reserved after quantize() returns
  capped at 14.5 GiB: default conf hits the cap (14.21 GiB), 8 alloc retries, 49.9 s; expandable-only 5.27 GiB, 0 retries, 44.1 s

On a shared or smaller card, this reservation starves other processes and causes alloc retries and slowdowns, even though the real working set is a few GiB. Fix: drop max_split_size_mb from the CUDA default, which is what is fragmenting expandable segments here, and call torch.cuda.empty_cache() when quantize() ends. A test asserting that peak reserved stays within ~1.5× peak allocated on a small model would catch regressions.

2. AllSpark keeps the weight in both checkpoint and kernel layout, so 8-bit W8A16 uses as much VRAM as fp16 (low-medium; dedup)

nn_modules/qlinear/allspark.py:279-280 (_repack) creates qweight_packed/scales_packed as non-persistent buffers, while the checkpoint qweight (int32) and scales stay resident on the GPU, used only as the export source and cache key. The module docstring (lines 14-21) describes this as intentional for export, but the memory cost isn't documented. allspark_awq.py:327-329 follows the same pattern and adds zeros_packed (code read only).

Qwen2.5-0.5B GPTQ 8-bit g=-1, BACKEND.ALLSPARK (R9700, same at 27c6b7b and 5065bf1):
  qweight int32 341.25 MiB + qweight_packed uint8 341.25 MiB + scales 0.58 + scales_packed 0.58
  quantized modules 683.7 MiB vs dense fp16 weights 682.5 MiB; whole model 944.0 MiB vs 603.5 MiB under AUTO (TorchLinear)

Saving: 341.8 MiB here (50% of the quantized-module memory), about 6.5 GiB on an 8B model. Fix: after the repack, move qweight/scales to CPU (pinned) or drop them. Rebuild them on demand for state_dict()/save (un-repacking is a deterministic inverse), and key the cache on a version counter instead of the GPU tensor. tests/test_allspark_linear.py::test_linear_bias_does_not_expand_persistent_storage checks only persistent buffers, not resident bytes.

3. TorchLinear on CPU with fp16 activations runs an fp16 GEMM that is ~120× slower than computing in fp32 (low-medium; perf)

nn_modules/qlinear/torch.py _forward_eager (the CPU fp16 addmm branch and the matmul_fp32_reduce fallthrough; _numerics.py:18 _WIDE_REDUCE_DEVICES = ("cuda",)). On CPUs without fp16 GEMM hardware (this host has AVX512-BF16 but no AVX512-FP16/AMX-FP16), PyTorch's fp16 matmul/addmm/linear is extremely slow:

(511 x 896) @ (896 x 4864):  fp16 matmul 1970 ms | addmm 2004 ms | F.linear 1938 ms | fp32 upcast 16.4 ms
Qwen2.5-0.5B, one 512-token forward, TorchLinear on CPU fp16: 147.3 s, NLL 2.709939
  x upcast to fp32 inside _forward_eager:                       2.59 s, NLL 2.709915

This path is hit by explicit BACKEND.TORCH on CPU and by in-session use after quantize() (#4380). Fix: on CPU with fp16 inputs, compute x.float() against the dequantized fp32 weight and round once. That is also more accurate. Alternatively, gate the change on cpu_features lacking fp16 GEMM support.

4. Concurrent quantize() runs report each other's peak VRAM (low)

models/base.py:285-316 (_reset_peak_vram_stats / _peak_vram_gib) uses the process-global allocator counters, so two concurrent runs reset each other's peak. Quantizing Qwen2.5-0.5B and Llama-3.2-1B in two threads, both "Quantize summary" lines report 1.57 GiB, while Qwen's real peak is 0.448 GiB. Label the figure as process-wide, or skip the reset while another run is active.

Checked, clean

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Taking this. I'll investigate against current main, add a targeted regression test, and open a PR. Will report back here if the claim turns out to be invalid or already fixed.

  2. self-assigned this
    on Nov 16, 2026
  3. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    CUDA default now installs expandable_segments:True only (no max_split_size_mb; inert garbage_collection_threshold dropped), ROCm default unchanged for #4313: #4419

  4. Qubitium commented on Nov 20, 2026

    @Qubitium
    CollaboratorAuthor

    Item 2 here (AllSpark keeps both weight layouts resident) is still present at HEAD 85caf6c. The issue was closed by #4419, which only changed the allocator config.

    • gptqmodel/nn_modules/qlinear/allspark.py:287-288: _repack() registers qweight_packed/scales_packed while the checkpoint qweight/scales stay on the GPU. forward() (lines 296-316) reads only the packed copies.
    • gptqmodel/nn_modules/qlinear/allspark_awq.py:339-341: same pattern, plus zeros_packed.

    CPU validation with the repack op replaced by a stand-in that returns the same shapes as utils/allspark.py:allspark_repack. Script: allspark_dual_residency.py, which constructs AllSparkLinear(K=4096, N=11008) and calls post_init(). Output:

    qweight        (checkpoint) : (1024, 11008) torch.int32 43.0 MiB
    qweight_packed (kernel)     : (11008, 4096) torch.uint8 43.0 MiB
    resident module bytes: 86.0 MiB  (ideal ~43.0 MiB)
    forward reads only: ['qweight_packed', 'scales_packed']
    

    Result: W8A16 under AllSpark still uses the same weight VRAM as FP16, double what the format needs. Marlin's approach would fix this: replace qweight in place, set a repacked flag, and fail closed in _save_to_state_dict (marlin.py:482,486-497). If export from a live module has to keep working, allspark_repack is a deterministic permutation, so the inverse could be computed on demand in _save_to_state_dict.

    Found by automated code audit (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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