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,620 @@
|
|
1
|
+
# Copyright 2024 MIT, Tsinghua University, NVIDIA CORPORATION 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
|
+
from typing import Optional, Tuple, Union
|
17
|
+
|
18
|
+
import torch
|
19
|
+
import torch.nn as nn
|
20
|
+
import torch.nn.functional as F
|
21
|
+
|
22
|
+
from ...configuration_utils import ConfigMixin, register_to_config
|
23
|
+
from ...loaders import FromOriginalModelMixin
|
24
|
+
from ...utils.accelerate_utils import apply_forward_hook
|
25
|
+
from ..activations import get_activation
|
26
|
+
from ..attention_processor import SanaMultiscaleLinearAttention
|
27
|
+
from ..modeling_utils import ModelMixin
|
28
|
+
from ..normalization import RMSNorm, get_normalization
|
29
|
+
from ..transformers.sana_transformer import GLUMBConv
|
30
|
+
from .vae import DecoderOutput, EncoderOutput
|
31
|
+
|
32
|
+
|
33
|
+
class ResBlock(nn.Module):
|
34
|
+
def __init__(
|
35
|
+
self,
|
36
|
+
in_channels: int,
|
37
|
+
out_channels: int,
|
38
|
+
norm_type: str = "batch_norm",
|
39
|
+
act_fn: str = "relu6",
|
40
|
+
) -> None:
|
41
|
+
super().__init__()
|
42
|
+
|
43
|
+
self.norm_type = norm_type
|
44
|
+
|
45
|
+
self.nonlinearity = get_activation(act_fn) if act_fn is not None else nn.Identity()
|
46
|
+
self.conv1 = nn.Conv2d(in_channels, in_channels, 3, 1, 1)
|
47
|
+
self.conv2 = nn.Conv2d(in_channels, out_channels, 3, 1, 1, bias=False)
|
48
|
+
self.norm = get_normalization(norm_type, out_channels)
|
49
|
+
|
50
|
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
51
|
+
residual = hidden_states
|
52
|
+
hidden_states = self.conv1(hidden_states)
|
53
|
+
hidden_states = self.nonlinearity(hidden_states)
|
54
|
+
hidden_states = self.conv2(hidden_states)
|
55
|
+
|
56
|
+
if self.norm_type == "rms_norm":
|
57
|
+
# move channel to the last dimension so we apply RMSnorm across channel dimension
|
58
|
+
hidden_states = self.norm(hidden_states.movedim(1, -1)).movedim(-1, 1)
|
59
|
+
else:
|
60
|
+
hidden_states = self.norm(hidden_states)
|
61
|
+
|
62
|
+
return hidden_states + residual
|
63
|
+
|
64
|
+
|
65
|
+
class EfficientViTBlock(nn.Module):
|
66
|
+
def __init__(
|
67
|
+
self,
|
68
|
+
in_channels: int,
|
69
|
+
mult: float = 1.0,
|
70
|
+
attention_head_dim: int = 32,
|
71
|
+
qkv_multiscales: Tuple[int, ...] = (5,),
|
72
|
+
norm_type: str = "batch_norm",
|
73
|
+
) -> None:
|
74
|
+
super().__init__()
|
75
|
+
|
76
|
+
self.attn = SanaMultiscaleLinearAttention(
|
77
|
+
in_channels=in_channels,
|
78
|
+
out_channels=in_channels,
|
79
|
+
mult=mult,
|
80
|
+
attention_head_dim=attention_head_dim,
|
81
|
+
norm_type=norm_type,
|
82
|
+
kernel_sizes=qkv_multiscales,
|
83
|
+
residual_connection=True,
|
84
|
+
)
|
85
|
+
|
86
|
+
self.conv_out = GLUMBConv(
|
87
|
+
in_channels=in_channels,
|
88
|
+
out_channels=in_channels,
|
89
|
+
norm_type="rms_norm",
|
90
|
+
)
|
91
|
+
|
92
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
93
|
+
x = self.attn(x)
|
94
|
+
x = self.conv_out(x)
|
95
|
+
return x
|
96
|
+
|
97
|
+
|
98
|
+
def get_block(
|
99
|
+
block_type: str,
|
100
|
+
in_channels: int,
|
101
|
+
out_channels: int,
|
102
|
+
attention_head_dim: int,
|
103
|
+
norm_type: str,
|
104
|
+
act_fn: str,
|
105
|
+
qkv_mutliscales: Tuple[int] = (),
|
106
|
+
):
|
107
|
+
if block_type == "ResBlock":
|
108
|
+
block = ResBlock(in_channels, out_channels, norm_type, act_fn)
|
109
|
+
|
110
|
+
elif block_type == "EfficientViTBlock":
|
111
|
+
block = EfficientViTBlock(
|
112
|
+
in_channels, attention_head_dim=attention_head_dim, norm_type=norm_type, qkv_multiscales=qkv_mutliscales
|
113
|
+
)
|
114
|
+
|
115
|
+
else:
|
116
|
+
raise ValueError(f"Block with {block_type=} is not supported.")
|
117
|
+
|
118
|
+
return block
|
119
|
+
|
120
|
+
|
121
|
+
class DCDownBlock2d(nn.Module):
|
122
|
+
def __init__(self, in_channels: int, out_channels: int, downsample: bool = False, shortcut: bool = True) -> None:
|
123
|
+
super().__init__()
|
124
|
+
|
125
|
+
self.downsample = downsample
|
126
|
+
self.factor = 2
|
127
|
+
self.stride = 1 if downsample else 2
|
128
|
+
self.group_size = in_channels * self.factor**2 // out_channels
|
129
|
+
self.shortcut = shortcut
|
130
|
+
|
131
|
+
out_ratio = self.factor**2
|
132
|
+
if downsample:
|
133
|
+
assert out_channels % out_ratio == 0
|
134
|
+
out_channels = out_channels // out_ratio
|
135
|
+
|
136
|
+
self.conv = nn.Conv2d(
|
137
|
+
in_channels,
|
138
|
+
out_channels,
|
139
|
+
kernel_size=3,
|
140
|
+
stride=self.stride,
|
141
|
+
padding=1,
|
142
|
+
)
|
143
|
+
|
144
|
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
145
|
+
x = self.conv(hidden_states)
|
146
|
+
if self.downsample:
|
147
|
+
x = F.pixel_unshuffle(x, self.factor)
|
148
|
+
|
149
|
+
if self.shortcut:
|
150
|
+
y = F.pixel_unshuffle(hidden_states, self.factor)
|
151
|
+
y = y.unflatten(1, (-1, self.group_size))
|
152
|
+
y = y.mean(dim=2)
|
153
|
+
hidden_states = x + y
|
154
|
+
else:
|
155
|
+
hidden_states = x
|
156
|
+
|
157
|
+
return hidden_states
|
158
|
+
|
159
|
+
|
160
|
+
class DCUpBlock2d(nn.Module):
|
161
|
+
def __init__(
|
162
|
+
self,
|
163
|
+
in_channels: int,
|
164
|
+
out_channels: int,
|
165
|
+
interpolate: bool = False,
|
166
|
+
shortcut: bool = True,
|
167
|
+
interpolation_mode: str = "nearest",
|
168
|
+
) -> None:
|
169
|
+
super().__init__()
|
170
|
+
|
171
|
+
self.interpolate = interpolate
|
172
|
+
self.interpolation_mode = interpolation_mode
|
173
|
+
self.shortcut = shortcut
|
174
|
+
self.factor = 2
|
175
|
+
self.repeats = out_channels * self.factor**2 // in_channels
|
176
|
+
|
177
|
+
out_ratio = self.factor**2
|
178
|
+
|
179
|
+
if not interpolate:
|
180
|
+
out_channels = out_channels * out_ratio
|
181
|
+
|
182
|
+
self.conv = nn.Conv2d(in_channels, out_channels, 3, 1, 1)
|
183
|
+
|
184
|
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
185
|
+
if self.interpolate:
|
186
|
+
x = F.interpolate(hidden_states, scale_factor=self.factor, mode=self.interpolation_mode)
|
187
|
+
x = self.conv(x)
|
188
|
+
else:
|
189
|
+
x = self.conv(hidden_states)
|
190
|
+
x = F.pixel_shuffle(x, self.factor)
|
191
|
+
|
192
|
+
if self.shortcut:
|
193
|
+
y = hidden_states.repeat_interleave(self.repeats, dim=1)
|
194
|
+
y = F.pixel_shuffle(y, self.factor)
|
195
|
+
hidden_states = x + y
|
196
|
+
else:
|
197
|
+
hidden_states = x
|
198
|
+
|
199
|
+
return hidden_states
|
200
|
+
|
201
|
+
|
202
|
+
class Encoder(nn.Module):
|
203
|
+
def __init__(
|
204
|
+
self,
|
205
|
+
in_channels: int,
|
206
|
+
latent_channels: int,
|
207
|
+
attention_head_dim: int = 32,
|
208
|
+
block_type: Union[str, Tuple[str]] = "ResBlock",
|
209
|
+
block_out_channels: Tuple[int] = (128, 256, 512, 512, 1024, 1024),
|
210
|
+
layers_per_block: Tuple[int] = (2, 2, 2, 2, 2, 2),
|
211
|
+
qkv_multiscales: Tuple[Tuple[int, ...], ...] = ((), (), (), (5,), (5,), (5,)),
|
212
|
+
downsample_block_type: str = "pixel_unshuffle",
|
213
|
+
out_shortcut: bool = True,
|
214
|
+
):
|
215
|
+
super().__init__()
|
216
|
+
|
217
|
+
num_blocks = len(block_out_channels)
|
218
|
+
|
219
|
+
if isinstance(block_type, str):
|
220
|
+
block_type = (block_type,) * num_blocks
|
221
|
+
|
222
|
+
if layers_per_block[0] > 0:
|
223
|
+
self.conv_in = nn.Conv2d(
|
224
|
+
in_channels,
|
225
|
+
block_out_channels[0] if layers_per_block[0] > 0 else block_out_channels[1],
|
226
|
+
kernel_size=3,
|
227
|
+
stride=1,
|
228
|
+
padding=1,
|
229
|
+
)
|
230
|
+
else:
|
231
|
+
self.conv_in = DCDownBlock2d(
|
232
|
+
in_channels=in_channels,
|
233
|
+
out_channels=block_out_channels[0] if layers_per_block[0] > 0 else block_out_channels[1],
|
234
|
+
downsample=downsample_block_type == "pixel_unshuffle",
|
235
|
+
shortcut=False,
|
236
|
+
)
|
237
|
+
|
238
|
+
down_blocks = []
|
239
|
+
for i, (out_channel, num_layers) in enumerate(zip(block_out_channels, layers_per_block)):
|
240
|
+
down_block_list = []
|
241
|
+
|
242
|
+
for _ in range(num_layers):
|
243
|
+
block = get_block(
|
244
|
+
block_type[i],
|
245
|
+
out_channel,
|
246
|
+
out_channel,
|
247
|
+
attention_head_dim=attention_head_dim,
|
248
|
+
norm_type="rms_norm",
|
249
|
+
act_fn="silu",
|
250
|
+
qkv_mutliscales=qkv_multiscales[i],
|
251
|
+
)
|
252
|
+
down_block_list.append(block)
|
253
|
+
|
254
|
+
if i < num_blocks - 1 and num_layers > 0:
|
255
|
+
downsample_block = DCDownBlock2d(
|
256
|
+
in_channels=out_channel,
|
257
|
+
out_channels=block_out_channels[i + 1],
|
258
|
+
downsample=downsample_block_type == "pixel_unshuffle",
|
259
|
+
shortcut=True,
|
260
|
+
)
|
261
|
+
down_block_list.append(downsample_block)
|
262
|
+
|
263
|
+
down_blocks.append(nn.Sequential(*down_block_list))
|
264
|
+
|
265
|
+
self.down_blocks = nn.ModuleList(down_blocks)
|
266
|
+
|
267
|
+
self.conv_out = nn.Conv2d(block_out_channels[-1], latent_channels, 3, 1, 1)
|
268
|
+
|
269
|
+
self.out_shortcut = out_shortcut
|
270
|
+
if out_shortcut:
|
271
|
+
self.out_shortcut_average_group_size = block_out_channels[-1] // latent_channels
|
272
|
+
|
273
|
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
274
|
+
hidden_states = self.conv_in(hidden_states)
|
275
|
+
for down_block in self.down_blocks:
|
276
|
+
hidden_states = down_block(hidden_states)
|
277
|
+
|
278
|
+
if self.out_shortcut:
|
279
|
+
x = hidden_states.unflatten(1, (-1, self.out_shortcut_average_group_size))
|
280
|
+
x = x.mean(dim=2)
|
281
|
+
hidden_states = self.conv_out(hidden_states) + x
|
282
|
+
else:
|
283
|
+
hidden_states = self.conv_out(hidden_states)
|
284
|
+
|
285
|
+
return hidden_states
|
286
|
+
|
287
|
+
|
288
|
+
class Decoder(nn.Module):
|
289
|
+
def __init__(
|
290
|
+
self,
|
291
|
+
in_channels: int,
|
292
|
+
latent_channels: int,
|
293
|
+
attention_head_dim: int = 32,
|
294
|
+
block_type: Union[str, Tuple[str]] = "ResBlock",
|
295
|
+
block_out_channels: Tuple[int] = (128, 256, 512, 512, 1024, 1024),
|
296
|
+
layers_per_block: Tuple[int] = (2, 2, 2, 2, 2, 2),
|
297
|
+
qkv_multiscales: Tuple[Tuple[int, ...], ...] = ((), (), (), (5,), (5,), (5,)),
|
298
|
+
norm_type: Union[str, Tuple[str]] = "rms_norm",
|
299
|
+
act_fn: Union[str, Tuple[str]] = "silu",
|
300
|
+
upsample_block_type: str = "pixel_shuffle",
|
301
|
+
in_shortcut: bool = True,
|
302
|
+
):
|
303
|
+
super().__init__()
|
304
|
+
|
305
|
+
num_blocks = len(block_out_channels)
|
306
|
+
|
307
|
+
if isinstance(block_type, str):
|
308
|
+
block_type = (block_type,) * num_blocks
|
309
|
+
if isinstance(norm_type, str):
|
310
|
+
norm_type = (norm_type,) * num_blocks
|
311
|
+
if isinstance(act_fn, str):
|
312
|
+
act_fn = (act_fn,) * num_blocks
|
313
|
+
|
314
|
+
self.conv_in = nn.Conv2d(latent_channels, block_out_channels[-1], 3, 1, 1)
|
315
|
+
|
316
|
+
self.in_shortcut = in_shortcut
|
317
|
+
if in_shortcut:
|
318
|
+
self.in_shortcut_repeats = block_out_channels[-1] // latent_channels
|
319
|
+
|
320
|
+
up_blocks = []
|
321
|
+
for i, (out_channel, num_layers) in reversed(list(enumerate(zip(block_out_channels, layers_per_block)))):
|
322
|
+
up_block_list = []
|
323
|
+
|
324
|
+
if i < num_blocks - 1 and num_layers > 0:
|
325
|
+
upsample_block = DCUpBlock2d(
|
326
|
+
block_out_channels[i + 1],
|
327
|
+
out_channel,
|
328
|
+
interpolate=upsample_block_type == "interpolate",
|
329
|
+
shortcut=True,
|
330
|
+
)
|
331
|
+
up_block_list.append(upsample_block)
|
332
|
+
|
333
|
+
for _ in range(num_layers):
|
334
|
+
block = get_block(
|
335
|
+
block_type[i],
|
336
|
+
out_channel,
|
337
|
+
out_channel,
|
338
|
+
attention_head_dim=attention_head_dim,
|
339
|
+
norm_type=norm_type[i],
|
340
|
+
act_fn=act_fn[i],
|
341
|
+
qkv_mutliscales=qkv_multiscales[i],
|
342
|
+
)
|
343
|
+
up_block_list.append(block)
|
344
|
+
|
345
|
+
up_blocks.insert(0, nn.Sequential(*up_block_list))
|
346
|
+
|
347
|
+
self.up_blocks = nn.ModuleList(up_blocks)
|
348
|
+
|
349
|
+
channels = block_out_channels[0] if layers_per_block[0] > 0 else block_out_channels[1]
|
350
|
+
|
351
|
+
self.norm_out = RMSNorm(channels, 1e-5, elementwise_affine=True, bias=True)
|
352
|
+
self.conv_act = nn.ReLU()
|
353
|
+
self.conv_out = None
|
354
|
+
|
355
|
+
if layers_per_block[0] > 0:
|
356
|
+
self.conv_out = nn.Conv2d(channels, in_channels, 3, 1, 1)
|
357
|
+
else:
|
358
|
+
self.conv_out = DCUpBlock2d(
|
359
|
+
channels, in_channels, interpolate=upsample_block_type == "interpolate", shortcut=False
|
360
|
+
)
|
361
|
+
|
362
|
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
363
|
+
if self.in_shortcut:
|
364
|
+
x = hidden_states.repeat_interleave(self.in_shortcut_repeats, dim=1)
|
365
|
+
hidden_states = self.conv_in(hidden_states) + x
|
366
|
+
else:
|
367
|
+
hidden_states = self.conv_in(hidden_states)
|
368
|
+
|
369
|
+
for up_block in reversed(self.up_blocks):
|
370
|
+
hidden_states = up_block(hidden_states)
|
371
|
+
|
372
|
+
hidden_states = self.norm_out(hidden_states.movedim(1, -1)).movedim(-1, 1)
|
373
|
+
hidden_states = self.conv_act(hidden_states)
|
374
|
+
hidden_states = self.conv_out(hidden_states)
|
375
|
+
return hidden_states
|
376
|
+
|
377
|
+
|
378
|
+
class AutoencoderDC(ModelMixin, ConfigMixin, FromOriginalModelMixin):
|
379
|
+
r"""
|
380
|
+
An Autoencoder model introduced in [DCAE](https://arxiv.org/abs/2410.10733) and used in
|
381
|
+
[SANA](https://arxiv.org/abs/2410.10629).
|
382
|
+
|
383
|
+
This model inherits from [`ModelMixin`]. Check the superclass documentation for it's generic methods implemented
|
384
|
+
for all models (such as downloading or saving).
|
385
|
+
|
386
|
+
Args:
|
387
|
+
in_channels (`int`, defaults to `3`):
|
388
|
+
The number of input channels in samples.
|
389
|
+
latent_channels (`int`, defaults to `32`):
|
390
|
+
The number of channels in the latent space representation.
|
391
|
+
encoder_block_types (`Union[str, Tuple[str]]`, defaults to `"ResBlock"`):
|
392
|
+
The type(s) of block to use in the encoder.
|
393
|
+
decoder_block_types (`Union[str, Tuple[str]]`, defaults to `"ResBlock"`):
|
394
|
+
The type(s) of block to use in the decoder.
|
395
|
+
encoder_block_out_channels (`Tuple[int, ...]`, defaults to `(128, 256, 512, 512, 1024, 1024)`):
|
396
|
+
The number of output channels for each block in the encoder.
|
397
|
+
decoder_block_out_channels (`Tuple[int, ...]`, defaults to `(128, 256, 512, 512, 1024, 1024)`):
|
398
|
+
The number of output channels for each block in the decoder.
|
399
|
+
encoder_layers_per_block (`Tuple[int]`, defaults to `(2, 2, 2, 3, 3, 3)`):
|
400
|
+
The number of layers per block in the encoder.
|
401
|
+
decoder_layers_per_block (`Tuple[int]`, defaults to `(3, 3, 3, 3, 3, 3)`):
|
402
|
+
The number of layers per block in the decoder.
|
403
|
+
encoder_qkv_multiscales (`Tuple[Tuple[int, ...], ...]`, defaults to `((), (), (), (5,), (5,), (5,))`):
|
404
|
+
Multi-scale configurations for the encoder's QKV (query-key-value) transformations.
|
405
|
+
decoder_qkv_multiscales (`Tuple[Tuple[int, ...], ...]`, defaults to `((), (), (), (5,), (5,), (5,))`):
|
406
|
+
Multi-scale configurations for the decoder's QKV (query-key-value) transformations.
|
407
|
+
upsample_block_type (`str`, defaults to `"pixel_shuffle"`):
|
408
|
+
The type of block to use for upsampling in the decoder.
|
409
|
+
downsample_block_type (`str`, defaults to `"pixel_unshuffle"`):
|
410
|
+
The type of block to use for downsampling in the encoder.
|
411
|
+
decoder_norm_types (`Union[str, Tuple[str]]`, defaults to `"rms_norm"`):
|
412
|
+
The normalization type(s) to use in the decoder.
|
413
|
+
decoder_act_fns (`Union[str, Tuple[str]]`, defaults to `"silu"`):
|
414
|
+
The activation function(s) to use in the decoder.
|
415
|
+
scaling_factor (`float`, defaults to `1.0`):
|
416
|
+
The multiplicative inverse of the root mean square of the latent features. This is used to scale the latent
|
417
|
+
space to have unit variance when training the diffusion model. The latents are scaled with the formula `z =
|
418
|
+
z * scaling_factor` before being passed to the diffusion model. When decoding, the latents are scaled back
|
419
|
+
to the original scale with the formula: `z = 1 / scaling_factor * z`.
|
420
|
+
"""
|
421
|
+
|
422
|
+
_supports_gradient_checkpointing = False
|
423
|
+
|
424
|
+
@register_to_config
|
425
|
+
def __init__(
|
426
|
+
self,
|
427
|
+
in_channels: int = 3,
|
428
|
+
latent_channels: int = 32,
|
429
|
+
attention_head_dim: int = 32,
|
430
|
+
encoder_block_types: Union[str, Tuple[str]] = "ResBlock",
|
431
|
+
decoder_block_types: Union[str, Tuple[str]] = "ResBlock",
|
432
|
+
encoder_block_out_channels: Tuple[int, ...] = (128, 256, 512, 512, 1024, 1024),
|
433
|
+
decoder_block_out_channels: Tuple[int, ...] = (128, 256, 512, 512, 1024, 1024),
|
434
|
+
encoder_layers_per_block: Tuple[int] = (2, 2, 2, 3, 3, 3),
|
435
|
+
decoder_layers_per_block: Tuple[int] = (3, 3, 3, 3, 3, 3),
|
436
|
+
encoder_qkv_multiscales: Tuple[Tuple[int, ...], ...] = ((), (), (), (5,), (5,), (5,)),
|
437
|
+
decoder_qkv_multiscales: Tuple[Tuple[int, ...], ...] = ((), (), (), (5,), (5,), (5,)),
|
438
|
+
upsample_block_type: str = "pixel_shuffle",
|
439
|
+
downsample_block_type: str = "pixel_unshuffle",
|
440
|
+
decoder_norm_types: Union[str, Tuple[str]] = "rms_norm",
|
441
|
+
decoder_act_fns: Union[str, Tuple[str]] = "silu",
|
442
|
+
scaling_factor: float = 1.0,
|
443
|
+
) -> None:
|
444
|
+
super().__init__()
|
445
|
+
|
446
|
+
self.encoder = Encoder(
|
447
|
+
in_channels=in_channels,
|
448
|
+
latent_channels=latent_channels,
|
449
|
+
attention_head_dim=attention_head_dim,
|
450
|
+
block_type=encoder_block_types,
|
451
|
+
block_out_channels=encoder_block_out_channels,
|
452
|
+
layers_per_block=encoder_layers_per_block,
|
453
|
+
qkv_multiscales=encoder_qkv_multiscales,
|
454
|
+
downsample_block_type=downsample_block_type,
|
455
|
+
)
|
456
|
+
self.decoder = Decoder(
|
457
|
+
in_channels=in_channels,
|
458
|
+
latent_channels=latent_channels,
|
459
|
+
attention_head_dim=attention_head_dim,
|
460
|
+
block_type=decoder_block_types,
|
461
|
+
block_out_channels=decoder_block_out_channels,
|
462
|
+
layers_per_block=decoder_layers_per_block,
|
463
|
+
qkv_multiscales=decoder_qkv_multiscales,
|
464
|
+
norm_type=decoder_norm_types,
|
465
|
+
act_fn=decoder_act_fns,
|
466
|
+
upsample_block_type=upsample_block_type,
|
467
|
+
)
|
468
|
+
|
469
|
+
self.spatial_compression_ratio = 2 ** (len(encoder_block_out_channels) - 1)
|
470
|
+
self.temporal_compression_ratio = 1
|
471
|
+
|
472
|
+
# When decoding a batch of video latents at a time, one can save memory by slicing across the batch dimension
|
473
|
+
# to perform decoding of a single video latent at a time.
|
474
|
+
self.use_slicing = False
|
475
|
+
|
476
|
+
# When decoding spatially large video latents, the memory requirement is very high. By breaking the video latent
|
477
|
+
# frames spatially into smaller tiles and performing multiple forward passes for decoding, and then blending the
|
478
|
+
# intermediate tiles together, the memory requirement can be lowered.
|
479
|
+
self.use_tiling = False
|
480
|
+
|
481
|
+
# The minimal tile height and width for spatial tiling to be used
|
482
|
+
self.tile_sample_min_height = 512
|
483
|
+
self.tile_sample_min_width = 512
|
484
|
+
|
485
|
+
# The minimal distance between two spatial tiles
|
486
|
+
self.tile_sample_stride_height = 448
|
487
|
+
self.tile_sample_stride_width = 448
|
488
|
+
|
489
|
+
def enable_tiling(
|
490
|
+
self,
|
491
|
+
tile_sample_min_height: Optional[int] = None,
|
492
|
+
tile_sample_min_width: Optional[int] = None,
|
493
|
+
tile_sample_stride_height: Optional[float] = None,
|
494
|
+
tile_sample_stride_width: Optional[float] = None,
|
495
|
+
) -> None:
|
496
|
+
r"""
|
497
|
+
Enable tiled AE decoding. When this option is enabled, the AE will split the input tensor into tiles to compute
|
498
|
+
decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow
|
499
|
+
processing larger images.
|
500
|
+
|
501
|
+
Args:
|
502
|
+
tile_sample_min_height (`int`, *optional*):
|
503
|
+
The minimum height required for a sample to be separated into tiles across the height dimension.
|
504
|
+
tile_sample_min_width (`int`, *optional*):
|
505
|
+
The minimum width required for a sample to be separated into tiles across the width dimension.
|
506
|
+
tile_sample_stride_height (`int`, *optional*):
|
507
|
+
The minimum amount of overlap between two consecutive vertical tiles. This is to ensure that there are
|
508
|
+
no tiling artifacts produced across the height dimension.
|
509
|
+
tile_sample_stride_width (`int`, *optional*):
|
510
|
+
The stride between two consecutive horizontal tiles. This is to ensure that there are no tiling
|
511
|
+
artifacts produced across the width dimension.
|
512
|
+
"""
|
513
|
+
self.use_tiling = True
|
514
|
+
self.tile_sample_min_height = tile_sample_min_height or self.tile_sample_min_height
|
515
|
+
self.tile_sample_min_width = tile_sample_min_width or self.tile_sample_min_width
|
516
|
+
self.tile_sample_stride_height = tile_sample_stride_height or self.tile_sample_stride_height
|
517
|
+
self.tile_sample_stride_width = tile_sample_stride_width or self.tile_sample_stride_width
|
518
|
+
|
519
|
+
def disable_tiling(self) -> None:
|
520
|
+
r"""
|
521
|
+
Disable tiled AE decoding. If `enable_tiling` was previously enabled, this method will go back to computing
|
522
|
+
decoding in one step.
|
523
|
+
"""
|
524
|
+
self.use_tiling = False
|
525
|
+
|
526
|
+
def enable_slicing(self) -> None:
|
527
|
+
r"""
|
528
|
+
Enable sliced AE decoding. When this option is enabled, the AE will split the input tensor in slices to compute
|
529
|
+
decoding in several steps. This is useful to save some memory and allow larger batch sizes.
|
530
|
+
"""
|
531
|
+
self.use_slicing = True
|
532
|
+
|
533
|
+
def disable_slicing(self) -> None:
|
534
|
+
r"""
|
535
|
+
Disable sliced AE decoding. If `enable_slicing` was previously enabled, this method will go back to computing
|
536
|
+
decoding in one step.
|
537
|
+
"""
|
538
|
+
self.use_slicing = False
|
539
|
+
|
540
|
+
def _encode(self, x: torch.Tensor) -> torch.Tensor:
|
541
|
+
batch_size, num_channels, height, width = x.shape
|
542
|
+
|
543
|
+
if self.use_tiling and (width > self.tile_sample_min_width or height > self.tile_sample_min_height):
|
544
|
+
return self.tiled_encode(x, return_dict=False)[0]
|
545
|
+
|
546
|
+
encoded = self.encoder(x)
|
547
|
+
|
548
|
+
return encoded
|
549
|
+
|
550
|
+
@apply_forward_hook
|
551
|
+
def encode(self, x: torch.Tensor, return_dict: bool = True) -> Union[EncoderOutput, Tuple[torch.Tensor]]:
|
552
|
+
r"""
|
553
|
+
Encode a batch of images into latents.
|
554
|
+
|
555
|
+
Args:
|
556
|
+
x (`torch.Tensor`): Input batch of images.
|
557
|
+
return_dict (`bool`, defaults to `True`):
|
558
|
+
Whether to return a [`~models.vae.EncoderOutput`] instead of a plain tuple.
|
559
|
+
|
560
|
+
Returns:
|
561
|
+
The latent representations of the encoded videos. If `return_dict` is True, a
|
562
|
+
[`~models.vae.EncoderOutput`] is returned, otherwise a plain `tuple` is returned.
|
563
|
+
"""
|
564
|
+
if self.use_slicing and x.shape[0] > 1:
|
565
|
+
encoded_slices = [self._encode(x_slice) for x_slice in x.split(1)]
|
566
|
+
encoded = torch.cat(encoded_slices)
|
567
|
+
else:
|
568
|
+
encoded = self._encode(x)
|
569
|
+
|
570
|
+
if not return_dict:
|
571
|
+
return (encoded,)
|
572
|
+
return EncoderOutput(latent=encoded)
|
573
|
+
|
574
|
+
def _decode(self, z: torch.Tensor) -> torch.Tensor:
|
575
|
+
batch_size, num_channels, height, width = z.shape
|
576
|
+
|
577
|
+
if self.use_tiling and (width > self.tile_latent_min_width or height > self.tile_latent_min_height):
|
578
|
+
return self.tiled_decode(z, return_dict=False)[0]
|
579
|
+
|
580
|
+
decoded = self.decoder(z)
|
581
|
+
|
582
|
+
return decoded
|
583
|
+
|
584
|
+
@apply_forward_hook
|
585
|
+
def decode(self, z: torch.Tensor, return_dict: bool = True) -> Union[DecoderOutput, Tuple[torch.Tensor]]:
|
586
|
+
r"""
|
587
|
+
Decode a batch of images.
|
588
|
+
|
589
|
+
Args:
|
590
|
+
z (`torch.Tensor`): Input batch of latent vectors.
|
591
|
+
return_dict (`bool`, defaults to `True`):
|
592
|
+
Whether to return a [`~models.vae.DecoderOutput`] instead of a plain tuple.
|
593
|
+
|
594
|
+
Returns:
|
595
|
+
[`~models.vae.DecoderOutput`] or `tuple`:
|
596
|
+
If return_dict is True, a [`~models.vae.DecoderOutput`] is returned, otherwise a plain `tuple` is
|
597
|
+
returned.
|
598
|
+
"""
|
599
|
+
if self.use_slicing and z.size(0) > 1:
|
600
|
+
decoded_slices = [self._decode(z_slice).sample for z_slice in z.split(1)]
|
601
|
+
decoded = torch.cat(decoded_slices)
|
602
|
+
else:
|
603
|
+
decoded = self._decode(z)
|
604
|
+
|
605
|
+
if not return_dict:
|
606
|
+
return (decoded,)
|
607
|
+
return DecoderOutput(sample=decoded)
|
608
|
+
|
609
|
+
def tiled_encode(self, x: torch.Tensor, return_dict: bool = True) -> torch.Tensor:
|
610
|
+
raise NotImplementedError("`tiled_encode` has not been implemented for AutoencoderDC.")
|
611
|
+
|
612
|
+
def tiled_decode(self, z: torch.Tensor, return_dict: bool = True) -> Union[DecoderOutput, torch.Tensor]:
|
613
|
+
raise NotImplementedError("`tiled_decode` has not been implemented for AutoencoderDC.")
|
614
|
+
|
615
|
+
def forward(self, sample: torch.Tensor, return_dict: bool = True) -> torch.Tensor:
|
616
|
+
encoded = self.encode(sample, return_dict=False)[0]
|
617
|
+
decoded = self.decode(encoded, return_dict=False)[0]
|
618
|
+
if not return_dict:
|
619
|
+
return (decoded,)
|
620
|
+
return DecoderOutput(sample=decoded)
|