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] optimize() on a model loaded on cuda:1 creates a 388 MiB CUDA context on cuda:0 (EXL3 on first generate; Marlin/AWQ-Marlin on a recompile): inductor's device-less SDPA pattern init runs on the current device, compiled forward has no device guard (#4863 class) #5101
Found on main @ e75181d, RTX 4090 + RTX 5090 visible as cuda:0 / cuda:1, torch 2.15 + CUDA 13.2 (triton 3.9).
What is wrong
A quantized model loaded on a non-zero GPU (device="cuda:1") and then compiled with model.optimize() creates a 388 MiB CUDA primary context on cuda:0 the first time inductor compiles a recompiled frame. cuda:0 is a GPU the caller never asked for, and the context stays for the life of the process. This is the #4863 class (fixed for load), but on the optimize() path.
EXL3 (ExllamaV3Linear): the first generate() after optimize() triggers it.
GPTQ Marlin / AWQ Marlin (AUTO): it happens once a later call recompiles for a new shape, for example a batched generate() followed by a single-prompt one.
GPTQ_TRITON / GPTQ_TORCH: not observed.
Without optimize(), load/forward/generate/save on cuda:1 never touch cuda:0 (checked for Marlin, TritonV2, ExllamaV2, Torch, W4AFP8, AWQ Marlin/GEMM/GEMM-Triton/Torch/ExllamaV2, EXL3, FP8, QQQ, BnB).
A plain Transformers Llama compiled the same way on cuda:1 does not touch cuda:0 either.
Source (found with sys.setprofile + torch._C._cuda_hasPrimaryContext(0)): inductor's lazy pattern init
lazy_init is cached per input_device. The quantized-kernel graph breaks leave frames whose inputs give inductor no device, so it builds its SDPA patterns on "cuda". That is the current device (cuda:0), not the model's device. The compiled forward that optimize() installs (gptqmodel/models/base.py, the compiled-forward proxy __call__ at :461-470) runs without a device guard.
Repro
importtorchfromgptqmodelimportGPTQModelhas=torch._C._cuda_hasPrimaryContextq=GPTQModel.load("Llama-3.2-1B-Instruct-GPTQ-4bits-gp128", device="cuda:1"); q.optimize()
tok=q.tokenizer; tok.padding_side="left"; tok.pad_token=tok.pad_tokenortok.eos_tokenq.generate(**tok(["The capital of France is", "Two plus two equals"], return_tensors="pt", padding=True).to("cuda:1"), max_new_tokens=8)
print("after batch-2 generate, ctx on cuda:0:", has(0))
q.generate(**tok(["The capital of France is"], return_tensors="pt").to("cuda:1"), max_new_tokens=8)
print("after batch-1 generate, ctx on cuda:0:", has(0))
Output (per-process memory from nvidia-smi --query-compute-apps):
after batch-2 generate, ctx on cuda:0: False
after batch-1 generate, ctx on cuda:0: True # cuda:0 388 MiB, cuda:1 1796 MiB
EXL3 (Llama-3.2-1B-Instruct-EXL3-W4-ALL), single generate() after optimize(): ctx on cuda:0 True (388 MiB)
same GPTQ sequence wrapped in `with torch.cuda.device(1):` -> ctx on cuda:0 False
Suggested fix
Enter the model's device for the compiled call: wrap the compiled-forward proxy (and the compiled kernel paths that optimize() installs) in torch.cuda.device(<model's execution device>) / torch.accelerator.device_index(...), so that inductor's device-less defaults resolve to the GPU that holds the model. This also keeps the current device unchanged for the caller. For a multi-device map, use the device of the first input or of the embedding.
Test that should have caught it
tests/test_load_device_context_4863.py covers load + generate only. Extend it with optimize() followed by two generates of different batch sizes (and an EXL3 checkpoint), asserting not torch._C._cuda_hasPrimaryContext(0).
Severity
Low-medium ([MEM]): 388 MiB plus a live context on a GPU the caller excluded. It matters on shared hosts, where another process already uses cuda:0.
Agent claim:agent-4e3a0639ecaf is working on this issue now.
Started 2026-11-18T07:23:31Z. The claim expires 4.0h after this comment was last edited (heartbeat). Other agents: do not start this issue while the claim is live.
optimize() on a model loaded on cuda:1 still creates a cuda:0 context: inductor's device-less SDPA pattern init and the compiled forward lack a device guard
Activity
Qubitium commentedon Nov 18, 2026
Agent claim:
agent-4e3a0639ecafis working on this issue now.Started 2026-11-18T07:23:31Z. The claim expires 4.0h after this comment was last edited (heartbeat). Other agents: do not start this issue while the claim is live.
optimize() on a model loaded on cuda:1 still creates a cuda:0 context: inductor's device-less SDPA pattern init and the compiled forward lack a device guard
fix(compile): scope the compiled forward to the model's device (#5101)
fix(compile): scope the compiled forward to the model's device (Model…