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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio diffusion: split the batch across two cards for LTX-2, Chroma, Qwen-Image, HunyuanImage and Krea-2, and pair Cosmos 3 guidance (#17278)
Daniel Han14a59fdf170c
diff --git a/studio/backend/core/inference/diffusion_multigpu.py b/studio/backend/core/inference/diffusion_multigpu.pyindex e0371131d5c..ca92fe798cc 100644--- a/studio/backend/core/inference/diffusion_multigpu.py+++ b/studio/backend/core/inference/diffusion_multigpu.py@@ -52,6 +52,23 @@ def multigpu_mode() -> str:return "auto"+MULTIGPU_ALL_ENV = "UNSLOTH_DIFFUSION_MULTIGPU_ALL"+++def multigpu_all_enabled() -> bool:+ """Multi-GPU paths for the remaining families and formats; ``0`` keeps the one-card choices."""+ if multigpu_mode() == "off":+ return False+ return (os.environ.get(MULTIGPU_ALL_ENV) or "").strip().lower() not in (+ "0",+ "off",+ "false",+ "no",+ "disable",+ "disabled",+ )++UNET_CFG_PARALLEL_ENV = "UNSLOTH_DIFFUSION_CFG_PARALLEL_UNET"# Automatic (no gpu_ids) arms CFG-parallel on an idle card of the same model; 0 keeps the replica to explicit picks.AUTO_CFG_PARALLEL_ENV = "UNSLOTH_DIFFUSION_CFG_PARALLEL_AUTO"@@ -2213,6 +2230,15 @@ def _latent_arg(args: tuple, kwargs: dict) -> Any:value = kwargs.get(key)if isinstance(value, torch.Tensor):return value+ # Cosmos 3: ``vision_tokens=[t]``, a new list each call around the same tensor+ vision = kwargs.get("vision_tokens")+ if (+ isinstance(vision, (list, tuple))+ and len(vision) == 1+ and isinstance(vision[0], torch.Tensor)+ and multigpu_all_enabled()+ ):+ return vision[0]for value in args:if isinstance(value, torch.Tensor):return value@@ -3004,11 +3030,11 @@ class CfgParallelRouter:def _batch_split(self, latent: Any, args: tuple, kwargs: dict) -> Any:"""A step without guidance branches (a distilled model, or both branches batched into one call) whose batch iseven: the first half runs on the source, the second on the replica at the same time. Only for denoiser classes- whose every batched input is known (``_DIT_BATCH_SPLIT_KWARGS``); anything else, an odd batch, or the kill- switch keeps the call whole. None = not split (the caller runs it whole)."""+ whose every batched input is known (``_DIT_BATCH_SPLIT_KWARGS`` / ``_ALL``); anything else, an odd+ batch, or the kill switch keeps the call whole. None = not split (the caller runs it whole)."""torch = _torch()module = self.module_ref()- allowed = _DIT_BATCH_SPLIT_KWARGS.get(type(module).__name__) if module is not None else None+ allowed, joins_all = _batch_split_kwargs(module)if allowed is None or _batch_split_disabled() or torch.compiler.is_compiling():return Noneif getattr(module, "_compiled_call_impl", None) is not None:@@ -3023,7 +3049,7 @@ class CfgParallelRouter:)if extra:return self._skip_split(f"unknown inputs {extra}")- if kwargs.get("joint_attention_kwargs"):+ if kwargs.get("joint_attention_kwargs") or (joins_all and kwargs.get("attention_kwargs")):return self._skip_split("attention kwargs")if self._split_scaled is None:from .diffusion_cfg_batch import per_call_scaled@@ -3049,9 +3075,16 @@ class CfgParallelRouter:first, second = resif second is None:second = self._primary(*a1, **k1)+ if joins_all:+ try:+ joined = _join_outputs(first, second)+ except TypeError as exc:+ return self._skip_split(f"unjoinable output ({exc})")+ else:+ joined = Noneself.stats["primary_calls"] += 1self.stats["batch_splits"] += 1- return _join_batch(first, second)+ return joined if joins_all else _join_batch(first, second)def _skip_split(self, reason: str) -> None:self.stats["batch_split_skip"] = reason@@ -3624,9 +3657,127 @@ _DIT_BATCH_SPLIT_KWARGS = {}),}+# Every tensor input is per sample on dim 0 or unbatched (ids / masks); LTX-2 returns (video, audio), both joined.+# Left out: HiDream (its llama states are layers x batch, batched on dim 1), MOSS-Image (positional inputs), Anima+# (Studio's wrapper renders one image per call, so no batch reaches the denoiser), FLUX.3 /+# Qwen-Image 2.1 (a per-branch prefix KV cache object), and an attention_kwargs that is not empty.+_DIT_BATCH_SPLIT_KWARGS_ALL = {+ "LTX2VideoTransformer3DModel": frozenset(+ {+ "hidden_states",+ "audio_hidden_states",+ "encoder_hidden_states",+ "audio_encoder_hidden_states",+ "timestep",+ "audio_timestep",+ "sigma",+ "audio_sigma",+ "encoder_attention_mask",+ "audio_encoder_attention_mask",+ "num_frames",+ "height",+ "width",+ "fps",+ "audio_num_frames",+ "video_coords",+ "audio_coords",+ "isolate_modalities",+ "use_cross_timestep",+ "attention_kwargs",+ "return_dict",+ }+ ),+ "ChromaTransformer2DModel": frozenset(+ {+ "hidden_states",+ "encoder_hidden_states",+ "timestep",+ "img_ids",+ "txt_ids",+ "attention_mask",+ "joint_attention_kwargs",+ "return_dict",+ }+ ),+ "QwenImageTransformer2DModel": frozenset(+ {+ "hidden_states",+ "encoder_hidden_states",+ "encoder_hidden_states_mask",+ "timestep",+ "img_shapes",+ "guidance",+ "attention_kwargs",+ "return_dict",+ }+ ),+ "HunyuanImageTransformer2DModel": frozenset(+ {+ "hidden_states",+ "timestep",+ "timestep_r",+ "encoder_hidden_states",+ "encoder_attention_mask",+ "encoder_hidden_states_2",+ "encoder_attention_mask_2",+ "guidance",+ "attention_kwargs",+ "return_dict",+ }+ ),+ "Krea2Transformer2DModel": frozenset(+ {+ "hidden_states",+ "encoder_hidden_states",+ "timestep",+ "position_ids",+ "encoder_attention_mask",+ "attention_kwargs",+ "return_dict",+ }+ ),+}BATCH_SPLIT_ENV = "UNSLOTH_DIFFUSION_BATCH_SPLIT"+def _batch_split_kwargs(module: Any) -> tuple[Optional[frozenset], bool]:+ """(the inputs a batch split of ``module`` may carry, or None; whether every output tensor is joined)."""+ name = type(module).__name__ if module is not None else None+ allowed = _DIT_BATCH_SPLIT_KWARGS.get(name)+ if allowed is not None:+ return allowed, False+ if name in _DIT_BATCH_SPLIT_KWARGS_ALL and multigpu_all_enabled():+ return _DIT_BATCH_SPLIT_KWARGS_ALL[name], True+ return None, False+++def _join_outputs(+ a: Any,+ b: Any,+ depth: int = 0,+) -> Any:+ """Join two halves' outputs on dim 0: every tensor of a tuple / list / output dict."""+ torch = _torch()+ if isinstance(a, torch.Tensor) and isinstance(b, torch.Tensor):+ return torch.cat([a, b.to(a.device)], dim = 0)+ if (+ depth < 4+ and isinstance(a, (tuple, list))+ and isinstance(b, (tuple, list))+ and len(a) == len(b)+ ):+ joined = [_join_outputs(x, y, depth + 1) for x, y in zip(a, b)]+ return tuple(joined) if isinstance(a, tuple) else joined+ if depth < 4 and isinstance(a, dict) and isinstance(b, dict) and a.keys() == b.keys():+ return type(a)(**{k: _join_outputs(a[k], b[k], depth + 1) for k in a})+ if a is None and b is None:+ return None+ sample_a, sample_b = getattr(a, "sample", None), getattr(b, "sample", None)+ if isinstance(sample_a, torch.Tensor) and isinstance(sample_b, torch.Tensor):+ return type(a)(sample = torch.cat([sample_a, sample_b.to(sample_a.device)], dim = 0))+ raise TypeError(f"unexpected denoiser output {type(a).__name__}")++def _batch_split_disabled() -> bool:return (os.environ.get(BATCH_SPLIT_ENV) or "").strip().lower() in ("0", "off", "false", "no")diff --git a/studio/backend/tests/test_diffusion_multigpu_all_batch_split.py b/studio/backend/tests/test_diffusion_multigpu_all_batch_split.pynew file mode 100644index 00000000000..d410570da09--- /dev/null+++ b/studio/backend/tests/test_diffusion_multigpu_all_batch_split.py@@ -0,0 +1,392 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Batch split for the remaining DiT classes and Cosmos 3 CFG pairing on tiny random diffusers models (CPU)."""++import types++import pytest++torch = pytest.importorskip("torch")+diffusers = pytest.importorskip("diffusers")++from core.inference import diffusion_multigpu as mg # noqa: E402++# literal, not ALL_ENV: on a tree without the switch the tests fail on behaviour, not on a name+ALL_ENV = "UNSLOTH_DIFFUSION_MULTIGPU_ALL"+B = 2+MASK = [[1, 1, 1, 0, 0], [1, 1, 1, 1, 1]]+++def _need(name):+ cls = getattr(diffusers, name, None)+ if cls is None:+ pytest.skip(f"diffusers has no {name}")+ return cls+++def _router(model):+ built = []++ def factory():+ r = mg.build_replica(model, torch.device("cpu"))+ built.append(r)+ return r++ router = mg.CfgParallelRouter(model, primary = "cpu", secondary = "cpu", replica_factory = factory)+ model.forward = router+ return router, built+++def _ltx2():+ m = _need("LTX2VideoTransformer3DModel")(+ in_channels = 8,+ out_channels = 8,+ num_attention_heads = 2,+ attention_head_dim = 8,+ cross_attention_dim = 16,+ audio_in_channels = 8,+ audio_out_channels = 8,+ audio_num_attention_heads = 2,+ audio_attention_head_dim = 8,+ audio_cross_attention_dim = 16,+ num_layers = 2,+ caption_channels = 16,+ )+ frames, height, width, audio_len = 2, 2, 2, 3+ g = torch.Generator().manual_seed(1)+ video_coords = m.rope.prepare_video_coords(+ 1, frames, height, width, torch.device("cpu"), fps = 24.0+ )+ audio_coords = m.audio_rope.prepare_audio_coords(1, audio_len, torch.device("cpu"))+ # LTX2Pipeline batches uncond + cond into one call+ state = {+ "video": torch.randn(1, frames * height * width, 8, generator = g),+ "audio": torch.randn(1, audio_len, 8, generator = g),+ }+ text = torch.randn(B, 5, 16, generator = g)+ audio_text = torch.randn(B, 5, 16, generator = g)+ mask = torch.tensor(MASK)++ def step(model, i):+ t = torch.tensor(1000.0 * (1 - i / 4)).expand(B)+ video, audio = model(+ hidden_states = torch.cat([state["video"]] * 2),+ audio_hidden_states = torch.cat([state["audio"]] * 2),+ encoder_hidden_states = text,+ audio_encoder_hidden_states = audio_text,+ timestep = t,+ sigma = t,+ audio_timestep = t,+ encoder_attention_mask = mask,+ audio_encoder_attention_mask = mask,+ num_frames = frames,+ height = height,+ width = width,+ fps = 24.0,+ audio_num_frames = audio_len,+ video_coords = video_coords.repeat(2, 1, 1, 1),+ audio_coords = audio_coords.repeat(2, 1, 1, 1),+ isolate_modalities = False,+ spatio_temporal_guidance_blocks = None,+ perturbation_mask = None,+ use_cross_timestep = False,+ attention_kwargs = None,+ return_dict = False,+ )+ uv, cv = video.chunk(2)+ ua, ca = audio.chunk(2)+ state["video"] = state["video"] - 0.1 * (uv + 3.0 * (cv - uv))+ state["audio"] = state["audio"] - 0.1 * (ua + 3.0 * (ca - ua))+ return torch.cat([state["video"].flatten(), state["audio"].flatten()])++ return m, step+++def _chroma():+ m = _need("ChromaTransformer2DModel")(+ in_channels = 8,+ num_layers = 1,+ num_single_layers = 1,+ attention_head_dim = 16,+ num_attention_heads = 2,+ joint_attention_dim = 16,+ axes_dims_rope = (4, 6, 6),+ approximator_num_channels = 8,+ approximator_hidden_dim = 16,+ approximator_layers = 1,+ )+ g = torch.Generator().manual_seed(2)+ state = {"x": torch.randn(B, 16, 8, generator = g)}+ kw = dict(+ encoder_hidden_states = torch.randn(B, 5, 16, generator = g),+ txt_ids = torch.zeros(5, 3),+ img_ids = torch.randn(16, 3, generator = g),+ attention_mask = torch.tensor([[1] * 3 + [0] * 2 + [1] * 16, [1] * 21]).bool(),+ joint_attention_kwargs = None,+ return_dict = False,+ )+ return m, _distilled_step(state, kw, lambda i: torch.full((B,), 1 - i / 4))+++def _qwen():+ m = _need("QwenImageTransformer2DModel")(+ patch_size = 2,+ in_channels = 16,+ out_channels = 4,+ num_layers = 2,+ attention_head_dim = 16,+ num_attention_heads = 2,+ joint_attention_dim = 16,+ axes_dims_rope = (4, 6, 6),+ )+ g = torch.Generator().manual_seed(3)+ state = {"x": torch.randn(B, 16, 16, generator = g)}+ kw = dict(+ guidance = None,+ encoder_hidden_states_mask = torch.tensor(MASK),+ encoder_hidden_states = torch.randn(B, 5, 16, generator = g),+ img_shapes = [[(1, 4, 4)]] * B,+ attention_kwargs = None,+ return_dict = False,+ )+ return m, _distilled_step(state, kw, lambda i: torch.full((B,), 1 - i / 4))+++def _hunyuan():+ m = _need("HunyuanImageTransformer2DModel")(+ in_channels = 8,+ out_channels = 8,+ num_attention_heads = 2,+ attention_head_dim = 8,+ num_layers = 1,+ num_single_layers = 1,+ num_refiner_layers = 1,+ text_embed_dim = 16,+ rope_axes_dim = (4, 4),+ guidance_embeds = True,+ )+ g = torch.Generator().manual_seed(4)+ state = {"x": torch.randn(B, 8, 4, 4, generator = g)}+ kw = dict(+ timestep_r = None,+ guidance = torch.full((B,), 3500.0),+ encoder_hidden_states = torch.randn(B, 5, 16, generator = g),+ encoder_attention_mask = torch.tensor(MASK),+ attention_kwargs = None,+ return_dict = False,+ )+ return m, _distilled_step(state, kw, lambda i: torch.full((B,), 1000.0 * (1 - i / 4)))+++def _krea2():+ m = _need("Krea2Transformer2DModel")(+ in_channels = 8,+ num_layers = 2,+ attention_head_dim = 16,+ num_attention_heads = 2,+ num_key_value_heads = 1,+ intermediate_size = 16,+ timestep_embed_dim = 8,+ text_hidden_dim = 16,+ num_text_layers = 2,+ text_num_attention_heads = 2,+ text_num_key_value_heads = 2,+ text_intermediate_size = 16,+ num_layerwise_text_blocks = 1,+ num_refiner_text_blocks = 1,+ axes_dims_rope = (4, 6, 6),+ )+ g = torch.Generator().manual_seed(5)+ image_ids = torch.zeros(4, 4, 3)+ image_ids[..., 1] = torch.arange(4)[:, None]+ image_ids[..., 2] = torch.arange(4)[None, :]+ position_ids = torch.cat(+ [torch.zeros(5, 3), image_ids.reshape(16, 3)]+ ) # Krea2Pipeline.prepare_position_ids+ state = {"x": torch.randn(B, 16, 8, generator = g)}+ kw = dict(+ encoder_hidden_states = torch.randn(B, 5, 2, 16, generator = g),+ position_ids = position_ids,+ encoder_attention_mask = torch.tensor(MASK).bool(),+ attention_kwargs = None,+ return_dict = False,+ )+ return m, _distilled_step(state, kw, lambda i: torch.full((B,), 1 - i / 4))+++def _distilled_step(state, kw, timestep):+ def step(model, i):+ out = model(hidden_states = state["x"], timestep = timestep(i), **kw)[0]+ state["x"] = state["x"] - 0.1 * out+ return state["x"].flatten()++ return step+++CASES = {+ "LTX2VideoTransformer3DModel": _ltx2,+ "ChromaTransformer2DModel": _chroma,+ "QwenImageTransformer2DModel": _qwen,+ "HunyuanImageTransformer2DModel": _hunyuan,+ "Krea2Transformer2DModel": _krea2,+}+STEPS = 4+++def _render(build, routed):+ torch.manual_seed(0)+ model, step = build()+ model.eval()+ router = built = None+ if routed:+ router, built = _router(model)+ with torch.no_grad():+ out = [step(model, i) for i in range(STEPS)][-1]+ return out, model, router, built++[email protected]("name", sorted(CASES))+def test_new_class_batch_splits_and_matches_one_card(name, monkeypatch):+ monkeypatch.delenv(ALL_ENV, raising = False)+ ref, _m, _r, _b = _render(CASES[name], routed = False)+ out, model, router, built = _render(CASES[name], routed = True)+ assert type(model).__name__ == name+ assert router.stats["batch_splits"] == STEPS - 1, router.stats+ assert len(built) == 1+ torch.testing.assert_close(out, ref, rtol = 1e-4, atol = 1e-5)++[email protected]("name", sorted(CASES))+def test_kill_switch_keeps_the_new_classes_whole(name, monkeypatch):+ monkeypatch.setenv(ALL_ENV, "0")+ ref, _m, _r, _b = _render(CASES[name], routed = False)+ out, _model, router, built = _render(CASES[name], routed = True)+ assert router.stats["batch_splits"] == 0 and not built+ torch.testing.assert_close(out, ref, rtol = 0, atol = 0)+ monkeypatch.delenv(ALL_ENV)+ _out, _model, router, built = _render(CASES[name], routed = True)+ assert router.stats["batch_splits"] == STEPS - 1 and len(built) == 1+++def _identity_class(name):+ return type(name, (torch.nn.Module,), {"forward": lambda self, **k: (k["hidden_states"] * 2,)})+++def _distilled_calls(model, steps = 3):+ x = torch.ones(2, 3)+ for i in range(steps):+ x = x - 0.1 * model(hidden_states = x, timestep = torch.ones(2) * i, return_dict = False)[0]+ return x+++def test_existing_classes_are_unchanged_by_the_kill_switch(monkeypatch):+ for value in ("0", "1"):+ monkeypatch.setenv(ALL_ENV, value)+ for name in (+ "FluxTransformer2DModel",+ "Flux2Transformer2DModel",+ "Lumina2Transformer2DModel",+ ):+ model = _identity_class(name)()+ router, built = _router(model)+ _distilled_calls(model)+ assert router.stats["batch_splits"] == 2 and len(built) == 1, (name, value)+ # never added: HiDream batches its llama states on dim 1+ model = _identity_class("HiDreamImageTransformer2DModel")()+ router, built = _router(model)+ _distilled_calls(model)+ assert router.stats["batch_splits"] == 0 and not built+++def test_attention_kwargs_keep_a_new_class_whole(monkeypatch):+ monkeypatch.delenv(ALL_ENV, raising = False)+ torch.manual_seed(0)+ model = type(+ "QwenImageTransformer2DModel",+ (torch.nn.Module,),+ {"forward": lambda self, **k: (k["hidden_states"],)},+ )()+ router, built = _router(model)+ x = torch.ones(2, 3)+ for i in range(3):+ model(hidden_states = x, timestep = torch.ones(2) * i, attention_kwargs = {"scale": 0.5})+ x = x + 1+ assert router.stats["batch_splits"] == 0 and not built+ assert router.stats["batch_split_skip"] == "attention kwargs"+++class Cosmos3OmniTransformer(torch.nn.Module):+ """Shaped like diffusers' Cosmos 3 call: packed per-branch ids, the noisy tokens as a one-element list."""++ def __init__(self):+ super().__init__()+ self.w = torch.nn.Linear(8, 8)+ self.e = torch.nn.Embedding(32, 8)++ def forward(+ self,+ input_ids,+ vision_tokens,+ vision_timesteps,+ return_dict = True,+ ):+ ctx = self.e(input_ids).mean(0)+ preds = [self.w(vision_tokens[0]) * vision_timesteps[:, None] + ctx]+ return preds, None, None+++def _cosmos3_render(model, steps = 5):+ g = torch.Generator().manual_seed(7)+ latents = torch.randn(6, 8, generator = g)+ cond_ids = torch.randint(0, 32, (9,), generator = g)+ uncond_ids = torch.randint(+ 0, 32, (4,), generator = g+ ) # the uncond pack is shorter, as in the pipeline+ for i in range(steps):+ vision = latents.to(+ dtype = torch.float32+ ) # same object: already fp32 (pipeline: latents.to(dtype))+ ts = torch.full((6,), 1.0 - i / steps)+ cond = model(+ input_ids = cond_ids, vision_tokens = [vision], vision_timesteps = ts, return_dict = False+ )[0][0]+ uncond = model(+ input_ids = uncond_ids, vision_tokens = [vision], vision_timesteps = ts, return_dict = False+ )[0][0]+ latents = latents - 0.1 * (uncond + 6.0 * (cond - uncond))+ return latents+++def test_cosmos3_vision_tokens_pair_on_the_replica_and_match_one_card(monkeypatch):+ monkeypatch.delenv(ALL_ENV, raising = False)+ torch.manual_seed(0)+ ref = _cosmos3_render(Cosmos3OmniTransformer())+ torch.manual_seed(0)+ model = Cosmos3OmniTransformer()+ router, built = _router(model)+ with torch.no_grad():+ out = _cosmos3_render(model)+ torch.testing.assert_close(out, ref, rtol = 0, atol = 0)+ assert len(built) == 1 and router.stats["replica_calls"] >= 4, router.stats+ monkeypatch.setenv(ALL_ENV, "0")+ torch.manual_seed(0)+ model = Cosmos3OmniTransformer()+ router, built = _router(model)+ with torch.no_grad():+ out = _cosmos3_render(model)+ torch.testing.assert_close(out, ref, rtol = 0, atol = 0)+ assert not built and router.stats["replica_calls"] == 0+++def test_one_card_selection_arms_nothing_and_two_cards_split(monkeypatch):+ monkeypatch.delenv(ALL_ENV, raising = False)+ torch.manual_seed(0)+ model, _step = _chroma()+ forward = model.forward+ pipe = types.SimpleNamespace(transformer = model)+ assert mg.install_cfg_parallel(pipe, offload_policy = "none") == "one card selected"+ assert model.forward == forward and "forward" not in model.__dict__+ # the same class through the router (what a two-card selection installs) splits+ _out, _model, router, built = _render(_chroma, routed = True)+ assert router.stats["batch_splits"] == STEPS - 1 and len(built) == 1