diffusers 0.23.0__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.
Files changed (177) hide show
  1. diffusers/__init__.py +16 -2
  2. diffusers/configuration_utils.py +1 -0
  3. diffusers/dependency_versions_check.py +1 -14
  4. diffusers/dependency_versions_table.py +5 -4
  5. diffusers/image_processor.py +186 -14
  6. diffusers/loaders/__init__.py +82 -0
  7. diffusers/loaders/ip_adapter.py +157 -0
  8. diffusers/loaders/lora.py +1415 -0
  9. diffusers/loaders/lora_conversion_utils.py +284 -0
  10. diffusers/loaders/single_file.py +631 -0
  11. diffusers/loaders/textual_inversion.py +459 -0
  12. diffusers/loaders/unet.py +735 -0
  13. diffusers/loaders/utils.py +59 -0
  14. diffusers/models/__init__.py +12 -1
  15. diffusers/models/attention.py +165 -14
  16. diffusers/models/attention_flax.py +9 -1
  17. diffusers/models/attention_processor.py +286 -1
  18. diffusers/models/autoencoder_asym_kl.py +14 -9
  19. diffusers/models/autoencoder_kl.py +3 -18
  20. diffusers/models/autoencoder_kl_temporal_decoder.py +402 -0
  21. diffusers/models/autoencoder_tiny.py +20 -24
  22. diffusers/models/consistency_decoder_vae.py +37 -30
  23. diffusers/models/controlnet.py +59 -39
  24. diffusers/models/controlnet_flax.py +19 -18
  25. diffusers/models/embeddings_flax.py +2 -0
  26. diffusers/models/lora.py +131 -1
  27. diffusers/models/modeling_flax_utils.py +2 -1
  28. diffusers/models/modeling_outputs.py +17 -0
  29. diffusers/models/modeling_utils.py +27 -19
  30. diffusers/models/normalization.py +2 -2
  31. diffusers/models/resnet.py +390 -59
  32. diffusers/models/transformer_2d.py +20 -3
  33. diffusers/models/transformer_temporal.py +183 -1
  34. diffusers/models/unet_2d_blocks_flax.py +5 -0
  35. diffusers/models/unet_2d_condition.py +9 -0
  36. diffusers/models/unet_2d_condition_flax.py +13 -13
  37. diffusers/models/unet_3d_blocks.py +957 -173
  38. diffusers/models/unet_3d_condition.py +16 -8
  39. diffusers/models/unet_kandi3.py +589 -0
  40. diffusers/models/unet_motion_model.py +48 -33
  41. diffusers/models/unet_spatio_temporal_condition.py +489 -0
  42. diffusers/models/vae.py +63 -13
  43. diffusers/models/vae_flax.py +7 -0
  44. diffusers/models/vq_model.py +3 -1
  45. diffusers/optimization.py +16 -9
  46. diffusers/pipelines/__init__.py +65 -12
  47. diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion.py +93 -23
  48. diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion_img2img.py +97 -25
  49. diffusers/pipelines/animatediff/pipeline_animatediff.py +34 -4
  50. diffusers/pipelines/audioldm/pipeline_audioldm.py +1 -0
  51. diffusers/pipelines/auto_pipeline.py +6 -0
  52. diffusers/pipelines/consistency_models/pipeline_consistency_models.py +1 -0
  53. diffusers/pipelines/controlnet/pipeline_controlnet.py +217 -31
  54. diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +101 -32
  55. diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +136 -39
  56. diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +119 -37
  57. diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +196 -35
  58. diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +102 -31
  59. diffusers/pipelines/dance_diffusion/pipeline_dance_diffusion.py +1 -0
  60. diffusers/pipelines/ddim/pipeline_ddim.py +1 -0
  61. diffusers/pipelines/ddpm/pipeline_ddpm.py +1 -0
  62. diffusers/pipelines/deepfloyd_if/pipeline_if.py +13 -1
  63. diffusers/pipelines/deepfloyd_if/pipeline_if_img2img.py +13 -1
  64. diffusers/pipelines/deepfloyd_if/pipeline_if_img2img_superresolution.py +13 -1
  65. diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.py +13 -1
  66. diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting_superresolution.py +13 -1
  67. diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.py +13 -1
  68. diffusers/pipelines/dit/pipeline_dit.py +1 -0
  69. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py +1 -1
  70. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py +3 -3
  71. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py +1 -1
  72. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpainting.py +1 -1
  73. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior.py +1 -1
  74. diffusers/pipelines/kandinsky3/__init__.py +49 -0
  75. diffusers/pipelines/kandinsky3/kandinsky3_pipeline.py +452 -0
  76. diffusers/pipelines/kandinsky3/kandinsky3img2img_pipeline.py +460 -0
  77. diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +65 -6
  78. diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +55 -3
  79. diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py +1 -0
  80. diffusers/pipelines/musicldm/pipeline_musicldm.py +1 -1
  81. diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py +7 -2
  82. diffusers/pipelines/pipeline_flax_utils.py +4 -2
  83. diffusers/pipelines/pipeline_utils.py +33 -13
  84. diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +196 -36
  85. diffusers/pipelines/score_sde_ve/pipeline_score_sde_ve.py +1 -0
  86. diffusers/pipelines/spectrogram_diffusion/pipeline_spectrogram_diffusion.py +1 -0
  87. diffusers/pipelines/stable_diffusion/__init__.py +64 -21
  88. diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +8 -3
  89. diffusers/pipelines/stable_diffusion/pipeline_cycle_diffusion.py +18 -2
  90. diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion.py +2 -2
  91. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_img2img.py +2 -4
  92. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_inpaint.py +1 -0
  93. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_inpaint_legacy.py +1 -0
  94. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +88 -9
  95. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_attend_and_excite.py +1 -0
  96. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_depth2img.py +8 -3
  97. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_diffedit.py +1 -0
  98. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_gligen.py +1 -0
  99. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_gligen_text_image.py +1 -0
  100. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_image_variation.py +1 -0
  101. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +92 -9
  102. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +92 -9
  103. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint_legacy.py +1 -0
  104. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +17 -13
  105. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_k_diffusion.py +1 -0
  106. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_latent_upscale.py +1 -0
  107. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_ldm3d.py +1 -0
  108. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_model_editing.py +1 -0
  109. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_panorama.py +1 -0
  110. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_paradigms.py +1 -0
  111. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_pix2pix_zero.py +1 -0
  112. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py +1 -0
  113. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +1 -0
  114. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +103 -8
  115. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +113 -8
  116. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +115 -9
  117. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +16 -12
  118. diffusers/pipelines/stable_video_diffusion/__init__.py +58 -0
  119. diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +649 -0
  120. diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_adapter.py +108 -12
  121. diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +109 -14
  122. diffusers/pipelines/text_to_video_synthesis/__init__.py +2 -0
  123. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +1 -0
  124. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +18 -3
  125. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +4 -2
  126. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +872 -0
  127. diffusers/pipelines/versatile_diffusion/modeling_text_unet.py +29 -40
  128. diffusers/pipelines/versatile_diffusion/pipeline_versatile_diffusion_dual_guided.py +1 -0
  129. diffusers/pipelines/versatile_diffusion/pipeline_versatile_diffusion_image_variation.py +1 -0
  130. diffusers/pipelines/versatile_diffusion/pipeline_versatile_diffusion_text_to_image.py +1 -0
  131. diffusers/pipelines/wuerstchen/modeling_wuerstchen_common.py +14 -4
  132. diffusers/pipelines/wuerstchen/modeling_wuerstchen_prior.py +9 -5
  133. diffusers/pipelines/wuerstchen/pipeline_wuerstchen.py +1 -1
  134. diffusers/pipelines/wuerstchen/pipeline_wuerstchen_combined.py +2 -2
  135. diffusers/pipelines/wuerstchen/pipeline_wuerstchen_prior.py +1 -1
  136. diffusers/schedulers/__init__.py +2 -4
  137. diffusers/schedulers/deprecated/__init__.py +50 -0
  138. diffusers/schedulers/{scheduling_karras_ve.py → deprecated/scheduling_karras_ve.py} +4 -4
  139. diffusers/schedulers/{scheduling_sde_vp.py → deprecated/scheduling_sde_vp.py} +4 -6
  140. diffusers/schedulers/scheduling_ddim.py +1 -3
  141. diffusers/schedulers/scheduling_ddim_inverse.py +1 -3
  142. diffusers/schedulers/scheduling_ddim_parallel.py +1 -3
  143. diffusers/schedulers/scheduling_ddpm.py +1 -3
  144. diffusers/schedulers/scheduling_ddpm_parallel.py +1 -3
  145. diffusers/schedulers/scheduling_deis_multistep.py +15 -5
  146. diffusers/schedulers/scheduling_dpmsolver_multistep.py +15 -5
  147. diffusers/schedulers/scheduling_dpmsolver_multistep_inverse.py +15 -5
  148. diffusers/schedulers/scheduling_dpmsolver_sde.py +1 -3
  149. diffusers/schedulers/scheduling_dpmsolver_singlestep.py +15 -5
  150. diffusers/schedulers/scheduling_euler_ancestral_discrete.py +1 -3
  151. diffusers/schedulers/scheduling_euler_discrete.py +40 -13
  152. diffusers/schedulers/scheduling_heun_discrete.py +15 -5
  153. diffusers/schedulers/scheduling_k_dpm_2_ancestral_discrete.py +15 -5
  154. diffusers/schedulers/scheduling_k_dpm_2_discrete.py +15 -5
  155. diffusers/schedulers/scheduling_lcm.py +123 -29
  156. diffusers/schedulers/scheduling_lms_discrete.py +1 -3
  157. diffusers/schedulers/scheduling_pndm.py +1 -3
  158. diffusers/schedulers/scheduling_repaint.py +1 -3
  159. diffusers/schedulers/scheduling_unipc_multistep.py +15 -5
  160. diffusers/utils/__init__.py +1 -0
  161. diffusers/utils/constants.py +11 -6
  162. diffusers/utils/dummy_pt_objects.py +45 -0
  163. diffusers/utils/dummy_torch_and_transformers_objects.py +60 -0
  164. diffusers/utils/dynamic_modules_utils.py +4 -4
  165. diffusers/utils/export_utils.py +8 -3
  166. diffusers/utils/logging.py +10 -10
  167. diffusers/utils/outputs.py +5 -5
  168. diffusers/utils/peft_utils.py +88 -44
  169. diffusers/utils/torch_utils.py +2 -2
  170. diffusers/utils/versions.py +117 -0
  171. {diffusers-0.23.0.dist-info → diffusers-0.24.0.dist-info}/METADATA +83 -64
  172. {diffusers-0.23.0.dist-info → diffusers-0.24.0.dist-info}/RECORD +176 -157
  173. {diffusers-0.23.0.dist-info → diffusers-0.24.0.dist-info}/WHEEL +1 -1
  174. {diffusers-0.23.0.dist-info → diffusers-0.24.0.dist-info}/entry_points.txt +1 -0
  175. diffusers/loaders.py +0 -3336
  176. {diffusers-0.23.0.dist-info → diffusers-0.24.0.dist-info}/LICENSE +0 -0
  177. {diffusers-0.23.0.dist-info → diffusers-0.24.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
@@ -164,8 +179,10 @@ class StableDiffusionXLControlNetInpaintPipeline(
164
179
  A scheduler to be used in combination with `unet` to denoise the encoded image latents. Can be one of
165
180
  [`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`].
166
181
  """
182
+
167
183
  model_cpu_offload_seq = "text_encoder->text_encoder_2->unet->vae"
168
184
  _optional_components = ["tokenizer", "tokenizer_2", "text_encoder", "text_encoder_2"]
185
+ _callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"]
169
186
 
170
187
  def __init__(
171
188
  self,
@@ -554,6 +571,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
554
571
  controlnet_conditioning_scale=1.0,
555
572
  control_guidance_start=0.0,
556
573
  control_guidance_end=1.0,
574
+ callback_on_step_end_tensor_inputs=None,
557
575
  ):
558
576
  if strength < 0 or strength > 1:
559
577
  raise ValueError(f"The value of strength should in [0.0, 1.0] but is {strength}")
@@ -564,14 +582,20 @@ class StableDiffusionXLControlNetInpaintPipeline(
564
582
  f"`num_inference_steps` has to be a positive integer but is {num_inference_steps} of type"
565
583
  f" {type(num_inference_steps)}."
566
584
  )
567
- if (callback_steps is None) or (
568
- callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0)
569
- ):
585
+
586
+ if callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0):
570
587
  raise ValueError(
571
588
  f"`callback_steps` has to be a positive integer but is {callback_steps} of type"
572
589
  f" {type(callback_steps)}."
573
590
  )
574
591
 
592
+ if callback_on_step_end_tensor_inputs is not None and not all(
593
+ k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs
594
+ ):
595
+ raise ValueError(
596
+ 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]}"
597
+ )
598
+
575
599
  if prompt is not None and prompt_embeds is not None:
576
600
  raise ValueError(
577
601
  f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
@@ -814,12 +838,12 @@ class StableDiffusionXLControlNetInpaintPipeline(
814
838
 
815
839
  if isinstance(generator, list):
816
840
  image_latents = [
817
- self.vae.encode(image[i : i + 1]).latent_dist.sample(generator=generator[i])
841
+ retrieve_latents(self.vae.encode(image[i : i + 1]), generator=generator[i])
818
842
  for i in range(image.shape[0])
819
843
  ]
820
844
  image_latents = torch.cat(image_latents, dim=0)
821
845
  else:
822
- image_latents = self.vae.encode(image).latent_dist.sample(generator=generator)
846
+ image_latents = retrieve_latents(self.vae.encode(image), generator=generator)
823
847
 
824
848
  if self.vae.config.force_upcast:
825
849
  self.vae.to(dtype)
@@ -1007,6 +1031,29 @@ class StableDiffusionXLControlNetInpaintPipeline(
1007
1031
  """Disables the FreeU mechanism if enabled."""
1008
1032
  self.unet.disable_freeu()
1009
1033
 
1034
+ @property
1035
+ def guidance_scale(self):
1036
+ return self._guidance_scale
1037
+
1038
+ @property
1039
+ def clip_skip(self):
1040
+ return self._clip_skip
1041
+
1042
+ # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
1043
+ # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
1044
+ # corresponds to doing no classifier free guidance.
1045
+ @property
1046
+ def do_classifier_free_guidance(self):
1047
+ return self._guidance_scale > 1
1048
+
1049
+ @property
1050
+ def cross_attention_kwargs(self):
1051
+ return self._cross_attention_kwargs
1052
+
1053
+ @property
1054
+ def num_timesteps(self):
1055
+ return self._num_timesteps
1056
+
1010
1057
  @torch.no_grad()
1011
1058
  @replace_example_docstring(EXAMPLE_DOC_STRING)
1012
1059
  def __call__(
@@ -1038,8 +1085,6 @@ class StableDiffusionXLControlNetInpaintPipeline(
1038
1085
  negative_pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
1039
1086
  output_type: Optional[str] = "pil",
1040
1087
  return_dict: bool = True,
1041
- callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
1042
- callback_steps: int = 1,
1043
1088
  cross_attention_kwargs: Optional[Dict[str, Any]] = None,
1044
1089
  controlnet_conditioning_scale: Union[float, List[float]] = 1.0,
1045
1090
  guess_mode: bool = False,
@@ -1052,6 +1097,9 @@ class StableDiffusionXLControlNetInpaintPipeline(
1052
1097
  aesthetic_score: float = 6.0,
1053
1098
  negative_aesthetic_score: float = 2.5,
1054
1099
  clip_skip: Optional[int] = None,
1100
+ callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
1101
+ callback_on_step_end_tensor_inputs: List[str] = ["latents"],
1102
+ **kwargs,
1055
1103
  ):
1056
1104
  r"""
1057
1105
  Function invoked when calling the pipeline for generation.
@@ -1146,12 +1194,6 @@ class StableDiffusionXLControlNetInpaintPipeline(
1146
1194
  return_dict (`bool`, *optional*, defaults to `True`):
1147
1195
  Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
1148
1196
  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
1197
  cross_attention_kwargs (`dict`, *optional*):
1156
1198
  A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
1157
1199
  `self.processor` in
@@ -1181,6 +1223,15 @@ class StableDiffusionXLControlNetInpaintPipeline(
1181
1223
  clip_skip (`int`, *optional*):
1182
1224
  Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
1183
1225
  the output of the pre-final layer will be used for computing the prompt embeddings.
1226
+ callback_on_step_end (`Callable`, *optional*):
1227
+ A function that calls at the end of each denoising steps during the inference. The function is called
1228
+ with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
1229
+ callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
1230
+ `callback_on_step_end_tensor_inputs`.
1231
+ callback_on_step_end_tensor_inputs (`List`, *optional*):
1232
+ The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
1233
+ will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
1234
+ `._callback_tensor_inputs` attribute of your pipeine class.
1184
1235
 
1185
1236
  Examples:
1186
1237
 
@@ -1189,6 +1240,23 @@ class StableDiffusionXLControlNetInpaintPipeline(
1189
1240
  [`~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput`] if `return_dict` is True, otherwise a
1190
1241
  `tuple. `tuple. When returning a tuple, the first element is a list with the generated images.
1191
1242
  """
1243
+
1244
+ callback = kwargs.pop("callback", None)
1245
+ callback_steps = kwargs.pop("callback_steps", None)
1246
+
1247
+ if callback is not None:
1248
+ deprecate(
1249
+ "callback",
1250
+ "1.0.0",
1251
+ "Passing `callback` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
1252
+ )
1253
+ if callback_steps is not None:
1254
+ deprecate(
1255
+ "callback_steps",
1256
+ "1.0.0",
1257
+ "Passing `callback_steps` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
1258
+ )
1259
+
1192
1260
  controlnet = self.controlnet._orig_mod if is_compiled_module(self.controlnet) else self.controlnet
1193
1261
 
1194
1262
  # align format for control guidance
@@ -1198,9 +1266,10 @@ class StableDiffusionXLControlNetInpaintPipeline(
1198
1266
  control_guidance_end = len(control_guidance_start) * [control_guidance_end]
1199
1267
  elif not isinstance(control_guidance_start, list) and not isinstance(control_guidance_end, list):
1200
1268
  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
- ]
1269
+ control_guidance_start, control_guidance_end = (
1270
+ mult * [control_guidance_start],
1271
+ mult * [control_guidance_end],
1272
+ )
1204
1273
 
1205
1274
  # # 0.0 Default height and width to unet
1206
1275
  # height = height or self.unet.config.sample_size * self.vae_scale_factor
@@ -1213,9 +1282,10 @@ class StableDiffusionXLControlNetInpaintPipeline(
1213
1282
  control_guidance_end = len(control_guidance_start) * [control_guidance_end]
1214
1283
  elif not isinstance(control_guidance_start, list) and not isinstance(control_guidance_end, list):
1215
1284
  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
- ]
1285
+ control_guidance_start, control_guidance_end = (
1286
+ mult * [control_guidance_start],
1287
+ mult * [control_guidance_end],
1288
+ )
1219
1289
 
1220
1290
  # 1. Check inputs
1221
1291
  self.check_inputs(
@@ -1234,8 +1304,13 @@ class StableDiffusionXLControlNetInpaintPipeline(
1234
1304
  controlnet_conditioning_scale,
1235
1305
  control_guidance_start,
1236
1306
  control_guidance_end,
1307
+ callback_on_step_end_tensor_inputs,
1237
1308
  )
1238
1309
 
1310
+ self._guidance_scale = guidance_scale
1311
+ self._clip_skip = clip_skip
1312
+ self._cross_attention_kwargs = cross_attention_kwargs
1313
+
1239
1314
  # 2. Define call parameters
1240
1315
  if prompt is not None and isinstance(prompt, str):
1241
1316
  batch_size = 1
@@ -1245,17 +1320,13 @@ class StableDiffusionXLControlNetInpaintPipeline(
1245
1320
  batch_size = prompt_embeds.shape[0]
1246
1321
 
1247
1322
  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
1323
 
1253
1324
  if isinstance(controlnet, MultiControlNetModel) and isinstance(controlnet_conditioning_scale, float):
1254
1325
  controlnet_conditioning_scale = [controlnet_conditioning_scale] * len(controlnet.nets)
1255
1326
 
1256
1327
  # 3. Encode input prompt
1257
1328
  text_encoder_lora_scale = (
1258
- cross_attention_kwargs.get("scale", None) if cross_attention_kwargs is not None else None
1329
+ self.cross_attention_kwargs.get("scale", None) if self.cross_attention_kwargs is not None else None
1259
1330
  )
1260
1331
 
1261
1332
  (
@@ -1268,7 +1339,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1268
1339
  prompt_2=prompt_2,
1269
1340
  device=device,
1270
1341
  num_images_per_prompt=num_images_per_prompt,
1271
- do_classifier_free_guidance=do_classifier_free_guidance,
1342
+ do_classifier_free_guidance=self.do_classifier_free_guidance,
1272
1343
  negative_prompt=negative_prompt,
1273
1344
  negative_prompt_2=negative_prompt_2,
1274
1345
  prompt_embeds=prompt_embeds,
@@ -1276,7 +1347,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1276
1347
  pooled_prompt_embeds=pooled_prompt_embeds,
1277
1348
  negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
1278
1349
  lora_scale=text_encoder_lora_scale,
1279
- clip_skip=clip_skip,
1350
+ clip_skip=self.clip_skip,
1280
1351
  )
1281
1352
 
1282
1353
  # 4. set timesteps
@@ -1297,6 +1368,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1297
1368
  latent_timestep = timesteps[:1].repeat(batch_size * num_images_per_prompt)
1298
1369
  # create a boolean to check if the strength is set to 1. if so then initialise the latents with pure noise
1299
1370
  is_strength_max = strength == 1.0
1371
+ self._num_timesteps = len(timesteps)
1300
1372
 
1301
1373
  # 5. Preprocess mask and image - resizes image and mask w.r.t height and width
1302
1374
  # 5.1 Prepare init image
@@ -1313,7 +1385,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1313
1385
  num_images_per_prompt=num_images_per_prompt,
1314
1386
  device=device,
1315
1387
  dtype=controlnet.dtype,
1316
- do_classifier_free_guidance=do_classifier_free_guidance,
1388
+ do_classifier_free_guidance=self.do_classifier_free_guidance,
1317
1389
  guess_mode=guess_mode,
1318
1390
  )
1319
1391
  elif isinstance(controlnet, MultiControlNetModel):
@@ -1328,7 +1400,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1328
1400
  num_images_per_prompt=num_images_per_prompt,
1329
1401
  device=device,
1330
1402
  dtype=controlnet.dtype,
1331
- do_classifier_free_guidance=do_classifier_free_guidance,
1403
+ do_classifier_free_guidance=self.do_classifier_free_guidance,
1332
1404
  guess_mode=guess_mode,
1333
1405
  )
1334
1406
 
@@ -1382,7 +1454,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1382
1454
  prompt_embeds.dtype,
1383
1455
  device,
1384
1456
  generator,
1385
- do_classifier_free_guidance,
1457
+ self.do_classifier_free_guidance,
1386
1458
  )
1387
1459
 
1388
1460
  # 8. Check that sizes of mask, masked image and latents match
@@ -1443,7 +1515,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1443
1515
  )
1444
1516
  add_time_ids = add_time_ids.repeat(batch_size * num_images_per_prompt, 1)
1445
1517
 
1446
- if do_classifier_free_guidance:
1518
+ if self.do_classifier_free_guidance:
1447
1519
  prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0)
1448
1520
  add_text_embeds = torch.cat([negative_pooled_prompt_embeds, add_text_embeds], dim=0)
1449
1521
  add_neg_time_ids = add_neg_time_ids.repeat(batch_size * num_images_per_prompt, 1)
@@ -1480,7 +1552,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1480
1552
  with self.progress_bar(total=num_inference_steps) as progress_bar:
1481
1553
  for i, t in enumerate(timesteps):
1482
1554
  # 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
1555
+ latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance else latents
1484
1556
 
1485
1557
  # concat latents, mask, masked_image_latents in the channel dimension
1486
1558
  latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
@@ -1488,7 +1560,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1488
1560
  added_cond_kwargs = {"text_embeds": add_text_embeds, "time_ids": add_time_ids}
1489
1561
 
1490
1562
  # controlnet(s) inference
1491
- if guess_mode and do_classifier_free_guidance:
1563
+ if guess_mode and self.do_classifier_free_guidance:
1492
1564
  # Infer ControlNet only for the conditional batch.
1493
1565
  control_model_input = latents
1494
1566
  control_model_input = self.scheduler.scale_model_input(control_model_input, t)
@@ -1525,7 +1597,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1525
1597
  return_dict=False,
1526
1598
  )
1527
1599
 
1528
- if guess_mode and do_classifier_free_guidance:
1600
+ if guess_mode and self.do_classifier_free_guidance:
1529
1601
  # Infered ControlNet only for the conditional batch.
1530
1602
  # To apply the output of ControlNet to both the unconditional and conditional batches,
1531
1603
  # add 0 to the unconditional batch to keep it unchanged.
@@ -1540,7 +1612,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1540
1612
  latent_model_input,
1541
1613
  t,
1542
1614
  encoder_hidden_states=prompt_embeds,
1543
- cross_attention_kwargs=cross_attention_kwargs,
1615
+ cross_attention_kwargs=self.cross_attention_kwargs,
1544
1616
  down_block_additional_residuals=down_block_res_samples,
1545
1617
  mid_block_additional_residual=mid_block_res_sample,
1546
1618
  added_cond_kwargs=added_cond_kwargs,
@@ -1548,11 +1620,11 @@ class StableDiffusionXLControlNetInpaintPipeline(
1548
1620
  )[0]
1549
1621
 
1550
1622
  # perform guidance
1551
- if do_classifier_free_guidance:
1623
+ if self.do_classifier_free_guidance:
1552
1624
  noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
1553
1625
  noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
1554
1626
 
1555
- if do_classifier_free_guidance and guidance_rescale > 0.0:
1627
+ if self.do_classifier_free_guidance and guidance_rescale > 0.0:
1556
1628
  # Based on 3.4. in https://arxiv.org/pdf/2305.08891.pdf
1557
1629
  noise_pred = rescale_noise_cfg(noise_pred, noise_pred_text, guidance_rescale=guidance_rescale)
1558
1630
 
@@ -1561,7 +1633,7 @@ class StableDiffusionXLControlNetInpaintPipeline(
1561
1633
 
1562
1634
  if num_channels_unet == 4:
1563
1635
  init_latents_proper = image_latents
1564
- if do_classifier_free_guidance:
1636
+ if self.do_classifier_free_guidance:
1565
1637
  init_mask, _ = mask.chunk(2)
1566
1638
  else:
1567
1639
  init_mask = mask
@@ -1574,6 +1646,16 @@ class StableDiffusionXLControlNetInpaintPipeline(
1574
1646
 
1575
1647
  latents = (1 - init_mask) * init_latents_proper + init_mask * latents
1576
1648
 
1649
+ if callback_on_step_end is not None:
1650
+ callback_kwargs = {}
1651
+ for k in callback_on_step_end_tensor_inputs:
1652
+ callback_kwargs[k] = locals()[k]
1653
+ callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
1654
+
1655
+ latents = callback_outputs.pop("latents", latents)
1656
+ prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
1657
+ negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds)
1658
+
1577
1659
  # call the callback, if provided
1578
1660
  if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
1579
1661
  progress_bar.update()