Skip to content

[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

Closed

Description

@Qubitium

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

import torch
from gptqmodel import GPTQModel
from gptqmodel.quantization.config import GPTQConfig
has = torch._C._cuda_hasPrimaryContext
torch.cuda.init(); assert not has(0)
q = GPTQModel.load("OLMoE-2L-GPTQ-4bit", device="cuda:1")          # OLMoE-1B-7B-0924 with 2 layers, quantized+saved by GPTQModel
print("inference load on cuda:1 -> ctx on cuda:0:", has(0))

Output (per-process memory from nvidia-smi --query-compute-apps):

inference load on cuda:1 -> ctx on cuda:0: True     {'cuda:0': 386 MiB, 'cuda:1': 1464 MiB}
quantize load, device=("cuda:1",), OLMoE-2L       : True  (cuda:0 386 MiB; quantize peak alloc [0, 3032] MiB)
quantize load, device=("cuda:1",), Qwen1.5-MoE-2L : False (no defuser conversion)
dense GPTQ/AWQ/EXL3/FP8/... loads on cuda:1       : False

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.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-4e3a0639ecaf finished 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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