diffusers 0.23.1__py3-none-any.whl → 0.24.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- diffusers/__init__.py +16 -2
- diffusers/configuration_utils.py +1 -0
- diffusers/dependency_versions_check.py +0 -1
- diffusers/dependency_versions_table.py +4 -5
- diffusers/image_processor.py +186 -14
- diffusers/loaders/__init__.py +82 -0
- diffusers/loaders/ip_adapter.py +157 -0
- diffusers/loaders/lora.py +1415 -0
- diffusers/loaders/lora_conversion_utils.py +284 -0
- diffusers/loaders/single_file.py +631 -0
- diffusers/loaders/textual_inversion.py +459 -0
- diffusers/loaders/unet.py +735 -0
- diffusers/loaders/utils.py +59 -0
- diffusers/models/__init__.py +12 -1
- diffusers/models/attention.py +165 -14
- diffusers/models/attention_flax.py +9 -1
- diffusers/models/attention_processor.py +286 -1
- diffusers/models/autoencoder_asym_kl.py +14 -9
- diffusers/models/autoencoder_kl.py +3 -18
- diffusers/models/autoencoder_kl_temporal_decoder.py +402 -0
- diffusers/models/autoencoder_tiny.py +20 -24
- diffusers/models/consistency_decoder_vae.py +37 -30
- diffusers/models/controlnet.py +59 -39
- diffusers/models/controlnet_flax.py +19 -18
- diffusers/models/embeddings_flax.py +2 -0
- diffusers/models/lora.py +131 -1
- diffusers/models/modeling_flax_utils.py +2 -1
- diffusers/models/modeling_outputs.py +17 -0
- diffusers/models/modeling_utils.py +27 -19
- diffusers/models/normalization.py +2 -2
- diffusers/models/resnet.py +390 -59
- diffusers/models/transformer_2d.py +20 -3
- diffusers/models/transformer_temporal.py +183 -1
- diffusers/models/unet_2d_blocks_flax.py +5 -0
- diffusers/models/unet_2d_condition.py +9 -0
- diffusers/models/unet_2d_condition_flax.py +13 -13
- diffusers/models/unet_3d_blocks.py +957 -173
- diffusers/models/unet_3d_condition.py +16 -8
- diffusers/models/unet_kandi3.py +589 -0
- diffusers/models/unet_motion_model.py +48 -33
- diffusers/models/unet_spatio_temporal_condition.py +489 -0
- diffusers/models/vae.py +63 -13
- diffusers/models/vae_flax.py +7 -0
- diffusers/models/vq_model.py +3 -1
- diffusers/optimization.py +16 -9
- diffusers/pipelines/__init__.py +65 -12
- diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion.py +93 -23
- diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion_img2img.py +97 -25
- diffusers/pipelines/animatediff/pipeline_animatediff.py +34 -4
- diffusers/pipelines/audioldm/pipeline_audioldm.py +1 -0
- diffusers/pipelines/auto_pipeline.py +6 -0
- diffusers/pipelines/consistency_models/pipeline_consistency_models.py +1 -0
- diffusers/pipelines/controlnet/pipeline_controlnet.py +217 -31
- diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +101 -32
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +136 -39
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +119 -37
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +196 -35
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +102 -31
- diffusers/pipelines/dance_diffusion/pipeline_dance_diffusion.py +1 -0
- diffusers/pipelines/ddim/pipeline_ddim.py +1 -0
- diffusers/pipelines/ddpm/pipeline_ddpm.py +1 -0
- diffusers/pipelines/deepfloyd_if/pipeline_if.py +13 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img.py +13 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img_superresolution.py +13 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.py +13 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting_superresolution.py +13 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.py +13 -1
- diffusers/pipelines/dit/pipeline_dit.py +1 -0
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py +3 -3
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpainting.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior.py +1 -1
- diffusers/pipelines/kandinsky3/__init__.py +49 -0
- diffusers/pipelines/kandinsky3/kandinsky3_pipeline.py +452 -0
- diffusers/pipelines/kandinsky3/kandinsky3img2img_pipeline.py +460 -0
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +65 -6
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +55 -3
- diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py +1 -0
- diffusers/pipelines/musicldm/pipeline_musicldm.py +1 -1
- diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py +7 -2
- diffusers/pipelines/pipeline_flax_utils.py +4 -2
- diffusers/pipelines/pipeline_utils.py +33 -13
- diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +196 -36
- diffusers/pipelines/score_sde_ve/pipeline_score_sde_ve.py +1 -0
- diffusers/pipelines/spectrogram_diffusion/pipeline_spectrogram_diffusion.py +1 -0
- diffusers/pipelines/stable_diffusion/__init__.py +64 -21
- diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +8 -3
- diffusers/pipelines/stable_diffusion/pipeline_cycle_diffusion.py +18 -2
- diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion.py +2 -2
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_img2img.py +2 -4
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_inpaint.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_inpaint_legacy.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +88 -9
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_attend_and_excite.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_depth2img.py +8 -3
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_diffedit.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_gligen.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_gligen_text_image.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_image_variation.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +92 -9
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +92 -9
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint_legacy.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +17 -13
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_k_diffusion.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_latent_upscale.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_ldm3d.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_model_editing.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_panorama.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_paradigms.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_pix2pix_zero.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +1 -0
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +103 -8
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +113 -8
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +115 -9
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +16 -12
- diffusers/pipelines/stable_video_diffusion/__init__.py +58 -0
- diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +649 -0
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_adapter.py +108 -12
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +109 -14
- diffusers/pipelines/text_to_video_synthesis/__init__.py +2 -0
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +1 -0
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +18 -3
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +4 -2
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +872 -0
- diffusers/pipelines/versatile_diffusion/modeling_text_unet.py +29 -40
- diffusers/pipelines/versatile_diffusion/pipeline_versatile_diffusion_dual_guided.py +1 -0
- diffusers/pipelines/versatile_diffusion/pipeline_versatile_diffusion_image_variation.py +1 -0
- diffusers/pipelines/versatile_diffusion/pipeline_versatile_diffusion_text_to_image.py +1 -0
- diffusers/pipelines/wuerstchen/modeling_wuerstchen_common.py +14 -4
- diffusers/pipelines/wuerstchen/modeling_wuerstchen_prior.py +9 -5
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen.py +1 -1
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen_combined.py +2 -2
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen_prior.py +1 -1
- diffusers/schedulers/__init__.py +2 -4
- diffusers/schedulers/deprecated/__init__.py +50 -0
- diffusers/schedulers/{scheduling_karras_ve.py → deprecated/scheduling_karras_ve.py} +4 -4
- diffusers/schedulers/{scheduling_sde_vp.py → deprecated/scheduling_sde_vp.py} +4 -6
- diffusers/schedulers/scheduling_ddim.py +1 -3
- diffusers/schedulers/scheduling_ddim_inverse.py +1 -3
- diffusers/schedulers/scheduling_ddim_parallel.py +1 -3
- diffusers/schedulers/scheduling_ddpm.py +1 -3
- diffusers/schedulers/scheduling_ddpm_parallel.py +1 -3
- diffusers/schedulers/scheduling_deis_multistep.py +15 -5
- diffusers/schedulers/scheduling_dpmsolver_multistep.py +15 -5
- diffusers/schedulers/scheduling_dpmsolver_multistep_inverse.py +15 -5
- diffusers/schedulers/scheduling_dpmsolver_sde.py +1 -3
- diffusers/schedulers/scheduling_dpmsolver_singlestep.py +15 -5
- diffusers/schedulers/scheduling_euler_ancestral_discrete.py +1 -3
- diffusers/schedulers/scheduling_euler_discrete.py +40 -13
- diffusers/schedulers/scheduling_heun_discrete.py +15 -5
- diffusers/schedulers/scheduling_k_dpm_2_ancestral_discrete.py +15 -5
- diffusers/schedulers/scheduling_k_dpm_2_discrete.py +15 -5
- diffusers/schedulers/scheduling_lcm.py +123 -29
- diffusers/schedulers/scheduling_lms_discrete.py +1 -3
- diffusers/schedulers/scheduling_pndm.py +1 -3
- diffusers/schedulers/scheduling_repaint.py +1 -3
- diffusers/schedulers/scheduling_unipc_multistep.py +15 -5
- diffusers/utils/__init__.py +1 -0
- diffusers/utils/constants.py +8 -7
- diffusers/utils/dummy_pt_objects.py +45 -0
- diffusers/utils/dummy_torch_and_transformers_objects.py +60 -0
- diffusers/utils/dynamic_modules_utils.py +4 -4
- diffusers/utils/export_utils.py +8 -3
- diffusers/utils/logging.py +10 -10
- diffusers/utils/outputs.py +5 -5
- diffusers/utils/peft_utils.py +88 -44
- diffusers/utils/torch_utils.py +2 -2
- {diffusers-0.23.1.dist-info → diffusers-0.24.0.dist-info}/METADATA +38 -22
- {diffusers-0.23.1.dist-info → diffusers-0.24.0.dist-info}/RECORD +175 -157
- diffusers/loaders.py +0 -3336
- {diffusers-0.23.1.dist-info → diffusers-0.24.0.dist-info}/LICENSE +0 -0
- {diffusers-0.23.1.dist-info → diffusers-0.24.0.dist-info}/WHEEL +0 -0
- {diffusers-0.23.1.dist-info → diffusers-0.24.0.dist-info}/entry_points.txt +0 -0
- {diffusers-0.23.1.dist-info → diffusers-0.24.0.dist-info}/top_level.txt +0 -0
@@ -20,12 +20,23 @@ import numpy as np
|
|
20
20
|
import PIL.Image
|
21
21
|
import torch
|
22
22
|
import torch.nn.functional as F
|
23
|
-
from transformers import
|
23
|
+
from transformers import (
|
24
|
+
CLIPImageProcessor,
|
25
|
+
CLIPTextModel,
|
26
|
+
CLIPTextModelWithProjection,
|
27
|
+
CLIPTokenizer,
|
28
|
+
CLIPVisionModelWithProjection,
|
29
|
+
)
|
24
30
|
|
25
31
|
from diffusers.utils.import_utils import is_invisible_watermark_available
|
26
32
|
|
27
33
|
from ...image_processor import PipelineImageInput, VaeImageProcessor
|
28
|
-
from ...loaders import
|
34
|
+
from ...loaders import (
|
35
|
+
FromSingleFileMixin,
|
36
|
+
IPAdapterMixin,
|
37
|
+
StableDiffusionXLLoraLoaderMixin,
|
38
|
+
TextualInversionLoaderMixin,
|
39
|
+
)
|
29
40
|
from ...models import AutoencoderKL, ControlNetModel, UNet2DConditionModel
|
30
41
|
from ...models.attention_processor import (
|
31
42
|
AttnProcessor2_0,
|
@@ -35,7 +46,14 @@ from ...models.attention_processor import (
|
|
35
46
|
)
|
36
47
|
from ...models.lora import adjust_lora_scale_text_encoder
|
37
48
|
from ...schedulers import KarrasDiffusionSchedulers
|
38
|
-
from ...utils import
|
49
|
+
from ...utils import (
|
50
|
+
USE_PEFT_BACKEND,
|
51
|
+
deprecate,
|
52
|
+
logging,
|
53
|
+
replace_example_docstring,
|
54
|
+
scale_lora_layers,
|
55
|
+
unscale_lora_layers,
|
56
|
+
)
|
39
57
|
from ...utils.torch_utils import is_compiled_module, is_torch_version, randn_tensor
|
40
58
|
from ..pipeline_utils import DiffusionPipeline
|
41
59
|
from ..stable_diffusion_xl.pipeline_output import StableDiffusionXLPipelineOutput
|
@@ -97,7 +115,11 @@ EXAMPLE_DOC_STRING = """
|
|
97
115
|
|
98
116
|
|
99
117
|
class StableDiffusionXLControlNetPipeline(
|
100
|
-
DiffusionPipeline,
|
118
|
+
DiffusionPipeline,
|
119
|
+
TextualInversionLoaderMixin,
|
120
|
+
StableDiffusionXLLoraLoaderMixin,
|
121
|
+
IPAdapterMixin,
|
122
|
+
FromSingleFileMixin,
|
101
123
|
):
|
102
124
|
r"""
|
103
125
|
Pipeline for text-to-image generation using Stable Diffusion XL with ControlNet guidance.
|
@@ -139,9 +161,18 @@ class StableDiffusionXLControlNetPipeline(
|
|
139
161
|
watermark output images. If not defined, it defaults to `True` if the package is installed; otherwise no
|
140
162
|
watermarker is used.
|
141
163
|
"""
|
164
|
+
|
142
165
|
# leave controlnet out on purpose because it iterates with unet
|
143
166
|
model_cpu_offload_seq = "text_encoder->text_encoder_2->unet->vae"
|
144
|
-
_optional_components = [
|
167
|
+
_optional_components = [
|
168
|
+
"tokenizer",
|
169
|
+
"tokenizer_2",
|
170
|
+
"text_encoder",
|
171
|
+
"text_encoder_2",
|
172
|
+
"feature_extractor",
|
173
|
+
"image_encoder",
|
174
|
+
]
|
175
|
+
_callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"]
|
145
176
|
|
146
177
|
def __init__(
|
147
178
|
self,
|
@@ -155,6 +186,8 @@ class StableDiffusionXLControlNetPipeline(
|
|
155
186
|
scheduler: KarrasDiffusionSchedulers,
|
156
187
|
force_zeros_for_empty_prompt: bool = True,
|
157
188
|
add_watermarker: Optional[bool] = None,
|
189
|
+
feature_extractor: CLIPImageProcessor = None,
|
190
|
+
image_encoder: CLIPVisionModelWithProjection = None,
|
158
191
|
):
|
159
192
|
super().__init__()
|
160
193
|
|
@@ -170,6 +203,8 @@ class StableDiffusionXLControlNetPipeline(
|
|
170
203
|
unet=unet,
|
171
204
|
controlnet=controlnet,
|
172
205
|
scheduler=scheduler,
|
206
|
+
feature_extractor=feature_extractor,
|
207
|
+
image_encoder=image_encoder,
|
173
208
|
)
|
174
209
|
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
|
175
210
|
self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor, do_convert_rgb=True)
|
@@ -453,6 +488,20 @@ class StableDiffusionXLControlNetPipeline(
|
|
453
488
|
|
454
489
|
return prompt_embeds, negative_prompt_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds
|
455
490
|
|
491
|
+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.encode_image
|
492
|
+
def encode_image(self, image, device, num_images_per_prompt):
|
493
|
+
dtype = next(self.image_encoder.parameters()).dtype
|
494
|
+
|
495
|
+
if not isinstance(image, torch.Tensor):
|
496
|
+
image = self.feature_extractor(image, return_tensors="pt").pixel_values
|
497
|
+
|
498
|
+
image = image.to(device=device, dtype=dtype)
|
499
|
+
image_embeds = self.image_encoder(image).image_embeds
|
500
|
+
image_embeds = image_embeds.repeat_interleave(num_images_per_prompt, dim=0)
|
501
|
+
|
502
|
+
uncond_image_embeds = torch.zeros_like(image_embeds)
|
503
|
+
return image_embeds, uncond_image_embeds
|
504
|
+
|
456
505
|
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_extra_step_kwargs
|
457
506
|
def prepare_extra_step_kwargs(self, generator, eta):
|
458
507
|
# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
|
@@ -486,15 +535,21 @@ class StableDiffusionXLControlNetPipeline(
|
|
486
535
|
controlnet_conditioning_scale=1.0,
|
487
536
|
control_guidance_start=0.0,
|
488
537
|
control_guidance_end=1.0,
|
538
|
+
callback_on_step_end_tensor_inputs=None,
|
489
539
|
):
|
490
|
-
if
|
491
|
-
callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0)
|
492
|
-
):
|
540
|
+
if callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0):
|
493
541
|
raise ValueError(
|
494
542
|
f"`callback_steps` has to be a positive integer but is {callback_steps} of type"
|
495
543
|
f" {type(callback_steps)}."
|
496
544
|
)
|
497
545
|
|
546
|
+
if callback_on_step_end_tensor_inputs is not None and not all(
|
547
|
+
k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs
|
548
|
+
):
|
549
|
+
raise ValueError(
|
550
|
+
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]}"
|
551
|
+
)
|
552
|
+
|
498
553
|
if prompt is not None and prompt_embeds is not None:
|
499
554
|
raise ValueError(
|
500
555
|
f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
|
@@ -791,6 +846,58 @@ class StableDiffusionXLControlNetPipeline(
|
|
791
846
|
"""Disables the FreeU mechanism if enabled."""
|
792
847
|
self.unet.disable_freeu()
|
793
848
|
|
849
|
+
# Copied from diffusers.pipelines.latent_consistency_models.pipeline_latent_consistency_text2img.LatentConsistencyModelPipeline.get_guidance_scale_embedding
|
850
|
+
def get_guidance_scale_embedding(self, w, embedding_dim=512, dtype=torch.float32):
|
851
|
+
"""
|
852
|
+
See https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298
|
853
|
+
|
854
|
+
Args:
|
855
|
+
timesteps (`torch.Tensor`):
|
856
|
+
generate embedding vectors at these timesteps
|
857
|
+
embedding_dim (`int`, *optional*, defaults to 512):
|
858
|
+
dimension of the embeddings to generate
|
859
|
+
dtype:
|
860
|
+
data type of the generated embeddings
|
861
|
+
|
862
|
+
Returns:
|
863
|
+
`torch.FloatTensor`: Embedding vectors with shape `(len(timesteps), embedding_dim)`
|
864
|
+
"""
|
865
|
+
assert len(w.shape) == 1
|
866
|
+
w = w * 1000.0
|
867
|
+
|
868
|
+
half_dim = embedding_dim // 2
|
869
|
+
emb = torch.log(torch.tensor(10000.0)) / (half_dim - 1)
|
870
|
+
emb = torch.exp(torch.arange(half_dim, dtype=dtype) * -emb)
|
871
|
+
emb = w.to(dtype)[:, None] * emb[None, :]
|
872
|
+
emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
|
873
|
+
if embedding_dim % 2 == 1: # zero pad
|
874
|
+
emb = torch.nn.functional.pad(emb, (0, 1))
|
875
|
+
assert emb.shape == (w.shape[0], embedding_dim)
|
876
|
+
return emb
|
877
|
+
|
878
|
+
@property
|
879
|
+
def guidance_scale(self):
|
880
|
+
return self._guidance_scale
|
881
|
+
|
882
|
+
@property
|
883
|
+
def clip_skip(self):
|
884
|
+
return self._clip_skip
|
885
|
+
|
886
|
+
# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
|
887
|
+
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
|
888
|
+
# corresponds to doing no classifier free guidance.
|
889
|
+
@property
|
890
|
+
def do_classifier_free_guidance(self):
|
891
|
+
return self._guidance_scale > 1 and self.unet.config.time_cond_proj_dim is None
|
892
|
+
|
893
|
+
@property
|
894
|
+
def cross_attention_kwargs(self):
|
895
|
+
return self._cross_attention_kwargs
|
896
|
+
|
897
|
+
@property
|
898
|
+
def num_timesteps(self):
|
899
|
+
return self._num_timesteps
|
900
|
+
|
794
901
|
@torch.no_grad()
|
795
902
|
@replace_example_docstring(EXAMPLE_DOC_STRING)
|
796
903
|
def __call__(
|
@@ -812,10 +919,9 @@ class StableDiffusionXLControlNetPipeline(
|
|
812
919
|
negative_prompt_embeds: Optional[torch.FloatTensor] = None,
|
813
920
|
pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
|
814
921
|
negative_pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
|
922
|
+
ip_adapter_image: Optional[PipelineImageInput] = None,
|
815
923
|
output_type: Optional[str] = "pil",
|
816
924
|
return_dict: bool = True,
|
817
|
-
callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
|
818
|
-
callback_steps: int = 1,
|
819
925
|
cross_attention_kwargs: Optional[Dict[str, Any]] = None,
|
820
926
|
controlnet_conditioning_scale: Union[float, List[float]] = 1.0,
|
821
927
|
guess_mode: bool = False,
|
@@ -828,6 +934,9 @@ class StableDiffusionXLControlNetPipeline(
|
|
828
934
|
negative_crops_coords_top_left: Tuple[int, int] = (0, 0),
|
829
935
|
negative_target_size: Optional[Tuple[int, int]] = None,
|
830
936
|
clip_skip: Optional[int] = None,
|
937
|
+
callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
|
938
|
+
callback_on_step_end_tensor_inputs: List[str] = ["latents"],
|
939
|
+
**kwargs,
|
831
940
|
):
|
832
941
|
r"""
|
833
942
|
The call function to the pipeline for generation.
|
@@ -891,17 +1000,12 @@ class StableDiffusionXLControlNetPipeline(
|
|
891
1000
|
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs (prompt
|
892
1001
|
weighting). If not provided, pooled `negative_prompt_embeds` are generated from `negative_prompt` input
|
893
1002
|
argument.
|
1003
|
+
ip_adapter_image: (`PipelineImageInput`, *optional*): Optional image input to work with IP Adapters.
|
894
1004
|
output_type (`str`, *optional*, defaults to `"pil"`):
|
895
1005
|
The output format of the generated image. Choose between `PIL.Image` or `np.array`.
|
896
1006
|
return_dict (`bool`, *optional*, defaults to `True`):
|
897
1007
|
Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
|
898
1008
|
plain tuple.
|
899
|
-
callback (`Callable`, *optional*):
|
900
|
-
A function that calls every `callback_steps` steps during inference. The function is called with the
|
901
|
-
following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
|
902
|
-
callback_steps (`int`, *optional*, defaults to 1):
|
903
|
-
The frequency at which the `callback` function is called. If not specified, the callback is called at
|
904
|
-
every step.
|
905
1009
|
cross_attention_kwargs (`dict`, *optional*):
|
906
1010
|
A kwargs dictionary that if specified is passed along to the [`AttentionProcessor`] as defined in
|
907
1011
|
[`self.processor`](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
|
@@ -948,6 +1052,15 @@ class StableDiffusionXLControlNetPipeline(
|
|
948
1052
|
clip_skip (`int`, *optional*):
|
949
1053
|
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
|
950
1054
|
the output of the pre-final layer will be used for computing the prompt embeddings.
|
1055
|
+
callback_on_step_end (`Callable`, *optional*):
|
1056
|
+
A function that calls at the end of each denoising steps during the inference. The function is called
|
1057
|
+
with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
|
1058
|
+
callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
|
1059
|
+
`callback_on_step_end_tensor_inputs`.
|
1060
|
+
callback_on_step_end_tensor_inputs (`List`, *optional*):
|
1061
|
+
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
|
1062
|
+
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
|
1063
|
+
`._callback_tensor_inputs` attribute of your pipeine class.
|
951
1064
|
|
952
1065
|
Examples:
|
953
1066
|
|
@@ -956,6 +1069,23 @@ class StableDiffusionXLControlNetPipeline(
|
|
956
1069
|
If `return_dict` is `True`, [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] is returned,
|
957
1070
|
otherwise a `tuple` is returned containing the output images.
|
958
1071
|
"""
|
1072
|
+
|
1073
|
+
callback = kwargs.pop("callback", None)
|
1074
|
+
callback_steps = kwargs.pop("callback_steps", None)
|
1075
|
+
|
1076
|
+
if callback is not None:
|
1077
|
+
deprecate(
|
1078
|
+
"callback",
|
1079
|
+
"1.0.0",
|
1080
|
+
"Passing `callback` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
|
1081
|
+
)
|
1082
|
+
if callback_steps is not None:
|
1083
|
+
deprecate(
|
1084
|
+
"callback_steps",
|
1085
|
+
"1.0.0",
|
1086
|
+
"Passing `callback_steps` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
|
1087
|
+
)
|
1088
|
+
|
959
1089
|
controlnet = self.controlnet._orig_mod if is_compiled_module(self.controlnet) else self.controlnet
|
960
1090
|
|
961
1091
|
# align format for control guidance
|
@@ -965,9 +1095,10 @@ class StableDiffusionXLControlNetPipeline(
|
|
965
1095
|
control_guidance_end = len(control_guidance_start) * [control_guidance_end]
|
966
1096
|
elif not isinstance(control_guidance_start, list) and not isinstance(control_guidance_end, list):
|
967
1097
|
mult = len(controlnet.nets) if isinstance(controlnet, MultiControlNetModel) else 1
|
968
|
-
control_guidance_start, control_guidance_end =
|
969
|
-
|
970
|
-
|
1098
|
+
control_guidance_start, control_guidance_end = (
|
1099
|
+
mult * [control_guidance_start],
|
1100
|
+
mult * [control_guidance_end],
|
1101
|
+
)
|
971
1102
|
|
972
1103
|
# 1. Check inputs. Raise error if not correct
|
973
1104
|
self.check_inputs(
|
@@ -984,8 +1115,13 @@ class StableDiffusionXLControlNetPipeline(
|
|
984
1115
|
controlnet_conditioning_scale,
|
985
1116
|
control_guidance_start,
|
986
1117
|
control_guidance_end,
|
1118
|
+
callback_on_step_end_tensor_inputs,
|
987
1119
|
)
|
988
1120
|
|
1121
|
+
self._guidance_scale = guidance_scale
|
1122
|
+
self._clip_skip = clip_skip
|
1123
|
+
self._cross_attention_kwargs = cross_attention_kwargs
|
1124
|
+
|
989
1125
|
# 2. Define call parameters
|
990
1126
|
if prompt is not None and isinstance(prompt, str):
|
991
1127
|
batch_size = 1
|
@@ -995,10 +1131,6 @@ class StableDiffusionXLControlNetPipeline(
|
|
995
1131
|
batch_size = prompt_embeds.shape[0]
|
996
1132
|
|
997
1133
|
device = self._execution_device
|
998
|
-
# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
|
999
|
-
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
|
1000
|
-
# corresponds to doing no classifier free guidance.
|
1001
|
-
do_classifier_free_guidance = guidance_scale > 1.0
|
1002
1134
|
|
1003
1135
|
if isinstance(controlnet, MultiControlNetModel) and isinstance(controlnet_conditioning_scale, float):
|
1004
1136
|
controlnet_conditioning_scale = [controlnet_conditioning_scale] * len(controlnet.nets)
|
@@ -1010,9 +1142,9 @@ class StableDiffusionXLControlNetPipeline(
|
|
1010
1142
|
)
|
1011
1143
|
guess_mode = guess_mode or global_pool_conditions
|
1012
1144
|
|
1013
|
-
# 3. Encode input prompt
|
1145
|
+
# 3.1 Encode input prompt
|
1014
1146
|
text_encoder_lora_scale = (
|
1015
|
-
cross_attention_kwargs.get("scale", None) if cross_attention_kwargs is not None else None
|
1147
|
+
self.cross_attention_kwargs.get("scale", None) if self.cross_attention_kwargs is not None else None
|
1016
1148
|
)
|
1017
1149
|
(
|
1018
1150
|
prompt_embeds,
|
@@ -1024,7 +1156,7 @@ class StableDiffusionXLControlNetPipeline(
|
|
1024
1156
|
prompt_2,
|
1025
1157
|
device,
|
1026
1158
|
num_images_per_prompt,
|
1027
|
-
do_classifier_free_guidance,
|
1159
|
+
self.do_classifier_free_guidance,
|
1028
1160
|
negative_prompt,
|
1029
1161
|
negative_prompt_2,
|
1030
1162
|
prompt_embeds=prompt_embeds,
|
@@ -1032,9 +1164,15 @@ class StableDiffusionXLControlNetPipeline(
|
|
1032
1164
|
pooled_prompt_embeds=pooled_prompt_embeds,
|
1033
1165
|
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
|
1034
1166
|
lora_scale=text_encoder_lora_scale,
|
1035
|
-
clip_skip=clip_skip,
|
1167
|
+
clip_skip=self.clip_skip,
|
1036
1168
|
)
|
1037
1169
|
|
1170
|
+
# 3.2 Encode ip_adapter_image
|
1171
|
+
if ip_adapter_image is not None:
|
1172
|
+
image_embeds, negative_image_embeds = self.encode_image(ip_adapter_image, device, num_images_per_prompt)
|
1173
|
+
if self.do_classifier_free_guidance:
|
1174
|
+
image_embeds = torch.cat([negative_image_embeds, image_embeds])
|
1175
|
+
|
1038
1176
|
# 4. Prepare image
|
1039
1177
|
if isinstance(controlnet, ControlNetModel):
|
1040
1178
|
image = self.prepare_image(
|
@@ -1045,7 +1183,7 @@ class StableDiffusionXLControlNetPipeline(
|
|
1045
1183
|
num_images_per_prompt=num_images_per_prompt,
|
1046
1184
|
device=device,
|
1047
1185
|
dtype=controlnet.dtype,
|
1048
|
-
do_classifier_free_guidance=do_classifier_free_guidance,
|
1186
|
+
do_classifier_free_guidance=self.do_classifier_free_guidance,
|
1049
1187
|
guess_mode=guess_mode,
|
1050
1188
|
)
|
1051
1189
|
height, width = image.shape[-2:]
|
@@ -1061,7 +1199,7 @@ class StableDiffusionXLControlNetPipeline(
|
|
1061
1199
|
num_images_per_prompt=num_images_per_prompt,
|
1062
1200
|
device=device,
|
1063
1201
|
dtype=controlnet.dtype,
|
1064
|
-
do_classifier_free_guidance=do_classifier_free_guidance,
|
1202
|
+
do_classifier_free_guidance=self.do_classifier_free_guidance,
|
1065
1203
|
guess_mode=guess_mode,
|
1066
1204
|
)
|
1067
1205
|
|
@@ -1075,6 +1213,7 @@ class StableDiffusionXLControlNetPipeline(
|
|
1075
1213
|
# 5. Prepare timesteps
|
1076
1214
|
self.scheduler.set_timesteps(num_inference_steps, device=device)
|
1077
1215
|
timesteps = self.scheduler.timesteps
|
1216
|
+
self._num_timesteps = len(timesteps)
|
1078
1217
|
|
1079
1218
|
# 6. Prepare latent variables
|
1080
1219
|
num_channels_latents = self.unet.config.in_channels
|
@@ -1089,6 +1228,14 @@ class StableDiffusionXLControlNetPipeline(
|
|
1089
1228
|
latents,
|
1090
1229
|
)
|
1091
1230
|
|
1231
|
+
# 6.5 Optionally get Guidance Scale Embedding
|
1232
|
+
timestep_cond = None
|
1233
|
+
if self.unet.config.time_cond_proj_dim is not None:
|
1234
|
+
guidance_scale_tensor = torch.tensor(self.guidance_scale - 1).repeat(batch_size * num_images_per_prompt)
|
1235
|
+
timestep_cond = self.get_guidance_scale_embedding(
|
1236
|
+
guidance_scale_tensor, embedding_dim=self.unet.config.time_cond_proj_dim
|
1237
|
+
).to(device=device, dtype=latents.dtype)
|
1238
|
+
|
1092
1239
|
# 7. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
|
1093
1240
|
extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
|
1094
1241
|
|
@@ -1133,7 +1280,7 @@ class StableDiffusionXLControlNetPipeline(
|
|
1133
1280
|
else:
|
1134
1281
|
negative_add_time_ids = add_time_ids
|
1135
1282
|
|
1136
|
-
if do_classifier_free_guidance:
|
1283
|
+
if self.do_classifier_free_guidance:
|
1137
1284
|
prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0)
|
1138
1285
|
add_text_embeds = torch.cat([negative_pooled_prompt_embeds, add_text_embeds], dim=0)
|
1139
1286
|
add_time_ids = torch.cat([negative_add_time_ids, add_time_ids], dim=0)
|
@@ -1154,13 +1301,13 @@ class StableDiffusionXLControlNetPipeline(
|
|
1154
1301
|
if (is_unet_compiled and is_controlnet_compiled) and is_torch_higher_equal_2_1:
|
1155
1302
|
torch._inductor.cudagraph_mark_step_begin()
|
1156
1303
|
# expand the latents if we are doing classifier free guidance
|
1157
|
-
latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
|
1304
|
+
latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance else latents
|
1158
1305
|
latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
|
1159
1306
|
|
1160
1307
|
added_cond_kwargs = {"text_embeds": add_text_embeds, "time_ids": add_time_ids}
|
1161
1308
|
|
1162
1309
|
# controlnet(s) inference
|
1163
|
-
if guess_mode and do_classifier_free_guidance:
|
1310
|
+
if guess_mode and self.do_classifier_free_guidance:
|
1164
1311
|
# Infer ControlNet only for the conditional batch.
|
1165
1312
|
control_model_input = latents
|
1166
1313
|
control_model_input = self.scheduler.scale_model_input(control_model_input, t)
|
@@ -1193,19 +1340,23 @@ class StableDiffusionXLControlNetPipeline(
|
|
1193
1340
|
return_dict=False,
|
1194
1341
|
)
|
1195
1342
|
|
1196
|
-
if guess_mode and do_classifier_free_guidance:
|
1343
|
+
if guess_mode and self.do_classifier_free_guidance:
|
1197
1344
|
# Infered ControlNet only for the conditional batch.
|
1198
1345
|
# To apply the output of ControlNet to both the unconditional and conditional batches,
|
1199
1346
|
# add 0 to the unconditional batch to keep it unchanged.
|
1200
1347
|
down_block_res_samples = [torch.cat([torch.zeros_like(d), d]) for d in down_block_res_samples]
|
1201
1348
|
mid_block_res_sample = torch.cat([torch.zeros_like(mid_block_res_sample), mid_block_res_sample])
|
1202
1349
|
|
1350
|
+
if ip_adapter_image is not None:
|
1351
|
+
added_cond_kwargs["image_embeds"] = image_embeds
|
1352
|
+
|
1203
1353
|
# predict the noise residual
|
1204
1354
|
noise_pred = self.unet(
|
1205
1355
|
latent_model_input,
|
1206
1356
|
t,
|
1207
1357
|
encoder_hidden_states=prompt_embeds,
|
1208
|
-
|
1358
|
+
timestep_cond=timestep_cond,
|
1359
|
+
cross_attention_kwargs=self.cross_attention_kwargs,
|
1209
1360
|
down_block_additional_residuals=down_block_res_samples,
|
1210
1361
|
mid_block_additional_residual=mid_block_res_sample,
|
1211
1362
|
added_cond_kwargs=added_cond_kwargs,
|
@@ -1213,13 +1364,23 @@ class StableDiffusionXLControlNetPipeline(
|
|
1213
1364
|
)[0]
|
1214
1365
|
|
1215
1366
|
# perform guidance
|
1216
|
-
if do_classifier_free_guidance:
|
1367
|
+
if self.do_classifier_free_guidance:
|
1217
1368
|
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
|
1218
1369
|
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
|
1219
1370
|
|
1220
1371
|
# compute the previous noisy sample x_t -> x_t-1
|
1221
1372
|
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
|
1222
1373
|
|
1374
|
+
if callback_on_step_end is not None:
|
1375
|
+
callback_kwargs = {}
|
1376
|
+
for k in callback_on_step_end_tensor_inputs:
|
1377
|
+
callback_kwargs[k] = locals()[k]
|
1378
|
+
callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
|
1379
|
+
|
1380
|
+
latents = callback_outputs.pop("latents", latents)
|
1381
|
+
prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
|
1382
|
+
negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds)
|
1383
|
+
|
1223
1384
|
# call the callback, if provided
|
1224
1385
|
if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
|
1225
1386
|
progress_bar.update()
|