Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio diffusion: off-GIL CUDA prime does not latch before torch is imported (#14683)

Daniel Han0c2dfdaa1e05
diff --git a/studio/backend/tests/test_cuda_prime_off_gil.py b/studio/backend/tests/test_cuda_prime_off_gil.py
index 8858e8815a1..c48e5d6fd78 100644
--- a/studio/backend/tests/test_cuda_prime_off_gil.py
+++ b/studio/backend/tests/test_cuda_prime_off_gil.py
@@ -138,6 +138,28 @@ def test_prime_never_imports_torch(fake_cdll, monkeypatch):
assert opened == [] and "torch" not in sys.modules
+def test_a_call_before_torch_is_imported_does_not_latch(fake_cdll, monkeypatch):
+ calls, opened, _gate = fake_cdll
+ monkeypatch.delitem(sys.modules, "torch", raising = False)
+ assert prime.prime_cuda_driver() is False
+ assert opened == [] and not prime._done.is_set()
+ assert prime.prime_cuda_driver(_fake_torch(calls)) is True
+ assert calls == [("cuInit", 0)]
+ assert prime.prime_cuda_driver(_fake_torch(calls)) is False
+ assert calls == [("cuInit", 0)]
+
+
+def test_a_call_while_torch_is_still_importing_does_not_latch(fake_cdll, monkeypatch):
+ calls, opened, _gate = fake_cdll
+ half = SimpleNamespace(version = SimpleNamespace(cuda = "12.8", hip = None))
+ monkeypatch.setitem(sys.modules, "torch", half)
+ assert prime.prime_cuda_driver() is False
+ assert opened == [] and not prime._done.is_set()
+ monkeypatch.delitem(sys.modules, "torch")
+ assert prime.prime_cuda_driver(_fake_torch(calls)) is True
+ assert calls == [("cuInit", 0)]
+
+
def test_prime_never_raises_without_a_driver(monkeypatch):
import ctypes
diff --git a/studio/backend/utils/cuda_driver_prime.py b/studio/backend/utils/cuda_driver_prime.py
index c2f73ac493d..a5679955b0c 100644
--- a/studio/backend/utils/cuda_driver_prime.py
+++ b/studio/backend/utils/cuda_driver_prime.py
@@ -66,6 +66,12 @@ def prime_cuda_driver(torch_module = None) -> bool:
global _claimed
if _done.is_set():
return False
+ if torch_module is None:
+ # Never imports torch (would deadlock mid-import waiters). Torch absent or mid-import (no ``torch.cuda`` yet)
+ # must not latch, so hardware detection's later call still primes.
+ torch_module = sys.modules.get("torch")
+ if torch_module is None or getattr(torch_module, "cuda", None) is None:
+ return False
with _claim_lock:
first = not _claimed
_claimed = True
@@ -75,11 +81,6 @@ def prime_cuda_driver(torch_module = None) -> bool:
try:
if not _driver_names():
return False
- if torch_module is None:
- # Never imports torch: a caller still inside `import torch` would deadlock its waiters.
- torch_module = sys.modules.get("torch")
- if torch_module is None:
- return False
if not _should_prime(torch_module):
return False
return _cu_init()