diffusers 0.26.3__py3-none-any.whl → 0.27.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (299) hide show
  1. diffusers/__init__.py +20 -1
  2. diffusers/commands/__init__.py +1 -1
  3. diffusers/commands/diffusers_cli.py +1 -1
  4. diffusers/commands/env.py +1 -1
  5. diffusers/commands/fp16_safetensors.py +1 -1
  6. diffusers/configuration_utils.py +7 -3
  7. diffusers/dependency_versions_check.py +1 -1
  8. diffusers/dependency_versions_table.py +2 -2
  9. diffusers/experimental/rl/value_guided_sampling.py +1 -1
  10. diffusers/image_processor.py +110 -4
  11. diffusers/loaders/autoencoder.py +7 -8
  12. diffusers/loaders/controlnet.py +17 -8
  13. diffusers/loaders/ip_adapter.py +86 -23
  14. diffusers/loaders/lora.py +105 -310
  15. diffusers/loaders/lora_conversion_utils.py +1 -1
  16. diffusers/loaders/peft.py +1 -1
  17. diffusers/loaders/single_file.py +51 -12
  18. diffusers/loaders/single_file_utils.py +274 -49
  19. diffusers/loaders/textual_inversion.py +23 -4
  20. diffusers/loaders/unet.py +195 -41
  21. diffusers/loaders/utils.py +1 -1
  22. diffusers/models/__init__.py +3 -1
  23. diffusers/models/activations.py +9 -9
  24. diffusers/models/attention.py +26 -36
  25. diffusers/models/attention_flax.py +1 -1
  26. diffusers/models/attention_processor.py +171 -114
  27. diffusers/models/autoencoders/autoencoder_asym_kl.py +1 -1
  28. diffusers/models/autoencoders/autoencoder_kl.py +3 -1
  29. diffusers/models/autoencoders/autoencoder_kl_temporal_decoder.py +1 -1
  30. diffusers/models/autoencoders/autoencoder_tiny.py +4 -2
  31. diffusers/models/autoencoders/consistency_decoder_vae.py +1 -1
  32. diffusers/models/autoencoders/vae.py +1 -1
  33. diffusers/models/controlnet.py +1 -1
  34. diffusers/models/controlnet_flax.py +1 -1
  35. diffusers/models/downsampling.py +8 -12
  36. diffusers/models/dual_transformer_2d.py +1 -1
  37. diffusers/models/embeddings.py +3 -4
  38. diffusers/models/embeddings_flax.py +1 -1
  39. diffusers/models/lora.py +33 -10
  40. diffusers/models/modeling_flax_pytorch_utils.py +1 -1
  41. diffusers/models/modeling_flax_utils.py +1 -1
  42. diffusers/models/modeling_pytorch_flax_utils.py +1 -1
  43. diffusers/models/modeling_utils.py +4 -6
  44. diffusers/models/normalization.py +1 -1
  45. diffusers/models/resnet.py +31 -58
  46. diffusers/models/resnet_flax.py +1 -1
  47. diffusers/models/t5_film_transformer.py +1 -1
  48. diffusers/models/transformer_2d.py +1 -1
  49. diffusers/models/transformer_temporal.py +1 -1
  50. diffusers/models/transformers/dual_transformer_2d.py +1 -1
  51. diffusers/models/transformers/t5_film_transformer.py +1 -1
  52. diffusers/models/transformers/transformer_2d.py +29 -31
  53. diffusers/models/transformers/transformer_temporal.py +1 -1
  54. diffusers/models/unet_1d.py +1 -1
  55. diffusers/models/unet_1d_blocks.py +1 -1
  56. diffusers/models/unet_2d.py +1 -1
  57. diffusers/models/unet_2d_blocks.py +1 -1
  58. diffusers/models/unet_2d_condition.py +1 -1
  59. diffusers/models/unets/__init__.py +1 -0
  60. diffusers/models/unets/unet_1d.py +1 -1
  61. diffusers/models/unets/unet_1d_blocks.py +1 -1
  62. diffusers/models/unets/unet_2d.py +4 -4
  63. diffusers/models/unets/unet_2d_blocks.py +238 -98
  64. diffusers/models/unets/unet_2d_blocks_flax.py +1 -1
  65. diffusers/models/unets/unet_2d_condition.py +420 -323
  66. diffusers/models/unets/unet_2d_condition_flax.py +21 -12
  67. diffusers/models/unets/unet_3d_blocks.py +50 -40
  68. diffusers/models/unets/unet_3d_condition.py +47 -8
  69. diffusers/models/unets/unet_i2vgen_xl.py +75 -30
  70. diffusers/models/unets/unet_kandinsky3.py +1 -1
  71. diffusers/models/unets/unet_motion_model.py +48 -8
  72. diffusers/models/unets/unet_spatio_temporal_condition.py +1 -1
  73. diffusers/models/unets/unet_stable_cascade.py +610 -0
  74. diffusers/models/unets/uvit_2d.py +1 -1
  75. diffusers/models/upsampling.py +10 -16
  76. diffusers/models/vae_flax.py +1 -1
  77. diffusers/models/vq_model.py +1 -1
  78. diffusers/optimization.py +1 -1
  79. diffusers/pipelines/__init__.py +26 -0
  80. diffusers/pipelines/amused/pipeline_amused.py +1 -1
  81. diffusers/pipelines/amused/pipeline_amused_img2img.py +1 -1
  82. diffusers/pipelines/amused/pipeline_amused_inpaint.py +1 -1
  83. diffusers/pipelines/animatediff/pipeline_animatediff.py +162 -417
  84. diffusers/pipelines/animatediff/pipeline_animatediff_video2video.py +165 -137
  85. diffusers/pipelines/animatediff/pipeline_output.py +7 -6
  86. diffusers/pipelines/audioldm/pipeline_audioldm.py +3 -19
  87. diffusers/pipelines/audioldm2/modeling_audioldm2.py +1 -1
  88. diffusers/pipelines/audioldm2/pipeline_audioldm2.py +3 -3
  89. diffusers/pipelines/auto_pipeline.py +7 -16
  90. diffusers/pipelines/blip_diffusion/blip_image_processing.py +1 -1
  91. diffusers/pipelines/blip_diffusion/modeling_blip2.py +1 -1
  92. diffusers/pipelines/blip_diffusion/modeling_ctx_clip.py +2 -2
  93. diffusers/pipelines/blip_diffusion/pipeline_blip_diffusion.py +2 -2
  94. diffusers/pipelines/consistency_models/pipeline_consistency_models.py +1 -1
  95. diffusers/pipelines/controlnet/pipeline_controlnet.py +90 -90
  96. diffusers/pipelines/controlnet/pipeline_controlnet_blip_diffusion.py +2 -2
  97. diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +98 -90
  98. diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +92 -90
  99. diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +145 -70
  100. diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +126 -89
  101. diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +108 -96
  102. diffusers/pipelines/controlnet/pipeline_flax_controlnet.py +2 -2
  103. diffusers/pipelines/dance_diffusion/pipeline_dance_diffusion.py +1 -1
  104. diffusers/pipelines/ddim/pipeline_ddim.py +1 -1
  105. diffusers/pipelines/ddpm/pipeline_ddpm.py +1 -1
  106. diffusers/pipelines/deepfloyd_if/pipeline_if.py +4 -4
  107. diffusers/pipelines/deepfloyd_if/pipeline_if_img2img.py +4 -4
  108. diffusers/pipelines/deepfloyd_if/pipeline_if_img2img_superresolution.py +5 -5
  109. diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.py +4 -4
  110. diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting_superresolution.py +5 -5
  111. diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.py +5 -5
  112. diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion.py +10 -120
  113. diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion_img2img.py +10 -91
  114. diffusers/pipelines/deprecated/audio_diffusion/mel.py +1 -1
  115. diffusers/pipelines/deprecated/audio_diffusion/pipeline_audio_diffusion.py +1 -1
  116. diffusers/pipelines/deprecated/latent_diffusion_uncond/pipeline_latent_diffusion_uncond.py +1 -1
  117. diffusers/pipelines/deprecated/pndm/pipeline_pndm.py +1 -1
  118. diffusers/pipelines/deprecated/repaint/pipeline_repaint.py +1 -1
  119. diffusers/pipelines/deprecated/score_sde_ve/pipeline_score_sde_ve.py +1 -1
  120. diffusers/pipelines/deprecated/spectrogram_diffusion/continuous_encoder.py +1 -1
  121. diffusers/pipelines/deprecated/spectrogram_diffusion/midi_utils.py +1 -1
  122. diffusers/pipelines/deprecated/spectrogram_diffusion/notes_encoder.py +1 -1
  123. diffusers/pipelines/deprecated/spectrogram_diffusion/pipeline_spectrogram_diffusion.py +1 -1
  124. diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_cycle_diffusion.py +5 -4
  125. diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_inpaint_legacy.py +5 -4
  126. diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_model_editing.py +7 -22
  127. diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_paradigms.py +5 -39
  128. diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_pix2pix_zero.py +5 -5
  129. diffusers/pipelines/deprecated/stochastic_karras_ve/pipeline_stochastic_karras_ve.py +1 -1
  130. diffusers/pipelines/deprecated/versatile_diffusion/modeling_text_unet.py +31 -22
  131. diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_dual_guided.py +1 -1
  132. diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_image_variation.py +1 -1
  133. diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_text_to_image.py +1 -2
  134. diffusers/pipelines/deprecated/vq_diffusion/pipeline_vq_diffusion.py +1 -1
  135. diffusers/pipelines/dit/pipeline_dit.py +1 -1
  136. diffusers/pipelines/free_init_utils.py +184 -0
  137. diffusers/pipelines/i2vgen_xl/pipeline_i2vgen_xl.py +22 -104
  138. diffusers/pipelines/kandinsky/pipeline_kandinsky.py +1 -1
  139. diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py +1 -1
  140. diffusers/pipelines/kandinsky/pipeline_kandinsky_img2img.py +1 -1
  141. diffusers/pipelines/kandinsky/pipeline_kandinsky_inpaint.py +2 -2
  142. diffusers/pipelines/kandinsky/pipeline_kandinsky_prior.py +1 -1
  143. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py +1 -1
  144. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py +1 -1
  145. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_controlnet.py +1 -1
  146. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_controlnet_img2img.py +1 -1
  147. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py +1 -1
  148. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpainting.py +2 -2
  149. diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +104 -93
  150. diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +112 -74
  151. diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py +1 -1
  152. diffusers/pipelines/ledits_pp/__init__.py +55 -0
  153. diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion.py +1505 -0
  154. diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion_xl.py +1797 -0
  155. diffusers/pipelines/ledits_pp/pipeline_output.py +43 -0
  156. diffusers/pipelines/musicldm/pipeline_musicldm.py +3 -19
  157. diffusers/pipelines/onnx_utils.py +1 -1
  158. diffusers/pipelines/paint_by_example/image_encoder.py +1 -1
  159. diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py +3 -3
  160. diffusers/pipelines/pia/pipeline_pia.py +168 -327
  161. diffusers/pipelines/pipeline_flax_utils.py +1 -1
  162. diffusers/pipelines/pipeline_loading_utils.py +508 -0
  163. diffusers/pipelines/pipeline_utils.py +188 -534
  164. diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +56 -10
  165. diffusers/pipelines/semantic_stable_diffusion/pipeline_semantic_stable_diffusion.py +3 -3
  166. diffusers/pipelines/shap_e/camera.py +1 -1
  167. diffusers/pipelines/shap_e/pipeline_shap_e.py +1 -1
  168. diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py +1 -1
  169. diffusers/pipelines/shap_e/renderer.py +1 -1
  170. diffusers/pipelines/stable_cascade/__init__.py +50 -0
  171. diffusers/pipelines/stable_cascade/pipeline_stable_cascade.py +482 -0
  172. diffusers/pipelines/stable_cascade/pipeline_stable_cascade_combined.py +311 -0
  173. diffusers/pipelines/stable_cascade/pipeline_stable_cascade_prior.py +638 -0
  174. diffusers/pipelines/stable_diffusion/clip_image_project_model.py +1 -1
  175. diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +4 -1
  176. diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion.py +1 -1
  177. diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion_img2img.py +2 -2
  178. diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion_inpaint.py +1 -1
  179. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion.py +1 -1
  180. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_img2img.py +1 -1
  181. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_inpaint.py +1 -1
  182. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_upscale.py +1 -1
  183. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +90 -146
  184. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_depth2img.py +5 -4
  185. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_image_variation.py +4 -32
  186. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +92 -119
  187. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +92 -119
  188. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +13 -59
  189. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_latent_upscale.py +3 -31
  190. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +5 -33
  191. diffusers/pipelines/stable_diffusion/pipeline_stable_unclip.py +5 -21
  192. diffusers/pipelines/stable_diffusion/pipeline_stable_unclip_img2img.py +7 -21
  193. diffusers/pipelines/stable_diffusion/safety_checker.py +1 -1
  194. diffusers/pipelines/stable_diffusion/safety_checker_flax.py +1 -1
  195. diffusers/pipelines/stable_diffusion/stable_unclip_image_normalizer.py +1 -1
  196. diffusers/pipelines/stable_diffusion_attend_and_excite/pipeline_stable_diffusion_attend_and_excite.py +5 -21
  197. diffusers/pipelines/stable_diffusion_diffedit/pipeline_stable_diffusion_diffedit.py +9 -38
  198. diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen.py +5 -34
  199. diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py +6 -35
  200. diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_k_diffusion.py +7 -6
  201. diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_xl_k_diffusion.py +4 -124
  202. diffusers/pipelines/stable_diffusion_ldm3d/pipeline_stable_diffusion_ldm3d.py +282 -80
  203. diffusers/pipelines/stable_diffusion_panorama/pipeline_stable_diffusion_panorama.py +94 -46
  204. diffusers/pipelines/stable_diffusion_safe/pipeline_stable_diffusion_safe.py +3 -3
  205. diffusers/pipelines/stable_diffusion_safe/safety_checker.py +1 -1
  206. diffusers/pipelines/stable_diffusion_sag/pipeline_stable_diffusion_sag.py +6 -22
  207. diffusers/pipelines/stable_diffusion_xl/pipeline_flax_stable_diffusion_xl.py +1 -1
  208. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +96 -148
  209. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +98 -154
  210. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +98 -153
  211. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +25 -87
  212. diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +89 -80
  213. diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_adapter.py +5 -49
  214. diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +80 -88
  215. diffusers/pipelines/text_to_video_synthesis/pipeline_output.py +8 -6
  216. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +15 -86
  217. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +20 -93
  218. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +5 -5
  219. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +3 -19
  220. diffusers/pipelines/unclip/pipeline_unclip.py +1 -1
  221. diffusers/pipelines/unclip/pipeline_unclip_image_variation.py +1 -1
  222. diffusers/pipelines/unclip/text_proj.py +1 -1
  223. diffusers/pipelines/unidiffuser/pipeline_unidiffuser.py +35 -35
  224. diffusers/pipelines/wuerstchen/modeling_paella_vq_model.py +1 -1
  225. diffusers/pipelines/wuerstchen/modeling_wuerstchen_common.py +4 -21
  226. diffusers/pipelines/wuerstchen/modeling_wuerstchen_diffnext.py +2 -2
  227. diffusers/pipelines/wuerstchen/modeling_wuerstchen_prior.py +4 -5
  228. diffusers/pipelines/wuerstchen/pipeline_wuerstchen.py +8 -8
  229. diffusers/pipelines/wuerstchen/pipeline_wuerstchen_combined.py +1 -1
  230. diffusers/pipelines/wuerstchen/pipeline_wuerstchen_prior.py +2 -2
  231. diffusers/schedulers/__init__.py +7 -1
  232. diffusers/schedulers/deprecated/scheduling_karras_ve.py +1 -1
  233. diffusers/schedulers/deprecated/scheduling_sde_vp.py +1 -1
  234. diffusers/schedulers/scheduling_consistency_models.py +42 -19
  235. diffusers/schedulers/scheduling_ddim.py +2 -4
  236. diffusers/schedulers/scheduling_ddim_flax.py +13 -5
  237. diffusers/schedulers/scheduling_ddim_inverse.py +2 -4
  238. diffusers/schedulers/scheduling_ddim_parallel.py +2 -4
  239. diffusers/schedulers/scheduling_ddpm.py +2 -4
  240. diffusers/schedulers/scheduling_ddpm_flax.py +1 -1
  241. diffusers/schedulers/scheduling_ddpm_parallel.py +2 -4
  242. diffusers/schedulers/scheduling_ddpm_wuerstchen.py +1 -1
  243. diffusers/schedulers/scheduling_deis_multistep.py +46 -19
  244. diffusers/schedulers/scheduling_dpmsolver_multistep.py +107 -21
  245. diffusers/schedulers/scheduling_dpmsolver_multistep_flax.py +1 -1
  246. diffusers/schedulers/scheduling_dpmsolver_multistep_inverse.py +9 -7
  247. diffusers/schedulers/scheduling_dpmsolver_sde.py +35 -35
  248. diffusers/schedulers/scheduling_dpmsolver_singlestep.py +49 -18
  249. diffusers/schedulers/scheduling_edm_dpmsolver_multistep.py +683 -0
  250. diffusers/schedulers/scheduling_edm_euler.py +381 -0
  251. diffusers/schedulers/scheduling_euler_ancestral_discrete.py +43 -15
  252. diffusers/schedulers/scheduling_euler_discrete.py +42 -17
  253. diffusers/schedulers/scheduling_euler_discrete_flax.py +1 -1
  254. diffusers/schedulers/scheduling_heun_discrete.py +35 -35
  255. diffusers/schedulers/scheduling_ipndm.py +37 -11
  256. diffusers/schedulers/scheduling_k_dpm_2_ancestral_discrete.py +44 -44
  257. diffusers/schedulers/scheduling_k_dpm_2_discrete.py +44 -44
  258. diffusers/schedulers/scheduling_karras_ve_flax.py +1 -1
  259. diffusers/schedulers/scheduling_lcm.py +38 -14
  260. diffusers/schedulers/scheduling_lms_discrete.py +43 -15
  261. diffusers/schedulers/scheduling_lms_discrete_flax.py +1 -1
  262. diffusers/schedulers/scheduling_pndm.py +2 -4
  263. diffusers/schedulers/scheduling_pndm_flax.py +2 -4
  264. diffusers/schedulers/scheduling_repaint.py +1 -1
  265. diffusers/schedulers/scheduling_sasolver.py +41 -9
  266. diffusers/schedulers/scheduling_sde_ve.py +1 -1
  267. diffusers/schedulers/scheduling_sde_ve_flax.py +1 -1
  268. diffusers/schedulers/scheduling_tcd.py +686 -0
  269. diffusers/schedulers/scheduling_unclip.py +1 -1
  270. diffusers/schedulers/scheduling_unipc_multistep.py +46 -19
  271. diffusers/schedulers/scheduling_utils.py +2 -1
  272. diffusers/schedulers/scheduling_utils_flax.py +1 -1
  273. diffusers/schedulers/scheduling_vq_diffusion.py +1 -1
  274. diffusers/training_utils.py +9 -2
  275. diffusers/utils/__init__.py +2 -1
  276. diffusers/utils/accelerate_utils.py +1 -1
  277. diffusers/utils/constants.py +1 -1
  278. diffusers/utils/doc_utils.py +1 -1
  279. diffusers/utils/dummy_pt_objects.py +60 -0
  280. diffusers/utils/dummy_torch_and_transformers_objects.py +75 -0
  281. diffusers/utils/dynamic_modules_utils.py +1 -1
  282. diffusers/utils/export_utils.py +3 -3
  283. diffusers/utils/hub_utils.py +60 -16
  284. diffusers/utils/import_utils.py +15 -1
  285. diffusers/utils/loading_utils.py +2 -0
  286. diffusers/utils/logging.py +1 -1
  287. diffusers/utils/model_card_template.md +24 -0
  288. diffusers/utils/outputs.py +14 -7
  289. diffusers/utils/peft_utils.py +1 -1
  290. diffusers/utils/state_dict_utils.py +1 -1
  291. diffusers/utils/testing_utils.py +2 -0
  292. diffusers/utils/torch_utils.py +1 -1
  293. {diffusers-0.26.3.dist-info → diffusers-0.27.0.dist-info}/METADATA +46 -46
  294. diffusers-0.27.0.dist-info/RECORD +399 -0
  295. {diffusers-0.26.3.dist-info → diffusers-0.27.0.dist-info}/WHEEL +1 -1
  296. diffusers-0.26.3.dist-info/RECORD +0 -384
  297. {diffusers-0.26.3.dist-info → diffusers-0.27.0.dist-info}/LICENSE +0 -0
  298. {diffusers-0.26.3.dist-info → diffusers-0.27.0.dist-info}/entry_points.txt +0 -0
  299. {diffusers-0.26.3.dist-info → diffusers-0.27.0.dist-info}/top_level.txt +0 -0
@@ -1,4 +1,4 @@
1
- # Copyright 2023 Katherine Crowson, The HuggingFace Team and hlky. All rights reserved.
1
+ # Copyright 2024 Katherine Crowson, The HuggingFace Team and hlky. All rights reserved.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
13
13
  # limitations under the License.
14
14
 
15
15
  import math
16
- from collections import defaultdict
17
16
  from typing import List, Optional, Tuple, Union
18
17
 
19
18
  import numpy as np
@@ -102,9 +101,7 @@ class HeunDiscreteScheduler(SchedulerMixin, ConfigMixin):
102
101
  The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
103
102
  Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
104
103
  steps_offset (`int`, defaults to 0):
105
- An offset added to the inference steps. You can use a combination of `offset=1` and
106
- `set_alpha_to_one=False` to make the last step use step 0 for the previous alpha product like in Stable
107
- Diffusion.
104
+ An offset added to the inference steps, as required by some model families.
108
105
  """
109
106
 
110
107
  _compatibles = [e.name for e in KarrasDiffusionSchedulers]
@@ -148,8 +145,10 @@ class HeunDiscreteScheduler(SchedulerMixin, ConfigMixin):
148
145
  self.use_karras_sigmas = use_karras_sigmas
149
146
 
150
147
  self._step_index = None
148
+ self._begin_index = None
151
149
  self.sigmas = self.sigmas.to("cpu") # to avoid too much CPU/GPU communication
152
150
 
151
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.index_for_timestep
153
152
  def index_for_timestep(self, timestep, schedule_timesteps=None):
154
153
  if schedule_timesteps is None:
155
154
  schedule_timesteps = self.timesteps
@@ -160,11 +159,7 @@ class HeunDiscreteScheduler(SchedulerMixin, ConfigMixin):
160
159
  # is always the second index (or the last index if there is only 1)
161
160
  # This way we can ensure we don't accidentally skip a sigma in
162
161
  # case we start in the middle of the denoising schedule (e.g. for image-to-image)
163
- if len(self._index_counter) == 0:
164
- pos = 1 if len(indices) > 1 else 0
165
- else:
166
- timestep_int = timestep.cpu().item() if torch.is_tensor(timestep) else timestep
167
- pos = self._index_counter[timestep_int]
162
+ pos = 1 if len(indices) > 1 else 0
168
163
 
169
164
  return indices[pos].item()
170
165
 
@@ -183,6 +178,24 @@ class HeunDiscreteScheduler(SchedulerMixin, ConfigMixin):
183
178
  """
184
179
  return self._step_index
185
180
 
181
+ @property
182
+ def begin_index(self):
183
+ """
184
+ The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
185
+ """
186
+ return self._begin_index
187
+
188
+ # Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.set_begin_index
189
+ def set_begin_index(self, begin_index: int = 0):
190
+ """
191
+ Sets the begin index for the scheduler. This function should be run from pipeline before the inference.
192
+
193
+ Args:
194
+ begin_index (`int`):
195
+ The begin index for the scheduler.
196
+ """
197
+ self._begin_index = begin_index
198
+
186
199
  def scale_model_input(
187
200
  self,
188
201
  sample: torch.FloatTensor,
@@ -270,13 +283,9 @@ class HeunDiscreteScheduler(SchedulerMixin, ConfigMixin):
270
283
  self.dt = None
271
284
 
272
285
  self._step_index = None
286
+ self._begin_index = None
273
287
  self.sigmas = self.sigmas.to("cpu") # to avoid too much CPU/GPU communication
274
288
 
275
- # (YiYi Notes: keep this for now since we are keeping add_noise function which use index_for_timestep)
276
- # for exp beta schedules, such as the one for `pipeline_shap_e.py`
277
- # we need an index counter
278
- self._index_counter = defaultdict(int)
279
-
280
289
  # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._sigma_to_t
281
290
  def _sigma_to_t(self, sigma, log_sigmas):
282
291
  # get log sigma
@@ -333,21 +342,12 @@ class HeunDiscreteScheduler(SchedulerMixin, ConfigMixin):
333
342
 
334
343
  # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._init_step_index
335
344
  def _init_step_index(self, timestep):
336
- if isinstance(timestep, torch.Tensor):
337
- timestep = timestep.to(self.timesteps.device)
338
-
339
- index_candidates = (self.timesteps == timestep).nonzero()
340
-
341
- # The sigma index that is taken for the **very** first `step`
342
- # is always the second index (or the last index if there is only 1)
343
- # This way we can ensure we don't accidentally skip a sigma in
344
- # case we start in the middle of the denoising schedule (e.g. for image-to-image)
345
- if len(index_candidates) > 1:
346
- step_index = index_candidates[1]
345
+ if self.begin_index is None:
346
+ if isinstance(timestep, torch.Tensor):
347
+ timestep = timestep.to(self.timesteps.device)
348
+ self._step_index = self.index_for_timestep(timestep)
347
349
  else:
348
- step_index = index_candidates[0]
349
-
350
- self._step_index = step_index.item()
350
+ self._step_index = self._begin_index
351
351
 
352
352
  def step(
353
353
  self,
@@ -378,11 +378,6 @@ class HeunDiscreteScheduler(SchedulerMixin, ConfigMixin):
378
378
  if self.step_index is None:
379
379
  self._init_step_index(timestep)
380
380
 
381
- # (YiYi notes: keep this for now since we are keeping the add_noise method)
382
- # advance index counter by 1
383
- timestep_int = timestep.cpu().item() if torch.is_tensor(timestep) else timestep
384
- self._index_counter[timestep_int] += 1
385
-
386
381
  if self.state_in_first_order:
387
382
  sigma = self.sigmas[self.step_index]
388
383
  sigma_next = self.sigmas[self.step_index + 1]
@@ -453,6 +448,7 @@ class HeunDiscreteScheduler(SchedulerMixin, ConfigMixin):
453
448
 
454
449
  return SchedulerOutput(prev_sample=prev_sample)
455
450
 
451
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.add_noise
456
452
  def add_noise(
457
453
  self,
458
454
  original_samples: torch.FloatTensor,
@@ -469,7 +465,11 @@ class HeunDiscreteScheduler(SchedulerMixin, ConfigMixin):
469
465
  schedule_timesteps = self.timesteps.to(original_samples.device)
470
466
  timesteps = timesteps.to(original_samples.device)
471
467
 
472
- step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timesteps]
468
+ # self.begin_index is None when scheduler is used for training, or pipeline does not implement set_begin_index
469
+ if self.begin_index is None:
470
+ step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timesteps]
471
+ else:
472
+ step_indices = [self.begin_index] * timesteps.shape[0]
473
473
 
474
474
  sigma = sigmas[step_indices].flatten()
475
475
  while len(sigma.shape) < len(original_samples.shape):
@@ -1,4 +1,4 @@
1
- # Copyright 2023 Zhejiang University Team and The HuggingFace Team. All rights reserved.
1
+ # Copyright 2024 Zhejiang University Team and The HuggingFace Team. All rights reserved.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -56,6 +56,7 @@ class IPNDMScheduler(SchedulerMixin, ConfigMixin):
56
56
  # running values
57
57
  self.ets = []
58
58
  self._step_index = None
59
+ self._begin_index = None
59
60
 
60
61
  @property
61
62
  def step_index(self):
@@ -64,6 +65,24 @@ class IPNDMScheduler(SchedulerMixin, ConfigMixin):
64
65
  """
65
66
  return self._step_index
66
67
 
68
+ @property
69
+ def begin_index(self):
70
+ """
71
+ The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
72
+ """
73
+ return self._begin_index
74
+
75
+ # Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.set_begin_index
76
+ def set_begin_index(self, begin_index: int = 0):
77
+ """
78
+ Sets the begin index for the scheduler. This function should be run from pipeline before the inference.
79
+
80
+ Args:
81
+ begin_index (`int`):
82
+ The begin index for the scheduler.
83
+ """
84
+ self._begin_index = begin_index
85
+
67
86
  def set_timesteps(self, num_inference_steps: int, device: Union[str, torch.device] = None):
68
87
  """
69
88
  Sets the discrete timesteps used for the diffusion chain (to be run before inference).
@@ -90,24 +109,31 @@ class IPNDMScheduler(SchedulerMixin, ConfigMixin):
90
109
 
91
110
  self.ets = []
92
111
  self._step_index = None
112
+ self._begin_index = None
93
113
 
94
- # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._init_step_index
95
- def _init_step_index(self, timestep):
96
- if isinstance(timestep, torch.Tensor):
97
- timestep = timestep.to(self.timesteps.device)
114
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.index_for_timestep
115
+ def index_for_timestep(self, timestep, schedule_timesteps=None):
116
+ if schedule_timesteps is None:
117
+ schedule_timesteps = self.timesteps
98
118
 
99
- index_candidates = (self.timesteps == timestep).nonzero()
119
+ indices = (schedule_timesteps == timestep).nonzero()
100
120
 
101
121
  # The sigma index that is taken for the **very** first `step`
102
122
  # is always the second index (or the last index if there is only 1)
103
123
  # This way we can ensure we don't accidentally skip a sigma in
104
124
  # case we start in the middle of the denoising schedule (e.g. for image-to-image)
105
- if len(index_candidates) > 1:
106
- step_index = index_candidates[1]
107
- else:
108
- step_index = index_candidates[0]
125
+ pos = 1 if len(indices) > 1 else 0
109
126
 
110
- self._step_index = step_index.item()
127
+ return indices[pos].item()
128
+
129
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._init_step_index
130
+ def _init_step_index(self, timestep):
131
+ if self.begin_index is None:
132
+ if isinstance(timestep, torch.Tensor):
133
+ timestep = timestep.to(self.timesteps.device)
134
+ self._step_index = self.index_for_timestep(timestep)
135
+ else:
136
+ self._step_index = self._begin_index
111
137
 
112
138
  def step(
113
139
  self,
@@ -1,4 +1,4 @@
1
- # Copyright 2023 Katherine Crowson, The HuggingFace Team and hlky. All rights reserved.
1
+ # Copyright 2024 Katherine Crowson, The HuggingFace Team and hlky. All rights reserved.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
13
13
  # limitations under the License.
14
14
 
15
15
  import math
16
- from collections import defaultdict
17
16
  from typing import List, Optional, Tuple, Union
18
17
 
19
18
  import numpy as np
@@ -100,9 +99,7 @@ class KDPM2AncestralDiscreteScheduler(SchedulerMixin, ConfigMixin):
100
99
  The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
101
100
  Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
102
101
  steps_offset (`int`, defaults to 0):
103
- An offset added to the inference steps. You can use a combination of `offset=1` and
104
- `set_alpha_to_one=False` to make the last step use step 0 for the previous alpha product like in Stable
105
- Diffusion.
102
+ An offset added to the inference steps, as required by some model families.
106
103
  """
107
104
 
108
105
  _compatibles = [e.name for e in KarrasDiffusionSchedulers]
@@ -140,27 +137,9 @@ class KDPM2AncestralDiscreteScheduler(SchedulerMixin, ConfigMixin):
140
137
  # set all values
141
138
  self.set_timesteps(num_train_timesteps, None, num_train_timesteps)
142
139
  self._step_index = None
140
+ self._begin_index = None
143
141
  self.sigmas = self.sigmas.to("cpu") # to avoid too much CPU/GPU communication
144
142
 
145
- # Copied from diffusers.schedulers.scheduling_heun_discrete.HeunDiscreteScheduler.index_for_timestep
146
- def index_for_timestep(self, timestep, schedule_timesteps=None):
147
- if schedule_timesteps is None:
148
- schedule_timesteps = self.timesteps
149
-
150
- indices = (schedule_timesteps == timestep).nonzero()
151
-
152
- # The sigma index that is taken for the **very** first `step`
153
- # is always the second index (or the last index if there is only 1)
154
- # This way we can ensure we don't accidentally skip a sigma in
155
- # case we start in the middle of the denoising schedule (e.g. for image-to-image)
156
- if len(self._index_counter) == 0:
157
- pos = 1 if len(indices) > 1 else 0
158
- else:
159
- timestep_int = timestep.cpu().item() if torch.is_tensor(timestep) else timestep
160
- pos = self._index_counter[timestep_int]
161
-
162
- return indices[pos].item()
163
-
164
143
  @property
165
144
  def init_noise_sigma(self):
166
145
  # standard deviation of the initial noise distribution
@@ -176,6 +155,24 @@ class KDPM2AncestralDiscreteScheduler(SchedulerMixin, ConfigMixin):
176
155
  """
177
156
  return self._step_index
178
157
 
158
+ @property
159
+ def begin_index(self):
160
+ """
161
+ The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
162
+ """
163
+ return self._begin_index
164
+
165
+ # Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.set_begin_index
166
+ def set_begin_index(self, begin_index: int = 0):
167
+ """
168
+ Sets the begin index for the scheduler. This function should be run from pipeline before the inference.
169
+
170
+ Args:
171
+ begin_index (`int`):
172
+ The begin index for the scheduler.
173
+ """
174
+ self._begin_index = begin_index
175
+
179
176
  def scale_model_input(
180
177
  self,
181
178
  sample: torch.FloatTensor,
@@ -295,11 +292,8 @@ class KDPM2AncestralDiscreteScheduler(SchedulerMixin, ConfigMixin):
295
292
 
296
293
  self.sample = None
297
294
 
298
- # for exp beta schedules, such as the one for `pipeline_shap_e.py`
299
- # we need an index counter
300
- self._index_counter = defaultdict(int)
301
-
302
295
  self._step_index = None
296
+ self._begin_index = None
303
297
  self.sigmas = self.sigmas.to("cpu") # to avoid too much CPU/GPU communication
304
298
 
305
299
  # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._sigma_to_t
@@ -356,23 +350,29 @@ class KDPM2AncestralDiscreteScheduler(SchedulerMixin, ConfigMixin):
356
350
  def state_in_first_order(self):
357
351
  return self.sample is None
358
352
 
359
- # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._init_step_index
360
- def _init_step_index(self, timestep):
361
- if isinstance(timestep, torch.Tensor):
362
- timestep = timestep.to(self.timesteps.device)
353
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.index_for_timestep
354
+ def index_for_timestep(self, timestep, schedule_timesteps=None):
355
+ if schedule_timesteps is None:
356
+ schedule_timesteps = self.timesteps
363
357
 
364
- index_candidates = (self.timesteps == timestep).nonzero()
358
+ indices = (schedule_timesteps == timestep).nonzero()
365
359
 
366
360
  # The sigma index that is taken for the **very** first `step`
367
361
  # is always the second index (or the last index if there is only 1)
368
362
  # This way we can ensure we don't accidentally skip a sigma in
369
363
  # case we start in the middle of the denoising schedule (e.g. for image-to-image)
370
- if len(index_candidates) > 1:
371
- step_index = index_candidates[1]
372
- else:
373
- step_index = index_candidates[0]
364
+ pos = 1 if len(indices) > 1 else 0
374
365
 
375
- self._step_index = step_index.item()
366
+ return indices[pos].item()
367
+
368
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._init_step_index
369
+ def _init_step_index(self, timestep):
370
+ if self.begin_index is None:
371
+ if isinstance(timestep, torch.Tensor):
372
+ timestep = timestep.to(self.timesteps.device)
373
+ self._step_index = self.index_for_timestep(timestep)
374
+ else:
375
+ self._step_index = self._begin_index
376
376
 
377
377
  def step(
378
378
  self,
@@ -406,10 +406,6 @@ class KDPM2AncestralDiscreteScheduler(SchedulerMixin, ConfigMixin):
406
406
  if self.step_index is None:
407
407
  self._init_step_index(timestep)
408
408
 
409
- # advance index counter by 1
410
- timestep_int = timestep.cpu().item() if torch.is_tensor(timestep) else timestep
411
- self._index_counter[timestep_int] += 1
412
-
413
409
  if self.state_in_first_order:
414
410
  sigma = self.sigmas[self.step_index]
415
411
  sigma_interpol = self.sigmas_interpol[self.step_index]
@@ -478,7 +474,7 @@ class KDPM2AncestralDiscreteScheduler(SchedulerMixin, ConfigMixin):
478
474
 
479
475
  return SchedulerOutput(prev_sample=prev_sample)
480
476
 
481
- # Copied from diffusers.schedulers.scheduling_heun_discrete.HeunDiscreteScheduler.add_noise
477
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.add_noise
482
478
  def add_noise(
483
479
  self,
484
480
  original_samples: torch.FloatTensor,
@@ -495,7 +491,11 @@ class KDPM2AncestralDiscreteScheduler(SchedulerMixin, ConfigMixin):
495
491
  schedule_timesteps = self.timesteps.to(original_samples.device)
496
492
  timesteps = timesteps.to(original_samples.device)
497
493
 
498
- step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timesteps]
494
+ # self.begin_index is None when scheduler is used for training, or pipeline does not implement set_begin_index
495
+ if self.begin_index is None:
496
+ step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timesteps]
497
+ else:
498
+ step_indices = [self.begin_index] * timesteps.shape[0]
499
499
 
500
500
  sigma = sigmas[step_indices].flatten()
501
501
  while len(sigma.shape) < len(original_samples.shape):
@@ -1,4 +1,4 @@
1
- # Copyright 2023 Katherine Crowson, The HuggingFace Team and hlky. All rights reserved.
1
+ # Copyright 2024 Katherine Crowson, The HuggingFace Team and hlky. All rights reserved.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
13
13
  # limitations under the License.
14
14
 
15
15
  import math
16
- from collections import defaultdict
17
16
  from typing import List, Optional, Tuple, Union
18
17
 
19
18
  import numpy as np
@@ -99,9 +98,7 @@ class KDPM2DiscreteScheduler(SchedulerMixin, ConfigMixin):
99
98
  The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
100
99
  Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
101
100
  steps_offset (`int`, defaults to 0):
102
- An offset added to the inference steps. You can use a combination of `offset=1` and
103
- `set_alpha_to_one=False` to make the last step use step 0 for the previous alpha product like in Stable
104
- Diffusion.
101
+ An offset added to the inference steps, as required by some model families.
105
102
  """
106
103
 
107
104
  _compatibles = [e.name for e in KarrasDiffusionSchedulers]
@@ -140,27 +137,9 @@ class KDPM2DiscreteScheduler(SchedulerMixin, ConfigMixin):
140
137
  self.set_timesteps(num_train_timesteps, None, num_train_timesteps)
141
138
 
142
139
  self._step_index = None
140
+ self._begin_index = None
143
141
  self.sigmas = self.sigmas.to("cpu") # to avoid too much CPU/GPU communication
144
142
 
145
- # Copied from diffusers.schedulers.scheduling_heun_discrete.HeunDiscreteScheduler.index_for_timestep
146
- def index_for_timestep(self, timestep, schedule_timesteps=None):
147
- if schedule_timesteps is None:
148
- schedule_timesteps = self.timesteps
149
-
150
- indices = (schedule_timesteps == timestep).nonzero()
151
-
152
- # The sigma index that is taken for the **very** first `step`
153
- # is always the second index (or the last index if there is only 1)
154
- # This way we can ensure we don't accidentally skip a sigma in
155
- # case we start in the middle of the denoising schedule (e.g. for image-to-image)
156
- if len(self._index_counter) == 0:
157
- pos = 1 if len(indices) > 1 else 0
158
- else:
159
- timestep_int = timestep.cpu().item() if torch.is_tensor(timestep) else timestep
160
- pos = self._index_counter[timestep_int]
161
-
162
- return indices[pos].item()
163
-
164
143
  @property
165
144
  def init_noise_sigma(self):
166
145
  # standard deviation of the initial noise distribution
@@ -176,6 +155,24 @@ class KDPM2DiscreteScheduler(SchedulerMixin, ConfigMixin):
176
155
  """
177
156
  return self._step_index
178
157
 
158
+ @property
159
+ def begin_index(self):
160
+ """
161
+ The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
162
+ """
163
+ return self._begin_index
164
+
165
+ # Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.set_begin_index
166
+ def set_begin_index(self, begin_index: int = 0):
167
+ """
168
+ Sets the begin index for the scheduler. This function should be run from pipeline before the inference.
169
+
170
+ Args:
171
+ begin_index (`int`):
172
+ The begin index for the scheduler.
173
+ """
174
+ self._begin_index = begin_index
175
+
179
176
  def scale_model_input(
180
177
  self,
181
178
  sample: torch.FloatTensor,
@@ -280,34 +277,37 @@ class KDPM2DiscreteScheduler(SchedulerMixin, ConfigMixin):
280
277
 
281
278
  self.sample = None
282
279
 
283
- # for exp beta schedules, such as the one for `pipeline_shap_e.py`
284
- # we need an index counter
285
- self._index_counter = defaultdict(int)
286
-
287
280
  self._step_index = None
281
+ self._begin_index = None
288
282
  self.sigmas = self.sigmas.to("cpu") # to avoid too much CPU/GPU communication
289
283
 
290
284
  @property
291
285
  def state_in_first_order(self):
292
286
  return self.sample is None
293
287
 
294
- # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._init_step_index
295
- def _init_step_index(self, timestep):
296
- if isinstance(timestep, torch.Tensor):
297
- timestep = timestep.to(self.timesteps.device)
288
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.index_for_timestep
289
+ def index_for_timestep(self, timestep, schedule_timesteps=None):
290
+ if schedule_timesteps is None:
291
+ schedule_timesteps = self.timesteps
298
292
 
299
- index_candidates = (self.timesteps == timestep).nonzero()
293
+ indices = (schedule_timesteps == timestep).nonzero()
300
294
 
301
295
  # The sigma index that is taken for the **very** first `step`
302
296
  # is always the second index (or the last index if there is only 1)
303
297
  # This way we can ensure we don't accidentally skip a sigma in
304
298
  # case we start in the middle of the denoising schedule (e.g. for image-to-image)
305
- if len(index_candidates) > 1:
306
- step_index = index_candidates[1]
307
- else:
308
- step_index = index_candidates[0]
299
+ pos = 1 if len(indices) > 1 else 0
309
300
 
310
- self._step_index = step_index.item()
301
+ return indices[pos].item()
302
+
303
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._init_step_index
304
+ def _init_step_index(self, timestep):
305
+ if self.begin_index is None:
306
+ if isinstance(timestep, torch.Tensor):
307
+ timestep = timestep.to(self.timesteps.device)
308
+ self._step_index = self.index_for_timestep(timestep)
309
+ else:
310
+ self._step_index = self._begin_index
311
311
 
312
312
  # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._sigma_to_t
313
313
  def _sigma_to_t(self, sigma, log_sigmas):
@@ -388,10 +388,6 @@ class KDPM2DiscreteScheduler(SchedulerMixin, ConfigMixin):
388
388
  if self.step_index is None:
389
389
  self._init_step_index(timestep)
390
390
 
391
- # advance index counter by 1
392
- timestep_int = timestep.cpu().item() if torch.is_tensor(timestep) else timestep
393
- self._index_counter[timestep_int] += 1
394
-
395
391
  if self.state_in_first_order:
396
392
  sigma = self.sigmas[self.step_index]
397
393
  sigma_interpol = self.sigmas_interpol[self.step_index + 1]
@@ -453,7 +449,7 @@ class KDPM2DiscreteScheduler(SchedulerMixin, ConfigMixin):
453
449
 
454
450
  return SchedulerOutput(prev_sample=prev_sample)
455
451
 
456
- # Copied from diffusers.schedulers.scheduling_heun_discrete.HeunDiscreteScheduler.add_noise
452
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.add_noise
457
453
  def add_noise(
458
454
  self,
459
455
  original_samples: torch.FloatTensor,
@@ -470,7 +466,11 @@ class KDPM2DiscreteScheduler(SchedulerMixin, ConfigMixin):
470
466
  schedule_timesteps = self.timesteps.to(original_samples.device)
471
467
  timesteps = timesteps.to(original_samples.device)
472
468
 
473
- step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timesteps]
469
+ # self.begin_index is None when scheduler is used for training, or pipeline does not implement set_begin_index
470
+ if self.begin_index is None:
471
+ step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timesteps]
472
+ else:
473
+ step_indices = [self.begin_index] * timesteps.shape[0]
474
474
 
475
475
  sigma = sigmas[step_indices].flatten()
476
476
  while len(sigma.shape) < len(original_samples.shape):
@@ -1,4 +1,4 @@
1
- # Copyright 2023 NVIDIA and The HuggingFace Team. All rights reserved.
1
+ # Copyright 2024 NVIDIA and The HuggingFace Team. All rights reserved.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright 2023 Stanford University Team and The HuggingFace Team. All rights reserved.
1
+ # Copyright 2024 Stanford University Team and The HuggingFace Team. All rights reserved.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -165,9 +165,7 @@ class LCMScheduler(SchedulerMixin, ConfigMixin):
165
165
  there is no previous alpha. When this option is `True` the previous alpha product is fixed to `1`,
166
166
  otherwise it uses the alpha value at step 0.
167
167
  steps_offset (`int`, defaults to 0):
168
- An offset added to the inference steps. You can use a combination of `offset=1` and
169
- `set_alpha_to_one=False` to make the last step use step 0 for the previous alpha product like in Stable
170
- Diffusion.
168
+ An offset added to the inference steps, as required by some model families.
171
169
  prediction_type (`str`, defaults to `epsilon`, *optional*):
172
170
  Prediction type of the scheduler function; can be `epsilon` (predicts the noise of the diffusion process),
173
171
  `sample` (directly predicts the noisy sample`) or `v_prediction` (see section 2.4 of [Imagen
@@ -250,29 +248,54 @@ class LCMScheduler(SchedulerMixin, ConfigMixin):
250
248
  self.custom_timesteps = False
251
249
 
252
250
  self._step_index = None
251
+ self._begin_index = None
253
252
 
254
- # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._init_step_index
255
- def _init_step_index(self, timestep):
256
- if isinstance(timestep, torch.Tensor):
257
- timestep = timestep.to(self.timesteps.device)
253
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.index_for_timestep
254
+ def index_for_timestep(self, timestep, schedule_timesteps=None):
255
+ if schedule_timesteps is None:
256
+ schedule_timesteps = self.timesteps
258
257
 
259
- index_candidates = (self.timesteps == timestep).nonzero()
258
+ indices = (schedule_timesteps == timestep).nonzero()
260
259
 
261
260
  # The sigma index that is taken for the **very** first `step`
262
261
  # is always the second index (or the last index if there is only 1)
263
262
  # This way we can ensure we don't accidentally skip a sigma in
264
263
  # case we start in the middle of the denoising schedule (e.g. for image-to-image)
265
- if len(index_candidates) > 1:
266
- step_index = index_candidates[1]
267
- else:
268
- step_index = index_candidates[0]
264
+ pos = 1 if len(indices) > 1 else 0
265
+
266
+ return indices[pos].item()
269
267
 
270
- self._step_index = step_index.item()
268
+ # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._init_step_index
269
+ def _init_step_index(self, timestep):
270
+ if self.begin_index is None:
271
+ if isinstance(timestep, torch.Tensor):
272
+ timestep = timestep.to(self.timesteps.device)
273
+ self._step_index = self.index_for_timestep(timestep)
274
+ else:
275
+ self._step_index = self._begin_index
271
276
 
272
277
  @property
273
278
  def step_index(self):
274
279
  return self._step_index
275
280
 
281
+ @property
282
+ def begin_index(self):
283
+ """
284
+ The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
285
+ """
286
+ return self._begin_index
287
+
288
+ # Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.set_begin_index
289
+ def set_begin_index(self, begin_index: int = 0):
290
+ """
291
+ Sets the begin index for the scheduler. This function should be run from pipeline before the inference.
292
+
293
+ Args:
294
+ begin_index (`int`):
295
+ The begin index for the scheduler.
296
+ """
297
+ self._begin_index = begin_index
298
+
276
299
  def scale_model_input(self, sample: torch.FloatTensor, timestep: Optional[int] = None) -> torch.FloatTensor:
277
300
  """
278
301
  Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
@@ -462,6 +485,7 @@ class LCMScheduler(SchedulerMixin, ConfigMixin):
462
485
  self.timesteps = torch.from_numpy(timesteps).to(device=device, dtype=torch.long)
463
486
 
464
487
  self._step_index = None
488
+ self._begin_index = None
465
489
 
466
490
  def get_scalings_for_boundary_condition_discrete(self, timestep):
467
491
  self.sigma_data = 0.5 # Default: 0.5