Skip to content

Conversation

@longkeyy
Copy link

Summary

This PR adds support for running DiffSynth-Studio on Apple Silicon (MPS) and CPU devices, which currently fails with various AttributeError exceptions.

Changes

  1. diffsynth/diffusion/base_pipeline.py: Check if empty_cache() method exists before calling it (only CUDA has this)

  2. diffsynth/models/siglip2_image_encoder.py: Change device parameter default from "cuda" to None, auto-detect from model parameters

  3. diffsynth/models/dinov3_image_encoder.py: Same fix as above

  4. 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

  • Tested on macOS with MPS device
  • LoRA training works correctly
  • Image generation works correctly

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.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 AttributeError and AssertionError exceptions that previously prevented the application from running on these platforms. The changes involve making device-specific utility calls conditional and allowing image encoders to infer the appropriate device dynamically, thereby broadening the accessibility and usability of the software across different hardware configurations.

Highlights

  • Enhanced Device Compatibility: Implemented conditional checks for device-specific utility methods like empty_cache() and mem_get_info(), ensuring that DiffSynth-Studio can run without AttributeError exceptions on non-CUDA devices such as Apple Silicon (MPS) and CPU.
  • Dynamic Device Assignment for Encoders: Modified image encoders (DINOv3 and SigLIP-2) to remove hardcoded 'cuda' device assignments. The forward methods now dynamically infer the appropriate device from the model's parameters, resolving AssertionError on non-CUDA systems.
  • Robust VRAM Management: Updated the VRAM management logic to gracefully handle devices that do not expose memory information functions, such as MPS and CPU, by assuming sufficient memory in these cases.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines +69 to +70
if device_module is None or not hasattr(device_module, "mem_get_info"):
return True
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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

Comment on lines +160 to +161
if device_module is not None and hasattr(device_module, "empty_cache"):
device_module.empty_cache()
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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()

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.

1 participant