Skip to content

fix(threadx): skip warmup for devices excluded from quantization - #4347

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4320-excluded-device-warmup
Nov 16, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4320-excluded-device-warmup

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4320

Summary

The global DeviceThreadPool eagerly created a worker for every visible CUDA device and kicked run_torch_linalg_warmup on all of them during GPTQModel.load. Selecting device=("cuda:1",) still gave the excluded GPU a CUDA context and ~524 MiB of warmup allocations.

Two changes make excluded devices inert:

  1. DeviceThreadPool(warmup_devices=...) restricts the eager warmup no-op to an allow-list. Workers for other devices still exist and can receive work, but they activate lazily.
  2. _DeviceWorker defers _activate_thread_device() until its first warmup/task, so merely constructing the pool no longer creates a CUDA context on every device.

GPTQModel.load() / from_pretrained() derive the allow-list from the existing device=("cuda:1",) selector (_explicit_requested_devices) and pass it to the pool before the pool is built. With no explicit device selector, the historical behavior (warm every discovered accelerator) is unchanged.

Evidence

GPTQModel.load(..., device=("cuda:1",)) with CUDA_VISIBLE_DEVICES=0,1

Before (origin/main):

pid 3701556
COMPUTE_APPS
3701556, GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae, 524 MiB   # GPU 0, excluded
3701556, GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e, 524 MiB   # GPU 1, permitted

After (this branch):

pid 3764090
alloc1 0
COMPUTE_APPS
3703451, GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae, 454 MiB   # other process
3764090, GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e, 524 MiB   # GPU 1, permitted

Our PID has no nvidia-smi compute-app row for GPU 0. The permitted GPU still has its context and warmup.

Direct pool check

Before (default pool):

task 1048576
COMPUTE_APPS
3686813, GPU-1892aea4-... (GPU 0), 524 MiB
3686813, GPU-1ece7ec2-... (GPU 1), 524 MiB

After (get_device_thread_pool(warmup_devices=[torch.device("cuda:1")])):

task 1048576
torch1_alloc 4194304
COMPUTE_APPS
3694677, GPU-1ece7ec2-... (GPU 1), 524 MiB

Tests

tests/test_excluded_device_warmup.py:

  • test_warmup_devices_skips_excluded_device — fake-CUDA unit test that asserts only the permitted worker activates.
  • test_default_pool_still_warms_every_accelerator — no allow-list keeps the historical all-device warmup.
  • test_prebuild_passes_explicit_devices_to_pool — GPTQModel plumbing passes the selector to the pool.
  • test_excluded_cuda_device_has_no_context — subprocess GPU test that runs a task on cuda:1, then parses nvidia-smi --query-compute-apps=pid,gpu_uuid,used_memory and asserts our PID appears for the permitted GPU but not the excluded one.

On origin/main the GPU test fails with the real assertion:

AssertionError: excluded GPU 0 (GPU-1892aea4-...) still has a CUDA context; compute apps:
['3717988, GPU-1892aea4-..., 524 MiB', '3717988, GPU-1ece7ec2-..., 524 MiB', ...]

After the fix:

tests/test_excluded_device_warmup.py 4 passed

Related threadx/compute-filter suite:

tests/test_excluded_device_warmup.py tests/test_threadx_warmup_accounting.py
tests/test_threadx_error_propagation.py tests/test_linalg_warmup.py
tests/test_thread_pool_exit.py tests/test_compute_device_filter.py
tests/test_pre_quantize_staging_device.py
32 passed, 1 skipped

Ruff

ruff check gptqmodel docs/eora tests setup.py
All checks passed!

Residual risk

  • The global pool is built once per process. If some other code built it before GPTQModel.load with no allow-list, excluded devices may already have been warmed; the normal load path builds it first with the selector.
  • Only tuple/list explicit selectors (device=("cuda:1",)) are treated as exclusions, matching the existing compute_device_filter contract. A single string device="cuda:1" is not converted to a filter by the loader.
  • Excluded devices still get idle Python worker threads; they hold no CUDA context and activate only if work is submitted to them.
  • warmup_devices does not change virtual-pool alias creation; the global pool does not use aliases.

The global DeviceThreadPool eagerly created every CUDA worker and kicked
linalg warmup on every visible device. A caller that selected only cuda:1 still
got a CUDA context and ~524 MiB of warmup allocations on cuda:0.

Defer worker device activation until the first task and restrict the eager
warmup no-op to an explicit allow-list. GPTQModel.load/from_pretrained derive
that allow-list from device=("cuda:1",), so excluded GPUs never get a context.
Workers for other devices remain available and activate lazily if work is
submitted to them.

Adds a CPU unit test for the allow-list and a subprocess GPU test that checks
nvidia-smi compute apps for the excluded and permitted GPUs.

Fixes #4320
@Qubitium
Qubitium merged commit 8b139ab into main Nov 16, 2026
8 of 9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4320-excluded-device-warmup branch November 16, 2026 09:18
Qubitium pushed a commit that referenced this pull request Nov 17, 2026
…4688)

#4320/#4347 made the pool's device activation lazy, so `load(device=("cuda:1",))`
kept the excluded GPU clean until quantization started. Three later paths still
touched it anyway:

- `torch_sync()` synchronized every *visible* CUDA index. `synchronize` creates
  a full primary context on a device the process never used, which reserves
  hundreds of MiB (measured 386 MiB) on the excluded GPU.
- `QuantizeConfig.compute_device_filter` was never consulted by the eager
  linalg warmup or the quantize/linear prewarms; both resolved their target
  from the `device=` load kwarg only. The documented filter therefore ran the
  full warmup on the excluded GPU (measured 384 MiB peak allocation).
- The device pool's janitor swept `empty_cache` over every pool device, and
  `device_ctx` collapsed the index-less family selector to `cuda:0`.

Resolve the warmup allow-list from the same filtered pool the loopers consume
(`_quantize_warmup_devices`), apply it to a retained pool through
`DeviceThreadPool.set_warmup_devices`, skip accelerators whose primary context
this process never created (`accelerator_context_claimed`), and keep the
current device for an index-less family selector while device 0 is untouched.
A torch build without the primary-context query, a filter that rejects every
device, and HIP (whose query reports every device active after the first
allocation) all keep the previous behavior.

Regression: tests/test_excluded_device_warmup.py runs a real quantization on
two GPUs through both exclusion routes and asserts the excluded device has no
primary context and no peak allocation.
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.

[BUG] A GPU excluded via device= still gets a CUDA context and linalg warmup allocations from the global DeviceThreadPool (follow-up to #4233)

1 participant