diffusers 0.32.1__py3-none-any.whl → 0.33.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diffusers/__init__.py +186 -3
- diffusers/configuration_utils.py +40 -12
- diffusers/dependency_versions_table.py +9 -2
- diffusers/hooks/__init__.py +9 -0
- diffusers/hooks/faster_cache.py +653 -0
- diffusers/hooks/group_offloading.py +793 -0
- diffusers/hooks/hooks.py +236 -0
- diffusers/hooks/layerwise_casting.py +245 -0
- diffusers/hooks/pyramid_attention_broadcast.py +311 -0
- diffusers/loaders/__init__.py +6 -0
- diffusers/loaders/ip_adapter.py +38 -30
- diffusers/loaders/lora_base.py +198 -28
- diffusers/loaders/lora_conversion_utils.py +679 -44
- diffusers/loaders/lora_pipeline.py +1963 -801
- diffusers/loaders/peft.py +169 -84
- diffusers/loaders/single_file.py +17 -2
- diffusers/loaders/single_file_model.py +53 -5
- diffusers/loaders/single_file_utils.py +653 -75
- diffusers/loaders/textual_inversion.py +9 -9
- diffusers/loaders/transformer_flux.py +8 -9
- diffusers/loaders/transformer_sd3.py +120 -39
- diffusers/loaders/unet.py +22 -32
- diffusers/models/__init__.py +22 -0
- diffusers/models/activations.py +9 -9
- diffusers/models/attention.py +0 -1
- diffusers/models/attention_processor.py +163 -25
- diffusers/models/auto_model.py +169 -0
- diffusers/models/autoencoders/__init__.py +2 -0
- diffusers/models/autoencoders/autoencoder_asym_kl.py +2 -0
- diffusers/models/autoencoders/autoencoder_dc.py +106 -4
- diffusers/models/autoencoders/autoencoder_kl.py +0 -4
- diffusers/models/autoencoders/autoencoder_kl_allegro.py +5 -23
- diffusers/models/autoencoders/autoencoder_kl_cogvideox.py +17 -55
- diffusers/models/autoencoders/autoencoder_kl_hunyuan_video.py +17 -97
- diffusers/models/autoencoders/autoencoder_kl_ltx.py +326 -107
- diffusers/models/autoencoders/autoencoder_kl_magvit.py +1094 -0
- diffusers/models/autoencoders/autoencoder_kl_mochi.py +21 -56
- diffusers/models/autoencoders/autoencoder_kl_temporal_decoder.py +11 -42
- diffusers/models/autoencoders/autoencoder_kl_wan.py +855 -0
- diffusers/models/autoencoders/autoencoder_oobleck.py +1 -0
- diffusers/models/autoencoders/autoencoder_tiny.py +0 -4
- diffusers/models/autoencoders/consistency_decoder_vae.py +3 -1
- diffusers/models/autoencoders/vae.py +31 -141
- diffusers/models/autoencoders/vq_model.py +3 -0
- diffusers/models/cache_utils.py +108 -0
- diffusers/models/controlnets/__init__.py +1 -0
- diffusers/models/controlnets/controlnet.py +3 -8
- diffusers/models/controlnets/controlnet_flux.py +14 -42
- diffusers/models/controlnets/controlnet_sd3.py +58 -34
- diffusers/models/controlnets/controlnet_sparsectrl.py +4 -7
- diffusers/models/controlnets/controlnet_union.py +27 -18
- diffusers/models/controlnets/controlnet_xs.py +7 -46
- diffusers/models/controlnets/multicontrolnet_union.py +196 -0
- diffusers/models/embeddings.py +18 -7
- diffusers/models/model_loading_utils.py +122 -80
- diffusers/models/modeling_flax_pytorch_utils.py +1 -1
- diffusers/models/modeling_flax_utils.py +1 -1
- diffusers/models/modeling_pytorch_flax_utils.py +1 -1
- diffusers/models/modeling_utils.py +617 -272
- diffusers/models/normalization.py +67 -14
- diffusers/models/resnet.py +1 -1
- diffusers/models/transformers/__init__.py +6 -0
- diffusers/models/transformers/auraflow_transformer_2d.py +9 -35
- diffusers/models/transformers/cogvideox_transformer_3d.py +13 -24
- diffusers/models/transformers/consisid_transformer_3d.py +789 -0
- diffusers/models/transformers/dit_transformer_2d.py +5 -19
- diffusers/models/transformers/hunyuan_transformer_2d.py +4 -3
- diffusers/models/transformers/latte_transformer_3d.py +20 -15
- diffusers/models/transformers/lumina_nextdit2d.py +3 -1
- diffusers/models/transformers/pixart_transformer_2d.py +4 -19
- diffusers/models/transformers/prior_transformer.py +5 -1
- diffusers/models/transformers/sana_transformer.py +144 -40
- diffusers/models/transformers/stable_audio_transformer.py +5 -20
- diffusers/models/transformers/transformer_2d.py +7 -22
- diffusers/models/transformers/transformer_allegro.py +9 -17
- diffusers/models/transformers/transformer_cogview3plus.py +6 -17
- diffusers/models/transformers/transformer_cogview4.py +462 -0
- diffusers/models/transformers/transformer_easyanimate.py +527 -0
- diffusers/models/transformers/transformer_flux.py +68 -110
- diffusers/models/transformers/transformer_hunyuan_video.py +409 -49
- diffusers/models/transformers/transformer_ltx.py +53 -35
- diffusers/models/transformers/transformer_lumina2.py +548 -0
- diffusers/models/transformers/transformer_mochi.py +6 -17
- diffusers/models/transformers/transformer_omnigen.py +469 -0
- diffusers/models/transformers/transformer_sd3.py +56 -86
- diffusers/models/transformers/transformer_temporal.py +5 -11
- diffusers/models/transformers/transformer_wan.py +469 -0
- diffusers/models/unets/unet_1d.py +3 -1
- diffusers/models/unets/unet_2d.py +21 -20
- diffusers/models/unets/unet_2d_blocks.py +19 -243
- diffusers/models/unets/unet_2d_condition.py +4 -6
- diffusers/models/unets/unet_3d_blocks.py +14 -127
- diffusers/models/unets/unet_3d_condition.py +8 -12
- diffusers/models/unets/unet_i2vgen_xl.py +5 -13
- diffusers/models/unets/unet_kandinsky3.py +0 -4
- diffusers/models/unets/unet_motion_model.py +20 -114
- diffusers/models/unets/unet_spatio_temporal_condition.py +7 -8
- diffusers/models/unets/unet_stable_cascade.py +8 -35
- diffusers/models/unets/uvit_2d.py +1 -4
- diffusers/optimization.py +2 -2
- diffusers/pipelines/__init__.py +57 -8
- diffusers/pipelines/allegro/pipeline_allegro.py +22 -2
- diffusers/pipelines/amused/pipeline_amused.py +15 -2
- diffusers/pipelines/amused/pipeline_amused_img2img.py +15 -2
- diffusers/pipelines/amused/pipeline_amused_inpaint.py +15 -2
- diffusers/pipelines/animatediff/pipeline_animatediff.py +15 -2
- diffusers/pipelines/animatediff/pipeline_animatediff_controlnet.py +15 -3
- diffusers/pipelines/animatediff/pipeline_animatediff_sdxl.py +24 -4
- diffusers/pipelines/animatediff/pipeline_animatediff_sparsectrl.py +15 -2
- diffusers/pipelines/animatediff/pipeline_animatediff_video2video.py +16 -4
- diffusers/pipelines/animatediff/pipeline_animatediff_video2video_controlnet.py +16 -4
- diffusers/pipelines/audioldm/pipeline_audioldm.py +13 -2
- diffusers/pipelines/audioldm2/modeling_audioldm2.py +13 -68
- diffusers/pipelines/audioldm2/pipeline_audioldm2.py +39 -9
- diffusers/pipelines/aura_flow/pipeline_aura_flow.py +63 -7
- diffusers/pipelines/auto_pipeline.py +35 -14
- diffusers/pipelines/blip_diffusion/blip_image_processing.py +1 -1
- diffusers/pipelines/blip_diffusion/modeling_blip2.py +5 -8
- diffusers/pipelines/blip_diffusion/pipeline_blip_diffusion.py +12 -0
- diffusers/pipelines/cogvideo/pipeline_cogvideox.py +22 -6
- diffusers/pipelines/cogvideo/pipeline_cogvideox_fun_control.py +22 -6
- diffusers/pipelines/cogvideo/pipeline_cogvideox_image2video.py +22 -5
- diffusers/pipelines/cogvideo/pipeline_cogvideox_video2video.py +22 -6
- diffusers/pipelines/cogview3/pipeline_cogview3plus.py +12 -4
- diffusers/pipelines/cogview4/__init__.py +49 -0
- diffusers/pipelines/cogview4/pipeline_cogview4.py +684 -0
- diffusers/pipelines/cogview4/pipeline_cogview4_control.py +732 -0
- diffusers/pipelines/cogview4/pipeline_output.py +21 -0
- diffusers/pipelines/consisid/__init__.py +49 -0
- diffusers/pipelines/consisid/consisid_utils.py +357 -0
- diffusers/pipelines/consisid/pipeline_consisid.py +974 -0
- diffusers/pipelines/consisid/pipeline_output.py +20 -0
- diffusers/pipelines/consistency_models/pipeline_consistency_models.py +11 -0
- diffusers/pipelines/controlnet/pipeline_controlnet.py +6 -5
- diffusers/pipelines/controlnet/pipeline_controlnet_blip_diffusion.py +13 -0
- diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +17 -5
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +31 -12
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +26 -7
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +20 -3
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +22 -3
- diffusers/pipelines/controlnet/pipeline_controlnet_union_inpaint_sd_xl.py +26 -25
- diffusers/pipelines/controlnet/pipeline_controlnet_union_sd_xl.py +224 -109
- diffusers/pipelines/controlnet/pipeline_controlnet_union_sd_xl_img2img.py +25 -29
- diffusers/pipelines/controlnet/pipeline_flax_controlnet.py +7 -4
- diffusers/pipelines/controlnet_hunyuandit/pipeline_hunyuandit_controlnet.py +3 -5
- diffusers/pipelines/controlnet_sd3/pipeline_stable_diffusion_3_controlnet.py +121 -10
- diffusers/pipelines/controlnet_sd3/pipeline_stable_diffusion_3_controlnet_inpainting.py +122 -11
- diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py +12 -1
- diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py +20 -3
- diffusers/pipelines/dance_diffusion/pipeline_dance_diffusion.py +14 -2
- diffusers/pipelines/ddim/pipeline_ddim.py +14 -1
- diffusers/pipelines/ddpm/pipeline_ddpm.py +15 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if.py +12 -0
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img.py +12 -0
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img_superresolution.py +14 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.py +12 -0
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting_superresolution.py +14 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.py +14 -1
- diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion.py +11 -7
- diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion_img2img.py +11 -7
- diffusers/pipelines/deprecated/repaint/pipeline_repaint.py +1 -1
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_cycle_diffusion.py +10 -6
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_onnx_stable_diffusion_inpaint_legacy.py +2 -2
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_inpaint_legacy.py +11 -7
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_model_editing.py +1 -1
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_paradigms.py +1 -1
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_pix2pix_zero.py +1 -1
- diffusers/pipelines/deprecated/versatile_diffusion/modeling_text_unet.py +10 -105
- diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion.py +1 -1
- diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_dual_guided.py +1 -1
- diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_image_variation.py +1 -1
- diffusers/pipelines/deprecated/versatile_diffusion/pipeline_versatile_diffusion_text_to_image.py +1 -1
- diffusers/pipelines/dit/pipeline_dit.py +15 -2
- diffusers/pipelines/easyanimate/__init__.py +52 -0
- diffusers/pipelines/easyanimate/pipeline_easyanimate.py +770 -0
- diffusers/pipelines/easyanimate/pipeline_easyanimate_control.py +994 -0
- diffusers/pipelines/easyanimate/pipeline_easyanimate_inpaint.py +1234 -0
- diffusers/pipelines/easyanimate/pipeline_output.py +20 -0
- diffusers/pipelines/flux/pipeline_flux.py +53 -21
- diffusers/pipelines/flux/pipeline_flux_control.py +9 -12
- diffusers/pipelines/flux/pipeline_flux_control_img2img.py +6 -10
- diffusers/pipelines/flux/pipeline_flux_control_inpaint.py +8 -10
- diffusers/pipelines/flux/pipeline_flux_controlnet.py +185 -13
- diffusers/pipelines/flux/pipeline_flux_controlnet_image_to_image.py +8 -10
- diffusers/pipelines/flux/pipeline_flux_controlnet_inpainting.py +16 -16
- diffusers/pipelines/flux/pipeline_flux_fill.py +107 -39
- diffusers/pipelines/flux/pipeline_flux_img2img.py +193 -15
- diffusers/pipelines/flux/pipeline_flux_inpaint.py +199 -19
- diffusers/pipelines/free_noise_utils.py +3 -3
- diffusers/pipelines/hunyuan_video/__init__.py +4 -0
- diffusers/pipelines/hunyuan_video/pipeline_hunyuan_skyreels_image2video.py +804 -0
- diffusers/pipelines/hunyuan_video/pipeline_hunyuan_video.py +90 -23
- diffusers/pipelines/hunyuan_video/pipeline_hunyuan_video_image2video.py +924 -0
- diffusers/pipelines/hunyuandit/pipeline_hunyuandit.py +3 -5
- diffusers/pipelines/i2vgen_xl/pipeline_i2vgen_xl.py +13 -1
- diffusers/pipelines/kandinsky/pipeline_kandinsky.py +12 -0
- diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py +1 -1
- diffusers/pipelines/kandinsky/pipeline_kandinsky_img2img.py +12 -0
- diffusers/pipelines/kandinsky/pipeline_kandinsky_inpaint.py +13 -1
- diffusers/pipelines/kandinsky/pipeline_kandinsky_prior.py +12 -0
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py +12 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_controlnet.py +13 -0
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_controlnet_img2img.py +12 -0
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py +12 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpainting.py +12 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior.py +12 -0
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior_emb2emb.py +12 -0
- diffusers/pipelines/kandinsky3/pipeline_kandinsky3.py +12 -0
- diffusers/pipelines/kandinsky3/pipeline_kandinsky3_img2img.py +12 -0
- diffusers/pipelines/kolors/pipeline_kolors.py +10 -8
- diffusers/pipelines/kolors/pipeline_kolors_img2img.py +6 -4
- diffusers/pipelines/kolors/text_encoder.py +7 -34
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +12 -1
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +13 -1
- diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py +14 -13
- diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion_superresolution.py +12 -1
- diffusers/pipelines/latte/pipeline_latte.py +36 -7
- diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion.py +67 -13
- diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion_xl.py +60 -15
- diffusers/pipelines/ltx/__init__.py +2 -0
- diffusers/pipelines/ltx/pipeline_ltx.py +25 -13
- diffusers/pipelines/ltx/pipeline_ltx_condition.py +1194 -0
- diffusers/pipelines/ltx/pipeline_ltx_image2video.py +31 -17
- diffusers/pipelines/lumina/__init__.py +2 -2
- diffusers/pipelines/lumina/pipeline_lumina.py +83 -20
- diffusers/pipelines/lumina2/__init__.py +48 -0
- diffusers/pipelines/lumina2/pipeline_lumina2.py +790 -0
- diffusers/pipelines/marigold/__init__.py +2 -0
- diffusers/pipelines/marigold/marigold_image_processing.py +127 -14
- diffusers/pipelines/marigold/pipeline_marigold_depth.py +31 -16
- diffusers/pipelines/marigold/pipeline_marigold_intrinsics.py +721 -0
- diffusers/pipelines/marigold/pipeline_marigold_normals.py +31 -16
- diffusers/pipelines/mochi/pipeline_mochi.py +14 -18
- diffusers/pipelines/musicldm/pipeline_musicldm.py +16 -1
- diffusers/pipelines/omnigen/__init__.py +50 -0
- diffusers/pipelines/omnigen/pipeline_omnigen.py +512 -0
- diffusers/pipelines/omnigen/processor_omnigen.py +327 -0
- diffusers/pipelines/onnx_utils.py +5 -3
- diffusers/pipelines/pag/pag_utils.py +1 -1
- diffusers/pipelines/pag/pipeline_pag_controlnet_sd.py +12 -1
- diffusers/pipelines/pag/pipeline_pag_controlnet_sd_inpaint.py +15 -4
- diffusers/pipelines/pag/pipeline_pag_controlnet_sd_xl.py +20 -3
- diffusers/pipelines/pag/pipeline_pag_controlnet_sd_xl_img2img.py +20 -3
- diffusers/pipelines/pag/pipeline_pag_hunyuandit.py +1 -3
- diffusers/pipelines/pag/pipeline_pag_kolors.py +6 -4
- diffusers/pipelines/pag/pipeline_pag_pixart_sigma.py +16 -3
- diffusers/pipelines/pag/pipeline_pag_sana.py +65 -8
- diffusers/pipelines/pag/pipeline_pag_sd.py +23 -7
- diffusers/pipelines/pag/pipeline_pag_sd_3.py +3 -5
- diffusers/pipelines/pag/pipeline_pag_sd_3_img2img.py +3 -5
- diffusers/pipelines/pag/pipeline_pag_sd_animatediff.py +13 -1
- diffusers/pipelines/pag/pipeline_pag_sd_img2img.py +23 -7
- diffusers/pipelines/pag/pipeline_pag_sd_inpaint.py +26 -10
- diffusers/pipelines/pag/pipeline_pag_sd_xl.py +12 -4
- diffusers/pipelines/pag/pipeline_pag_sd_xl_img2img.py +7 -3
- diffusers/pipelines/pag/pipeline_pag_sd_xl_inpaint.py +10 -6
- diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py +13 -3
- diffusers/pipelines/pia/pipeline_pia.py +13 -1
- diffusers/pipelines/pipeline_flax_utils.py +7 -7
- diffusers/pipelines/pipeline_loading_utils.py +193 -83
- diffusers/pipelines/pipeline_utils.py +221 -106
- diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +17 -5
- diffusers/pipelines/pixart_alpha/pipeline_pixart_sigma.py +17 -4
- diffusers/pipelines/sana/__init__.py +2 -0
- diffusers/pipelines/sana/pipeline_sana.py +183 -58
- diffusers/pipelines/sana/pipeline_sana_sprint.py +889 -0
- diffusers/pipelines/semantic_stable_diffusion/pipeline_semantic_stable_diffusion.py +12 -2
- diffusers/pipelines/shap_e/pipeline_shap_e.py +12 -0
- diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py +12 -0
- diffusers/pipelines/shap_e/renderer.py +6 -6
- diffusers/pipelines/stable_audio/pipeline_stable_audio.py +1 -1
- diffusers/pipelines/stable_cascade/pipeline_stable_cascade.py +15 -4
- diffusers/pipelines/stable_cascade/pipeline_stable_cascade_combined.py +12 -8
- diffusers/pipelines/stable_cascade/pipeline_stable_cascade_prior.py +12 -1
- diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +3 -2
- diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion.py +14 -10
- diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion_img2img.py +3 -3
- diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion_inpaint.py +14 -10
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion.py +2 -2
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_img2img.py +4 -3
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_inpaint.py +5 -4
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_upscale.py +2 -2
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +18 -13
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_depth2img.py +30 -8
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_image_variation.py +24 -10
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +28 -12
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +39 -18
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +17 -6
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_latent_upscale.py +13 -3
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +20 -3
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip.py +14 -2
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip_img2img.py +13 -1
- diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py +16 -17
- diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_img2img.py +136 -18
- diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_inpaint.py +150 -21
- diffusers/pipelines/stable_diffusion_attend_and_excite/pipeline_stable_diffusion_attend_and_excite.py +15 -3
- diffusers/pipelines/stable_diffusion_diffedit/pipeline_stable_diffusion_diffedit.py +26 -11
- diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen.py +15 -3
- diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py +22 -4
- diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_k_diffusion.py +30 -13
- diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_xl_k_diffusion.py +12 -4
- diffusers/pipelines/stable_diffusion_ldm3d/pipeline_stable_diffusion_ldm3d.py +15 -3
- diffusers/pipelines/stable_diffusion_panorama/pipeline_stable_diffusion_panorama.py +15 -3
- diffusers/pipelines/stable_diffusion_safe/pipeline_stable_diffusion_safe.py +26 -12
- diffusers/pipelines/stable_diffusion_sag/pipeline_stable_diffusion_sag.py +16 -4
- diffusers/pipelines/stable_diffusion_xl/pipeline_flax_stable_diffusion_xl.py +1 -1
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +12 -4
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +7 -3
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +10 -6
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +11 -4
- diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +13 -2
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_adapter.py +18 -4
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +26 -5
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +13 -1
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +13 -1
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +28 -6
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +26 -4
- diffusers/pipelines/transformers_loading_utils.py +121 -0
- diffusers/pipelines/unclip/pipeline_unclip.py +11 -1
- diffusers/pipelines/unclip/pipeline_unclip_image_variation.py +11 -1
- diffusers/pipelines/unidiffuser/pipeline_unidiffuser.py +19 -2
- diffusers/pipelines/wan/__init__.py +51 -0
- diffusers/pipelines/wan/pipeline_output.py +20 -0
- diffusers/pipelines/wan/pipeline_wan.py +593 -0
- diffusers/pipelines/wan/pipeline_wan_i2v.py +722 -0
- diffusers/pipelines/wan/pipeline_wan_video2video.py +725 -0
- diffusers/pipelines/wuerstchen/modeling_wuerstchen_prior.py +7 -31
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen.py +12 -1
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen_prior.py +12 -1
- diffusers/quantizers/auto.py +5 -1
- diffusers/quantizers/base.py +5 -9
- diffusers/quantizers/bitsandbytes/bnb_quantizer.py +41 -29
- diffusers/quantizers/bitsandbytes/utils.py +30 -20
- diffusers/quantizers/gguf/gguf_quantizer.py +1 -0
- diffusers/quantizers/gguf/utils.py +4 -2
- diffusers/quantizers/quantization_config.py +59 -4
- diffusers/quantizers/quanto/__init__.py +1 -0
- diffusers/quantizers/quanto/quanto_quantizer.py +177 -0
- diffusers/quantizers/quanto/utils.py +60 -0
- diffusers/quantizers/torchao/__init__.py +1 -1
- diffusers/quantizers/torchao/torchao_quantizer.py +47 -2
- diffusers/schedulers/__init__.py +2 -1
- diffusers/schedulers/scheduling_consistency_models.py +1 -2
- diffusers/schedulers/scheduling_ddim_inverse.py +1 -1
- diffusers/schedulers/scheduling_ddpm.py +2 -3
- diffusers/schedulers/scheduling_ddpm_parallel.py +1 -2
- diffusers/schedulers/scheduling_dpmsolver_multistep.py +12 -4
- diffusers/schedulers/scheduling_edm_euler.py +45 -10
- diffusers/schedulers/scheduling_flow_match_euler_discrete.py +116 -28
- diffusers/schedulers/scheduling_flow_match_heun_discrete.py +7 -6
- diffusers/schedulers/scheduling_heun_discrete.py +1 -1
- diffusers/schedulers/scheduling_lcm.py +1 -2
- diffusers/schedulers/scheduling_lms_discrete.py +1 -1
- diffusers/schedulers/scheduling_repaint.py +5 -1
- diffusers/schedulers/scheduling_scm.py +265 -0
- diffusers/schedulers/scheduling_tcd.py +1 -2
- diffusers/schedulers/scheduling_utils.py +2 -1
- diffusers/training_utils.py +14 -7
- diffusers/utils/__init__.py +10 -2
- diffusers/utils/constants.py +13 -1
- diffusers/utils/deprecation_utils.py +1 -1
- diffusers/utils/dummy_bitsandbytes_objects.py +17 -0
- diffusers/utils/dummy_gguf_objects.py +17 -0
- diffusers/utils/dummy_optimum_quanto_objects.py +17 -0
- diffusers/utils/dummy_pt_objects.py +233 -0
- diffusers/utils/dummy_torch_and_transformers_and_opencv_objects.py +17 -0
- diffusers/utils/dummy_torch_and_transformers_objects.py +270 -0
- diffusers/utils/dummy_torchao_objects.py +17 -0
- diffusers/utils/dynamic_modules_utils.py +1 -1
- diffusers/utils/export_utils.py +28 -3
- diffusers/utils/hub_utils.py +52 -102
- diffusers/utils/import_utils.py +121 -221
- diffusers/utils/loading_utils.py +14 -1
- diffusers/utils/logging.py +1 -2
- diffusers/utils/peft_utils.py +6 -14
- diffusers/utils/remote_utils.py +425 -0
- diffusers/utils/source_code_parsing_utils.py +52 -0
- diffusers/utils/state_dict_utils.py +15 -1
- diffusers/utils/testing_utils.py +243 -13
- diffusers/utils/torch_utils.py +10 -0
- diffusers/utils/typing_utils.py +91 -0
- diffusers/video_processor.py +1 -1
- {diffusers-0.32.1.dist-info → diffusers-0.33.0.dist-info}/METADATA +76 -44
- diffusers-0.33.0.dist-info/RECORD +608 -0
- {diffusers-0.32.1.dist-info → diffusers-0.33.0.dist-info}/WHEEL +1 -1
- diffusers-0.32.1.dist-info/RECORD +0 -550
- {diffusers-0.32.1.dist-info → diffusers-0.33.0.dist-info}/LICENSE +0 -0
- {diffusers-0.32.1.dist-info → diffusers-0.33.0.dist-info}/entry_points.txt +0 -0
- {diffusers-0.32.1.dist-info → diffusers-0.33.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,855 @@
|
|
1
|
+
# Copyright 2025 The Wan Team 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
|
+
from typing import List, Optional, Tuple, Union
|
16
|
+
|
17
|
+
import torch
|
18
|
+
import torch.nn as nn
|
19
|
+
import torch.nn.functional as F
|
20
|
+
import torch.utils.checkpoint
|
21
|
+
|
22
|
+
from ...configuration_utils import ConfigMixin, register_to_config
|
23
|
+
from ...loaders import FromOriginalModelMixin
|
24
|
+
from ...utils import logging
|
25
|
+
from ...utils.accelerate_utils import apply_forward_hook
|
26
|
+
from ..activations import get_activation
|
27
|
+
from ..modeling_outputs import AutoencoderKLOutput
|
28
|
+
from ..modeling_utils import ModelMixin
|
29
|
+
from .vae import DecoderOutput, DiagonalGaussianDistribution
|
30
|
+
|
31
|
+
|
32
|
+
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
33
|
+
|
34
|
+
CACHE_T = 2
|
35
|
+
|
36
|
+
|
37
|
+
class WanCausalConv3d(nn.Conv3d):
|
38
|
+
r"""
|
39
|
+
A custom 3D causal convolution layer with feature caching support.
|
40
|
+
|
41
|
+
This layer extends the standard Conv3D layer by ensuring causality in the time dimension and handling feature
|
42
|
+
caching for efficient inference.
|
43
|
+
|
44
|
+
Args:
|
45
|
+
in_channels (int): Number of channels in the input image
|
46
|
+
out_channels (int): Number of channels produced by the convolution
|
47
|
+
kernel_size (int or tuple): Size of the convolving kernel
|
48
|
+
stride (int or tuple, optional): Stride of the convolution. Default: 1
|
49
|
+
padding (int or tuple, optional): Zero-padding added to all three sides of the input. Default: 0
|
50
|
+
"""
|
51
|
+
|
52
|
+
def __init__(
|
53
|
+
self,
|
54
|
+
in_channels: int,
|
55
|
+
out_channels: int,
|
56
|
+
kernel_size: Union[int, Tuple[int, int, int]],
|
57
|
+
stride: Union[int, Tuple[int, int, int]] = 1,
|
58
|
+
padding: Union[int, Tuple[int, int, int]] = 0,
|
59
|
+
) -> None:
|
60
|
+
super().__init__(
|
61
|
+
in_channels=in_channels,
|
62
|
+
out_channels=out_channels,
|
63
|
+
kernel_size=kernel_size,
|
64
|
+
stride=stride,
|
65
|
+
padding=padding,
|
66
|
+
)
|
67
|
+
|
68
|
+
# Set up causal padding
|
69
|
+
self._padding = (self.padding[2], self.padding[2], self.padding[1], self.padding[1], 2 * self.padding[0], 0)
|
70
|
+
self.padding = (0, 0, 0)
|
71
|
+
|
72
|
+
def forward(self, x, cache_x=None):
|
73
|
+
padding = list(self._padding)
|
74
|
+
if cache_x is not None and self._padding[4] > 0:
|
75
|
+
cache_x = cache_x.to(x.device)
|
76
|
+
x = torch.cat([cache_x, x], dim=2)
|
77
|
+
padding[4] -= cache_x.shape[2]
|
78
|
+
x = F.pad(x, padding)
|
79
|
+
return super().forward(x)
|
80
|
+
|
81
|
+
|
82
|
+
class WanRMS_norm(nn.Module):
|
83
|
+
r"""
|
84
|
+
A custom RMS normalization layer.
|
85
|
+
|
86
|
+
Args:
|
87
|
+
dim (int): The number of dimensions to normalize over.
|
88
|
+
channel_first (bool, optional): Whether the input tensor has channels as the first dimension.
|
89
|
+
Default is True.
|
90
|
+
images (bool, optional): Whether the input represents image data. Default is True.
|
91
|
+
bias (bool, optional): Whether to include a learnable bias term. Default is False.
|
92
|
+
"""
|
93
|
+
|
94
|
+
def __init__(self, dim: int, channel_first: bool = True, images: bool = True, bias: bool = False) -> None:
|
95
|
+
super().__init__()
|
96
|
+
broadcastable_dims = (1, 1, 1) if not images else (1, 1)
|
97
|
+
shape = (dim, *broadcastable_dims) if channel_first else (dim,)
|
98
|
+
|
99
|
+
self.channel_first = channel_first
|
100
|
+
self.scale = dim**0.5
|
101
|
+
self.gamma = nn.Parameter(torch.ones(shape))
|
102
|
+
self.bias = nn.Parameter(torch.zeros(shape)) if bias else 0.0
|
103
|
+
|
104
|
+
def forward(self, x):
|
105
|
+
return F.normalize(x, dim=(1 if self.channel_first else -1)) * self.scale * self.gamma + self.bias
|
106
|
+
|
107
|
+
|
108
|
+
class WanUpsample(nn.Upsample):
|
109
|
+
r"""
|
110
|
+
Perform upsampling while ensuring the output tensor has the same data type as the input.
|
111
|
+
|
112
|
+
Args:
|
113
|
+
x (torch.Tensor): Input tensor to be upsampled.
|
114
|
+
|
115
|
+
Returns:
|
116
|
+
torch.Tensor: Upsampled tensor with the same data type as the input.
|
117
|
+
"""
|
118
|
+
|
119
|
+
def forward(self, x):
|
120
|
+
return super().forward(x.float()).type_as(x)
|
121
|
+
|
122
|
+
|
123
|
+
class WanResample(nn.Module):
|
124
|
+
r"""
|
125
|
+
A custom resampling module for 2D and 3D data.
|
126
|
+
|
127
|
+
Args:
|
128
|
+
dim (int): The number of input/output channels.
|
129
|
+
mode (str): The resampling mode. Must be one of:
|
130
|
+
- 'none': No resampling (identity operation).
|
131
|
+
- 'upsample2d': 2D upsampling with nearest-exact interpolation and convolution.
|
132
|
+
- 'upsample3d': 3D upsampling with nearest-exact interpolation, convolution, and causal 3D convolution.
|
133
|
+
- 'downsample2d': 2D downsampling with zero-padding and convolution.
|
134
|
+
- 'downsample3d': 3D downsampling with zero-padding, convolution, and causal 3D convolution.
|
135
|
+
"""
|
136
|
+
|
137
|
+
def __init__(self, dim: int, mode: str) -> None:
|
138
|
+
super().__init__()
|
139
|
+
self.dim = dim
|
140
|
+
self.mode = mode
|
141
|
+
|
142
|
+
# layers
|
143
|
+
if mode == "upsample2d":
|
144
|
+
self.resample = nn.Sequential(
|
145
|
+
WanUpsample(scale_factor=(2.0, 2.0), mode="nearest-exact"), nn.Conv2d(dim, dim // 2, 3, padding=1)
|
146
|
+
)
|
147
|
+
elif mode == "upsample3d":
|
148
|
+
self.resample = nn.Sequential(
|
149
|
+
WanUpsample(scale_factor=(2.0, 2.0), mode="nearest-exact"), nn.Conv2d(dim, dim // 2, 3, padding=1)
|
150
|
+
)
|
151
|
+
self.time_conv = WanCausalConv3d(dim, dim * 2, (3, 1, 1), padding=(1, 0, 0))
|
152
|
+
|
153
|
+
elif mode == "downsample2d":
|
154
|
+
self.resample = nn.Sequential(nn.ZeroPad2d((0, 1, 0, 1)), nn.Conv2d(dim, dim, 3, stride=(2, 2)))
|
155
|
+
elif mode == "downsample3d":
|
156
|
+
self.resample = nn.Sequential(nn.ZeroPad2d((0, 1, 0, 1)), nn.Conv2d(dim, dim, 3, stride=(2, 2)))
|
157
|
+
self.time_conv = WanCausalConv3d(dim, dim, (3, 1, 1), stride=(2, 1, 1), padding=(0, 0, 0))
|
158
|
+
|
159
|
+
else:
|
160
|
+
self.resample = nn.Identity()
|
161
|
+
|
162
|
+
def forward(self, x, feat_cache=None, feat_idx=[0]):
|
163
|
+
b, c, t, h, w = x.size()
|
164
|
+
if self.mode == "upsample3d":
|
165
|
+
if feat_cache is not None:
|
166
|
+
idx = feat_idx[0]
|
167
|
+
if feat_cache[idx] is None:
|
168
|
+
feat_cache[idx] = "Rep"
|
169
|
+
feat_idx[0] += 1
|
170
|
+
else:
|
171
|
+
cache_x = x[:, :, -CACHE_T:, :, :].clone()
|
172
|
+
if cache_x.shape[2] < 2 and feat_cache[idx] is not None and feat_cache[idx] != "Rep":
|
173
|
+
# cache last frame of last two chunk
|
174
|
+
cache_x = torch.cat(
|
175
|
+
[feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2
|
176
|
+
)
|
177
|
+
if cache_x.shape[2] < 2 and feat_cache[idx] is not None and feat_cache[idx] == "Rep":
|
178
|
+
cache_x = torch.cat([torch.zeros_like(cache_x).to(cache_x.device), cache_x], dim=2)
|
179
|
+
if feat_cache[idx] == "Rep":
|
180
|
+
x = self.time_conv(x)
|
181
|
+
else:
|
182
|
+
x = self.time_conv(x, feat_cache[idx])
|
183
|
+
feat_cache[idx] = cache_x
|
184
|
+
feat_idx[0] += 1
|
185
|
+
|
186
|
+
x = x.reshape(b, 2, c, t, h, w)
|
187
|
+
x = torch.stack((x[:, 0, :, :, :, :], x[:, 1, :, :, :, :]), 3)
|
188
|
+
x = x.reshape(b, c, t * 2, h, w)
|
189
|
+
t = x.shape[2]
|
190
|
+
x = x.permute(0, 2, 1, 3, 4).reshape(b * t, c, h, w)
|
191
|
+
x = self.resample(x)
|
192
|
+
x = x.view(b, t, x.size(1), x.size(2), x.size(3)).permute(0, 2, 1, 3, 4)
|
193
|
+
|
194
|
+
if self.mode == "downsample3d":
|
195
|
+
if feat_cache is not None:
|
196
|
+
idx = feat_idx[0]
|
197
|
+
if feat_cache[idx] is None:
|
198
|
+
feat_cache[idx] = x.clone()
|
199
|
+
feat_idx[0] += 1
|
200
|
+
else:
|
201
|
+
cache_x = x[:, :, -1:, :, :].clone()
|
202
|
+
x = self.time_conv(torch.cat([feat_cache[idx][:, :, -1:, :, :], x], 2))
|
203
|
+
feat_cache[idx] = cache_x
|
204
|
+
feat_idx[0] += 1
|
205
|
+
return x
|
206
|
+
|
207
|
+
|
208
|
+
class WanResidualBlock(nn.Module):
|
209
|
+
r"""
|
210
|
+
A custom residual block module.
|
211
|
+
|
212
|
+
Args:
|
213
|
+
in_dim (int): Number of input channels.
|
214
|
+
out_dim (int): Number of output channels.
|
215
|
+
dropout (float, optional): Dropout rate for the dropout layer. Default is 0.0.
|
216
|
+
non_linearity (str, optional): Type of non-linearity to use. Default is "silu".
|
217
|
+
"""
|
218
|
+
|
219
|
+
def __init__(
|
220
|
+
self,
|
221
|
+
in_dim: int,
|
222
|
+
out_dim: int,
|
223
|
+
dropout: float = 0.0,
|
224
|
+
non_linearity: str = "silu",
|
225
|
+
) -> None:
|
226
|
+
super().__init__()
|
227
|
+
self.in_dim = in_dim
|
228
|
+
self.out_dim = out_dim
|
229
|
+
self.nonlinearity = get_activation(non_linearity)
|
230
|
+
|
231
|
+
# layers
|
232
|
+
self.norm1 = WanRMS_norm(in_dim, images=False)
|
233
|
+
self.conv1 = WanCausalConv3d(in_dim, out_dim, 3, padding=1)
|
234
|
+
self.norm2 = WanRMS_norm(out_dim, images=False)
|
235
|
+
self.dropout = nn.Dropout(dropout)
|
236
|
+
self.conv2 = WanCausalConv3d(out_dim, out_dim, 3, padding=1)
|
237
|
+
self.conv_shortcut = WanCausalConv3d(in_dim, out_dim, 1) if in_dim != out_dim else nn.Identity()
|
238
|
+
|
239
|
+
def forward(self, x, feat_cache=None, feat_idx=[0]):
|
240
|
+
# Apply shortcut connection
|
241
|
+
h = self.conv_shortcut(x)
|
242
|
+
|
243
|
+
# First normalization and activation
|
244
|
+
x = self.norm1(x)
|
245
|
+
x = self.nonlinearity(x)
|
246
|
+
|
247
|
+
if feat_cache is not None:
|
248
|
+
idx = feat_idx[0]
|
249
|
+
cache_x = x[:, :, -CACHE_T:, :, :].clone()
|
250
|
+
if cache_x.shape[2] < 2 and feat_cache[idx] is not None:
|
251
|
+
cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2)
|
252
|
+
|
253
|
+
x = self.conv1(x, feat_cache[idx])
|
254
|
+
feat_cache[idx] = cache_x
|
255
|
+
feat_idx[0] += 1
|
256
|
+
else:
|
257
|
+
x = self.conv1(x)
|
258
|
+
|
259
|
+
# Second normalization and activation
|
260
|
+
x = self.norm2(x)
|
261
|
+
x = self.nonlinearity(x)
|
262
|
+
|
263
|
+
# Dropout
|
264
|
+
x = self.dropout(x)
|
265
|
+
|
266
|
+
if feat_cache is not None:
|
267
|
+
idx = feat_idx[0]
|
268
|
+
cache_x = x[:, :, -CACHE_T:, :, :].clone()
|
269
|
+
if cache_x.shape[2] < 2 and feat_cache[idx] is not None:
|
270
|
+
cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2)
|
271
|
+
|
272
|
+
x = self.conv2(x, feat_cache[idx])
|
273
|
+
feat_cache[idx] = cache_x
|
274
|
+
feat_idx[0] += 1
|
275
|
+
else:
|
276
|
+
x = self.conv2(x)
|
277
|
+
|
278
|
+
# Add residual connection
|
279
|
+
return x + h
|
280
|
+
|
281
|
+
|
282
|
+
class WanAttentionBlock(nn.Module):
|
283
|
+
r"""
|
284
|
+
Causal self-attention with a single head.
|
285
|
+
|
286
|
+
Args:
|
287
|
+
dim (int): The number of channels in the input tensor.
|
288
|
+
"""
|
289
|
+
|
290
|
+
def __init__(self, dim):
|
291
|
+
super().__init__()
|
292
|
+
self.dim = dim
|
293
|
+
|
294
|
+
# layers
|
295
|
+
self.norm = WanRMS_norm(dim)
|
296
|
+
self.to_qkv = nn.Conv2d(dim, dim * 3, 1)
|
297
|
+
self.proj = nn.Conv2d(dim, dim, 1)
|
298
|
+
|
299
|
+
def forward(self, x):
|
300
|
+
identity = x
|
301
|
+
batch_size, channels, time, height, width = x.size()
|
302
|
+
|
303
|
+
x = x.permute(0, 2, 1, 3, 4).reshape(batch_size * time, channels, height, width)
|
304
|
+
x = self.norm(x)
|
305
|
+
|
306
|
+
# compute query, key, value
|
307
|
+
qkv = self.to_qkv(x)
|
308
|
+
qkv = qkv.reshape(batch_size * time, 1, channels * 3, -1)
|
309
|
+
qkv = qkv.permute(0, 1, 3, 2).contiguous()
|
310
|
+
q, k, v = qkv.chunk(3, dim=-1)
|
311
|
+
|
312
|
+
# apply attention
|
313
|
+
x = F.scaled_dot_product_attention(q, k, v)
|
314
|
+
|
315
|
+
x = x.squeeze(1).permute(0, 2, 1).reshape(batch_size * time, channels, height, width)
|
316
|
+
|
317
|
+
# output projection
|
318
|
+
x = self.proj(x)
|
319
|
+
|
320
|
+
# Reshape back: [(b*t), c, h, w] -> [b, c, t, h, w]
|
321
|
+
x = x.view(batch_size, time, channels, height, width)
|
322
|
+
x = x.permute(0, 2, 1, 3, 4)
|
323
|
+
|
324
|
+
return x + identity
|
325
|
+
|
326
|
+
|
327
|
+
class WanMidBlock(nn.Module):
|
328
|
+
"""
|
329
|
+
Middle block for WanVAE encoder and decoder.
|
330
|
+
|
331
|
+
Args:
|
332
|
+
dim (int): Number of input/output channels.
|
333
|
+
dropout (float): Dropout rate.
|
334
|
+
non_linearity (str): Type of non-linearity to use.
|
335
|
+
"""
|
336
|
+
|
337
|
+
def __init__(self, dim: int, dropout: float = 0.0, non_linearity: str = "silu", num_layers: int = 1):
|
338
|
+
super().__init__()
|
339
|
+
self.dim = dim
|
340
|
+
|
341
|
+
# Create the components
|
342
|
+
resnets = [WanResidualBlock(dim, dim, dropout, non_linearity)]
|
343
|
+
attentions = []
|
344
|
+
for _ in range(num_layers):
|
345
|
+
attentions.append(WanAttentionBlock(dim))
|
346
|
+
resnets.append(WanResidualBlock(dim, dim, dropout, non_linearity))
|
347
|
+
self.attentions = nn.ModuleList(attentions)
|
348
|
+
self.resnets = nn.ModuleList(resnets)
|
349
|
+
|
350
|
+
self.gradient_checkpointing = False
|
351
|
+
|
352
|
+
def forward(self, x, feat_cache=None, feat_idx=[0]):
|
353
|
+
# First residual block
|
354
|
+
x = self.resnets[0](x, feat_cache, feat_idx)
|
355
|
+
|
356
|
+
# Process through attention and residual blocks
|
357
|
+
for attn, resnet in zip(self.attentions, self.resnets[1:]):
|
358
|
+
if attn is not None:
|
359
|
+
x = attn(x)
|
360
|
+
|
361
|
+
x = resnet(x, feat_cache, feat_idx)
|
362
|
+
|
363
|
+
return x
|
364
|
+
|
365
|
+
|
366
|
+
class WanEncoder3d(nn.Module):
|
367
|
+
r"""
|
368
|
+
A 3D encoder module.
|
369
|
+
|
370
|
+
Args:
|
371
|
+
dim (int): The base number of channels in the first layer.
|
372
|
+
z_dim (int): The dimensionality of the latent space.
|
373
|
+
dim_mult (list of int): Multipliers for the number of channels in each block.
|
374
|
+
num_res_blocks (int): Number of residual blocks in each block.
|
375
|
+
attn_scales (list of float): Scales at which to apply attention mechanisms.
|
376
|
+
temperal_downsample (list of bool): Whether to downsample temporally in each block.
|
377
|
+
dropout (float): Dropout rate for the dropout layers.
|
378
|
+
non_linearity (str): Type of non-linearity to use.
|
379
|
+
"""
|
380
|
+
|
381
|
+
def __init__(
|
382
|
+
self,
|
383
|
+
dim=128,
|
384
|
+
z_dim=4,
|
385
|
+
dim_mult=[1, 2, 4, 4],
|
386
|
+
num_res_blocks=2,
|
387
|
+
attn_scales=[],
|
388
|
+
temperal_downsample=[True, True, False],
|
389
|
+
dropout=0.0,
|
390
|
+
non_linearity: str = "silu",
|
391
|
+
):
|
392
|
+
super().__init__()
|
393
|
+
self.dim = dim
|
394
|
+
self.z_dim = z_dim
|
395
|
+
self.dim_mult = dim_mult
|
396
|
+
self.num_res_blocks = num_res_blocks
|
397
|
+
self.attn_scales = attn_scales
|
398
|
+
self.temperal_downsample = temperal_downsample
|
399
|
+
self.nonlinearity = get_activation(non_linearity)
|
400
|
+
|
401
|
+
# dimensions
|
402
|
+
dims = [dim * u for u in [1] + dim_mult]
|
403
|
+
scale = 1.0
|
404
|
+
|
405
|
+
# init block
|
406
|
+
self.conv_in = WanCausalConv3d(3, dims[0], 3, padding=1)
|
407
|
+
|
408
|
+
# downsample blocks
|
409
|
+
self.down_blocks = nn.ModuleList([])
|
410
|
+
for i, (in_dim, out_dim) in enumerate(zip(dims[:-1], dims[1:])):
|
411
|
+
# residual (+attention) blocks
|
412
|
+
for _ in range(num_res_blocks):
|
413
|
+
self.down_blocks.append(WanResidualBlock(in_dim, out_dim, dropout))
|
414
|
+
if scale in attn_scales:
|
415
|
+
self.down_blocks.append(WanAttentionBlock(out_dim))
|
416
|
+
in_dim = out_dim
|
417
|
+
|
418
|
+
# downsample block
|
419
|
+
if i != len(dim_mult) - 1:
|
420
|
+
mode = "downsample3d" if temperal_downsample[i] else "downsample2d"
|
421
|
+
self.down_blocks.append(WanResample(out_dim, mode=mode))
|
422
|
+
scale /= 2.0
|
423
|
+
|
424
|
+
# middle blocks
|
425
|
+
self.mid_block = WanMidBlock(out_dim, dropout, non_linearity, num_layers=1)
|
426
|
+
|
427
|
+
# output blocks
|
428
|
+
self.norm_out = WanRMS_norm(out_dim, images=False)
|
429
|
+
self.conv_out = WanCausalConv3d(out_dim, z_dim, 3, padding=1)
|
430
|
+
|
431
|
+
self.gradient_checkpointing = False
|
432
|
+
|
433
|
+
def forward(self, x, feat_cache=None, feat_idx=[0]):
|
434
|
+
if feat_cache is not None:
|
435
|
+
idx = feat_idx[0]
|
436
|
+
cache_x = x[:, :, -CACHE_T:, :, :].clone()
|
437
|
+
if cache_x.shape[2] < 2 and feat_cache[idx] is not None:
|
438
|
+
# cache last frame of last two chunk
|
439
|
+
cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2)
|
440
|
+
x = self.conv_in(x, feat_cache[idx])
|
441
|
+
feat_cache[idx] = cache_x
|
442
|
+
feat_idx[0] += 1
|
443
|
+
else:
|
444
|
+
x = self.conv_in(x)
|
445
|
+
|
446
|
+
## downsamples
|
447
|
+
for layer in self.down_blocks:
|
448
|
+
if feat_cache is not None:
|
449
|
+
x = layer(x, feat_cache, feat_idx)
|
450
|
+
else:
|
451
|
+
x = layer(x)
|
452
|
+
|
453
|
+
## middle
|
454
|
+
x = self.mid_block(x, feat_cache, feat_idx)
|
455
|
+
|
456
|
+
## head
|
457
|
+
x = self.norm_out(x)
|
458
|
+
x = self.nonlinearity(x)
|
459
|
+
if feat_cache is not None:
|
460
|
+
idx = feat_idx[0]
|
461
|
+
cache_x = x[:, :, -CACHE_T:, :, :].clone()
|
462
|
+
if cache_x.shape[2] < 2 and feat_cache[idx] is not None:
|
463
|
+
# cache last frame of last two chunk
|
464
|
+
cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2)
|
465
|
+
x = self.conv_out(x, feat_cache[idx])
|
466
|
+
feat_cache[idx] = cache_x
|
467
|
+
feat_idx[0] += 1
|
468
|
+
else:
|
469
|
+
x = self.conv_out(x)
|
470
|
+
return x
|
471
|
+
|
472
|
+
|
473
|
+
class WanUpBlock(nn.Module):
|
474
|
+
"""
|
475
|
+
A block that handles upsampling for the WanVAE decoder.
|
476
|
+
|
477
|
+
Args:
|
478
|
+
in_dim (int): Input dimension
|
479
|
+
out_dim (int): Output dimension
|
480
|
+
num_res_blocks (int): Number of residual blocks
|
481
|
+
dropout (float): Dropout rate
|
482
|
+
upsample_mode (str, optional): Mode for upsampling ('upsample2d' or 'upsample3d')
|
483
|
+
non_linearity (str): Type of non-linearity to use
|
484
|
+
"""
|
485
|
+
|
486
|
+
def __init__(
|
487
|
+
self,
|
488
|
+
in_dim: int,
|
489
|
+
out_dim: int,
|
490
|
+
num_res_blocks: int,
|
491
|
+
dropout: float = 0.0,
|
492
|
+
upsample_mode: Optional[str] = None,
|
493
|
+
non_linearity: str = "silu",
|
494
|
+
):
|
495
|
+
super().__init__()
|
496
|
+
self.in_dim = in_dim
|
497
|
+
self.out_dim = out_dim
|
498
|
+
|
499
|
+
# Create layers list
|
500
|
+
resnets = []
|
501
|
+
# Add residual blocks and attention if needed
|
502
|
+
current_dim = in_dim
|
503
|
+
for _ in range(num_res_blocks + 1):
|
504
|
+
resnets.append(WanResidualBlock(current_dim, out_dim, dropout, non_linearity))
|
505
|
+
current_dim = out_dim
|
506
|
+
|
507
|
+
self.resnets = nn.ModuleList(resnets)
|
508
|
+
|
509
|
+
# Add upsampling layer if needed
|
510
|
+
self.upsamplers = None
|
511
|
+
if upsample_mode is not None:
|
512
|
+
self.upsamplers = nn.ModuleList([WanResample(out_dim, mode=upsample_mode)])
|
513
|
+
|
514
|
+
self.gradient_checkpointing = False
|
515
|
+
|
516
|
+
def forward(self, x, feat_cache=None, feat_idx=[0]):
|
517
|
+
"""
|
518
|
+
Forward pass through the upsampling block.
|
519
|
+
|
520
|
+
Args:
|
521
|
+
x (torch.Tensor): Input tensor
|
522
|
+
feat_cache (list, optional): Feature cache for causal convolutions
|
523
|
+
feat_idx (list, optional): Feature index for cache management
|
524
|
+
|
525
|
+
Returns:
|
526
|
+
torch.Tensor: Output tensor
|
527
|
+
"""
|
528
|
+
for resnet in self.resnets:
|
529
|
+
if feat_cache is not None:
|
530
|
+
x = resnet(x, feat_cache, feat_idx)
|
531
|
+
else:
|
532
|
+
x = resnet(x)
|
533
|
+
|
534
|
+
if self.upsamplers is not None:
|
535
|
+
if feat_cache is not None:
|
536
|
+
x = self.upsamplers[0](x, feat_cache, feat_idx)
|
537
|
+
else:
|
538
|
+
x = self.upsamplers[0](x)
|
539
|
+
return x
|
540
|
+
|
541
|
+
|
542
|
+
class WanDecoder3d(nn.Module):
|
543
|
+
r"""
|
544
|
+
A 3D decoder module.
|
545
|
+
|
546
|
+
Args:
|
547
|
+
dim (int): The base number of channels in the first layer.
|
548
|
+
z_dim (int): The dimensionality of the latent space.
|
549
|
+
dim_mult (list of int): Multipliers for the number of channels in each block.
|
550
|
+
num_res_blocks (int): Number of residual blocks in each block.
|
551
|
+
attn_scales (list of float): Scales at which to apply attention mechanisms.
|
552
|
+
temperal_upsample (list of bool): Whether to upsample temporally in each block.
|
553
|
+
dropout (float): Dropout rate for the dropout layers.
|
554
|
+
non_linearity (str): Type of non-linearity to use.
|
555
|
+
"""
|
556
|
+
|
557
|
+
def __init__(
|
558
|
+
self,
|
559
|
+
dim=128,
|
560
|
+
z_dim=4,
|
561
|
+
dim_mult=[1, 2, 4, 4],
|
562
|
+
num_res_blocks=2,
|
563
|
+
attn_scales=[],
|
564
|
+
temperal_upsample=[False, True, True],
|
565
|
+
dropout=0.0,
|
566
|
+
non_linearity: str = "silu",
|
567
|
+
):
|
568
|
+
super().__init__()
|
569
|
+
self.dim = dim
|
570
|
+
self.z_dim = z_dim
|
571
|
+
self.dim_mult = dim_mult
|
572
|
+
self.num_res_blocks = num_res_blocks
|
573
|
+
self.attn_scales = attn_scales
|
574
|
+
self.temperal_upsample = temperal_upsample
|
575
|
+
|
576
|
+
self.nonlinearity = get_activation(non_linearity)
|
577
|
+
|
578
|
+
# dimensions
|
579
|
+
dims = [dim * u for u in [dim_mult[-1]] + dim_mult[::-1]]
|
580
|
+
scale = 1.0 / 2 ** (len(dim_mult) - 2)
|
581
|
+
|
582
|
+
# init block
|
583
|
+
self.conv_in = WanCausalConv3d(z_dim, dims[0], 3, padding=1)
|
584
|
+
|
585
|
+
# middle blocks
|
586
|
+
self.mid_block = WanMidBlock(dims[0], dropout, non_linearity, num_layers=1)
|
587
|
+
|
588
|
+
# upsample blocks
|
589
|
+
self.up_blocks = nn.ModuleList([])
|
590
|
+
for i, (in_dim, out_dim) in enumerate(zip(dims[:-1], dims[1:])):
|
591
|
+
# residual (+attention) blocks
|
592
|
+
if i > 0:
|
593
|
+
in_dim = in_dim // 2
|
594
|
+
|
595
|
+
# Determine if we need upsampling
|
596
|
+
upsample_mode = None
|
597
|
+
if i != len(dim_mult) - 1:
|
598
|
+
upsample_mode = "upsample3d" if temperal_upsample[i] else "upsample2d"
|
599
|
+
|
600
|
+
# Create and add the upsampling block
|
601
|
+
up_block = WanUpBlock(
|
602
|
+
in_dim=in_dim,
|
603
|
+
out_dim=out_dim,
|
604
|
+
num_res_blocks=num_res_blocks,
|
605
|
+
dropout=dropout,
|
606
|
+
upsample_mode=upsample_mode,
|
607
|
+
non_linearity=non_linearity,
|
608
|
+
)
|
609
|
+
self.up_blocks.append(up_block)
|
610
|
+
|
611
|
+
# Update scale for next iteration
|
612
|
+
if upsample_mode is not None:
|
613
|
+
scale *= 2.0
|
614
|
+
|
615
|
+
# output blocks
|
616
|
+
self.norm_out = WanRMS_norm(out_dim, images=False)
|
617
|
+
self.conv_out = WanCausalConv3d(out_dim, 3, 3, padding=1)
|
618
|
+
|
619
|
+
self.gradient_checkpointing = False
|
620
|
+
|
621
|
+
def forward(self, x, feat_cache=None, feat_idx=[0]):
|
622
|
+
## conv1
|
623
|
+
if feat_cache is not None:
|
624
|
+
idx = feat_idx[0]
|
625
|
+
cache_x = x[:, :, -CACHE_T:, :, :].clone()
|
626
|
+
if cache_x.shape[2] < 2 and feat_cache[idx] is not None:
|
627
|
+
# cache last frame of last two chunk
|
628
|
+
cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2)
|
629
|
+
x = self.conv_in(x, feat_cache[idx])
|
630
|
+
feat_cache[idx] = cache_x
|
631
|
+
feat_idx[0] += 1
|
632
|
+
else:
|
633
|
+
x = self.conv_in(x)
|
634
|
+
|
635
|
+
## middle
|
636
|
+
x = self.mid_block(x, feat_cache, feat_idx)
|
637
|
+
|
638
|
+
## upsamples
|
639
|
+
for up_block in self.up_blocks:
|
640
|
+
x = up_block(x, feat_cache, feat_idx)
|
641
|
+
|
642
|
+
## head
|
643
|
+
x = self.norm_out(x)
|
644
|
+
x = self.nonlinearity(x)
|
645
|
+
if feat_cache is not None:
|
646
|
+
idx = feat_idx[0]
|
647
|
+
cache_x = x[:, :, -CACHE_T:, :, :].clone()
|
648
|
+
if cache_x.shape[2] < 2 and feat_cache[idx] is not None:
|
649
|
+
# cache last frame of last two chunk
|
650
|
+
cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2)
|
651
|
+
x = self.conv_out(x, feat_cache[idx])
|
652
|
+
feat_cache[idx] = cache_x
|
653
|
+
feat_idx[0] += 1
|
654
|
+
else:
|
655
|
+
x = self.conv_out(x)
|
656
|
+
return x
|
657
|
+
|
658
|
+
|
659
|
+
class AutoencoderKLWan(ModelMixin, ConfigMixin, FromOriginalModelMixin):
|
660
|
+
r"""
|
661
|
+
A VAE model with KL loss for encoding videos into latents and decoding latent representations into videos.
|
662
|
+
Introduced in [Wan 2.1].
|
663
|
+
|
664
|
+
This model inherits from [`ModelMixin`]. Check the superclass documentation for it's generic methods implemented
|
665
|
+
for all models (such as downloading or saving).
|
666
|
+
"""
|
667
|
+
|
668
|
+
_supports_gradient_checkpointing = False
|
669
|
+
|
670
|
+
@register_to_config
|
671
|
+
def __init__(
|
672
|
+
self,
|
673
|
+
base_dim: int = 96,
|
674
|
+
z_dim: int = 16,
|
675
|
+
dim_mult: Tuple[int] = [1, 2, 4, 4],
|
676
|
+
num_res_blocks: int = 2,
|
677
|
+
attn_scales: List[float] = [],
|
678
|
+
temperal_downsample: List[bool] = [False, True, True],
|
679
|
+
dropout: float = 0.0,
|
680
|
+
latents_mean: List[float] = [
|
681
|
+
-0.7571,
|
682
|
+
-0.7089,
|
683
|
+
-0.9113,
|
684
|
+
0.1075,
|
685
|
+
-0.1745,
|
686
|
+
0.9653,
|
687
|
+
-0.1517,
|
688
|
+
1.5508,
|
689
|
+
0.4134,
|
690
|
+
-0.0715,
|
691
|
+
0.5517,
|
692
|
+
-0.3632,
|
693
|
+
-0.1922,
|
694
|
+
-0.9497,
|
695
|
+
0.2503,
|
696
|
+
-0.2921,
|
697
|
+
],
|
698
|
+
latents_std: List[float] = [
|
699
|
+
2.8184,
|
700
|
+
1.4541,
|
701
|
+
2.3275,
|
702
|
+
2.6558,
|
703
|
+
1.2196,
|
704
|
+
1.7708,
|
705
|
+
2.6052,
|
706
|
+
2.0743,
|
707
|
+
3.2687,
|
708
|
+
2.1526,
|
709
|
+
2.8652,
|
710
|
+
1.5579,
|
711
|
+
1.6382,
|
712
|
+
1.1253,
|
713
|
+
2.8251,
|
714
|
+
1.9160,
|
715
|
+
],
|
716
|
+
) -> None:
|
717
|
+
super().__init__()
|
718
|
+
|
719
|
+
self.z_dim = z_dim
|
720
|
+
self.temperal_downsample = temperal_downsample
|
721
|
+
self.temperal_upsample = temperal_downsample[::-1]
|
722
|
+
|
723
|
+
self.encoder = WanEncoder3d(
|
724
|
+
base_dim, z_dim * 2, dim_mult, num_res_blocks, attn_scales, self.temperal_downsample, dropout
|
725
|
+
)
|
726
|
+
self.quant_conv = WanCausalConv3d(z_dim * 2, z_dim * 2, 1)
|
727
|
+
self.post_quant_conv = WanCausalConv3d(z_dim, z_dim, 1)
|
728
|
+
|
729
|
+
self.decoder = WanDecoder3d(
|
730
|
+
base_dim, z_dim, dim_mult, num_res_blocks, attn_scales, self.temperal_upsample, dropout
|
731
|
+
)
|
732
|
+
|
733
|
+
def clear_cache(self):
|
734
|
+
def _count_conv3d(model):
|
735
|
+
count = 0
|
736
|
+
for m in model.modules():
|
737
|
+
if isinstance(m, WanCausalConv3d):
|
738
|
+
count += 1
|
739
|
+
return count
|
740
|
+
|
741
|
+
self._conv_num = _count_conv3d(self.decoder)
|
742
|
+
self._conv_idx = [0]
|
743
|
+
self._feat_map = [None] * self._conv_num
|
744
|
+
# cache encode
|
745
|
+
self._enc_conv_num = _count_conv3d(self.encoder)
|
746
|
+
self._enc_conv_idx = [0]
|
747
|
+
self._enc_feat_map = [None] * self._enc_conv_num
|
748
|
+
|
749
|
+
def _encode(self, x: torch.Tensor) -> torch.Tensor:
|
750
|
+
self.clear_cache()
|
751
|
+
## cache
|
752
|
+
t = x.shape[2]
|
753
|
+
iter_ = 1 + (t - 1) // 4
|
754
|
+
for i in range(iter_):
|
755
|
+
self._enc_conv_idx = [0]
|
756
|
+
if i == 0:
|
757
|
+
out = self.encoder(x[:, :, :1, :, :], feat_cache=self._enc_feat_map, feat_idx=self._enc_conv_idx)
|
758
|
+
else:
|
759
|
+
out_ = self.encoder(
|
760
|
+
x[:, :, 1 + 4 * (i - 1) : 1 + 4 * i, :, :],
|
761
|
+
feat_cache=self._enc_feat_map,
|
762
|
+
feat_idx=self._enc_conv_idx,
|
763
|
+
)
|
764
|
+
out = torch.cat([out, out_], 2)
|
765
|
+
|
766
|
+
enc = self.quant_conv(out)
|
767
|
+
mu, logvar = enc[:, : self.z_dim, :, :, :], enc[:, self.z_dim :, :, :, :]
|
768
|
+
enc = torch.cat([mu, logvar], dim=1)
|
769
|
+
self.clear_cache()
|
770
|
+
return enc
|
771
|
+
|
772
|
+
@apply_forward_hook
|
773
|
+
def encode(
|
774
|
+
self, x: torch.Tensor, return_dict: bool = True
|
775
|
+
) -> Union[AutoencoderKLOutput, Tuple[DiagonalGaussianDistribution]]:
|
776
|
+
r"""
|
777
|
+
Encode a batch of images into latents.
|
778
|
+
|
779
|
+
Args:
|
780
|
+
x (`torch.Tensor`): Input batch of images.
|
781
|
+
return_dict (`bool`, *optional*, defaults to `True`):
|
782
|
+
Whether to return a [`~models.autoencoder_kl.AutoencoderKLOutput`] instead of a plain tuple.
|
783
|
+
|
784
|
+
Returns:
|
785
|
+
The latent representations of the encoded videos. If `return_dict` is True, a
|
786
|
+
[`~models.autoencoder_kl.AutoencoderKLOutput`] is returned, otherwise a plain `tuple` is returned.
|
787
|
+
"""
|
788
|
+
h = self._encode(x)
|
789
|
+
posterior = DiagonalGaussianDistribution(h)
|
790
|
+
if not return_dict:
|
791
|
+
return (posterior,)
|
792
|
+
return AutoencoderKLOutput(latent_dist=posterior)
|
793
|
+
|
794
|
+
def _decode(self, z: torch.Tensor, return_dict: bool = True) -> Union[DecoderOutput, torch.Tensor]:
|
795
|
+
self.clear_cache()
|
796
|
+
|
797
|
+
iter_ = z.shape[2]
|
798
|
+
x = self.post_quant_conv(z)
|
799
|
+
for i in range(iter_):
|
800
|
+
self._conv_idx = [0]
|
801
|
+
if i == 0:
|
802
|
+
out = self.decoder(x[:, :, i : i + 1, :, :], feat_cache=self._feat_map, feat_idx=self._conv_idx)
|
803
|
+
else:
|
804
|
+
out_ = self.decoder(x[:, :, i : i + 1, :, :], feat_cache=self._feat_map, feat_idx=self._conv_idx)
|
805
|
+
out = torch.cat([out, out_], 2)
|
806
|
+
|
807
|
+
out = torch.clamp(out, min=-1.0, max=1.0)
|
808
|
+
self.clear_cache()
|
809
|
+
if not return_dict:
|
810
|
+
return (out,)
|
811
|
+
|
812
|
+
return DecoderOutput(sample=out)
|
813
|
+
|
814
|
+
@apply_forward_hook
|
815
|
+
def decode(self, z: torch.Tensor, return_dict: bool = True) -> Union[DecoderOutput, torch.Tensor]:
|
816
|
+
r"""
|
817
|
+
Decode a batch of images.
|
818
|
+
|
819
|
+
Args:
|
820
|
+
z (`torch.Tensor`): Input batch of latent vectors.
|
821
|
+
return_dict (`bool`, *optional*, defaults to `True`):
|
822
|
+
Whether to return a [`~models.vae.DecoderOutput`] instead of a plain tuple.
|
823
|
+
|
824
|
+
Returns:
|
825
|
+
[`~models.vae.DecoderOutput`] or `tuple`:
|
826
|
+
If return_dict is True, a [`~models.vae.DecoderOutput`] is returned, otherwise a plain `tuple` is
|
827
|
+
returned.
|
828
|
+
"""
|
829
|
+
decoded = self._decode(z).sample
|
830
|
+
if not return_dict:
|
831
|
+
return (decoded,)
|
832
|
+
|
833
|
+
return DecoderOutput(sample=decoded)
|
834
|
+
|
835
|
+
def forward(
|
836
|
+
self,
|
837
|
+
sample: torch.Tensor,
|
838
|
+
sample_posterior: bool = False,
|
839
|
+
return_dict: bool = True,
|
840
|
+
generator: Optional[torch.Generator] = None,
|
841
|
+
) -> Union[DecoderOutput, torch.Tensor]:
|
842
|
+
"""
|
843
|
+
Args:
|
844
|
+
sample (`torch.Tensor`): Input sample.
|
845
|
+
return_dict (`bool`, *optional*, defaults to `True`):
|
846
|
+
Whether or not to return a [`DecoderOutput`] instead of a plain tuple.
|
847
|
+
"""
|
848
|
+
x = sample
|
849
|
+
posterior = self.encode(x).latent_dist
|
850
|
+
if sample_posterior:
|
851
|
+
z = posterior.sample(generator=generator)
|
852
|
+
else:
|
853
|
+
z = posterior.mode()
|
854
|
+
dec = self.decode(z, return_dict=return_dict)
|
855
|
+
return dec
|