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: report the split's primary card as the execution device for diffusers ModularPipeline (FLUX.3 on two cards) (#14917)

Daniel Han001c3c231d23
diff --git a/studio/backend/core/inference/diffusion_multigpu.py b/studio/backend/core/inference/diffusion_multigpu.py
index f97a37dd2d5..36d0eef7dda 100644
--- a/studio/backend/core/inference/diffusion_multigpu.py
+++ b/studio/backend/core/inference/diffusion_multigpu.py
@@ -1111,34 +1111,55 @@ def _split_home(pipe: Any) -> Any:
return None
-def install_execution_device_patch() -> bool:
- """``DiffusionPipeline._execution_device`` / ``.device`` report the primary card of a split pipeline. diffusers
- returns the first non-CPU component in signature order, which is the VAE (on the secondary) for most families.
- Inert for any pipeline without a split denoiser; also covers ``from_pipe`` siblings, which share the modules."""
+def _pipeline_bases() -> list[type]:
+ """``DiffusionPipeline`` plus ``ModularPipeline`` (FLUX.3 / MiniMax-H3; not a subclass) when this diffusers has it."""
+ bases: list[type] = []
try:
from diffusers import DiffusionPipeline
+ bases.append(DiffusionPipeline)
except Exception: # noqa: BLE001
+ pass
+ try:
+ from diffusers import ModularPipeline
+ bases.append(ModularPipeline)
+ except Exception: # noqa: BLE001 - diffusers before modular pipelines
+ pass
+ return bases
+
+
+def _patch_execution_device(cls: type) -> bool:
+ if getattr(cls, "_unsloth_split_exec_patch", False):
+ return True
+ orig_exec = cls.__dict__.get("_execution_device")
+ orig_dev = cls.__dict__.get("device")
+ if not isinstance(orig_exec, property) or not isinstance(orig_dev, property):
return False
- with _EXEC_PATCH_LOCK:
- if getattr(DiffusionPipeline, "_unsloth_split_exec_patch", False):
- return True
- orig_exec = DiffusionPipeline.__dict__.get("_execution_device")
- orig_dev = DiffusionPipeline.__dict__.get("device")
- if not isinstance(orig_exec, property) or not isinstance(orig_dev, property):
- return False
- def _exec(self: Any) -> Any:
- home = _split_home(self)
- return home if home is not None else orig_exec.fget(self)
+ def _exec(self: Any) -> Any:
+ home = _split_home(self)
+ return home if home is not None else orig_exec.fget(self)
- def _dev(self: Any) -> Any:
- home = _split_home(self)
- return home if home is not None else orig_dev.fget(self)
+ def _dev(self: Any) -> Any:
+ home = _split_home(self)
+ return home if home is not None else orig_dev.fget(self)
- DiffusionPipeline._execution_device = property(_exec, doc = orig_exec.__doc__)
- DiffusionPipeline.device = property(_dev, doc = orig_dev.__doc__)
- DiffusionPipeline._unsloth_split_exec_patch = True
- return True
+ cls._execution_device = property(_exec, doc = orig_exec.__doc__)
+ cls.device = property(_dev, doc = orig_dev.__doc__)
+ cls._unsloth_split_exec_patch = True
+ return True
+
+
+def install_execution_device_patch() -> bool:
+ """``_execution_device`` / ``.device`` report the primary card of a split pipeline. diffusers returns the first
+ non-CPU component (the VAE) or, for a modular pipeline, the first component (the text encoder), so latents landed
+ on the secondary. Inert without a split denoiser; covers ``from_pipe`` siblings. True when ``DiffusionPipeline``
+ is patched."""
+ bases = _pipeline_bases()
+ if not bases:
+ return False
+ with _EXEC_PATCH_LOCK:
+ patched = [_patch_execution_device(cls) for cls in bases]
+ return patched[0]
def companion_names(pipe: Any) -> tuple[str, ...]:
diff --git a/studio/backend/tests/test_diffusion_multigpu.py b/studio/backend/tests/test_diffusion_multigpu.py
index 98b22d1abfe..5ac7c592ef1 100644
--- a/studio/backend/tests/test_diffusion_multigpu.py
+++ b/studio/backend/tests/test_diffusion_multigpu.py
@@ -482,6 +482,42 @@ def test_execution_device_reports_the_primary_of_a_split_pipeline(monkeypatch):
assert pipe.device == before
+def test_execution_device_reports_the_primary_of_a_split_modular_pipeline(monkeypatch):
+ """ModularPipeline reports its first component's card (the text encoder, on the secondary): step 0 device mismatch."""
+ monkeypatch.setattr(mg, "_SPLIT_EVER_PLACED", True)
+ import importlib
+
+ try:
+ diffusers = importlib.import_module("diffusers")
+ except (ImportError, RuntimeError) as exc:
+ pytest.skip(f"diffusers is not importable here: {type(exc).__name__}")
+ ModularPipeline = getattr(diffusers, "ModularPipeline", None)
+ if ModularPipeline is None:
+ pytest.skip("this diffusers has no modular pipelines")
+
+ class Mod(torch.nn.Linear):
+ @property
+ def device(self):
+ return self.weight.device
+
+ class Pipe(ModularPipeline):
+ def __init__(self, text_encoder, transformer):
+ self._component_specs = {"text_encoder": None, "transformer": None}
+ self.text_encoder = text_encoder
+ self.transformer = transformer
+
+ text_encoder = Mod(1, 1).to("meta") # the secondary card of the split
+ transformer = Mod(1, 1) # the primary
+ pipe = Pipe(text_encoder, transformer)
+ assert pipe.device == torch.device("meta") # stock: the first component's card
+ mg.install_execution_device_patch()
+ transformer._unsloth_split_home = torch.device("cpu")
+ assert pipe._execution_device == torch.device("cpu")
+ assert pipe.device == torch.device("cpu")
+ del transformer._unsloth_split_home
+ assert pipe.device == torch.device("meta") # no split: diffusers' own answer again
+
+
def test_place_split_and_fallback(monkeypatch):
class Pipe:
def __init__(self):