Fix MPS float64 crash in MiniMax-H3 PrepareLayoutStep - #14648
Open
Greninja44 wants to merge 1 commit into
Open
Conversation
position_ids is built in float64 for cumsum precision over long rotary time spans, but MPS has no float64 support, so moving it to an MPS device crashed. Downcast to float32 only on device transfer, only for MPS/NPU, using the existing maybe_adjust_dtype_for_device helper already used throughout the codebase for this exact limitation. The rope embedding already casts position_ids to float32 on every device before use, so this changes nothing downstream. Fixes huggingface#14639
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MiniMaxH3PrepareLayoutStepandMiniMaxH3Ref2VAPrepareLayoutStepbuildposition_idsin float64 (needed forcumsumprecision over long-video rotary time spans, see_temporal_position_grid), then unconditionally move it to the execution device. This crashes on MPS withCannot convert a MPS Tensor to float64.The float64 computation itself is unchanged. The fix only adjusts the dtype used for the device transfer, via
maybe_adjust_dtype_for_device— a helper already used throughout the codebase for this exact MPS/NPU limitation (transformer_flux.py,transformer_bria.py,embeddings.py,transformer_anyflow_far.py, etc.), rather than a one-off inline device check. It downcasts to float32 only on MPS/NPU and is a no-op everywhere else.This is safe because
MiniMaxH3RotaryPosEmbed.forwardalready unconditionally castsposition_idsto float32 before computing rotary frequencies, on every device — so the fix changes zero downstream numerics, on any device.Changes
src/diffusers/modular_pipelines/minimax_h3/before_denoise.py: bothposition_ids.to(device)call sites now passdtype=maybe_adjust_dtype_for_device(torch.float64, device).tests/modular_pipelines/minimax_h3/test_modular_pipeline_minimax_h3.py: addsTestMiniMaxH3PositionIdsDevice, covering:Fixes #14639
Test plan
pytest tests/modular_pipelines/minimax_h3/test_modular_pipeline_minimax_h3.py::TestMiniMaxH3PositionIdsDevice— 4 passed, 1 skipped (no MPS hardware in CI sandbox)pytest tests/modular_pipelines/minimax_h3/test_modular_pipeline_minimax_h3.py::TestMiniMaxH3Reference— 6 passed (no regression)pytest tests/models/transformers/test_models_transformer_minimax_h3.py— 28 passed, 49 skipped (unrelated hardware/LoRA features)ruff check/ruff format --checkon changed files — clean