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: stop MiniMax-H3 recompiling on the second caption and the first i2v (#11880)
Daniel Han6c19cf007932
diff --git a/studio/backend/core/inference/diffusion_dynamic_text.py b/studio/backend/core/inference/diffusion_dynamic_text.pyindex 753578211f3..eab28441571 100644--- a/studio/backend/core/inference/diffusion_dynamic_text.py+++ b/studio/backend/core/inference/diffusion_dynamic_text.py@@ -33,9 +33,19 @@ _QWEN_IMAGE_SOURCES: tuple[str, ...] = (r".*\['image_rotary_emb'\]\[1\]$",)+# MiniMax-H3: packed length S (transformer block), caption length (refiner block), timestep count (temb; i2v adds one).+_MINIMAX_H3_SOURCES: tuple[str, ...] = (+ "L['hidden_states']",+ "L['adaln_indices']",+ "L['rotary_emb'][0]",+ "L['rotary_emb'][1]",+ "L['temb']",+)+_FAMILY_SOURCES: dict[str, tuple[str, ...]] = {"QwenImage21Transformer2DModel": _QWEN_IMAGE_21_SOURCES,"QwenImageTransformer2DModel": _QWEN_IMAGE_SOURCES,+ "MiniMaxH3Transformer3DModel": _MINIMAX_H3_SOURCES,}diff --git a/studio/backend/core/inference/diffusion_quant_pad.py b/studio/backend/core/inference/diffusion_quant_pad.pyindex 33f8718de46..be9553c9ca1 100644--- a/studio/backend/core/inference/diffusion_quant_pad.py+++ b/studio/backend/core/inference/diffusion_quant_pad.py@@ -128,7 +128,12 @@ class PadToMinM(nn.Module):# quantized 1472 -> 2048 Linear maps [0, 1472] to [0, 1472]), which then breaks a downstream# width-sensitive add, so synthesise the empty result at the right width instead of calling through.return x.new_empty((*lead, self.inner.out_features))- if m < self.pad_to:+ if torch.compiler.is_compiling():+ # Branch-free: `m < pad_to` guards a dynamic row count, so an H3 i2v caption past it recompiles.+ # Arithmetic max: dynamo <= 2.8 rejects torch.sym_max on a concrete m; max() left an `m <= pad_to` guard.+ rows = torch.arange((m + self.pad_to + abs(m - self.pad_to)) // 2, device = flat.device)+ out = self.inner(flat.index_select(0, torch.where(rows < m, rows, 0)))[:m]+ elif m < self.pad_to:# Everything below pad_to normalises to pad_to, not just what is below min_m. Clearing the floor takes only# the latter, but pinning ONE row count means one inductor graph covers every prompt length in the range# instead of one per length, and the extra rows are free at these sizes (measured on H3's 13 modules: 1.57diff --git a/studio/backend/tests/test_diffusion_dynamic_text.py b/studio/backend/tests/test_diffusion_dynamic_text.pyindex 487671d0f25..f965d169268 100644--- a/studio/backend/tests/test_diffusion_dynamic_text.py+++ b/studio/backend/tests/test_diffusion_dynamic_text.py@@ -212,3 +212,61 @@ def test_torch_that_reads_the_allowlist_once_is_not_armed(monkeypatch):assert dt.fingerprint(model, None) is Nonemodel(torch.zeros(1))assert model.seen == _cfg().dynamic_sources+++class MiniMaxH3Transformer3DModel(torch.nn.Module):+ def __init__(self):+ super().__init__()+ self.seen = None+ self.block = torch.compile(self._block, backend = "eager", dynamic = None)++ @staticmethod+ def _block(hidden_states, temb, adaln_indices, rotary_emb):+ mod = temb.index_select(0, adaln_indices)+ return (hidden_states * rotary_emb[0] + rotary_emb[1]) * (1 + mod)++ def forward(self, seq_len, n_timesteps):+ self.seen = _cfg().dynamic_sources+ hidden_states = torch.randn(1, seq_len, 8)+ temb = torch.randn(n_timesteps, 8)+ adaln_indices = torch.arange(seq_len) % n_timesteps+ rotary_emb = (torch.randn(seq_len, 8), torch.randn(seq_len, 8))+ return self.block(hidden_states, temb, adaln_indices, rotary_emb)+++def test_minimax_h3_packed_length_is_armed():+ m = MiniMaxH3Transformer3DModel()+ assert dt.install(m) is True+ m(12, 2)+ seen = m.seen.split(",")+ for name in (+ "L['hidden_states']",+ "L['adaln_indices']",+ "L['rotary_emb'][0]",+ "L['rotary_emb'][1]",+ "L['temb']",+ ):+ assert name in seen+ dt.uninstall(m)+++def test_minimax_h3_new_caption_and_i2v_reuse_the_first_graphs():+ from torch._dynamo.utils import counters++ def graphs_per_render(install):+ torch._dynamo.reset()+ counters.clear()+ m = MiniMaxH3Transformer3DModel()+ if install:+ assert dt.install(m)+ seen = []+ for render in ((100, 1), (100, 2), (104, 1), (104, 2), (130, 3)):+ before = counters["stats"]["unique_graphs"]+ m(*render)+ seen.append(counters["stats"]["unique_graphs"] - before)+ dt.uninstall(m)+ torch._dynamo.reset()+ return seen++ assert graphs_per_render(False)[2:] != [0, 0, 0]+ assert graphs_per_render(True)[2:] == [0, 0, 0]diff --git a/studio/backend/tests/test_diffusion_quant_pad.py b/studio/backend/tests/test_diffusion_quant_pad.pyindex 93c352af464..2905361c284 100644--- a/studio/backend/tests/test_diffusion_quant_pad.py+++ b/studio/backend/tests/test_diffusion_quant_pad.py@@ -408,6 +408,40 @@ def test_int8_padding_is_bitwise_exact_on_a_real_quantized_linear():assert padding_is_bitwise_exact(lin, m), f"padding changed the kept rows at M = {m}"+def test_a_dynamic_row_count_compiles_one_graph_for_short_and_long_captions():+ from torch._dynamo.utils import counters++ torch.manual_seed(0)+ wrapped = PadToMinM(_RecordingLinear(64, 48), min_m = 17, pad_to = 32).eval()+ torch._dynamo.reset()+ counters.clear()+ compiled = torch.compile(wrapped, backend = "eager", dynamic = None)+ for m in (19, 21, 540, 7, 32):+ x = torch.randn(1, m, 64)+ torch._dynamo.mark_dynamic(x, 1)+ with torch.no_grad():+ got = compiled(x)+ want = wrapped(x)+ assert got.shape == (1, m, 48)+ assert torch.equal(got, want), m+ if tuple(int(v) for v in torch.__version__.split(".")[:2]) >= (2, 8):+ assert counters["stats"]["unique_graphs"] == 1+ torch._dynamo.reset()++[email protected]("dynamic", [None, False])+def test_a_static_row_count_still_compiles_fullgraph(dynamic):+ torch.manual_seed(0)+ wrapped = PadToMinM(nn.Linear(64, 48), min_m = 17, pad_to = 32).eval()+ torch._dynamo.reset()+ compiled = torch.compile(wrapped, backend = "eager", dynamic = dynamic, fullgraph = True)+ for m in (1, 19, 32, 540):+ x = torch.randn(2, m, 64)+ with torch.no_grad():+ assert torch.equal(compiled(x), wrapped(x)), m+ torch._dynamo.reset()++@pytest.mark.skipif(not torch.cuda.is_available(), reason = "nvfp4 dynamic quant needs Blackwell")def test_the_zero_row_guard_on_a_real_nvfp4_linear():"""A real nvfp4 Linear survives a zero-row call once wrapped."""