-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[NPU]:Replace 'cuda' in the project with abstract interfaces #1207
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
Summary of ChangesHello @Feng0w0, 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 improves the project's hardware compatibility by abstracting device-specific code. It moves away from direct 'cuda' references to more general 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 abstracts device-specific code by replacing hardcoded 'cuda' references to better support NPUs, primarily through helper functions like get_device_type() and get_torch_device(). The introduction of a monkey patch for torch.autocast is a clever solution for handling NPU-specific behavior. I've noted a couple of areas for improvement: there's some redundant code in diffsynth/models/longcat_video_dit.py where NPU-specific logic is manually added despite the monkey patch, and a potential bug in diffsynth/models/step1x_text_encoder.py with get_torch_device().current_device() that could fail on CPU-only systems. My suggestions aim to resolve these issues. Overall, this is a solid step towards making the codebase more device-agnostic.
| self.model.config.hidden_size, | ||
| dtype=torch.bfloat16, | ||
| device=torch.cuda.current_device(), | ||
| device=get_torch_device().current_device(), |
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.
Using get_torch_device().current_device() will cause a crash on CPU-only systems. The get_torch_device function falls back to torch.cuda when the device is 'cpu', and torch.cuda.current_device() will then fail if no CUDA device is available. A simpler and more robust approach is to use get_device_type() directly, as it returns a device string ('cpu', 'cuda', 'npu') that is accepted by PyTorch tensor creation functions.
| device=get_torch_device().current_device(), | |
| device=get_device_type(), |
| self.max_length, | ||
| dtype=torch.long, | ||
| device=torch.cuda.current_device(), | ||
| device=get_torch_device().current_device(), |
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.
Using get_torch_device().current_device() will cause a crash on CPU-only systems. The get_torch_device function falls back to torch.cuda when the device is 'cpu', and torch.cuda.current_device() will then fail if no CUDA device is available. A simpler and more robust approach is to use get_device_type() directly, as it returns a device string ('cpu', 'cuda', 'npu') that is accepted by PyTorch tensor creation functions.
| device=get_torch_device().current_device(), | |
| device=get_device_type(), |
| (min(self.max_length, emb.shape[1] - 217)), | ||
| dtype=torch.long, | ||
| device=torch.cuda.current_device(), | ||
| device=get_torch_device().current_device(), |
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.
Using get_torch_device().current_device() will cause a crash on CPU-only systems. The get_torch_device function falls back to torch.cuda when the device is 'cpu', and torch.cuda.current_device() will then fail if no CUDA device is available. A simpler and more robust approach is to use get_device_type() directly, as it returns a device string ('cpu', 'cuda', 'npu') that is accepted by PyTorch tensor creation functions.
| device=get_torch_device().current_device(), | |
| device=get_device_type(), |
| if IS_NPU_AVAILABLE: | ||
| torch.npu.set_autocast_enabled(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.
This conditional block is redundant. The monkey patch for torch.amp.autocast in diffsynth/core/npu_patch/npu_autocast_patch.py already handles this logic when get_device_type() returns 'npu'. Removing this will improve code clarity and avoid duplication. This comment applies to similar changes in this file (lines 590-591, 611-612, 626-627, and 810-811).
Replace the
cudafield in the project with the abstract interface:1.'cuda' -> get_device_type()
2.torch.cuda -> get_torch_device()