Skip to content

Conversation

Dong1017
Copy link

@Dong1017 Dong1017 commented Sep 17, 2025

What does this PR do?

Adds

1. QwenImage Pipelines and Required Modules

(Comparable with Diffusers Master)

a. Pipelines

  • mindone.diffusers.QwenImagePipeline
  • mindone.diffusers.QwenImageImg2ImgPipeline
  • mindone.diffusers.QwenImageInpaintPipeline
  • mindone.diffusers.QwenImageEditPipeline
  • mindone.diffusers.QwenImageEditInpaintPipeline

b. Modules

  • mindone.diffusers.models.AutoencoderQwenImage
  • mindone.diffusers.models.QwenImageTransformer2DModel
  • mindone.diffusers.loaders.QwenImageLoraLoaderMixin

2. add UTs of pipelines — all passed

(targeting Diffusers Master)

  • tests/diffusers_tests/pipelines/qwenimage/test_qwenimage.py
  • tests/diffusers_tests/pipelines/qwenimage/test_qwenimage_img2img.py
  • tests/diffusers_tests/pipelines/qwenimage/test_qwenimage_inpaint.py
  • tests/diffusers_tests/pipelines/qwenimage/test_qwenimage_edit.py

Usage

  • QwenImagePipeline
import mindspore as ms 
from mindone.diffusers import QwenImagePipeline 

pipe = QwenImagePipeline.from_pretrained("Qwen/Qwen-Image", mindspore_dtype=ms.bfloat16) 
prompt = "A cat holding a sign that says hello world" 
# Depending on the variant being used, the pipeline call will slightly vary. 
# Refer to the pipeline documentation for more details. 
image = pipe(prompt, num_inference_steps=50)[0][0] 
image.save("qwenimage.png") 
  • QwenImageImg2ImgPipeline
import mindspore as ms 
from mindone.diffusers import QwenImageImg2ImgPipeline
from mindone.diffusers.utils import load_image

pipe = QwenImageImg2ImgPipeline.from_pretrained("Qwen/Qwen-Image", mindspore_dtype=mindspore.bfloat16)
url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
init_image = load_image(url).resize((1024, 1024))
prompt = "cat wizard, gandalf, lord of the rings, detailed, fantasy, cute, adorable, Pixar, Disney"
images = pipe(prompt=prompt, negative_prompt=" ", image=init_image, strength=0.95)[0][0]
images.save("qwenimage_img2img.png")
  • QwenImageInpaintPipeline
import mindspore as ms 
from mindone.diffusers import QwenImageInpaintPipeline 
from mindone.diffusers.utils import load_image 

pipe = QwenImageInpaintPipeline.from_pretrained("Qwen/Qwen-Image", mindspore_dtype=ms.bfloat16) 
prompt = "Face of a yellow cat, high resolution, sitting on a park bench" 
img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" 
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png" 
source = load_image(img_url) 
mask = load_image(mask_url) 
image = pipe(prompt=prompt, negative_prompt=" ", image=source, mask_image=mask, strength=0.85)[0][0] 
image.save("qwenimage_inpainting.png") 
  • QwenImageEditPipeline
import mindspore as ms 
from PIL import Image 
from mindone.diffusers import QwenImageEditPipeline 
from mindone.diffusers.utils import load_image 

pipe = QwenImageEditPipeline.from_pretrained("Qwen/Qwen-Image-Edit", mindspore_dtype=ms.bfloat16) 
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/yarn-art-pikachu.png").convert("RGB") 
prompt = ("Make Pikachu hold a sign that says 'Qwen Edit is awesome', yarn art style, detailed, vibrant colors") 
# Depending on the variant being used, the pipeline call will slightly vary. 
# Refer to the pipeline documentation for more details. 
image = pipe(image, prompt, num_inference_steps=50)[0][0] 
image.save("qwenimage_edit.png") 
  • QwenImageEditInpaintPipeline
import mindspore as ms 
from PIL import Image
from mindone.diffusers import QwenImageEditInpaintPipeline
from mindone.diffusers.utils import load_image

pipe = QwenImageEditInpaintPipeline.from_pretrained("Qwen/Qwen-Image-Edit", mindspore_dtype=mindspore.bfloat16)
prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
source = load_image(img_url)
mask = load_image(mask_url)
image = pipe(prompt=prompt, negative_prompt=" ", image=source, mask_image=mask, strength=1.0, num_inference_steps=50)[0][0]
image.save("qwenimage_inpainting.png")

Performance

(Infer experiments are tested on Ascend Atlas 800T A2 machines with MindSpore 2.7.0, setting to pynative mode)

Pipeline Weight Loading Time Mode Speed
QwenImagePipeline 15m21s Pynative 9.93 s/it
QwenImageImg2ImgPipeline 14m57s Pynative 9.56 s/it
QwenImageInpaintPipeline 10m10s Pynative 4.80 s/it
QwenImageEditPipeline 13m57s Pynative 13.25 s/it
QwenImageEditInpaintPipeline 13m20s Pynative 13.98 s/it

Limitation

QwenImageEditPipeline and QwenImageEditInpaintPipeline will load modules from Qwen-Image-Edit. The use of these two pipes requires manually changing image_processor_type from Qwen2VLImageProcessorFast to Qwen2VLImageProcessor in Qwen-Image-Edit/processor/preprocessor_config.json

Notes

  1. require transformers==4.52.1
  2. The produced pictures are nearly identical to those by Torch, when setting consistent random seed and hidden states from the text encoder.
  3. TODO: jit mode; LORA test; UTs of modules

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline?
  • Did you make sure to update the documentation with your changes? E.g. record bug fixes or new features in What's New. Here are the
    documentation guidelines
  • Did you build and run the code without any errors?
  • Did you report the running environment (NPU type/MS version) and performance in the doc? (better record it for data loading, model inference, or training tasks)
  • Did you write any new necessary tests?

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

@Dong1017 Dong1017 requested a review from vigo999 as a code owner September 17, 2025 06:53
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.

Summary of Changes

Hello @Dong1017, 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 QwenImage model family into the Diffusers library, providing a robust set of pipelines for various image generation and editing tasks. It introduces specialized model architectures and utilities tailored for QwenImage's unique features, enhancing the library's capabilities for advanced diffusion models.

Highlights

  • New QwenImage Integration: Comprehensive support for QwenImage models has been added, including its unique VAE and Transformer architectures, enabling advanced image generation capabilities.
  • Diverse Pipeline Support: Five distinct pipelines for QwenImage have been introduced: text-to-image, image-to-image, inpainting, editing, and combined edit-inpainting, offering a wide range of functionalities.
  • Custom Model Components: Specialized components like AutoencoderKLQwenImage with causal 3D convolutions and QwenImageTransformer2DModel featuring dual-stream attention and rotary embeddings have been implemented.
  • LoRA Loading Capability: The QwenImageLoraLoaderMixin and associated conversion utilities were added to enable seamless loading of LoRA checkpoints for QwenImage models.
  • MindSpore Compatibility Enhancements: Core utilities related to attention processing, weight normalization, and model loading have been updated to ensure full compatibility and optimized performance within the MindSpore framework.
  • Comprehensive Unit Testing: New unit tests have been included for all newly added QwenImage pipelines, ensuring their correctness and stability.
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 introduces support for QwenImage pipelines and their associated modules into the Diffusers library. The changes are extensive, including new pipelines for text-to-image, image-to-image, inpainting, and editing, as well as the necessary model components like AutoencoderKLQwenImage and QwenImageTransformer2DModel. My review has identified several critical syntax errors in the newly added test files that will prevent them from running. Additionally, there are some documentation errors, including incorrect model identifiers and type hints, and a notable performance issue in the blending logic within the VAE model that could be significantly improved with vectorization. Addressing these points will enhance the correctness, usability, and performance of this new feature.

@Dong1017 Dong1017 changed the title Add pipelines and required modules of QwenImage in Diffusers Master feat(diffusers/pipelines): add pipelines and required modules of QwenImage in Diffusers Master Sep 17, 2025
adapter_name=adapter_name,
metadata=metadata,
_pipeline=_pipeline,
low_cpu_mem_usage=low_cpu_mem_usage,
Copy link
Contributor

Choose a reason for hiding this comment

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

可以先删掉 我们暂时还不支持

)
encoder_hidden_states = self.text_encoder(
input_ids=ms.Tensor(txt_tokens.input_ids),
attention_mask=ms.Tensor(txt_tokens.attention_mask),
Copy link
Contributor

Choose a reason for hiding this comment

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

tensor使用优先ms.tensor 后面应该还有一些类似Tensor的问题

Copy link
Author

Choose a reason for hiding this comment

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

其他pipeline也将进行对应修改

pt_pipe = pt_pipe.to(pt_dtype)
ms_pipe = ms_pipe.to(ms_dtype)

sys.modules[ms_pipe.__module__].randn_tensor = randn_tensor
Copy link
Contributor

Choose a reason for hiding this comment

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

为什么这里要单独替换呀?

Copy link
Author

@Dong1017 Dong1017 Sep 25, 2025

Choose a reason for hiding this comment

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

img2img pipe中单独替换以保证随机性一致;text2img, inpaint, edit pipes中无需单独设置,将删除冗余行

@SamitHuang
Copy link
Collaborator

How to fix the requirement of transformers==4.52.1?

)
latents = latents / latents_std + latents_mean
# TODO: we use pynative mode here since cache in vae.decode which not supported in graph mode
with pynative_context():
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it necessary? since the default MS mode and the supported mode is pynative?

Copy link
Author

@Dong1017 Dong1017 Sep 25, 2025

Choose a reason for hiding this comment

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

Thanks for the review. with pynative_context() will be removed, since the double-check shows that its impact on results is minimal. Tests for all pipelines are doing.

[CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer).
"""

model_cpu_offload_seq = "text_encoder->transformer->vae"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the offloading seq supported?

@SamitHuang
Copy link
Collaborator

Can add an inference example and lora fine-tune example in examples folder, which helps introduce QwenImage

@SamitHuang SamitHuang mentioned this pull request Sep 22, 2025
6 tasks
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.

3 participants