diffusers 0.27.1__py3-none-any.whl → 0.32.2__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- diffusers/__init__.py +233 -6
- diffusers/callbacks.py +209 -0
- diffusers/commands/env.py +102 -6
- diffusers/configuration_utils.py +45 -16
- diffusers/dependency_versions_table.py +4 -3
- diffusers/image_processor.py +434 -110
- diffusers/loaders/__init__.py +42 -9
- diffusers/loaders/ip_adapter.py +626 -36
- diffusers/loaders/lora_base.py +900 -0
- diffusers/loaders/lora_conversion_utils.py +991 -125
- diffusers/loaders/lora_pipeline.py +3812 -0
- diffusers/loaders/peft.py +571 -7
- diffusers/loaders/single_file.py +405 -173
- diffusers/loaders/single_file_model.py +385 -0
- diffusers/loaders/single_file_utils.py +1783 -713
- diffusers/loaders/textual_inversion.py +41 -23
- diffusers/loaders/transformer_flux.py +181 -0
- diffusers/loaders/transformer_sd3.py +89 -0
- diffusers/loaders/unet.py +464 -540
- diffusers/loaders/unet_loader_utils.py +163 -0
- diffusers/models/__init__.py +76 -7
- diffusers/models/activations.py +65 -10
- diffusers/models/adapter.py +53 -53
- diffusers/models/attention.py +605 -18
- diffusers/models/attention_flax.py +1 -1
- diffusers/models/attention_processor.py +4304 -687
- diffusers/models/autoencoders/__init__.py +8 -0
- diffusers/models/autoencoders/autoencoder_asym_kl.py +15 -17
- diffusers/models/autoencoders/autoencoder_dc.py +620 -0
- diffusers/models/autoencoders/autoencoder_kl.py +110 -28
- diffusers/models/autoencoders/autoencoder_kl_allegro.py +1149 -0
- diffusers/models/autoencoders/autoencoder_kl_cogvideox.py +1482 -0
- diffusers/models/autoencoders/autoencoder_kl_hunyuan_video.py +1176 -0
- diffusers/models/autoencoders/autoencoder_kl_ltx.py +1338 -0
- diffusers/models/autoencoders/autoencoder_kl_mochi.py +1166 -0
- diffusers/models/autoencoders/autoencoder_kl_temporal_decoder.py +19 -24
- diffusers/models/autoencoders/autoencoder_oobleck.py +464 -0
- diffusers/models/autoencoders/autoencoder_tiny.py +21 -18
- diffusers/models/autoencoders/consistency_decoder_vae.py +45 -20
- diffusers/models/autoencoders/vae.py +41 -29
- diffusers/models/autoencoders/vq_model.py +182 -0
- diffusers/models/controlnet.py +47 -800
- diffusers/models/controlnet_flux.py +70 -0
- diffusers/models/controlnet_sd3.py +68 -0
- diffusers/models/controlnet_sparsectrl.py +116 -0
- diffusers/models/controlnets/__init__.py +23 -0
- diffusers/models/controlnets/controlnet.py +872 -0
- diffusers/models/{controlnet_flax.py → controlnets/controlnet_flax.py} +9 -9
- diffusers/models/controlnets/controlnet_flux.py +536 -0
- diffusers/models/controlnets/controlnet_hunyuan.py +401 -0
- diffusers/models/controlnets/controlnet_sd3.py +489 -0
- diffusers/models/controlnets/controlnet_sparsectrl.py +788 -0
- diffusers/models/controlnets/controlnet_union.py +832 -0
- diffusers/models/controlnets/controlnet_xs.py +1946 -0
- diffusers/models/controlnets/multicontrolnet.py +183 -0
- diffusers/models/downsampling.py +85 -18
- diffusers/models/embeddings.py +1856 -158
- diffusers/models/embeddings_flax.py +23 -9
- diffusers/models/model_loading_utils.py +480 -0
- diffusers/models/modeling_flax_pytorch_utils.py +2 -1
- diffusers/models/modeling_flax_utils.py +2 -7
- diffusers/models/modeling_outputs.py +14 -0
- diffusers/models/modeling_pytorch_flax_utils.py +1 -1
- diffusers/models/modeling_utils.py +611 -146
- diffusers/models/normalization.py +361 -20
- diffusers/models/resnet.py +18 -23
- diffusers/models/transformers/__init__.py +16 -0
- diffusers/models/transformers/auraflow_transformer_2d.py +544 -0
- diffusers/models/transformers/cogvideox_transformer_3d.py +542 -0
- diffusers/models/transformers/dit_transformer_2d.py +240 -0
- diffusers/models/transformers/dual_transformer_2d.py +9 -8
- diffusers/models/transformers/hunyuan_transformer_2d.py +578 -0
- diffusers/models/transformers/latte_transformer_3d.py +327 -0
- diffusers/models/transformers/lumina_nextdit2d.py +340 -0
- diffusers/models/transformers/pixart_transformer_2d.py +445 -0
- diffusers/models/transformers/prior_transformer.py +13 -13
- diffusers/models/transformers/sana_transformer.py +488 -0
- diffusers/models/transformers/stable_audio_transformer.py +458 -0
- diffusers/models/transformers/t5_film_transformer.py +17 -19
- diffusers/models/transformers/transformer_2d.py +297 -187
- diffusers/models/transformers/transformer_allegro.py +422 -0
- diffusers/models/transformers/transformer_cogview3plus.py +386 -0
- diffusers/models/transformers/transformer_flux.py +593 -0
- diffusers/models/transformers/transformer_hunyuan_video.py +791 -0
- diffusers/models/transformers/transformer_ltx.py +469 -0
- diffusers/models/transformers/transformer_mochi.py +499 -0
- diffusers/models/transformers/transformer_sd3.py +461 -0
- diffusers/models/transformers/transformer_temporal.py +21 -19
- diffusers/models/unets/unet_1d.py +8 -8
- diffusers/models/unets/unet_1d_blocks.py +31 -31
- diffusers/models/unets/unet_2d.py +17 -10
- diffusers/models/unets/unet_2d_blocks.py +225 -149
- diffusers/models/unets/unet_2d_condition.py +41 -40
- diffusers/models/unets/unet_2d_condition_flax.py +6 -5
- diffusers/models/unets/unet_3d_blocks.py +192 -1057
- diffusers/models/unets/unet_3d_condition.py +22 -27
- diffusers/models/unets/unet_i2vgen_xl.py +22 -18
- diffusers/models/unets/unet_kandinsky3.py +2 -2
- diffusers/models/unets/unet_motion_model.py +1413 -89
- diffusers/models/unets/unet_spatio_temporal_condition.py +40 -16
- diffusers/models/unets/unet_stable_cascade.py +19 -18
- diffusers/models/unets/uvit_2d.py +2 -2
- diffusers/models/upsampling.py +95 -26
- diffusers/models/vq_model.py +12 -164
- diffusers/optimization.py +1 -1
- diffusers/pipelines/__init__.py +202 -3
- diffusers/pipelines/allegro/__init__.py +48 -0
- diffusers/pipelines/allegro/pipeline_allegro.py +938 -0
- diffusers/pipelines/allegro/pipeline_output.py +23 -0
- diffusers/pipelines/amused/pipeline_amused.py +12 -12
- diffusers/pipelines/amused/pipeline_amused_img2img.py +14 -12
- diffusers/pipelines/amused/pipeline_amused_inpaint.py +13 -11
- diffusers/pipelines/animatediff/__init__.py +8 -0
- diffusers/pipelines/animatediff/pipeline_animatediff.py +122 -109
- diffusers/pipelines/animatediff/pipeline_animatediff_controlnet.py +1106 -0
- diffusers/pipelines/animatediff/pipeline_animatediff_sdxl.py +1288 -0
- diffusers/pipelines/animatediff/pipeline_animatediff_sparsectrl.py +1010 -0
- diffusers/pipelines/animatediff/pipeline_animatediff_video2video.py +236 -180
- diffusers/pipelines/animatediff/pipeline_animatediff_video2video_controlnet.py +1341 -0
- diffusers/pipelines/animatediff/pipeline_output.py +3 -2
- diffusers/pipelines/audioldm/pipeline_audioldm.py +14 -14
- diffusers/pipelines/audioldm2/modeling_audioldm2.py +58 -39
- diffusers/pipelines/audioldm2/pipeline_audioldm2.py +121 -36
- diffusers/pipelines/aura_flow/__init__.py +48 -0
- diffusers/pipelines/aura_flow/pipeline_aura_flow.py +584 -0
- diffusers/pipelines/auto_pipeline.py +196 -28
- diffusers/pipelines/blip_diffusion/blip_image_processing.py +1 -1
- diffusers/pipelines/blip_diffusion/modeling_blip2.py +6 -6
- diffusers/pipelines/blip_diffusion/modeling_ctx_clip.py +1 -1
- diffusers/pipelines/blip_diffusion/pipeline_blip_diffusion.py +2 -2
- diffusers/pipelines/cogvideo/__init__.py +54 -0
- diffusers/pipelines/cogvideo/pipeline_cogvideox.py +772 -0
- diffusers/pipelines/cogvideo/pipeline_cogvideox_fun_control.py +825 -0
- diffusers/pipelines/cogvideo/pipeline_cogvideox_image2video.py +885 -0
- diffusers/pipelines/cogvideo/pipeline_cogvideox_video2video.py +851 -0
- diffusers/pipelines/cogvideo/pipeline_output.py +20 -0
- diffusers/pipelines/cogview3/__init__.py +47 -0
- diffusers/pipelines/cogview3/pipeline_cogview3plus.py +674 -0
- diffusers/pipelines/cogview3/pipeline_output.py +21 -0
- diffusers/pipelines/consistency_models/pipeline_consistency_models.py +6 -6
- diffusers/pipelines/controlnet/__init__.py +86 -80
- diffusers/pipelines/controlnet/multicontrolnet.py +7 -182
- diffusers/pipelines/controlnet/pipeline_controlnet.py +134 -87
- diffusers/pipelines/controlnet/pipeline_controlnet_blip_diffusion.py +2 -2
- diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +93 -77
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +88 -197
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +136 -90
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +176 -80
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +125 -89
- diffusers/pipelines/controlnet/pipeline_controlnet_union_inpaint_sd_xl.py +1790 -0
- diffusers/pipelines/controlnet/pipeline_controlnet_union_sd_xl.py +1501 -0
- diffusers/pipelines/controlnet/pipeline_controlnet_union_sd_xl_img2img.py +1627 -0
- diffusers/pipelines/controlnet/pipeline_flax_controlnet.py +2 -2
- diffusers/pipelines/controlnet_hunyuandit/__init__.py +48 -0
- diffusers/pipelines/controlnet_hunyuandit/pipeline_hunyuandit_controlnet.py +1060 -0
- diffusers/pipelines/controlnet_sd3/__init__.py +57 -0
- diffusers/pipelines/controlnet_sd3/pipeline_stable_diffusion_3_controlnet.py +1133 -0
- diffusers/pipelines/controlnet_sd3/pipeline_stable_diffusion_3_controlnet_inpainting.py +1153 -0
- diffusers/pipelines/controlnet_xs/__init__.py +68 -0
- diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py +916 -0
- diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py +1111 -0
- diffusers/pipelines/ddpm/pipeline_ddpm.py +2 -2
- diffusers/pipelines/deepfloyd_if/pipeline_if.py +16 -30
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img.py +20 -35
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img_superresolution.py +23 -41
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.py +22 -38
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting_superresolution.py +25 -41
- diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.py +19 -34
- diffusers/pipelines/deepfloyd_if/pipeline_output.py +6 -5
- diffusers/pipelines/deepfloyd_if/watermark.py +1 -1
- diffusers/pipelines/deprecated/alt_diffusion/modeling_roberta_series.py +11 -11
- diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion.py +70 -30
- diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion_img2img.py +48 -25
- diffusers/pipelines/deprecated/repaint/pipeline_repaint.py +2 -2
- diffusers/pipelines/deprecated/spectrogram_diffusion/pipeline_spectrogram_diffusion.py +7 -7
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_cycle_diffusion.py +21 -20
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_inpaint_legacy.py +27 -29
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_model_editing.py +33 -27
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_paradigms.py +33 -23
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_pix2pix_zero.py +36 -30
- diffusers/pipelines/deprecated/versatile_diffusion/modeling_text_unet.py +102 -69
- diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion.py +13 -13
- diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_dual_guided.py +10 -5
- diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_image_variation.py +11 -6
- diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_text_to_image.py +10 -5
- diffusers/pipelines/deprecated/vq_diffusion/pipeline_vq_diffusion.py +5 -5
- diffusers/pipelines/dit/pipeline_dit.py +7 -4
- diffusers/pipelines/flux/__init__.py +69 -0
- diffusers/pipelines/flux/modeling_flux.py +47 -0
- diffusers/pipelines/flux/pipeline_flux.py +957 -0
- diffusers/pipelines/flux/pipeline_flux_control.py +889 -0
- diffusers/pipelines/flux/pipeline_flux_control_img2img.py +945 -0
- diffusers/pipelines/flux/pipeline_flux_control_inpaint.py +1141 -0
- diffusers/pipelines/flux/pipeline_flux_controlnet.py +1006 -0
- diffusers/pipelines/flux/pipeline_flux_controlnet_image_to_image.py +998 -0
- diffusers/pipelines/flux/pipeline_flux_controlnet_inpainting.py +1204 -0
- diffusers/pipelines/flux/pipeline_flux_fill.py +969 -0
- diffusers/pipelines/flux/pipeline_flux_img2img.py +856 -0
- diffusers/pipelines/flux/pipeline_flux_inpaint.py +1022 -0
- diffusers/pipelines/flux/pipeline_flux_prior_redux.py +492 -0
- diffusers/pipelines/flux/pipeline_output.py +37 -0
- diffusers/pipelines/free_init_utils.py +41 -38
- diffusers/pipelines/free_noise_utils.py +596 -0
- diffusers/pipelines/hunyuan_video/__init__.py +48 -0
- diffusers/pipelines/hunyuan_video/pipeline_hunyuan_video.py +687 -0
- diffusers/pipelines/hunyuan_video/pipeline_output.py +20 -0
- diffusers/pipelines/hunyuandit/__init__.py +48 -0
- diffusers/pipelines/hunyuandit/pipeline_hunyuandit.py +916 -0
- diffusers/pipelines/i2vgen_xl/pipeline_i2vgen_xl.py +33 -48
- diffusers/pipelines/kandinsky/pipeline_kandinsky.py +8 -8
- diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py +32 -29
- diffusers/pipelines/kandinsky/pipeline_kandinsky_img2img.py +11 -11
- diffusers/pipelines/kandinsky/pipeline_kandinsky_inpaint.py +12 -12
- diffusers/pipelines/kandinsky/pipeline_kandinsky_prior.py +10 -10
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py +6 -6
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py +34 -31
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_controlnet.py +10 -10
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_controlnet_img2img.py +10 -10
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py +6 -6
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpainting.py +8 -8
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior.py +7 -7
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior_emb2emb.py +6 -6
- diffusers/pipelines/kandinsky3/convert_kandinsky3_unet.py +3 -3
- diffusers/pipelines/kandinsky3/pipeline_kandinsky3.py +22 -35
- diffusers/pipelines/kandinsky3/pipeline_kandinsky3_img2img.py +26 -37
- diffusers/pipelines/kolors/__init__.py +54 -0
- diffusers/pipelines/kolors/pipeline_kolors.py +1070 -0
- diffusers/pipelines/kolors/pipeline_kolors_img2img.py +1250 -0
- diffusers/pipelines/kolors/pipeline_output.py +21 -0
- diffusers/pipelines/kolors/text_encoder.py +889 -0
- diffusers/pipelines/kolors/tokenizer.py +338 -0
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +82 -62
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +77 -60
- diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py +12 -12
- diffusers/pipelines/latte/__init__.py +48 -0
- diffusers/pipelines/latte/pipeline_latte.py +881 -0
- diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion.py +80 -74
- diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion_xl.py +85 -76
- diffusers/pipelines/ledits_pp/pipeline_output.py +2 -2
- diffusers/pipelines/ltx/__init__.py +50 -0
- diffusers/pipelines/ltx/pipeline_ltx.py +789 -0
- diffusers/pipelines/ltx/pipeline_ltx_image2video.py +885 -0
- diffusers/pipelines/ltx/pipeline_output.py +20 -0
- diffusers/pipelines/lumina/__init__.py +48 -0
- diffusers/pipelines/lumina/pipeline_lumina.py +890 -0
- diffusers/pipelines/marigold/__init__.py +50 -0
- diffusers/pipelines/marigold/marigold_image_processing.py +576 -0
- diffusers/pipelines/marigold/pipeline_marigold_depth.py +813 -0
- diffusers/pipelines/marigold/pipeline_marigold_normals.py +690 -0
- diffusers/pipelines/mochi/__init__.py +48 -0
- diffusers/pipelines/mochi/pipeline_mochi.py +748 -0
- diffusers/pipelines/mochi/pipeline_output.py +20 -0
- diffusers/pipelines/musicldm/pipeline_musicldm.py +14 -14
- diffusers/pipelines/pag/__init__.py +80 -0
- diffusers/pipelines/pag/pag_utils.py +243 -0
- diffusers/pipelines/pag/pipeline_pag_controlnet_sd.py +1328 -0
- diffusers/pipelines/pag/pipeline_pag_controlnet_sd_inpaint.py +1543 -0
- diffusers/pipelines/pag/pipeline_pag_controlnet_sd_xl.py +1610 -0
- diffusers/pipelines/pag/pipeline_pag_controlnet_sd_xl_img2img.py +1683 -0
- diffusers/pipelines/pag/pipeline_pag_hunyuandit.py +969 -0
- diffusers/pipelines/pag/pipeline_pag_kolors.py +1136 -0
- diffusers/pipelines/pag/pipeline_pag_pixart_sigma.py +865 -0
- diffusers/pipelines/pag/pipeline_pag_sana.py +886 -0
- diffusers/pipelines/pag/pipeline_pag_sd.py +1062 -0
- diffusers/pipelines/pag/pipeline_pag_sd_3.py +994 -0
- diffusers/pipelines/pag/pipeline_pag_sd_3_img2img.py +1058 -0
- diffusers/pipelines/pag/pipeline_pag_sd_animatediff.py +866 -0
- diffusers/pipelines/pag/pipeline_pag_sd_img2img.py +1094 -0
- diffusers/pipelines/pag/pipeline_pag_sd_inpaint.py +1356 -0
- diffusers/pipelines/pag/pipeline_pag_sd_xl.py +1345 -0
- diffusers/pipelines/pag/pipeline_pag_sd_xl_img2img.py +1544 -0
- diffusers/pipelines/pag/pipeline_pag_sd_xl_inpaint.py +1776 -0
- diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py +17 -12
- diffusers/pipelines/pia/pipeline_pia.py +74 -164
- diffusers/pipelines/pipeline_flax_utils.py +5 -10
- diffusers/pipelines/pipeline_loading_utils.py +515 -53
- diffusers/pipelines/pipeline_utils.py +411 -222
- diffusers/pipelines/pixart_alpha/__init__.py +8 -1
- diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +76 -93
- diffusers/pipelines/pixart_alpha/pipeline_pixart_sigma.py +873 -0
- diffusers/pipelines/sana/__init__.py +47 -0
- diffusers/pipelines/sana/pipeline_output.py +21 -0
- diffusers/pipelines/sana/pipeline_sana.py +884 -0
- diffusers/pipelines/semantic_stable_diffusion/pipeline_semantic_stable_diffusion.py +27 -23
- diffusers/pipelines/shap_e/pipeline_shap_e.py +3 -3
- diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py +14 -14
- diffusers/pipelines/shap_e/renderer.py +1 -1
- diffusers/pipelines/stable_audio/__init__.py +50 -0
- diffusers/pipelines/stable_audio/modeling_stable_audio.py +158 -0
- diffusers/pipelines/stable_audio/pipeline_stable_audio.py +756 -0
- diffusers/pipelines/stable_cascade/pipeline_stable_cascade.py +71 -25
- diffusers/pipelines/stable_cascade/pipeline_stable_cascade_combined.py +23 -19
- diffusers/pipelines/stable_cascade/pipeline_stable_cascade_prior.py +35 -34
- diffusers/pipelines/stable_diffusion/__init__.py +0 -1
- diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +20 -11
- diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion.py +1 -1
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion.py +2 -2
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_upscale.py +6 -6
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +145 -79
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_depth2img.py +43 -28
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_image_variation.py +13 -8
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +100 -68
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +109 -201
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +131 -32
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_latent_upscale.py +247 -87
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +30 -29
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip.py +35 -27
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip_img2img.py +49 -42
- diffusers/pipelines/stable_diffusion/safety_checker.py +2 -1
- diffusers/pipelines/stable_diffusion_3/__init__.py +54 -0
- diffusers/pipelines/stable_diffusion_3/pipeline_output.py +21 -0
- diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py +1140 -0
- diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_img2img.py +1036 -0
- diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_inpaint.py +1250 -0
- diffusers/pipelines/stable_diffusion_attend_and_excite/pipeline_stable_diffusion_attend_and_excite.py +29 -20
- diffusers/pipelines/stable_diffusion_diffedit/pipeline_stable_diffusion_diffedit.py +59 -58
- diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen.py +31 -25
- diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py +38 -22
- diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_k_diffusion.py +30 -24
- diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_xl_k_diffusion.py +24 -23
- diffusers/pipelines/stable_diffusion_ldm3d/pipeline_stable_diffusion_ldm3d.py +107 -67
- diffusers/pipelines/stable_diffusion_panorama/pipeline_stable_diffusion_panorama.py +316 -69
- diffusers/pipelines/stable_diffusion_safe/pipeline_stable_diffusion_safe.py +10 -5
- diffusers/pipelines/stable_diffusion_safe/safety_checker.py +1 -1
- diffusers/pipelines/stable_diffusion_sag/pipeline_stable_diffusion_sag.py +98 -30
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +121 -83
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +161 -105
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +142 -218
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +45 -29
- diffusers/pipelines/stable_diffusion_xl/watermark.py +9 -3
- diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +110 -57
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_adapter.py +69 -39
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +105 -74
- diffusers/pipelines/text_to_video_synthesis/pipeline_output.py +3 -2
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +29 -49
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +32 -93
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +37 -25
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +54 -40
- diffusers/pipelines/unclip/pipeline_unclip.py +6 -6
- diffusers/pipelines/unclip/pipeline_unclip_image_variation.py +6 -6
- diffusers/pipelines/unidiffuser/modeling_text_decoder.py +1 -1
- diffusers/pipelines/unidiffuser/modeling_uvit.py +12 -12
- diffusers/pipelines/unidiffuser/pipeline_unidiffuser.py +29 -28
- diffusers/pipelines/wuerstchen/modeling_paella_vq_model.py +5 -5
- diffusers/pipelines/wuerstchen/modeling_wuerstchen_common.py +5 -10
- diffusers/pipelines/wuerstchen/modeling_wuerstchen_prior.py +6 -8
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen.py +4 -4
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen_combined.py +12 -12
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen_prior.py +15 -14
- diffusers/{models/dual_transformer_2d.py → quantizers/__init__.py} +2 -6
- diffusers/quantizers/auto.py +139 -0
- diffusers/quantizers/base.py +233 -0
- diffusers/quantizers/bitsandbytes/__init__.py +2 -0
- diffusers/quantizers/bitsandbytes/bnb_quantizer.py +561 -0
- diffusers/quantizers/bitsandbytes/utils.py +306 -0
- diffusers/quantizers/gguf/__init__.py +1 -0
- diffusers/quantizers/gguf/gguf_quantizer.py +159 -0
- diffusers/quantizers/gguf/utils.py +456 -0
- diffusers/quantizers/quantization_config.py +669 -0
- diffusers/quantizers/torchao/__init__.py +15 -0
- diffusers/quantizers/torchao/torchao_quantizer.py +292 -0
- diffusers/schedulers/__init__.py +12 -2
- diffusers/schedulers/deprecated/__init__.py +1 -1
- diffusers/schedulers/deprecated/scheduling_karras_ve.py +25 -25
- diffusers/schedulers/scheduling_amused.py +5 -5
- diffusers/schedulers/scheduling_consistency_decoder.py +11 -11
- diffusers/schedulers/scheduling_consistency_models.py +23 -25
- diffusers/schedulers/scheduling_cosine_dpmsolver_multistep.py +572 -0
- diffusers/schedulers/scheduling_ddim.py +27 -26
- diffusers/schedulers/scheduling_ddim_cogvideox.py +452 -0
- diffusers/schedulers/scheduling_ddim_flax.py +2 -1
- diffusers/schedulers/scheduling_ddim_inverse.py +16 -16
- diffusers/schedulers/scheduling_ddim_parallel.py +32 -31
- diffusers/schedulers/scheduling_ddpm.py +27 -30
- diffusers/schedulers/scheduling_ddpm_flax.py +7 -3
- diffusers/schedulers/scheduling_ddpm_parallel.py +33 -36
- diffusers/schedulers/scheduling_ddpm_wuerstchen.py +14 -14
- diffusers/schedulers/scheduling_deis_multistep.py +150 -50
- diffusers/schedulers/scheduling_dpm_cogvideox.py +489 -0
- diffusers/schedulers/scheduling_dpmsolver_multistep.py +221 -84
- diffusers/schedulers/scheduling_dpmsolver_multistep_flax.py +2 -2
- diffusers/schedulers/scheduling_dpmsolver_multistep_inverse.py +158 -52
- diffusers/schedulers/scheduling_dpmsolver_sde.py +153 -34
- diffusers/schedulers/scheduling_dpmsolver_singlestep.py +275 -86
- diffusers/schedulers/scheduling_edm_dpmsolver_multistep.py +81 -57
- diffusers/schedulers/scheduling_edm_euler.py +62 -39
- diffusers/schedulers/scheduling_euler_ancestral_discrete.py +30 -29
- diffusers/schedulers/scheduling_euler_discrete.py +255 -74
- diffusers/schedulers/scheduling_flow_match_euler_discrete.py +458 -0
- diffusers/schedulers/scheduling_flow_match_heun_discrete.py +320 -0
- diffusers/schedulers/scheduling_heun_discrete.py +174 -46
- diffusers/schedulers/scheduling_ipndm.py +9 -9
- diffusers/schedulers/scheduling_k_dpm_2_ancestral_discrete.py +138 -29
- diffusers/schedulers/scheduling_k_dpm_2_discrete.py +132 -26
- diffusers/schedulers/scheduling_karras_ve_flax.py +6 -6
- diffusers/schedulers/scheduling_lcm.py +23 -29
- diffusers/schedulers/scheduling_lms_discrete.py +105 -28
- diffusers/schedulers/scheduling_pndm.py +20 -20
- diffusers/schedulers/scheduling_repaint.py +21 -21
- diffusers/schedulers/scheduling_sasolver.py +157 -60
- diffusers/schedulers/scheduling_sde_ve.py +19 -19
- diffusers/schedulers/scheduling_tcd.py +41 -36
- diffusers/schedulers/scheduling_unclip.py +19 -16
- diffusers/schedulers/scheduling_unipc_multistep.py +243 -47
- diffusers/schedulers/scheduling_utils.py +12 -5
- diffusers/schedulers/scheduling_utils_flax.py +1 -3
- diffusers/schedulers/scheduling_vq_diffusion.py +10 -10
- diffusers/training_utils.py +214 -30
- diffusers/utils/__init__.py +17 -1
- diffusers/utils/constants.py +3 -0
- diffusers/utils/doc_utils.py +1 -0
- diffusers/utils/dummy_pt_objects.py +592 -7
- diffusers/utils/dummy_torch_and_torchsde_objects.py +15 -0
- diffusers/utils/dummy_torch_and_transformers_and_sentencepiece_objects.py +47 -0
- diffusers/utils/dummy_torch_and_transformers_objects.py +1001 -71
- diffusers/utils/dynamic_modules_utils.py +34 -29
- diffusers/utils/export_utils.py +50 -6
- diffusers/utils/hub_utils.py +131 -17
- diffusers/utils/import_utils.py +210 -8
- diffusers/utils/loading_utils.py +118 -5
- diffusers/utils/logging.py +4 -2
- diffusers/utils/peft_utils.py +37 -7
- diffusers/utils/state_dict_utils.py +13 -2
- diffusers/utils/testing_utils.py +193 -11
- diffusers/utils/torch_utils.py +4 -0
- diffusers/video_processor.py +113 -0
- {diffusers-0.27.1.dist-info → diffusers-0.32.2.dist-info}/METADATA +82 -91
- diffusers-0.32.2.dist-info/RECORD +550 -0
- {diffusers-0.27.1.dist-info → diffusers-0.32.2.dist-info}/WHEEL +1 -1
- diffusers/loaders/autoencoder.py +0 -146
- diffusers/loaders/controlnet.py +0 -136
- diffusers/loaders/lora.py +0 -1349
- diffusers/models/prior_transformer.py +0 -12
- diffusers/models/t5_film_transformer.py +0 -70
- diffusers/models/transformer_2d.py +0 -25
- diffusers/models/transformer_temporal.py +0 -34
- diffusers/models/unet_1d.py +0 -26
- diffusers/models/unet_1d_blocks.py +0 -203
- diffusers/models/unet_2d.py +0 -27
- diffusers/models/unet_2d_blocks.py +0 -375
- diffusers/models/unet_2d_condition.py +0 -25
- diffusers-0.27.1.dist-info/RECORD +0 -399
- {diffusers-0.27.1.dist-info → diffusers-0.32.2.dist-info}/LICENSE +0 -0
- {diffusers-0.27.1.dist-info → diffusers-0.32.2.dist-info}/entry_points.txt +0 -0
- {diffusers-0.27.1.dist-info → diffusers-0.32.2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,938 @@
|
|
1
|
+
# Copyright 2024 The RhymesAI and The HuggingFace Team.
|
2
|
+
# All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
import html
|
17
|
+
import inspect
|
18
|
+
import math
|
19
|
+
import re
|
20
|
+
import urllib.parse as ul
|
21
|
+
from typing import Callable, Dict, List, Optional, Tuple, Union
|
22
|
+
|
23
|
+
import torch
|
24
|
+
from transformers import T5EncoderModel, T5Tokenizer
|
25
|
+
|
26
|
+
from ...callbacks import MultiPipelineCallbacks, PipelineCallback
|
27
|
+
from ...models import AllegroTransformer3DModel, AutoencoderKLAllegro
|
28
|
+
from ...models.embeddings import get_3d_rotary_pos_embed_allegro
|
29
|
+
from ...pipelines.pipeline_utils import DiffusionPipeline
|
30
|
+
from ...schedulers import KarrasDiffusionSchedulers
|
31
|
+
from ...utils import (
|
32
|
+
BACKENDS_MAPPING,
|
33
|
+
deprecate,
|
34
|
+
is_bs4_available,
|
35
|
+
is_ftfy_available,
|
36
|
+
logging,
|
37
|
+
replace_example_docstring,
|
38
|
+
)
|
39
|
+
from ...utils.torch_utils import randn_tensor
|
40
|
+
from ...video_processor import VideoProcessor
|
41
|
+
from .pipeline_output import AllegroPipelineOutput
|
42
|
+
|
43
|
+
|
44
|
+
logger = logging.get_logger(__name__)
|
45
|
+
|
46
|
+
if is_bs4_available():
|
47
|
+
from bs4 import BeautifulSoup
|
48
|
+
|
49
|
+
if is_ftfy_available():
|
50
|
+
import ftfy
|
51
|
+
|
52
|
+
|
53
|
+
EXAMPLE_DOC_STRING = """
|
54
|
+
Examples:
|
55
|
+
```py
|
56
|
+
>>> import torch
|
57
|
+
>>> from diffusers import AutoencoderKLAllegro, AllegroPipeline
|
58
|
+
>>> from diffusers.utils import export_to_video
|
59
|
+
|
60
|
+
>>> vae = AutoencoderKLAllegro.from_pretrained("rhymes-ai/Allegro", subfolder="vae", torch_dtype=torch.float32)
|
61
|
+
>>> pipe = AllegroPipeline.from_pretrained("rhymes-ai/Allegro", vae=vae, torch_dtype=torch.bfloat16).to("cuda")
|
62
|
+
>>> pipe.enable_vae_tiling()
|
63
|
+
|
64
|
+
>>> prompt = (
|
65
|
+
... "A seaside harbor with bright sunlight and sparkling seawater, with many boats in the water. From an aerial view, "
|
66
|
+
... "the boats vary in size and color, some moving and some stationary. Fishing boats in the water suggest that this "
|
67
|
+
... "location might be a popular spot for docking fishing boats."
|
68
|
+
... )
|
69
|
+
>>> video = pipe(prompt, guidance_scale=7.5, max_sequence_length=512).frames[0]
|
70
|
+
>>> export_to_video(video, "output.mp4", fps=15)
|
71
|
+
```
|
72
|
+
"""
|
73
|
+
|
74
|
+
|
75
|
+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps
|
76
|
+
def retrieve_timesteps(
|
77
|
+
scheduler,
|
78
|
+
num_inference_steps: Optional[int] = None,
|
79
|
+
device: Optional[Union[str, torch.device]] = None,
|
80
|
+
timesteps: Optional[List[int]] = None,
|
81
|
+
sigmas: Optional[List[float]] = None,
|
82
|
+
**kwargs,
|
83
|
+
):
|
84
|
+
r"""
|
85
|
+
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
|
86
|
+
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.
|
87
|
+
|
88
|
+
Args:
|
89
|
+
scheduler (`SchedulerMixin`):
|
90
|
+
The scheduler to get timesteps from.
|
91
|
+
num_inference_steps (`int`):
|
92
|
+
The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
|
93
|
+
must be `None`.
|
94
|
+
device (`str` or `torch.device`, *optional*):
|
95
|
+
The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
|
96
|
+
timesteps (`List[int]`, *optional*):
|
97
|
+
Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
|
98
|
+
`num_inference_steps` and `sigmas` must be `None`.
|
99
|
+
sigmas (`List[float]`, *optional*):
|
100
|
+
Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
|
101
|
+
`num_inference_steps` and `timesteps` must be `None`.
|
102
|
+
|
103
|
+
Returns:
|
104
|
+
`Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
|
105
|
+
second element is the number of inference steps.
|
106
|
+
"""
|
107
|
+
if timesteps is not None and sigmas is not None:
|
108
|
+
raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values")
|
109
|
+
if timesteps is not None:
|
110
|
+
accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
|
111
|
+
if not accepts_timesteps:
|
112
|
+
raise ValueError(
|
113
|
+
f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
|
114
|
+
f" timestep schedules. Please check whether you are using the correct scheduler."
|
115
|
+
)
|
116
|
+
scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
|
117
|
+
timesteps = scheduler.timesteps
|
118
|
+
num_inference_steps = len(timesteps)
|
119
|
+
elif sigmas is not None:
|
120
|
+
accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
|
121
|
+
if not accept_sigmas:
|
122
|
+
raise ValueError(
|
123
|
+
f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
|
124
|
+
f" sigmas schedules. Please check whether you are using the correct scheduler."
|
125
|
+
)
|
126
|
+
scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
|
127
|
+
timesteps = scheduler.timesteps
|
128
|
+
num_inference_steps = len(timesteps)
|
129
|
+
else:
|
130
|
+
scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
|
131
|
+
timesteps = scheduler.timesteps
|
132
|
+
return timesteps, num_inference_steps
|
133
|
+
|
134
|
+
|
135
|
+
class AllegroPipeline(DiffusionPipeline):
|
136
|
+
r"""
|
137
|
+
Pipeline for text-to-video generation using Allegro.
|
138
|
+
|
139
|
+
This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods the
|
140
|
+
library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
|
141
|
+
|
142
|
+
Args:
|
143
|
+
vae ([`AllegroAutoEncoderKL3D`]):
|
144
|
+
Variational Auto-Encoder (VAE) Model to encode and decode video to and from latent representations.
|
145
|
+
text_encoder ([`T5EncoderModel`]):
|
146
|
+
Frozen text-encoder. PixArt-Alpha uses
|
147
|
+
[T5](https://huggingface.co/docs/transformers/model_doc/t5#transformers.T5EncoderModel), specifically the
|
148
|
+
[t5-v1_1-xxl](https://huggingface.co/PixArt-alpha/PixArt-alpha/tree/main/t5-v1_1-xxl) variant.
|
149
|
+
tokenizer (`T5Tokenizer`):
|
150
|
+
Tokenizer of class
|
151
|
+
[T5Tokenizer](https://huggingface.co/docs/transformers/model_doc/t5#transformers.T5Tokenizer).
|
152
|
+
transformer ([`AllegroTransformer3DModel`]):
|
153
|
+
A text conditioned `AllegroTransformer3DModel` to denoise the encoded video latents.
|
154
|
+
scheduler ([`SchedulerMixin`]):
|
155
|
+
A scheduler to be used in combination with `transformer` to denoise the encoded video latents.
|
156
|
+
"""
|
157
|
+
|
158
|
+
bad_punct_regex = re.compile(
|
159
|
+
r"["
|
160
|
+
+ "#®•©™&@·º½¾¿¡§~"
|
161
|
+
+ r"\)"
|
162
|
+
+ r"\("
|
163
|
+
+ r"\]"
|
164
|
+
+ r"\["
|
165
|
+
+ r"\}"
|
166
|
+
+ r"\{"
|
167
|
+
+ r"\|"
|
168
|
+
+ "\\"
|
169
|
+
+ r"\/"
|
170
|
+
+ r"\*"
|
171
|
+
+ r"]{1,}"
|
172
|
+
) # noqa
|
173
|
+
|
174
|
+
_optional_components = []
|
175
|
+
model_cpu_offload_seq = "text_encoder->transformer->vae"
|
176
|
+
|
177
|
+
_callback_tensor_inputs = [
|
178
|
+
"latents",
|
179
|
+
"prompt_embeds",
|
180
|
+
"negative_prompt_embeds",
|
181
|
+
]
|
182
|
+
|
183
|
+
def __init__(
|
184
|
+
self,
|
185
|
+
tokenizer: T5Tokenizer,
|
186
|
+
text_encoder: T5EncoderModel,
|
187
|
+
vae: AutoencoderKLAllegro,
|
188
|
+
transformer: AllegroTransformer3DModel,
|
189
|
+
scheduler: KarrasDiffusionSchedulers,
|
190
|
+
):
|
191
|
+
super().__init__()
|
192
|
+
|
193
|
+
self.register_modules(
|
194
|
+
tokenizer=tokenizer, text_encoder=text_encoder, vae=vae, transformer=transformer, scheduler=scheduler
|
195
|
+
)
|
196
|
+
self.vae_scale_factor_spatial = (
|
197
|
+
2 ** (len(self.vae.config.block_out_channels) - 1) if hasattr(self, "vae") and self.vae is not None else 8
|
198
|
+
)
|
199
|
+
self.vae_scale_factor_temporal = (
|
200
|
+
self.vae.config.temporal_compression_ratio if hasattr(self, "vae") and self.vae is not None else 4
|
201
|
+
)
|
202
|
+
|
203
|
+
self.video_processor = VideoProcessor(vae_scale_factor=self.vae_scale_factor_spatial)
|
204
|
+
|
205
|
+
# Copied from diffusers.pipelines.pixart_alpha.pipeline_pixart_alpha.PixArtAlphaPipeline.encode_prompt with 120->512, num_images_per_prompt->num_videos_per_prompt
|
206
|
+
def encode_prompt(
|
207
|
+
self,
|
208
|
+
prompt: Union[str, List[str]],
|
209
|
+
do_classifier_free_guidance: bool = True,
|
210
|
+
negative_prompt: str = "",
|
211
|
+
num_videos_per_prompt: int = 1,
|
212
|
+
device: Optional[torch.device] = None,
|
213
|
+
prompt_embeds: Optional[torch.Tensor] = None,
|
214
|
+
negative_prompt_embeds: Optional[torch.Tensor] = None,
|
215
|
+
prompt_attention_mask: Optional[torch.Tensor] = None,
|
216
|
+
negative_prompt_attention_mask: Optional[torch.Tensor] = None,
|
217
|
+
clean_caption: bool = False,
|
218
|
+
max_sequence_length: int = 512,
|
219
|
+
**kwargs,
|
220
|
+
):
|
221
|
+
r"""
|
222
|
+
Encodes the prompt into text encoder hidden states.
|
223
|
+
|
224
|
+
Args:
|
225
|
+
prompt (`str` or `List[str]`, *optional*):
|
226
|
+
prompt to be encoded
|
227
|
+
negative_prompt (`str` or `List[str]`, *optional*):
|
228
|
+
The prompt not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds`
|
229
|
+
instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). For
|
230
|
+
PixArt-Alpha, this should be "".
|
231
|
+
do_classifier_free_guidance (`bool`, *optional*, defaults to `True`):
|
232
|
+
whether to use classifier free guidance or not
|
233
|
+
num_videos_per_prompt (`int`, *optional*, defaults to 1):
|
234
|
+
number of images that should be generated per prompt
|
235
|
+
device: (`torch.device`, *optional*):
|
236
|
+
torch device to place the resulting embeddings on
|
237
|
+
prompt_embeds (`torch.Tensor`, *optional*):
|
238
|
+
Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
|
239
|
+
provided, text embeddings will be generated from `prompt` input argument.
|
240
|
+
negative_prompt_embeds (`torch.Tensor`, *optional*):
|
241
|
+
Pre-generated negative text embeddings. For PixArt-Alpha, it's should be the embeddings of the ""
|
242
|
+
string.
|
243
|
+
clean_caption (`bool`, defaults to `False`):
|
244
|
+
If `True`, the function will preprocess and clean the provided caption before encoding.
|
245
|
+
max_sequence_length (`int`, defaults to 512): Maximum sequence length to use for the prompt.
|
246
|
+
"""
|
247
|
+
|
248
|
+
if "mask_feature" in kwargs:
|
249
|
+
deprecation_message = "The use of `mask_feature` is deprecated. It is no longer used in any computation and that doesn't affect the end results. It will be removed in a future version."
|
250
|
+
deprecate("mask_feature", "1.0.0", deprecation_message, standard_warn=False)
|
251
|
+
|
252
|
+
if device is None:
|
253
|
+
device = self._execution_device
|
254
|
+
|
255
|
+
# See Section 3.1. of the paper.
|
256
|
+
max_length = max_sequence_length
|
257
|
+
|
258
|
+
if prompt_embeds is None:
|
259
|
+
prompt = self._text_preprocessing(prompt, clean_caption=clean_caption)
|
260
|
+
text_inputs = self.tokenizer(
|
261
|
+
prompt,
|
262
|
+
padding="max_length",
|
263
|
+
max_length=max_length,
|
264
|
+
truncation=True,
|
265
|
+
add_special_tokens=True,
|
266
|
+
return_tensors="pt",
|
267
|
+
)
|
268
|
+
text_input_ids = text_inputs.input_ids
|
269
|
+
untruncated_ids = self.tokenizer(prompt, padding="longest", return_tensors="pt").input_ids
|
270
|
+
|
271
|
+
if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
|
272
|
+
text_input_ids, untruncated_ids
|
273
|
+
):
|
274
|
+
removed_text = self.tokenizer.batch_decode(untruncated_ids[:, max_length - 1 : -1])
|
275
|
+
logger.warning(
|
276
|
+
"The following part of your input was truncated because T5 can only handle sequences up to"
|
277
|
+
f" {max_length} tokens: {removed_text}"
|
278
|
+
)
|
279
|
+
|
280
|
+
prompt_attention_mask = text_inputs.attention_mask
|
281
|
+
prompt_attention_mask = prompt_attention_mask.to(device)
|
282
|
+
|
283
|
+
prompt_embeds = self.text_encoder(text_input_ids.to(device), attention_mask=prompt_attention_mask)
|
284
|
+
prompt_embeds = prompt_embeds[0]
|
285
|
+
|
286
|
+
if self.text_encoder is not None:
|
287
|
+
dtype = self.text_encoder.dtype
|
288
|
+
elif self.transformer is not None:
|
289
|
+
dtype = self.transformer.dtype
|
290
|
+
else:
|
291
|
+
dtype = None
|
292
|
+
|
293
|
+
prompt_embeds = prompt_embeds.to(dtype=dtype, device=device)
|
294
|
+
|
295
|
+
bs_embed, seq_len, _ = prompt_embeds.shape
|
296
|
+
# duplicate text embeddings and attention mask for each generation per prompt, using mps friendly method
|
297
|
+
prompt_embeds = prompt_embeds.repeat(1, num_videos_per_prompt, 1)
|
298
|
+
prompt_embeds = prompt_embeds.view(bs_embed * num_videos_per_prompt, seq_len, -1)
|
299
|
+
prompt_attention_mask = prompt_attention_mask.repeat(1, num_videos_per_prompt)
|
300
|
+
prompt_attention_mask = prompt_attention_mask.view(bs_embed * num_videos_per_prompt, -1)
|
301
|
+
|
302
|
+
# get unconditional embeddings for classifier free guidance
|
303
|
+
if do_classifier_free_guidance and negative_prompt_embeds is None:
|
304
|
+
uncond_tokens = [negative_prompt] * bs_embed if isinstance(negative_prompt, str) else negative_prompt
|
305
|
+
uncond_tokens = self._text_preprocessing(uncond_tokens, clean_caption=clean_caption)
|
306
|
+
max_length = prompt_embeds.shape[1]
|
307
|
+
uncond_input = self.tokenizer(
|
308
|
+
uncond_tokens,
|
309
|
+
padding="max_length",
|
310
|
+
max_length=max_length,
|
311
|
+
truncation=True,
|
312
|
+
return_attention_mask=True,
|
313
|
+
add_special_tokens=True,
|
314
|
+
return_tensors="pt",
|
315
|
+
)
|
316
|
+
negative_prompt_attention_mask = uncond_input.attention_mask
|
317
|
+
negative_prompt_attention_mask = negative_prompt_attention_mask.to(device)
|
318
|
+
|
319
|
+
negative_prompt_embeds = self.text_encoder(
|
320
|
+
uncond_input.input_ids.to(device), attention_mask=negative_prompt_attention_mask
|
321
|
+
)
|
322
|
+
negative_prompt_embeds = negative_prompt_embeds[0]
|
323
|
+
|
324
|
+
if do_classifier_free_guidance:
|
325
|
+
# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
|
326
|
+
seq_len = negative_prompt_embeds.shape[1]
|
327
|
+
|
328
|
+
negative_prompt_embeds = negative_prompt_embeds.to(dtype=dtype, device=device)
|
329
|
+
|
330
|
+
negative_prompt_embeds = negative_prompt_embeds.repeat(1, num_videos_per_prompt, 1)
|
331
|
+
negative_prompt_embeds = negative_prompt_embeds.view(bs_embed * num_videos_per_prompt, seq_len, -1)
|
332
|
+
|
333
|
+
negative_prompt_attention_mask = negative_prompt_attention_mask.repeat(1, num_videos_per_prompt)
|
334
|
+
negative_prompt_attention_mask = negative_prompt_attention_mask.view(bs_embed * num_videos_per_prompt, -1)
|
335
|
+
else:
|
336
|
+
negative_prompt_embeds = None
|
337
|
+
negative_prompt_attention_mask = None
|
338
|
+
|
339
|
+
return prompt_embeds, prompt_attention_mask, negative_prompt_embeds, negative_prompt_attention_mask
|
340
|
+
|
341
|
+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_extra_step_kwargs
|
342
|
+
def prepare_extra_step_kwargs(self, generator, eta):
|
343
|
+
# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
|
344
|
+
# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
|
345
|
+
# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
|
346
|
+
# and should be between [0, 1]
|
347
|
+
|
348
|
+
accepts_eta = "eta" in set(inspect.signature(self.scheduler.step).parameters.keys())
|
349
|
+
extra_step_kwargs = {}
|
350
|
+
if accepts_eta:
|
351
|
+
extra_step_kwargs["eta"] = eta
|
352
|
+
|
353
|
+
# check if the scheduler accepts generator
|
354
|
+
accepts_generator = "generator" in set(inspect.signature(self.scheduler.step).parameters.keys())
|
355
|
+
if accepts_generator:
|
356
|
+
extra_step_kwargs["generator"] = generator
|
357
|
+
return extra_step_kwargs
|
358
|
+
|
359
|
+
def check_inputs(
|
360
|
+
self,
|
361
|
+
prompt,
|
362
|
+
num_frames,
|
363
|
+
height,
|
364
|
+
width,
|
365
|
+
callback_on_step_end_tensor_inputs,
|
366
|
+
negative_prompt=None,
|
367
|
+
prompt_embeds=None,
|
368
|
+
negative_prompt_embeds=None,
|
369
|
+
prompt_attention_mask=None,
|
370
|
+
negative_prompt_attention_mask=None,
|
371
|
+
):
|
372
|
+
if num_frames <= 0:
|
373
|
+
raise ValueError(f"`num_frames` have to be positive but is {num_frames}.")
|
374
|
+
if height % 8 != 0 or width % 8 != 0:
|
375
|
+
raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}.")
|
376
|
+
|
377
|
+
if callback_on_step_end_tensor_inputs is not None and not all(
|
378
|
+
k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs
|
379
|
+
):
|
380
|
+
raise ValueError(
|
381
|
+
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]}"
|
382
|
+
)
|
383
|
+
|
384
|
+
if prompt is not None and prompt_embeds is not None:
|
385
|
+
raise ValueError(
|
386
|
+
f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
|
387
|
+
" only forward one of the two."
|
388
|
+
)
|
389
|
+
elif prompt is None and prompt_embeds is None:
|
390
|
+
raise ValueError(
|
391
|
+
"Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined."
|
392
|
+
)
|
393
|
+
elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)):
|
394
|
+
raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
|
395
|
+
|
396
|
+
if prompt is not None and negative_prompt_embeds is not None:
|
397
|
+
raise ValueError(
|
398
|
+
f"Cannot forward both `prompt`: {prompt} and `negative_prompt_embeds`:"
|
399
|
+
f" {negative_prompt_embeds}. Please make sure to only forward one of the two."
|
400
|
+
)
|
401
|
+
|
402
|
+
if negative_prompt is not None and negative_prompt_embeds is not None:
|
403
|
+
raise ValueError(
|
404
|
+
f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:"
|
405
|
+
f" {negative_prompt_embeds}. Please make sure to only forward one of the two."
|
406
|
+
)
|
407
|
+
|
408
|
+
if prompt_embeds is not None and prompt_attention_mask is None:
|
409
|
+
raise ValueError("Must provide `prompt_attention_mask` when specifying `prompt_embeds`.")
|
410
|
+
|
411
|
+
if negative_prompt_embeds is not None and negative_prompt_attention_mask is None:
|
412
|
+
raise ValueError("Must provide `negative_prompt_attention_mask` when specifying `negative_prompt_embeds`.")
|
413
|
+
|
414
|
+
if prompt_embeds is not None and negative_prompt_embeds is not None:
|
415
|
+
if prompt_embeds.shape != negative_prompt_embeds.shape:
|
416
|
+
raise ValueError(
|
417
|
+
"`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but"
|
418
|
+
f" got: `prompt_embeds` {prompt_embeds.shape} != `negative_prompt_embeds`"
|
419
|
+
f" {negative_prompt_embeds.shape}."
|
420
|
+
)
|
421
|
+
if prompt_attention_mask.shape != negative_prompt_attention_mask.shape:
|
422
|
+
raise ValueError(
|
423
|
+
"`prompt_attention_mask` and `negative_prompt_attention_mask` must have the same shape when passed directly, but"
|
424
|
+
f" got: `prompt_attention_mask` {prompt_attention_mask.shape} != `negative_prompt_attention_mask`"
|
425
|
+
f" {negative_prompt_attention_mask.shape}."
|
426
|
+
)
|
427
|
+
|
428
|
+
# Copied from diffusers.pipelines.deepfloyd_if.pipeline_if.IFPipeline._text_preprocessing
|
429
|
+
def _text_preprocessing(self, text, clean_caption=False):
|
430
|
+
if clean_caption and not is_bs4_available():
|
431
|
+
logger.warning(BACKENDS_MAPPING["bs4"][-1].format("Setting `clean_caption=True`"))
|
432
|
+
logger.warning("Setting `clean_caption` to False...")
|
433
|
+
clean_caption = False
|
434
|
+
|
435
|
+
if clean_caption and not is_ftfy_available():
|
436
|
+
logger.warning(BACKENDS_MAPPING["ftfy"][-1].format("Setting `clean_caption=True`"))
|
437
|
+
logger.warning("Setting `clean_caption` to False...")
|
438
|
+
clean_caption = False
|
439
|
+
|
440
|
+
if not isinstance(text, (tuple, list)):
|
441
|
+
text = [text]
|
442
|
+
|
443
|
+
def process(text: str):
|
444
|
+
if clean_caption:
|
445
|
+
text = self._clean_caption(text)
|
446
|
+
text = self._clean_caption(text)
|
447
|
+
else:
|
448
|
+
text = text.lower().strip()
|
449
|
+
return text
|
450
|
+
|
451
|
+
return [process(t) for t in text]
|
452
|
+
|
453
|
+
# Copied from diffusers.pipelines.deepfloyd_if.pipeline_if.IFPipeline._clean_caption
|
454
|
+
def _clean_caption(self, caption):
|
455
|
+
caption = str(caption)
|
456
|
+
caption = ul.unquote_plus(caption)
|
457
|
+
caption = caption.strip().lower()
|
458
|
+
caption = re.sub("<person>", "person", caption)
|
459
|
+
# urls:
|
460
|
+
caption = re.sub(
|
461
|
+
r"\b((?:https?:(?:\/{1,3}|[a-zA-Z0-9%])|[a-zA-Z0-9.\-]+[.](?:com|co|ru|net|org|edu|gov|it)[\w/-]*\b\/?(?!@)))", # noqa
|
462
|
+
"",
|
463
|
+
caption,
|
464
|
+
) # regex for urls
|
465
|
+
caption = re.sub(
|
466
|
+
r"\b((?:www:(?:\/{1,3}|[a-zA-Z0-9%])|[a-zA-Z0-9.\-]+[.](?:com|co|ru|net|org|edu|gov|it)[\w/-]*\b\/?(?!@)))", # noqa
|
467
|
+
"",
|
468
|
+
caption,
|
469
|
+
) # regex for urls
|
470
|
+
# html:
|
471
|
+
caption = BeautifulSoup(caption, features="html.parser").text
|
472
|
+
|
473
|
+
# @<nickname>
|
474
|
+
caption = re.sub(r"@[\w\d]+\b", "", caption)
|
475
|
+
|
476
|
+
# 31C0—31EF CJK Strokes
|
477
|
+
# 31F0—31FF Katakana Phonetic Extensions
|
478
|
+
# 3200—32FF Enclosed CJK Letters and Months
|
479
|
+
# 3300—33FF CJK Compatibility
|
480
|
+
# 3400—4DBF CJK Unified Ideographs Extension A
|
481
|
+
# 4DC0—4DFF Yijing Hexagram Symbols
|
482
|
+
# 4E00—9FFF CJK Unified Ideographs
|
483
|
+
caption = re.sub(r"[\u31c0-\u31ef]+", "", caption)
|
484
|
+
caption = re.sub(r"[\u31f0-\u31ff]+", "", caption)
|
485
|
+
caption = re.sub(r"[\u3200-\u32ff]+", "", caption)
|
486
|
+
caption = re.sub(r"[\u3300-\u33ff]+", "", caption)
|
487
|
+
caption = re.sub(r"[\u3400-\u4dbf]+", "", caption)
|
488
|
+
caption = re.sub(r"[\u4dc0-\u4dff]+", "", caption)
|
489
|
+
caption = re.sub(r"[\u4e00-\u9fff]+", "", caption)
|
490
|
+
#######################################################
|
491
|
+
|
492
|
+
# все виды тире / all types of dash --> "-"
|
493
|
+
caption = re.sub(
|
494
|
+
r"[\u002D\u058A\u05BE\u1400\u1806\u2010-\u2015\u2E17\u2E1A\u2E3A\u2E3B\u2E40\u301C\u3030\u30A0\uFE31\uFE32\uFE58\uFE63\uFF0D]+", # noqa
|
495
|
+
"-",
|
496
|
+
caption,
|
497
|
+
)
|
498
|
+
|
499
|
+
# кавычки к одному стандарту
|
500
|
+
caption = re.sub(r"[`´«»“”¨]", '"', caption)
|
501
|
+
caption = re.sub(r"[‘’]", "'", caption)
|
502
|
+
|
503
|
+
# "
|
504
|
+
caption = re.sub(r""?", "", caption)
|
505
|
+
# &
|
506
|
+
caption = re.sub(r"&", "", caption)
|
507
|
+
|
508
|
+
# ip adresses:
|
509
|
+
caption = re.sub(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", " ", caption)
|
510
|
+
|
511
|
+
# article ids:
|
512
|
+
caption = re.sub(r"\d:\d\d\s+$", "", caption)
|
513
|
+
|
514
|
+
# \n
|
515
|
+
caption = re.sub(r"\\n", " ", caption)
|
516
|
+
|
517
|
+
# "#123"
|
518
|
+
caption = re.sub(r"#\d{1,3}\b", "", caption)
|
519
|
+
# "#12345.."
|
520
|
+
caption = re.sub(r"#\d{5,}\b", "", caption)
|
521
|
+
# "123456.."
|
522
|
+
caption = re.sub(r"\b\d{6,}\b", "", caption)
|
523
|
+
# filenames:
|
524
|
+
caption = re.sub(r"[\S]+\.(?:png|jpg|jpeg|bmp|webp|eps|pdf|apk|mp4)", "", caption)
|
525
|
+
|
526
|
+
#
|
527
|
+
caption = re.sub(r"[\"\']{2,}", r'"', caption) # """AUSVERKAUFT"""
|
528
|
+
caption = re.sub(r"[\.]{2,}", r" ", caption) # """AUSVERKAUFT"""
|
529
|
+
|
530
|
+
caption = re.sub(self.bad_punct_regex, r" ", caption) # ***AUSVERKAUFT***, #AUSVERKAUFT
|
531
|
+
caption = re.sub(r"\s+\.\s+", r" ", caption) # " . "
|
532
|
+
|
533
|
+
# this-is-my-cute-cat / this_is_my_cute_cat
|
534
|
+
regex2 = re.compile(r"(?:\-|\_)")
|
535
|
+
if len(re.findall(regex2, caption)) > 3:
|
536
|
+
caption = re.sub(regex2, " ", caption)
|
537
|
+
|
538
|
+
caption = ftfy.fix_text(caption)
|
539
|
+
caption = html.unescape(html.unescape(caption))
|
540
|
+
|
541
|
+
caption = re.sub(r"\b[a-zA-Z]{1,3}\d{3,15}\b", "", caption) # jc6640
|
542
|
+
caption = re.sub(r"\b[a-zA-Z]+\d+[a-zA-Z]+\b", "", caption) # jc6640vc
|
543
|
+
caption = re.sub(r"\b\d+[a-zA-Z]+\d+\b", "", caption) # 6640vc231
|
544
|
+
|
545
|
+
caption = re.sub(r"(worldwide\s+)?(free\s+)?shipping", "", caption)
|
546
|
+
caption = re.sub(r"(free\s)?download(\sfree)?", "", caption)
|
547
|
+
caption = re.sub(r"\bclick\b\s(?:for|on)\s\w+", "", caption)
|
548
|
+
caption = re.sub(r"\b(?:png|jpg|jpeg|bmp|webp|eps|pdf|apk|mp4)(\simage[s]?)?", "", caption)
|
549
|
+
caption = re.sub(r"\bpage\s+\d+\b", "", caption)
|
550
|
+
|
551
|
+
caption = re.sub(r"\b\d*[a-zA-Z]+\d+[a-zA-Z]+\d+[a-zA-Z\d]*\b", r" ", caption) # j2d1a2a...
|
552
|
+
|
553
|
+
caption = re.sub(r"\b\d+\.?\d*[xх×]\d+\.?\d*\b", "", caption)
|
554
|
+
|
555
|
+
caption = re.sub(r"\b\s+\:\s+", r": ", caption)
|
556
|
+
caption = re.sub(r"(\D[,\./])\b", r"\1 ", caption)
|
557
|
+
caption = re.sub(r"\s+", " ", caption)
|
558
|
+
|
559
|
+
caption.strip()
|
560
|
+
|
561
|
+
caption = re.sub(r"^[\"\']([\w\W]+)[\"\']$", r"\1", caption)
|
562
|
+
caption = re.sub(r"^[\'\_,\-\:;]", r"", caption)
|
563
|
+
caption = re.sub(r"[\'\_,\-\:\-\+]$", r"", caption)
|
564
|
+
caption = re.sub(r"^\.\S+$", "", caption)
|
565
|
+
|
566
|
+
return caption.strip()
|
567
|
+
|
568
|
+
def prepare_latents(
|
569
|
+
self, batch_size, num_channels_latents, num_frames, height, width, dtype, device, generator, latents=None
|
570
|
+
):
|
571
|
+
if isinstance(generator, list) and len(generator) != batch_size:
|
572
|
+
raise ValueError(
|
573
|
+
f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
|
574
|
+
f" size of {batch_size}. Make sure the batch size matches the length of the generators."
|
575
|
+
)
|
576
|
+
|
577
|
+
if num_frames % 2 == 0:
|
578
|
+
num_frames = math.ceil(num_frames / self.vae_scale_factor_temporal)
|
579
|
+
else:
|
580
|
+
num_frames = math.ceil((num_frames - 1) / self.vae_scale_factor_temporal) + 1
|
581
|
+
|
582
|
+
shape = (
|
583
|
+
batch_size,
|
584
|
+
num_channels_latents,
|
585
|
+
num_frames,
|
586
|
+
height // self.vae_scale_factor_spatial,
|
587
|
+
width // self.vae_scale_factor_spatial,
|
588
|
+
)
|
589
|
+
|
590
|
+
if latents is None:
|
591
|
+
latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
|
592
|
+
else:
|
593
|
+
latents = latents.to(device)
|
594
|
+
|
595
|
+
# scale the initial noise by the standard deviation required by the scheduler
|
596
|
+
latents = latents * self.scheduler.init_noise_sigma
|
597
|
+
return latents
|
598
|
+
|
599
|
+
def decode_latents(self, latents: torch.Tensor) -> torch.Tensor:
|
600
|
+
latents = 1 / self.vae.config.scaling_factor * latents
|
601
|
+
frames = self.vae.decode(latents).sample
|
602
|
+
frames = frames.permute(0, 2, 1, 3, 4) # [batch_size, channels, num_frames, height, width]
|
603
|
+
return frames
|
604
|
+
|
605
|
+
def _prepare_rotary_positional_embeddings(
|
606
|
+
self,
|
607
|
+
batch_size: int,
|
608
|
+
height: int,
|
609
|
+
width: int,
|
610
|
+
num_frames: int,
|
611
|
+
device: torch.device,
|
612
|
+
):
|
613
|
+
grid_height = height // (self.vae_scale_factor_spatial * self.transformer.config.patch_size)
|
614
|
+
grid_width = width // (self.vae_scale_factor_spatial * self.transformer.config.patch_size)
|
615
|
+
|
616
|
+
start, stop = (0, 0), (grid_height, grid_width)
|
617
|
+
freqs_t, freqs_h, freqs_w, grid_t, grid_h, grid_w = get_3d_rotary_pos_embed_allegro(
|
618
|
+
embed_dim=self.transformer.config.attention_head_dim,
|
619
|
+
crops_coords=(start, stop),
|
620
|
+
grid_size=(grid_height, grid_width),
|
621
|
+
temporal_size=num_frames,
|
622
|
+
interpolation_scale=(
|
623
|
+
self.transformer.config.interpolation_scale_t,
|
624
|
+
self.transformer.config.interpolation_scale_h,
|
625
|
+
self.transformer.config.interpolation_scale_w,
|
626
|
+
),
|
627
|
+
device=device,
|
628
|
+
)
|
629
|
+
|
630
|
+
grid_t = grid_t.to(dtype=torch.long)
|
631
|
+
grid_h = grid_h.to(dtype=torch.long)
|
632
|
+
grid_w = grid_w.to(dtype=torch.long)
|
633
|
+
|
634
|
+
pos = torch.cartesian_prod(grid_t, grid_h, grid_w)
|
635
|
+
pos = pos.reshape(-1, 3).transpose(0, 1).reshape(3, 1, -1).contiguous()
|
636
|
+
grid_t, grid_h, grid_w = pos
|
637
|
+
|
638
|
+
return (freqs_t, freqs_h, freqs_w), (grid_t, grid_h, grid_w)
|
639
|
+
|
640
|
+
def enable_vae_slicing(self):
|
641
|
+
r"""
|
642
|
+
Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to
|
643
|
+
compute decoding in several steps. This is useful to save some memory and allow larger batch sizes.
|
644
|
+
"""
|
645
|
+
self.vae.enable_slicing()
|
646
|
+
|
647
|
+
def disable_vae_slicing(self):
|
648
|
+
r"""
|
649
|
+
Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to
|
650
|
+
computing decoding in one step.
|
651
|
+
"""
|
652
|
+
self.vae.disable_slicing()
|
653
|
+
|
654
|
+
def enable_vae_tiling(self):
|
655
|
+
r"""
|
656
|
+
Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to
|
657
|
+
compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow
|
658
|
+
processing larger images.
|
659
|
+
"""
|
660
|
+
self.vae.enable_tiling()
|
661
|
+
|
662
|
+
def disable_vae_tiling(self):
|
663
|
+
r"""
|
664
|
+
Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to
|
665
|
+
computing decoding in one step.
|
666
|
+
"""
|
667
|
+
self.vae.disable_tiling()
|
668
|
+
|
669
|
+
@property
|
670
|
+
def guidance_scale(self):
|
671
|
+
return self._guidance_scale
|
672
|
+
|
673
|
+
@property
|
674
|
+
def num_timesteps(self):
|
675
|
+
return self._num_timesteps
|
676
|
+
|
677
|
+
@property
|
678
|
+
def interrupt(self):
|
679
|
+
return self._interrupt
|
680
|
+
|
681
|
+
@torch.no_grad()
|
682
|
+
@replace_example_docstring(EXAMPLE_DOC_STRING)
|
683
|
+
def __call__(
|
684
|
+
self,
|
685
|
+
prompt: Union[str, List[str]] = None,
|
686
|
+
negative_prompt: str = "",
|
687
|
+
num_inference_steps: int = 100,
|
688
|
+
timesteps: List[int] = None,
|
689
|
+
guidance_scale: float = 7.5,
|
690
|
+
num_frames: Optional[int] = None,
|
691
|
+
height: Optional[int] = None,
|
692
|
+
width: Optional[int] = None,
|
693
|
+
num_videos_per_prompt: int = 1,
|
694
|
+
eta: float = 0.0,
|
695
|
+
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
|
696
|
+
latents: Optional[torch.Tensor] = None,
|
697
|
+
prompt_embeds: Optional[torch.Tensor] = None,
|
698
|
+
prompt_attention_mask: Optional[torch.Tensor] = None,
|
699
|
+
negative_prompt_embeds: Optional[torch.Tensor] = None,
|
700
|
+
negative_prompt_attention_mask: Optional[torch.Tensor] = None,
|
701
|
+
output_type: Optional[str] = "pil",
|
702
|
+
return_dict: bool = True,
|
703
|
+
callback_on_step_end: Optional[
|
704
|
+
Union[Callable[[int, int, Dict], None], PipelineCallback, MultiPipelineCallbacks]
|
705
|
+
] = None,
|
706
|
+
callback_on_step_end_tensor_inputs: List[str] = ["latents"],
|
707
|
+
clean_caption: bool = True,
|
708
|
+
max_sequence_length: int = 512,
|
709
|
+
) -> Union[AllegroPipelineOutput, Tuple]:
|
710
|
+
"""
|
711
|
+
Function invoked when calling the pipeline for generation.
|
712
|
+
|
713
|
+
Args:
|
714
|
+
prompt (`str` or `List[str]`, *optional*):
|
715
|
+
The prompt or prompts to guide the video generation. If not defined, one has to pass `prompt_embeds`.
|
716
|
+
instead.
|
717
|
+
negative_prompt (`str` or `List[str]`, *optional*):
|
718
|
+
The prompt or prompts not to guide the video generation. If not defined, one has to pass
|
719
|
+
`negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
|
720
|
+
less than `1`).
|
721
|
+
num_inference_steps (`int`, *optional*, defaults to 100):
|
722
|
+
The number of denoising steps. More denoising steps usually lead to a higher quality video at the
|
723
|
+
expense of slower inference.
|
724
|
+
timesteps (`List[int]`, *optional*):
|
725
|
+
Custom timesteps to use for the denoising process. If not defined, equal spaced `num_inference_steps`
|
726
|
+
timesteps are used. Must be in descending order.
|
727
|
+
guidance_scale (`float`, *optional*, defaults to 7.5):
|
728
|
+
Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598).
|
729
|
+
`guidance_scale` is defined as `w` of equation 2. of [Imagen
|
730
|
+
Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale >
|
731
|
+
1`. Higher guidance scale encourages to generate videos that are closely linked to the text `prompt`,
|
732
|
+
usually at the expense of lower video quality.
|
733
|
+
num_videos_per_prompt (`int`, *optional*, defaults to 1):
|
734
|
+
The number of videos to generate per prompt.
|
735
|
+
num_frames: (`int`, *optional*, defaults to 88):
|
736
|
+
The number controls the generated video frames.
|
737
|
+
height (`int`, *optional*, defaults to self.unet.config.sample_size):
|
738
|
+
The height in pixels of the generated video.
|
739
|
+
width (`int`, *optional*, defaults to self.unet.config.sample_size):
|
740
|
+
The width in pixels of the generated video.
|
741
|
+
eta (`float`, *optional*, defaults to 0.0):
|
742
|
+
Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to
|
743
|
+
[`schedulers.DDIMScheduler`], will be ignored for others.
|
744
|
+
generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
|
745
|
+
One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
|
746
|
+
to make generation deterministic.
|
747
|
+
latents (`torch.Tensor`, *optional*):
|
748
|
+
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
|
749
|
+
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for video
|
750
|
+
tensor will ge generated by sampling using the supplied random `generator`.
|
751
|
+
prompt_embeds (`torch.Tensor`, *optional*):
|
752
|
+
Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
|
753
|
+
provided, text embeddings will be generated from `prompt` input argument.
|
754
|
+
prompt_attention_mask (`torch.Tensor`, *optional*): Pre-generated attention mask for text embeddings.
|
755
|
+
negative_prompt_embeds (`torch.Tensor`, *optional*):
|
756
|
+
Pre-generated negative text embeddings. For PixArt-Sigma this negative prompt should be "". If not
|
757
|
+
provided, negative_prompt_embeds will be generated from `negative_prompt` input argument.
|
758
|
+
negative_prompt_attention_mask (`torch.Tensor`, *optional*):
|
759
|
+
Pre-generated attention mask for negative text embeddings.
|
760
|
+
output_type (`str`, *optional*, defaults to `"pil"`):
|
761
|
+
The output format of the generate video. Choose between
|
762
|
+
[PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
|
763
|
+
return_dict (`bool`, *optional*, defaults to `True`):
|
764
|
+
Whether or not to return a [`~pipelines.stable_diffusion.IFPipelineOutput`] instead of a plain tuple.
|
765
|
+
callback (`Callable`, *optional*):
|
766
|
+
A function that will be called every `callback_steps` steps during inference. The function will be
|
767
|
+
called with the following arguments: `callback(step: int, timestep: int, latents: torch.Tensor)`.
|
768
|
+
callback_steps (`int`, *optional*, defaults to 1):
|
769
|
+
The frequency at which the `callback` function will be called. If not specified, the callback will be
|
770
|
+
called at every step.
|
771
|
+
clean_caption (`bool`, *optional*, defaults to `True`):
|
772
|
+
Whether or not to clean the caption before creating embeddings. Requires `beautifulsoup4` and `ftfy` to
|
773
|
+
be installed. If the dependencies are not installed, the embeddings will be created from the raw
|
774
|
+
prompt.
|
775
|
+
max_sequence_length (`int` defaults to `512`):
|
776
|
+
Maximum sequence length to use with the `prompt`.
|
777
|
+
|
778
|
+
Examples:
|
779
|
+
|
780
|
+
Returns:
|
781
|
+
[`~pipelines.allegro.pipeline_output.AllegroPipelineOutput`] or `tuple`:
|
782
|
+
If `return_dict` is `True`, [`~pipelines.allegro.pipeline_output.AllegroPipelineOutput`] is returned,
|
783
|
+
otherwise a `tuple` is returned where the first element is a list with the generated videos.
|
784
|
+
"""
|
785
|
+
|
786
|
+
if isinstance(callback_on_step_end, (PipelineCallback, MultiPipelineCallbacks)):
|
787
|
+
callback_on_step_end_tensor_inputs = callback_on_step_end.tensor_inputs
|
788
|
+
|
789
|
+
num_videos_per_prompt = 1
|
790
|
+
|
791
|
+
# 1. Check inputs. Raise error if not correct
|
792
|
+
num_frames = num_frames or self.transformer.config.sample_frames * self.vae_scale_factor_temporal
|
793
|
+
height = height or self.transformer.config.sample_height * self.vae_scale_factor_spatial
|
794
|
+
width = width or self.transformer.config.sample_width * self.vae_scale_factor_spatial
|
795
|
+
|
796
|
+
self.check_inputs(
|
797
|
+
prompt,
|
798
|
+
num_frames,
|
799
|
+
height,
|
800
|
+
width,
|
801
|
+
callback_on_step_end_tensor_inputs,
|
802
|
+
negative_prompt,
|
803
|
+
prompt_embeds,
|
804
|
+
negative_prompt_embeds,
|
805
|
+
prompt_attention_mask,
|
806
|
+
negative_prompt_attention_mask,
|
807
|
+
)
|
808
|
+
self._guidance_scale = guidance_scale
|
809
|
+
self._interrupt = False
|
810
|
+
|
811
|
+
# 2. Default height and width to transformer
|
812
|
+
if prompt is not None and isinstance(prompt, str):
|
813
|
+
batch_size = 1
|
814
|
+
elif prompt is not None and isinstance(prompt, list):
|
815
|
+
batch_size = len(prompt)
|
816
|
+
else:
|
817
|
+
batch_size = prompt_embeds.shape[0]
|
818
|
+
|
819
|
+
device = self._execution_device
|
820
|
+
|
821
|
+
# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
|
822
|
+
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
|
823
|
+
# corresponds to doing no classifier free guidance.
|
824
|
+
do_classifier_free_guidance = guidance_scale > 1.0
|
825
|
+
|
826
|
+
# 3. Encode input prompt
|
827
|
+
(
|
828
|
+
prompt_embeds,
|
829
|
+
prompt_attention_mask,
|
830
|
+
negative_prompt_embeds,
|
831
|
+
negative_prompt_attention_mask,
|
832
|
+
) = self.encode_prompt(
|
833
|
+
prompt,
|
834
|
+
do_classifier_free_guidance,
|
835
|
+
negative_prompt=negative_prompt,
|
836
|
+
num_videos_per_prompt=num_videos_per_prompt,
|
837
|
+
device=device,
|
838
|
+
prompt_embeds=prompt_embeds,
|
839
|
+
negative_prompt_embeds=negative_prompt_embeds,
|
840
|
+
prompt_attention_mask=prompt_attention_mask,
|
841
|
+
negative_prompt_attention_mask=negative_prompt_attention_mask,
|
842
|
+
clean_caption=clean_caption,
|
843
|
+
max_sequence_length=max_sequence_length,
|
844
|
+
)
|
845
|
+
if do_classifier_free_guidance:
|
846
|
+
prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0)
|
847
|
+
prompt_attention_mask = torch.cat([negative_prompt_attention_mask, prompt_attention_mask], dim=0)
|
848
|
+
if prompt_embeds.ndim == 3:
|
849
|
+
prompt_embeds = prompt_embeds.unsqueeze(1) # b l d -> b 1 l d
|
850
|
+
|
851
|
+
# 4. Prepare timesteps
|
852
|
+
timesteps, num_inference_steps = retrieve_timesteps(self.scheduler, num_inference_steps, device, timesteps)
|
853
|
+
self.scheduler.set_timesteps(num_inference_steps, device=device)
|
854
|
+
|
855
|
+
# 5. Prepare latents.
|
856
|
+
latent_channels = self.transformer.config.in_channels
|
857
|
+
latents = self.prepare_latents(
|
858
|
+
batch_size * num_videos_per_prompt,
|
859
|
+
latent_channels,
|
860
|
+
num_frames,
|
861
|
+
height,
|
862
|
+
width,
|
863
|
+
prompt_embeds.dtype,
|
864
|
+
device,
|
865
|
+
generator,
|
866
|
+
latents,
|
867
|
+
)
|
868
|
+
|
869
|
+
# 6. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
|
870
|
+
extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
|
871
|
+
|
872
|
+
# 7. Prepare rotary embeddings
|
873
|
+
image_rotary_emb = self._prepare_rotary_positional_embeddings(
|
874
|
+
batch_size, height, width, latents.size(2), device
|
875
|
+
)
|
876
|
+
|
877
|
+
# 8. Denoising loop
|
878
|
+
num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0)
|
879
|
+
self._num_timesteps = len(timesteps)
|
880
|
+
|
881
|
+
with self.progress_bar(total=num_inference_steps) as progress_bar:
|
882
|
+
for i, t in enumerate(timesteps):
|
883
|
+
if self.interrupt:
|
884
|
+
continue
|
885
|
+
|
886
|
+
latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
|
887
|
+
latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
|
888
|
+
|
889
|
+
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
|
890
|
+
timestep = t.expand(latent_model_input.shape[0])
|
891
|
+
|
892
|
+
# predict noise model_output
|
893
|
+
noise_pred = self.transformer(
|
894
|
+
hidden_states=latent_model_input,
|
895
|
+
encoder_hidden_states=prompt_embeds,
|
896
|
+
encoder_attention_mask=prompt_attention_mask,
|
897
|
+
timestep=timestep,
|
898
|
+
image_rotary_emb=image_rotary_emb,
|
899
|
+
return_dict=False,
|
900
|
+
)[0]
|
901
|
+
|
902
|
+
# perform guidance
|
903
|
+
if do_classifier_free_guidance:
|
904
|
+
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
|
905
|
+
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
|
906
|
+
|
907
|
+
# compute previous image: x_t -> x_t-1
|
908
|
+
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
|
909
|
+
|
910
|
+
# call the callback, if provided
|
911
|
+
if callback_on_step_end is not None:
|
912
|
+
callback_kwargs = {}
|
913
|
+
for k in callback_on_step_end_tensor_inputs:
|
914
|
+
callback_kwargs[k] = locals()[k]
|
915
|
+
callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
|
916
|
+
|
917
|
+
latents = callback_outputs.pop("latents", latents)
|
918
|
+
prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
|
919
|
+
negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds)
|
920
|
+
|
921
|
+
if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
|
922
|
+
progress_bar.update()
|
923
|
+
|
924
|
+
if not output_type == "latent":
|
925
|
+
latents = latents.to(self.vae.dtype)
|
926
|
+
video = self.decode_latents(latents)
|
927
|
+
video = video[:, :, :num_frames, :height, :width]
|
928
|
+
video = self.video_processor.postprocess_video(video=video, output_type=output_type)
|
929
|
+
else:
|
930
|
+
video = latents
|
931
|
+
|
932
|
+
# Offload all models
|
933
|
+
self.maybe_free_model_hooks()
|
934
|
+
|
935
|
+
if not return_dict:
|
936
|
+
return (video,)
|
937
|
+
|
938
|
+
return AllegroPipelineOutput(frames=video)
|