Professional Noob commited on
Commit
1cfb46c
·
verified ·
1 Parent(s): 0b6c578

Update qwenimage/pipeline_qwenimage_edit_plus.py

Browse files
qwenimage/pipeline_qwenimage_edit_plus.py CHANGED
@@ -1,891 +1,932 @@
1
- # Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- import inspect
16
- import math
17
- from typing import Any, Callable, Dict, List, Optional, Union
18
-
19
- import numpy as np
20
- import torch
21
- from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer, Qwen2VLProcessor
22
-
23
- from diffusers.image_processor import PipelineImageInput, VaeImageProcessor
24
- from diffusers.loaders import QwenImageLoraLoaderMixin
25
- from diffusers.models import AutoencoderKLQwenImage, QwenImageTransformer2DModel
26
- from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
27
- from diffusers.utils import is_torch_xla_available, logging, replace_example_docstring
28
- from diffusers.utils.torch_utils import randn_tensor
29
- from diffusers.pipelines.pipeline_utils import DiffusionPipeline
30
- from diffusers.pipelines.qwenimage.pipeline_output import QwenImagePipelineOutput
31
-
32
-
33
- if is_torch_xla_available():
34
- import torch_xla.core.xla_model as xm
35
-
36
- XLA_AVAILABLE = True
37
- else:
38
- XLA_AVAILABLE = False
39
-
40
-
41
- logger = logging.get_logger(__name__) # pylint: disable=invalid-name
42
-
43
- EXAMPLE_DOC_STRING = """
44
- Examples:
45
- ```py
46
- >>> import torch
47
- >>> from PIL import Image
48
- >>> from diffusers import QwenImageEditPlusPipeline
49
- >>> from diffusers.utils import load_image
50
-
51
- >>> pipe = QwenImageEditPlusPipeline.from_pretrained("Qwen/Qwen-Image-Edit-2509", torch_dtype=torch.bfloat16)
52
- >>> pipe.to("cuda")
53
- >>> image = load_image(
54
- ... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/yarn-art-pikachu.png"
55
- ... ).convert("RGB")
56
- >>> prompt = (
57
- ... "Make Pikachu hold a sign that says 'Qwen Edit is awesome', yarn art style, detailed, vibrant colors"
58
- ... )
59
- >>> # Depending on the variant being used, the pipeline call will slightly vary.
60
- >>> # Refer to the pipeline documentation for more details.
61
- >>> image = pipe(image, prompt, num_inference_steps=50).images[0]
62
- >>> image.save("qwenimage_edit_plus.png")
63
- ```
64
- """
65
-
66
- CONDITION_IMAGE_SIZE = 384 * 384
67
- VAE_IMAGE_SIZE = 1024 * 1024
68
-
69
-
70
- # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.calculate_shift
71
- def calculate_shift(
72
- image_seq_len,
73
- base_seq_len: int = 256,
74
- max_seq_len: int = 4096,
75
- base_shift: float = 0.5,
76
- max_shift: float = 1.15,
77
- ):
78
- m = (max_shift - base_shift) / (max_seq_len - base_seq_len)
79
- b = base_shift - m * base_seq_len
80
- mu = image_seq_len * m + b
81
- return mu
82
-
83
-
84
- # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps
85
- def retrieve_timesteps(
86
- scheduler,
87
- num_inference_steps: Optional[int] = None,
88
- device: Optional[Union[str, torch.device]] = None,
89
- timesteps: Optional[List[int]] = None,
90
- sigmas: Optional[List[float]] = None,
91
- **kwargs,
92
- ):
93
- r"""
94
- Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
95
- custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.
96
-
97
- Args:
98
- scheduler (`SchedulerMixin`):
99
- The scheduler to get timesteps from.
100
- num_inference_steps (`int`):
101
- The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
102
- must be `None`.
103
- device (`str` or `torch.device`, *optional*):
104
- The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
105
- timesteps (`List[int]`, *optional*):
106
- Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
107
- `num_inference_steps` and `sigmas` must be `None`.
108
- sigmas (`List[float]`, *optional*):
109
- Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
110
- `num_inference_steps` and `timesteps` must be `None`.
111
-
112
- Returns:
113
- `Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
114
- second element is the number of inference steps.
115
- """
116
- if timesteps is not None and sigmas is not None:
117
- raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values")
118
- if timesteps is not None:
119
- accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
120
- if not accepts_timesteps:
121
- raise ValueError(
122
- f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
123
- f" timestep schedules. Please check whether you are using the correct scheduler."
124
- )
125
- scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
126
- timesteps = scheduler.timesteps
127
- num_inference_steps = len(timesteps)
128
- elif sigmas is not None:
129
- accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
130
- if not accept_sigmas:
131
- raise ValueError(
132
- f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
133
- f" sigmas schedules. Please check whether you are using the correct scheduler."
134
- )
135
- scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
136
- timesteps = scheduler.timesteps
137
- num_inference_steps = len(timesteps)
138
- else:
139
- scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
140
- timesteps = scheduler.timesteps
141
- return timesteps, num_inference_steps
142
-
143
-
144
- # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents
145
- def retrieve_latents(
146
- encoder_output: torch.Tensor, generator: Optional[torch.Generator] = None, sample_mode: str = "sample"
147
- ):
148
- if hasattr(encoder_output, "latent_dist") and sample_mode == "sample":
149
- return encoder_output.latent_dist.sample(generator)
150
- elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax":
151
- return encoder_output.latent_dist.mode()
152
- elif hasattr(encoder_output, "latents"):
153
- return encoder_output.latents
154
- else:
155
- raise AttributeError("Could not access latents of provided encoder_output")
156
-
157
-
158
- def calculate_dimensions(target_area, ratio):
159
- width = math.sqrt(target_area * ratio)
160
- height = width / ratio
161
-
162
- width = round(width / 32) * 32
163
- height = round(height / 32) * 32
164
-
165
- return width, height
166
-
167
-
168
- class QwenImageEditPlusPipeline(DiffusionPipeline, QwenImageLoraLoaderMixin):
169
- r"""
170
- The Qwen-Image-Edit pipeline for image editing.
171
-
172
- Args:
173
- transformer ([`QwenImageTransformer2DModel`]):
174
- Conditional Transformer (MMDiT) architecture to denoise the encoded image latents.
175
- scheduler ([`FlowMatchEulerDiscreteScheduler`]):
176
- A scheduler to be used in combination with `transformer` to denoise the encoded image latents.
177
- vae ([`AutoencoderKL`]):
178
- Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
179
- text_encoder ([`Qwen2.5-VL-7B-Instruct`]):
180
- [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the
181
- [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant.
182
- tokenizer (`QwenTokenizer`):
183
- Tokenizer of class
184
- [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer).
185
- """
186
-
187
- model_cpu_offload_seq = "text_encoder->transformer->vae"
188
- _callback_tensor_inputs = ["latents", "prompt_embeds"]
189
-
190
- def __init__(
191
- self,
192
- scheduler: FlowMatchEulerDiscreteScheduler,
193
- vae: AutoencoderKLQwenImage,
194
- text_encoder: Qwen2_5_VLForConditionalGeneration,
195
- tokenizer: Qwen2Tokenizer,
196
- processor: Qwen2VLProcessor,
197
- transformer: QwenImageTransformer2DModel,
198
- ):
199
- super().__init__()
200
-
201
- self.register_modules(
202
- vae=vae,
203
- text_encoder=text_encoder,
204
- tokenizer=tokenizer,
205
- processor=processor,
206
- transformer=transformer,
207
- scheduler=scheduler,
208
- )
209
- self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8
210
- self.latent_channels = self.vae.config.z_dim if getattr(self, "vae", None) else 16
211
- # QwenImage latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible
212
- # by the patch size. So the vae scale factor is multiplied by the patch size to account for this
213
- self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor * 2)
214
- self.tokenizer_max_length = 1024
215
-
216
- self.prompt_template_encode = "<|im_start|>system\nDescribe the key features of the input image (color, shape, size, texture, objects, background), then explain how the user's text instruction should alter or modify the image. Generate a new image that meets the user's requirements while maintaining consistency with the original input where appropriate.<|im_end|>\n<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n"
217
- self.prompt_template_encode_start_idx = 64
218
- self.default_sample_size = 128
219
-
220
- # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._extract_masked_hidden
221
- def _extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor):
222
- bool_mask = mask.bool()
223
- valid_lengths = bool_mask.sum(dim=1)
224
- selected = hidden_states[bool_mask]
225
- split_result = torch.split(selected, valid_lengths.tolist(), dim=0)
226
-
227
- return split_result
228
-
229
- def _get_qwen_prompt_embeds(
230
- self,
231
- prompt: Union[str, List[str]] = None,
232
- image: Optional[torch.Tensor] = None,
233
- device: Optional[torch.device] = None,
234
- dtype: Optional[torch.dtype] = None,
235
- ):
236
- device = device or self._execution_device
237
- dtype = dtype or self.text_encoder.dtype
238
-
239
- prompt = [prompt] if isinstance(prompt, str) else prompt
240
- img_prompt_template = "Picture {}: <|vision_start|><|image_pad|><|vision_end|>"
241
- if isinstance(image, list):
242
- base_img_prompt = ""
243
- for i, img in enumerate(image):
244
- base_img_prompt += img_prompt_template.format(i + 1)
245
- elif image is not None:
246
- base_img_prompt = img_prompt_template.format(1)
247
- else:
248
- base_img_prompt = ""
249
-
250
- template = self.prompt_template_encode
251
-
252
- drop_idx = self.prompt_template_encode_start_idx
253
- txt = [template.format(base_img_prompt + e) for e in prompt]
254
-
255
- model_inputs = self.processor(
256
- text=txt,
257
- images=image,
258
- padding=True,
259
- return_tensors="pt",
260
- ).to(device)
261
-
262
- outputs = self.text_encoder(
263
- input_ids=model_inputs.input_ids,
264
- attention_mask=model_inputs.attention_mask,
265
- pixel_values=model_inputs.pixel_values,
266
- image_grid_thw=model_inputs.image_grid_thw,
267
- output_hidden_states=True,
268
- )
269
-
270
- hidden_states = outputs.hidden_states[-1]
271
- split_hidden_states = self._extract_masked_hidden(hidden_states, model_inputs.attention_mask)
272
- split_hidden_states = [e[drop_idx:] for e in split_hidden_states]
273
- attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states]
274
- max_seq_len = max([e.size(0) for e in split_hidden_states])
275
- prompt_embeds = torch.stack(
276
- [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states]
277
- )
278
- encoder_attention_mask = torch.stack(
279
- [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list]
280
- )
281
-
282
- prompt_embeds = prompt_embeds.to(dtype=dtype, device=device)
283
-
284
- return prompt_embeds, encoder_attention_mask
285
-
286
- # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.QwenImageEditPipeline.encode_prompt
287
- def encode_prompt(
288
- self,
289
- prompt: Union[str, List[str]],
290
- image: Optional[torch.Tensor] = None,
291
- device: Optional[torch.device] = None,
292
- num_images_per_prompt: int = 1,
293
- prompt_embeds: Optional[torch.Tensor] = None,
294
- prompt_embeds_mask: Optional[torch.Tensor] = None,
295
- max_sequence_length: int = 1024,
296
- ):
297
- r"""
298
-
299
- Args:
300
- prompt (`str` or `List[str]`, *optional*):
301
- prompt to be encoded
302
- image (`torch.Tensor`, *optional*):
303
- image to be encoded
304
- device: (`torch.device`):
305
- torch device
306
- num_images_per_prompt (`int`):
307
- number of images that should be generated per prompt
308
- prompt_embeds (`torch.Tensor`, *optional*):
309
- Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
310
- provided, text embeddings will be generated from `prompt` input argument.
311
- """
312
- device = device or self._execution_device
313
-
314
- prompt = [prompt] if isinstance(prompt, str) else prompt
315
- batch_size = len(prompt) if prompt_embeds is None else prompt_embeds.shape[0]
316
-
317
- if prompt_embeds is None:
318
- prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds(prompt, image, device)
319
-
320
- _, seq_len, _ = prompt_embeds.shape
321
- prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1)
322
- prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1)
323
- prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1)
324
- prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len)
325
-
326
- return prompt_embeds, prompt_embeds_mask
327
-
328
- # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.QwenImageEditPipeline.check_inputs
329
- def check_inputs(
330
- self,
331
- prompt,
332
- height,
333
- width,
334
- negative_prompt=None,
335
- prompt_embeds=None,
336
- negative_prompt_embeds=None,
337
- prompt_embeds_mask=None,
338
- negative_prompt_embeds_mask=None,
339
- callback_on_step_end_tensor_inputs=None,
340
- max_sequence_length=None,
341
- ):
342
- if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0:
343
- logger.warning(
344
- f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly"
345
- )
346
-
347
- if callback_on_step_end_tensor_inputs is not None and not all(
348
- k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs
349
- ):
350
- raise ValueError(
351
- f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}"
352
- )
353
-
354
- if prompt is not None and prompt_embeds is not None:
355
- raise ValueError(
356
- f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
357
- " only forward one of the two."
358
- )
359
- elif prompt is None and prompt_embeds is None:
360
- raise ValueError(
361
- "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined."
362
- )
363
- elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)):
364
- raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
365
-
366
- if negative_prompt is not None and negative_prompt_embeds is not None:
367
- raise ValueError(
368
- f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:"
369
- f" {negative_prompt_embeds}. Please make sure to only forward one of the two."
370
- )
371
-
372
- if prompt_embeds is not None and prompt_embeds_mask is None:
373
- raise ValueError(
374
- "If `prompt_embeds` are provided, `prompt_embeds_mask` also have to be passed. Make sure to generate `prompt_embeds_mask` from the same text encoder that was used to generate `prompt_embeds`."
375
- )
376
- if negative_prompt_embeds is not None and negative_prompt_embeds_mask is None:
377
- raise ValueError(
378
- "If `negative_prompt_embeds` are provided, `negative_prompt_embeds_mask` also have to be passed. Make sure to generate `negative_prompt_embeds_mask` from the same text encoder that was used to generate `negative_prompt_embeds`."
379
- )
380
-
381
- if max_sequence_length is not None and max_sequence_length > 1024:
382
- raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}")
383
-
384
- @staticmethod
385
- # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._pack_latents
386
- def _pack_latents(latents, batch_size, num_channels_latents, height, width):
387
- latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2)
388
- latents = latents.permute(0, 2, 4, 1, 3, 5)
389
- latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4)
390
-
391
- return latents
392
-
393
- @staticmethod
394
- # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._unpack_latents
395
- def _unpack_latents(latents, height, width, vae_scale_factor):
396
- batch_size, num_patches, channels = latents.shape
397
-
398
- # VAE applies 8x compression on images but we must also account for packing which requires
399
- # latent height and width to be divisible by 2.
400
- height = 2 * (int(height) // (vae_scale_factor * 2))
401
- width = 2 * (int(width) // (vae_scale_factor * 2))
402
-
403
- latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2)
404
- latents = latents.permute(0, 3, 1, 4, 2, 5)
405
-
406
- latents = latents.reshape(batch_size, channels // (2 * 2), 1, height, width)
407
-
408
- return latents
409
-
410
- # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.QwenImageEditPipeline._encode_vae_image
411
- def _encode_vae_image(self, image: torch.Tensor, generator: torch.Generator):
412
- if isinstance(generator, list):
413
- image_latents = [
414
- retrieve_latents(self.vae.encode(image[i : i + 1]), generator=generator[i], sample_mode="argmax")
415
- for i in range(image.shape[0])
416
- ]
417
- image_latents = torch.cat(image_latents, dim=0)
418
- else:
419
- image_latents = retrieve_latents(self.vae.encode(image), generator=generator, sample_mode="argmax")
420
- latents_mean = (
421
- torch.tensor(self.vae.config.latents_mean)
422
- .view(1, self.latent_channels, 1, 1, 1)
423
- .to(image_latents.device, image_latents.dtype)
424
- )
425
- latents_std = (
426
- torch.tensor(self.vae.config.latents_std)
427
- .view(1, self.latent_channels, 1, 1, 1)
428
- .to(image_latents.device, image_latents.dtype)
429
- )
430
- image_latents = (image_latents - latents_mean) / latents_std
431
-
432
- return image_latents
433
-
434
- def prepare_latents(
435
- self,
436
- images,
437
- batch_size,
438
- num_channels_latents,
439
- height,
440
- width,
441
- dtype,
442
- device,
443
- generator,
444
- latents=None,
445
- ):
446
- # VAE applies 8x compression on images but we must also account for packing which requires
447
- # latent height and width to be divisible by 2.
448
- height = 2 * (int(height) // (self.vae_scale_factor * 2))
449
- width = 2 * (int(width) // (self.vae_scale_factor * 2))
450
-
451
- shape = (batch_size, 1, num_channels_latents, height, width)
452
-
453
- image_latents = None
454
- if images is not None:
455
- if not isinstance(images, list):
456
- images = [images]
457
- all_image_latents = []
458
- for image in images:
459
- image = image.to(device=device, dtype=dtype)
460
- if image.shape[1] != self.latent_channels:
461
- image_latents = self._encode_vae_image(image=image, generator=generator)
462
- else:
463
- image_latents = image
464
- if batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] == 0:
465
- # expand init_latents for batch_size
466
- additional_image_per_prompt = batch_size // image_latents.shape[0]
467
- image_latents = torch.cat([image_latents] * additional_image_per_prompt, dim=0)
468
- elif batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] != 0:
469
- raise ValueError(
470
- f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts."
471
- )
472
- else:
473
- image_latents = torch.cat([image_latents], dim=0)
474
-
475
- image_latent_height, image_latent_width = image_latents.shape[3:]
476
- image_latents = self._pack_latents(
477
- image_latents, batch_size, num_channels_latents, image_latent_height, image_latent_width
478
- )
479
- all_image_latents.append(image_latents)
480
- image_latents = torch.cat(all_image_latents, dim=1)
481
-
482
- if isinstance(generator, list) and len(generator) != batch_size:
483
- raise ValueError(
484
- f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
485
- f" size of {batch_size}. Make sure the batch size matches the length of the generators."
486
- )
487
- if latents is None:
488
- latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
489
- latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width)
490
- else:
491
- latents = latents.to(device=device, dtype=dtype)
492
-
493
- return latents, image_latents
494
-
495
- @property
496
- def guidance_scale(self):
497
- return self._guidance_scale
498
-
499
- @property
500
- def attention_kwargs(self):
501
- return self._attention_kwargs
502
-
503
- @property
504
- def num_timesteps(self):
505
- return self._num_timesteps
506
-
507
- @property
508
- def current_timestep(self):
509
- return self._current_timestep
510
-
511
- @property
512
- def interrupt(self):
513
- return self._interrupt
514
-
515
- @torch.no_grad()
516
- @replace_example_docstring(EXAMPLE_DOC_STRING)
517
- def __call__(
518
- self,
519
- image: Optional[PipelineImageInput] = None,
520
- prompt: Union[str, List[str]] = None,
521
- negative_prompt: Union[str, List[str]] = None,
522
- true_cfg_scale: float = 4.0,
523
- height: Optional[int] = None,
524
- width: Optional[int] = None,
525
- num_inference_steps: int = 50,
526
- sigmas: Optional[List[float]] = None,
527
- guidance_scale: Optional[float] = None,
528
- num_images_per_prompt: int = 1,
529
- generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
530
- latents: Optional[torch.Tensor] = None,
531
- prompt_embeds: Optional[torch.Tensor] = None,
532
- prompt_embeds_mask: Optional[torch.Tensor] = None,
533
- negative_prompt_embeds: Optional[torch.Tensor] = None,
534
- negative_prompt_embeds_mask: Optional[torch.Tensor] = None,
535
- output_type: Optional[str] = "pil",
536
- return_dict: bool = True,
537
- attention_kwargs: Optional[Dict[str, Any]] = None,
538
- callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
539
- callback_on_step_end_tensor_inputs: List[str] = ["latents"],
540
- max_sequence_length: int = 512,
541
- ):
542
- r"""
543
- Function invoked when calling the pipeline for generation.
544
-
545
- Args:
546
- image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `List[torch.Tensor]`, `List[PIL.Image.Image]`, or `List[np.ndarray]`):
547
- `Image`, numpy array or tensor representing an image batch to be used as the starting point. For both
548
- numpy array and pytorch tensor, the expected value range is between `[0, 1]` If it's a tensor or a list
549
- or tensors, the expected shape should be `(B, C, H, W)` or `(C, H, W)`. If it is a numpy array or a
550
- list of arrays, the expected shape should be `(B, H, W, C)` or `(H, W, C)` It can also accept image
551
- latents as `image`, but if passing latents directly it is not encoded again.
552
- prompt (`str` or `List[str]`, *optional*):
553
- The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
554
- instead.
555
- negative_prompt (`str` or `List[str]`, *optional*):
556
- The prompt or prompts not to guide the image generation. If not defined, one has to pass
557
- `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `true_cfg_scale` is
558
- not greater than `1`).
559
- true_cfg_scale (`float`, *optional*, defaults to 1.0):
560
- true_cfg_scale (`float`, *optional*, defaults to 1.0): Guidance scale as defined in [Classifier-Free
561
- Diffusion Guidance](https://huggingface.co/papers/2207.12598). `true_cfg_scale` is defined as `w` of
562
- equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Classifier-free guidance is
563
- enabled by setting `true_cfg_scale > 1` and a provided `negative_prompt`. Higher guidance scale
564
- encourages to generate images that are closely linked to the text `prompt`, usually at the expense of
565
- lower image quality.
566
- height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
567
- The height in pixels of the generated image. This is set to 1024 by default for the best results.
568
- width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
569
- The width in pixels of the generated image. This is set to 1024 by default for the best results.
570
- num_inference_steps (`int`, *optional*, defaults to 50):
571
- The number of denoising steps. More denoising steps usually lead to a higher quality image at the
572
- expense of slower inference.
573
- sigmas (`List[float]`, *optional*):
574
- Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in
575
- their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed
576
- will be used.
577
- guidance_scale (`float`, *optional*, defaults to None):
578
- A guidance scale value for guidance distilled models. Unlike the traditional classifier-free guidance
579
- where the guidance scale is applied during inference through noise prediction rescaling, guidance
580
- distilled models take the guidance scale directly as an input parameter during forward pass. Guidance
581
- scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images
582
- that are closely linked to the text `prompt`, usually at the expense of lower image quality. This
583
- parameter in the pipeline is there to support future guidance-distilled models when they come up. It is
584
- ignored when not using guidance distilled models. To enable traditional classifier-free guidance,
585
- please pass `true_cfg_scale > 1.0` and `negative_prompt` (even an empty negative prompt like " " should
586
- enable classifier-free guidance computations).
587
- num_images_per_prompt (`int`, *optional*, defaults to 1):
588
- The number of images to generate per prompt.
589
- generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
590
- One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
591
- to make generation deterministic.
592
- latents (`torch.Tensor`, *optional*):
593
- Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
594
- generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
595
- tensor will be generated by sampling using the supplied random `generator`.
596
- prompt_embeds (`torch.Tensor`, *optional*):
597
- Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
598
- provided, text embeddings will be generated from `prompt` input argument.
599
- negative_prompt_embeds (`torch.Tensor`, *optional*):
600
- Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
601
- weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
602
- argument.
603
- output_type (`str`, *optional*, defaults to `"pil"`):
604
- The output format of the generate image. Choose between
605
- [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
606
- return_dict (`bool`, *optional*, defaults to `True`):
607
- Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple.
608
- attention_kwargs (`dict`, *optional*):
609
- A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
610
- `self.processor` in
611
- [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
612
- callback_on_step_end (`Callable`, *optional*):
613
- A function that calls at the end of each denoising steps during the inference. The function is called
614
- with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
615
- callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
616
- `callback_on_step_end_tensor_inputs`.
617
- callback_on_step_end_tensor_inputs (`List`, *optional*):
618
- The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
619
- will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
620
- `._callback_tensor_inputs` attribute of your pipeline class.
621
- max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`.
622
-
623
- Examples:
624
-
625
- Returns:
626
- [`~pipelines.qwenimage.QwenImagePipelineOutput`] or `tuple`:
627
- [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When
628
- returning a tuple, the first element is a list with the generated images.
629
- """
630
- image_size = image[-1].size if isinstance(image, list) else image.size
631
- calculated_width, calculated_height = calculate_dimensions(1024 * 1024, image_size[0] / image_size[1])
632
- height = height or calculated_height
633
- width = width or calculated_width
634
-
635
- multiple_of = self.vae_scale_factor * 2
636
- width = width // multiple_of * multiple_of
637
- height = height // multiple_of * multiple_of
638
-
639
- # 1. Check inputs. Raise error if not correct
640
- self.check_inputs(
641
- prompt,
642
- height,
643
- width,
644
- negative_prompt=negative_prompt,
645
- prompt_embeds=prompt_embeds,
646
- negative_prompt_embeds=negative_prompt_embeds,
647
- prompt_embeds_mask=prompt_embeds_mask,
648
- negative_prompt_embeds_mask=negative_prompt_embeds_mask,
649
- callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs,
650
- max_sequence_length=max_sequence_length,
651
- )
652
-
653
- self._guidance_scale = guidance_scale
654
- self._attention_kwargs = attention_kwargs
655
- self._current_timestep = None
656
- self._interrupt = False
657
-
658
- # 2. Define call parameters
659
- if prompt is not None and isinstance(prompt, str):
660
- batch_size = 1
661
- elif prompt is not None and isinstance(prompt, list):
662
- batch_size = len(prompt)
663
- else:
664
- batch_size = prompt_embeds.shape[0]
665
-
666
- device = self._execution_device
667
- # 3. Preprocess image
668
- if image is not None and not (isinstance(image, torch.Tensor) and image.size(1) == self.latent_channels):
669
- if not isinstance(image, list):
670
- image = [image]
671
- condition_image_sizes = []
672
- condition_images = []
673
- vae_image_sizes = []
674
- vae_images = []
675
- for img in image:
676
- image_width, image_height = img.size
677
- condition_width, condition_height = calculate_dimensions(
678
- CONDITION_IMAGE_SIZE, image_width / image_height
679
- )
680
- vae_width, vae_height = calculate_dimensions(VAE_IMAGE_SIZE, image_width / image_height)
681
- condition_image_sizes.append((condition_width, condition_height))
682
- vae_image_sizes.append((vae_width, vae_height))
683
- condition_images.append(self.image_processor.resize(img, condition_height, condition_width))
684
- vae_images.append(self.image_processor.preprocess(img, vae_height, vae_width).unsqueeze(2))
685
-
686
- has_neg_prompt = negative_prompt is not None or (
687
- negative_prompt_embeds is not None and negative_prompt_embeds_mask is not None
688
- )
689
-
690
- if true_cfg_scale > 1 and not has_neg_prompt:
691
- logger.warning(
692
- f"true_cfg_scale is passed as {true_cfg_scale}, but classifier-free guidance is not enabled since no negative_prompt is provided."
693
- )
694
- elif true_cfg_scale <= 1 and has_neg_prompt:
695
- logger.warning(
696
- " negative_prompt is passed but classifier-free guidance is not enabled since true_cfg_scale <= 1"
697
- )
698
-
699
- do_true_cfg = true_cfg_scale > 1 and has_neg_prompt
700
- prompt_embeds, prompt_embeds_mask = self.encode_prompt(
701
- image=condition_images,
702
- prompt=prompt,
703
- prompt_embeds=prompt_embeds,
704
- prompt_embeds_mask=prompt_embeds_mask,
705
- device=device,
706
- num_images_per_prompt=num_images_per_prompt,
707
- max_sequence_length=max_sequence_length,
708
- )
709
- if do_true_cfg:
710
- negative_prompt_embeds, negative_prompt_embeds_mask = self.encode_prompt(
711
- image=condition_images,
712
- prompt=negative_prompt,
713
- prompt_embeds=negative_prompt_embeds,
714
- prompt_embeds_mask=negative_prompt_embeds_mask,
715
- device=device,
716
- num_images_per_prompt=num_images_per_prompt,
717
- max_sequence_length=max_sequence_length,
718
- )
719
-
720
- # 4. Prepare latent variables
721
- num_channels_latents = self.transformer.config.in_channels // 4
722
- latents, image_latents = self.prepare_latents(
723
- vae_images,
724
- batch_size * num_images_per_prompt,
725
- num_channels_latents,
726
- height,
727
- width,
728
- prompt_embeds.dtype,
729
- device,
730
- generator,
731
- latents,
732
- )
733
- img_shapes = [
734
- [
735
- (1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2),
736
- *[
737
- (1, vae_height // self.vae_scale_factor // 2, vae_width // self.vae_scale_factor // 2)
738
- for vae_width, vae_height in vae_image_sizes
739
- ],
740
- ]
741
- ] * batch_size
742
-
743
- # 5. Prepare timesteps
744
- sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas
745
- image_seq_len = latents.shape[1]
746
- mu = calculate_shift(
747
- image_seq_len,
748
- self.scheduler.config.get("base_image_seq_len", 256),
749
- self.scheduler.config.get("max_image_seq_len", 4096),
750
- self.scheduler.config.get("base_shift", 0.5),
751
- self.scheduler.config.get("max_shift", 1.15),
752
- )
753
- timesteps, num_inference_steps = retrieve_timesteps(
754
- self.scheduler,
755
- num_inference_steps,
756
- device,
757
- sigmas=sigmas,
758
- mu=mu,
759
- )
760
- num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0)
761
- self._num_timesteps = len(timesteps)
762
-
763
- # handle guidance
764
- if self.transformer.config.guidance_embeds and guidance_scale is None:
765
- raise ValueError("guidance_scale is required for guidance-distilled model.")
766
- elif self.transformer.config.guidance_embeds:
767
- guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32)
768
- guidance = guidance.expand(latents.shape[0])
769
- elif not self.transformer.config.guidance_embeds and guidance_scale is not None:
770
- logger.warning(
771
- f"guidance_scale is passed as {guidance_scale}, but ignored since the model is not guidance-distilled."
772
- )
773
- guidance = None
774
- elif not self.transformer.config.guidance_embeds and guidance_scale is None:
775
- guidance = None
776
-
777
- if self.attention_kwargs is None:
778
- self._attention_kwargs = {}
779
-
780
- txt_seq_lens = prompt_embeds_mask.sum(dim=1).tolist() if prompt_embeds_mask is not None else None
781
-
782
- image_rotary_emb = self.transformer.pos_embed(img_shapes, txt_seq_lens, device=latents.device)
783
- if do_true_cfg:
784
- negative_txt_seq_lens = (
785
- negative_prompt_embeds_mask.sum(dim=1).tolist()
786
- if negative_prompt_embeds_mask is not None
787
- else None
788
- )
789
- uncond_image_rotary_emb = self.transformer.pos_embed(
790
- img_shapes, negative_txt_seq_lens, device=latents.device
791
- )
792
- else:
793
- uncond_image_rotary_emb = None
794
-
795
- # 6. Denoising loop
796
- self.scheduler.set_begin_index(0)
797
- with self.progress_bar(total=num_inference_steps) as progress_bar:
798
- for i, t in enumerate(timesteps):
799
- if self.interrupt:
800
- continue
801
-
802
- self._current_timestep = t
803
-
804
- latent_model_input = latents
805
- if image_latents is not None:
806
- latent_model_input = torch.cat([latents, image_latents], dim=1)
807
-
808
- # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
809
- timestep = t.expand(latents.shape[0]).to(latents.dtype)
810
- with self.transformer.cache_context("cond"):
811
- noise_pred = self.transformer(
812
- hidden_states=latent_model_input,
813
- timestep=timestep / 1000,
814
- guidance=guidance,
815
- encoder_hidden_states_mask=prompt_embeds_mask,
816
- encoder_hidden_states=prompt_embeds,
817
- image_rotary_emb=image_rotary_emb,
818
- attention_kwargs=self.attention_kwargs,
819
- return_dict=False,
820
- )[0]
821
- noise_pred = noise_pred[:, : latents.size(1)]
822
-
823
- if do_true_cfg:
824
- with self.transformer.cache_context("uncond"):
825
- neg_noise_pred = self.transformer(
826
- hidden_states=latent_model_input,
827
- timestep=timestep / 1000,
828
- guidance=guidance,
829
- encoder_hidden_states_mask=negative_prompt_embeds_mask,
830
- encoder_hidden_states=negative_prompt_embeds,
831
- image_rotary_emb=uncond_image_rotary_emb,
832
- attention_kwargs=self.attention_kwargs,
833
- return_dict=False,
834
- )[0]
835
- neg_noise_pred = neg_noise_pred[:, : latents.size(1)]
836
- comb_pred = neg_noise_pred + true_cfg_scale * (noise_pred - neg_noise_pred)
837
-
838
- cond_norm = torch.norm(noise_pred, dim=-1, keepdim=True)
839
- noise_norm = torch.norm(comb_pred, dim=-1, keepdim=True)
840
- noise_pred = comb_pred * (cond_norm / noise_norm)
841
-
842
- # compute the previous noisy sample x_t -> x_t-1
843
- latents_dtype = latents.dtype
844
- latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
845
-
846
- if latents.dtype != latents_dtype:
847
- if torch.backends.mps.is_available():
848
- # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272
849
- latents = latents.to(latents_dtype)
850
-
851
- if callback_on_step_end is not None:
852
- callback_kwargs = {}
853
- for k in callback_on_step_end_tensor_inputs:
854
- callback_kwargs[k] = locals()[k]
855
- callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
856
-
857
- latents = callback_outputs.pop("latents", latents)
858
- prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
859
-
860
- # call the callback, if provided
861
- if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
862
- progress_bar.update()
863
-
864
- if XLA_AVAILABLE:
865
- xm.mark_step()
866
-
867
- self._current_timestep = None
868
- if output_type == "latent":
869
- image = latents
870
- else:
871
- latents = self._unpack_latents(latents, height, width, self.vae_scale_factor)
872
- latents = latents.to(self.vae.dtype)
873
- latents_mean = (
874
- torch.tensor(self.vae.config.latents_mean)
875
- .view(1, self.vae.config.z_dim, 1, 1, 1)
876
- .to(latents.device, latents.dtype)
877
- )
878
- latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to(
879
- latents.device, latents.dtype
880
- )
881
- latents = latents / latents_std + latents_mean
882
- image = self.vae.decode(latents, return_dict=False)[0][:, :, 0]
883
- image = self.image_processor.postprocess(image, output_type=output_type)
884
-
885
- # Offload all models
886
- self.maybe_free_model_hooks()
887
-
888
- if not return_dict:
889
- return (image,)
890
-
891
- return QwenImagePipelineOutput(images=image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import inspect
16
+ import math
17
+ from typing import Any, Callable, Dict, List, Optional, Union
18
+
19
+ import numpy as np
20
+ import torch
21
+ from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer, Qwen2VLProcessor
22
+
23
+ from diffusers.image_processor import PipelineImageInput, VaeImageProcessor
24
+ from diffusers.loaders import QwenImageLoraLoaderMixin
25
+ from diffusers.models import AutoencoderKLQwenImage, QwenImageTransformer2DModel
26
+ from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
27
+ from diffusers.utils import is_torch_xla_available, logging, replace_example_docstring
28
+ from diffusers.utils.torch_utils import randn_tensor
29
+ from diffusers.pipelines.pipeline_utils import DiffusionPipeline
30
+ from diffusers.pipelines.qwenimage.pipeline_output import QwenImagePipelineOutput
31
+
32
+
33
+ if is_torch_xla_available():
34
+ import torch_xla.core.xla_model as xm
35
+
36
+ XLA_AVAILABLE = True
37
+ else:
38
+ XLA_AVAILABLE = False
39
+
40
+
41
+ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
42
+
43
+ EXAMPLE_DOC_STRING = """
44
+ Examples:
45
+ ```py
46
+ >>> import torch
47
+ >>> from PIL import Image, ImageOps
48
+ >>> from diffusers import QwenImageEditPlusPipeline
49
+ >>> from diffusers.utils import load_image
50
+
51
+ >>> pipe = QwenImageEditPlusPipeline.from_pretrained("Qwen/Qwen-Image-Edit-2509", torch_dtype=torch.bfloat16)
52
+ >>> pipe.to("cuda")
53
+ >>> image = load_image(
54
+ ... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/yarn-art-pikachu.png"
55
+ ... ).convert("RGB")
56
+ >>> prompt = (
57
+ ... "Make Pikachu hold a sign that says 'Qwen Edit is awesome', yarn art style, detailed, vibrant colors"
58
+ ... )
59
+ >>> # Depending on the variant being used, the pipeline call will slightly vary.
60
+ >>> # Refer to the pipeline documentation for more details.
61
+ >>> image = pipe(image, prompt, num_inference_steps=50).images[0]
62
+ >>> image.save("qwenimage_edit_plus.png")
63
+ ```
64
+ """
65
+
66
+ CONDITION_IMAGE_SIZE = 384 * 384
67
+ VAE_IMAGE_SIZE = 1024 * 1024
68
+
69
+
70
+ def pad_to_aspect(img: Image.Image, target_w: int, target_h: int) -> Image.Image:
71
+ """Pad (letterbox) to target aspect ratio without warping, then return padded image."""
72
+ return ImageOps.pad(
73
+ img,
74
+ (int(target_w), int(target_h)),
75
+ method=Image.Resampling.LANCZOS,
76
+ color=(0, 0, 0),
77
+ centering=(0.5, 0.5),
78
+ )
79
+
80
+ def choose_condition_area(canvas_area: int, base_area: int = CONDITION_IMAGE_SIZE) -> int:
81
+ """Choose a conditioning target area derived from canvas area with sensible bounds."""
82
+ # Scale conditioning area roughly with canvas, anchored at 384^2 when canvas is ~1MP.
83
+ scaled = int(canvas_area * (base_area / (1024 * 1024)))
84
+ return int(min(base_area, max(256 * 256, scaled)))
85
+
86
+
87
+ # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.calculate_shift
88
+ def calculate_shift(
89
+ image_seq_len,
90
+ base_seq_len: int = 256,
91
+ max_seq_len: int = 4096,
92
+ base_shift: float = 0.5,
93
+ max_shift: float = 1.15,
94
+ ):
95
+ m = (max_shift - base_shift) / (max_seq_len - base_seq_len)
96
+ b = base_shift - m * base_seq_len
97
+ mu = image_seq_len * m + b
98
+ return mu
99
+
100
+
101
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps
102
+ def retrieve_timesteps(
103
+ scheduler,
104
+ num_inference_steps: Optional[int] = None,
105
+ device: Optional[Union[str, torch.device]] = None,
106
+ timesteps: Optional[List[int]] = None,
107
+ sigmas: Optional[List[float]] = None,
108
+ **kwargs,
109
+ ):
110
+ r"""
111
+ Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
112
+ custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.
113
+
114
+ Args:
115
+ scheduler (`SchedulerMixin`):
116
+ The scheduler to get timesteps from.
117
+ num_inference_steps (`int`):
118
+ The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
119
+ must be `None`.
120
+ device (`str` or `torch.device`, *optional*):
121
+ The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
122
+ timesteps (`List[int]`, *optional*):
123
+ Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
124
+ `num_inference_steps` and `sigmas` must be `None`.
125
+ sigmas (`List[float]`, *optional*):
126
+ Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
127
+ `num_inference_steps` and `timesteps` must be `None`.
128
+
129
+ Returns:
130
+ `Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
131
+ second element is the number of inference steps.
132
+ """
133
+ if timesteps is not None and sigmas is not None:
134
+ raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values")
135
+ if timesteps is not None:
136
+ accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
137
+ if not accepts_timesteps:
138
+ raise ValueError(
139
+ f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
140
+ f" timestep schedules. Please check whether you are using the correct scheduler."
141
+ )
142
+ scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
143
+ timesteps = scheduler.timesteps
144
+ num_inference_steps = len(timesteps)
145
+ elif sigmas is not None:
146
+ accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
147
+ if not accept_sigmas:
148
+ raise ValueError(
149
+ f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
150
+ f" sigmas schedules. Please check whether you are using the correct scheduler."
151
+ )
152
+ scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
153
+ timesteps = scheduler.timesteps
154
+ num_inference_steps = len(timesteps)
155
+ else:
156
+ scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
157
+ timesteps = scheduler.timesteps
158
+ return timesteps, num_inference_steps
159
+
160
+
161
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents
162
+ def retrieve_latents(
163
+ encoder_output: torch.Tensor, generator: Optional[torch.Generator] = None, sample_mode: str = "sample"
164
+ ):
165
+ if hasattr(encoder_output, "latent_dist") and sample_mode == "sample":
166
+ return encoder_output.latent_dist.sample(generator)
167
+ elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax":
168
+ return encoder_output.latent_dist.mode()
169
+ elif hasattr(encoder_output, "latents"):
170
+ return encoder_output.latents
171
+ else:
172
+ raise AttributeError("Could not access latents of provided encoder_output")
173
+
174
+
175
+ def calculate_dimensions(target_area, ratio):
176
+ width = math.sqrt(target_area * ratio)
177
+ height = width / ratio
178
+
179
+ width = round(width / 32) * 32
180
+ height = round(height / 32) * 32
181
+
182
+ return width, height
183
+
184
+
185
+ class QwenImageEditPlusPipeline(DiffusionPipeline, QwenImageLoraLoaderMixin):
186
+ r"""
187
+ The Qwen-Image-Edit pipeline for image editing.
188
+
189
+ Args:
190
+ transformer ([`QwenImageTransformer2DModel`]):
191
+ Conditional Transformer (MMDiT) architecture to denoise the encoded image latents.
192
+ scheduler ([`FlowMatchEulerDiscreteScheduler`]):
193
+ A scheduler to be used in combination with `transformer` to denoise the encoded image latents.
194
+ vae ([`AutoencoderKL`]):
195
+ Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
196
+ text_encoder ([`Qwen2.5-VL-7B-Instruct`]):
197
+ [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the
198
+ [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant.
199
+ tokenizer (`QwenTokenizer`):
200
+ Tokenizer of class
201
+ [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer).
202
+ """
203
+
204
+ model_cpu_offload_seq = "text_encoder->transformer->vae"
205
+ _callback_tensor_inputs = ["latents", "prompt_embeds"]
206
+
207
+ def __init__(
208
+ self,
209
+ scheduler: FlowMatchEulerDiscreteScheduler,
210
+ vae: AutoencoderKLQwenImage,
211
+ text_encoder: Qwen2_5_VLForConditionalGeneration,
212
+ tokenizer: Qwen2Tokenizer,
213
+ processor: Qwen2VLProcessor,
214
+ transformer: QwenImageTransformer2DModel,
215
+ ):
216
+ super().__init__()
217
+
218
+ self.register_modules(
219
+ vae=vae,
220
+ text_encoder=text_encoder,
221
+ tokenizer=tokenizer,
222
+ processor=processor,
223
+ transformer=transformer,
224
+ scheduler=scheduler,
225
+ )
226
+ self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8
227
+ self.latent_channels = self.vae.config.z_dim if getattr(self, "vae", None) else 16
228
+ # QwenImage latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible
229
+ # by the patch size. So the vae scale factor is multiplied by the patch size to account for this
230
+ self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor * 2)
231
+ self.tokenizer_max_length = 1024
232
+
233
+ self.prompt_template_encode = "<|im_start|>system\nDescribe the key features of the input image (color, shape, size, texture, objects, background), then explain how the user's text instruction should alter or modify the image. Generate a new image that meets the user's requirements while maintaining consistency with the original input where appropriate.<|im_end|>\n<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n"
234
+ self.prompt_template_encode_start_idx = 64
235
+ self.default_sample_size = 128
236
+
237
+ # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._extract_masked_hidden
238
+ def _extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor):
239
+ bool_mask = mask.bool()
240
+ valid_lengths = bool_mask.sum(dim=1)
241
+ selected = hidden_states[bool_mask]
242
+ split_result = torch.split(selected, valid_lengths.tolist(), dim=0)
243
+
244
+ return split_result
245
+
246
+ def _get_qwen_prompt_embeds(
247
+ self,
248
+ prompt: Union[str, List[str]] = None,
249
+ image: Optional[torch.Tensor] = None,
250
+ device: Optional[torch.device] = None,
251
+ dtype: Optional[torch.dtype] = None,
252
+ ):
253
+ device = device or self._execution_device
254
+ dtype = dtype or self.text_encoder.dtype
255
+
256
+ prompt = [prompt] if isinstance(prompt, str) else prompt
257
+ img_prompt_template = "Picture {}: <|vision_start|><|image_pad|><|vision_end|>"
258
+ if isinstance(image, list):
259
+ base_img_prompt = ""
260
+ for i, img in enumerate(image):
261
+ base_img_prompt += img_prompt_template.format(i + 1)
262
+ elif image is not None:
263
+ base_img_prompt = img_prompt_template.format(1)
264
+ else:
265
+ base_img_prompt = ""
266
+
267
+ template = self.prompt_template_encode
268
+
269
+ drop_idx = self.prompt_template_encode_start_idx
270
+ txt = [template.format(base_img_prompt + e) for e in prompt]
271
+
272
+ model_inputs = self.processor(
273
+ text=txt,
274
+ images=image,
275
+ padding=True,
276
+ return_tensors="pt",
277
+ ).to(device)
278
+
279
+ outputs = self.text_encoder(
280
+ input_ids=model_inputs.input_ids,
281
+ attention_mask=model_inputs.attention_mask,
282
+ pixel_values=model_inputs.pixel_values,
283
+ image_grid_thw=model_inputs.image_grid_thw,
284
+ output_hidden_states=True,
285
+ )
286
+
287
+ hidden_states = outputs.hidden_states[-1]
288
+ split_hidden_states = self._extract_masked_hidden(hidden_states, model_inputs.attention_mask)
289
+ split_hidden_states = [e[drop_idx:] for e in split_hidden_states]
290
+ attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states]
291
+ max_seq_len = max([e.size(0) for e in split_hidden_states])
292
+ prompt_embeds = torch.stack(
293
+ [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states]
294
+ )
295
+ encoder_attention_mask = torch.stack(
296
+ [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list]
297
+ )
298
+
299
+ prompt_embeds = prompt_embeds.to(dtype=dtype, device=device)
300
+
301
+ return prompt_embeds, encoder_attention_mask
302
+
303
+ # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.QwenImageEditPipeline.encode_prompt
304
+ def encode_prompt(
305
+ self,
306
+ prompt: Union[str, List[str]],
307
+ image: Optional[torch.Tensor] = None,
308
+ device: Optional[torch.device] = None,
309
+ num_images_per_prompt: int = 1,
310
+ prompt_embeds: Optional[torch.Tensor] = None,
311
+ prompt_embeds_mask: Optional[torch.Tensor] = None,
312
+ max_sequence_length: int = 1024,
313
+ ):
314
+ r"""
315
+
316
+ Args:
317
+ prompt (`str` or `List[str]`, *optional*):
318
+ prompt to be encoded
319
+ image (`torch.Tensor`, *optional*):
320
+ image to be encoded
321
+ device: (`torch.device`):
322
+ torch device
323
+ num_images_per_prompt (`int`):
324
+ number of images that should be generated per prompt
325
+ prompt_embeds (`torch.Tensor`, *optional*):
326
+ Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
327
+ provided, text embeddings will be generated from `prompt` input argument.
328
+ """
329
+ device = device or self._execution_device
330
+
331
+ prompt = [prompt] if isinstance(prompt, str) else prompt
332
+ batch_size = len(prompt) if prompt_embeds is None else prompt_embeds.shape[0]
333
+
334
+ if prompt_embeds is None:
335
+ prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds(prompt, image, device)
336
+
337
+ _, seq_len, _ = prompt_embeds.shape
338
+ prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1)
339
+ prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1)
340
+ prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1)
341
+ prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len)
342
+
343
+ return prompt_embeds, prompt_embeds_mask
344
+
345
+ # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.QwenImageEditPipeline.check_inputs
346
+ def check_inputs(
347
+ self,
348
+ prompt,
349
+ height,
350
+ width,
351
+ negative_prompt=None,
352
+ prompt_embeds=None,
353
+ negative_prompt_embeds=None,
354
+ prompt_embeds_mask=None,
355
+ negative_prompt_embeds_mask=None,
356
+ callback_on_step_end_tensor_inputs=None,
357
+ max_sequence_length=None,
358
+ ):
359
+ if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0:
360
+ logger.warning(
361
+ f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly"
362
+ )
363
+
364
+ if callback_on_step_end_tensor_inputs is not None and not all(
365
+ k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs
366
+ ):
367
+ raise ValueError(
368
+ f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}"
369
+ )
370
+
371
+ if prompt is not None and prompt_embeds is not None:
372
+ raise ValueError(
373
+ f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
374
+ " only forward one of the two."
375
+ )
376
+ elif prompt is None and prompt_embeds is None:
377
+ raise ValueError(
378
+ "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined."
379
+ )
380
+ elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)):
381
+ raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
382
+
383
+ if negative_prompt is not None and negative_prompt_embeds is not None:
384
+ raise ValueError(
385
+ f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:"
386
+ f" {negative_prompt_embeds}. Please make sure to only forward one of the two."
387
+ )
388
+
389
+ if prompt_embeds is not None and prompt_embeds_mask is None:
390
+ raise ValueError(
391
+ "If `prompt_embeds` are provided, `prompt_embeds_mask` also have to be passed. Make sure to generate `prompt_embeds_mask` from the same text encoder that was used to generate `prompt_embeds`."
392
+ )
393
+ if negative_prompt_embeds is not None and negative_prompt_embeds_mask is None:
394
+ raise ValueError(
395
+ "If `negative_prompt_embeds` are provided, `negative_prompt_embeds_mask` also have to be passed. Make sure to generate `negative_prompt_embeds_mask` from the same text encoder that was used to generate `negative_prompt_embeds`."
396
+ )
397
+
398
+ if max_sequence_length is not None and max_sequence_length > 1024:
399
+ raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}")
400
+
401
+ @staticmethod
402
+ # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._pack_latents
403
+ def _pack_latents(latents, batch_size, num_channels_latents, height, width):
404
+ latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2)
405
+ latents = latents.permute(0, 2, 4, 1, 3, 5)
406
+ latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4)
407
+
408
+ return latents
409
+
410
+ @staticmethod
411
+ # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._unpack_latents
412
+ def _unpack_latents(latents, height, width, vae_scale_factor):
413
+ batch_size, num_patches, channels = latents.shape
414
+
415
+ # VAE applies 8x compression on images but we must also account for packing which requires
416
+ # latent height and width to be divisible by 2.
417
+ height = 2 * (int(height) // (vae_scale_factor * 2))
418
+ width = 2 * (int(width) // (vae_scale_factor * 2))
419
+
420
+ latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2)
421
+ latents = latents.permute(0, 3, 1, 4, 2, 5)
422
+
423
+ latents = latents.reshape(batch_size, channels // (2 * 2), 1, height, width)
424
+
425
+ return latents
426
+
427
+ # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.QwenImageEditPipeline._encode_vae_image
428
+ def _encode_vae_image(self, image: torch.Tensor, generator: torch.Generator):
429
+ if isinstance(generator, list):
430
+ image_latents = [
431
+ retrieve_latents(self.vae.encode(image[i : i + 1]), generator=generator[i], sample_mode="argmax")
432
+ for i in range(image.shape[0])
433
+ ]
434
+ image_latents = torch.cat(image_latents, dim=0)
435
+ else:
436
+ image_latents = retrieve_latents(self.vae.encode(image), generator=generator, sample_mode="argmax")
437
+ latents_mean = (
438
+ torch.tensor(self.vae.config.latents_mean)
439
+ .view(1, self.latent_channels, 1, 1, 1)
440
+ .to(image_latents.device, image_latents.dtype)
441
+ )
442
+ latents_std = (
443
+ torch.tensor(self.vae.config.latents_std)
444
+ .view(1, self.latent_channels, 1, 1, 1)
445
+ .to(image_latents.device, image_latents.dtype)
446
+ )
447
+ image_latents = (image_latents - latents_mean) / latents_std
448
+
449
+ return image_latents
450
+
451
+ def prepare_latents(
452
+ self,
453
+ images,
454
+ batch_size,
455
+ num_channels_latents,
456
+ height,
457
+ width,
458
+ dtype,
459
+ device,
460
+ generator,
461
+ latents=None,
462
+ ):
463
+ # VAE applies 8x compression on images but we must also account for packing which requires
464
+ # latent height and width to be divisible by 2.
465
+ height = 2 * (int(height) // (self.vae_scale_factor * 2))
466
+ width = 2 * (int(width) // (self.vae_scale_factor * 2))
467
+
468
+ shape = (batch_size, 1, num_channels_latents, height, width)
469
+
470
+ image_latents = None
471
+ if images is not None:
472
+ if not isinstance(images, list):
473
+ images = [images]
474
+ all_image_latents = []
475
+ for image in images:
476
+ image = image.to(device=device, dtype=dtype)
477
+ if image.shape[1] != self.latent_channels:
478
+ image_latents = self._encode_vae_image(image=image, generator=generator)
479
+ else:
480
+ image_latents = image
481
+ if batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] == 0:
482
+ # expand init_latents for batch_size
483
+ additional_image_per_prompt = batch_size // image_latents.shape[0]
484
+ image_latents = torch.cat([image_latents] * additional_image_per_prompt, dim=0)
485
+ elif batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] != 0:
486
+ raise ValueError(
487
+ f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts."
488
+ )
489
+ else:
490
+ image_latents = torch.cat([image_latents], dim=0)
491
+
492
+ image_latent_height, image_latent_width = image_latents.shape[3:]
493
+ image_latents = self._pack_latents(
494
+ image_latents, batch_size, num_channels_latents, image_latent_height, image_latent_width
495
+ )
496
+ all_image_latents.append(image_latents)
497
+ image_latents = torch.cat(all_image_latents, dim=1)
498
+
499
+ if isinstance(generator, list) and len(generator) != batch_size:
500
+ raise ValueError(
501
+ f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
502
+ f" size of {batch_size}. Make sure the batch size matches the length of the generators."
503
+ )
504
+ if latents is None:
505
+ latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
506
+ latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width)
507
+ else:
508
+ latents = latents.to(device=device, dtype=dtype)
509
+
510
+ return latents, image_latents
511
+
512
+ @property
513
+ def guidance_scale(self):
514
+ return self._guidance_scale
515
+
516
+ @property
517
+ def attention_kwargs(self):
518
+ return self._attention_kwargs
519
+
520
+ @property
521
+ def num_timesteps(self):
522
+ return self._num_timesteps
523
+
524
+ @property
525
+ def current_timestep(self):
526
+ return self._current_timestep
527
+
528
+ @property
529
+ def interrupt(self):
530
+ return self._interrupt
531
+
532
+ @torch.no_grad()
533
+ @replace_example_docstring(EXAMPLE_DOC_STRING)
534
+ def __call__(
535
+ self,
536
+ image: Optional[PipelineImageInput] = None,
537
+ prompt: Union[str, List[str]] = None,
538
+ negative_prompt: Union[str, List[str]] = None,
539
+ true_cfg_scale: float = 4.0,
540
+ height: Optional[int] = None,
541
+ width: Optional[int] = None,
542
+ condition_area: Optional[int] = None,
543
+ vae_image_indices: Optional[List[int]] = None,
544
+ pad_to_canvas: bool = True,
545
+ num_inference_steps: int = 50,
546
+ sigmas: Optional[List[float]] = None,
547
+ guidance_scale: Optional[float] = None,
548
+ num_images_per_prompt: int = 1,
549
+ generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
550
+ latents: Optional[torch.Tensor] = None,
551
+ prompt_embeds: Optional[torch.Tensor] = None,
552
+ prompt_embeds_mask: Optional[torch.Tensor] = None,
553
+ negative_prompt_embeds: Optional[torch.Tensor] = None,
554
+ negative_prompt_embeds_mask: Optional[torch.Tensor] = None,
555
+ output_type: Optional[str] = "pil",
556
+ return_dict: bool = True,
557
+ attention_kwargs: Optional[Dict[str, Any]] = None,
558
+ callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
559
+ callback_on_step_end_tensor_inputs: List[str] = ["latents"],
560
+ max_sequence_length: int = 512,
561
+ ):
562
+ r"""
563
+ Function invoked when calling the pipeline for generation.
564
+
565
+ Args:
566
+ image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `List[torch.Tensor]`, `List[PIL.Image.Image]`, or `List[np.ndarray]`):
567
+ `Image`, numpy array or tensor representing an image batch to be used as the starting point. For both
568
+ numpy array and pytorch tensor, the expected value range is between `[0, 1]` If it's a tensor or a list
569
+ or tensors, the expected shape should be `(B, C, H, W)` or `(C, H, W)`. If it is a numpy array or a
570
+ list of arrays, the expected shape should be `(B, H, W, C)` or `(H, W, C)` It can also accept image
571
+ latents as `image`, but if passing latents directly it is not encoded again.
572
+ prompt (`str` or `List[str]`, *optional*):
573
+ The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
574
+ instead.
575
+ negative_prompt (`str` or `List[str]`, *optional*):
576
+ The prompt or prompts not to guide the image generation. If not defined, one has to pass
577
+ `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `true_cfg_scale` is
578
+ not greater than `1`).
579
+ true_cfg_scale (`float`, *optional*, defaults to 1.0):
580
+ true_cfg_scale (`float`, *optional*, defaults to 1.0): Guidance scale as defined in [Classifier-Free
581
+ Diffusion Guidance](https://huggingface.co/papers/2207.12598). `true_cfg_scale` is defined as `w` of
582
+ equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Classifier-free guidance is
583
+ enabled by setting `true_cfg_scale > 1` and a provided `negative_prompt`. Higher guidance scale
584
+ encourages to generate images that are closely linked to the text `prompt`, usually at the expense of
585
+ lower image quality.
586
+ height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
587
+ The height in pixels of the generated image. This is set to 1024 by default for the best results.
588
+ width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
589
+ The width in pixels of the generated image. This is set to 1024 by default for the best results.
590
+ num_inference_steps (`int`, *optional*, defaults to 50):
591
+ The number of denoising steps. More denoising steps usually lead to a higher quality image at the
592
+ expense of slower inference.
593
+ sigmas (`List[float]`, *optional*):
594
+ Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in
595
+ their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed
596
+ will be used.
597
+ guidance_scale (`float`, *optional*, defaults to None):
598
+ A guidance scale value for guidance distilled models. Unlike the traditional classifier-free guidance
599
+ where the guidance scale is applied during inference through noise prediction rescaling, guidance
600
+ distilled models take the guidance scale directly as an input parameter during forward pass. Guidance
601
+ scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images
602
+ that are closely linked to the text `prompt`, usually at the expense of lower image quality. This
603
+ parameter in the pipeline is there to support future guidance-distilled models when they come up. It is
604
+ ignored when not using guidance distilled models. To enable traditional classifier-free guidance,
605
+ please pass `true_cfg_scale > 1.0` and `negative_prompt` (even an empty negative prompt like " " should
606
+ enable classifier-free guidance computations).
607
+ num_images_per_prompt (`int`, *optional*, defaults to 1):
608
+ The number of images to generate per prompt.
609
+ generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
610
+ One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
611
+ to make generation deterministic.
612
+ latents (`torch.Tensor`, *optional*):
613
+ Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
614
+ generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
615
+ tensor will be generated by sampling using the supplied random `generator`.
616
+ prompt_embeds (`torch.Tensor`, *optional*):
617
+ Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
618
+ provided, text embeddings will be generated from `prompt` input argument.
619
+ negative_prompt_embeds (`torch.Tensor`, *optional*):
620
+ Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
621
+ weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
622
+ argument.
623
+ output_type (`str`, *optional*, defaults to `"pil"`):
624
+ The output format of the generate image. Choose between
625
+ [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
626
+ return_dict (`bool`, *optional*, defaults to `True`):
627
+ Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple.
628
+ attention_kwargs (`dict`, *optional*):
629
+ A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
630
+ `self.processor` in
631
+ [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
632
+ callback_on_step_end (`Callable`, *optional*):
633
+ A function that calls at the end of each denoising steps during the inference. The function is called
634
+ with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
635
+ callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
636
+ `callback_on_step_end_tensor_inputs`.
637
+ callback_on_step_end_tensor_inputs (`List`, *optional*):
638
+ The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
639
+ will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
640
+ `._callback_tensor_inputs` attribute of your pipeline class.
641
+ max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`.
642
+
643
+ Examples:
644
+
645
+ Returns:
646
+ [`~pipelines.qwenimage.QwenImagePipelineOutput`] or `tuple`:
647
+ [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When
648
+ returning a tuple, the first element is a list with the generated images.
649
+ """
650
+ image_size = image[0].size if isinstance(image, list) else image.size
651
+ calculated_width, calculated_height = calculate_dimensions(1024 * 1024, image_size[0] / image_size[1])
652
+ height = height or calculated_height
653
+ width = width or calculated_width
654
+
655
+ multiple_of = self.vae_scale_factor * 2
656
+ width = width // multiple_of * multiple_of
657
+ height = height // multiple_of * multiple_of
658
+
659
+ # 1. Check inputs. Raise error if not correct
660
+ self.check_inputs(
661
+ prompt,
662
+ height,
663
+ width,
664
+ negative_prompt=negative_prompt,
665
+ prompt_embeds=prompt_embeds,
666
+ negative_prompt_embeds=negative_prompt_embeds,
667
+ prompt_embeds_mask=prompt_embeds_mask,
668
+ negative_prompt_embeds_mask=negative_prompt_embeds_mask,
669
+ callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs,
670
+ max_sequence_length=max_sequence_length,
671
+ )
672
+
673
+ self._guidance_scale = guidance_scale
674
+ self._attention_kwargs = attention_kwargs
675
+ self._current_timestep = None
676
+ self._interrupt = False
677
+
678
+ # 2. Define call parameters
679
+ if prompt is not None and isinstance(prompt, str):
680
+ batch_size = 1
681
+ elif prompt is not None and isinstance(prompt, list):
682
+ batch_size = len(prompt)
683
+ else:
684
+ batch_size = prompt_embeds.shape[0]
685
+
686
+ device = self._execution_device
687
+ # 3. Preprocess image
688
+ if image is not None and not (isinstance(image, torch.Tensor) and image.size(1) == self.latent_channels):
689
+ if not isinstance(image, list):
690
+ image = [image]
691
+ condition_image_sizes = []
692
+ condition_images = []
693
+ vae_image_sizes = []
694
+ vae_images = []
695
+
696
+ # Decide which images participate in the VAE latent stream.
697
+ # Default: all images. This can be overridden (e.g. extras conditioning-only).
698
+ if vae_image_indices is None:
699
+ vae_image_indices = list(range(len(image)))
700
+ vae_set = set(int(i) for i in vae_image_indices)
701
+
702
+ # Conditioning resolution: derived from canvas unless explicitly provided.
703
+ canvas_area = int(width) * int(height)
704
+ cond_area = int(condition_area) if condition_area is not None else choose_condition_area(canvas_area)
705
+ cond_w, cond_h = calculate_dimensions(cond_area, float(width) / float(height))
706
+
707
+ for idx, img in enumerate(image):
708
+ # Always ensure RGB PIL
709
+ if isinstance(img, Image.Image):
710
+ pil = img.convert("RGB")
711
+ else:
712
+ pil = img
713
+
714
+ if pad_to_canvas and isinstance(pil, Image.Image):
715
+ pil = pad_to_aspect(pil, int(width), int(height))
716
+
717
+ # Conditioning (VL) path: always include
718
+ condition_image_sizes.append((cond_w, cond_h))
719
+ condition_images.append(self.image_processor.resize(pil, cond_h, cond_w))
720
+
721
+ # VAE latent path: include only selected indices
722
+ if idx in vae_set:
723
+ # IMPORTANT: VAE preprocessing uses the *canvas* size to avoid drift/zoom.
724
+ vae_image_sizes.append((int(width), int(height)))
725
+ vae_images.append(self.image_processor.preprocess(pil, int(height), int(width)).unsqueeze(2))
726
+
727
+ has_neg_prompt = negative_prompt is not None or (
728
+ negative_prompt_embeds is not None and negative_prompt_embeds_mask is not None
729
+ )
730
+
731
+ if true_cfg_scale > 1 and not has_neg_prompt:
732
+ logger.warning(
733
+ f"true_cfg_scale is passed as {true_cfg_scale}, but classifier-free guidance is not enabled since no negative_prompt is provided."
734
+ )
735
+ elif true_cfg_scale <= 1 and has_neg_prompt:
736
+ logger.warning(
737
+ " negative_prompt is passed but classifier-free guidance is not enabled since true_cfg_scale <= 1"
738
+ )
739
+
740
+ do_true_cfg = true_cfg_scale > 1 and has_neg_prompt
741
+ prompt_embeds, prompt_embeds_mask = self.encode_prompt(
742
+ image=condition_images,
743
+ prompt=prompt,
744
+ prompt_embeds=prompt_embeds,
745
+ prompt_embeds_mask=prompt_embeds_mask,
746
+ device=device,
747
+ num_images_per_prompt=num_images_per_prompt,
748
+ max_sequence_length=max_sequence_length,
749
+ )
750
+ if do_true_cfg:
751
+ negative_prompt_embeds, negative_prompt_embeds_mask = self.encode_prompt(
752
+ image=condition_images,
753
+ prompt=negative_prompt,
754
+ prompt_embeds=negative_prompt_embeds,
755
+ prompt_embeds_mask=negative_prompt_embeds_mask,
756
+ device=device,
757
+ num_images_per_prompt=num_images_per_prompt,
758
+ max_sequence_length=max_sequence_length,
759
+ )
760
+
761
+ # 4. Prepare latent variables
762
+ num_channels_latents = self.transformer.config.in_channels // 4
763
+ latents, image_latents = self.prepare_latents(
764
+ vae_images,
765
+ batch_size * num_images_per_prompt,
766
+ num_channels_latents,
767
+ height,
768
+ width,
769
+ prompt_embeds.dtype,
770
+ device,
771
+ generator,
772
+ latents,
773
+ )
774
+ img_shapes = [
775
+ [
776
+ (1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2),
777
+ *[
778
+ (1, vae_height // self.vae_scale_factor // 2, vae_width // self.vae_scale_factor // 2)
779
+ for vae_width, vae_height in vae_image_sizes
780
+ ],
781
+ ]
782
+ ] * batch_size
783
+
784
+ # 5. Prepare timesteps
785
+ sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas
786
+ image_seq_len = latents.shape[1]
787
+ mu = calculate_shift(
788
+ image_seq_len,
789
+ self.scheduler.config.get("base_image_seq_len", 256),
790
+ self.scheduler.config.get("max_image_seq_len", 4096),
791
+ self.scheduler.config.get("base_shift", 0.5),
792
+ self.scheduler.config.get("max_shift", 1.15),
793
+ )
794
+ timesteps, num_inference_steps = retrieve_timesteps(
795
+ self.scheduler,
796
+ num_inference_steps,
797
+ device,
798
+ sigmas=sigmas,
799
+ mu=mu,
800
+ )
801
+ num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0)
802
+ self._num_timesteps = len(timesteps)
803
+
804
+ # handle guidance
805
+ if self.transformer.config.guidance_embeds and guidance_scale is None:
806
+ raise ValueError("guidance_scale is required for guidance-distilled model.")
807
+ elif self.transformer.config.guidance_embeds:
808
+ guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32)
809
+ guidance = guidance.expand(latents.shape[0])
810
+ elif not self.transformer.config.guidance_embeds and guidance_scale is not None:
811
+ logger.warning(
812
+ f"guidance_scale is passed as {guidance_scale}, but ignored since the model is not guidance-distilled."
813
+ )
814
+ guidance = None
815
+ elif not self.transformer.config.guidance_embeds and guidance_scale is None:
816
+ guidance = None
817
+
818
+ if self.attention_kwargs is None:
819
+ self._attention_kwargs = {}
820
+
821
+ txt_seq_lens = prompt_embeds_mask.sum(dim=1).tolist() if prompt_embeds_mask is not None else None
822
+
823
+ image_rotary_emb = self.transformer.pos_embed(img_shapes, txt_seq_lens, device=latents.device)
824
+ if do_true_cfg:
825
+ negative_txt_seq_lens = (
826
+ negative_prompt_embeds_mask.sum(dim=1).tolist()
827
+ if negative_prompt_embeds_mask is not None
828
+ else None
829
+ )
830
+ uncond_image_rotary_emb = self.transformer.pos_embed(
831
+ img_shapes, negative_txt_seq_lens, device=latents.device
832
+ )
833
+ else:
834
+ uncond_image_rotary_emb = None
835
+
836
+ # 6. Denoising loop
837
+ self.scheduler.set_begin_index(0)
838
+ with self.progress_bar(total=num_inference_steps) as progress_bar:
839
+ for i, t in enumerate(timesteps):
840
+ if self.interrupt:
841
+ continue
842
+
843
+ self._current_timestep = t
844
+
845
+ latent_model_input = latents
846
+ if image_latents is not None:
847
+ latent_model_input = torch.cat([latents, image_latents], dim=1)
848
+
849
+ # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
850
+ timestep = t.expand(latents.shape[0]).to(latents.dtype)
851
+ with self.transformer.cache_context("cond"):
852
+ noise_pred = self.transformer(
853
+ hidden_states=latent_model_input,
854
+ timestep=timestep / 1000,
855
+ guidance=guidance,
856
+ encoder_hidden_states_mask=prompt_embeds_mask,
857
+ encoder_hidden_states=prompt_embeds,
858
+ image_rotary_emb=image_rotary_emb,
859
+ attention_kwargs=self.attention_kwargs,
860
+ return_dict=False,
861
+ )[0]
862
+ noise_pred = noise_pred[:, : latents.size(1)]
863
+
864
+ if do_true_cfg:
865
+ with self.transformer.cache_context("uncond"):
866
+ neg_noise_pred = self.transformer(
867
+ hidden_states=latent_model_input,
868
+ timestep=timestep / 1000,
869
+ guidance=guidance,
870
+ encoder_hidden_states_mask=negative_prompt_embeds_mask,
871
+ encoder_hidden_states=negative_prompt_embeds,
872
+ image_rotary_emb=uncond_image_rotary_emb,
873
+ attention_kwargs=self.attention_kwargs,
874
+ return_dict=False,
875
+ )[0]
876
+ neg_noise_pred = neg_noise_pred[:, : latents.size(1)]
877
+ comb_pred = neg_noise_pred + true_cfg_scale * (noise_pred - neg_noise_pred)
878
+
879
+ cond_norm = torch.norm(noise_pred, dim=-1, keepdim=True)
880
+ noise_norm = torch.norm(comb_pred, dim=-1, keepdim=True)
881
+ noise_pred = comb_pred * (cond_norm / noise_norm)
882
+
883
+ # compute the previous noisy sample x_t -> x_t-1
884
+ latents_dtype = latents.dtype
885
+ latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
886
+
887
+ if latents.dtype != latents_dtype:
888
+ if torch.backends.mps.is_available():
889
+ # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272
890
+ latents = latents.to(latents_dtype)
891
+
892
+ if callback_on_step_end is not None:
893
+ callback_kwargs = {}
894
+ for k in callback_on_step_end_tensor_inputs:
895
+ callback_kwargs[k] = locals()[k]
896
+ callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
897
+
898
+ latents = callback_outputs.pop("latents", latents)
899
+ prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
900
+
901
+ # call the callback, if provided
902
+ if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
903
+ progress_bar.update()
904
+
905
+ if XLA_AVAILABLE:
906
+ xm.mark_step()
907
+
908
+ self._current_timestep = None
909
+ if output_type == "latent":
910
+ image = latents
911
+ else:
912
+ latents = self._unpack_latents(latents, height, width, self.vae_scale_factor)
913
+ latents = latents.to(self.vae.dtype)
914
+ latents_mean = (
915
+ torch.tensor(self.vae.config.latents_mean)
916
+ .view(1, self.vae.config.z_dim, 1, 1, 1)
917
+ .to(latents.device, latents.dtype)
918
+ )
919
+ latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to(
920
+ latents.device, latents.dtype
921
+ )
922
+ latents = latents / latents_std + latents_mean
923
+ image = self.vae.decode(latents, return_dict=False)[0][:, :, 0]
924
+ image = self.image_processor.postprocess(image, output_type=output_type)
925
+
926
+ # Offload all models
927
+ self.maybe_free_model_hooks()
928
+
929
+ if not return_dict:
930
+ return (image,)
931
+
932
+ return QwenImagePipelineOutput(images=image)