Repository navigation
fix(looper): stop claiming GPUs a quantization run excluded - #4688
Merged
Merged
Conversation
#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.
This was referenced Nov 17, 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.
Fixes #4653.
TL;DR
A run that excludes a GPU now leaves that GPU without a context and without
allocations. Three paths outside the filtered pool created one anyway:
torch_sync()synchronized every visible index, the eager linalg/quantizewarmup ignored
QuantizeConfig.compute_device_filter, and the pool janitorswept
empty_cacheover every device. Net diff: 8 files, ~750 lines, most ofit regression tests.
1. The problem
#4320/#4347already made the pool's device activation lazy, soload(device=("cuda:1",))stayed clean through load. The remaining toucheshappened during quantization:
flowchart TD A["quantize()"] --> B["stage_subset._run_single_subset_pass"] B --> C["torch_sync()"] C --> D["for idx in range(device_count)"] D --> E["torch.cuda.synchronize idx"] E --> F["primary context created on the excluded GPU"] L["GPTQModel.load"] --> M["_kick_quantize_prewarms device kwarg only"] M --> N["_resolve_prewarm_device -> cuda:0"] N --> O["kick_torch_linear_prewarm / kick_torch_quantize_prewarm"] O --> P["linalg + quantize warmup on the excluded GPU"] L --> Q["_prebuild_device_thread_pool device kwarg only"] Q --> R["DeviceThreadPool warmup_devices"] R --> P S["DP-Janitor trigger"] --> T["sweep every pool device"] T --> U["torch_empty_cache device"] U --> F classDef failure fill:#ffe8e8,stroke:#b30000,color:#111; class F,P failure;main@ 9593348)torch_sync()(gptqmodel/utils/torch.py:564)torch.cuda.synchronize(idx)for every visible index; creates the contextgptqmodel/models/auto.py:747)warmup_devicesderived only from_explicit_requested_devices(device)gptqmodel/models/auto.py:697)_resolve_prewarm_device(device, device_map), filter never readgptqmodel/utils/threadx.py:2295-2312)_ordered_keysaccelerator devicedevice_ctx(gptqmodel/utils/looper_helpers.py:234)normalize_device_likecollapsed the family tocuda:0Measured on the two-GPU host with a 0.5B model, excluded GPU idle:
compute_device_filterexcludes 0load(device=("cuda:1",))2. The new flow
flowchart TD A["GPTQModel.load device kwarg + QuantizeConfig"] --> B["_quantize_warmup_devices"] B --> C{"explicit ordinals or compute_device_filter?"} C -- "neither" --> D["None: warm the whole family as before"] C -- "either" --> E["select_forward_devices base_device"] E --> F["compute_device_filter candidates"] F -- "empty" --> D F -- "non-empty" --> G["warmup_devices = filtered pool"] G --> H["_prebuild_device_thread_pool -> resize/set_warmup_devices"] G --> I["_resolve_prewarm_device -> first filtered device"] H --> J["DeviceThreadPool._kick_worker_warmups"] I --> K["kick_torch_linear_prewarm / kick_torch_quantize_prewarm"] L["torch_sync()"] --> M{"accelerator_context_claimed idx?"} M -- "no" --> N["skip"] M -- "yes" --> O["torch.cuda.synchronize idx"] P["DP-Janitor sweep"] --> M Q["device_ctx family selector"] --> R{"device 0 already claimed?"} R -- "no" --> S["keep the current device"] R -- "yes" --> T["torch.cuda.device 0, historical behavior"] classDef failure fill:#ffe8e8,stroke:#b30000,color:#111; class N,S failure;accelerator_context_claimedreadstorch._C._cuda_hasPrimaryContext, a puredriver-state query that does not create the context. Skipping an untouched
device cannot lose a barrier: enqueueing work is what creates the context.
3. Failure and ambiguity contract
_cuda_hasPrimaryContextNonekeeps synchronize/invalidate exactly as beforecompute_device_filterrejects every devicedevice=sequenceNone)4. Change inventory
gptqmodel/utils/torch.pycuda_primary_context_claimed,accelerator_context_claimed;torch_sync()skips unclaimed indicesgptqmodel/models/auto.py_quantize_warmup_devices, filtered prewarm target,quantize_configthreaded through both load entry pointsgptqmodel/utils/threadx.pyset_warmup_devicesfor a retained pool; janitor sweep skips unclaimed devicesgptqmodel/utils/looper_helpers.pydocs/multi_gpu_quantization.mdpytorch_gil_0_compat.md,scripts/gil0_api_probe.py5. Validation
tests/test_excluded_device_warmup.py::test_quantize_never_claims_the_excluded_device[filter]claimed=Trueonmaindevice=("cuda:1",)...[loaddev]claimed=True, before=Falsetest_prebuild_applies_compute_device_filterAttributeError+ no filter usetest_prebuild_keeps_full_pool_when_filter_rejects_everythingtest_prewarm_target_follows_the_filtered_pooltorch_syncskips unclaimed indicestest_torch_sync_skips_devices_without_a_contexttorch_synckeeps barriers when the query is absenttest_torch_sync_keeps_the_previous_behavior_without_the_querytest_janitor_sweep_skips_devices_without_a_contexttest_set_warmup_devices_narrows_a_retained_pooltest_device_ctx_family_selector_does_not_claim_an_unused_device_0test_device_ctx_family_selector_keeps_selecting_device_0_when_activeRed evidence: the same test file on
main959334876gives10 failed, 7 passed; on this branch17 passed. Green runs also covertest_pre_quantize_staging_device.py,test_compute_device_filter.py,test_torch.py,test_looper_helpers.py,test_runtime_config_4448.py,test_backend_config.py,test_backend_config_reuse_4446.py,test_stage_modules.pyandtest_silent_exception_handling.py.scripts/gil0_api_probe.py --only torch._C._cuda_hasPrimaryContextpasses 8threads x 4 iterations with
PYTHON_GIL=0.6. Reviewer notes
process-wide on the measured wheels, so the GPU test skips there and only
peak0 == 0behavior is verified separately. No ROCm allocation increase wasobserved. The
_cuda_hasPrimaryContextskip is a no-op on ROCm today._prebuild_device_thread_poolstill tolerates pools withoutset_warmup_devices(fake pools in tests, older singletons).