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: multistage companion and CFG replica fit checks do not count CUDA graph pool cache as free (#15069)

Daniel Han8262da203384
diff --git a/studio/backend/core/inference/diffusion_multigpu.py b/studio/backend/core/inference/diffusion_multigpu.py
index fccf44aef54..57d774fbd68 100644
--- a/studio/backend/core/inference/diffusion_multigpu.py
+++ b/studio/backend/core/inference/diffusion_multigpu.py
@@ -3004,15 +3004,12 @@ class CfgParallelRouter:
return mirror
def _secondary_free_total_mib(self) -> tuple[Optional[int], Optional[int]]:
- """(free, total) MiB on the replica card; free counts the allocator's cached-but-unused blocks."""
+ """(free, total) MiB on the replica card; free counts the allocator's cached-but-unused blocks, minus CUDA graph pools."""
free, total = _free_total_mib(self.secondary.index)
if free is not None:
with contextlib.suppress(Exception):
- torch = _torch()
- free += (
- torch.cuda.memory_reserved(self.secondary)
- - torch.cuda.memory_allocated(self.secondary)
- ) // (1 << 20)
+ from .diffusion_memory import cuda_reusable_cache_bytes
+ free += cuda_reusable_cache_bytes(self.secondary) // (1 << 20)
return free, total
def _run_replica(self, args: tuple, kwargs: dict) -> Any:
diff --git a/studio/backend/core/inference/diffusion_multistage.py b/studio/backend/core/inference/diffusion_multistage.py
index c94c0bd6245..edd5c1db7fb 100644
--- a/studio/backend/core/inference/diffusion_multistage.py
+++ b/studio/backend/core/inference/diffusion_multistage.py
@@ -37,7 +37,8 @@ def _denoisers(pipe: Any) -> list:
def companions_fit_beside_denoiser(pipe: Any, companions: Iterable[Any]) -> bool:
"""Whether the denoiser a call onloaded can stay while ``companions`` (the modules that run before it is needed
- again) are onloaded beside it: free memory plus the allocator's idle cache covers them with a margin."""
+ again) are onloaded beside it: free memory plus the allocator's idle cache (minus CUDA graph pools) covers them
+ with a margin."""
try:
import torch
@@ -46,8 +47,9 @@ def companions_fit_beside_denoiser(pipe: Any, companions: Iterable[Any]) -> bool
if param is None or param.device.type != "cuda":
return False
free, total = torch.cuda.mem_get_info(param.device)
- idle = torch.cuda.memory_reserved(param.device) - torch.cuda.memory_allocated(param.device)
- from .diffusion_memory import _user_budget_reserve_mib
+ from .diffusion_memory import _user_budget_reserve_mib, cuda_reusable_cache_bytes
+
+ idle = cuda_reusable_cache_bytes(param.device)
reserve = _user_budget_reserve_mib(int(total) >> 20) << 20
except Exception: # noqa: BLE001 -- no CUDA / no denoiser: keep the stock offload
diff --git a/studio/backend/tests/test_diffusion_memory_vram_budget.py b/studio/backend/tests/test_diffusion_memory_vram_budget.py
index d0002207c13..eabe9a633b7 100644
--- a/studio/backend/tests/test_diffusion_memory_vram_budget.py
+++ b/studio/backend/tests/test_diffusion_memory_vram_budget.py
@@ -738,3 +738,41 @@ def test_backend_entry_points_run_under_their_budget_scope():
assert (owner, True) in _scopes(getattr(cls, name)), (cls.__name__, name)
for name in gens:
assert (owner, False) in _scopes(getattr(cls, name)), (cls.__name__, name)
+
+
+def test_denoiser_hold_fit_does_not_count_graph_held_cache(monkeypatch):
+ torch = pytest.importorskip("torch")
+ from core.inference import diffusion_multistage as MS
+
+ param = types.SimpleNamespace(device = torch.device("cuda", 0))
+ dit = types.SimpleNamespace(parameters = lambda: iter([param]))
+ monkeypatch.setattr(MS, "_denoisers", lambda pipe: [dit])
+ monkeypatch.setattr(MS, "HOLD_MARGIN_BYTES", 1200 * 2**20)
+ monkeypatch.setattr(torch.cuda, "mem_get_info", lambda *a: (1000 * 2**20, 24 * GiB << 20))
+ monkeypatch.setattr(torch.cuda, "memory_reserved", lambda device = None: 600 * 2**20)
+ monkeypatch.setattr(torch.cuda, "memory_allocated", lambda device = None: 0)
+ monkeypatch.setattr(torch.cuda, "current_device", lambda: 0)
+ monkeypatch.setattr(dm, "_graph_held_cache_bytes", lambda device = None: 500 * 2**20)
+ # 1000 free + 100 reusable = 1100 MiB < 1200 need; counting the 500 graph-held gave 1600.
+ assert MS.companions_fit_beside_denoiser(object(), ()) is False
+ monkeypatch.setattr(dm, "_graph_held_cache_bytes", lambda device = None: 0)
+ assert MS.companions_fit_beside_denoiser(object(), ()) is True # no graph pool: unchanged
+
+
+def test_cfg_replica_free_does_not_count_graph_held_cache(monkeypatch):
+ torch = pytest.importorskip("torch")
+ from core.inference import diffusion_multigpu as mg
+
+ seen = []
+ monkeypatch.setattr(mg, "_free_total_mib", lambda ordinal: (1000, 24 * GiB))
+ monkeypatch.setattr(torch.cuda, "memory_reserved", lambda device = None: 600 * 2**20)
+ monkeypatch.setattr(torch.cuda, "memory_allocated", lambda device = None: 0)
+ monkeypatch.setattr(torch.cuda, "current_device", lambda: 1)
+ monkeypatch.setattr(
+ dm, "_graph_held_cache_bytes", lambda device = None: seen.append(device) or 500 * 2**20
+ )
+ router = types.SimpleNamespace(secondary = torch.device("cuda", 1))
+ assert mg.CfgParallelRouter._secondary_free_total_mib(router) == (1100, 24 * GiB)
+ assert seen == [1] # the replica card's pools, not the primary's
+ monkeypatch.setattr(dm, "_graph_held_cache_bytes", lambda device = None: 0)
+ assert mg.CfgParallelRouter._secondary_free_total_mib(router) == (1600, 24 * GiB)