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.

MOSS-Image: build the float64 timestep sinusoid on the CPU off CUDA (#14929)

Daniel Han87378714a1d3
diff --git a/studio/backend/core/inference/diffusion_moss_image.py b/studio/backend/core/inference/diffusion_moss_image.py
index bee913d3cc6..21de8c3372f 100644
--- a/studio/backend/core/inference/diffusion_moss_image.py
+++ b/studio/backend/core/inference/diffusion_moss_image.py
@@ -358,6 +358,15 @@ _ModelMixin, _ConfigMixin = _model_bases()
from diffusers.configuration_utils import register_to_config # noqa: E402
+def _float64_device(device):
+ """Where the float64 timestep sinusoid is built: MPS and Arc XPUs have no float64, so there it is the CPU."""
+ if device.type in ("cuda", "cpu"):
+ return device
+ if device.type == "xpu" and getattr(torch.xpu.get_device_properties(device), "has_fp64", False):
+ return device
+ return torch.device("cpu")
+
+
class MOSSImageTransformer(_ModelMixin, _ConfigMixin):
"""MOSS-Image DiT: a text refiner, then 42 single-stream joint blocks over [image ; text] sharing one AdaLN.
@@ -425,9 +434,11 @@ class MOSSImageTransformer(_ModelMixin, _ConfigMixin):
times = times.expand(batch)
# Training computes the sinusoid in float64 and feeds float32 into its MLP.
half = self.config.freq_dim // 2
- freq = 10000 ** (-torch.arange(half, device = times.device, dtype = torch.float64) / half)
- phase = times.double()[:, None] * freq
- embedding = torch.cat((phase.cos(), phase.sin()), -1).float()
+ where = _float64_device(times.device)
+ freq = 10000 ** (-torch.arange(half, device = where, dtype = torch.float64) / half)
+ # Moved first, then cast: an MPS copy straight into a float64 CPU tensor fails or writes nothing.
+ phase = times.to(where).double()[:, None] * freq
+ embedding = torch.cat((phase.cos(), phase.sin()), -1).float().to(times.device)
time = self.time_embedding(embedding.to(_compute_dtype(self.time_embedding[0]))).float()
mod = self.shared_block_modulation(
time.to(_compute_dtype(self.shared_block_modulation))
diff --git a/studio/backend/tests/test_diffusion_moss_image.py b/studio/backend/tests/test_diffusion_moss_image.py
index f112790e78c..1e2aae5ec21 100644
--- a/studio/backend/tests/test_diffusion_moss_image.py
+++ b/studio/backend/tests/test_diffusion_moss_image.py
@@ -132,6 +132,27 @@ def test_unmasked_forward_takes_no_host_sync(transformers_pair, monkeypatch):
ours(latents, torch.tensor(10.0), embeds, return_dict = False)
+ "device, fp64, where",
+ [
+ ("mps", None, "cpu"),
+ ("xpu", False, "cpu"),
+ ("xpu", True, "xpu"),
+ ("cuda", None, "cuda"),
+ ("cpu", None, "cpu"),
+ ],
+)
+def test_timestep_sinusoid_keeps_float64_off_mps(moss, monkeypatch, device, fp64, where):
+ from types import SimpleNamespace
+
+ import torch
+
+ monkeypatch.setattr(
+ torch.xpu, "get_device_properties", lambda *a, **k: SimpleNamespace(has_fp64 = fp64)
+ )
+ assert moss._float64_device(torch.device(device)).type == where
+
+
def test_rope_tables_are_cached_and_rebuilt_on_moves(transformers_pair):
import torch