Skip to content

Add MPS (Apple Silicon) support - #155

Open
imperatormk wants to merge 1 commit into
genmoai:mainfrom
imperatormk:main
Open

Add MPS (Apple Silicon) support#155
imperatormk wants to merge 1 commit into
genmoai:mainfrom
imperatormk:main

Conversation

@imperatormk

Copy link
Copy Markdown

Fixes #74

Summary

Full MPS (Apple Silicon) support for Mochi video generation - no more "Torch not compiled with CUDA enabled" errors or black/noisy outputs.

MPS Compatibility Fixes

The core issue was CUDA-specific code paths and precision problems on MPS:

  • RoPE embeddings: Compute in float32 (bf16 loses precision on MPS, causes artifacts)
  • CFG computation: Keep in float32 (critical for proper guidance)
  • VAE decode: Disable bf16 autocast on MPS (causes corruption)
  • Timestep embeddings: Float32 sigma tensors
  • FinalLayer / patch embed: Cast to weight dtype before ops
  • Attention: Added mps_flash mode, SDPA fallback works on MPS
  • Device detection: CUDA → MPS → CPU fallback throughout codebase
  • CUDA guards: Wrapped torch.cuda.* calls to not crash on MPS

Memory Optimizations

  • NF4 quantization: ~20GB → ~5GB model size via --quantize flag
  • Lazy loading: Models loaded on-demand
  • T5 cleanup: Encoder freed after prompt encoding
  • DiT cleanup: Freed before VAE decode on MPS

Optional Dependencies

For additional features, install these MPS-optimized packages:

# NF4 quantization support
pip install mps-bitsandbytes

# Flash attention for MPS
pip install mps-flash-attn

# Native Conv3D for MPS
pip install mps-conv3d

CLI Flags

  • --quantize - NF4 4-bit quantization (requires mps-bitsandbytes)
  • --attention-mode - override: flash, mps_flash, sdpa, sage

Scripts

  • scripts/quantize_dit.py - pre-quantize Genmo DiT
  • scripts/quantize_diffusers_transformer.py - pre-quantize Diffusers

Usage

# Basic MPS
python demos/cli.py --model_dir weights/ --prompt "A cat"

# With quantization (~5GB instead of ~20GB)
python demos/cli.py --model_dir weights/ --quantize --prompt "A cat"

# Pre-quantize weights
python scripts/quantize_dit.py weights/dit.safetensors --dtype bf16

- Device detection: CUDA > MPS > CPU fallback
- NF4 quantization via mps-bitsandbytes (~20GB -> ~5GB)
- MPS flash attention via mps-flash-attn
- Float32 precision for RoPE, CFG, VAE decode on MPS
- Lazy loading for memory-constrained devices
- T5 encoder freed after encoding

CLI flags:
- --quantize: enable NF4 quantization
- --attention-mode: override attention (flash/mps_flash/sdpa/sage)

Scripts:
- scripts/quantize_dit.py: quantize Genmo DiT
- scripts/quantize_diffusers_transformer.py: quantize Diffusers transformer

Misc:
- moviepy 2.x compatibility
- ffmpeg fast path for video encoding
- ray import guard for multi-GPU mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to run on Mac : Torch not compiled with CUDA enabled

1 participant