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: one host read per guided step in the CFG-batch and CFG-parallel argument checks (#14734)
Daniel Hancf3134d69249
diff --git a/studio/backend/core/inference/diffusion_cfg_batch.py b/studio/backend/core/inference/diffusion_cfg_batch.pyindex 9c97a123a15..f79f57c68c1 100644--- a/studio/backend/core/inference/diffusion_cfg_batch.py+++ b/studio/backend/core/inference/diffusion_cfg_batch.py@@ -706,15 +706,16 @@ def _matches(snapshots: list, call: tuple) -> bool:actual = mg._leaves(call)if len(actual) != len(snapshots):return False+ tensors = []for (pa, va), (ps, vs) in zip(actual, snapshots):if pa != ps:return Falseif isinstance(vs, torch.Tensor):- if not mg._bitwise_equal(va, vs):- return False+ tensors.append((va, vs))elif not mg._same_value(va, vs):return False- return True+ # Every tensor argument under one host read per device (once per guided step), not one torch.equal each.+ return mg._bitwise_equal_all(tensors)def _pad_text(first: dict, second: dict) -> None:diff --git a/studio/backend/core/inference/diffusion_multigpu.py b/studio/backend/core/inference/diffusion_multigpu.pyindex 27919a2b36f..cc76b436ca5 100644--- a/studio/backend/core/inference/diffusion_multigpu.py+++ b/studio/backend/core/inference/diffusion_multigpu.py@@ -2064,6 +2064,34 @@ def _bitwise_equal(a: Any, b: Any) -> bool:return False+def _bitwise_equal_all(pairs: Any) -> bool:+ """``all(_bitwise_equal(a, b) for a, b in pairs)`` with one host read per device instead of one per pair: each+ pair's comparison stays on its device and only the combined flag is read back. Same verdict as the per-pair form+ (a byte view compared elementwise is what ``torch.equal`` does on it); metadata mismatches still answer on the host+ without touching the device."""+ torch = _torch()+ by_device: dict = {}+ for a, b in pairs:+ if not (isinstance(a, torch.Tensor) and isinstance(b, torch.Tensor)):+ return False+ if a.shape != b.shape or a.dtype != b.dtype or a.device != b.device:+ return False+ if a is b or a.numel() == 0:+ continue+ try:+ if a.device.type == "cpu":+ if not torch.equal(_as_bytes(a), _as_bytes(b)):+ return False+ continue+ by_device.setdefault(a.device, []).append((_as_bytes(a) == _as_bytes(b)).all())+ except Exception: # noqa: BLE001 - a layout that cannot be viewed as bytes is never assumed equal+ return False+ try:+ return all(bool(torch.stack(flags).all().item()) for flags in by_device.values())+ except Exception: # noqa: BLE001 - an unreadable verdict is never assumed equal+ return False++def _unchanged_since_launch(snapshots: list) -> bool:"""True when every (source, launch-time copy) pair still holds the same bytes. One host sync for all pairs, onthe replica's card; it waits for the primary branch (the copy is ordered after it), which the guidance combine@@ -2580,9 +2608,16 @@ class CfgParallelRouter:actual = _leaves((args, kwargs))predicted = pending["leaves"]by_value = pending["by_value"]- ok = len(actual) == len(predicted) and all(- pa == pp and (_bitwise_equal(va, vp) if pa in by_value else _same_value(va, vp))- for (pa, va), (pp, vp) in zip(actual, predicted)+ # Host-side checks first (paths, plain values), then every by-value tensor pair under one device read.+ ok = (+ len(actual) == len(predicted)+ and all(+ pa == pp and (pa in by_value or _same_value(va, vp))+ for (pa, va), (pp, vp) in zip(actual, predicted)+ )+ and _bitwise_equal_all(+ (va, vp) for (pa, va), (_pp, vp) in zip(actual, predicted) if pa in by_value+ ))# the source's root hooks have now run for THIS call: the replica must have seen the same per-call stateok = ok and _hook_state(self.pairs) == pending["hook_state"]diff --git a/studio/backend/tests/test_diffusion_cfg_batch_sync.py b/studio/backend/tests/test_diffusion_cfg_batch_sync.pynew file mode 100644index 00000000000..28a2b7f0c24--- /dev/null+++ b/studio/backend/tests/test_diffusion_cfg_batch_sync.py@@ -0,0 +1,93 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The CFG-batch / CFG-parallel argument checks read one flag per device, not one per tensor (no per-argument host+sync in the guided step), with the same verdict as the per-pair ``_bitwise_equal`` form."""++from __future__ import annotations++import itertools++import pytest+import torch++from core.inference import diffusion_cfg_batch as cb+from core.inference import diffusion_multigpu as mg+++def _cases():+ base = torch.arange(24, dtype = torch.float32).reshape(2, 3, 4)+ nan = torch.tensor([float("nan"), 1.0])+ neg0 = torch.tensor([-0.0, 1.0])+ return [+ (base, base), # same object+ (base, base.clone()), # equal bytes+ (base, base + 1), # different values+ (base, base.reshape(4, 6)), # different shape+ (base, base.double()), # different dtype+ (+ base,+ base.transpose(0, 1).contiguous().transpose(0, 1),+ ), # non-contiguous view of equal values+ (nan, nan.clone()), # NaN: bitwise equal although nan != nan+ (neg0, torch.tensor([0.0, 1.0])), # -0.0 vs 0.0: equal values, different bytes+ (torch.tensor(3.0).expand(5), torch.full((5,), 3.0)), # stride-0 expand+ (torch.empty(0), torch.empty(0)),+ (base, 1.0), # not a tensor+ (torch.ones(3, dtype = torch.bfloat16), torch.ones(3, dtype = torch.bfloat16)),+ ]+++def test_bitwise_equal_all_matches_per_pair_form():+ cases = _cases()+ for a, b in cases:+ assert mg._bitwise_equal_all([(a, b)]) == mg._bitwise_equal(a, b), (a, b)+ for combo in itertools.combinations(cases, 3):+ assert mg._bitwise_equal_all(combo) == all(mg._bitwise_equal(a, b) for a, b in combo)+ assert mg._bitwise_equal_all([]) is True+ assert mg._bitwise_equal_all(iter([(cases[1][0], cases[1][1])])) is True+++def test_matches_verdicts_unchanged():+ x = torch.randn(2, 8)+ t = torch.tensor([0.5])+ call = ((x,), {"timestep": t, "guidance": None, "return_dict": False})+ snap = cb._snapshot(call)+ assert cb._matches(snap, call)+ assert not cb._matches(+ snap, ((x + 1,), {"timestep": t, "guidance": None, "return_dict": False})+ )+ assert not cb._matches(snap, ((x,), {"timestep": t, "guidance": None, "return_dict": True}))+ assert not cb._matches(+ snap, ((x,), {"timestep": t + 1, "guidance": None, "return_dict": False})+ )+ assert not cb._matches(snap, ((x,), {"timestep": t, "guidance": 1.0, "return_dict": False}))+ assert not cb._matches(snap, ((x,), {"timestep": t}))++[email protected](not torch.cuda.is_available(), reason = "needs a CUDA device")+def test_one_host_read_per_step_on_cuda(monkeypatch):+ dev = torch.device("cuda")+ tensors = [torch.randn(4, 64, device = dev) for _ in range(6)]+ call = (tuple(tensors[:2]), {f"k{i}": t for i, t in enumerate(tensors[2:])})+ snap = cb._snapshot(call)+ torch.cuda.synchronize()+ reads, equals = [0], [0]+ orig_item, orig_equal = torch.Tensor.item, torch.equal++ def item(self):+ reads[0] += 1+ return orig_item(self)++ def equal(a, b):+ equals[0] += 1+ return orig_equal(a, b)++ monkeypatch.setattr(torch.Tensor, "item", item)+ monkeypatch.setattr(torch, "equal", equal)+ assert cb._matches(snap, call)+ assert (reads[0], equals[0]) == (1, 0) # six tensor arguments on one device: one host read+ other = dict(call[1], k0 = tensors[2] + 1)+ reads[0] = 0+ assert not cb._matches(snap, (call[0], other))+ assert (reads[0], equals[0]) == (1, 0)