You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[MEM] Fused-expert MoE load on cuda:1 (inference load and quantize load with device=("cuda:1",)) creates a 386 MiB CUDA context on cuda:0: defuser's clear_memory() calls torch.cuda.synchronize() on the current device (#4863 class, MoE path) #5105
Found on main @ e75181d, RTX 4090 + RTX 5090 visible as cuda:0 / cuda:1, torch 2.15 + CUDA 13.2, defuser 0.0.31 (also 0.0.28).
What is wrong
Loading any fused-expert MoE model (the families that go through defuser, e.g. OLMoE; Qwen1.5-MoE, which is already per-expert, is not affected) on a non-zero GPU creates a 386 MiB CUDA primary context on cuda:0. That GPU was never requested. Both the inference load (GPTQModel.load(quantized, device="cuda:1")) and the quantize load (GPTQModel.load(model, qcfg, device=("cuda:1",)), which documents that cuda:0 is excluded) are affected. The context stays for the life of the process. This is the #4863 / #4653 class: #4915 fixed the dense load path, and the MoE conversion step still leaks.
Source (found with sys.setprofile + torch._C._cuda_hasPrimaryContext(0)):
gptqmodel/models/loader.py:3080 from_quantized -> _convert_model_with_defuser (loader.py:1094-1116)
defuser/modeling/moe_experts_interface.py:790 prepare_model_for_moe_quantization -> clear_memory()
defuser/utils/device.py:61 clear_memory: `if not device_list: torch.cuda.synchronize(); torch.cuda.empty_cache()`
-> torch.cuda.synchronize() on the *current* device (cuda:0) -> primary context
The quantize load reaches the same defuser.convert_model through loader.py:2390/2395/2440, and the HF bridge through utils/hf_load.py:328-332.
Repro
importtorchfromgptqmodelimportGPTQModelfromgptqmodel.quantization.configimportGPTQConfighas=torch._C._cuda_hasPrimaryContexttorch.cuda.init(); assertnothas(0)
q=GPTQModel.load("OLMoE-2L-GPTQ-4bit", device="cuda:1") # OLMoE-1B-7B-0924 with 2 layers, quantized+saved by GPTQModelprint("inference load on cuda:1 -> ctx on cuda:0:", has(0))
Output (per-process memory from nvidia-smi --query-compute-apps):
Nothing is allocated on cuda:0. It is only the context, but on a shared host that is 386 MiB of a GPU the caller excluded.
Suggested fix
Run the defuser conversion under the target device: with torch.cuda.device(target_index): defuser.convert_model(...) in _convert_model_with_defuser and in hf_load.py, or with a CPU-only guard when the model is still on meta/CPU. Better still, have defuser's clear_memory() skip synchronize() when nothing in the converted module lives on CUDA, or pass device_list from its caller. GPT-QModel pins the defuser version, so the guard in the loader is the quick fix here.
Test that should have caught it
tests/test_load_device_context_4863.py uses a dense checkpoint. Add a fused-expert MoE fixture (the tiny OLMoE / Qwen3-MoE builder in tests/test_checkpoint_quantization.py) to its load-on-cuda:1 case, and to tests/test_excluded_device_warmup.py for the quantize load with device=("cuda:1",).
Severity
Low-medium ([MEM]): 386 MiB plus a live context on an excluded or unused GPU for every fused-expert MoE load.
Activity
Qubitium commentedon Nov 18, 2026
Agent claim released:
agent-4e3a0639ecaffinished this issue.Pull request: #5158
Fixed: defuser conversion now scopes clear_memory to the model's actual device ordinals, so a load on cuda:1 creates no cuda:0 primary context.
fix(loader): scope Defuser's cache flush to the converted model's dev…
Scope clear_memory to the devices a conversion actually uses (#75)
Port every supported model onto the Transformers 5.23 ABI; never patc…