mirror of
https://github.com/Wan-Video/Wan2.1.git
synced 2025-11-04 14:16:57 +00:00
1000 lines
48 KiB
Python
1000 lines
48 KiB
Python
# Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
from mmgp import offload
|
|
import inspect
|
|
from typing import Any, Callable, Dict, List, Optional, Union
|
|
|
|
import numpy as np
|
|
import torch, json
|
|
import math
|
|
from diffusers.image_processor import VaeImageProcessor
|
|
from .transformer_qwenimage import QwenImageTransformer2DModel
|
|
|
|
from diffusers.utils import logging, replace_example_docstring
|
|
from diffusers.utils.torch_utils import randn_tensor
|
|
from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer, AutoTokenizer
|
|
from .autoencoder_kl_qwenimage import AutoencoderKLQwenImage
|
|
from diffusers import FlowMatchEulerDiscreteScheduler
|
|
from PIL import Image
|
|
from shared.utils.utils import calculate_new_dimensions, convert_image_to_tensor, convert_tensor_to_image
|
|
|
|
XLA_AVAILABLE = False
|
|
|
|
PREFERRED_QWENIMAGE_RESOLUTIONS = [
|
|
(672, 1568),
|
|
(688, 1504),
|
|
(720, 1456),
|
|
(752, 1392),
|
|
(800, 1328),
|
|
(832, 1248),
|
|
(880, 1184),
|
|
(944, 1104),
|
|
(1024, 1024),
|
|
(1104, 944),
|
|
(1184, 880),
|
|
(1248, 832),
|
|
(1328, 800),
|
|
(1392, 752),
|
|
(1456, 720),
|
|
(1504, 688),
|
|
(1568, 672),
|
|
]
|
|
|
|
|
|
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
|
|
|
EXAMPLE_DOC_STRING = """
|
|
Examples:
|
|
```py
|
|
>>> import torch
|
|
>>> from diffusers import QwenImagePipeline
|
|
|
|
>>> pipe = QwenImagePipeline.from_pretrained("Qwen/QwenImage-20B", torch_dtype=torch.bfloat16)
|
|
>>> pipe.to("cuda")
|
|
>>> 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=4, guidance_scale=0.0).images[0]
|
|
>>> image.save("qwenimage.png")
|
|
```
|
|
"""
|
|
|
|
|
|
def calculate_shift(
|
|
image_seq_len,
|
|
base_seq_len: int = 256,
|
|
max_seq_len: int = 4096,
|
|
base_shift: float = 0.5,
|
|
max_shift: float = 1.15,
|
|
):
|
|
m = (max_shift - base_shift) / (max_seq_len - base_seq_len)
|
|
b = base_shift - m * base_seq_len
|
|
mu = image_seq_len * m + b
|
|
return mu
|
|
|
|
|
|
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps
|
|
def retrieve_timesteps(
|
|
scheduler,
|
|
num_inference_steps: Optional[int] = None,
|
|
device: Optional[Union[str, torch.device]] = None,
|
|
timesteps: Optional[List[int]] = None,
|
|
sigmas: Optional[List[float]] = None,
|
|
**kwargs,
|
|
):
|
|
r"""
|
|
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
|
|
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.
|
|
|
|
Args:
|
|
scheduler (`SchedulerMixin`):
|
|
The scheduler to get timesteps from.
|
|
num_inference_steps (`int`):
|
|
The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
|
|
must be `None`.
|
|
device (`str` or `torch.device`, *optional*):
|
|
The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
|
|
timesteps (`List[int]`, *optional*):
|
|
Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
|
|
`num_inference_steps` and `sigmas` must be `None`.
|
|
sigmas (`List[float]`, *optional*):
|
|
Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
|
|
`num_inference_steps` and `timesteps` must be `None`.
|
|
|
|
Returns:
|
|
`Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
|
|
second element is the number of inference steps.
|
|
"""
|
|
if timesteps is not None and sigmas is not None:
|
|
raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values")
|
|
if timesteps is not None:
|
|
accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
|
|
if not accepts_timesteps:
|
|
raise ValueError(
|
|
f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
|
|
f" timestep schedules. Please check whether you are using the correct scheduler."
|
|
)
|
|
scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
|
|
timesteps = scheduler.timesteps
|
|
num_inference_steps = len(timesteps)
|
|
elif sigmas is not None:
|
|
accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
|
|
if not accept_sigmas:
|
|
raise ValueError(
|
|
f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
|
|
f" sigmas schedules. Please check whether you are using the correct scheduler."
|
|
)
|
|
scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
|
|
timesteps = scheduler.timesteps
|
|
num_inference_steps = len(timesteps)
|
|
else:
|
|
scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
|
|
timesteps = scheduler.timesteps
|
|
return timesteps, num_inference_steps
|
|
|
|
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents
|
|
def retrieve_latents(
|
|
encoder_output: torch.Tensor, generator: Optional[torch.Generator] = None, sample_mode: str = "sample"
|
|
):
|
|
if hasattr(encoder_output, "latent_dist") and sample_mode == "sample":
|
|
return encoder_output.latent_dist.sample(generator)
|
|
elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax":
|
|
return encoder_output.latent_dist.mode()
|
|
elif hasattr(encoder_output, "latents"):
|
|
return encoder_output.latents
|
|
else:
|
|
raise AttributeError("Could not access latents of provided encoder_output")
|
|
|
|
class QwenImagePipeline(): #DiffusionPipeline
|
|
r"""
|
|
The QwenImage pipeline for text-to-image generation.
|
|
|
|
Args:
|
|
transformer ([`QwenImageTransformer2DModel`]):
|
|
Conditional Transformer (MMDiT) architecture to denoise the encoded image latents.
|
|
scheduler ([`FlowMatchEulerDiscreteScheduler`]):
|
|
A scheduler to be used in combination with `transformer` to denoise the encoded image latents.
|
|
vae ([`AutoencoderKL`]):
|
|
Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
|
|
text_encoder ([`Qwen2.5-VL-7B-Instruct`]):
|
|
[Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the
|
|
[Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant.
|
|
tokenizer (`QwenTokenizer`):
|
|
Tokenizer of class
|
|
[CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer).
|
|
"""
|
|
|
|
model_cpu_offload_seq = "text_encoder->transformer->vae"
|
|
_callback_tensor_inputs = ["latents", "prompt_embeds"]
|
|
|
|
def __init__(
|
|
self,
|
|
vae,
|
|
text_encoder,
|
|
tokenizer,
|
|
transformer,
|
|
processor,
|
|
):
|
|
|
|
self.vae=vae
|
|
self.text_encoder=text_encoder
|
|
self.tokenizer=tokenizer
|
|
self.transformer=transformer
|
|
self.processor = processor
|
|
|
|
self.latent_channels = self.vae.config.z_dim if getattr(self, "vae", None) else 16
|
|
self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8
|
|
# QwenImage latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible
|
|
# by the patch size. So the vae scale factor is multiplied by the patch size to account for this
|
|
self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor * 2)
|
|
self.tokenizer_max_length = 1024
|
|
if processor is not None:
|
|
# 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<|vision_start|><|image_pad|><|vision_end|>{}<|im_end|>\n<|im_start|>assistant\n"
|
|
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"
|
|
self.prompt_template_encode_start_idx = 64
|
|
else:
|
|
self.prompt_template_encode = "<|im_start|>system\nDescribe the image by detailing the color, shape, size, texture, quantity, text, spatial relationships of the objects and background:<|im_end|>\n<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n"
|
|
self.prompt_template_encode_start_idx = 34
|
|
self.default_sample_size = 128
|
|
|
|
def _extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor):
|
|
bool_mask = mask.bool()
|
|
valid_lengths = bool_mask.sum(dim=1)
|
|
selected = hidden_states[bool_mask]
|
|
split_result = torch.split(selected, valid_lengths.tolist(), dim=0)
|
|
|
|
return split_result
|
|
|
|
def _get_qwen_prompt_embeds(
|
|
self,
|
|
prompt: Union[str, List[str]] = None,
|
|
image: Optional[torch.Tensor] = None,
|
|
device: Optional[torch.device] = None,
|
|
dtype: Optional[torch.dtype] = None,
|
|
):
|
|
device = device or self._execution_device
|
|
dtype = dtype or self.text_encoder.dtype
|
|
|
|
prompt = [prompt] if isinstance(prompt, str) else prompt
|
|
|
|
template = self.prompt_template_encode
|
|
drop_idx = self.prompt_template_encode_start_idx
|
|
txt = [template.format(e) for e in prompt]
|
|
|
|
if self.processor is not None and image is not None:
|
|
img_prompt_template = "Picture {}: <|vision_start|><|image_pad|><|vision_end|>"
|
|
if isinstance(image, list):
|
|
base_img_prompt = ""
|
|
for i, img in enumerate(image):
|
|
base_img_prompt += img_prompt_template.format(i + 1)
|
|
elif image is not None:
|
|
base_img_prompt = img_prompt_template.format(1)
|
|
else:
|
|
base_img_prompt = ""
|
|
|
|
template = self.prompt_template_encode
|
|
|
|
drop_idx = self.prompt_template_encode_start_idx
|
|
txt = [template.format(base_img_prompt + e) for e in prompt]
|
|
|
|
model_inputs = self.processor(
|
|
text=txt,
|
|
images=image,
|
|
padding=True,
|
|
return_tensors="pt",
|
|
).to(device)
|
|
|
|
outputs = self.text_encoder(
|
|
input_ids=model_inputs.input_ids,
|
|
attention_mask=model_inputs.attention_mask,
|
|
pixel_values=model_inputs.pixel_values,
|
|
image_grid_thw=model_inputs.image_grid_thw,
|
|
output_hidden_states=True,
|
|
)
|
|
hidden_states = outputs.hidden_states[-1]
|
|
split_hidden_states = self._extract_masked_hidden(hidden_states, model_inputs.attention_mask)
|
|
else:
|
|
txt_tokens = self.tokenizer(
|
|
txt, max_length=self.tokenizer_max_length + drop_idx, padding=True, truncation=True, return_tensors="pt"
|
|
).to(device)
|
|
encoder_hidden_states = self.text_encoder(
|
|
input_ids=txt_tokens.input_ids,
|
|
attention_mask=txt_tokens.attention_mask,
|
|
output_hidden_states=True,
|
|
)
|
|
hidden_states = encoder_hidden_states.hidden_states[-1]
|
|
split_hidden_states = self._extract_masked_hidden(hidden_states, txt_tokens.attention_mask)
|
|
|
|
split_hidden_states = [e[drop_idx:] for e in split_hidden_states]
|
|
attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states]
|
|
max_seq_len = max([e.size(0) for e in split_hidden_states])
|
|
prompt_embeds = torch.stack(
|
|
[torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states]
|
|
)
|
|
encoder_attention_mask = torch.stack(
|
|
[torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list]
|
|
)
|
|
|
|
prompt_embeds = prompt_embeds.to(dtype=dtype, device=device)
|
|
|
|
return prompt_embeds, encoder_attention_mask
|
|
|
|
def encode_prompt(
|
|
self,
|
|
prompt: Union[str, List[str]],
|
|
image: Optional[torch.Tensor] = None,
|
|
device: Optional[torch.device] = None,
|
|
num_images_per_prompt: int = 1,
|
|
prompt_embeds: Optional[torch.Tensor] = None,
|
|
prompt_embeds_mask: Optional[torch.Tensor] = None,
|
|
max_sequence_length: int = 1024,
|
|
):
|
|
r"""
|
|
|
|
Args:
|
|
prompt (`str` or `List[str]`, *optional*):
|
|
prompt to be encoded
|
|
image (`torch.Tensor`, *optional*):
|
|
image to be encoded
|
|
device: (`torch.device`):
|
|
torch device
|
|
num_images_per_prompt (`int`):
|
|
number of images that should be generated per prompt
|
|
prompt_embeds (`torch.Tensor`, *optional*):
|
|
Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
|
|
provided, text embeddings will be generated from `prompt` input argument.
|
|
"""
|
|
device = device or self._execution_device
|
|
|
|
prompt = [prompt] if isinstance(prompt, str) else prompt
|
|
batch_size = len(prompt) if prompt_embeds is None else prompt_embeds.shape[0]
|
|
|
|
if prompt_embeds is None:
|
|
prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds(prompt, image, device)
|
|
|
|
_, seq_len, _ = prompt_embeds.shape
|
|
prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1)
|
|
prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1)
|
|
prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1)
|
|
prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len)
|
|
|
|
return prompt_embeds, prompt_embeds_mask
|
|
|
|
|
|
def check_inputs(
|
|
self,
|
|
prompt,
|
|
height,
|
|
width,
|
|
negative_prompt=None,
|
|
prompt_embeds=None,
|
|
negative_prompt_embeds=None,
|
|
prompt_embeds_mask=None,
|
|
negative_prompt_embeds_mask=None,
|
|
callback_on_step_end_tensor_inputs=None,
|
|
max_sequence_length=None,
|
|
):
|
|
if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0:
|
|
logger.warning(
|
|
f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly"
|
|
)
|
|
|
|
if callback_on_step_end_tensor_inputs is not None and not all(
|
|
k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs
|
|
):
|
|
raise ValueError(
|
|
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]}"
|
|
)
|
|
|
|
if prompt is not None and prompt_embeds is not None:
|
|
raise ValueError(
|
|
f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
|
|
" only forward one of the two."
|
|
)
|
|
elif prompt is None and prompt_embeds is None:
|
|
raise ValueError(
|
|
"Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined."
|
|
)
|
|
elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)):
|
|
raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
|
|
|
|
if negative_prompt is not None and negative_prompt_embeds is not None:
|
|
raise ValueError(
|
|
f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:"
|
|
f" {negative_prompt_embeds}. Please make sure to only forward one of the two."
|
|
)
|
|
|
|
if prompt_embeds is not None and prompt_embeds_mask is None:
|
|
raise ValueError(
|
|
"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`."
|
|
)
|
|
if negative_prompt_embeds is not None and negative_prompt_embeds_mask is None:
|
|
raise ValueError(
|
|
"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`."
|
|
)
|
|
|
|
if max_sequence_length is not None and max_sequence_length > 1024:
|
|
raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}")
|
|
|
|
@staticmethod
|
|
def _prepare_latent_image_ids(batch_size, height, width, device, dtype):
|
|
latent_image_ids = torch.zeros(height, width, 3)
|
|
latent_image_ids[..., 1] = latent_image_ids[..., 1] + torch.arange(height)[:, None]
|
|
latent_image_ids[..., 2] = latent_image_ids[..., 2] + torch.arange(width)[None, :]
|
|
|
|
latent_image_id_height, latent_image_id_width, latent_image_id_channels = latent_image_ids.shape
|
|
|
|
latent_image_ids = latent_image_ids.reshape(
|
|
latent_image_id_height * latent_image_id_width, latent_image_id_channels
|
|
)
|
|
|
|
return latent_image_ids.to(device=device, dtype=dtype)
|
|
|
|
@staticmethod
|
|
def _pack_latents(latents):
|
|
batch_size, num_channels_latents, _, height, width = latents.shape
|
|
latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2)
|
|
latents = latents.permute(0, 2, 4, 1, 3, 5)
|
|
latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4)
|
|
|
|
return latents
|
|
|
|
@staticmethod
|
|
def _unpack_latents(latents, height, width, vae_scale_factor):
|
|
batch_size, num_patches, channels = latents.shape
|
|
|
|
# VAE applies 8x compression on images but we must also account for packing which requires
|
|
# latent height and width to be divisible by 2.
|
|
height = 2 * (int(height) // (vae_scale_factor * 2))
|
|
width = 2 * (int(width) // (vae_scale_factor * 2))
|
|
|
|
latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2)
|
|
latents = latents.permute(0, 3, 1, 4, 2, 5)
|
|
|
|
latents = latents.reshape(batch_size, channels // (2 * 2), 1, height, width)
|
|
|
|
return latents
|
|
|
|
|
|
def _encode_vae_image(self, image: torch.Tensor, generator: torch.Generator):
|
|
if isinstance(generator, list):
|
|
image_latents = [
|
|
retrieve_latents(self.vae.encode(image[i : i + 1]), generator=generator[i], sample_mode="argmax")
|
|
for i in range(image.shape[0])
|
|
]
|
|
image_latents = torch.cat(image_latents, dim=0)
|
|
else:
|
|
image_latents = retrieve_latents(self.vae.encode(image), generator=generator, sample_mode="argmax")
|
|
latents_mean = (
|
|
torch.tensor(self.vae.config.latents_mean)
|
|
.view(1, self.latent_channels, 1, 1, 1)
|
|
.to(image_latents.device, image_latents.dtype)
|
|
)
|
|
latents_std = (
|
|
torch.tensor(self.vae.config.latents_std)
|
|
.view(1, self.latent_channels, 1, 1, 1)
|
|
.to(image_latents.device, image_latents.dtype)
|
|
)
|
|
image_latents = (image_latents - latents_mean) / latents_std
|
|
|
|
return image_latents
|
|
def enable_vae_slicing(self):
|
|
r"""
|
|
Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to
|
|
compute decoding in several steps. This is useful to save some memory and allow larger batch sizes.
|
|
"""
|
|
self.vae.enable_slicing()
|
|
|
|
def disable_vae_slicing(self):
|
|
r"""
|
|
Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to
|
|
computing decoding in one step.
|
|
"""
|
|
self.vae.disable_slicing()
|
|
|
|
def enable_vae_tiling(self):
|
|
r"""
|
|
Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to
|
|
compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow
|
|
processing larger images.
|
|
"""
|
|
self.vae.enable_tiling()
|
|
|
|
def disable_vae_tiling(self):
|
|
r"""
|
|
Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to
|
|
computing decoding in one step.
|
|
"""
|
|
self.vae.disable_tiling()
|
|
|
|
def prepare_latents(
|
|
self,
|
|
images,
|
|
batch_size,
|
|
num_channels_latents,
|
|
height,
|
|
width,
|
|
dtype,
|
|
device,
|
|
generator,
|
|
latents=None,
|
|
):
|
|
# VAE applies 8x compression on images but we must also account for packing which requires
|
|
# latent height and width to be divisible by 2.
|
|
height = 2 * (int(height) // (self.vae_scale_factor * 2))
|
|
width = 2 * (int(width) // (self.vae_scale_factor * 2))
|
|
|
|
shape = (batch_size, num_channels_latents, 1, height, width)
|
|
|
|
image_latents = None
|
|
if images is not None and len(images ) > 0:
|
|
if not isinstance(images, list):
|
|
images = [images]
|
|
all_image_latents = []
|
|
for image in images:
|
|
image = image.to(device=device, dtype=dtype)
|
|
if image.shape[1] != self.latent_channels:
|
|
image_latents = self._encode_vae_image(image=image, generator=generator)
|
|
else:
|
|
image_latents = image
|
|
if batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] == 0:
|
|
# expand init_latents for batch_size
|
|
additional_image_per_prompt = batch_size // image_latents.shape[0]
|
|
image_latents = torch.cat([image_latents] * additional_image_per_prompt, dim=0)
|
|
elif batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] != 0:
|
|
raise ValueError(
|
|
f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts."
|
|
)
|
|
else:
|
|
image_latents = torch.cat([image_latents], dim=0)
|
|
|
|
image_latents = self._pack_latents(image_latents)
|
|
all_image_latents.append(image_latents)
|
|
image_latents = torch.cat(all_image_latents, dim=1)
|
|
|
|
if isinstance(generator, list) and len(generator) != batch_size:
|
|
raise ValueError(
|
|
f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
|
|
f" size of {batch_size}. Make sure the batch size matches the length of the generators."
|
|
)
|
|
if latents is None:
|
|
latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
|
|
latents = self._pack_latents(latents)
|
|
else:
|
|
latents = latents.to(device=device, dtype=dtype)
|
|
|
|
return latents, image_latents
|
|
|
|
@property
|
|
def guidance_scale(self):
|
|
return self._guidance_scale
|
|
|
|
@property
|
|
def attention_kwargs(self):
|
|
return self._attention_kwargs
|
|
|
|
@property
|
|
def num_timesteps(self):
|
|
return self._num_timesteps
|
|
|
|
@property
|
|
def current_timestep(self):
|
|
return self._current_timestep
|
|
|
|
@property
|
|
def interrupt(self):
|
|
return self._interrupt
|
|
|
|
@torch.no_grad()
|
|
@replace_example_docstring(EXAMPLE_DOC_STRING)
|
|
def __call__(
|
|
self,
|
|
prompt: Union[str, List[str]] = None,
|
|
negative_prompt: Union[str, List[str]] = None,
|
|
true_cfg_scale: float = 4.0,
|
|
height: Optional[int] = None,
|
|
width: Optional[int] = None,
|
|
num_inference_steps: int = 50,
|
|
sigmas: Optional[List[float]] = None,
|
|
guidance_scale: float = 1.0,
|
|
num_images_per_prompt: int = 1,
|
|
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
|
|
latents: Optional[torch.Tensor] = None,
|
|
prompt_embeds: Optional[torch.Tensor] = None,
|
|
prompt_embeds_mask: Optional[torch.Tensor] = None,
|
|
negative_prompt_embeds: Optional[torch.Tensor] = None,
|
|
negative_prompt_embeds_mask: Optional[torch.Tensor] = None,
|
|
output_type: Optional[str] = "pil",
|
|
return_dict: bool = True,
|
|
attention_kwargs: Optional[Dict[str, Any]] = None,
|
|
callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
|
|
callback_on_step_end_tensor_inputs: List[str] = ["latents"],
|
|
max_sequence_length: int = 512,
|
|
image = None,
|
|
image_mask = None,
|
|
denoising_strength = 0,
|
|
callback=None,
|
|
pipeline=None,
|
|
loras_slists=None,
|
|
joint_pass= True,
|
|
lora_inpaint = False,
|
|
outpainting_dims = None,
|
|
qwen_edit_plus = False,
|
|
):
|
|
r"""
|
|
Function invoked when calling the pipeline for generation.
|
|
|
|
Args:
|
|
prompt (`str` or `List[str]`, *optional*):
|
|
The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
|
|
instead.
|
|
negative_prompt (`str` or `List[str]`, *optional*):
|
|
The prompt or prompts not to guide the image generation. If not defined, one has to pass
|
|
`negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `true_cfg_scale` is
|
|
not greater than `1`).
|
|
true_cfg_scale (`float`, *optional*, defaults to 1.0):
|
|
When > 1.0 and a provided `negative_prompt`, enables true classifier-free guidance.
|
|
height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
|
|
The height in pixels of the generated image. This is set to 1024 by default for the best results.
|
|
width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
|
|
The width in pixels of the generated image. This is set to 1024 by default for the best results.
|
|
num_inference_steps (`int`, *optional*, defaults to 50):
|
|
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
|
|
expense of slower inference.
|
|
sigmas (`List[float]`, *optional*):
|
|
Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in
|
|
their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed
|
|
will be used.
|
|
guidance_scale (`float`, *optional*, defaults to 3.5):
|
|
Guidance scale as defined in [Classifier-Free Diffusion
|
|
Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2.
|
|
of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting
|
|
`guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to
|
|
the text `prompt`, usually at the expense of lower image quality.
|
|
num_images_per_prompt (`int`, *optional*, defaults to 1):
|
|
The number of images to generate per prompt.
|
|
generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
|
|
One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
|
|
to make generation deterministic.
|
|
latents (`torch.Tensor`, *optional*):
|
|
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
|
|
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
|
|
tensor will be generated by sampling using the supplied random `generator`.
|
|
prompt_embeds (`torch.Tensor`, *optional*):
|
|
Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
|
|
provided, text embeddings will be generated from `prompt` input argument.
|
|
negative_prompt_embeds (`torch.Tensor`, *optional*):
|
|
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
|
|
weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
|
|
argument.
|
|
output_type (`str`, *optional*, defaults to `"pil"`):
|
|
The output format of the generate image. Choose between
|
|
[PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
|
|
return_dict (`bool`, *optional*, defaults to `True`):
|
|
Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple.
|
|
attention_kwargs (`dict`, *optional*):
|
|
A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
|
|
`self.processor` in
|
|
[diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
|
|
callback_on_step_end (`Callable`, *optional*):
|
|
A function that calls at the end of each denoising steps during the inference. The function is called
|
|
with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
|
|
callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
|
|
`callback_on_step_end_tensor_inputs`.
|
|
callback_on_step_end_tensor_inputs (`List`, *optional*):
|
|
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
|
|
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
|
|
`._callback_tensor_inputs` attribute of your pipeline class.
|
|
max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`.
|
|
|
|
Examples:
|
|
|
|
Returns:
|
|
[`~pipelines.qwenimage.QwenImagePipelineOutput`] or `tuple`:
|
|
[`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When
|
|
returning a tuple, the first element is a list with the generated images.
|
|
"""
|
|
|
|
kwargs = {'pipeline': pipeline, 'callback': callback}
|
|
if callback != None:
|
|
callback(-1, None, True)
|
|
|
|
|
|
height = height or self.default_sample_size * self.vae_scale_factor
|
|
width = width or self.default_sample_size * self.vae_scale_factor
|
|
|
|
multiple_of = self.vae_scale_factor * 2
|
|
width = width // multiple_of * multiple_of
|
|
height = height // multiple_of * multiple_of
|
|
|
|
# 1. Check inputs. Raise error if not correct
|
|
self.check_inputs(
|
|
prompt,
|
|
height,
|
|
width,
|
|
negative_prompt=negative_prompt,
|
|
prompt_embeds=prompt_embeds,
|
|
negative_prompt_embeds=negative_prompt_embeds,
|
|
prompt_embeds_mask=prompt_embeds_mask,
|
|
negative_prompt_embeds_mask=negative_prompt_embeds_mask,
|
|
callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs,
|
|
max_sequence_length=max_sequence_length,
|
|
)
|
|
|
|
self._guidance_scale = guidance_scale
|
|
self._attention_kwargs = attention_kwargs
|
|
self._current_timestep = None
|
|
self._interrupt = False
|
|
|
|
# 2. Define call parameters
|
|
if prompt is not None and isinstance(prompt, str):
|
|
batch_size = 1
|
|
elif prompt is not None and isinstance(prompt, list):
|
|
batch_size = len(prompt)
|
|
else:
|
|
batch_size = prompt_embeds.shape[0]
|
|
device = "cuda"
|
|
|
|
condition_images = []
|
|
vae_image_sizes = []
|
|
vae_images = []
|
|
image_mask_latents = None
|
|
ref_size = 1024
|
|
ref_text_encoder_size = 384 if qwen_edit_plus else 1024
|
|
if image is not None:
|
|
if not isinstance(image, list): image = [image]
|
|
if height * width < ref_size * ref_size: ref_size = round(math.sqrt(height * width))
|
|
for ref_no, img in enumerate(image):
|
|
image_width, image_height = img.size
|
|
any_mask = ref_no == 0 and image_mask is not None
|
|
if (image_height * image_width > ref_size * ref_size) and not any_mask:
|
|
vae_height, vae_width =calculate_new_dimensions(ref_size, ref_size, image_height, image_width, False, block_size=multiple_of)
|
|
else:
|
|
vae_height, vae_width = image_height, image_width
|
|
vae_width = vae_width // multiple_of * multiple_of
|
|
vae_height = vae_height // multiple_of * multiple_of
|
|
vae_image_sizes.append((vae_width, vae_height))
|
|
condition_height, condition_width =calculate_new_dimensions(ref_text_encoder_size, ref_text_encoder_size, image_height, image_width, False, block_size=multiple_of)
|
|
condition_images.append(img.resize((condition_width, condition_height), resample=Image.Resampling.LANCZOS) )
|
|
if img.size != (vae_width, vae_height):
|
|
img = img.resize((vae_width, vae_height), resample=Image.Resampling.LANCZOS)
|
|
if any_mask :
|
|
if lora_inpaint:
|
|
image_mask_rebuilt = torch.where(convert_image_to_tensor(image_mask)>-0.5, 1., 0. )[0:1]
|
|
img = convert_image_to_tensor(img)
|
|
green = torch.tensor([-1.0, 1.0, -1.0]).to(img)
|
|
green_image = green[:, None, None] .expand_as(img)
|
|
img = torch.where(image_mask_rebuilt > 0, green_image, img)
|
|
img = convert_tensor_to_image(img)
|
|
else:
|
|
image_mask_latents = convert_image_to_tensor(image_mask.resize((vae_width // 8, vae_height // 8), resample=Image.Resampling.LANCZOS))
|
|
image_mask_latents = torch.where(image_mask_latents>-0.5, 1., 0. )[0:1]
|
|
image_mask_rebuilt = image_mask_latents.repeat_interleave(8, dim=-1).repeat_interleave(8, dim=-2).unsqueeze(0)
|
|
# convert_tensor_to_image( image_mask_rebuilt.squeeze(0).repeat(3,1,1)).save("mmm.png")
|
|
image_mask_latents = image_mask_latents.to(device).unsqueeze(0).unsqueeze(0).repeat(1,16,1,1,1)
|
|
image_mask_latents = self._pack_latents(image_mask_latents)
|
|
# img.save("nnn.png")
|
|
vae_images.append( convert_image_to_tensor(img).unsqueeze(0).unsqueeze(2) )
|
|
|
|
|
|
has_neg_prompt = negative_prompt is not None or (
|
|
negative_prompt_embeds is not None and negative_prompt_embeds_mask is not None
|
|
)
|
|
do_true_cfg = true_cfg_scale > 1 and has_neg_prompt
|
|
prompt_embeds, prompt_embeds_mask = self.encode_prompt(
|
|
image=condition_images,
|
|
prompt=prompt,
|
|
prompt_embeds=prompt_embeds,
|
|
prompt_embeds_mask=prompt_embeds_mask,
|
|
device=device,
|
|
num_images_per_prompt=num_images_per_prompt,
|
|
max_sequence_length=max_sequence_length,
|
|
)
|
|
if do_true_cfg:
|
|
negative_prompt_embeds, negative_prompt_embeds_mask = self.encode_prompt(
|
|
image=condition_images,
|
|
prompt=negative_prompt,
|
|
prompt_embeds=negative_prompt_embeds,
|
|
prompt_embeds_mask=negative_prompt_embeds_mask,
|
|
device=device,
|
|
num_images_per_prompt=num_images_per_prompt,
|
|
max_sequence_length=max_sequence_length,
|
|
)
|
|
dtype = torch.bfloat16
|
|
prompt_embeds = prompt_embeds.to(dtype)
|
|
if do_true_cfg:
|
|
negative_prompt_embeds = negative_prompt_embeds.to(dtype)
|
|
|
|
# 4. Prepare latent variables
|
|
num_channels_latents = self.transformer.in_channels // 4
|
|
latents, image_latents = self.prepare_latents(
|
|
vae_images,
|
|
batch_size * num_images_per_prompt,
|
|
num_channels_latents,
|
|
height,
|
|
width,
|
|
prompt_embeds.dtype,
|
|
device,
|
|
generator,
|
|
latents,
|
|
)
|
|
original_image_latents = None if image_latents is None else image_latents.clone()
|
|
|
|
if image is not None:
|
|
img_shapes = [
|
|
[
|
|
(1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2),
|
|
# (1, image_height // self.vae_scale_factor // 2, image_width // self.vae_scale_factor // 2),
|
|
*[
|
|
(1, vae_height // self.vae_scale_factor // 2, vae_width // self.vae_scale_factor // 2)
|
|
for vae_width, vae_height in vae_image_sizes
|
|
],
|
|
|
|
]
|
|
] * batch_size
|
|
else:
|
|
img_shapes = [(1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2)] * batch_size
|
|
|
|
# 5. Prepare timesteps
|
|
sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas
|
|
image_seq_len = latents.shape[1]
|
|
mu = calculate_shift(
|
|
image_seq_len,
|
|
self.scheduler.config.get("base_image_seq_len", 256),
|
|
self.scheduler.config.get("max_image_seq_len", 4096),
|
|
self.scheduler.config.get("base_shift", 0.5),
|
|
self.scheduler.config.get("max_shift", 1.15),
|
|
)
|
|
timesteps, num_inference_steps = retrieve_timesteps(
|
|
self.scheduler,
|
|
num_inference_steps,
|
|
device,
|
|
sigmas=sigmas,
|
|
mu=mu,
|
|
)
|
|
num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0)
|
|
self._num_timesteps = len(timesteps)
|
|
|
|
# handle guidance
|
|
if self.transformer.guidance_embeds:
|
|
guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32)
|
|
guidance = guidance.expand(latents.shape[0])
|
|
else:
|
|
guidance = None
|
|
|
|
if self.attention_kwargs is None:
|
|
self._attention_kwargs = {}
|
|
|
|
txt_seq_lens = prompt_embeds_mask.sum(dim=1).tolist() if prompt_embeds_mask is not None else None
|
|
negative_txt_seq_lens = (
|
|
negative_prompt_embeds_mask.sum(dim=1).tolist() if negative_prompt_embeds_mask is not None else None
|
|
)
|
|
morph, first_step = False, 0
|
|
lanpaint_proc = None
|
|
if image_mask_latents is not None:
|
|
randn = torch.randn_like(original_image_latents)
|
|
if denoising_strength < 1.:
|
|
first_step = int(len(timesteps) * (1. - denoising_strength))
|
|
if not morph:
|
|
latent_noise_factor = timesteps[first_step]/1000
|
|
# latents = original_image_latents * (1.0 - latent_noise_factor) + torch.randn_like(original_image_latents) * latent_noise_factor
|
|
latents = original_image_latents * (1.0 - latent_noise_factor) + randn * latent_noise_factor
|
|
timesteps = timesteps[first_step:]
|
|
self.scheduler.timesteps = timesteps
|
|
self.scheduler.sigmas= self.scheduler.sigmas[first_step:]
|
|
# from shared.inpainting.lanpaint import LanPaint
|
|
# lanpaint_proc = LanPaint()
|
|
# 6. Denoising loop
|
|
self.scheduler.set_begin_index(0)
|
|
updated_num_steps= len(timesteps)
|
|
if callback != None:
|
|
from shared.utils.loras_mutipliers import update_loras_slists
|
|
update_loras_slists(self.transformer, loras_slists, updated_num_steps)
|
|
callback(-1, None, True, override_num_inference_steps = updated_num_steps)
|
|
|
|
|
|
for i, t in enumerate(timesteps):
|
|
offload.set_step_no_for_lora(self.transformer, first_step + i)
|
|
if self.interrupt:
|
|
continue
|
|
self._current_timestep = t
|
|
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
|
|
timestep = t.expand(latents.shape[0]).to(latents.dtype)
|
|
|
|
if image_mask_latents is not None and denoising_strength <1. and i == first_step and morph:
|
|
latent_noise_factor = t/1000
|
|
latents = original_image_latents * (1.0 - latent_noise_factor) + latents * latent_noise_factor
|
|
|
|
|
|
latents_dtype = latents.dtype
|
|
|
|
# latent_model_input = latents
|
|
def denoise(latent_model_input, true_cfg_scale):
|
|
if image_latents is not None:
|
|
latent_model_input = torch.cat([latents, image_latents], dim=1)
|
|
do_true_cfg = true_cfg_scale > 1
|
|
if do_true_cfg and joint_pass:
|
|
noise_pred, neg_noise_pred = self.transformer(
|
|
hidden_states=latent_model_input,
|
|
timestep=timestep / 1000,
|
|
guidance=guidance, #!!!!
|
|
encoder_hidden_states_mask_list=[prompt_embeds_mask,negative_prompt_embeds_mask],
|
|
encoder_hidden_states_list=[prompt_embeds, negative_prompt_embeds],
|
|
img_shapes=img_shapes,
|
|
txt_seq_lens_list=[txt_seq_lens, negative_txt_seq_lens],
|
|
attention_kwargs=self.attention_kwargs,
|
|
**kwargs
|
|
)
|
|
if noise_pred == None: return None, None
|
|
noise_pred = noise_pred[:, : latents.size(1)]
|
|
neg_noise_pred = neg_noise_pred[:, : latents.size(1)]
|
|
else:
|
|
neg_noise_pred = None
|
|
noise_pred = self.transformer(
|
|
hidden_states=latent_model_input,
|
|
timestep=timestep / 1000,
|
|
guidance=guidance,
|
|
encoder_hidden_states_mask_list=[prompt_embeds_mask],
|
|
encoder_hidden_states_list=[prompt_embeds],
|
|
img_shapes=img_shapes,
|
|
txt_seq_lens_list=[txt_seq_lens],
|
|
attention_kwargs=self.attention_kwargs,
|
|
**kwargs
|
|
)[0]
|
|
if noise_pred == None: return None, None
|
|
noise_pred = noise_pred[:, : latents.size(1)]
|
|
|
|
if do_true_cfg:
|
|
neg_noise_pred = self.transformer(
|
|
hidden_states=latent_model_input,
|
|
timestep=timestep / 1000,
|
|
guidance=guidance,
|
|
encoder_hidden_states_mask_list=[negative_prompt_embeds_mask],
|
|
encoder_hidden_states_list=[negative_prompt_embeds],
|
|
img_shapes=img_shapes,
|
|
txt_seq_lens_list=[negative_txt_seq_lens],
|
|
attention_kwargs=self.attention_kwargs,
|
|
**kwargs
|
|
)[0]
|
|
if neg_noise_pred == None: return None, None
|
|
neg_noise_pred = neg_noise_pred[:, : latents.size(1)]
|
|
return noise_pred, neg_noise_pred
|
|
def cfg_predictions( noise_pred, neg_noise_pred, guidance, t):
|
|
if do_true_cfg:
|
|
comb_pred = neg_noise_pred + guidance * (noise_pred - neg_noise_pred)
|
|
if comb_pred == None: return None
|
|
|
|
cond_norm = torch.norm(noise_pred, dim=-1, keepdim=True)
|
|
noise_norm = torch.norm(comb_pred, dim=-1, keepdim=True)
|
|
noise_pred = comb_pred * (cond_norm / noise_norm)
|
|
|
|
return noise_pred
|
|
|
|
|
|
if lanpaint_proc is not None and i<=3:
|
|
latents = lanpaint_proc(denoise, cfg_predictions, true_cfg_scale, 1., latents, original_image_latents, randn, t/1000, image_mask_latents, height=height , width= width, vae_scale_factor= 8)
|
|
if latents is None: return None
|
|
|
|
noise_pred, neg_noise_pred = denoise(latents, true_cfg_scale)
|
|
if noise_pred == None: return None
|
|
noise_pred = cfg_predictions(noise_pred, neg_noise_pred, true_cfg_scale, t)
|
|
neg_noise_pred = None
|
|
# compute the previous noisy sample x_t -> x_t-1
|
|
latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
|
|
noise_pred = None
|
|
|
|
if image_mask_latents is not None:
|
|
if lanpaint_proc is not None:
|
|
latents = original_image_latents * (1-image_mask_latents) + image_mask_latents * latents
|
|
else:
|
|
next_t = timesteps[i+1] if i<len(timesteps)-1 else 0
|
|
latent_noise_factor = next_t / 1000
|
|
# noisy_image = original_image_latents * (1.0 - latent_noise_factor) + torch.randn_like(original_image_latents) * latent_noise_factor
|
|
noisy_image = original_image_latents * (1.0 - latent_noise_factor) + randn * latent_noise_factor
|
|
latents = noisy_image * (1-image_mask_latents) + image_mask_latents * latents
|
|
noisy_image = None
|
|
|
|
if latents.dtype != latents_dtype:
|
|
if torch.backends.mps.is_available():
|
|
# some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272
|
|
latents = latents.to(latents_dtype)
|
|
|
|
if callback is not None:
|
|
preview = self._unpack_latents(latents, height, width, self.vae_scale_factor)
|
|
preview = preview.squeeze(0)
|
|
callback(i, preview, False)
|
|
|
|
|
|
self._current_timestep = None
|
|
if output_type == "latent":
|
|
output_image = latents
|
|
else:
|
|
latents = self._unpack_latents(latents, height, width, self.vae_scale_factor)
|
|
latents = latents.to(self.vae.dtype)
|
|
latents_mean = (
|
|
torch.tensor(self.vae.config.latents_mean)
|
|
.view(1, self.vae.config.z_dim, 1, 1, 1)
|
|
.to(latents.device, latents.dtype)
|
|
)
|
|
latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to(
|
|
latents.device, latents.dtype
|
|
)
|
|
latents = latents / latents_std + latents_mean
|
|
output_image = self.vae.decode(latents, return_dict=False)[0][:, :, 0]
|
|
if image_mask is not None and not lora_inpaint : #not (lora_inpaint and outpainting_dims is not None):
|
|
output_image = vae_images[0].squeeze(2) * (1 - image_mask_rebuilt) + output_image.to(vae_images[0] ) * image_mask_rebuilt
|
|
|
|
|
|
return output_image
|