Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions docs/source/en/api/pipelines/qwenimage21.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,83 @@ pipe.transformer.set_attn_processor(QwenImage21FlexAttnProcessor())
pipe.transformer.compile()
```

## Modular generation and inpainting

[`QwenImage21ModularPipeline`] selects text-to-image, image-conditioned generation, or inpainting from the inputs.
It reuses the QwenImage21 transformer, RGBA VAE, and Qwen3-VL processor from the standard checkpoint.

```python
import torch
from diffusers import ModularPipeline
from diffusers.utils import load_image

pipe = ModularPipeline.from_pretrained("Qwen/Qwen-Image-2.1")
pipe.load_components(dtype=torch.bfloat16)
pipe.to("cuda")

images = pipe(prompt="A capybara wearing a wizard hat", output="images")

source = load_image("source.png")
mask = load_image("mask.png").convert("L")
images = pipe(
prompt="Give the capybara a red scarf",
image=source,
mask_image=mask,
strength=1.0,
generator=torch.Generator("cpu").manual_seed(0),
output="images",
)
images[0].save("inpaint.png")
```

White mask pixels are repainted; black pixels restore the source latents after each denoising step. Preservation
is in latent space, so decoding can change unmasked pixels through VAE reconstruction. `strength` must be in
`(0, 1]` and leave at least one denoising step. Lower values start from a less noisy source image.

For multiple-reference inpainting, keep one source in `image` and pass the extra images as `reference_images`:

```python
images = pipe(
prompt="Use the scarf from image 2 and the fabric pattern from image 3 in the masked area of image 1",
image=source,
mask_image=mask,
reference_images=[load_image("scarf.png"), load_image("fabric.png")],
output="images",
)
```

The source is the first condition image, followed by references in the supplied order. All condition images feed
both Qwen3-VL and the VAE. References can have different aspect ratios; only the source has a repaint mask. The
source, mask, and references are shared across a batch of prompts. Nested per-prompt reference lists are unsupported.
Without a mask, `image` can be a single image or a flat list for image-conditioned generation.

`height` and `width` must be multiples of 32. `output_resolution` defaults to 1024 and controls the area used to
resize condition images, independently of explicit output dimensions. Inpainting derives omitted dimensions from
the source; ordinary image-conditioned generation derives them from the last condition image.

The default guider disables classifier-free guidance. Configure guidance through the component rather than a
pipeline argument:

```python
from diffusers import ClassifierFreeGuidance

pipe.update_components(guider=ClassifierFreeGuidance(guidance_scale=3.0))
images = pipe(prompt="A capybara", negative_prompt="blurry", output="images")
```

`use_kv_cache=True` caches step-independent condition tokens when the transformer has `causal_condition` enabled.
Each generation and guidance branch has its own cache. As with the standard pipeline, reduced-precision cached
and uncached outputs can differ because the attention layouts differ.

For standalone encoding or denoising, obtain a workflow with
`pipe.blocks.get_workflow("text2image")`, `"image_conditioned"`, or `"inpainting"`, and initialize a pipeline from
its individual blocks using `init_pipeline()`. Encoders return unexpanded embeddings; the denoise input block
applies `num_images_per_prompt`, so encoded features can be reused for different output counts.

## QwenImage21ModularPipeline

[[autodoc]] QwenImage21ModularPipeline

## QwenImage21Pipeline

[[autodoc]] QwenImage21Pipeline
Expand Down
4 changes: 4 additions & 0 deletions src/diffusers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@
"MiniMaxH3ModularPipeline",
"MiniMaxMusic3Blocks",
"MiniMaxMusic3ModularPipeline",
"QwenImage21AutoBlocks",
"QwenImage21ModularPipeline",
"QwenImageAutoBlocks",
"QwenImageEditAutoBlocks",
"QwenImageEditModularPipeline",
Expand Down Expand Up @@ -1418,6 +1420,8 @@
MiniMaxH3ModularPipeline,
MiniMaxMusic3Blocks,
MiniMaxMusic3ModularPipeline,
QwenImage21AutoBlocks,
QwenImage21ModularPipeline,
QwenImageAutoBlocks,
QwenImageEditAutoBlocks,
QwenImageEditModularPipeline,
Expand Down
2 changes: 2 additions & 0 deletions src/diffusers/modular_pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"Krea2TurboAutoBlocks",
"Krea2TurboModularPipeline",
]
_import_structure["qwenimage21"] = ["QwenImage21AutoBlocks", "QwenImage21ModularPipeline"]
_import_structure["qwenimage"] = [
"QwenImageAutoBlocks",
"QwenImageModularPipeline",
Expand Down Expand Up @@ -221,6 +222,7 @@
QwenImageLayeredModularPipeline,
QwenImageModularPipeline,
)
from .qwenimage21 import QwenImage21AutoBlocks, QwenImage21ModularPipeline
from .stable_diffusion_3 import StableDiffusion3AutoBlocks, StableDiffusion3ModularPipeline
from .stable_diffusion_xl import StableDiffusionXLAutoBlocks, StableDiffusionXLModularPipeline
from .wan import (
Expand Down
1 change: 1 addition & 0 deletions src/diffusers/modular_pipelines/modular_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def _helios_pyramid_map_fn(config_dict=None):
("ideogram4", _create_default_map_fn("Ideogram4ModularPipeline")),
("krea2", _krea2_map_fn),
("qwenimage", _create_default_map_fn("QwenImageModularPipeline")),
("qwenimage21", _create_default_map_fn("QwenImage21ModularPipeline")),
("qwenimage-edit", _create_default_map_fn("QwenImageEditModularPipeline")),
("qwenimage-edit-plus", _create_default_map_fn("QwenImageEditPlusModularPipeline")),
("qwenimage-layered", _create_default_map_fn("QwenImageLayeredModularPipeline")),
Expand Down
47 changes: 47 additions & 0 deletions src/diffusers/modular_pipelines/qwenimage21/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import TYPE_CHECKING

from ...utils import (
DIFFUSERS_SLOW_IMPORT,
OptionalDependencyNotAvailable,
_LazyModule,
get_objects_from_module,
is_torch_available,
is_transformers_available,
)


_dummy_objects = {}
_import_structure = {}

try:
if not (is_transformers_available() and is_torch_available()):
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
from ...utils import dummy_torch_and_transformers_objects # noqa F403

_dummy_objects.update(get_objects_from_module(dummy_torch_and_transformers_objects))
else:
_import_structure["modular_blocks_qwenimage21"] = ["QwenImage21AutoBlocks"]
_import_structure["modular_pipeline"] = ["QwenImage21ModularPipeline"]

if TYPE_CHECKING or DIFFUSERS_SLOW_IMPORT:
try:
if not (is_transformers_available() and is_torch_available()):
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
from ...utils.dummy_torch_and_transformers_objects import * # noqa F403
else:
from .modular_blocks_qwenimage21 import QwenImage21AutoBlocks
from .modular_pipeline import QwenImage21ModularPipeline
else:
import sys

sys.modules[__name__] = _LazyModule(
__name__,
globals()["__file__"],
_import_structure,
module_spec=__spec__,
)

for name, value in _dummy_objects.items():
setattr(sys.modules[__name__], name, value)
Loading
Loading