Skip to content

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

Merged
danielhanchen merged 4 commits into
mainfrom
fix-moss-image-mps-float64
Oct 24, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
fix-moss-image-mps-float64

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Problem

MOSSImageTransformer._time_modulation (#14552) builds the timestep sinusoid in float64 on the model's device:

freq = 10000 ** (-torch.arange(half, device = times.device, dtype = torch.float64) / half)
phase = times.double()[:, None] * freq

MPS has no float64 (https://docs.pytorch.org/docs/main/notes/mps.html, double precision types), and PYTORCH_ENABLE_MPS_FALLBACK does not cover dtypes, so on Apple Silicon the first denoise step of a MOSS-Image safetensors render raises. Studio routes MOSS safetensors on MPS to this in-tree pipeline (sd.cpp on MPS is opt-in through UNSLOTH_DIFFUSION_SD_CPP_MPS), so every Mac render of it fails. Intel Arc XPUs have no native float64 either.

Change

The sinusoid is built on the model's device on CUDA, CPU and XPUs that report has_fp64 (Data Center GPU Max), exactly as before (bitwise the reference there), and on the CPU everywhere else (MPS, Arc XPUs), then moved over as float32. The timestep is moved to the CPU before the float64 cast, since a dtype-converting copy out of MPS into a float64 CPU tensor fails or, on torch 2.14, writes nothing (pytorch/pytorch#197715). The value fed to the time MLP is float32 in every case, as in the reference.

Tests

studio/backend/tests/test_diffusion_moss_image.py:

  • new test_timestep_sinusoid_keeps_float64_off_mps: MPS and an XPU without float64 map to the CPU; CUDA, CPU and an XPU with float64 stay in place. Fails on main, passes here.
  • the existing bitwise-parity tests against the reference forward and pipeline still pass.

test_diffusion_moss_image.py + test_diffusion_moss_fused.py: 49 passed, 1 skipped on this branch (CPU).

Not covered: a real Apple Silicon render; this host has no Mac. The MPS failure is from the code path and the PyTorch MPS dtype documentation, not a run.

MPS has no float64, so the first denoise step raised on Apple Silicon.
CUDA and CPU keep computing it in place, bitwise as before.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T06:00:35.016773Z 7c17a6f Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 45dc8ee76b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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)
phase = times.to(where, torch.float64)[:, None] * freq

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Move to CPU before casting the timestep to float64

On the MPS path this change is intended to repair, times is still an MPS tensor, so .to(where, torch.float64) requests a dtype-converting MPS-to-CPU copy. Supported PyTorch releases such as 2.13 reject this operation, while 2.14.0 can silently return uninitialized data, meaning the first denoise step still fails or computes corrupt timestep phases; the upstream MPS report specifically identifies x.cpu().to(torch.float64) as the safe ordering. Move the tensor to CPU while retaining float32, then cast it to float64 there.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7c17a6f: the timestep is moved to the CPU as float32 first and cast to float64 there (times.to(where).double()), the ordering pytorch/pytorch#197715 identifies as safe.


def _float64_device(device):
"""Where the float64 timestep sinusoid is built: MPS (and Arc XPUs) have no float64, so off CUDA it is the CPU."""
return device if device.type in ("cuda", "cpu") else torch.device("cpu")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retain XPU float64 when the selected device supports it

When Studio runs on a supported Intel Data Center GPU Max/PVC device, this blanket type check sends the sinusoid to the CPU even though that XPU has native FP64; the repository explicitly recognizes PVC as an XPU device, and Intel documents its FP64 execution capability. This adds an accelerator-to-host-to-accelerator synchronization to every transformer invocation and changes the backend used for the transcendental calculation unnecessarily. Query the selected XPU's FP64 capability (PyTorch exposes get_device_properties(...).has_fp64) and fall back only for devices such as Arc that actually lack it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7c17a6f: an XPU whose get_device_properties reports has_fp64 keeps the sinusoid on the device; only XPUs without float64 (Arc) and MPS use the CPU. Added parametrized cases for both XPU kinds.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 7c17a6f98c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 8737871 into main Oct 24, 2026
36 of 42 checks passed
@danielhanchen
danielhanchen deleted the fix-moss-image-mps-float64 branch October 24, 2026 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant