-
Couldn't load subscription status.
- Fork 88
feat(diffusers): add prx to diffusers #1389
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: master
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @Cui-yshoho, 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 integrates the advanced PRX text-to-image generation model into the diffusers library. The changes encompass the full model architecture, a specialized inference pipeline, and foundational utilities for efficient attention processing in MindSpore, ensuring robust functionality and ease of use for high-quality image synthesis. Highlights
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 introduces the PRX model, a new text-to-image transformer, into the diffusers library. The changes are comprehensive, including the PRXTransformer2DModel, the PRXPipeline, along with corresponding documentation and tests. I've identified a critical issue in the Rotary Positional Embedding (RoPE) application logic within the transformer model that needs to be addressed. Additionally, there are a few minor issues in the documentation and a bug in the new test case that I've flagged for correction. Overall, this is a great contribution.
| def apply_rope(xq: ms.Tensor, freqs_cis: ms.Tensor) -> ms.Tensor: | ||
| r""" | ||
| Applies rotary positional embeddings (RoPE) to a query tensor. | ||
|
|
||
| Args: | ||
| xq (`ms.Tensor`): | ||
| Input tensor of shape `(..., dim)` representing the queries. | ||
| freqs_cis (`ms.Tensor`): | ||
| Precomputed rotary frequency components of shape `(..., dim/2, 2)` containing cosine and sine pairs. | ||
|
|
||
| Returns: | ||
| `ms.Tensor`: | ||
| Tensor of the same shape as `xq` with rotary embeddings applied. | ||
| """ | ||
| xq_ = xq.float().reshape(*xq.shape[:-1], -1, 1, 2) | ||
| # Ensure freqs_cis is on the same device as queries to avoid device mismatches with offloading | ||
| freqs_cis = freqs_cis.to(dtype=xq_.dtype) | ||
| xq_out = freqs_cis[..., 0] * xq_[..., 0] + freqs_cis[..., 1] * xq_[..., 1] | ||
| return xq_out.reshape(*xq.shape).type_as(xq) |
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 implementation of apply_rope has a bug that will cause a runtime error due to a shape mismatch during multiplication. The rotary embeddings should be applied using matrix multiplication. The current implementation attempts an element-wise operation that is not broadcast-compatible.
I've provided a corrected version that performs the intended batched matrix-vector product. I've also updated the docstring to reflect the correct shape of freqs_cis.
| def apply_rope(xq: ms.Tensor, freqs_cis: ms.Tensor) -> ms.Tensor: | |
| r""" | |
| Applies rotary positional embeddings (RoPE) to a query tensor. | |
| Args: | |
| xq (`ms.Tensor`): | |
| Input tensor of shape `(..., dim)` representing the queries. | |
| freqs_cis (`ms.Tensor`): | |
| Precomputed rotary frequency components of shape `(..., dim/2, 2)` containing cosine and sine pairs. | |
| Returns: | |
| `ms.Tensor`: | |
| Tensor of the same shape as `xq` with rotary embeddings applied. | |
| """ | |
| xq_ = xq.float().reshape(*xq.shape[:-1], -1, 1, 2) | |
| # Ensure freqs_cis is on the same device as queries to avoid device mismatches with offloading | |
| freqs_cis = freqs_cis.to(dtype=xq_.dtype) | |
| xq_out = freqs_cis[..., 0] * xq_[..., 0] + freqs_cis[..., 1] * xq_[..., 1] | |
| return xq_out.reshape(*xq.shape).type_as(xq) | |
| def apply_rope(xq: ms.Tensor, freqs_cis: ms.Tensor) -> ms.Tensor: | |
| r""" | |
| Applies rotary positional embeddings (RoPE) to a query tensor. | |
| Args: | |
| xq (`ms.Tensor`): | |
| Input tensor of shape `(..., dim)` representing the queries. | |
| freqs_cis (`ms.Tensor`): | |
| Precomputed rotary frequency components of shape `(..., dim/2, 2, 2)` containing rotation matrices. | |
| Returns: | |
| `ms.Tensor`: | |
| Tensor of the same shape as `xq` with rotary embeddings applied. | |
| """ | |
| xq_ = xq.float().reshape(*xq.shape[:-1], -1, 2) | |
| # Ensure freqs_cis is on the same device as queries to avoid device mismatches with offloading | |
| freqs_cis = freqs_cis.to(dtype=xq_.dtype) | |
| xq_ = xq_.unsqueeze(-2) | |
| xq_out = mint.matmul(xq_, freqs_cis).squeeze(-2) | |
| return xq_out.reshape(*xq.shape).type_as(xq) |
| torch.manual_seed(0) | ||
| pt_image = pt_pipe(**inputs).images[0, -3:, -3:, -1] | ||
| torch.manual_seed(0) | ||
| ms_image = ms_pipe(**inputs)[0][0, -3:, -3:, -1] |
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.
When return_dict=True (the default), the pipeline returns a PRXPipelineOutput object. Accessing the output with [0] will cause a TypeError. You should access the generated images using the .images attribute to be consistent with the PyTorch implementation and to avoid the error.
| ms_image = ms_pipe(**inputs)[0][0, -3:, -3:, -1] | |
| ms_image = ms_pipe(**inputs).images[0, -3:, -3:, -1] |
| | [`Photoroom/prx-512-t2i-sft-distilled`](https://huggingface.co/Photoroom/prx-512-t2i-sft-distilled)| 512 | Yes | Yes | 8-step distilled model from [`Photoroom/prx-512-t2i-sft`](https://huggingface.co/Photoroom/prx-512-t2i-sft) | Can handle less detailed prompts in natural language|8 steps, cfg=1.0| `mindspore.bfloat16` | | ||
| | [`Photoroom/prx-512-t2i-dc-ae`](https://huggingface.co/Photoroom/prx-512-t2i-dc-ae)| 512 | No | No | Base model pre-trained at 512 with [Deep Compression Autoencoder (DC-AE)](https://hanlab.mit.edu/projects/dc-ae)|Works best with detailed prompts in natural language|28 steps, cfg=5.0| `mindspore.bfloat16` | | ||
| | [`Photoroom/prx-512-t2i-dc-ae-sft`](https://huggingface.co/Photoroom/prx-512-t2i-dc-ae-sft)| 512 | Yes | No | Fine-tuned on the [Alchemist dataset](https://huggingface.co/datasets/yandex/alchemist) dataset with [Deep Compression Autoencoder (DC-AE)](https://hanlab.mit.edu/projects/dc-ae) | Can handle less detailed prompts in natural language|28 steps, cfg=5.0| `mindspore.bfloat16` | | ||
| | [`Photoroom/prx-512-t2i-dc-ae-sft-distilled`](https://huggingface.co/Photoroom/prx-512-t2i-dc-ae-sft-distilled)| 512 | Yes | Yes | 8-step distilled model from [`Photoroom/prx-512-t2i-dc-ae-sft-distilled`](https://huggingface.co/Photoroom/prx-512-t2i-dc-ae-sft-distilled) | Can handle less detailed prompts in natural language|8 steps, cfg=1.0| `mindspore.bfloat16` |s |
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.
There appears to be a typo (s) at the end of this line. Additionally, the description for Photoroom/prx-512-t2i-dc-ae-sft-distilled incorrectly states that it is distilled from itself. It should likely be distilled from Photoroom/prx-512-t2i-dc-ae-sft.
| | [`Photoroom/prx-512-t2i-dc-ae-sft-distilled`](https://huggingface.co/Photoroom/prx-512-t2i-dc-ae-sft-distilled)| 512 | Yes | Yes | 8-step distilled model from [`Photoroom/prx-512-t2i-dc-ae-sft-distilled`](https://huggingface.co/Photoroom/prx-512-t2i-dc-ae-sft-distilled) | Can handle less detailed prompts in natural language|8 steps, cfg=1.0| `mindspore.bfloat16` |s | |
| | [`Photoroom/prx-512-t2i-dc-ae-sft-distilled`](https://huggingface.co/Photoroom/prx-512-t2i-dc-ae-sft-distilled)| 512 | Yes | Yes | 8-step distilled model from [`Photoroom/prx-512-t2i-dc-ae-sft`](https://huggingface.co/Photoroom/prx-512-t2i-dc-ae-sft) | Can handle less detailed prompts in natural language|8 steps, cfg=1.0| `mindspore.bfloat16` | |
| # Load pipeline - VAE and text encoder will be loaded from HuggingFace | ||
| pipe = PRXPipeline.from_pretrained("Photoroom/prx-512-t2i-sft", mindspore_dtype=ms.bfloat16) | ||
|
|
||
| prompt = "A front-facing portrait of a lion the golden savanna at sunset." |
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 example prompt appears to be missing a preposition. It should probably be "...a lion in the golden savanna..." for better grammatical correctness.
| prompt = "A front-facing portrait of a lion the golden savanna at sunset." | |
| prompt = "A front-facing portrait of a lion in the golden savanna at sunset." |
What does this PR do?
Fixes # (issue)
Adds # (feature)
Before submitting
What's New. Here are thedocumentation guidelines
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@xxx