diffusers 0.32.2__py3-none-any.whl → 0.33.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (389) hide show
  1. diffusers/__init__.py +186 -3
  2. diffusers/configuration_utils.py +40 -12
  3. diffusers/dependency_versions_table.py +9 -2
  4. diffusers/hooks/__init__.py +9 -0
  5. diffusers/hooks/faster_cache.py +653 -0
  6. diffusers/hooks/group_offloading.py +793 -0
  7. diffusers/hooks/hooks.py +236 -0
  8. diffusers/hooks/layerwise_casting.py +245 -0
  9. diffusers/hooks/pyramid_attention_broadcast.py +311 -0
  10. diffusers/loaders/__init__.py +6 -0
  11. diffusers/loaders/ip_adapter.py +38 -30
  12. diffusers/loaders/lora_base.py +121 -86
  13. diffusers/loaders/lora_conversion_utils.py +504 -44
  14. diffusers/loaders/lora_pipeline.py +1769 -181
  15. diffusers/loaders/peft.py +167 -57
  16. diffusers/loaders/single_file.py +17 -2
  17. diffusers/loaders/single_file_model.py +53 -5
  18. diffusers/loaders/single_file_utils.py +646 -72
  19. diffusers/loaders/textual_inversion.py +9 -9
  20. diffusers/loaders/transformer_flux.py +8 -9
  21. diffusers/loaders/transformer_sd3.py +120 -39
  22. diffusers/loaders/unet.py +20 -7
  23. diffusers/models/__init__.py +22 -0
  24. diffusers/models/activations.py +9 -9
  25. diffusers/models/attention.py +0 -1
  26. diffusers/models/attention_processor.py +163 -25
  27. diffusers/models/auto_model.py +169 -0
  28. diffusers/models/autoencoders/__init__.py +2 -0
  29. diffusers/models/autoencoders/autoencoder_asym_kl.py +2 -0
  30. diffusers/models/autoencoders/autoencoder_dc.py +106 -4
  31. diffusers/models/autoencoders/autoencoder_kl.py +0 -4
  32. diffusers/models/autoencoders/autoencoder_kl_allegro.py +5 -23
  33. diffusers/models/autoencoders/autoencoder_kl_cogvideox.py +17 -55
  34. diffusers/models/autoencoders/autoencoder_kl_hunyuan_video.py +17 -97
  35. diffusers/models/autoencoders/autoencoder_kl_ltx.py +326 -107
  36. diffusers/models/autoencoders/autoencoder_kl_magvit.py +1094 -0
  37. diffusers/models/autoencoders/autoencoder_kl_mochi.py +21 -56
  38. diffusers/models/autoencoders/autoencoder_kl_temporal_decoder.py +11 -42
  39. diffusers/models/autoencoders/autoencoder_kl_wan.py +855 -0
  40. diffusers/models/autoencoders/autoencoder_oobleck.py +1 -0
  41. diffusers/models/autoencoders/autoencoder_tiny.py +0 -4
  42. diffusers/models/autoencoders/consistency_decoder_vae.py +3 -1
  43. diffusers/models/autoencoders/vae.py +31 -141
  44. diffusers/models/autoencoders/vq_model.py +3 -0
  45. diffusers/models/cache_utils.py +108 -0
  46. diffusers/models/controlnets/__init__.py +1 -0
  47. diffusers/models/controlnets/controlnet.py +3 -8
  48. diffusers/models/controlnets/controlnet_flux.py +14 -42
  49. diffusers/models/controlnets/controlnet_sd3.py +58 -34
  50. diffusers/models/controlnets/controlnet_sparsectrl.py +4 -7
  51. diffusers/models/controlnets/controlnet_union.py +27 -18
  52. diffusers/models/controlnets/controlnet_xs.py +7 -46
  53. diffusers/models/controlnets/multicontrolnet_union.py +196 -0
  54. diffusers/models/embeddings.py +18 -7
  55. diffusers/models/model_loading_utils.py +122 -80
  56. diffusers/models/modeling_flax_pytorch_utils.py +1 -1
  57. diffusers/models/modeling_flax_utils.py +1 -1
  58. diffusers/models/modeling_pytorch_flax_utils.py +1 -1
  59. diffusers/models/modeling_utils.py +617 -272
  60. diffusers/models/normalization.py +67 -14
  61. diffusers/models/resnet.py +1 -1
  62. diffusers/models/transformers/__init__.py +6 -0
  63. diffusers/models/transformers/auraflow_transformer_2d.py +9 -35
  64. diffusers/models/transformers/cogvideox_transformer_3d.py +13 -24
  65. diffusers/models/transformers/consisid_transformer_3d.py +789 -0
  66. diffusers/models/transformers/dit_transformer_2d.py +5 -19
  67. diffusers/models/transformers/hunyuan_transformer_2d.py +4 -3
  68. diffusers/models/transformers/latte_transformer_3d.py +20 -15
  69. diffusers/models/transformers/lumina_nextdit2d.py +3 -1
  70. diffusers/models/transformers/pixart_transformer_2d.py +4 -19
  71. diffusers/models/transformers/prior_transformer.py +5 -1
  72. diffusers/models/transformers/sana_transformer.py +144 -40
  73. diffusers/models/transformers/stable_audio_transformer.py +5 -20
  74. diffusers/models/transformers/transformer_2d.py +7 -22
  75. diffusers/models/transformers/transformer_allegro.py +9 -17
  76. diffusers/models/transformers/transformer_cogview3plus.py +6 -17
  77. diffusers/models/transformers/transformer_cogview4.py +462 -0
  78. diffusers/models/transformers/transformer_easyanimate.py +527 -0
  79. diffusers/models/transformers/transformer_flux.py +68 -110
  80. diffusers/models/transformers/transformer_hunyuan_video.py +404 -46
  81. diffusers/models/transformers/transformer_ltx.py +53 -35
  82. diffusers/models/transformers/transformer_lumina2.py +548 -0
  83. diffusers/models/transformers/transformer_mochi.py +6 -17
  84. diffusers/models/transformers/transformer_omnigen.py +469 -0
  85. diffusers/models/transformers/transformer_sd3.py +56 -86
  86. diffusers/models/transformers/transformer_temporal.py +5 -11
  87. diffusers/models/transformers/transformer_wan.py +469 -0
  88. diffusers/models/unets/unet_1d.py +3 -1
  89. diffusers/models/unets/unet_2d.py +21 -20
  90. diffusers/models/unets/unet_2d_blocks.py +19 -243
  91. diffusers/models/unets/unet_2d_condition.py +4 -6
  92. diffusers/models/unets/unet_3d_blocks.py +14 -127
  93. diffusers/models/unets/unet_3d_condition.py +8 -12
  94. diffusers/models/unets/unet_i2vgen_xl.py +5 -13
  95. diffusers/models/unets/unet_kandinsky3.py +0 -4
  96. diffusers/models/unets/unet_motion_model.py +20 -114
  97. diffusers/models/unets/unet_spatio_temporal_condition.py +7 -8
  98. diffusers/models/unets/unet_stable_cascade.py +8 -35
  99. diffusers/models/unets/uvit_2d.py +1 -4
  100. diffusers/optimization.py +2 -2
  101. diffusers/pipelines/__init__.py +57 -8
  102. diffusers/pipelines/allegro/pipeline_allegro.py +22 -2
  103. diffusers/pipelines/amused/pipeline_amused.py +15 -2
  104. diffusers/pipelines/amused/pipeline_amused_img2img.py +15 -2
  105. diffusers/pipelines/amused/pipeline_amused_inpaint.py +15 -2
  106. diffusers/pipelines/animatediff/pipeline_animatediff.py +15 -2
  107. diffusers/pipelines/animatediff/pipeline_animatediff_controlnet.py +15 -3
  108. diffusers/pipelines/animatediff/pipeline_animatediff_sdxl.py +24 -4
  109. diffusers/pipelines/animatediff/pipeline_animatediff_sparsectrl.py +15 -2
  110. diffusers/pipelines/animatediff/pipeline_animatediff_video2video.py +16 -4
  111. diffusers/pipelines/animatediff/pipeline_animatediff_video2video_controlnet.py +16 -4
  112. diffusers/pipelines/audioldm/pipeline_audioldm.py +13 -2
  113. diffusers/pipelines/audioldm2/modeling_audioldm2.py +13 -68
  114. diffusers/pipelines/audioldm2/pipeline_audioldm2.py +39 -9
  115. diffusers/pipelines/aura_flow/pipeline_aura_flow.py +63 -7
  116. diffusers/pipelines/auto_pipeline.py +35 -14
  117. diffusers/pipelines/blip_diffusion/blip_image_processing.py +1 -1
  118. diffusers/pipelines/blip_diffusion/modeling_blip2.py +5 -8
  119. diffusers/pipelines/blip_diffusion/pipeline_blip_diffusion.py +12 -0
  120. diffusers/pipelines/cogvideo/pipeline_cogvideox.py +22 -6
  121. diffusers/pipelines/cogvideo/pipeline_cogvideox_fun_control.py +22 -6
  122. diffusers/pipelines/cogvideo/pipeline_cogvideox_image2video.py +22 -5
  123. diffusers/pipelines/cogvideo/pipeline_cogvideox_video2video.py +22 -6
  124. diffusers/pipelines/cogview3/pipeline_cogview3plus.py +12 -4
  125. diffusers/pipelines/cogview4/__init__.py +49 -0
  126. diffusers/pipelines/cogview4/pipeline_cogview4.py +684 -0
  127. diffusers/pipelines/cogview4/pipeline_cogview4_control.py +732 -0
  128. diffusers/pipelines/cogview4/pipeline_output.py +21 -0
  129. diffusers/pipelines/consisid/__init__.py +49 -0
  130. diffusers/pipelines/consisid/consisid_utils.py +357 -0
  131. diffusers/pipelines/consisid/pipeline_consisid.py +974 -0
  132. diffusers/pipelines/consisid/pipeline_output.py +20 -0
  133. diffusers/pipelines/consistency_models/pipeline_consistency_models.py +11 -0
  134. diffusers/pipelines/controlnet/pipeline_controlnet.py +6 -5
  135. diffusers/pipelines/controlnet/pipeline_controlnet_blip_diffusion.py +13 -0
  136. diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +17 -5
  137. diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +31 -12
  138. diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +26 -7
  139. diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +20 -3
  140. diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +22 -3
  141. diffusers/pipelines/controlnet/pipeline_controlnet_union_inpaint_sd_xl.py +26 -25
  142. diffusers/pipelines/controlnet/pipeline_controlnet_union_sd_xl.py +224 -109
  143. diffusers/pipelines/controlnet/pipeline_controlnet_union_sd_xl_img2img.py +25 -29
  144. diffusers/pipelines/controlnet/pipeline_flax_controlnet.py +7 -4
  145. diffusers/pipelines/controlnet_hunyuandit/pipeline_hunyuandit_controlnet.py +3 -5
  146. diffusers/pipelines/controlnet_sd3/pipeline_stable_diffusion_3_controlnet.py +121 -10
  147. diffusers/pipelines/controlnet_sd3/pipeline_stable_diffusion_3_controlnet_inpainting.py +122 -11
  148. diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py +12 -1
  149. diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py +20 -3
  150. diffusers/pipelines/dance_diffusion/pipeline_dance_diffusion.py +14 -2
  151. diffusers/pipelines/ddim/pipeline_ddim.py +14 -1
  152. diffusers/pipelines/ddpm/pipeline_ddpm.py +15 -1
  153. diffusers/pipelines/deepfloyd_if/pipeline_if.py +12 -0
  154. diffusers/pipelines/deepfloyd_if/pipeline_if_img2img.py +12 -0
  155. diffusers/pipelines/deepfloyd_if/pipeline_if_img2img_superresolution.py +14 -1
  156. diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.py +12 -0
  157. diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting_superresolution.py +14 -1
  158. diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.py +14 -1
  159. diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion.py +11 -7
  160. diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion_img2img.py +11 -7
  161. diffusers/pipelines/deprecated/repaint/pipeline_repaint.py +1 -1
  162. diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_cycle_diffusion.py +10 -6
  163. diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_onnx_stable_diffusion_inpaint_legacy.py +2 -2
  164. diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_inpaint_legacy.py +11 -7
  165. diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_model_editing.py +1 -1
  166. diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_paradigms.py +1 -1
  167. diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_pix2pix_zero.py +1 -1
  168. diffusers/pipelines/deprecated/versatile_diffusion/modeling_text_unet.py +10 -105
  169. diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion.py +1 -1
  170. diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_dual_guided.py +1 -1
  171. diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_image_variation.py +1 -1
  172. diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_text_to_image.py +1 -1
  173. diffusers/pipelines/dit/pipeline_dit.py +15 -2
  174. diffusers/pipelines/easyanimate/__init__.py +52 -0
  175. diffusers/pipelines/easyanimate/pipeline_easyanimate.py +770 -0
  176. diffusers/pipelines/easyanimate/pipeline_easyanimate_control.py +994 -0
  177. diffusers/pipelines/easyanimate/pipeline_easyanimate_inpaint.py +1234 -0
  178. diffusers/pipelines/easyanimate/pipeline_output.py +20 -0
  179. diffusers/pipelines/flux/pipeline_flux.py +53 -21
  180. diffusers/pipelines/flux/pipeline_flux_control.py +9 -12
  181. diffusers/pipelines/flux/pipeline_flux_control_img2img.py +6 -10
  182. diffusers/pipelines/flux/pipeline_flux_control_inpaint.py +8 -10
  183. diffusers/pipelines/flux/pipeline_flux_controlnet.py +185 -13
  184. diffusers/pipelines/flux/pipeline_flux_controlnet_image_to_image.py +8 -10
  185. diffusers/pipelines/flux/pipeline_flux_controlnet_inpainting.py +16 -16
  186. diffusers/pipelines/flux/pipeline_flux_fill.py +107 -39
  187. diffusers/pipelines/flux/pipeline_flux_img2img.py +193 -15
  188. diffusers/pipelines/flux/pipeline_flux_inpaint.py +199 -19
  189. diffusers/pipelines/free_noise_utils.py +3 -3
  190. diffusers/pipelines/hunyuan_video/__init__.py +4 -0
  191. diffusers/pipelines/hunyuan_video/pipeline_hunyuan_skyreels_image2video.py +804 -0
  192. diffusers/pipelines/hunyuan_video/pipeline_hunyuan_video.py +90 -23
  193. diffusers/pipelines/hunyuan_video/pipeline_hunyuan_video_image2video.py +924 -0
  194. diffusers/pipelines/hunyuandit/pipeline_hunyuandit.py +3 -5
  195. diffusers/pipelines/i2vgen_xl/pipeline_i2vgen_xl.py +13 -1
  196. diffusers/pipelines/kandinsky/pipeline_kandinsky.py +12 -0
  197. diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py +1 -1
  198. diffusers/pipelines/kandinsky/pipeline_kandinsky_img2img.py +12 -0
  199. diffusers/pipelines/kandinsky/pipeline_kandinsky_inpaint.py +13 -1
  200. diffusers/pipelines/kandinsky/pipeline_kandinsky_prior.py +12 -0
  201. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py +12 -1
  202. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_controlnet.py +13 -0
  203. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_controlnet_img2img.py +12 -0
  204. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py +12 -1
  205. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpainting.py +12 -1
  206. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior.py +12 -0
  207. diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior_emb2emb.py +12 -0
  208. diffusers/pipelines/kandinsky3/pipeline_kandinsky3.py +12 -0
  209. diffusers/pipelines/kandinsky3/pipeline_kandinsky3_img2img.py +12 -0
  210. diffusers/pipelines/kolors/pipeline_kolors.py +10 -8
  211. diffusers/pipelines/kolors/pipeline_kolors_img2img.py +6 -4
  212. diffusers/pipelines/kolors/text_encoder.py +7 -34
  213. diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +12 -1
  214. diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +13 -1
  215. diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py +14 -13
  216. diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion_superresolution.py +12 -1
  217. diffusers/pipelines/latte/pipeline_latte.py +36 -7
  218. diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion.py +67 -13
  219. diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion_xl.py +60 -15
  220. diffusers/pipelines/ltx/__init__.py +2 -0
  221. diffusers/pipelines/ltx/pipeline_ltx.py +25 -13
  222. diffusers/pipelines/ltx/pipeline_ltx_condition.py +1194 -0
  223. diffusers/pipelines/ltx/pipeline_ltx_image2video.py +31 -17
  224. diffusers/pipelines/lumina/__init__.py +2 -2
  225. diffusers/pipelines/lumina/pipeline_lumina.py +83 -20
  226. diffusers/pipelines/lumina2/__init__.py +48 -0
  227. diffusers/pipelines/lumina2/pipeline_lumina2.py +790 -0
  228. diffusers/pipelines/marigold/__init__.py +2 -0
  229. diffusers/pipelines/marigold/marigold_image_processing.py +127 -14
  230. diffusers/pipelines/marigold/pipeline_marigold_depth.py +31 -16
  231. diffusers/pipelines/marigold/pipeline_marigold_intrinsics.py +721 -0
  232. diffusers/pipelines/marigold/pipeline_marigold_normals.py +31 -16
  233. diffusers/pipelines/mochi/pipeline_mochi.py +14 -18
  234. diffusers/pipelines/musicldm/pipeline_musicldm.py +16 -1
  235. diffusers/pipelines/omnigen/__init__.py +50 -0
  236. diffusers/pipelines/omnigen/pipeline_omnigen.py +512 -0
  237. diffusers/pipelines/omnigen/processor_omnigen.py +327 -0
  238. diffusers/pipelines/onnx_utils.py +5 -3
  239. diffusers/pipelines/pag/pag_utils.py +1 -1
  240. diffusers/pipelines/pag/pipeline_pag_controlnet_sd.py +12 -1
  241. diffusers/pipelines/pag/pipeline_pag_controlnet_sd_inpaint.py +15 -4
  242. diffusers/pipelines/pag/pipeline_pag_controlnet_sd_xl.py +20 -3
  243. diffusers/pipelines/pag/pipeline_pag_controlnet_sd_xl_img2img.py +20 -3
  244. diffusers/pipelines/pag/pipeline_pag_hunyuandit.py +1 -3
  245. diffusers/pipelines/pag/pipeline_pag_kolors.py +6 -4
  246. diffusers/pipelines/pag/pipeline_pag_pixart_sigma.py +16 -3
  247. diffusers/pipelines/pag/pipeline_pag_sana.py +65 -8
  248. diffusers/pipelines/pag/pipeline_pag_sd.py +23 -7
  249. diffusers/pipelines/pag/pipeline_pag_sd_3.py +3 -5
  250. diffusers/pipelines/pag/pipeline_pag_sd_3_img2img.py +3 -5
  251. diffusers/pipelines/pag/pipeline_pag_sd_animatediff.py +13 -1
  252. diffusers/pipelines/pag/pipeline_pag_sd_img2img.py +23 -7
  253. diffusers/pipelines/pag/pipeline_pag_sd_inpaint.py +26 -10
  254. diffusers/pipelines/pag/pipeline_pag_sd_xl.py +12 -4
  255. diffusers/pipelines/pag/pipeline_pag_sd_xl_img2img.py +7 -3
  256. diffusers/pipelines/pag/pipeline_pag_sd_xl_inpaint.py +10 -6
  257. diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py +13 -3
  258. diffusers/pipelines/pia/pipeline_pia.py +13 -1
  259. diffusers/pipelines/pipeline_flax_utils.py +7 -7
  260. diffusers/pipelines/pipeline_loading_utils.py +193 -83
  261. diffusers/pipelines/pipeline_utils.py +221 -106
  262. diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +17 -5
  263. diffusers/pipelines/pixart_alpha/pipeline_pixart_sigma.py +17 -4
  264. diffusers/pipelines/sana/__init__.py +2 -0
  265. diffusers/pipelines/sana/pipeline_sana.py +183 -58
  266. diffusers/pipelines/sana/pipeline_sana_sprint.py +889 -0
  267. diffusers/pipelines/semantic_stable_diffusion/pipeline_semantic_stable_diffusion.py +12 -2
  268. diffusers/pipelines/shap_e/pipeline_shap_e.py +12 -0
  269. diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py +12 -0
  270. diffusers/pipelines/shap_e/renderer.py +6 -6
  271. diffusers/pipelines/stable_audio/pipeline_stable_audio.py +1 -1
  272. diffusers/pipelines/stable_cascade/pipeline_stable_cascade.py +15 -4
  273. diffusers/pipelines/stable_cascade/pipeline_stable_cascade_combined.py +12 -8
  274. diffusers/pipelines/stable_cascade/pipeline_stable_cascade_prior.py +12 -1
  275. diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +3 -2
  276. diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion.py +14 -10
  277. diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion_img2img.py +3 -3
  278. diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion_inpaint.py +14 -10
  279. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion.py +2 -2
  280. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_img2img.py +4 -3
  281. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_inpaint.py +5 -4
  282. diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_upscale.py +2 -2
  283. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +18 -13
  284. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_depth2img.py +30 -8
  285. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_image_variation.py +24 -10
  286. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +28 -12
  287. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +39 -18
  288. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +17 -6
  289. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_latent_upscale.py +13 -3
  290. diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +20 -3
  291. diffusers/pipelines/stable_diffusion/pipeline_stable_unclip.py +14 -2
  292. diffusers/pipelines/stable_diffusion/pipeline_stable_unclip_img2img.py +13 -1
  293. diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py +16 -17
  294. diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_img2img.py +136 -18
  295. diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_inpaint.py +150 -21
  296. diffusers/pipelines/stable_diffusion_attend_and_excite/pipeline_stable_diffusion_attend_and_excite.py +15 -3
  297. diffusers/pipelines/stable_diffusion_diffedit/pipeline_stable_diffusion_diffedit.py +26 -11
  298. diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen.py +15 -3
  299. diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py +22 -4
  300. diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_k_diffusion.py +30 -13
  301. diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_xl_k_diffusion.py +12 -4
  302. diffusers/pipelines/stable_diffusion_ldm3d/pipeline_stable_diffusion_ldm3d.py +15 -3
  303. diffusers/pipelines/stable_diffusion_panorama/pipeline_stable_diffusion_panorama.py +15 -3
  304. diffusers/pipelines/stable_diffusion_safe/pipeline_stable_diffusion_safe.py +26 -12
  305. diffusers/pipelines/stable_diffusion_sag/pipeline_stable_diffusion_sag.py +16 -4
  306. diffusers/pipelines/stable_diffusion_xl/pipeline_flax_stable_diffusion_xl.py +1 -1
  307. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +12 -4
  308. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +7 -3
  309. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +10 -6
  310. diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +11 -4
  311. diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +13 -2
  312. diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_adapter.py +18 -4
  313. diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +26 -5
  314. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +13 -1
  315. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +13 -1
  316. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +28 -6
  317. diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +26 -4
  318. diffusers/pipelines/transformers_loading_utils.py +121 -0
  319. diffusers/pipelines/unclip/pipeline_unclip.py +11 -1
  320. diffusers/pipelines/unclip/pipeline_unclip_image_variation.py +11 -1
  321. diffusers/pipelines/unidiffuser/pipeline_unidiffuser.py +19 -2
  322. diffusers/pipelines/wan/__init__.py +51 -0
  323. diffusers/pipelines/wan/pipeline_output.py +20 -0
  324. diffusers/pipelines/wan/pipeline_wan.py +593 -0
  325. diffusers/pipelines/wan/pipeline_wan_i2v.py +722 -0
  326. diffusers/pipelines/wan/pipeline_wan_video2video.py +725 -0
  327. diffusers/pipelines/wuerstchen/modeling_wuerstchen_prior.py +7 -31
  328. diffusers/pipelines/wuerstchen/pipeline_wuerstchen.py +12 -1
  329. diffusers/pipelines/wuerstchen/pipeline_wuerstchen_prior.py +12 -1
  330. diffusers/quantizers/auto.py +5 -1
  331. diffusers/quantizers/base.py +5 -9
  332. diffusers/quantizers/bitsandbytes/bnb_quantizer.py +41 -29
  333. diffusers/quantizers/bitsandbytes/utils.py +30 -20
  334. diffusers/quantizers/gguf/gguf_quantizer.py +1 -0
  335. diffusers/quantizers/gguf/utils.py +4 -2
  336. diffusers/quantizers/quantization_config.py +59 -4
  337. diffusers/quantizers/quanto/__init__.py +1 -0
  338. diffusers/quantizers/quanto/quanto_quantizer.py +177 -0
  339. diffusers/quantizers/quanto/utils.py +60 -0
  340. diffusers/quantizers/torchao/__init__.py +1 -1
  341. diffusers/quantizers/torchao/torchao_quantizer.py +47 -2
  342. diffusers/schedulers/__init__.py +2 -1
  343. diffusers/schedulers/scheduling_consistency_models.py +1 -2
  344. diffusers/schedulers/scheduling_ddim_inverse.py +1 -1
  345. diffusers/schedulers/scheduling_ddpm.py +2 -3
  346. diffusers/schedulers/scheduling_ddpm_parallel.py +1 -2
  347. diffusers/schedulers/scheduling_dpmsolver_multistep.py +12 -4
  348. diffusers/schedulers/scheduling_edm_euler.py +45 -10
  349. diffusers/schedulers/scheduling_flow_match_euler_discrete.py +116 -28
  350. diffusers/schedulers/scheduling_flow_match_heun_discrete.py +7 -6
  351. diffusers/schedulers/scheduling_heun_discrete.py +1 -1
  352. diffusers/schedulers/scheduling_lcm.py +1 -2
  353. diffusers/schedulers/scheduling_lms_discrete.py +1 -1
  354. diffusers/schedulers/scheduling_repaint.py +5 -1
  355. diffusers/schedulers/scheduling_scm.py +265 -0
  356. diffusers/schedulers/scheduling_tcd.py +1 -2
  357. diffusers/schedulers/scheduling_utils.py +2 -1
  358. diffusers/training_utils.py +14 -7
  359. diffusers/utils/__init__.py +9 -1
  360. diffusers/utils/constants.py +13 -1
  361. diffusers/utils/deprecation_utils.py +1 -1
  362. diffusers/utils/dummy_bitsandbytes_objects.py +17 -0
  363. diffusers/utils/dummy_gguf_objects.py +17 -0
  364. diffusers/utils/dummy_optimum_quanto_objects.py +17 -0
  365. diffusers/utils/dummy_pt_objects.py +233 -0
  366. diffusers/utils/dummy_torch_and_transformers_and_opencv_objects.py +17 -0
  367. diffusers/utils/dummy_torch_and_transformers_objects.py +270 -0
  368. diffusers/utils/dummy_torchao_objects.py +17 -0
  369. diffusers/utils/dynamic_modules_utils.py +1 -1
  370. diffusers/utils/export_utils.py +28 -3
  371. diffusers/utils/hub_utils.py +52 -102
  372. diffusers/utils/import_utils.py +121 -221
  373. diffusers/utils/loading_utils.py +2 -1
  374. diffusers/utils/logging.py +1 -2
  375. diffusers/utils/peft_utils.py +6 -14
  376. diffusers/utils/remote_utils.py +425 -0
  377. diffusers/utils/source_code_parsing_utils.py +52 -0
  378. diffusers/utils/state_dict_utils.py +15 -1
  379. diffusers/utils/testing_utils.py +243 -13
  380. diffusers/utils/torch_utils.py +10 -0
  381. diffusers/utils/typing_utils.py +91 -0
  382. diffusers/video_processor.py +1 -1
  383. {diffusers-0.32.2.dist-info → diffusers-0.33.0.dist-info}/METADATA +76 -44
  384. diffusers-0.33.0.dist-info/RECORD +608 -0
  385. {diffusers-0.32.2.dist-info → diffusers-0.33.0.dist-info}/WHEEL +1 -1
  386. diffusers-0.32.2.dist-info/RECORD +0 -550
  387. {diffusers-0.32.2.dist-info → diffusers-0.33.0.dist-info}/LICENSE +0 -0
  388. {diffusers-0.32.2.dist-info → diffusers-0.33.0.dist-info}/entry_points.txt +0 -0
  389. {diffusers-0.32.2.dist-info → diffusers-0.33.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,1194 @@
1
+ # Copyright 2024 Lightricks and The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import inspect
16
+ from dataclasses import dataclass
17
+ from typing import Any, Callable, Dict, List, Optional, Tuple, Union
18
+
19
+ import PIL.Image
20
+ import torch
21
+ from transformers import T5EncoderModel, T5TokenizerFast
22
+
23
+ from ...callbacks import MultiPipelineCallbacks, PipelineCallback
24
+ from ...image_processor import PipelineImageInput
25
+ from ...loaders import FromSingleFileMixin, LTXVideoLoraLoaderMixin
26
+ from ...models.autoencoders import AutoencoderKLLTXVideo
27
+ from ...models.transformers import LTXVideoTransformer3DModel
28
+ from ...schedulers import FlowMatchEulerDiscreteScheduler
29
+ from ...utils import is_torch_xla_available, logging, replace_example_docstring
30
+ from ...utils.torch_utils import randn_tensor
31
+ from ...video_processor import VideoProcessor
32
+ from ..pipeline_utils import DiffusionPipeline
33
+ from .pipeline_output import LTXPipelineOutput
34
+
35
+
36
+ if is_torch_xla_available():
37
+ import torch_xla.core.xla_model as xm
38
+
39
+ XLA_AVAILABLE = True
40
+ else:
41
+ XLA_AVAILABLE = False
42
+
43
+ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
44
+
45
+ EXAMPLE_DOC_STRING = """
46
+ Examples:
47
+ ```py
48
+ >>> import torch
49
+ >>> from diffusers.pipelines.ltx.pipeline_ltx_condition import LTXConditionPipeline, LTXVideoCondition
50
+ >>> from diffusers.utils import export_to_video, load_video, load_image
51
+
52
+ >>> pipe = LTXConditionPipeline.from_pretrained("Lightricks/LTX-Video-0.9.5", torch_dtype=torch.bfloat16)
53
+ >>> pipe.to("cuda")
54
+
55
+ >>> # Load input image and video
56
+ >>> video = load_video(
57
+ ... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cosmos/cosmos-video2world-input-vid.mp4"
58
+ ... )
59
+ >>> image = load_image(
60
+ ... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cosmos/cosmos-video2world-input.jpg"
61
+ ... )
62
+
63
+ >>> # Create conditioning objects
64
+ >>> condition1 = LTXVideoCondition(
65
+ ... image=image,
66
+ ... frame_index=0,
67
+ ... )
68
+ >>> condition2 = LTXVideoCondition(
69
+ ... video=video,
70
+ ... frame_index=80,
71
+ ... )
72
+
73
+ >>> prompt = "The video depicts a long, straight highway stretching into the distance, flanked by metal guardrails. The road is divided into multiple lanes, with a few vehicles visible in the far distance. The surrounding landscape features dry, grassy fields on one side and rolling hills on the other. The sky is mostly clear with a few scattered clouds, suggesting a bright, sunny day. And then the camera switch to a winding mountain road covered in snow, with a single vehicle traveling along it. The road is flanked by steep, rocky cliffs and sparse vegetation. The landscape is characterized by rugged terrain and a river visible in the distance. The scene captures the solitude and beauty of a winter drive through a mountainous region."
74
+ >>> negative_prompt = "worst quality, inconsistent motion, blurry, jittery, distorted"
75
+
76
+ >>> # Generate video
77
+ >>> generator = torch.Generator("cuda").manual_seed(0)
78
+ >>> # Text-only conditioning is also supported without the need to pass `conditions`
79
+ >>> video = pipe(
80
+ ... conditions=[condition1, condition2],
81
+ ... prompt=prompt,
82
+ ... negative_prompt=negative_prompt,
83
+ ... width=768,
84
+ ... height=512,
85
+ ... num_frames=161,
86
+ ... num_inference_steps=40,
87
+ ... generator=generator,
88
+ ... ).frames[0]
89
+
90
+ >>> export_to_video(video, "output.mp4", fps=24)
91
+ ```
92
+ """
93
+
94
+
95
+ @dataclass
96
+ class LTXVideoCondition:
97
+ """
98
+ Defines a single frame-conditioning item for LTX Video - a single frame or a sequence of frames.
99
+
100
+ Attributes:
101
+ image (`PIL.Image.Image`):
102
+ The image to condition the video on.
103
+ video (`List[PIL.Image.Image]`):
104
+ The video to condition the video on.
105
+ frame_index (`int`):
106
+ The frame index at which the image or video will conditionally effect the video generation.
107
+ strength (`float`, defaults to `1.0`):
108
+ The strength of the conditioning effect. A value of `1.0` means the conditioning effect is fully applied.
109
+ """
110
+
111
+ image: Optional[PIL.Image.Image] = None
112
+ video: Optional[List[PIL.Image.Image]] = None
113
+ frame_index: int = 0
114
+ strength: float = 1.0
115
+
116
+
117
+ # from LTX-Video/ltx_video/schedulers/rf.py
118
+ def linear_quadratic_schedule(num_steps, threshold_noise=0.025, linear_steps=None):
119
+ if linear_steps is None:
120
+ linear_steps = num_steps // 2
121
+ if num_steps < 2:
122
+ return torch.tensor([1.0])
123
+ linear_sigma_schedule = [i * threshold_noise / linear_steps for i in range(linear_steps)]
124
+ threshold_noise_step_diff = linear_steps - threshold_noise * num_steps
125
+ quadratic_steps = num_steps - linear_steps
126
+ quadratic_coef = threshold_noise_step_diff / (linear_steps * quadratic_steps**2)
127
+ linear_coef = threshold_noise / linear_steps - 2 * threshold_noise_step_diff / (quadratic_steps**2)
128
+ const = quadratic_coef * (linear_steps**2)
129
+ quadratic_sigma_schedule = [
130
+ quadratic_coef * (i**2) + linear_coef * i + const for i in range(linear_steps, num_steps)
131
+ ]
132
+ sigma_schedule = linear_sigma_schedule + quadratic_sigma_schedule + [1.0]
133
+ sigma_schedule = [1.0 - x for x in sigma_schedule]
134
+ return torch.tensor(sigma_schedule[:-1])
135
+
136
+
137
+ # Copied from diffusers.pipelines.flux.pipeline_flux.calculate_shift
138
+ def calculate_shift(
139
+ image_seq_len,
140
+ base_seq_len: int = 256,
141
+ max_seq_len: int = 4096,
142
+ base_shift: float = 0.5,
143
+ max_shift: float = 1.15,
144
+ ):
145
+ m = (max_shift - base_shift) / (max_seq_len - base_seq_len)
146
+ b = base_shift - m * base_seq_len
147
+ mu = image_seq_len * m + b
148
+ return mu
149
+
150
+
151
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps
152
+ def retrieve_timesteps(
153
+ scheduler,
154
+ num_inference_steps: Optional[int] = None,
155
+ device: Optional[Union[str, torch.device]] = None,
156
+ timesteps: Optional[List[int]] = None,
157
+ sigmas: Optional[List[float]] = None,
158
+ **kwargs,
159
+ ):
160
+ r"""
161
+ Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
162
+ custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.
163
+
164
+ Args:
165
+ scheduler (`SchedulerMixin`):
166
+ The scheduler to get timesteps from.
167
+ num_inference_steps (`int`):
168
+ The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
169
+ must be `None`.
170
+ device (`str` or `torch.device`, *optional*):
171
+ The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
172
+ timesteps (`List[int]`, *optional*):
173
+ Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
174
+ `num_inference_steps` and `sigmas` must be `None`.
175
+ sigmas (`List[float]`, *optional*):
176
+ Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
177
+ `num_inference_steps` and `timesteps` must be `None`.
178
+
179
+ Returns:
180
+ `Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
181
+ second element is the number of inference steps.
182
+ """
183
+ if timesteps is not None and sigmas is not None:
184
+ raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values")
185
+ if timesteps is not None:
186
+ accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
187
+ if not accepts_timesteps:
188
+ raise ValueError(
189
+ f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
190
+ f" timestep schedules. Please check whether you are using the correct scheduler."
191
+ )
192
+ scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
193
+ timesteps = scheduler.timesteps
194
+ num_inference_steps = len(timesteps)
195
+ elif sigmas is not None:
196
+ accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
197
+ if not accept_sigmas:
198
+ raise ValueError(
199
+ f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
200
+ f" sigmas schedules. Please check whether you are using the correct scheduler."
201
+ )
202
+ scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
203
+ timesteps = scheduler.timesteps
204
+ num_inference_steps = len(timesteps)
205
+ else:
206
+ scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
207
+ timesteps = scheduler.timesteps
208
+ return timesteps, num_inference_steps
209
+
210
+
211
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents
212
+ def retrieve_latents(
213
+ encoder_output: torch.Tensor, generator: Optional[torch.Generator] = None, sample_mode: str = "sample"
214
+ ):
215
+ if hasattr(encoder_output, "latent_dist") and sample_mode == "sample":
216
+ return encoder_output.latent_dist.sample(generator)
217
+ elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax":
218
+ return encoder_output.latent_dist.mode()
219
+ elif hasattr(encoder_output, "latents"):
220
+ return encoder_output.latents
221
+ else:
222
+ raise AttributeError("Could not access latents of provided encoder_output")
223
+
224
+
225
+ class LTXConditionPipeline(DiffusionPipeline, FromSingleFileMixin, LTXVideoLoraLoaderMixin):
226
+ r"""
227
+ Pipeline for text/image/video-to-video generation.
228
+
229
+ Reference: https://github.com/Lightricks/LTX-Video
230
+
231
+ Args:
232
+ transformer ([`LTXVideoTransformer3DModel`]):
233
+ Conditional Transformer architecture to denoise the encoded video latents.
234
+ scheduler ([`FlowMatchEulerDiscreteScheduler`]):
235
+ A scheduler to be used in combination with `transformer` to denoise the encoded image latents.
236
+ vae ([`AutoencoderKLLTXVideo`]):
237
+ Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
238
+ text_encoder ([`T5EncoderModel`]):
239
+ [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5EncoderModel), specifically
240
+ the [google/t5-v1_1-xxl](https://huggingface.co/google/t5-v1_1-xxl) variant.
241
+ tokenizer (`CLIPTokenizer`):
242
+ Tokenizer of class
243
+ [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer).
244
+ tokenizer (`T5TokenizerFast`):
245
+ Second Tokenizer of class
246
+ [T5TokenizerFast](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5TokenizerFast).
247
+ """
248
+
249
+ model_cpu_offload_seq = "text_encoder->transformer->vae"
250
+ _optional_components = []
251
+ _callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"]
252
+
253
+ def __init__(
254
+ self,
255
+ scheduler: FlowMatchEulerDiscreteScheduler,
256
+ vae: AutoencoderKLLTXVideo,
257
+ text_encoder: T5EncoderModel,
258
+ tokenizer: T5TokenizerFast,
259
+ transformer: LTXVideoTransformer3DModel,
260
+ ):
261
+ super().__init__()
262
+
263
+ self.register_modules(
264
+ vae=vae,
265
+ text_encoder=text_encoder,
266
+ tokenizer=tokenizer,
267
+ transformer=transformer,
268
+ scheduler=scheduler,
269
+ )
270
+
271
+ self.vae_spatial_compression_ratio = (
272
+ self.vae.spatial_compression_ratio if getattr(self, "vae", None) is not None else 32
273
+ )
274
+ self.vae_temporal_compression_ratio = (
275
+ self.vae.temporal_compression_ratio if getattr(self, "vae", None) is not None else 8
276
+ )
277
+ self.transformer_spatial_patch_size = (
278
+ self.transformer.config.patch_size if getattr(self, "transformer", None) is not None else 1
279
+ )
280
+ self.transformer_temporal_patch_size = (
281
+ self.transformer.config.patch_size_t if getattr(self, "transformer") is not None else 1
282
+ )
283
+
284
+ self.video_processor = VideoProcessor(vae_scale_factor=self.vae_spatial_compression_ratio)
285
+ self.tokenizer_max_length = (
286
+ self.tokenizer.model_max_length if getattr(self, "tokenizer", None) is not None else 128
287
+ )
288
+
289
+ self.default_height = 512
290
+ self.default_width = 704
291
+ self.default_frames = 121
292
+
293
+ def _get_t5_prompt_embeds(
294
+ self,
295
+ prompt: Union[str, List[str]] = None,
296
+ num_videos_per_prompt: int = 1,
297
+ max_sequence_length: int = 256,
298
+ device: Optional[torch.device] = None,
299
+ dtype: Optional[torch.dtype] = None,
300
+ ):
301
+ device = device or self._execution_device
302
+ dtype = dtype or self.text_encoder.dtype
303
+
304
+ prompt = [prompt] if isinstance(prompt, str) else prompt
305
+ batch_size = len(prompt)
306
+
307
+ text_inputs = self.tokenizer(
308
+ prompt,
309
+ padding="max_length",
310
+ max_length=max_sequence_length,
311
+ truncation=True,
312
+ add_special_tokens=True,
313
+ return_tensors="pt",
314
+ )
315
+ text_input_ids = text_inputs.input_ids
316
+ prompt_attention_mask = text_inputs.attention_mask
317
+ prompt_attention_mask = prompt_attention_mask.bool().to(device)
318
+
319
+ untruncated_ids = self.tokenizer(prompt, padding="longest", return_tensors="pt").input_ids
320
+
321
+ if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
322
+ removed_text = self.tokenizer.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
323
+ logger.warning(
324
+ "The following part of your input was truncated because `max_sequence_length` is set to "
325
+ f" {max_sequence_length} tokens: {removed_text}"
326
+ )
327
+
328
+ prompt_embeds = self.text_encoder(text_input_ids.to(device), attention_mask=prompt_attention_mask)[0]
329
+ prompt_embeds = prompt_embeds.to(dtype=dtype, device=device)
330
+
331
+ # duplicate text embeddings for each generation per prompt, using mps friendly method
332
+ _, seq_len, _ = prompt_embeds.shape
333
+ prompt_embeds = prompt_embeds.repeat(1, num_videos_per_prompt, 1)
334
+ prompt_embeds = prompt_embeds.view(batch_size * num_videos_per_prompt, seq_len, -1)
335
+
336
+ prompt_attention_mask = prompt_attention_mask.view(batch_size, -1)
337
+ prompt_attention_mask = prompt_attention_mask.repeat(num_videos_per_prompt, 1)
338
+
339
+ return prompt_embeds, prompt_attention_mask
340
+
341
+ # Copied from diffusers.pipelines.mochi.pipeline_mochi.MochiPipeline.encode_prompt
342
+ def encode_prompt(
343
+ self,
344
+ prompt: Union[str, List[str]],
345
+ negative_prompt: Optional[Union[str, List[str]]] = None,
346
+ do_classifier_free_guidance: bool = True,
347
+ num_videos_per_prompt: int = 1,
348
+ prompt_embeds: Optional[torch.Tensor] = None,
349
+ negative_prompt_embeds: Optional[torch.Tensor] = None,
350
+ prompt_attention_mask: Optional[torch.Tensor] = None,
351
+ negative_prompt_attention_mask: Optional[torch.Tensor] = None,
352
+ max_sequence_length: int = 256,
353
+ device: Optional[torch.device] = None,
354
+ dtype: Optional[torch.dtype] = None,
355
+ ):
356
+ r"""
357
+ Encodes the prompt into text encoder hidden states.
358
+
359
+ Args:
360
+ prompt (`str` or `List[str]`, *optional*):
361
+ prompt to be encoded
362
+ negative_prompt (`str` or `List[str]`, *optional*):
363
+ The prompt or prompts not to guide the image generation. If not defined, one has to pass
364
+ `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
365
+ less than `1`).
366
+ do_classifier_free_guidance (`bool`, *optional*, defaults to `True`):
367
+ Whether to use classifier free guidance or not.
368
+ num_videos_per_prompt (`int`, *optional*, defaults to 1):
369
+ Number of videos that should be generated per prompt. torch device to place the resulting embeddings on
370
+ prompt_embeds (`torch.Tensor`, *optional*):
371
+ Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
372
+ provided, text embeddings will be generated from `prompt` input argument.
373
+ negative_prompt_embeds (`torch.Tensor`, *optional*):
374
+ Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
375
+ weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
376
+ argument.
377
+ device: (`torch.device`, *optional*):
378
+ torch device
379
+ dtype: (`torch.dtype`, *optional*):
380
+ torch dtype
381
+ """
382
+ device = device or self._execution_device
383
+
384
+ prompt = [prompt] if isinstance(prompt, str) else prompt
385
+ if prompt is not None:
386
+ batch_size = len(prompt)
387
+ else:
388
+ batch_size = prompt_embeds.shape[0]
389
+
390
+ if prompt_embeds is None:
391
+ prompt_embeds, prompt_attention_mask = self._get_t5_prompt_embeds(
392
+ prompt=prompt,
393
+ num_videos_per_prompt=num_videos_per_prompt,
394
+ max_sequence_length=max_sequence_length,
395
+ device=device,
396
+ dtype=dtype,
397
+ )
398
+
399
+ if do_classifier_free_guidance and negative_prompt_embeds is None:
400
+ negative_prompt = negative_prompt or ""
401
+ negative_prompt = batch_size * [negative_prompt] if isinstance(negative_prompt, str) else negative_prompt
402
+
403
+ if prompt is not None and type(prompt) is not type(negative_prompt):
404
+ raise TypeError(
405
+ f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !="
406
+ f" {type(prompt)}."
407
+ )
408
+ elif batch_size != len(negative_prompt):
409
+ raise ValueError(
410
+ f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:"
411
+ f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
412
+ " the batch size of `prompt`."
413
+ )
414
+
415
+ negative_prompt_embeds, negative_prompt_attention_mask = self._get_t5_prompt_embeds(
416
+ prompt=negative_prompt,
417
+ num_videos_per_prompt=num_videos_per_prompt,
418
+ max_sequence_length=max_sequence_length,
419
+ device=device,
420
+ dtype=dtype,
421
+ )
422
+
423
+ return prompt_embeds, prompt_attention_mask, negative_prompt_embeds, negative_prompt_attention_mask
424
+
425
+ def check_inputs(
426
+ self,
427
+ prompt,
428
+ conditions,
429
+ image,
430
+ video,
431
+ frame_index,
432
+ strength,
433
+ height,
434
+ width,
435
+ callback_on_step_end_tensor_inputs=None,
436
+ prompt_embeds=None,
437
+ negative_prompt_embeds=None,
438
+ prompt_attention_mask=None,
439
+ negative_prompt_attention_mask=None,
440
+ ):
441
+ if height % 32 != 0 or width % 32 != 0:
442
+ raise ValueError(f"`height` and `width` have to be divisible by 32 but are {height} and {width}.")
443
+
444
+ if callback_on_step_end_tensor_inputs is not None and not all(
445
+ k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs
446
+ ):
447
+ raise ValueError(
448
+ 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]}"
449
+ )
450
+
451
+ if prompt is not None and prompt_embeds is not None:
452
+ raise ValueError(
453
+ f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
454
+ " only forward one of the two."
455
+ )
456
+ elif prompt is None and prompt_embeds is None:
457
+ raise ValueError(
458
+ "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined."
459
+ )
460
+ elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)):
461
+ raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
462
+
463
+ if prompt_embeds is not None and prompt_attention_mask is None:
464
+ raise ValueError("Must provide `prompt_attention_mask` when specifying `prompt_embeds`.")
465
+
466
+ if negative_prompt_embeds is not None and negative_prompt_attention_mask is None:
467
+ raise ValueError("Must provide `negative_prompt_attention_mask` when specifying `negative_prompt_embeds`.")
468
+
469
+ if prompt_embeds is not None and negative_prompt_embeds is not None:
470
+ if prompt_embeds.shape != negative_prompt_embeds.shape:
471
+ raise ValueError(
472
+ "`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but"
473
+ f" got: `prompt_embeds` {prompt_embeds.shape} != `negative_prompt_embeds`"
474
+ f" {negative_prompt_embeds.shape}."
475
+ )
476
+ if prompt_attention_mask.shape != negative_prompt_attention_mask.shape:
477
+ raise ValueError(
478
+ "`prompt_attention_mask` and `negative_prompt_attention_mask` must have the same shape when passed directly, but"
479
+ f" got: `prompt_attention_mask` {prompt_attention_mask.shape} != `negative_prompt_attention_mask`"
480
+ f" {negative_prompt_attention_mask.shape}."
481
+ )
482
+
483
+ if conditions is not None and (image is not None or video is not None):
484
+ raise ValueError("If `conditions` is provided, `image` and `video` must not be provided.")
485
+
486
+ if conditions is None:
487
+ if isinstance(image, list) and isinstance(frame_index, list) and len(image) != len(frame_index):
488
+ raise ValueError(
489
+ "If `conditions` is not provided, `image` and `frame_index` must be of the same length."
490
+ )
491
+ elif isinstance(image, list) and isinstance(strength, list) and len(image) != len(strength):
492
+ raise ValueError("If `conditions` is not provided, `image` and `strength` must be of the same length.")
493
+ elif isinstance(video, list) and isinstance(frame_index, list) and len(video) != len(frame_index):
494
+ raise ValueError(
495
+ "If `conditions` is not provided, `video` and `frame_index` must be of the same length."
496
+ )
497
+ elif isinstance(video, list) and isinstance(strength, list) and len(video) != len(strength):
498
+ raise ValueError("If `conditions` is not provided, `video` and `strength` must be of the same length.")
499
+
500
+ @staticmethod
501
+ def _prepare_video_ids(
502
+ batch_size: int,
503
+ num_frames: int,
504
+ height: int,
505
+ width: int,
506
+ patch_size: int = 1,
507
+ patch_size_t: int = 1,
508
+ device: torch.device = None,
509
+ ) -> torch.Tensor:
510
+ latent_sample_coords = torch.meshgrid(
511
+ torch.arange(0, num_frames, patch_size_t, device=device),
512
+ torch.arange(0, height, patch_size, device=device),
513
+ torch.arange(0, width, patch_size, device=device),
514
+ indexing="ij",
515
+ )
516
+ latent_sample_coords = torch.stack(latent_sample_coords, dim=0)
517
+ latent_coords = latent_sample_coords.unsqueeze(0).repeat(batch_size, 1, 1, 1, 1)
518
+ latent_coords = latent_coords.reshape(batch_size, -1, num_frames * height * width)
519
+
520
+ return latent_coords
521
+
522
+ @staticmethod
523
+ def _scale_video_ids(
524
+ video_ids: torch.Tensor,
525
+ scale_factor: int = 32,
526
+ scale_factor_t: int = 8,
527
+ frame_index: int = 0,
528
+ device: torch.device = None,
529
+ ) -> torch.Tensor:
530
+ scaled_latent_coords = (
531
+ video_ids
532
+ * torch.tensor([scale_factor_t, scale_factor, scale_factor], device=video_ids.device)[None, :, None]
533
+ )
534
+ scaled_latent_coords[:, 0] = (scaled_latent_coords[:, 0] + 1 - scale_factor_t).clamp(min=0)
535
+ scaled_latent_coords[:, 0] += frame_index
536
+
537
+ return scaled_latent_coords
538
+
539
+ @staticmethod
540
+ # Copied from diffusers.pipelines.ltx.pipeline_ltx.LTXPipeline._pack_latents
541
+ def _pack_latents(latents: torch.Tensor, patch_size: int = 1, patch_size_t: int = 1) -> torch.Tensor:
542
+ # Unpacked latents of shape are [B, C, F, H, W] are patched into tokens of shape [B, C, F // p_t, p_t, H // p, p, W // p, p].
543
+ # The patch dimensions are then permuted and collapsed into the channel dimension of shape:
544
+ # [B, F // p_t * H // p * W // p, C * p_t * p * p] (an ndim=3 tensor).
545
+ # dim=0 is the batch size, dim=1 is the effective video sequence length, dim=2 is the effective number of input features
546
+ batch_size, num_channels, num_frames, height, width = latents.shape
547
+ post_patch_num_frames = num_frames // patch_size_t
548
+ post_patch_height = height // patch_size
549
+ post_patch_width = width // patch_size
550
+ latents = latents.reshape(
551
+ batch_size,
552
+ -1,
553
+ post_patch_num_frames,
554
+ patch_size_t,
555
+ post_patch_height,
556
+ patch_size,
557
+ post_patch_width,
558
+ patch_size,
559
+ )
560
+ latents = latents.permute(0, 2, 4, 6, 1, 3, 5, 7).flatten(4, 7).flatten(1, 3)
561
+ return latents
562
+
563
+ @staticmethod
564
+ # Copied from diffusers.pipelines.ltx.pipeline_ltx.LTXPipeline._unpack_latents
565
+ def _unpack_latents(
566
+ latents: torch.Tensor, num_frames: int, height: int, width: int, patch_size: int = 1, patch_size_t: int = 1
567
+ ) -> torch.Tensor:
568
+ # Packed latents of shape [B, S, D] (S is the effective video sequence length, D is the effective feature dimensions)
569
+ # are unpacked and reshaped into a video tensor of shape [B, C, F, H, W]. This is the inverse operation of
570
+ # what happens in the `_pack_latents` method.
571
+ batch_size = latents.size(0)
572
+ latents = latents.reshape(batch_size, num_frames, height, width, -1, patch_size_t, patch_size, patch_size)
573
+ latents = latents.permute(0, 4, 1, 5, 2, 6, 3, 7).flatten(6, 7).flatten(4, 5).flatten(2, 3)
574
+ return latents
575
+
576
+ @staticmethod
577
+ # Copied from diffusers.pipelines.ltx.pipeline_ltx.LTXPipeline._normalize_latents
578
+ def _normalize_latents(
579
+ latents: torch.Tensor, latents_mean: torch.Tensor, latents_std: torch.Tensor, scaling_factor: float = 1.0
580
+ ) -> torch.Tensor:
581
+ # Normalize latents across the channel dimension [B, C, F, H, W]
582
+ latents_mean = latents_mean.view(1, -1, 1, 1, 1).to(latents.device, latents.dtype)
583
+ latents_std = latents_std.view(1, -1, 1, 1, 1).to(latents.device, latents.dtype)
584
+ latents = (latents - latents_mean) * scaling_factor / latents_std
585
+ return latents
586
+
587
+ @staticmethod
588
+ # Copied from diffusers.pipelines.ltx.pipeline_ltx.LTXPipeline._denormalize_latents
589
+ def _denormalize_latents(
590
+ latents: torch.Tensor, latents_mean: torch.Tensor, latents_std: torch.Tensor, scaling_factor: float = 1.0
591
+ ) -> torch.Tensor:
592
+ # Denormalize latents across the channel dimension [B, C, F, H, W]
593
+ latents_mean = latents_mean.view(1, -1, 1, 1, 1).to(latents.device, latents.dtype)
594
+ latents_std = latents_std.view(1, -1, 1, 1, 1).to(latents.device, latents.dtype)
595
+ latents = latents * latents_std / scaling_factor + latents_mean
596
+ return latents
597
+
598
+ def trim_conditioning_sequence(self, start_frame: int, sequence_num_frames: int, target_num_frames: int):
599
+ """
600
+ Trim a conditioning sequence to the allowed number of frames.
601
+
602
+ Args:
603
+ start_frame (int): The target frame number of the first frame in the sequence.
604
+ sequence_num_frames (int): The number of frames in the sequence.
605
+ target_num_frames (int): The target number of frames in the generated video.
606
+ Returns:
607
+ int: updated sequence length
608
+ """
609
+ scale_factor = self.vae_temporal_compression_ratio
610
+ num_frames = min(sequence_num_frames, target_num_frames - start_frame)
611
+ # Trim down to a multiple of temporal_scale_factor frames plus 1
612
+ num_frames = (num_frames - 1) // scale_factor * scale_factor + 1
613
+ return num_frames
614
+
615
+ @staticmethod
616
+ def add_noise_to_image_conditioning_latents(
617
+ t: float,
618
+ init_latents: torch.Tensor,
619
+ latents: torch.Tensor,
620
+ noise_scale: float,
621
+ conditioning_mask: torch.Tensor,
622
+ generator,
623
+ eps=1e-6,
624
+ ):
625
+ """
626
+ Add timestep-dependent noise to the hard-conditioning latents. This helps with motion continuity, especially
627
+ when conditioned on a single frame.
628
+ """
629
+ noise = randn_tensor(
630
+ latents.shape,
631
+ generator=generator,
632
+ device=latents.device,
633
+ dtype=latents.dtype,
634
+ )
635
+ # Add noise only to hard-conditioning latents (conditioning_mask = 1.0)
636
+ need_to_noise = (conditioning_mask > 1.0 - eps).unsqueeze(-1)
637
+ noised_latents = init_latents + noise_scale * noise * (t**2)
638
+ latents = torch.where(need_to_noise, noised_latents, latents)
639
+ return latents
640
+
641
+ def prepare_latents(
642
+ self,
643
+ conditions: Optional[List[torch.Tensor]] = None,
644
+ condition_strength: Optional[List[float]] = None,
645
+ condition_frame_index: Optional[List[int]] = None,
646
+ batch_size: int = 1,
647
+ num_channels_latents: int = 128,
648
+ height: int = 512,
649
+ width: int = 704,
650
+ num_frames: int = 161,
651
+ num_prefix_latent_frames: int = 2,
652
+ generator: Optional[torch.Generator] = None,
653
+ device: Optional[torch.device] = None,
654
+ dtype: Optional[torch.dtype] = None,
655
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, int]:
656
+ num_latent_frames = (num_frames - 1) // self.vae_temporal_compression_ratio + 1
657
+ latent_height = height // self.vae_spatial_compression_ratio
658
+ latent_width = width // self.vae_spatial_compression_ratio
659
+
660
+ shape = (batch_size, num_channels_latents, num_latent_frames, latent_height, latent_width)
661
+ latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
662
+
663
+ if len(conditions) > 0:
664
+ condition_latent_frames_mask = torch.zeros(
665
+ (batch_size, num_latent_frames), device=device, dtype=torch.float32
666
+ )
667
+
668
+ extra_conditioning_latents = []
669
+ extra_conditioning_video_ids = []
670
+ extra_conditioning_mask = []
671
+ extra_conditioning_num_latents = 0
672
+ for data, strength, frame_index in zip(conditions, condition_strength, condition_frame_index):
673
+ condition_latents = retrieve_latents(self.vae.encode(data), generator=generator)
674
+ condition_latents = self._normalize_latents(
675
+ condition_latents, self.vae.latents_mean, self.vae.latents_std
676
+ ).to(device, dtype=dtype)
677
+
678
+ num_data_frames = data.size(2)
679
+ num_cond_frames = condition_latents.size(2)
680
+
681
+ if frame_index == 0:
682
+ latents[:, :, :num_cond_frames] = torch.lerp(
683
+ latents[:, :, :num_cond_frames], condition_latents, strength
684
+ )
685
+ condition_latent_frames_mask[:, :num_cond_frames] = strength
686
+
687
+ else:
688
+ if num_data_frames > 1:
689
+ if num_cond_frames < num_prefix_latent_frames:
690
+ raise ValueError(
691
+ f"Number of latent frames must be at least {num_prefix_latent_frames} but got {num_data_frames}."
692
+ )
693
+
694
+ if num_cond_frames > num_prefix_latent_frames:
695
+ start_frame = frame_index // self.vae_temporal_compression_ratio + num_prefix_latent_frames
696
+ end_frame = start_frame + num_cond_frames - num_prefix_latent_frames
697
+ latents[:, :, start_frame:end_frame] = torch.lerp(
698
+ latents[:, :, start_frame:end_frame],
699
+ condition_latents[:, :, num_prefix_latent_frames:],
700
+ strength,
701
+ )
702
+ condition_latent_frames_mask[:, start_frame:end_frame] = strength
703
+ condition_latents = condition_latents[:, :, :num_prefix_latent_frames]
704
+
705
+ noise = randn_tensor(condition_latents.shape, generator=generator, device=device, dtype=dtype)
706
+ condition_latents = torch.lerp(noise, condition_latents, strength)
707
+
708
+ condition_video_ids = self._prepare_video_ids(
709
+ batch_size,
710
+ condition_latents.size(2),
711
+ latent_height,
712
+ latent_width,
713
+ patch_size=self.transformer_spatial_patch_size,
714
+ patch_size_t=self.transformer_temporal_patch_size,
715
+ device=device,
716
+ )
717
+ condition_video_ids = self._scale_video_ids(
718
+ condition_video_ids,
719
+ scale_factor=self.vae_spatial_compression_ratio,
720
+ scale_factor_t=self.vae_temporal_compression_ratio,
721
+ frame_index=frame_index,
722
+ device=device,
723
+ )
724
+ condition_latents = self._pack_latents(
725
+ condition_latents,
726
+ self.transformer_spatial_patch_size,
727
+ self.transformer_temporal_patch_size,
728
+ )
729
+ condition_conditioning_mask = torch.full(
730
+ condition_latents.shape[:2], strength, device=device, dtype=dtype
731
+ )
732
+
733
+ extra_conditioning_latents.append(condition_latents)
734
+ extra_conditioning_video_ids.append(condition_video_ids)
735
+ extra_conditioning_mask.append(condition_conditioning_mask)
736
+ extra_conditioning_num_latents += condition_latents.size(1)
737
+
738
+ video_ids = self._prepare_video_ids(
739
+ batch_size,
740
+ num_latent_frames,
741
+ latent_height,
742
+ latent_width,
743
+ patch_size_t=self.transformer_temporal_patch_size,
744
+ patch_size=self.transformer_spatial_patch_size,
745
+ device=device,
746
+ )
747
+ if len(conditions) > 0:
748
+ conditioning_mask = condition_latent_frames_mask.gather(1, video_ids[:, 0])
749
+ else:
750
+ conditioning_mask, extra_conditioning_num_latents = None, 0
751
+ video_ids = self._scale_video_ids(
752
+ video_ids,
753
+ scale_factor=self.vae_spatial_compression_ratio,
754
+ scale_factor_t=self.vae_temporal_compression_ratio,
755
+ frame_index=0,
756
+ device=device,
757
+ )
758
+ latents = self._pack_latents(
759
+ latents, self.transformer_spatial_patch_size, self.transformer_temporal_patch_size
760
+ )
761
+
762
+ if len(conditions) > 0 and len(extra_conditioning_latents) > 0:
763
+ latents = torch.cat([*extra_conditioning_latents, latents], dim=1)
764
+ video_ids = torch.cat([*extra_conditioning_video_ids, video_ids], dim=2)
765
+ conditioning_mask = torch.cat([*extra_conditioning_mask, conditioning_mask], dim=1)
766
+
767
+ return latents, conditioning_mask, video_ids, extra_conditioning_num_latents
768
+
769
+ @property
770
+ def guidance_scale(self):
771
+ return self._guidance_scale
772
+
773
+ @property
774
+ def do_classifier_free_guidance(self):
775
+ return self._guidance_scale > 1.0
776
+
777
+ @property
778
+ def num_timesteps(self):
779
+ return self._num_timesteps
780
+
781
+ @property
782
+ def current_timestep(self):
783
+ return self._current_timestep
784
+
785
+ @property
786
+ def attention_kwargs(self):
787
+ return self._attention_kwargs
788
+
789
+ @property
790
+ def interrupt(self):
791
+ return self._interrupt
792
+
793
+ @torch.no_grad()
794
+ @replace_example_docstring(EXAMPLE_DOC_STRING)
795
+ def __call__(
796
+ self,
797
+ conditions: Union[LTXVideoCondition, List[LTXVideoCondition]] = None,
798
+ image: Union[PipelineImageInput, List[PipelineImageInput]] = None,
799
+ video: List[PipelineImageInput] = None,
800
+ frame_index: Union[int, List[int]] = 0,
801
+ strength: Union[float, List[float]] = 1.0,
802
+ prompt: Union[str, List[str]] = None,
803
+ negative_prompt: Optional[Union[str, List[str]]] = None,
804
+ height: int = 512,
805
+ width: int = 704,
806
+ num_frames: int = 161,
807
+ frame_rate: int = 25,
808
+ num_inference_steps: int = 50,
809
+ timesteps: List[int] = None,
810
+ guidance_scale: float = 3,
811
+ image_cond_noise_scale: float = 0.15,
812
+ num_videos_per_prompt: Optional[int] = 1,
813
+ generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
814
+ latents: Optional[torch.Tensor] = None,
815
+ prompt_embeds: Optional[torch.Tensor] = None,
816
+ prompt_attention_mask: Optional[torch.Tensor] = None,
817
+ negative_prompt_embeds: Optional[torch.Tensor] = None,
818
+ negative_prompt_attention_mask: Optional[torch.Tensor] = None,
819
+ decode_timestep: Union[float, List[float]] = 0.0,
820
+ decode_noise_scale: Optional[Union[float, List[float]]] = None,
821
+ output_type: Optional[str] = "pil",
822
+ return_dict: bool = True,
823
+ attention_kwargs: Optional[Dict[str, Any]] = None,
824
+ callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
825
+ callback_on_step_end_tensor_inputs: List[str] = ["latents"],
826
+ max_sequence_length: int = 256,
827
+ ):
828
+ r"""
829
+ Function invoked when calling the pipeline for generation.
830
+
831
+ Args:
832
+ conditions (`List[LTXVideoCondition], *optional*`):
833
+ The list of frame-conditioning items for the video generation.If not provided, conditions will be
834
+ created using `image`, `video`, `frame_index` and `strength`.
835
+ image (`PipelineImageInput` or `List[PipelineImageInput]`, *optional*):
836
+ The image or images to condition the video generation. If not provided, one has to pass `video` or
837
+ `conditions`.
838
+ video (`List[PipelineImageInput]`, *optional*):
839
+ The video to condition the video generation. If not provided, one has to pass `image` or `conditions`.
840
+ frame_index (`int` or `List[int]`, *optional*):
841
+ The frame index or frame indices at which the image or video will conditionally effect the video
842
+ generation. If not provided, one has to pass `conditions`.
843
+ strength (`float` or `List[float]`, *optional*):
844
+ The strength or strengths of the conditioning effect. If not provided, one has to pass `conditions`.
845
+ prompt (`str` or `List[str]`, *optional*):
846
+ The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
847
+ instead.
848
+ height (`int`, defaults to `512`):
849
+ The height in pixels of the generated image. This is set to 480 by default for the best results.
850
+ width (`int`, defaults to `704`):
851
+ The width in pixels of the generated image. This is set to 848 by default for the best results.
852
+ num_frames (`int`, defaults to `161`):
853
+ The number of video frames to generate
854
+ num_inference_steps (`int`, *optional*, defaults to 50):
855
+ The number of denoising steps. More denoising steps usually lead to a higher quality image at the
856
+ expense of slower inference.
857
+ timesteps (`List[int]`, *optional*):
858
+ Custom timesteps to use for the denoising process with schedulers which support a `timesteps` argument
859
+ in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is
860
+ passed will be used. Must be in descending order.
861
+ guidance_scale (`float`, defaults to `3 `):
862
+ Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598).
863
+ `guidance_scale` is defined as `w` of equation 2. of [Imagen
864
+ Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale >
865
+ 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`,
866
+ usually at the expense of lower image quality.
867
+ num_videos_per_prompt (`int`, *optional*, defaults to 1):
868
+ The number of videos to generate per prompt.
869
+ generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
870
+ One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
871
+ to make generation deterministic.
872
+ latents (`torch.Tensor`, *optional*):
873
+ Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
874
+ generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
875
+ tensor will ge generated by sampling using the supplied random `generator`.
876
+ prompt_embeds (`torch.Tensor`, *optional*):
877
+ Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
878
+ provided, text embeddings will be generated from `prompt` input argument.
879
+ prompt_attention_mask (`torch.Tensor`, *optional*):
880
+ Pre-generated attention mask for text embeddings.
881
+ negative_prompt_embeds (`torch.FloatTensor`, *optional*):
882
+ Pre-generated negative text embeddings. For PixArt-Sigma this negative prompt should be "". If not
883
+ provided, negative_prompt_embeds will be generated from `negative_prompt` input argument.
884
+ negative_prompt_attention_mask (`torch.FloatTensor`, *optional*):
885
+ Pre-generated attention mask for negative text embeddings.
886
+ decode_timestep (`float`, defaults to `0.0`):
887
+ The timestep at which generated video is decoded.
888
+ decode_noise_scale (`float`, defaults to `None`):
889
+ The interpolation factor between random noise and denoised latents at the decode timestep.
890
+ output_type (`str`, *optional*, defaults to `"pil"`):
891
+ The output format of the generate image. Choose between
892
+ [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
893
+ return_dict (`bool`, *optional*, defaults to `True`):
894
+ Whether or not to return a [`~pipelines.ltx.LTXPipelineOutput`] instead of a plain tuple.
895
+ attention_kwargs (`dict`, *optional*):
896
+ A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
897
+ `self.processor` in
898
+ [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
899
+ callback_on_step_end (`Callable`, *optional*):
900
+ A function that calls at the end of each denoising steps during the inference. The function is called
901
+ with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
902
+ callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
903
+ `callback_on_step_end_tensor_inputs`.
904
+ callback_on_step_end_tensor_inputs (`List`, *optional*):
905
+ The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
906
+ will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
907
+ `._callback_tensor_inputs` attribute of your pipeline class.
908
+ max_sequence_length (`int` defaults to `128 `):
909
+ Maximum sequence length to use with the `prompt`.
910
+
911
+ Examples:
912
+
913
+ Returns:
914
+ [`~pipelines.ltx.LTXPipelineOutput`] or `tuple`:
915
+ If `return_dict` is `True`, [`~pipelines.ltx.LTXPipelineOutput`] is returned, otherwise a `tuple` is
916
+ returned where the first element is a list with the generated images.
917
+ """
918
+
919
+ if isinstance(callback_on_step_end, (PipelineCallback, MultiPipelineCallbacks)):
920
+ callback_on_step_end_tensor_inputs = callback_on_step_end.tensor_inputs
921
+ if latents is not None:
922
+ raise ValueError("Passing latents is not yet supported.")
923
+
924
+ # 1. Check inputs. Raise error if not correct
925
+ self.check_inputs(
926
+ prompt=prompt,
927
+ conditions=conditions,
928
+ image=image,
929
+ video=video,
930
+ frame_index=frame_index,
931
+ strength=strength,
932
+ height=height,
933
+ width=width,
934
+ callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs,
935
+ prompt_embeds=prompt_embeds,
936
+ negative_prompt_embeds=negative_prompt_embeds,
937
+ prompt_attention_mask=prompt_attention_mask,
938
+ negative_prompt_attention_mask=negative_prompt_attention_mask,
939
+ )
940
+
941
+ self._guidance_scale = guidance_scale
942
+ self._attention_kwargs = attention_kwargs
943
+ self._interrupt = False
944
+ self._current_timestep = None
945
+
946
+ # 2. Define call parameters
947
+ if prompt is not None and isinstance(prompt, str):
948
+ batch_size = 1
949
+ elif prompt is not None and isinstance(prompt, list):
950
+ batch_size = len(prompt)
951
+ else:
952
+ batch_size = prompt_embeds.shape[0]
953
+
954
+ if conditions is not None:
955
+ if not isinstance(conditions, list):
956
+ conditions = [conditions]
957
+
958
+ strength = [condition.strength for condition in conditions]
959
+ frame_index = [condition.frame_index for condition in conditions]
960
+ image = [condition.image for condition in conditions]
961
+ video = [condition.video for condition in conditions]
962
+ elif image is not None or video is not None:
963
+ if not isinstance(image, list):
964
+ image = [image]
965
+ num_conditions = 1
966
+ elif isinstance(image, list):
967
+ num_conditions = len(image)
968
+ if not isinstance(video, list):
969
+ video = [video]
970
+ num_conditions = 1
971
+ elif isinstance(video, list):
972
+ num_conditions = len(video)
973
+
974
+ if not isinstance(frame_index, list):
975
+ frame_index = [frame_index] * num_conditions
976
+ if not isinstance(strength, list):
977
+ strength = [strength] * num_conditions
978
+
979
+ device = self._execution_device
980
+
981
+ # 3. Prepare text embeddings
982
+ (
983
+ prompt_embeds,
984
+ prompt_attention_mask,
985
+ negative_prompt_embeds,
986
+ negative_prompt_attention_mask,
987
+ ) = self.encode_prompt(
988
+ prompt=prompt,
989
+ negative_prompt=negative_prompt,
990
+ do_classifier_free_guidance=self.do_classifier_free_guidance,
991
+ num_videos_per_prompt=num_videos_per_prompt,
992
+ prompt_embeds=prompt_embeds,
993
+ negative_prompt_embeds=negative_prompt_embeds,
994
+ prompt_attention_mask=prompt_attention_mask,
995
+ negative_prompt_attention_mask=negative_prompt_attention_mask,
996
+ max_sequence_length=max_sequence_length,
997
+ device=device,
998
+ )
999
+ if self.do_classifier_free_guidance:
1000
+ prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0)
1001
+ prompt_attention_mask = torch.cat([negative_prompt_attention_mask, prompt_attention_mask], dim=0)
1002
+
1003
+ vae_dtype = self.vae.dtype
1004
+
1005
+ conditioning_tensors = []
1006
+ is_conditioning_image_or_video = image is not None or video is not None
1007
+ if is_conditioning_image_or_video:
1008
+ for condition_image, condition_video, condition_frame_index, condition_strength in zip(
1009
+ image, video, frame_index, strength
1010
+ ):
1011
+ if condition_image is not None:
1012
+ condition_tensor = (
1013
+ self.video_processor.preprocess(condition_image, height, width)
1014
+ .unsqueeze(2)
1015
+ .to(device, dtype=vae_dtype)
1016
+ )
1017
+ elif condition_video is not None:
1018
+ condition_tensor = self.video_processor.preprocess_video(condition_video, height, width)
1019
+ num_frames_input = condition_tensor.size(2)
1020
+ num_frames_output = self.trim_conditioning_sequence(
1021
+ condition_frame_index, num_frames_input, num_frames
1022
+ )
1023
+ condition_tensor = condition_tensor[:, :, :num_frames_output]
1024
+ condition_tensor = condition_tensor.to(device, dtype=vae_dtype)
1025
+ else:
1026
+ raise ValueError("Either `image` or `video` must be provided for conditioning.")
1027
+
1028
+ if condition_tensor.size(2) % self.vae_temporal_compression_ratio != 1:
1029
+ raise ValueError(
1030
+ f"Number of frames in the video must be of the form (k * {self.vae_temporal_compression_ratio} + 1) "
1031
+ f"but got {condition_tensor.size(2)} frames."
1032
+ )
1033
+ conditioning_tensors.append(condition_tensor)
1034
+
1035
+ # 4. Prepare latent variables
1036
+ num_channels_latents = self.transformer.config.in_channels
1037
+ latents, conditioning_mask, video_coords, extra_conditioning_num_latents = self.prepare_latents(
1038
+ conditioning_tensors,
1039
+ strength,
1040
+ frame_index,
1041
+ batch_size=batch_size * num_videos_per_prompt,
1042
+ num_channels_latents=num_channels_latents,
1043
+ height=height,
1044
+ width=width,
1045
+ num_frames=num_frames,
1046
+ generator=generator,
1047
+ device=device,
1048
+ dtype=torch.float32,
1049
+ )
1050
+
1051
+ video_coords = video_coords.float()
1052
+ video_coords[:, 0] = video_coords[:, 0] * (1.0 / frame_rate)
1053
+
1054
+ init_latents = latents.clone() if is_conditioning_image_or_video else None
1055
+
1056
+ if self.do_classifier_free_guidance:
1057
+ video_coords = torch.cat([video_coords, video_coords], dim=0)
1058
+
1059
+ # 5. Prepare timesteps
1060
+ latent_num_frames = (num_frames - 1) // self.vae_temporal_compression_ratio + 1
1061
+ latent_height = height // self.vae_spatial_compression_ratio
1062
+ latent_width = width // self.vae_spatial_compression_ratio
1063
+ sigmas = linear_quadratic_schedule(num_inference_steps)
1064
+ timesteps = sigmas * 1000
1065
+ timesteps, num_inference_steps = retrieve_timesteps(
1066
+ self.scheduler,
1067
+ num_inference_steps,
1068
+ device,
1069
+ timesteps=timesteps,
1070
+ )
1071
+ num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0)
1072
+ self._num_timesteps = len(timesteps)
1073
+
1074
+ # 6. Denoising loop
1075
+ with self.progress_bar(total=num_inference_steps) as progress_bar:
1076
+ for i, t in enumerate(timesteps):
1077
+ if self.interrupt:
1078
+ continue
1079
+
1080
+ self._current_timestep = t
1081
+
1082
+ if image_cond_noise_scale > 0 and init_latents is not None:
1083
+ # Add timestep-dependent noise to the hard-conditioning latents
1084
+ # This helps with motion continuity, especially when conditioned on a single frame
1085
+ latents = self.add_noise_to_image_conditioning_latents(
1086
+ t / 1000.0,
1087
+ init_latents,
1088
+ latents,
1089
+ image_cond_noise_scale,
1090
+ conditioning_mask,
1091
+ generator,
1092
+ )
1093
+
1094
+ latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance else latents
1095
+ if is_conditioning_image_or_video:
1096
+ conditioning_mask_model_input = (
1097
+ torch.cat([conditioning_mask, conditioning_mask])
1098
+ if self.do_classifier_free_guidance
1099
+ else conditioning_mask
1100
+ )
1101
+ latent_model_input = latent_model_input.to(prompt_embeds.dtype)
1102
+
1103
+ # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
1104
+ timestep = t.expand(latent_model_input.shape[0]).unsqueeze(-1).float()
1105
+ if is_conditioning_image_or_video:
1106
+ timestep = torch.min(timestep, (1 - conditioning_mask_model_input) * 1000.0)
1107
+
1108
+ noise_pred = self.transformer(
1109
+ hidden_states=latent_model_input,
1110
+ encoder_hidden_states=prompt_embeds,
1111
+ timestep=timestep,
1112
+ encoder_attention_mask=prompt_attention_mask,
1113
+ video_coords=video_coords,
1114
+ attention_kwargs=attention_kwargs,
1115
+ return_dict=False,
1116
+ )[0]
1117
+
1118
+ if self.do_classifier_free_guidance:
1119
+ noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
1120
+ noise_pred = noise_pred_uncond + self.guidance_scale * (noise_pred_text - noise_pred_uncond)
1121
+ timestep, _ = timestep.chunk(2)
1122
+
1123
+ denoised_latents = self.scheduler.step(
1124
+ -noise_pred, t, latents, per_token_timesteps=timestep, return_dict=False
1125
+ )[0]
1126
+ if is_conditioning_image_or_video:
1127
+ tokens_to_denoise_mask = (t / 1000 - 1e-6 < (1.0 - conditioning_mask)).unsqueeze(-1)
1128
+ latents = torch.where(tokens_to_denoise_mask, denoised_latents, latents)
1129
+ else:
1130
+ latents = denoised_latents
1131
+
1132
+ if callback_on_step_end is not None:
1133
+ callback_kwargs = {}
1134
+ for k in callback_on_step_end_tensor_inputs:
1135
+ callback_kwargs[k] = locals()[k]
1136
+ callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
1137
+
1138
+ latents = callback_outputs.pop("latents", latents)
1139
+ prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
1140
+
1141
+ # call the callback, if provided
1142
+ if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
1143
+ progress_bar.update()
1144
+
1145
+ if XLA_AVAILABLE:
1146
+ xm.mark_step()
1147
+
1148
+ if is_conditioning_image_or_video:
1149
+ latents = latents[:, extra_conditioning_num_latents:]
1150
+
1151
+ latents = self._unpack_latents(
1152
+ latents,
1153
+ latent_num_frames,
1154
+ latent_height,
1155
+ latent_width,
1156
+ self.transformer_spatial_patch_size,
1157
+ self.transformer_temporal_patch_size,
1158
+ )
1159
+
1160
+ if output_type == "latent":
1161
+ video = latents
1162
+ else:
1163
+ latents = self._denormalize_latents(
1164
+ latents, self.vae.latents_mean, self.vae.latents_std, self.vae.config.scaling_factor
1165
+ )
1166
+ latents = latents.to(prompt_embeds.dtype)
1167
+
1168
+ if not self.vae.config.timestep_conditioning:
1169
+ timestep = None
1170
+ else:
1171
+ noise = torch.randn(latents.shape, generator=generator, device=device, dtype=latents.dtype)
1172
+ if not isinstance(decode_timestep, list):
1173
+ decode_timestep = [decode_timestep] * batch_size
1174
+ if decode_noise_scale is None:
1175
+ decode_noise_scale = decode_timestep
1176
+ elif not isinstance(decode_noise_scale, list):
1177
+ decode_noise_scale = [decode_noise_scale] * batch_size
1178
+
1179
+ timestep = torch.tensor(decode_timestep, device=device, dtype=latents.dtype)
1180
+ decode_noise_scale = torch.tensor(decode_noise_scale, device=device, dtype=latents.dtype)[
1181
+ :, None, None, None, None
1182
+ ]
1183
+ latents = (1 - decode_noise_scale) * latents + decode_noise_scale * noise
1184
+
1185
+ video = self.vae.decode(latents, timestep, return_dict=False)[0]
1186
+ video = self.video_processor.postprocess_video(video, output_type=output_type)
1187
+
1188
+ # Offload all models
1189
+ self.maybe_free_model_hooks()
1190
+
1191
+ if not return_dict:
1192
+ return (video,)
1193
+
1194
+ return LTXPipelineOutput(frames=video)