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,756 @@
|
|
1
|
+
# Copyright 2024 Stability AI 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 typing import Callable, List, Optional, Union
|
17
|
+
|
18
|
+
import torch
|
19
|
+
from transformers import (
|
20
|
+
T5EncoderModel,
|
21
|
+
T5Tokenizer,
|
22
|
+
T5TokenizerFast,
|
23
|
+
)
|
24
|
+
|
25
|
+
from ...models import AutoencoderOobleck, StableAudioDiTModel
|
26
|
+
from ...models.embeddings import get_1d_rotary_pos_embed
|
27
|
+
from ...schedulers import EDMDPMSolverMultistepScheduler
|
28
|
+
from ...utils import (
|
29
|
+
is_torch_xla_available,
|
30
|
+
logging,
|
31
|
+
replace_example_docstring,
|
32
|
+
)
|
33
|
+
from ...utils.torch_utils import randn_tensor
|
34
|
+
from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline
|
35
|
+
from .modeling_stable_audio import StableAudioProjectionModel
|
36
|
+
|
37
|
+
|
38
|
+
if is_torch_xla_available():
|
39
|
+
import torch_xla.core.xla_model as xm
|
40
|
+
|
41
|
+
XLA_AVAILABLE = True
|
42
|
+
else:
|
43
|
+
XLA_AVAILABLE = False
|
44
|
+
|
45
|
+
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
46
|
+
|
47
|
+
EXAMPLE_DOC_STRING = """
|
48
|
+
Examples:
|
49
|
+
```py
|
50
|
+
>>> import scipy
|
51
|
+
>>> import torch
|
52
|
+
>>> import soundfile as sf
|
53
|
+
>>> from diffusers import StableAudioPipeline
|
54
|
+
|
55
|
+
>>> repo_id = "stabilityai/stable-audio-open-1.0"
|
56
|
+
>>> pipe = StableAudioPipeline.from_pretrained(repo_id, torch_dtype=torch.float16)
|
57
|
+
>>> pipe = pipe.to("cuda")
|
58
|
+
|
59
|
+
>>> # define the prompts
|
60
|
+
>>> prompt = "The sound of a hammer hitting a wooden surface."
|
61
|
+
>>> negative_prompt = "Low quality."
|
62
|
+
|
63
|
+
>>> # set the seed for generator
|
64
|
+
>>> generator = torch.Generator("cuda").manual_seed(0)
|
65
|
+
|
66
|
+
>>> # run the generation
|
67
|
+
>>> audio = pipe(
|
68
|
+
... prompt,
|
69
|
+
... negative_prompt=negative_prompt,
|
70
|
+
... num_inference_steps=200,
|
71
|
+
... audio_end_in_s=10.0,
|
72
|
+
... num_waveforms_per_prompt=3,
|
73
|
+
... generator=generator,
|
74
|
+
... ).audios
|
75
|
+
|
76
|
+
>>> output = audio[0].T.float().cpu().numpy()
|
77
|
+
>>> sf.write("hammer.wav", output, pipe.vae.sampling_rate)
|
78
|
+
```
|
79
|
+
"""
|
80
|
+
|
81
|
+
|
82
|
+
class StableAudioPipeline(DiffusionPipeline):
|
83
|
+
r"""
|
84
|
+
Pipeline for text-to-audio generation using StableAudio.
|
85
|
+
|
86
|
+
This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods
|
87
|
+
implemented for all pipelines (downloading, saving, running on a particular device, etc.).
|
88
|
+
|
89
|
+
Args:
|
90
|
+
vae ([`AutoencoderOobleck`]):
|
91
|
+
Variational Auto-Encoder (VAE) model to encode and decode images to and from latent representations.
|
92
|
+
text_encoder ([`~transformers.T5EncoderModel`]):
|
93
|
+
Frozen text-encoder. StableAudio uses the encoder of
|
94
|
+
[T5](https://huggingface.co/docs/transformers/model_doc/t5#transformers.T5EncoderModel), specifically the
|
95
|
+
[google-t5/t5-base](https://huggingface.co/google-t5/t5-base) variant.
|
96
|
+
projection_model ([`StableAudioProjectionModel`]):
|
97
|
+
A trained model used to linearly project the hidden-states from the text encoder model and the start and
|
98
|
+
end seconds. The projected hidden-states from the encoder and the conditional seconds are concatenated to
|
99
|
+
give the input to the transformer model.
|
100
|
+
tokenizer ([`~transformers.T5Tokenizer`]):
|
101
|
+
Tokenizer to tokenize text for the frozen text-encoder.
|
102
|
+
transformer ([`StableAudioDiTModel`]):
|
103
|
+
A `StableAudioDiTModel` to denoise the encoded audio latents.
|
104
|
+
scheduler ([`EDMDPMSolverMultistepScheduler`]):
|
105
|
+
A scheduler to be used in combination with `transformer` to denoise the encoded audio latents.
|
106
|
+
"""
|
107
|
+
|
108
|
+
model_cpu_offload_seq = "text_encoder->projection_model->transformer->vae"
|
109
|
+
|
110
|
+
def __init__(
|
111
|
+
self,
|
112
|
+
vae: AutoencoderOobleck,
|
113
|
+
text_encoder: T5EncoderModel,
|
114
|
+
projection_model: StableAudioProjectionModel,
|
115
|
+
tokenizer: Union[T5Tokenizer, T5TokenizerFast],
|
116
|
+
transformer: StableAudioDiTModel,
|
117
|
+
scheduler: EDMDPMSolverMultistepScheduler,
|
118
|
+
):
|
119
|
+
super().__init__()
|
120
|
+
|
121
|
+
self.register_modules(
|
122
|
+
vae=vae,
|
123
|
+
text_encoder=text_encoder,
|
124
|
+
projection_model=projection_model,
|
125
|
+
tokenizer=tokenizer,
|
126
|
+
transformer=transformer,
|
127
|
+
scheduler=scheduler,
|
128
|
+
)
|
129
|
+
self.rotary_embed_dim = self.transformer.config.attention_head_dim // 2
|
130
|
+
|
131
|
+
# Copied from diffusers.pipelines.pipeline_utils.StableDiffusionMixin.enable_vae_slicing
|
132
|
+
def enable_vae_slicing(self):
|
133
|
+
r"""
|
134
|
+
Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to
|
135
|
+
compute decoding in several steps. This is useful to save some memory and allow larger batch sizes.
|
136
|
+
"""
|
137
|
+
self.vae.enable_slicing()
|
138
|
+
|
139
|
+
# Copied from diffusers.pipelines.pipeline_utils.StableDiffusionMixin.disable_vae_slicing
|
140
|
+
def disable_vae_slicing(self):
|
141
|
+
r"""
|
142
|
+
Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to
|
143
|
+
computing decoding in one step.
|
144
|
+
"""
|
145
|
+
self.vae.disable_slicing()
|
146
|
+
|
147
|
+
def encode_prompt(
|
148
|
+
self,
|
149
|
+
prompt,
|
150
|
+
device,
|
151
|
+
do_classifier_free_guidance,
|
152
|
+
negative_prompt=None,
|
153
|
+
prompt_embeds: Optional[torch.Tensor] = None,
|
154
|
+
negative_prompt_embeds: Optional[torch.Tensor] = None,
|
155
|
+
attention_mask: Optional[torch.LongTensor] = None,
|
156
|
+
negative_attention_mask: Optional[torch.LongTensor] = None,
|
157
|
+
):
|
158
|
+
if prompt is not None and isinstance(prompt, str):
|
159
|
+
batch_size = 1
|
160
|
+
elif prompt is not None and isinstance(prompt, list):
|
161
|
+
batch_size = len(prompt)
|
162
|
+
else:
|
163
|
+
batch_size = prompt_embeds.shape[0]
|
164
|
+
|
165
|
+
if prompt_embeds is None:
|
166
|
+
# 1. Tokenize text
|
167
|
+
text_inputs = self.tokenizer(
|
168
|
+
prompt,
|
169
|
+
padding="max_length",
|
170
|
+
max_length=self.tokenizer.model_max_length,
|
171
|
+
truncation=True,
|
172
|
+
return_tensors="pt",
|
173
|
+
)
|
174
|
+
text_input_ids = text_inputs.input_ids
|
175
|
+
attention_mask = text_inputs.attention_mask
|
176
|
+
untruncated_ids = self.tokenizer(prompt, padding="longest", return_tensors="pt").input_ids
|
177
|
+
|
178
|
+
if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
|
179
|
+
text_input_ids, untruncated_ids
|
180
|
+
):
|
181
|
+
removed_text = self.tokenizer.batch_decode(
|
182
|
+
untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]
|
183
|
+
)
|
184
|
+
logger.warning(
|
185
|
+
f"The following part of your input was truncated because {self.text_encoder.config.model_type} can "
|
186
|
+
f"only handle sequences up to {self.tokenizer.model_max_length} tokens: {removed_text}"
|
187
|
+
)
|
188
|
+
|
189
|
+
text_input_ids = text_input_ids.to(device)
|
190
|
+
attention_mask = attention_mask.to(device)
|
191
|
+
|
192
|
+
# 2. Text encoder forward
|
193
|
+
self.text_encoder.eval()
|
194
|
+
prompt_embeds = self.text_encoder(
|
195
|
+
text_input_ids,
|
196
|
+
attention_mask=attention_mask,
|
197
|
+
)
|
198
|
+
prompt_embeds = prompt_embeds[0]
|
199
|
+
|
200
|
+
if do_classifier_free_guidance and negative_prompt is not None:
|
201
|
+
uncond_tokens: List[str]
|
202
|
+
if type(prompt) is not type(negative_prompt):
|
203
|
+
raise TypeError(
|
204
|
+
f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !="
|
205
|
+
f" {type(prompt)}."
|
206
|
+
)
|
207
|
+
elif isinstance(negative_prompt, str):
|
208
|
+
uncond_tokens = [negative_prompt]
|
209
|
+
elif batch_size != len(negative_prompt):
|
210
|
+
raise ValueError(
|
211
|
+
f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:"
|
212
|
+
f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
|
213
|
+
" the batch size of `prompt`."
|
214
|
+
)
|
215
|
+
else:
|
216
|
+
uncond_tokens = negative_prompt
|
217
|
+
|
218
|
+
# 1. Tokenize text
|
219
|
+
uncond_input = self.tokenizer(
|
220
|
+
uncond_tokens,
|
221
|
+
padding="max_length",
|
222
|
+
max_length=self.tokenizer.model_max_length,
|
223
|
+
truncation=True,
|
224
|
+
return_tensors="pt",
|
225
|
+
)
|
226
|
+
|
227
|
+
uncond_input_ids = uncond_input.input_ids.to(device)
|
228
|
+
negative_attention_mask = uncond_input.attention_mask.to(device)
|
229
|
+
|
230
|
+
# 2. Text encoder forward
|
231
|
+
self.text_encoder.eval()
|
232
|
+
negative_prompt_embeds = self.text_encoder(
|
233
|
+
uncond_input_ids,
|
234
|
+
attention_mask=negative_attention_mask,
|
235
|
+
)
|
236
|
+
negative_prompt_embeds = negative_prompt_embeds[0]
|
237
|
+
|
238
|
+
if negative_attention_mask is not None:
|
239
|
+
# set the masked tokens to the null embed
|
240
|
+
negative_prompt_embeds = torch.where(
|
241
|
+
negative_attention_mask.to(torch.bool).unsqueeze(2), negative_prompt_embeds, 0.0
|
242
|
+
)
|
243
|
+
|
244
|
+
# 3. Project prompt_embeds and negative_prompt_embeds
|
245
|
+
if do_classifier_free_guidance and negative_prompt_embeds is not None:
|
246
|
+
# For classifier free guidance, we need to do two forward passes.
|
247
|
+
# Here we concatenate the negative and text embeddings into a single batch
|
248
|
+
# to avoid doing two forward passes
|
249
|
+
prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds])
|
250
|
+
if attention_mask is not None and negative_attention_mask is None:
|
251
|
+
negative_attention_mask = torch.ones_like(attention_mask)
|
252
|
+
elif attention_mask is None and negative_attention_mask is not None:
|
253
|
+
attention_mask = torch.ones_like(negative_attention_mask)
|
254
|
+
|
255
|
+
if attention_mask is not None:
|
256
|
+
attention_mask = torch.cat([negative_attention_mask, attention_mask])
|
257
|
+
|
258
|
+
prompt_embeds = self.projection_model(
|
259
|
+
text_hidden_states=prompt_embeds,
|
260
|
+
).text_hidden_states
|
261
|
+
if attention_mask is not None:
|
262
|
+
prompt_embeds = prompt_embeds * attention_mask.unsqueeze(-1).to(prompt_embeds.dtype)
|
263
|
+
prompt_embeds = prompt_embeds * attention_mask.unsqueeze(-1).to(prompt_embeds.dtype)
|
264
|
+
|
265
|
+
return prompt_embeds
|
266
|
+
|
267
|
+
def encode_duration(
|
268
|
+
self,
|
269
|
+
audio_start_in_s,
|
270
|
+
audio_end_in_s,
|
271
|
+
device,
|
272
|
+
do_classifier_free_guidance,
|
273
|
+
batch_size,
|
274
|
+
):
|
275
|
+
audio_start_in_s = audio_start_in_s if isinstance(audio_start_in_s, list) else [audio_start_in_s]
|
276
|
+
audio_end_in_s = audio_end_in_s if isinstance(audio_end_in_s, list) else [audio_end_in_s]
|
277
|
+
|
278
|
+
if len(audio_start_in_s) == 1:
|
279
|
+
audio_start_in_s = audio_start_in_s * batch_size
|
280
|
+
if len(audio_end_in_s) == 1:
|
281
|
+
audio_end_in_s = audio_end_in_s * batch_size
|
282
|
+
|
283
|
+
# Cast the inputs to floats
|
284
|
+
audio_start_in_s = [float(x) for x in audio_start_in_s]
|
285
|
+
audio_start_in_s = torch.tensor(audio_start_in_s).to(device)
|
286
|
+
|
287
|
+
audio_end_in_s = [float(x) for x in audio_end_in_s]
|
288
|
+
audio_end_in_s = torch.tensor(audio_end_in_s).to(device)
|
289
|
+
|
290
|
+
projection_output = self.projection_model(
|
291
|
+
start_seconds=audio_start_in_s,
|
292
|
+
end_seconds=audio_end_in_s,
|
293
|
+
)
|
294
|
+
seconds_start_hidden_states = projection_output.seconds_start_hidden_states
|
295
|
+
seconds_end_hidden_states = projection_output.seconds_end_hidden_states
|
296
|
+
|
297
|
+
# For classifier free guidance, we need to do two forward passes.
|
298
|
+
# Here we repeat the audio hidden states to avoid doing two forward passes
|
299
|
+
if do_classifier_free_guidance:
|
300
|
+
seconds_start_hidden_states = torch.cat([seconds_start_hidden_states, seconds_start_hidden_states], dim=0)
|
301
|
+
seconds_end_hidden_states = torch.cat([seconds_end_hidden_states, seconds_end_hidden_states], dim=0)
|
302
|
+
|
303
|
+
return seconds_start_hidden_states, seconds_end_hidden_states
|
304
|
+
|
305
|
+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_extra_step_kwargs
|
306
|
+
def prepare_extra_step_kwargs(self, generator, eta):
|
307
|
+
# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
|
308
|
+
# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
|
309
|
+
# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
|
310
|
+
# and should be between [0, 1]
|
311
|
+
|
312
|
+
accepts_eta = "eta" in set(inspect.signature(self.scheduler.step).parameters.keys())
|
313
|
+
extra_step_kwargs = {}
|
314
|
+
if accepts_eta:
|
315
|
+
extra_step_kwargs["eta"] = eta
|
316
|
+
|
317
|
+
# check if the scheduler accepts generator
|
318
|
+
accepts_generator = "generator" in set(inspect.signature(self.scheduler.step).parameters.keys())
|
319
|
+
if accepts_generator:
|
320
|
+
extra_step_kwargs["generator"] = generator
|
321
|
+
return extra_step_kwargs
|
322
|
+
|
323
|
+
def check_inputs(
|
324
|
+
self,
|
325
|
+
prompt,
|
326
|
+
audio_start_in_s,
|
327
|
+
audio_end_in_s,
|
328
|
+
callback_steps,
|
329
|
+
negative_prompt=None,
|
330
|
+
prompt_embeds=None,
|
331
|
+
negative_prompt_embeds=None,
|
332
|
+
attention_mask=None,
|
333
|
+
negative_attention_mask=None,
|
334
|
+
initial_audio_waveforms=None,
|
335
|
+
initial_audio_sampling_rate=None,
|
336
|
+
):
|
337
|
+
if audio_end_in_s < audio_start_in_s:
|
338
|
+
raise ValueError(
|
339
|
+
f"`audio_end_in_s={audio_end_in_s}' must be higher than 'audio_start_in_s={audio_start_in_s}` but "
|
340
|
+
)
|
341
|
+
|
342
|
+
if (
|
343
|
+
audio_start_in_s < self.projection_model.config.min_value
|
344
|
+
or audio_start_in_s > self.projection_model.config.max_value
|
345
|
+
):
|
346
|
+
raise ValueError(
|
347
|
+
f"`audio_start_in_s` must be greater than or equal to {self.projection_model.config.min_value}, and lower than or equal to {self.projection_model.config.max_value} but "
|
348
|
+
f"is {audio_start_in_s}."
|
349
|
+
)
|
350
|
+
|
351
|
+
if (
|
352
|
+
audio_end_in_s < self.projection_model.config.min_value
|
353
|
+
or audio_end_in_s > self.projection_model.config.max_value
|
354
|
+
):
|
355
|
+
raise ValueError(
|
356
|
+
f"`audio_end_in_s` must be greater than or equal to {self.projection_model.config.min_value}, and lower than or equal to {self.projection_model.config.max_value} but "
|
357
|
+
f"is {audio_end_in_s}."
|
358
|
+
)
|
359
|
+
|
360
|
+
if (callback_steps is None) or (
|
361
|
+
callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0)
|
362
|
+
):
|
363
|
+
raise ValueError(
|
364
|
+
f"`callback_steps` has to be a positive integer but is {callback_steps} of type"
|
365
|
+
f" {type(callback_steps)}."
|
366
|
+
)
|
367
|
+
|
368
|
+
if prompt is not None and prompt_embeds is not None:
|
369
|
+
raise ValueError(
|
370
|
+
f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
|
371
|
+
" only forward one of the two."
|
372
|
+
)
|
373
|
+
elif prompt is None and (prompt_embeds is None):
|
374
|
+
raise ValueError(
|
375
|
+
"Provide either `prompt`, or `prompt_embeds`. Cannot leave"
|
376
|
+
"`prompt` undefined without specifying `prompt_embeds`."
|
377
|
+
)
|
378
|
+
elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)):
|
379
|
+
raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
|
380
|
+
|
381
|
+
if negative_prompt is not None and negative_prompt_embeds is not None:
|
382
|
+
raise ValueError(
|
383
|
+
f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:"
|
384
|
+
f" {negative_prompt_embeds}. Please make sure to only forward one of the two."
|
385
|
+
)
|
386
|
+
|
387
|
+
if prompt_embeds is not None and negative_prompt_embeds is not None:
|
388
|
+
if prompt_embeds.shape != negative_prompt_embeds.shape:
|
389
|
+
raise ValueError(
|
390
|
+
"`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but"
|
391
|
+
f" got: `prompt_embeds` {prompt_embeds.shape} != `negative_prompt_embeds`"
|
392
|
+
f" {negative_prompt_embeds.shape}."
|
393
|
+
)
|
394
|
+
if attention_mask is not None and attention_mask.shape != prompt_embeds.shape[:2]:
|
395
|
+
raise ValueError(
|
396
|
+
"`attention_mask should have the same batch size and sequence length as `prompt_embeds`, but got:"
|
397
|
+
f"`attention_mask: {attention_mask.shape} != `prompt_embeds` {prompt_embeds.shape}"
|
398
|
+
)
|
399
|
+
|
400
|
+
if initial_audio_sampling_rate is None and initial_audio_waveforms is not None:
|
401
|
+
raise ValueError(
|
402
|
+
"`initial_audio_waveforms' is provided but the sampling rate is not. Make sure to pass `initial_audio_sampling_rate`."
|
403
|
+
)
|
404
|
+
|
405
|
+
if initial_audio_sampling_rate is not None and initial_audio_sampling_rate != self.vae.sampling_rate:
|
406
|
+
raise ValueError(
|
407
|
+
f"`initial_audio_sampling_rate` must be {self.vae.hop_length}' but is `{initial_audio_sampling_rate}`."
|
408
|
+
"Make sure to resample the `initial_audio_waveforms` and to correct the sampling rate. "
|
409
|
+
)
|
410
|
+
|
411
|
+
def prepare_latents(
|
412
|
+
self,
|
413
|
+
batch_size,
|
414
|
+
num_channels_vae,
|
415
|
+
sample_size,
|
416
|
+
dtype,
|
417
|
+
device,
|
418
|
+
generator,
|
419
|
+
latents=None,
|
420
|
+
initial_audio_waveforms=None,
|
421
|
+
num_waveforms_per_prompt=None,
|
422
|
+
audio_channels=None,
|
423
|
+
):
|
424
|
+
shape = (batch_size, num_channels_vae, sample_size)
|
425
|
+
if isinstance(generator, list) and len(generator) != batch_size:
|
426
|
+
raise ValueError(
|
427
|
+
f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
|
428
|
+
f" size of {batch_size}. Make sure the batch size matches the length of the generators."
|
429
|
+
)
|
430
|
+
|
431
|
+
if latents is None:
|
432
|
+
latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
|
433
|
+
else:
|
434
|
+
latents = latents.to(device)
|
435
|
+
|
436
|
+
# scale the initial noise by the standard deviation required by the scheduler
|
437
|
+
latents = latents * self.scheduler.init_noise_sigma
|
438
|
+
|
439
|
+
# encode the initial audio for use by the model
|
440
|
+
if initial_audio_waveforms is not None:
|
441
|
+
# check dimension
|
442
|
+
if initial_audio_waveforms.ndim == 2:
|
443
|
+
initial_audio_waveforms = initial_audio_waveforms.unsqueeze(1)
|
444
|
+
elif initial_audio_waveforms.ndim != 3:
|
445
|
+
raise ValueError(
|
446
|
+
f"`initial_audio_waveforms` must be of shape `(batch_size, num_channels, audio_length)` or `(batch_size, audio_length)` but has `{initial_audio_waveforms.ndim}` dimensions"
|
447
|
+
)
|
448
|
+
|
449
|
+
audio_vae_length = int(self.transformer.config.sample_size) * self.vae.hop_length
|
450
|
+
audio_shape = (batch_size // num_waveforms_per_prompt, audio_channels, audio_vae_length)
|
451
|
+
|
452
|
+
# check num_channels
|
453
|
+
if initial_audio_waveforms.shape[1] == 1 and audio_channels == 2:
|
454
|
+
initial_audio_waveforms = initial_audio_waveforms.repeat(1, 2, 1)
|
455
|
+
elif initial_audio_waveforms.shape[1] == 2 and audio_channels == 1:
|
456
|
+
initial_audio_waveforms = initial_audio_waveforms.mean(1, keepdim=True)
|
457
|
+
|
458
|
+
if initial_audio_waveforms.shape[:2] != audio_shape[:2]:
|
459
|
+
raise ValueError(
|
460
|
+
f"`initial_audio_waveforms` must be of shape `(batch_size, num_channels, audio_length)` or `(batch_size, audio_length)` but is of shape `{initial_audio_waveforms.shape}`"
|
461
|
+
)
|
462
|
+
|
463
|
+
# crop or pad
|
464
|
+
audio_length = initial_audio_waveforms.shape[-1]
|
465
|
+
if audio_length < audio_vae_length:
|
466
|
+
logger.warning(
|
467
|
+
f"The provided input waveform is shorter ({audio_length}) than the required audio length ({audio_vae_length}) of the model and will thus be padded."
|
468
|
+
)
|
469
|
+
elif audio_length > audio_vae_length:
|
470
|
+
logger.warning(
|
471
|
+
f"The provided input waveform is longer ({audio_length}) than the required audio length ({audio_vae_length}) of the model and will thus be cropped."
|
472
|
+
)
|
473
|
+
|
474
|
+
audio = initial_audio_waveforms.new_zeros(audio_shape)
|
475
|
+
audio[:, :, : min(audio_length, audio_vae_length)] = initial_audio_waveforms[:, :, :audio_vae_length]
|
476
|
+
|
477
|
+
encoded_audio = self.vae.encode(audio).latent_dist.sample(generator)
|
478
|
+
encoded_audio = encoded_audio.repeat((num_waveforms_per_prompt, 1, 1))
|
479
|
+
latents = encoded_audio + latents
|
480
|
+
return latents
|
481
|
+
|
482
|
+
@torch.no_grad()
|
483
|
+
@replace_example_docstring(EXAMPLE_DOC_STRING)
|
484
|
+
def __call__(
|
485
|
+
self,
|
486
|
+
prompt: Union[str, List[str]] = None,
|
487
|
+
audio_end_in_s: Optional[float] = None,
|
488
|
+
audio_start_in_s: Optional[float] = 0.0,
|
489
|
+
num_inference_steps: int = 100,
|
490
|
+
guidance_scale: float = 7.0,
|
491
|
+
negative_prompt: Optional[Union[str, List[str]]] = None,
|
492
|
+
num_waveforms_per_prompt: Optional[int] = 1,
|
493
|
+
eta: float = 0.0,
|
494
|
+
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
|
495
|
+
latents: Optional[torch.Tensor] = None,
|
496
|
+
initial_audio_waveforms: Optional[torch.Tensor] = None,
|
497
|
+
initial_audio_sampling_rate: Optional[torch.Tensor] = None,
|
498
|
+
prompt_embeds: Optional[torch.Tensor] = None,
|
499
|
+
negative_prompt_embeds: Optional[torch.Tensor] = None,
|
500
|
+
attention_mask: Optional[torch.LongTensor] = None,
|
501
|
+
negative_attention_mask: Optional[torch.LongTensor] = None,
|
502
|
+
return_dict: bool = True,
|
503
|
+
callback: Optional[Callable[[int, int, torch.Tensor], None]] = None,
|
504
|
+
callback_steps: Optional[int] = 1,
|
505
|
+
output_type: Optional[str] = "pt",
|
506
|
+
):
|
507
|
+
r"""
|
508
|
+
The call function to the pipeline for generation.
|
509
|
+
|
510
|
+
Args:
|
511
|
+
prompt (`str` or `List[str]`, *optional*):
|
512
|
+
The prompt or prompts to guide audio generation. If not defined, you need to pass `prompt_embeds`.
|
513
|
+
audio_end_in_s (`float`, *optional*, defaults to 47.55):
|
514
|
+
Audio end index in seconds.
|
515
|
+
audio_start_in_s (`float`, *optional*, defaults to 0):
|
516
|
+
Audio start index in seconds.
|
517
|
+
num_inference_steps (`int`, *optional*, defaults to 100):
|
518
|
+
The number of denoising steps. More denoising steps usually lead to a higher quality audio at the
|
519
|
+
expense of slower inference.
|
520
|
+
guidance_scale (`float`, *optional*, defaults to 7.0):
|
521
|
+
A higher guidance scale value encourages the model to generate audio that is closely linked to the text
|
522
|
+
`prompt` at the expense of lower sound quality. Guidance scale is enabled when `guidance_scale > 1`.
|
523
|
+
negative_prompt (`str` or `List[str]`, *optional*):
|
524
|
+
The prompt or prompts to guide what to not include in audio generation. If not defined, you need to
|
525
|
+
pass `negative_prompt_embeds` instead. Ignored when not using guidance (`guidance_scale < 1`).
|
526
|
+
num_waveforms_per_prompt (`int`, *optional*, defaults to 1):
|
527
|
+
The number of waveforms to generate per prompt.
|
528
|
+
eta (`float`, *optional*, defaults to 0.0):
|
529
|
+
Corresponds to parameter eta (η) from the [DDIM](https://arxiv.org/abs/2010.02502) paper. Only applies
|
530
|
+
to the [`~schedulers.DDIMScheduler`], and is ignored in other schedulers.
|
531
|
+
generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
|
532
|
+
A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make
|
533
|
+
generation deterministic.
|
534
|
+
latents (`torch.Tensor`, *optional*):
|
535
|
+
Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for audio
|
536
|
+
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
|
537
|
+
tensor is generated by sampling using the supplied random `generator`.
|
538
|
+
initial_audio_waveforms (`torch.Tensor`, *optional*):
|
539
|
+
Optional initial audio waveforms to use as the initial audio waveform for generation. Must be of shape
|
540
|
+
`(batch_size, num_channels, audio_length)` or `(batch_size, audio_length)`, where `batch_size`
|
541
|
+
corresponds to the number of prompts passed to the model.
|
542
|
+
initial_audio_sampling_rate (`int`, *optional*):
|
543
|
+
Sampling rate of the `initial_audio_waveforms`, if they are provided. Must be the same as the model.
|
544
|
+
prompt_embeds (`torch.Tensor`, *optional*):
|
545
|
+
Pre-computed text embeddings from the text encoder model. Can be used to easily tweak text inputs,
|
546
|
+
*e.g.* prompt weighting. If not provided, text embeddings will be computed from `prompt` input
|
547
|
+
argument.
|
548
|
+
negative_prompt_embeds (`torch.Tensor`, *optional*):
|
549
|
+
Pre-computed negative text embeddings from the text encoder model. Can be used to easily tweak text
|
550
|
+
inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be computed from
|
551
|
+
`negative_prompt` input argument.
|
552
|
+
attention_mask (`torch.LongTensor`, *optional*):
|
553
|
+
Pre-computed attention mask to be applied to the `prompt_embeds`. If not provided, attention mask will
|
554
|
+
be computed from `prompt` input argument.
|
555
|
+
negative_attention_mask (`torch.LongTensor`, *optional*):
|
556
|
+
Pre-computed attention mask to be applied to the `negative_text_audio_duration_embeds`.
|
557
|
+
return_dict (`bool`, *optional*, defaults to `True`):
|
558
|
+
Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
|
559
|
+
plain tuple.
|
560
|
+
callback (`Callable`, *optional*):
|
561
|
+
A function that calls every `callback_steps` steps during inference. The function is called with the
|
562
|
+
following arguments: `callback(step: int, timestep: int, latents: torch.Tensor)`.
|
563
|
+
callback_steps (`int`, *optional*, defaults to 1):
|
564
|
+
The frequency at which the `callback` function is called. If not specified, the callback is called at
|
565
|
+
every step.
|
566
|
+
output_type (`str`, *optional*, defaults to `"pt"`):
|
567
|
+
The output format of the generated audio. Choose between `"np"` to return a NumPy `np.ndarray` or
|
568
|
+
`"pt"` to return a PyTorch `torch.Tensor` object. Set to `"latent"` to return the latent diffusion
|
569
|
+
model (LDM) output.
|
570
|
+
|
571
|
+
Examples:
|
572
|
+
|
573
|
+
Returns:
|
574
|
+
[`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] or `tuple`:
|
575
|
+
If `return_dict` is `True`, [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] is returned,
|
576
|
+
otherwise a `tuple` is returned where the first element is a list with the generated audio.
|
577
|
+
"""
|
578
|
+
# 0. Convert audio input length from seconds to latent length
|
579
|
+
downsample_ratio = self.vae.hop_length
|
580
|
+
|
581
|
+
max_audio_length_in_s = self.transformer.config.sample_size * downsample_ratio / self.vae.config.sampling_rate
|
582
|
+
if audio_end_in_s is None:
|
583
|
+
audio_end_in_s = max_audio_length_in_s
|
584
|
+
|
585
|
+
if audio_end_in_s - audio_start_in_s > max_audio_length_in_s:
|
586
|
+
raise ValueError(
|
587
|
+
f"The total audio length requested ({audio_end_in_s-audio_start_in_s}s) is longer than the model maximum possible length ({max_audio_length_in_s}). Make sure that 'audio_end_in_s-audio_start_in_s<={max_audio_length_in_s}'."
|
588
|
+
)
|
589
|
+
|
590
|
+
waveform_start = int(audio_start_in_s * self.vae.config.sampling_rate)
|
591
|
+
waveform_end = int(audio_end_in_s * self.vae.config.sampling_rate)
|
592
|
+
waveform_length = int(self.transformer.config.sample_size)
|
593
|
+
|
594
|
+
# 1. Check inputs. Raise error if not correct
|
595
|
+
self.check_inputs(
|
596
|
+
prompt,
|
597
|
+
audio_start_in_s,
|
598
|
+
audio_end_in_s,
|
599
|
+
callback_steps,
|
600
|
+
negative_prompt,
|
601
|
+
prompt_embeds,
|
602
|
+
negative_prompt_embeds,
|
603
|
+
attention_mask,
|
604
|
+
negative_attention_mask,
|
605
|
+
initial_audio_waveforms,
|
606
|
+
initial_audio_sampling_rate,
|
607
|
+
)
|
608
|
+
|
609
|
+
# 2. Define call parameters
|
610
|
+
if prompt is not None and isinstance(prompt, str):
|
611
|
+
batch_size = 1
|
612
|
+
elif prompt is not None and isinstance(prompt, list):
|
613
|
+
batch_size = len(prompt)
|
614
|
+
else:
|
615
|
+
batch_size = prompt_embeds.shape[0]
|
616
|
+
|
617
|
+
device = self._execution_device
|
618
|
+
# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
|
619
|
+
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
|
620
|
+
# corresponds to doing no classifier free guidance.
|
621
|
+
do_classifier_free_guidance = guidance_scale > 1.0
|
622
|
+
|
623
|
+
# 3. Encode input prompt
|
624
|
+
prompt_embeds = self.encode_prompt(
|
625
|
+
prompt,
|
626
|
+
device,
|
627
|
+
do_classifier_free_guidance,
|
628
|
+
negative_prompt,
|
629
|
+
prompt_embeds,
|
630
|
+
negative_prompt_embeds,
|
631
|
+
attention_mask,
|
632
|
+
negative_attention_mask,
|
633
|
+
)
|
634
|
+
|
635
|
+
# Encode duration
|
636
|
+
seconds_start_hidden_states, seconds_end_hidden_states = self.encode_duration(
|
637
|
+
audio_start_in_s,
|
638
|
+
audio_end_in_s,
|
639
|
+
device,
|
640
|
+
do_classifier_free_guidance and (negative_prompt is not None or negative_prompt_embeds is not None),
|
641
|
+
batch_size,
|
642
|
+
)
|
643
|
+
|
644
|
+
# Create text_audio_duration_embeds and audio_duration_embeds
|
645
|
+
text_audio_duration_embeds = torch.cat(
|
646
|
+
[prompt_embeds, seconds_start_hidden_states, seconds_end_hidden_states], dim=1
|
647
|
+
)
|
648
|
+
|
649
|
+
audio_duration_embeds = torch.cat([seconds_start_hidden_states, seconds_end_hidden_states], dim=2)
|
650
|
+
|
651
|
+
# In case of classifier free guidance without negative prompt, we need to create unconditional embeddings and
|
652
|
+
# to concatenate it to the embeddings
|
653
|
+
if do_classifier_free_guidance and negative_prompt_embeds is None and negative_prompt is None:
|
654
|
+
negative_text_audio_duration_embeds = torch.zeros_like(
|
655
|
+
text_audio_duration_embeds, device=text_audio_duration_embeds.device
|
656
|
+
)
|
657
|
+
text_audio_duration_embeds = torch.cat(
|
658
|
+
[negative_text_audio_duration_embeds, text_audio_duration_embeds], dim=0
|
659
|
+
)
|
660
|
+
audio_duration_embeds = torch.cat([audio_duration_embeds, audio_duration_embeds], dim=0)
|
661
|
+
|
662
|
+
bs_embed, seq_len, hidden_size = text_audio_duration_embeds.shape
|
663
|
+
# duplicate audio_duration_embeds and text_audio_duration_embeds for each generation per prompt, using mps friendly method
|
664
|
+
text_audio_duration_embeds = text_audio_duration_embeds.repeat(1, num_waveforms_per_prompt, 1)
|
665
|
+
text_audio_duration_embeds = text_audio_duration_embeds.view(
|
666
|
+
bs_embed * num_waveforms_per_prompt, seq_len, hidden_size
|
667
|
+
)
|
668
|
+
|
669
|
+
audio_duration_embeds = audio_duration_embeds.repeat(1, num_waveforms_per_prompt, 1)
|
670
|
+
audio_duration_embeds = audio_duration_embeds.view(
|
671
|
+
bs_embed * num_waveforms_per_prompt, -1, audio_duration_embeds.shape[-1]
|
672
|
+
)
|
673
|
+
|
674
|
+
# 4. Prepare timesteps
|
675
|
+
self.scheduler.set_timesteps(num_inference_steps, device=device)
|
676
|
+
timesteps = self.scheduler.timesteps
|
677
|
+
|
678
|
+
# 5. Prepare latent variables
|
679
|
+
num_channels_vae = self.transformer.config.in_channels
|
680
|
+
latents = self.prepare_latents(
|
681
|
+
batch_size * num_waveforms_per_prompt,
|
682
|
+
num_channels_vae,
|
683
|
+
waveform_length,
|
684
|
+
text_audio_duration_embeds.dtype,
|
685
|
+
device,
|
686
|
+
generator,
|
687
|
+
latents,
|
688
|
+
initial_audio_waveforms,
|
689
|
+
num_waveforms_per_prompt,
|
690
|
+
audio_channels=self.vae.config.audio_channels,
|
691
|
+
)
|
692
|
+
|
693
|
+
# 6. Prepare extra step kwargs
|
694
|
+
extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
|
695
|
+
|
696
|
+
# 7. Prepare rotary positional embedding
|
697
|
+
rotary_embedding = get_1d_rotary_pos_embed(
|
698
|
+
self.rotary_embed_dim,
|
699
|
+
latents.shape[2] + audio_duration_embeds.shape[1],
|
700
|
+
use_real=True,
|
701
|
+
repeat_interleave_real=False,
|
702
|
+
)
|
703
|
+
|
704
|
+
# 8. Denoising loop
|
705
|
+
num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order
|
706
|
+
with self.progress_bar(total=num_inference_steps) as progress_bar:
|
707
|
+
for i, t in enumerate(timesteps):
|
708
|
+
# expand the latents if we are doing classifier free guidance
|
709
|
+
latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
|
710
|
+
latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
|
711
|
+
|
712
|
+
# predict the noise residual
|
713
|
+
noise_pred = self.transformer(
|
714
|
+
latent_model_input,
|
715
|
+
t.unsqueeze(0),
|
716
|
+
encoder_hidden_states=text_audio_duration_embeds,
|
717
|
+
global_hidden_states=audio_duration_embeds,
|
718
|
+
rotary_embedding=rotary_embedding,
|
719
|
+
return_dict=False,
|
720
|
+
)[0]
|
721
|
+
|
722
|
+
# perform guidance
|
723
|
+
if do_classifier_free_guidance:
|
724
|
+
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
|
725
|
+
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
|
726
|
+
|
727
|
+
# compute the previous noisy sample x_t -> x_t-1
|
728
|
+
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs).prev_sample
|
729
|
+
|
730
|
+
# call the callback, if provided
|
731
|
+
if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
|
732
|
+
progress_bar.update()
|
733
|
+
if callback is not None and i % callback_steps == 0:
|
734
|
+
step_idx = i // getattr(self.scheduler, "order", 1)
|
735
|
+
callback(step_idx, t, latents)
|
736
|
+
|
737
|
+
if XLA_AVAILABLE:
|
738
|
+
xm.mark_step()
|
739
|
+
|
740
|
+
# 9. Post-processing
|
741
|
+
if not output_type == "latent":
|
742
|
+
audio = self.vae.decode(latents).sample
|
743
|
+
else:
|
744
|
+
return AudioPipelineOutput(audios=latents)
|
745
|
+
|
746
|
+
audio = audio[:, :, waveform_start:waveform_end]
|
747
|
+
|
748
|
+
if output_type == "np":
|
749
|
+
audio = audio.cpu().float().numpy()
|
750
|
+
|
751
|
+
self.maybe_free_model_hooks()
|
752
|
+
|
753
|
+
if not return_dict:
|
754
|
+
return (audio,)
|
755
|
+
|
756
|
+
return AudioPipelineOutput(audios=audio)
|