Skip to content

fix(looper): stop claiming GPUs a quantization run excluded - #4688

Merged
Qubitium merged 1 commit into
mainfrom
fix/4653-excluded-gpu-sync
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/4653-excluded-gpu-sync

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

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/quantize
warmup ignored QuantizeConfig.compute_device_filter, and the pool janitor
swept empty_cache over every device. Net diff: 8 files, ~750 lines, most of
it regression tests.

1. The problem

#4320/#4347 already made the pool's device activation lazy, so
load(device=("cuda:1",)) stayed clean through load. The remaining touches
happened 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;
Loading
Mechanism Before (main @ 9593348)
torch_sync() (gptqmodel/utils/torch.py:564) torch.cuda.synchronize(idx) for every visible index; creates the context
Warmup pool (gptqmodel/models/auto.py:747) warmup_devices derived only from _explicit_requested_devices(device)
Prewarm target (gptqmodel/models/auto.py:697) _resolve_prewarm_device(device, device_map), filter never read
Janitor sweep (gptqmodel/utils/threadx.py:2295-2312) Every _ordered_keys accelerator device
device_ctx (gptqmodel/utils/looper_helpers.py:234) normalize_device_like collapsed the family to cuda:0

Measured on the two-GPU host with a 0.5B model, excluded GPU idle:

Route Before After
compute_device_filter excludes 0 384 MiB peak allocation, live context 0 MiB, no context
load(device=("cuda:1",)) 386 MiB context no context

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;
Loading

accelerator_context_claimed reads torch._C._cuda_hasPrimaryContext, a pure
driver-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

Situation Before After
torch build without _cuda_hasPrimaryContext n/a None keeps synchronize/invalidate exactly as before
compute_device_filter rejects every device loopers keep the full pool warmup keeps the full pool, matching them
No filter and no explicit device= sequence family-wide warmup unchanged (None)
HIP runtime n/a query reports every device active after the first allocation, so ROCm synchronizes all devices; no allocation regression verified

4. Change inventory

Area File Change
Context query gptqmodel/utils/torch.py cuda_primary_context_claimed, accelerator_context_claimed; torch_sync() skips unclaimed indices
Warmup pool gptqmodel/models/auto.py _quantize_warmup_devices, filtered prewarm target, quantize_config threaded through both load entry points
Pool gptqmodel/utils/threadx.py set_warmup_devices for a retained pool; janitor sweep skips unclaimed devices
Context manager gptqmodel/utils/looper_helpers.py family selector no longer claims device 0 while it is unused
Docs docs/multi_gpu_quantization.md new "Excluded devices stay unclaimed" section with the diagrams
Ledger pytorch_gil_0_compat.md, scripts/gil0_api_probe.py new probed API row

5. Validation

Invariant Test node Red Green
Excluded GPU has no context after a real quantization (filter) tests/test_excluded_device_warmup.py::test_quantize_never_claims_the_excluded_device[filter] claimed=True on main pass
Same for device=("cuda:1",) ...[loaddev] claimed=True, before=False pass
Filter narrows the warmup pool test_prebuild_applies_compute_device_filter AttributeError + no filter use pass
Empty filter keeps the full pool test_prebuild_keeps_full_pool_when_filter_rejects_everything fail pass
Prewarm follows the filtered pool test_prewarm_target_follows_the_filtered_pool fail pass
torch_sync skips unclaimed indices test_torch_sync_skips_devices_without_a_context fail pass
torch_sync keeps barriers when the query is absent test_torch_sync_keeps_the_previous_behavior_without_the_query fail pass
Janitor sweep skips unclaimed devices test_janitor_sweep_skips_devices_without_a_context fail pass
Retained pool applies a new allow-list test_set_warmup_devices_narrows_a_retained_pool fail pass
Family selector does not claim unused device 0 test_device_ctx_family_selector_does_not_claim_an_unused_device_0 fail pass
Family selector keeps device-0 behavior when active test_device_ctx_family_selector_keeps_selecting_device_0_when_active pass (control) pass

Red evidence: the same test file on main 959334876 gives 10 failed, 7 passed; on this branch 17 passed. Green runs also cover
test_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.py and test_silent_exception_handling.py.

scripts/gil0_api_probe.py --only torch._C._cuda_hasPrimaryContext passes 8
threads x 4 iterations with PYTHON_GIL=0.

6. Reviewer notes

  • ROCm is not proven by the context assertion: HIP's primary-context query is
    process-wide on the measured wheels, so the GPU test skips there and only
    peak0 == 0 behavior is verified separately. No ROCm allocation increase was
    observed. The _cuda_hasPrimaryContext skip is a no-op on ROCm today.
  • _prebuild_device_thread_pool still tolerates pools without
    set_warmup_devices (fake pools in tests, older singletons).
  • Follow-up not in this PR: none required for the issue.

#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.
Comment thread tests/test_excluded_device_warmup.py Dismissed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants