-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: Add MPS and CPU device support #1152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This PR fixes several issues that prevent DiffSynth-Studio from running on non-CUDA devices (Apple Silicon MPS and CPU): 1. base_pipeline.py: Check if empty_cache exists before calling it - Only CUDA has torch.cuda.empty_cache() - MPS and CPU don't have this method 2. siglip2_image_encoder.py: Remove hardcoded device="cuda" default - Now auto-detects device from model parameters - Falls back to specified device if provided 3. dinov3_image_encoder.py: Remove hardcoded device="cuda" default - Same fix as siglip2_image_encoder.py 4. vram/layers.py: Check if mem_get_info exists before calling it - Only CUDA and NPU have mem_get_info() - For MPS/CPU, assume enough memory is available These changes enable running Qwen-Image pipelines on Apple Silicon Macs and CPU-only machines without requiring any monkey-patching workarounds.
Summary of ChangesHello @longkeyy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the device compatibility of DiffSynth-Studio by introducing robust support for Apple Silicon (MPS) and CPU environments. It addresses several critical Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request effectively adds support for MPS and CPU devices by addressing several AttributeError exceptions. The changes correctly replace hardcoded "cuda" device arguments with dynamic device detection from model parameters. Additionally, the code now properly checks for the existence of device-specific functions like empty_cache() and mem_get_info() before calling them, which prevents crashes on non-CUDA devices. I've made a couple of minor suggestions to simplify these checks for better readability, but overall the changes are solid and well-implemented.
| if device_module is None or not hasattr(device_module, "mem_get_info"): | ||
| return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition to check for mem_get_info can be simplified. hasattr works correctly even if device_module is None (it returns False). This makes the device_module is None check redundant.
| if device_module is None or not hasattr(device_module, "mem_get_info"): | |
| return True | |
| if not hasattr(device_module, "mem_get_info"): | |
| return True |
| if device_module is not None and hasattr(device_module, "empty_cache"): | ||
| device_module.empty_cache() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition to check for empty_cache can be simplified. hasattr returns False if the object is None, so the device_module is not None check is redundant.
| if device_module is not None and hasattr(device_module, "empty_cache"): | |
| device_module.empty_cache() | |
| if hasattr(device_module, "empty_cache"): | |
| device_module.empty_cache() |
Summary
This PR adds support for running DiffSynth-Studio on Apple Silicon (MPS) and CPU devices, which currently fails with various AttributeError exceptions.
Changes
diffsynth/diffusion/base_pipeline.py: Check if
empty_cache()method exists before calling it (only CUDA has this)diffsynth/models/siglip2_image_encoder.py: Change
deviceparameter default from"cuda"toNone, auto-detect from model parametersdiffsynth/models/dinov3_image_encoder.py: Same fix as above
diffsynth/core/vram/layers.py: Check if
mem_get_info()method exists before calling it (only CUDA/NPU have this)Issues Fixed
AttributeError: module 'torch.cpu' has no attribute 'empty_cache'AttributeError: module 'torch.mps' has no attribute 'empty_cache'AttributeError: module 'torch.cpu' has no attribute 'mem_get_info'AttributeError: module 'torch.mps' has no attribute 'mem_get_info'AssertionError: Torch not compiled with CUDA enabled(from hardcoded cuda in encoders)Test Plan