diffusers 0.23.1__py3-none-any.whl → 0.25.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (238) hide show
  1. diffusers/__init__.py +26 -2
  2. diffusers/commands/fp16_safetensors.py +10 -11
  3. diffusers/configuration_utils.py +13 -8
  4. diffusers/dependency_versions_check.py +0 -1
  5. diffusers/dependency_versions_table.py +5 -5
  6. diffusers/experimental/rl/value_guided_sampling.py +1 -1
  7. diffusers/image_processor.py +463 -51
  8. diffusers/loaders/__init__.py +82 -0
  9. diffusers/loaders/ip_adapter.py +159 -0
  10. diffusers/loaders/lora.py +1553 -0
  11. diffusers/loaders/lora_conversion_utils.py +284 -0
  12. diffusers/loaders/single_file.py +637 -0
  13. diffusers/loaders/textual_inversion.py +455 -0
  14. diffusers/loaders/unet.py +828 -0
  15. diffusers/loaders/utils.py +59 -0
  16. diffusers/models/__init__.py +26 -9
  17. diffusers/models/activations.py +9 -6
  18. diffusers/models/attention.py +301 -29
  19. diffusers/models/attention_flax.py +9 -1
  20. diffusers/models/attention_processor.py +378 -6
  21. diffusers/models/autoencoders/__init__.py +5 -0
  22. diffusers/models/{autoencoder_asym_kl.py → autoencoders/autoencoder_asym_kl.py} +17 -12
  23. diffusers/models/{autoencoder_kl.py → autoencoders/autoencoder_kl.py} +47 -23
  24. diffusers/models/autoencoders/autoencoder_kl_temporal_decoder.py +402 -0
  25. diffusers/models/{autoencoder_tiny.py → autoencoders/autoencoder_tiny.py} +24 -28
  26. diffusers/models/{consistency_decoder_vae.py → autoencoders/consistency_decoder_vae.py} +51 -44
  27. diffusers/models/{vae.py → autoencoders/vae.py} +71 -17
  28. diffusers/models/controlnet.py +59 -39
  29. diffusers/models/controlnet_flax.py +19 -18
  30. diffusers/models/downsampling.py +338 -0
  31. diffusers/models/embeddings.py +112 -29
  32. diffusers/models/embeddings_flax.py +2 -0
  33. diffusers/models/lora.py +131 -1
  34. diffusers/models/modeling_flax_utils.py +14 -8
  35. diffusers/models/modeling_outputs.py +17 -0
  36. diffusers/models/modeling_utils.py +37 -29
  37. diffusers/models/normalization.py +110 -4
  38. diffusers/models/resnet.py +299 -652
  39. diffusers/models/transformer_2d.py +22 -5
  40. diffusers/models/transformer_temporal.py +183 -1
  41. diffusers/models/unet_2d_blocks_flax.py +5 -0
  42. diffusers/models/unet_2d_condition.py +46 -0
  43. diffusers/models/unet_2d_condition_flax.py +13 -13
  44. diffusers/models/unet_3d_blocks.py +957 -173
  45. diffusers/models/unet_3d_condition.py +16 -8
  46. diffusers/models/unet_kandinsky3.py +535 -0
  47. diffusers/models/unet_motion_model.py +48 -33
  48. diffusers/models/unet_spatio_temporal_condition.py +489 -0
  49. diffusers/models/upsampling.py +454 -0
  50. diffusers/models/uvit_2d.py +471 -0
  51. diffusers/models/vae_flax.py +7 -0
  52. diffusers/models/vq_model.py +12 -3
  53. diffusers/optimization.py +16 -9
  54. diffusers/pipelines/__init__.py +137 -76
  55. diffusers/pipelines/amused/__init__.py +62 -0
  56. diffusers/pipelines/amused/pipeline_amused.py +328 -0
  57. diffusers/pipelines/amused/pipeline_amused_img2img.py +347 -0
  58. diffusers/pipelines/amused/pipeline_amused_inpaint.py +378 -0
  59. diffusers/pipelines/animatediff/pipeline_animatediff.py +66 -8
  60. diffusers/pipelines/audioldm/pipeline_audioldm.py +1 -0
  61. diffusers/pipelines/auto_pipeline.py +23 -13
  62. diffusers/pipelines/consistency_models/pipeline_consistency_models.py +1 -0
  63. diffusers/pipelines/controlnet/pipeline_controlnet.py +238 -35
  64. diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +148 -37
  65. diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +155 -41
  66. diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +123 -43
  67. diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +216 -39
  68. diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +106 -34
  69. diffusers/pipelines/dance_diffusion/pipeline_dance_diffusion.py +1 -0
  70. diffusers/pipelines/ddim/pipeline_ddim.py +1 -0
  71. diffusers/pipelines/ddpm/pipeline_ddpm.py +1 -0
  72. diffusers/pipelines/deepfloyd_if/pipeline_if.py +13 -1
  73. diffusers/pipelines/deepfloyd_if/pipeline_if_img2img.py +13 -1
  74. diffusers/pipelines/deepfloyd_if/pipeline_if_img2img_superresolution.py +13 -1
  75. diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.py +13 -1
  76. diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting_superresolution.py +13 -1
  77. diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.py +13 -1
  78. diffusers/pipelines/deprecated/__init__.py +153 -0
  79. diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/__init__.py +3 -3
  80. diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/pipeline_alt_diffusion.py +177 -34
  81. diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/pipeline_alt_diffusion_img2img.py +182 -37
  82. diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/pipeline_output.py +1 -1
  83. diffusers/pipelines/{audio_diffusion → deprecated/audio_diffusion}/__init__.py +1 -1
  84. diffusers/pipelines/{audio_diffusion → deprecated/audio_diffusion}/mel.py +2 -2
  85. diffusers/pipelines/{audio_diffusion → deprecated/audio_diffusion}/pipeline_audio_diffusion.py +4 -4
  86. diffusers/pipelines/{latent_diffusion_uncond → deprecated/latent_diffusion_uncond}/__init__.py +1 -1
  87. diffusers/pipelines/{latent_diffusion_uncond → deprecated/latent_diffusion_uncond}/pipeline_latent_diffusion_uncond.py +4 -4
  88. diffusers/pipelines/{pndm → deprecated/pndm}/__init__.py +1 -1
  89. diffusers/pipelines/{pndm → deprecated/pndm}/pipeline_pndm.py +4 -4
  90. diffusers/pipelines/{repaint → deprecated/repaint}/__init__.py +1 -1
  91. diffusers/pipelines/{repaint → deprecated/repaint}/pipeline_repaint.py +5 -5
  92. diffusers/pipelines/{score_sde_ve → deprecated/score_sde_ve}/__init__.py +1 -1
  93. diffusers/pipelines/{score_sde_ve → deprecated/score_sde_ve}/pipeline_score_sde_ve.py +5 -4
  94. diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/__init__.py +6 -6
  95. diffusers/pipelines/{spectrogram_diffusion/continous_encoder.py → deprecated/spectrogram_diffusion/continuous_encoder.py} +2 -2
  96. diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/midi_utils.py +1 -1
  97. diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/notes_encoder.py +2 -2
  98. diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/pipeline_spectrogram_diffusion.py +8 -7
  99. diffusers/pipelines/deprecated/stable_diffusion_variants/__init__.py +55 -0
  100. diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_cycle_diffusion.py +34 -13
  101. diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_onnx_stable_diffusion_inpaint_legacy.py +7 -6
  102. diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_inpaint_legacy.py +12 -11
  103. diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_model_editing.py +17 -11
  104. diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_paradigms.py +11 -10
  105. diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_pix2pix_zero.py +14 -13
  106. diffusers/pipelines/{stochastic_karras_ve → deprecated/stochastic_karras_ve}/__init__.py +1 -1
  107. diffusers/pipelines/{stochastic_karras_ve → deprecated/stochastic_karras_ve}/pipeline_stochastic_karras_ve.py +4 -4
  108. diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/__init__.py +3 -3
  109. diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/modeling_text_unet.py +83 -51
  110. diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion.py +4 -4
  111. diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion_dual_guided.py +7 -6
  112. diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion_image_variation.py +7 -6
  113. diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion_text_to_image.py +7 -6
  114. diffusers/pipelines/{vq_diffusion → deprecated/vq_diffusion}/__init__.py +3 -3
  115. diffusers/pipelines/{vq_diffusion → deprecated/vq_diffusion}/pipeline_vq_diffusion.py +5 -5
  116. diffusers/pipelines/dit/pipeline_dit.py +1 -0
  117. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py +1 -1
  118. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py +3 -3
  119. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py +1 -1
  120. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpainting.py +1 -1
  121. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior.py +1 -1
  122. diffusers/pipelines/kandinsky3/__init__.py +49 -0
  123. diffusers/pipelines/kandinsky3/convert_kandinsky3_unet.py +98 -0
  124. diffusers/pipelines/kandinsky3/pipeline_kandinsky3.py +589 -0
  125. diffusers/pipelines/kandinsky3/pipeline_kandinsky3_img2img.py +654 -0
  126. diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +111 -11
  127. diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +102 -9
  128. diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py +1 -0
  129. diffusers/pipelines/musicldm/pipeline_musicldm.py +1 -1
  130. diffusers/pipelines/onnx_utils.py +8 -5
  131. diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py +7 -2
  132. diffusers/pipelines/pipeline_flax_utils.py +11 -8
  133. diffusers/pipelines/pipeline_utils.py +63 -42
  134. diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +247 -38
  135. diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py +3 -3
  136. diffusers/pipelines/stable_diffusion/__init__.py +37 -65
  137. diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +75 -78
  138. diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion.py +2 -2
  139. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_img2img.py +2 -4
  140. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_inpaint.py +1 -0
  141. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +174 -11
  142. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_depth2img.py +8 -3
  143. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_image_variation.py +1 -0
  144. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +178 -11
  145. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +224 -13
  146. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +74 -20
  147. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_latent_upscale.py +4 -0
  148. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +7 -0
  149. diffusers/pipelines/stable_diffusion/pipeline_stable_unclip.py +5 -0
  150. diffusers/pipelines/stable_diffusion/pipeline_stable_unclip_img2img.py +5 -0
  151. diffusers/pipelines/stable_diffusion_attend_and_excite/__init__.py +48 -0
  152. diffusers/pipelines/{stable_diffusion → stable_diffusion_attend_and_excite}/pipeline_stable_diffusion_attend_and_excite.py +6 -2
  153. diffusers/pipelines/stable_diffusion_diffedit/__init__.py +48 -0
  154. diffusers/pipelines/{stable_diffusion → stable_diffusion_diffedit}/pipeline_stable_diffusion_diffedit.py +3 -3
  155. diffusers/pipelines/stable_diffusion_gligen/__init__.py +50 -0
  156. diffusers/pipelines/{stable_diffusion → stable_diffusion_gligen}/pipeline_stable_diffusion_gligen.py +3 -2
  157. diffusers/pipelines/{stable_diffusion → stable_diffusion_gligen}/pipeline_stable_diffusion_gligen_text_image.py +4 -3
  158. diffusers/pipelines/stable_diffusion_k_diffusion/__init__.py +60 -0
  159. diffusers/pipelines/{stable_diffusion → stable_diffusion_k_diffusion}/pipeline_stable_diffusion_k_diffusion.py +7 -1
  160. diffusers/pipelines/stable_diffusion_ldm3d/__init__.py +48 -0
  161. diffusers/pipelines/{stable_diffusion → stable_diffusion_ldm3d}/pipeline_stable_diffusion_ldm3d.py +51 -7
  162. diffusers/pipelines/stable_diffusion_panorama/__init__.py +48 -0
  163. diffusers/pipelines/{stable_diffusion → stable_diffusion_panorama}/pipeline_stable_diffusion_panorama.py +57 -8
  164. diffusers/pipelines/stable_diffusion_safe/pipeline_stable_diffusion_safe.py +58 -6
  165. diffusers/pipelines/stable_diffusion_sag/__init__.py +48 -0
  166. diffusers/pipelines/{stable_diffusion → stable_diffusion_sag}/pipeline_stable_diffusion_sag.py +68 -10
  167. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +194 -17
  168. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +205 -16
  169. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +206 -17
  170. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +23 -17
  171. diffusers/pipelines/stable_video_diffusion/__init__.py +58 -0
  172. diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +652 -0
  173. diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_adapter.py +108 -12
  174. diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +115 -14
  175. diffusers/pipelines/text_to_video_synthesis/__init__.py +2 -0
  176. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +6 -0
  177. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +23 -3
  178. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +334 -10
  179. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +1331 -0
  180. diffusers/pipelines/unclip/pipeline_unclip.py +2 -1
  181. diffusers/pipelines/unclip/pipeline_unclip_image_variation.py +1 -0
  182. diffusers/pipelines/wuerstchen/modeling_paella_vq_model.py +1 -1
  183. diffusers/pipelines/wuerstchen/modeling_wuerstchen_common.py +14 -4
  184. diffusers/pipelines/wuerstchen/modeling_wuerstchen_prior.py +9 -5
  185. diffusers/pipelines/wuerstchen/pipeline_wuerstchen.py +1 -1
  186. diffusers/pipelines/wuerstchen/pipeline_wuerstchen_combined.py +2 -2
  187. diffusers/pipelines/wuerstchen/pipeline_wuerstchen_prior.py +5 -1
  188. diffusers/schedulers/__init__.py +4 -4
  189. diffusers/schedulers/deprecated/__init__.py +50 -0
  190. diffusers/schedulers/{scheduling_karras_ve.py → deprecated/scheduling_karras_ve.py} +4 -4
  191. diffusers/schedulers/{scheduling_sde_vp.py → deprecated/scheduling_sde_vp.py} +4 -6
  192. diffusers/schedulers/scheduling_amused.py +162 -0
  193. diffusers/schedulers/scheduling_consistency_models.py +2 -0
  194. diffusers/schedulers/scheduling_ddim.py +1 -3
  195. diffusers/schedulers/scheduling_ddim_inverse.py +2 -7
  196. diffusers/schedulers/scheduling_ddim_parallel.py +1 -3
  197. diffusers/schedulers/scheduling_ddpm.py +47 -3
  198. diffusers/schedulers/scheduling_ddpm_parallel.py +47 -3
  199. diffusers/schedulers/scheduling_deis_multistep.py +28 -6
  200. diffusers/schedulers/scheduling_dpmsolver_multistep.py +28 -6
  201. diffusers/schedulers/scheduling_dpmsolver_multistep_inverse.py +28 -6
  202. diffusers/schedulers/scheduling_dpmsolver_sde.py +3 -3
  203. diffusers/schedulers/scheduling_dpmsolver_singlestep.py +28 -6
  204. diffusers/schedulers/scheduling_euler_ancestral_discrete.py +59 -3
  205. diffusers/schedulers/scheduling_euler_discrete.py +102 -16
  206. diffusers/schedulers/scheduling_heun_discrete.py +17 -5
  207. diffusers/schedulers/scheduling_k_dpm_2_ancestral_discrete.py +17 -5
  208. diffusers/schedulers/scheduling_k_dpm_2_discrete.py +17 -5
  209. diffusers/schedulers/scheduling_lcm.py +123 -29
  210. diffusers/schedulers/scheduling_lms_discrete.py +3 -3
  211. diffusers/schedulers/scheduling_pndm.py +1 -3
  212. diffusers/schedulers/scheduling_repaint.py +1 -3
  213. diffusers/schedulers/scheduling_unipc_multistep.py +28 -6
  214. diffusers/schedulers/scheduling_utils.py +3 -1
  215. diffusers/schedulers/scheduling_utils_flax.py +3 -1
  216. diffusers/training_utils.py +1 -1
  217. diffusers/utils/__init__.py +1 -2
  218. diffusers/utils/constants.py +10 -12
  219. diffusers/utils/dummy_pt_objects.py +75 -0
  220. diffusers/utils/dummy_torch_and_transformers_objects.py +105 -0
  221. diffusers/utils/dynamic_modules_utils.py +18 -22
  222. diffusers/utils/export_utils.py +8 -3
  223. diffusers/utils/hub_utils.py +24 -36
  224. diffusers/utils/logging.py +11 -11
  225. diffusers/utils/outputs.py +5 -5
  226. diffusers/utils/peft_utils.py +88 -44
  227. diffusers/utils/state_dict_utils.py +8 -0
  228. diffusers/utils/testing_utils.py +199 -1
  229. diffusers/utils/torch_utils.py +4 -4
  230. {diffusers-0.23.1.dist-info → diffusers-0.25.0.dist-info}/METADATA +86 -69
  231. diffusers-0.25.0.dist-info/RECORD +360 -0
  232. {diffusers-0.23.1.dist-info → diffusers-0.25.0.dist-info}/WHEEL +1 -1
  233. {diffusers-0.23.1.dist-info → diffusers-0.25.0.dist-info}/entry_points.txt +0 -1
  234. diffusers/loaders.py +0 -3336
  235. diffusers-0.23.1.dist-info/RECORD +0 -323
  236. /diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/modeling_roberta_series.py +0 -0
  237. {diffusers-0.23.1.dist-info → diffusers-0.25.0.dist-info}/LICENSE +0 -0
  238. {diffusers-0.23.1.dist-info → diffusers-0.25.0.dist-info}/top_level.txt +0 -0
@@ -34,6 +34,7 @@ from ...models.lora import adjust_lora_scale_text_encoder
34
34
  from ...schedulers import KarrasDiffusionSchedulers
35
35
  from ...utils import (
36
36
  USE_PEFT_BACKEND,
37
+ deprecate,
37
38
  is_invisible_watermark_available,
38
39
  logging,
39
40
  replace_example_docstring,
@@ -53,6 +54,20 @@ if is_invisible_watermark_available():
53
54
  logger = logging.get_logger(__name__) # pylint: disable=invalid-name
54
55
 
55
56
 
57
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents
58
+ def retrieve_latents(
59
+ encoder_output: torch.Tensor, generator: Optional[torch.Generator] = None, sample_mode: str = "sample"
60
+ ):
61
+ if hasattr(encoder_output, "latent_dist") and sample_mode == "sample":
62
+ return encoder_output.latent_dist.sample(generator)
63
+ elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax":
64
+ return encoder_output.latent_dist.mode()
65
+ elif hasattr(encoder_output, "latents"):
66
+ return encoder_output.latents
67
+ else:
68
+ raise AttributeError("Could not access latents of provided encoder_output")
69
+
70
+
56
71
  EXAMPLE_DOC_STRING = """
57
72
  Examples:
58
73
  ```py
@@ -133,12 +148,10 @@ class StableDiffusionXLControlNetInpaintPipeline(
133
148
  This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods the
134
149
  library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
135
150
 
136
- In addition the pipeline inherits the following loading methods:
137
- - *LoRA*: [`loaders.StableDiffusionXLLoraLoaderMixin.load_lora_weights`]
138
- - *Ckpt*: [`loaders.FromSingleFileMixin.from_single_file`]
139
-
140
- as well as the following saving methods:
141
- - *LoRA*: [`loaders.StableDiffusionXLLoraLoaderMixin.save_lora_weights`]
151
+ The pipeline also inherits the following loading methods:
152
+ - [`~loaders.StableDiffusionXLLoraLoaderMixin.load_lora_weights`] for loading LoRA weights
153
+ - [`~loaders.StableDiffusionXLLoraLoaderMixin.save_lora_weights`] for saving LoRA weights
154
+ - [`~loaders.FromSingleFileMixin.from_single_file`] for loading `.ckpt` files
142
155
 
143
156
  Args:
144
157
  vae ([`AutoencoderKL`]):
@@ -164,8 +177,10 @@ class StableDiffusionXLControlNetInpaintPipeline(
164
177
  A scheduler to be used in combination with `unet` to denoise the encoded image latents. Can be one of
165
178
  [`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`].
166
179
  """
180
+
167
181
  model_cpu_offload_seq = "text_encoder->text_encoder_2->unet->vae"
168
182
  _optional_components = ["tokenizer", "tokenizer_2", "text_encoder", "text_encoder_2"]
183
+ _callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"]
169
184
 
170
185
  def __init__(
171
186
  self,
@@ -554,6 +569,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
554
569
  controlnet_conditioning_scale=1.0,
555
570
  control_guidance_start=0.0,
556
571
  control_guidance_end=1.0,
572
+ callback_on_step_end_tensor_inputs=None,
557
573
  ):
558
574
  if strength < 0 or strength > 1:
559
575
  raise ValueError(f"The value of strength should in [0.0, 1.0] but is {strength}")
@@ -564,14 +580,20 @@ class StableDiffusionXLControlNetInpaintPipeline(
564
580
  f"`num_inference_steps` has to be a positive integer but is {num_inference_steps} of type"
565
581
  f" {type(num_inference_steps)}."
566
582
  )
567
- if (callback_steps is None) or (
568
- callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0)
569
- ):
583
+
584
+ if callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0):
570
585
  raise ValueError(
571
586
  f"`callback_steps` has to be a positive integer but is {callback_steps} of type"
572
587
  f" {type(callback_steps)}."
573
588
  )
574
589
 
590
+ if callback_on_step_end_tensor_inputs is not None and not all(
591
+ k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs
592
+ ):
593
+ raise ValueError(
594
+ 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]}"
595
+ )
596
+
575
597
  if prompt is not None and prompt_embeds is not None:
576
598
  raise ValueError(
577
599
  f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
@@ -814,12 +836,12 @@ class StableDiffusionXLControlNetInpaintPipeline(
814
836
 
815
837
  if isinstance(generator, list):
816
838
  image_latents = [
817
- self.vae.encode(image[i : i + 1]).latent_dist.sample(generator=generator[i])
839
+ retrieve_latents(self.vae.encode(image[i : i + 1]), generator=generator[i])
818
840
  for i in range(image.shape[0])
819
841
  ]
820
842
  image_latents = torch.cat(image_latents, dim=0)
821
843
  else:
822
- image_latents = self.vae.encode(image).latent_dist.sample(generator=generator)
844
+ image_latents = retrieve_latents(self.vae.encode(image), generator=generator)
823
845
 
824
846
  if self.vae.config.force_upcast:
825
847
  self.vae.to(dtype)
@@ -1007,6 +1029,29 @@ class StableDiffusionXLControlNetInpaintPipeline(
1007
1029
  """Disables the FreeU mechanism if enabled."""
1008
1030
  self.unet.disable_freeu()
1009
1031
 
1032
+ @property
1033
+ def guidance_scale(self):
1034
+ return self._guidance_scale
1035
+
1036
+ @property
1037
+ def clip_skip(self):
1038
+ return self._clip_skip
1039
+
1040
+ # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
1041
+ # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
1042
+ # corresponds to doing no classifier free guidance.
1043
+ @property
1044
+ def do_classifier_free_guidance(self):
1045
+ return self._guidance_scale > 1
1046
+
1047
+ @property
1048
+ def cross_attention_kwargs(self):
1049
+ return self._cross_attention_kwargs
1050
+
1051
+ @property
1052
+ def num_timesteps(self):
1053
+ return self._num_timesteps
1054
+
1010
1055
  @torch.no_grad()
1011
1056
  @replace_example_docstring(EXAMPLE_DOC_STRING)
1012
1057
  def __call__(
@@ -1038,8 +1083,6 @@ class StableDiffusionXLControlNetInpaintPipeline(
1038
1083
  negative_pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
1039
1084
  output_type: Optional[str] = "pil",
1040
1085
  return_dict: bool = True,
1041
- callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
1042
- callback_steps: int = 1,
1043
1086
  cross_attention_kwargs: Optional[Dict[str, Any]] = None,
1044
1087
  controlnet_conditioning_scale: Union[float, List[float]] = 1.0,
1045
1088
  guess_mode: bool = False,
@@ -1052,6 +1095,9 @@ class StableDiffusionXLControlNetInpaintPipeline(
1052
1095
  aesthetic_score: float = 6.0,
1053
1096
  negative_aesthetic_score: float = 2.5,
1054
1097
  clip_skip: Optional[int] = None,
1098
+ callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
1099
+ callback_on_step_end_tensor_inputs: List[str] = ["latents"],
1100
+ **kwargs,
1055
1101
  ):
1056
1102
  r"""
1057
1103
  Function invoked when calling the pipeline for generation.
@@ -1146,12 +1192,6 @@ class StableDiffusionXLControlNetInpaintPipeline(
1146
1192
  return_dict (`bool`, *optional*, defaults to `True`):
1147
1193
  Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
1148
1194
  plain tuple.
1149
- callback (`Callable`, *optional*):
1150
- A function that will be called every `callback_steps` steps during inference. The function will be
1151
- called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
1152
- callback_steps (`int`, *optional*, defaults to 1):
1153
- The frequency at which the `callback` function will be called. If not specified, the callback will be
1154
- called at every step.
1155
1195
  cross_attention_kwargs (`dict`, *optional*):
1156
1196
  A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
1157
1197
  `self.processor` in
@@ -1181,6 +1221,15 @@ class StableDiffusionXLControlNetInpaintPipeline(
1181
1221
  clip_skip (`int`, *optional*):
1182
1222
  Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
1183
1223
  the output of the pre-final layer will be used for computing the prompt embeddings.
1224
+ callback_on_step_end (`Callable`, *optional*):
1225
+ A function that calls at the end of each denoising steps during the inference. The function is called
1226
+ with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
1227
+ callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
1228
+ `callback_on_step_end_tensor_inputs`.
1229
+ callback_on_step_end_tensor_inputs (`List`, *optional*):
1230
+ The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
1231
+ will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
1232
+ `._callback_tensor_inputs` attribute of your pipeine class.
1184
1233
 
1185
1234
  Examples:
1186
1235
 
@@ -1189,6 +1238,23 @@ class StableDiffusionXLControlNetInpaintPipeline(
1189
1238
  [`~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput`] if `return_dict` is True, otherwise a
1190
1239
  `tuple. `tuple. When returning a tuple, the first element is a list with the generated images.
1191
1240
  """
1241
+
1242
+ callback = kwargs.pop("callback", None)
1243
+ callback_steps = kwargs.pop("callback_steps", None)
1244
+
1245
+ if callback is not None:
1246
+ deprecate(
1247
+ "callback",
1248
+ "1.0.0",
1249
+ "Passing `callback` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
1250
+ )
1251
+ if callback_steps is not None:
1252
+ deprecate(
1253
+ "callback_steps",
1254
+ "1.0.0",
1255
+ "Passing `callback_steps` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
1256
+ )
1257
+
1192
1258
  controlnet = self.controlnet._orig_mod if is_compiled_module(self.controlnet) else self.controlnet
1193
1259
 
1194
1260
  # align format for control guidance
@@ -1198,9 +1264,10 @@ class StableDiffusionXLControlNetInpaintPipeline(
1198
1264
  control_guidance_end = len(control_guidance_start) * [control_guidance_end]
1199
1265
  elif not isinstance(control_guidance_start, list) and not isinstance(control_guidance_end, list):
1200
1266
  mult = len(controlnet.nets) if isinstance(controlnet, MultiControlNetModel) else 1
1201
- control_guidance_start, control_guidance_end = mult * [control_guidance_start], mult * [
1202
- control_guidance_end
1203
- ]
1267
+ control_guidance_start, control_guidance_end = (
1268
+ mult * [control_guidance_start],
1269
+ mult * [control_guidance_end],
1270
+ )
1204
1271
 
1205
1272
  # # 0.0 Default height and width to unet
1206
1273
  # height = height or self.unet.config.sample_size * self.vae_scale_factor
@@ -1213,9 +1280,10 @@ class StableDiffusionXLControlNetInpaintPipeline(
1213
1280
  control_guidance_end = len(control_guidance_start) * [control_guidance_end]
1214
1281
  elif not isinstance(control_guidance_start, list) and not isinstance(control_guidance_end, list):
1215
1282
  mult = len(controlnet.nets) if isinstance(controlnet, MultiControlNetModel) else 1
1216
- control_guidance_start, control_guidance_end = mult * [control_guidance_start], mult * [
1217
- control_guidance_end
1218
- ]
1283
+ control_guidance_start, control_guidance_end = (
1284
+ mult * [control_guidance_start],
1285
+ mult * [control_guidance_end],
1286
+ )
1219
1287
 
1220
1288
  # 1. Check inputs
1221
1289
  self.check_inputs(
@@ -1234,8 +1302,13 @@ class StableDiffusionXLControlNetInpaintPipeline(
1234
1302
  controlnet_conditioning_scale,
1235
1303
  control_guidance_start,
1236
1304
  control_guidance_end,
1305
+ callback_on_step_end_tensor_inputs,
1237
1306
  )
1238
1307
 
1308
+ self._guidance_scale = guidance_scale
1309
+ self._clip_skip = clip_skip
1310
+ self._cross_attention_kwargs = cross_attention_kwargs
1311
+
1239
1312
  # 2. Define call parameters
1240
1313
  if prompt is not None and isinstance(prompt, str):
1241
1314
  batch_size = 1
@@ -1245,17 +1318,13 @@ class StableDiffusionXLControlNetInpaintPipeline(
1245
1318
  batch_size = prompt_embeds.shape[0]
1246
1319
 
1247
1320
  device = self._execution_device
1248
- # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
1249
- # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
1250
- # corresponds to doing no classifier free guidance.
1251
- do_classifier_free_guidance = guidance_scale > 1.0
1252
1321
 
1253
1322
  if isinstance(controlnet, MultiControlNetModel) and isinstance(controlnet_conditioning_scale, float):
1254
1323
  controlnet_conditioning_scale = [controlnet_conditioning_scale] * len(controlnet.nets)
1255
1324
 
1256
1325
  # 3. Encode input prompt
1257
1326
  text_encoder_lora_scale = (
1258
- cross_attention_kwargs.get("scale", None) if cross_attention_kwargs is not None else None
1327
+ self.cross_attention_kwargs.get("scale", None) if self.cross_attention_kwargs is not None else None
1259
1328
  )
1260
1329
 
1261
1330
  (
@@ -1268,7 +1337,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1268
1337
  prompt_2=prompt_2,
1269
1338
  device=device,
1270
1339
  num_images_per_prompt=num_images_per_prompt,
1271
- do_classifier_free_guidance=do_classifier_free_guidance,
1340
+ do_classifier_free_guidance=self.do_classifier_free_guidance,
1272
1341
  negative_prompt=negative_prompt,
1273
1342
  negative_prompt_2=negative_prompt_2,
1274
1343
  prompt_embeds=prompt_embeds,
@@ -1276,7 +1345,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1276
1345
  pooled_prompt_embeds=pooled_prompt_embeds,
1277
1346
  negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
1278
1347
  lora_scale=text_encoder_lora_scale,
1279
- clip_skip=clip_skip,
1348
+ clip_skip=self.clip_skip,
1280
1349
  )
1281
1350
 
1282
1351
  # 4. set timesteps
@@ -1297,6 +1366,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1297
1366
  latent_timestep = timesteps[:1].repeat(batch_size * num_images_per_prompt)
1298
1367
  # create a boolean to check if the strength is set to 1. if so then initialise the latents with pure noise
1299
1368
  is_strength_max = strength == 1.0
1369
+ self._num_timesteps = len(timesteps)
1300
1370
 
1301
1371
  # 5. Preprocess mask and image - resizes image and mask w.r.t height and width
1302
1372
  # 5.1 Prepare init image
@@ -1313,7 +1383,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1313
1383
  num_images_per_prompt=num_images_per_prompt,
1314
1384
  device=device,
1315
1385
  dtype=controlnet.dtype,
1316
- do_classifier_free_guidance=do_classifier_free_guidance,
1386
+ do_classifier_free_guidance=self.do_classifier_free_guidance,
1317
1387
  guess_mode=guess_mode,
1318
1388
  )
1319
1389
  elif isinstance(controlnet, MultiControlNetModel):
@@ -1328,7 +1398,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1328
1398
  num_images_per_prompt=num_images_per_prompt,
1329
1399
  device=device,
1330
1400
  dtype=controlnet.dtype,
1331
- do_classifier_free_guidance=do_classifier_free_guidance,
1401
+ do_classifier_free_guidance=self.do_classifier_free_guidance,
1332
1402
  guess_mode=guess_mode,
1333
1403
  )
1334
1404
 
@@ -1382,7 +1452,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1382
1452
  prompt_embeds.dtype,
1383
1453
  device,
1384
1454
  generator,
1385
- do_classifier_free_guidance,
1455
+ self.do_classifier_free_guidance,
1386
1456
  )
1387
1457
 
1388
1458
  # 8. Check that sizes of mask, masked image and latents match
@@ -1443,7 +1513,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1443
1513
  )
1444
1514
  add_time_ids = add_time_ids.repeat(batch_size * num_images_per_prompt, 1)
1445
1515
 
1446
- if do_classifier_free_guidance:
1516
+ if self.do_classifier_free_guidance:
1447
1517
  prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0)
1448
1518
  add_text_embeds = torch.cat([negative_pooled_prompt_embeds, add_text_embeds], dim=0)
1449
1519
  add_neg_time_ids = add_neg_time_ids.repeat(batch_size * num_images_per_prompt, 1)
@@ -1480,7 +1550,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1480
1550
  with self.progress_bar(total=num_inference_steps) as progress_bar:
1481
1551
  for i, t in enumerate(timesteps):
1482
1552
  # expand the latents if we are doing classifier free guidance
1483
- latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
1553
+ latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance else latents
1484
1554
 
1485
1555
  # concat latents, mask, masked_image_latents in the channel dimension
1486
1556
  latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
@@ -1488,7 +1558,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1488
1558
  added_cond_kwargs = {"text_embeds": add_text_embeds, "time_ids": add_time_ids}
1489
1559
 
1490
1560
  # controlnet(s) inference
1491
- if guess_mode and do_classifier_free_guidance:
1561
+ if guess_mode and self.do_classifier_free_guidance:
1492
1562
  # Infer ControlNet only for the conditional batch.
1493
1563
  control_model_input = latents
1494
1564
  control_model_input = self.scheduler.scale_model_input(control_model_input, t)
@@ -1525,7 +1595,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1525
1595
  return_dict=False,
1526
1596
  )
1527
1597
 
1528
- if guess_mode and do_classifier_free_guidance:
1598
+ if guess_mode and self.do_classifier_free_guidance:
1529
1599
  # Infered ControlNet only for the conditional batch.
1530
1600
  # To apply the output of ControlNet to both the unconditional and conditional batches,
1531
1601
  # add 0 to the unconditional batch to keep it unchanged.
@@ -1540,7 +1610,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1540
1610
  latent_model_input,
1541
1611
  t,
1542
1612
  encoder_hidden_states=prompt_embeds,
1543
- cross_attention_kwargs=cross_attention_kwargs,
1613
+ cross_attention_kwargs=self.cross_attention_kwargs,
1544
1614
  down_block_additional_residuals=down_block_res_samples,
1545
1615
  mid_block_additional_residual=mid_block_res_sample,
1546
1616
  added_cond_kwargs=added_cond_kwargs,
@@ -1548,11 +1618,11 @@ class StableDiffusionXLControlNetInpaintPipeline(
1548
1618
  )[0]
1549
1619
 
1550
1620
  # perform guidance
1551
- if do_classifier_free_guidance:
1621
+ if self.do_classifier_free_guidance:
1552
1622
  noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
1553
1623
  noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
1554
1624
 
1555
- if do_classifier_free_guidance and guidance_rescale > 0.0:
1625
+ if self.do_classifier_free_guidance and guidance_rescale > 0.0:
1556
1626
  # Based on 3.4. in https://arxiv.org/pdf/2305.08891.pdf
1557
1627
  noise_pred = rescale_noise_cfg(noise_pred, noise_pred_text, guidance_rescale=guidance_rescale)
1558
1628
 
@@ -1561,7 +1631,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1561
1631
 
1562
1632
  if num_channels_unet == 4:
1563
1633
  init_latents_proper = image_latents
1564
- if do_classifier_free_guidance:
1634
+ if self.do_classifier_free_guidance:
1565
1635
  init_mask, _ = mask.chunk(2)
1566
1636
  else:
1567
1637
  init_mask = mask
@@ -1574,6 +1644,16 @@ class StableDiffusionXLControlNetInpaintPipeline(
1574
1644
 
1575
1645
  latents = (1 - init_mask) * init_latents_proper + init_mask * latents
1576
1646
 
1647
+ if callback_on_step_end is not None:
1648
+ callback_kwargs = {}
1649
+ for k in callback_on_step_end_tensor_inputs:
1650
+ callback_kwargs[k] = locals()[k]
1651
+ callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
1652
+
1653
+ latents = callback_outputs.pop("latents", latents)
1654
+ prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
1655
+ negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds)
1656
+
1577
1657
  # call the callback, if provided
1578
1658
  if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
1579
1659
  progress_bar.update()