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 multi-GPU: split an even Lumina 2 batch across two cards (1.7-2.1x, fp-order only) (#16786)

Daniel Han1e50addac6c3
diff --git a/studio/backend/core/inference/diffusion_multigpu.py b/studio/backend/core/inference/diffusion_multigpu.py
index 99ee646b41e..21e25535d82 100644
--- a/studio/backend/core/inference/diffusion_multigpu.py
+++ b/studio/backend/core/inference/diffusion_multigpu.py
@@ -3537,6 +3537,7 @@ class UnetCfgParallelRouter(CfgParallelRouter):
# DiT classes whose batch may be cut along dim 0 (every per-sample input is a dim-0 tensor; ids, when unbatched, keep
# a leading size that is the token count, never the batch). Z-Image passes per-sample lists and has its own split.
+# Lumina 2: a half pads captions to its own longest, and the padding is masked out (fp order only).
_DIT_BATCH_SPLIT_KWARGS = {
"FluxTransformer2DModel": frozenset(
{
@@ -3563,6 +3564,15 @@ _DIT_BATCH_SPLIT_KWARGS = {
"return_dict",
}
),
+ "Lumina2Transformer2DModel": frozenset(
+ {
+ "hidden_states",
+ "timestep",
+ "encoder_hidden_states",
+ "encoder_attention_mask",
+ "return_dict",
+ }
+ ),
}
BATCH_SPLIT_ENV = "UNSLOTH_DIFFUSION_BATCH_SPLIT"
diff --git a/studio/backend/tests/test_diffusion_auto_multigpu_policy.py b/studio/backend/tests/test_diffusion_auto_multigpu_policy.py
index dfef681df37..3c623c02521 100644
--- a/studio/backend/tests/test_diffusion_auto_multigpu_policy.py
+++ b/studio/backend/tests/test_diffusion_auto_multigpu_policy.py
@@ -266,6 +266,83 @@ def test_batch_split_skips_are_recorded_and_stay_exact():
assert "unknown inputs" in router.stats["batch_split_skip"]
+class Lumina2Transformer2DModel(torch.nn.Module):
+ """Named like diffusers' Lumina 2 class: per-sample caption masks ride along dim 0."""
+
+ def __init__(self):
+ super().__init__()
+ self.x = torch.nn.Linear(8, 8)
+ self.t = torch.nn.Linear(6, 8)
+ self.calls = []
+
+ def forward(
+ self,
+ hidden_states,
+ timestep,
+ encoder_hidden_states,
+ encoder_attention_mask,
+ attention_kwargs = None,
+ return_dict = True,
+ ):
+ self.calls.append(int(hidden_states.shape[0]))
+ mask = encoder_attention_mask[:, :, None].to(encoder_hidden_states.dtype)
+ cap = (self.t(encoder_hidden_states) * mask).sum(1, keepdim = True) / mask.sum(
+ 1, keepdim = True
+ )
+ h = self.x(hidden_states) * timestep[:, None, None] + cap
+ if attention_kwargs:
+ h = h * float(attention_kwargs.get("scale", 1.0))
+ return (h,) if not return_dict else type("Out", (), {"sample": h})()
+
+
+def _lumina_loop(model, batch, steps, **extra):
+ g = torch.Generator().manual_seed(9)
+ latents = torch.randn(batch, 16, 8, generator = g)
+ emb = torch.randn(batch, 7, 6, generator = g)
+ mask = torch.zeros(batch, 7, dtype = torch.bool)
+ for i in range(batch):
+ mask[i, : 3 + i % 4] = True # a different caption length per sample
+ for i in range(steps):
+ t = torch.full((batch,), 1.0 - i / steps)
+ out = model(
+ hidden_states = latents,
+ timestep = t,
+ encoder_hidden_states = emb,
+ encoder_attention_mask = mask,
+ return_dict = False,
+ **extra,
+ )[0]
+ latents = latents - 0.1 * out
+ return latents
+
+
+def test_lumina2_batch_splits_with_per_sample_masks_and_matches_one_card():
+ torch.manual_seed(0)
+ ref = _lumina_loop(Lumina2Transformer2DModel(), 4, 4)
+ torch.manual_seed(0)
+ model = Lumina2Transformer2DModel()
+ router, built = _router(model)
+ out = _lumina_loop(model, 4, 4)
+ torch.testing.assert_close(out, ref, rtol = 1e-5, atol = 1e-6)
+ assert router.stats["batch_splits"] == 3 and len(built) == 1
+ assert sorted(set(model.calls[1:])) == [2]
+
+
+def test_lumina2_attention_kwargs_and_kill_switch_stay_whole(monkeypatch):
+ torch.manual_seed(0)
+ model = Lumina2Transformer2DModel()
+ router, built = _router(model)
+ _lumina_loop(model, 2, 3, attention_kwargs = {"scale": 0.5})
+ assert router.stats["batch_splits"] == 0 and not built
+ assert "unknown inputs" in router.stats["batch_split_skip"]
+ monkeypatch.setenv(mg.BATCH_SPLIT_ENV, "0")
+ torch.manual_seed(0)
+ model = Lumina2Transformer2DModel()
+ router, built = _router(model)
+ _lumina_loop(model, 2, 3)
+ assert router.stats["batch_splits"] == 0 and not built
+
+
# --- LoRA weight-only change: mirror the scales onto the replica instead of dropping it ---------------------------