Skip to content

[MEM] Excluded GPU still claimed during quantize (#4320/#4347 incomplete): torch_sync() synchronizes every visible device (498 MiB context), and QuantizeConfig.compute_device_filter gets full warmup/prewarm (1.48 GiB, 384 MiB peak alloc) #4653

Closed

Description

@Qubitium

Found on main @ 597b39b, RTX 5090 (cuda:0) + RTX 4090 (cuda:1), CUDA 13.2, torch 2.15.

#4320 (fixed by #4347, "skip warmup for devices excluded from quantization") still reproduces in two ways. In both, the GPU the user excluded ends up with a CUDA context, and in the second it also gets allocations.

1. torch_sync() creates a context on the excluded GPU (even with load(device=("cuda:1",)))

gptqmodel/utils/torch.py:564 torch_sync() with no argument calls torch.cuda.synchronize(idx) for every visible CUDA index. The looper calls it unconditionally:

  • looper/stage_layer.py:923, 1342, 1386 (1386 is hit on layer 0 before finalize);
  • looper/forward_executor.py:668, looper/stage_subset.py:1669;
  • quantization/gptaq.py:571, quantization/foem.py:585, 678.

synchronize(0) on a device that has never been touched initializes a full CUDA context there. #4347 made the pool's device activation lazy, so cuda:0 now stays clean through load() and is then claimed on layer 0. I wrapped torch.cuda.* and the first touch of cuda:0 is:

stage_layer.py:1386 run_layer_stage -> torch_sync() -> utils/torch.py:580 torch.cuda.synchronize(0)

2. QuantizeConfig(compute_device_filter=...) is not consulted by any of the #4347 gates

The compute_device_filter field help documents this exact way to exclude GPU 0: compute_device_filter=lambda devices: [d for d in devices if d.index != 0]. _prebuild_device_thread_pool(device) (models/auto.py:747-764) derives warmup_devices only from the device= load kwarg. _kick_quantize_prewarms (auto.py:720) likewise targets the default device. With the filter, the excluded GPU therefore still runs the full linalg warmup plus the quantize/linear prewarms. An allocator history (torch.cuda.memory._record_memory_history) of cuda:0 during load + quantize shows only warmup frames:

2881.9 MiB (cumulative allocs)  utils/linalg_warmup.py:108 _run_qr <- linalg_warmup.py:128 run_torch_linalg_warmup <- utils/threadx.py:374 run
  32.0 MiB  utils/linalg_warmup.py:149 _run_linear_prewarm <- linalg_warmup.py:385 _worker
   3.9 MiB  utils/linalg_warmup.py:94 _run_cholesky_and_eigh <- ...
   ...      utils/linalg_warmup.py:2xx _run_quantize_prewarm <- linalg_warmup.py:385 _worker

Setting GPTQMODEL_POOL_PREWARM=0 does not help: cuda:0 still peaks at 384 MiB allocated.

Repro (Qwen2.5-0.5B-Instruct, GPTQ 4-bit, 32 C4 samples)

import os, sys, torch, subprocess
from gptqmodel import GPTQModel
from gptqmodel.quantization import GPTQConfig
def my_gpu_mib():  # this PID's memory per physical GPU, read from nvidia-smi (no CUDA calls)
    ...
mode = sys.argv[1]
if mode == "filter":
    cfg = GPTQConfig(bits=4, group_size=128, compute_device_filter=lambda ds: [d for d in ds if d.index != 0])
    m = GPTQModel.load("Qwen/Qwen2.5-0.5B-Instruct", cfg, dtype=torch.float16)
else:
    m = GPTQModel.load("Qwen/Qwen2.5-0.5B-Instruct", GPTQConfig(bits=4, group_size=128), dtype=torch.float16, device=("cuda:1",))
print("after load :", my_gpu_mib(), flush=True)
m.quantize(calib, batch_size=1)
print("after quant:", my_gpu_mib(), "peak alloc cuda:0 =", torch.cuda.max_memory_allocated(0) >> 20, "MiB, cuda:1 =", torch.cuda.max_memory_allocated(1) >> 20, "MiB", flush=True)

In the output, phys2 is cuda:0 (the excluded 5090) and phys4 is cuda:1:

== filter   (QuantizeConfig.compute_device_filter excludes cuda:0)
after load : {'phys2': 990, 'phys4': 386}
after quant: {'phys2': 1480, 'phys4': 1164} peak alloc cuda:0 = 384 MiB, cuda:1 = 432 MiB
== loaddev  (load(device=("cuda:1",)), the #4347 path)
after load : {'phys4': 904}
after quant: {'phys2': 498, 'phys4': 1164} peak alloc cuda:0 = 0 MiB, cuda:1 = 432 MiB

So the excluded GPU holds 1.48 GiB (filter path) or a 498 MiB context (device-sequence path). On a GPU that is full or shared with another job, the first case can OOM, as in #4233.

Suggested fix

  • Make torch_sync() synchronize only devices that the run actually uses: the filtered forward/quantize pool, or the devices in the current layer's device map. At minimum, skip indices on which this process has not initialized a context (torch.cuda.is_initialized() is global, so track the pool's activated devices instead).
  • Resolve warmup_devices and the prewarm target from the same filtered pool the loopers use, including quantize_config.compute_device_filter, not only from the device= kwarg. The filter is a callable on the config, so the pool build or warmup kick has to be deferred until the config is known, or the config has to be passed to _prebuild_device_thread_pool.

Test that should have caught it

tests/test_compute_device_filter.py should run, on 2 GPUs, one full layer of a tiny model with (a) device=("cuda:1",) and (b) compute_device_filter excluding 0. Then assert via NVML or torch.cuda.list_gpu_processes(0) that this PID has no context on GPU 0, and that torch.cuda.max_memory_allocated(0) == 0. The #4347 test only checks warmup scheduling at pool build.

Severity

Low-medium. No wrong results, but the GPU the user excluded loses 0.5-1.5 GiB, and the documented compute_device_filter route gets no protection from the fix at all.

🤖 Generated with Claude Code

Activity

  1. ZX-ModelCloud commented on Nov 17, 2026

    @ZX-ModelCloud
    Collaborator

    Claimed. I'm working on this issue and will submit a fix PR. Please avoid duplicate work in the meantime.

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