Sign In

Fixing Black Images in ComfyUI on Mac M1/M2 (PyTorch 2.6.0 & MPS)

6
Fixing Black Images in ComfyUI on Mac M1/M2 (PyTorch 2.6.0 & MPS)

APPLE USERS // Fixing Black Images in ComfyUI on Mac M1/M2 (PyTorch 2.6.0 & MPS)

Introduction :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Many Mac users running ComfyUI on an M1/M2 chip have encountered an issue where images generated using DPM++ 2 SDE (or similar samplers) come out completely black. Interestingly, AUTOMATIC1111 (A1111) works fine, but ComfyUI does not. This article provides a complete fix for the issue by forcing FP32 precision on MPS and resolving sampler inconsistencies.

🧶 Why This Happens

The core issue stems from PyTorch 2.6.0's handling of FP16 (half-precision) on MPS (Metal Performance Shaders). ComfyUI defaults to using FP16 for efficiency, but on Mac, this can cause certain samplers (like DPM++ 2 SDE) to fail, resulting in black images.

// Key Differences Between A1111 & ComfyUI

  1. Different sampler implementations: A1111 and ComfyUI handle samplers differently under the hood.

  2. MPS and FP16 problems: ComfyUI defaults to FP16 on MPS, while A1111 may use FP32.

  3. PyTorch 2.6.0 changes: PyTorch recently altered how precision is handled, which affects Mac users.

To fix this, we need to manually force FP32 precision in ComfyUI’s model_management.py file.


🛠 Step-by-Step Fix

// Quick Solution: Use Command-Line Flags

If you don’t want to make permanent code edits, simply launch ComfyUI with the following flags:

python main.py --force-fp32 --fp32-unet --fp32-vae

This forces FP32 precision without modifying the source code.


// Permanent Fix: Modify model_management.py

If you prefer a permanent fix, follow these steps:

Edit ComfyUI/comfy/model_management.py to Force FP32

Find this section in the file:

if args.force_fp32:
    logging.info("Forcing FP32, if this improves things please report it.")
    FORCE_FP32 = True

if args.force_fp16:
    logging.info("Forcing FP16.")
    FORCE_FP16 = True

Replace it with:

FORCE_FP32 = True
FORCE_FP16 = False
logging.info("Forcing FP32 globally for MPS stability.")

This ensures that FP16 is completely disabled and ComfyUI always runs in FP32 mode.


// Modify UNet, VAE, and Text Encoder Precision

Now, we need to manually set FP32 precision for ComfyUI’s key components.

Fix unet_dtype

Find this section function:

def unet_dtype(device=None, model_params=0, supported_dtypes=[torch.float16, torch.bfloat16, torch.float32]):

Modify it to always return FP32:

return torch.float32

Fix vae_dtype

Find this section function:

def vae_dtype(device=None, allowed_dtypes=[]):

Change:

return torch.float16

To:

return torch.float32

Fix text_encoder_dtype

Find this section function:

def text_encoder_dtype(device=None):

Change:

return torch.float16

To:

return torch.float32

📌 This ensures that FP32 precision is used everywhere.


// Enable MPS Fallback & Tokenizers

If certain operations don’t fully support MPS, PyTorch can revert to CPU seamlessly:

  1. Open your shell configuration file (e.g., ~/.zshrc) using nano:

    nano ~/.zshrc
  2. Locate the section with other export lines, which configure environment variables. Add the following line next to them:

    export PYTORCH_ENABLE_MPS_FALLBACK=1
    export RUSTFLAGS="-A invalid_reference_casting"
  3. Save the changes by pressing CTRL + O, then press Enter to confirm. Exit nano by pressing CTRL + X.

  4. Reload your shell configuration:

    source ~/.zshrc
  5. Check:

    echo $PYTORCH_ENABLE_MPS_FALLBACK
    echo $RUSTFLAGS

    Pytorch should print 1.
    Rust should print -C target-cpu=native.


// Restart ComfyUI

After modifying model_management.py, restart ComfyUI:

python main.py

🛟 Downgrade PyTorch (Last Resort)

If black images persist after forcing FP32, the problem may be PyTorch 2.6.0 itself. Try rolling back to PyTorch 2.3.1:

pip install torch==2.3.1 torchvision==0.18.0 torchaudio==2.3.1

Then restart ComfyUI:

python main.py

Final Summary

ComfyUI’s black images on Mac M1/M2 (MPS) are caused by FP16 precision issues.
Manually forcing FP32 precision in model_management.py fixes the issue.
Enabling MPS fallback ensures compatibility with unsupported operations.
Switching DPM++ 2 SDE scheduler to Euler helps in most cases.
🛟 Rolling back to PyTorch 2.3.1 may be necessary if PyTorch 2.6.0 continues to break MPS.

Now, ComfyUI runs perfectly on M1/M2 without black images! 🎉

If this guide helped, consider sharing it with other Mac users facing the same issue. Happy generating! 🔥

// Got Questions or Feedback?

Feel free to reach out! Happy unlock stunning visuals on your Apple Silicon device!


Created by Magnabos.co | Creative Artist

6

Comments