diffusers 0.26.2__py3-none-any.whl → 0.27.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- diffusers/__init__.py +20 -1
- diffusers/commands/__init__.py +1 -1
- diffusers/commands/diffusers_cli.py +1 -1
- diffusers/commands/env.py +1 -1
- diffusers/commands/fp16_safetensors.py +1 -1
- diffusers/configuration_utils.py +7 -3
- diffusers/dependency_versions_check.py +1 -1
- diffusers/dependency_versions_table.py +2 -2
- diffusers/experimental/rl/value_guided_sampling.py +1 -1
- diffusers/image_processor.py +110 -4
- diffusers/loaders/autoencoder.py +28 -8
- diffusers/loaders/controlnet.py +17 -8
- diffusers/loaders/ip_adapter.py +86 -23
- diffusers/loaders/lora.py +105 -310
- diffusers/loaders/lora_conversion_utils.py +1 -1
- diffusers/loaders/peft.py +1 -1
- diffusers/loaders/single_file.py +51 -12
- diffusers/loaders/single_file_utils.py +278 -49
- diffusers/loaders/textual_inversion.py +23 -4
- diffusers/loaders/unet.py +195 -41
- diffusers/loaders/utils.py +1 -1
- diffusers/models/__init__.py +3 -1
- diffusers/models/activations.py +9 -9
- diffusers/models/attention.py +26 -36
- diffusers/models/attention_flax.py +1 -1
- diffusers/models/attention_processor.py +171 -114
- diffusers/models/autoencoders/autoencoder_asym_kl.py +1 -1
- diffusers/models/autoencoders/autoencoder_kl.py +3 -1
- diffusers/models/autoencoders/autoencoder_kl_temporal_decoder.py +1 -1
- diffusers/models/autoencoders/autoencoder_tiny.py +4 -2
- diffusers/models/autoencoders/consistency_decoder_vae.py +1 -1
- diffusers/models/autoencoders/vae.py +1 -1
- diffusers/models/controlnet.py +1 -1
- diffusers/models/controlnet_flax.py +1 -1
- diffusers/models/downsampling.py +8 -12
- diffusers/models/dual_transformer_2d.py +1 -1
- diffusers/models/embeddings.py +3 -4
- diffusers/models/embeddings_flax.py +1 -1
- diffusers/models/lora.py +33 -10
- 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 +4 -6
- diffusers/models/normalization.py +1 -1
- diffusers/models/resnet.py +31 -58
- diffusers/models/resnet_flax.py +1 -1
- diffusers/models/t5_film_transformer.py +1 -1
- diffusers/models/transformer_2d.py +1 -1
- diffusers/models/transformer_temporal.py +1 -1
- diffusers/models/transformers/dual_transformer_2d.py +1 -1
- diffusers/models/transformers/t5_film_transformer.py +1 -1
- diffusers/models/transformers/transformer_2d.py +29 -31
- diffusers/models/transformers/transformer_temporal.py +1 -1
- diffusers/models/unet_1d.py +1 -1
- diffusers/models/unet_1d_blocks.py +1 -1
- diffusers/models/unet_2d.py +1 -1
- diffusers/models/unet_2d_blocks.py +1 -1
- diffusers/models/unet_2d_condition.py +1 -1
- diffusers/models/unets/__init__.py +1 -0
- diffusers/models/unets/unet_1d.py +1 -1
- diffusers/models/unets/unet_1d_blocks.py +1 -1
- diffusers/models/unets/unet_2d.py +4 -4
- diffusers/models/unets/unet_2d_blocks.py +238 -98
- diffusers/models/unets/unet_2d_blocks_flax.py +1 -1
- diffusers/models/unets/unet_2d_condition.py +420 -323
- diffusers/models/unets/unet_2d_condition_flax.py +21 -12
- diffusers/models/unets/unet_3d_blocks.py +50 -40
- diffusers/models/unets/unet_3d_condition.py +47 -8
- diffusers/models/unets/unet_i2vgen_xl.py +75 -30
- diffusers/models/unets/unet_kandinsky3.py +1 -1
- diffusers/models/unets/unet_motion_model.py +48 -8
- diffusers/models/unets/unet_spatio_temporal_condition.py +1 -1
- diffusers/models/unets/unet_stable_cascade.py +610 -0
- diffusers/models/unets/uvit_2d.py +1 -1
- diffusers/models/upsampling.py +10 -16
- diffusers/models/vae_flax.py +1 -1
- diffusers/models/vq_model.py +1 -1
- diffusers/optimization.py +1 -1
- diffusers/pipelines/__init__.py +26 -0
- diffusers/pipelines/amused/pipeline_amused.py +1 -1
- diffusers/pipelines/amused/pipeline_amused_img2img.py +1 -1
- diffusers/pipelines/amused/pipeline_amused_inpaint.py +1 -1
- diffusers/pipelines/animatediff/pipeline_animatediff.py +162 -417
- diffusers/pipelines/animatediff/pipeline_animatediff_video2video.py +165 -137
- diffusers/pipelines/animatediff/pipeline_output.py +7 -6
- diffusers/pipelines/audioldm/pipeline_audioldm.py +3 -19
- diffusers/pipelines/audioldm2/modeling_audioldm2.py +1 -1
- diffusers/pipelines/audioldm2/pipeline_audioldm2.py +3 -3
- diffusers/pipelines/auto_pipeline.py +7 -16
- diffusers/pipelines/blip_diffusion/blip_image_processing.py +1 -1
- diffusers/pipelines/blip_diffusion/modeling_blip2.py +1 -1
- diffusers/pipelines/blip_diffusion/modeling_ctx_clip.py +2 -2
- diffusers/pipelines/blip_diffusion/pipeline_blip_diffusion.py +2 -2
- diffusers/pipelines/consistency_models/pipeline_consistency_models.py +1 -1
- diffusers/pipelines/controlnet/pipeline_controlnet.py +90 -90
- diffusers/pipelines/controlnet/pipeline_controlnet_blip_diffusion.py +2 -2
- diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +98 -90
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +92 -90
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +145 -70
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +126 -89
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +108 -96
- diffusers/pipelines/controlnet/pipeline_flax_controlnet.py +2 -2
- diffusers/pipelines/dance_diffusion/pipeline_dance_diffusion.py +1 -1
- diffusers/pipelines/ddim/pipeline_ddim.py +1 -1
- diffusers/pipelines/ddpm/pipeline_ddpm.py +1 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if.py +4 -4
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img.py +4 -4
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img_superresolution.py +5 -5
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.py +4 -4
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting_superresolution.py +5 -5
- diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.py +5 -5
- diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion.py +10 -120
- diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion_img2img.py +10 -91
- diffusers/pipelines/deprecated/audio_diffusion/mel.py +1 -1
- diffusers/pipelines/deprecated/audio_diffusion/pipeline_audio_diffusion.py +1 -1
- diffusers/pipelines/deprecated/latent_diffusion_uncond/pipeline_latent_diffusion_uncond.py +1 -1
- diffusers/pipelines/deprecated/pndm/pipeline_pndm.py +1 -1
- diffusers/pipelines/deprecated/repaint/pipeline_repaint.py +1 -1
- diffusers/pipelines/deprecated/score_sde_ve/pipeline_score_sde_ve.py +1 -1
- diffusers/pipelines/deprecated/spectrogram_diffusion/continuous_encoder.py +1 -1
- diffusers/pipelines/deprecated/spectrogram_diffusion/midi_utils.py +1 -1
- diffusers/pipelines/deprecated/spectrogram_diffusion/notes_encoder.py +1 -1
- diffusers/pipelines/deprecated/spectrogram_diffusion/pipeline_spectrogram_diffusion.py +1 -1
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_cycle_diffusion.py +5 -4
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_inpaint_legacy.py +5 -4
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_model_editing.py +7 -22
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_paradigms.py +5 -39
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_pix2pix_zero.py +5 -5
- diffusers/pipelines/deprecated/stochastic_karras_ve/pipeline_stochastic_karras_ve.py +1 -1
- diffusers/pipelines/deprecated/versatile_diffusion/modeling_text_unet.py +31 -22
- 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 -2
- diffusers/pipelines/deprecated/vq_diffusion/pipeline_vq_diffusion.py +1 -1
- diffusers/pipelines/dit/pipeline_dit.py +1 -1
- diffusers/pipelines/free_init_utils.py +184 -0
- diffusers/pipelines/i2vgen_xl/pipeline_i2vgen_xl.py +22 -104
- diffusers/pipelines/kandinsky/pipeline_kandinsky.py +1 -1
- diffusers/pipelines/kandinsky/pipeline_kandinsky_combined.py +1 -1
- diffusers/pipelines/kandinsky/pipeline_kandinsky_img2img.py +1 -1
- diffusers/pipelines/kandinsky/pipeline_kandinsky_inpaint.py +2 -2
- diffusers/pipelines/kandinsky/pipeline_kandinsky_prior.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_controlnet.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_controlnet_img2img.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpainting.py +2 -2
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +104 -93
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +112 -74
- diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py +1 -1
- diffusers/pipelines/ledits_pp/__init__.py +55 -0
- diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion.py +1505 -0
- diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion_xl.py +1797 -0
- diffusers/pipelines/ledits_pp/pipeline_output.py +43 -0
- diffusers/pipelines/musicldm/pipeline_musicldm.py +3 -19
- diffusers/pipelines/onnx_utils.py +1 -1
- diffusers/pipelines/paint_by_example/image_encoder.py +1 -1
- diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py +3 -3
- diffusers/pipelines/pia/pipeline_pia.py +168 -327
- diffusers/pipelines/pipeline_flax_utils.py +1 -1
- diffusers/pipelines/pipeline_loading_utils.py +508 -0
- diffusers/pipelines/pipeline_utils.py +188 -534
- diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +56 -10
- diffusers/pipelines/semantic_stable_diffusion/pipeline_semantic_stable_diffusion.py +3 -3
- diffusers/pipelines/shap_e/camera.py +1 -1
- diffusers/pipelines/shap_e/pipeline_shap_e.py +1 -1
- diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py +1 -1
- diffusers/pipelines/shap_e/renderer.py +1 -1
- diffusers/pipelines/stable_cascade/__init__.py +50 -0
- diffusers/pipelines/stable_cascade/pipeline_stable_cascade.py +482 -0
- diffusers/pipelines/stable_cascade/pipeline_stable_cascade_combined.py +311 -0
- diffusers/pipelines/stable_cascade/pipeline_stable_cascade_prior.py +638 -0
- diffusers/pipelines/stable_diffusion/clip_image_project_model.py +1 -1
- diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +4 -1
- diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion.py +1 -1
- diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion_img2img.py +2 -2
- diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion_inpaint.py +1 -1
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion.py +1 -1
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_img2img.py +1 -1
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_inpaint.py +1 -1
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_upscale.py +1 -1
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +90 -146
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_depth2img.py +5 -4
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_image_variation.py +4 -32
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +92 -119
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +92 -119
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +13 -59
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_latent_upscale.py +3 -31
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +5 -33
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip.py +5 -21
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip_img2img.py +7 -21
- diffusers/pipelines/stable_diffusion/safety_checker.py +1 -1
- diffusers/pipelines/stable_diffusion/safety_checker_flax.py +1 -1
- diffusers/pipelines/stable_diffusion/stable_unclip_image_normalizer.py +1 -1
- diffusers/pipelines/stable_diffusion_attend_and_excite/pipeline_stable_diffusion_attend_and_excite.py +5 -21
- diffusers/pipelines/stable_diffusion_diffedit/pipeline_stable_diffusion_diffedit.py +9 -38
- diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen.py +5 -34
- diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py +6 -35
- diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_k_diffusion.py +7 -6
- diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_xl_k_diffusion.py +4 -124
- diffusers/pipelines/stable_diffusion_ldm3d/pipeline_stable_diffusion_ldm3d.py +282 -80
- diffusers/pipelines/stable_diffusion_panorama/pipeline_stable_diffusion_panorama.py +94 -46
- diffusers/pipelines/stable_diffusion_safe/pipeline_stable_diffusion_safe.py +3 -3
- diffusers/pipelines/stable_diffusion_safe/safety_checker.py +1 -1
- diffusers/pipelines/stable_diffusion_sag/pipeline_stable_diffusion_sag.py +6 -22
- diffusers/pipelines/stable_diffusion_xl/pipeline_flax_stable_diffusion_xl.py +1 -1
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +96 -148
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +98 -154
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +98 -153
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +25 -87
- diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +89 -80
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_adapter.py +5 -49
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +80 -88
- diffusers/pipelines/text_to_video_synthesis/pipeline_output.py +8 -6
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +15 -86
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +20 -93
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +5 -5
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +3 -19
- diffusers/pipelines/unclip/pipeline_unclip.py +1 -1
- diffusers/pipelines/unclip/pipeline_unclip_image_variation.py +1 -1
- diffusers/pipelines/unclip/text_proj.py +1 -1
- diffusers/pipelines/unidiffuser/pipeline_unidiffuser.py +35 -35
- diffusers/pipelines/wuerstchen/modeling_paella_vq_model.py +1 -1
- diffusers/pipelines/wuerstchen/modeling_wuerstchen_common.py +4 -21
- diffusers/pipelines/wuerstchen/modeling_wuerstchen_diffnext.py +2 -2
- diffusers/pipelines/wuerstchen/modeling_wuerstchen_prior.py +4 -5
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen.py +8 -8
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen_combined.py +1 -1
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen_prior.py +2 -2
- diffusers/schedulers/__init__.py +7 -1
- diffusers/schedulers/deprecated/scheduling_karras_ve.py +1 -1
- diffusers/schedulers/deprecated/scheduling_sde_vp.py +1 -1
- diffusers/schedulers/scheduling_consistency_models.py +42 -19
- diffusers/schedulers/scheduling_ddim.py +2 -4
- diffusers/schedulers/scheduling_ddim_flax.py +13 -5
- diffusers/schedulers/scheduling_ddim_inverse.py +2 -4
- diffusers/schedulers/scheduling_ddim_parallel.py +2 -4
- diffusers/schedulers/scheduling_ddpm.py +2 -4
- diffusers/schedulers/scheduling_ddpm_flax.py +1 -1
- diffusers/schedulers/scheduling_ddpm_parallel.py +2 -4
- diffusers/schedulers/scheduling_ddpm_wuerstchen.py +1 -1
- diffusers/schedulers/scheduling_deis_multistep.py +46 -19
- diffusers/schedulers/scheduling_dpmsolver_multistep.py +107 -21
- diffusers/schedulers/scheduling_dpmsolver_multistep_flax.py +1 -1
- diffusers/schedulers/scheduling_dpmsolver_multistep_inverse.py +9 -7
- diffusers/schedulers/scheduling_dpmsolver_sde.py +35 -35
- diffusers/schedulers/scheduling_dpmsolver_singlestep.py +52 -21
- diffusers/schedulers/scheduling_edm_dpmsolver_multistep.py +683 -0
- diffusers/schedulers/scheduling_edm_euler.py +381 -0
- diffusers/schedulers/scheduling_euler_ancestral_discrete.py +43 -15
- diffusers/schedulers/scheduling_euler_discrete.py +42 -17
- diffusers/schedulers/scheduling_euler_discrete_flax.py +1 -1
- diffusers/schedulers/scheduling_heun_discrete.py +35 -35
- diffusers/schedulers/scheduling_ipndm.py +37 -11
- diffusers/schedulers/scheduling_k_dpm_2_ancestral_discrete.py +44 -44
- diffusers/schedulers/scheduling_k_dpm_2_discrete.py +44 -44
- diffusers/schedulers/scheduling_karras_ve_flax.py +1 -1
- diffusers/schedulers/scheduling_lcm.py +38 -14
- diffusers/schedulers/scheduling_lms_discrete.py +43 -15
- diffusers/schedulers/scheduling_lms_discrete_flax.py +1 -1
- diffusers/schedulers/scheduling_pndm.py +2 -4
- diffusers/schedulers/scheduling_pndm_flax.py +2 -4
- diffusers/schedulers/scheduling_repaint.py +1 -1
- diffusers/schedulers/scheduling_sasolver.py +41 -9
- diffusers/schedulers/scheduling_sde_ve.py +1 -1
- diffusers/schedulers/scheduling_sde_ve_flax.py +1 -1
- diffusers/schedulers/scheduling_tcd.py +686 -0
- diffusers/schedulers/scheduling_unclip.py +1 -1
- diffusers/schedulers/scheduling_unipc_multistep.py +46 -19
- diffusers/schedulers/scheduling_utils.py +2 -1
- diffusers/schedulers/scheduling_utils_flax.py +1 -1
- diffusers/schedulers/scheduling_vq_diffusion.py +1 -1
- diffusers/training_utils.py +9 -2
- diffusers/utils/__init__.py +2 -1
- diffusers/utils/accelerate_utils.py +1 -1
- diffusers/utils/constants.py +1 -1
- diffusers/utils/doc_utils.py +1 -1
- diffusers/utils/dummy_pt_objects.py +60 -0
- diffusers/utils/dummy_torch_and_transformers_objects.py +75 -0
- diffusers/utils/dynamic_modules_utils.py +1 -1
- diffusers/utils/export_utils.py +3 -3
- diffusers/utils/hub_utils.py +60 -16
- diffusers/utils/import_utils.py +15 -1
- diffusers/utils/loading_utils.py +2 -0
- diffusers/utils/logging.py +1 -1
- diffusers/utils/model_card_template.md +24 -0
- diffusers/utils/outputs.py +14 -7
- diffusers/utils/peft_utils.py +1 -1
- diffusers/utils/state_dict_utils.py +1 -1
- diffusers/utils/testing_utils.py +2 -0
- diffusers/utils/torch_utils.py +1 -1
- {diffusers-0.26.2.dist-info → diffusers-0.27.0.dist-info}/METADATA +5 -5
- diffusers-0.27.0.dist-info/RECORD +399 -0
- diffusers-0.26.2.dist-info/RECORD +0 -384
- {diffusers-0.26.2.dist-info → diffusers-0.27.0.dist-info}/LICENSE +0 -0
- {diffusers-0.26.2.dist-info → diffusers-0.27.0.dist-info}/WHEEL +0 -0
- {diffusers-0.26.2.dist-info → diffusers-0.27.0.dist-info}/entry_points.txt +0 -0
- {diffusers-0.26.2.dist-info → diffusers-0.27.0.dist-info}/top_level.txt +0 -0
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright
|
1
|
+
# Copyright 2024 The Intel Labs Team Authors and the HuggingFace Team. All rights reserved.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -36,7 +36,7 @@ from ...utils import (
|
|
36
36
|
unscale_lora_layers,
|
37
37
|
)
|
38
38
|
from ...utils.torch_utils import randn_tensor
|
39
|
-
from ..pipeline_utils import DiffusionPipeline
|
39
|
+
from ..pipeline_utils import DiffusionPipeline, StableDiffusionMixin
|
40
40
|
from ..stable_diffusion.safety_checker import StableDiffusionSafetyChecker
|
41
41
|
|
42
42
|
|
@@ -59,6 +59,66 @@ EXAMPLE_DOC_STRING = """
|
|
59
59
|
"""
|
60
60
|
|
61
61
|
|
62
|
+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.rescale_noise_cfg
|
63
|
+
def rescale_noise_cfg(noise_cfg, noise_pred_text, guidance_rescale=0.0):
|
64
|
+
"""
|
65
|
+
Rescale `noise_cfg` according to `guidance_rescale`. Based on findings of [Common Diffusion Noise Schedules and
|
66
|
+
Sample Steps are Flawed](https://arxiv.org/pdf/2305.08891.pdf). See Section 3.4
|
67
|
+
"""
|
68
|
+
std_text = noise_pred_text.std(dim=list(range(1, noise_pred_text.ndim)), keepdim=True)
|
69
|
+
std_cfg = noise_cfg.std(dim=list(range(1, noise_cfg.ndim)), keepdim=True)
|
70
|
+
# rescale the results from guidance (fixes overexposure)
|
71
|
+
noise_pred_rescaled = noise_cfg * (std_text / std_cfg)
|
72
|
+
# mix with the original results from guidance by factor guidance_rescale to avoid "plain looking" images
|
73
|
+
noise_cfg = guidance_rescale * noise_pred_rescaled + (1 - guidance_rescale) * noise_cfg
|
74
|
+
return noise_cfg
|
75
|
+
|
76
|
+
|
77
|
+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps
|
78
|
+
def retrieve_timesteps(
|
79
|
+
scheduler,
|
80
|
+
num_inference_steps: Optional[int] = None,
|
81
|
+
device: Optional[Union[str, torch.device]] = None,
|
82
|
+
timesteps: Optional[List[int]] = None,
|
83
|
+
**kwargs,
|
84
|
+
):
|
85
|
+
"""
|
86
|
+
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
|
87
|
+
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.
|
88
|
+
|
89
|
+
Args:
|
90
|
+
scheduler (`SchedulerMixin`):
|
91
|
+
The scheduler to get timesteps from.
|
92
|
+
num_inference_steps (`int`):
|
93
|
+
The number of diffusion steps used when generating samples with a pre-trained model. If used,
|
94
|
+
`timesteps` must be `None`.
|
95
|
+
device (`str` or `torch.device`, *optional*):
|
96
|
+
The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
|
97
|
+
timesteps (`List[int]`, *optional*):
|
98
|
+
Custom timesteps used to support arbitrary spacing between timesteps. If `None`, then the default
|
99
|
+
timestep spacing strategy of the scheduler is used. If `timesteps` is passed, `num_inference_steps`
|
100
|
+
must be `None`.
|
101
|
+
|
102
|
+
Returns:
|
103
|
+
`Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
|
104
|
+
second element is the number of inference steps.
|
105
|
+
"""
|
106
|
+
if timesteps is not None:
|
107
|
+
accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
|
108
|
+
if not accepts_timesteps:
|
109
|
+
raise ValueError(
|
110
|
+
f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
|
111
|
+
f" timestep schedules. Please check whether you are using the correct scheduler."
|
112
|
+
)
|
113
|
+
scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
|
114
|
+
timesteps = scheduler.timesteps
|
115
|
+
num_inference_steps = len(timesteps)
|
116
|
+
else:
|
117
|
+
scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
|
118
|
+
timesteps = scheduler.timesteps
|
119
|
+
return timesteps, num_inference_steps
|
120
|
+
|
121
|
+
|
62
122
|
@dataclass
|
63
123
|
class LDM3DPipelineOutput(BaseOutput):
|
64
124
|
"""
|
@@ -82,7 +142,12 @@ class LDM3DPipelineOutput(BaseOutput):
|
|
82
142
|
|
83
143
|
|
84
144
|
class StableDiffusionLDM3DPipeline(
|
85
|
-
DiffusionPipeline,
|
145
|
+
DiffusionPipeline,
|
146
|
+
StableDiffusionMixin,
|
147
|
+
TextualInversionLoaderMixin,
|
148
|
+
IPAdapterMixin,
|
149
|
+
LoraLoaderMixin,
|
150
|
+
FromSingleFileMixin,
|
86
151
|
):
|
87
152
|
r"""
|
88
153
|
Pipeline for text-to-image and 3D generation using LDM3D.
|
@@ -120,6 +185,7 @@ class StableDiffusionLDM3DPipeline(
|
|
120
185
|
model_cpu_offload_seq = "text_encoder->unet->vae"
|
121
186
|
_optional_components = ["safety_checker", "feature_extractor", "image_encoder"]
|
122
187
|
_exclude_from_cpu_offload = ["safety_checker"]
|
188
|
+
_callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"]
|
123
189
|
|
124
190
|
def __init__(
|
125
191
|
self,
|
@@ -165,39 +231,6 @@ class StableDiffusionLDM3DPipeline(
|
|
165
231
|
self.image_processor = VaeImageProcessorLDM3D(vae_scale_factor=self.vae_scale_factor)
|
166
232
|
self.register_to_config(requires_safety_checker=requires_safety_checker)
|
167
233
|
|
168
|
-
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.enable_vae_slicing
|
169
|
-
def enable_vae_slicing(self):
|
170
|
-
r"""
|
171
|
-
Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to
|
172
|
-
compute decoding in several steps. This is useful to save some memory and allow larger batch sizes.
|
173
|
-
"""
|
174
|
-
self.vae.enable_slicing()
|
175
|
-
|
176
|
-
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.disable_vae_slicing
|
177
|
-
def disable_vae_slicing(self):
|
178
|
-
r"""
|
179
|
-
Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to
|
180
|
-
computing decoding in one step.
|
181
|
-
"""
|
182
|
-
self.vae.disable_slicing()
|
183
|
-
|
184
|
-
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.enable_vae_tiling
|
185
|
-
def enable_vae_tiling(self):
|
186
|
-
r"""
|
187
|
-
Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to
|
188
|
-
compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow
|
189
|
-
processing larger images.
|
190
|
-
"""
|
191
|
-
self.vae.enable_tiling()
|
192
|
-
|
193
|
-
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.disable_vae_tiling
|
194
|
-
def disable_vae_tiling(self):
|
195
|
-
r"""
|
196
|
-
Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to
|
197
|
-
computing decoding in one step.
|
198
|
-
"""
|
199
|
-
self.vae.disable_tiling()
|
200
|
-
|
201
234
|
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._encode_prompt
|
202
235
|
def _encode_prompt(
|
203
236
|
self,
|
@@ -292,7 +325,7 @@ class StableDiffusionLDM3DPipeline(
|
|
292
325
|
batch_size = prompt_embeds.shape[0]
|
293
326
|
|
294
327
|
if prompt_embeds is None:
|
295
|
-
# textual inversion:
|
328
|
+
# textual inversion: process multi-vector tokens if necessary
|
296
329
|
if isinstance(self, TextualInversionLoaderMixin):
|
297
330
|
prompt = self.maybe_convert_prompt(prompt, self.tokenizer)
|
298
331
|
|
@@ -374,7 +407,7 @@ class StableDiffusionLDM3DPipeline(
|
|
374
407
|
else:
|
375
408
|
uncond_tokens = negative_prompt
|
376
409
|
|
377
|
-
# textual inversion:
|
410
|
+
# textual inversion: process multi-vector tokens if necessary
|
378
411
|
if isinstance(self, TextualInversionLoaderMixin):
|
379
412
|
uncond_tokens = self.maybe_convert_prompt(uncond_tokens, self.tokenizer)
|
380
413
|
|
@@ -439,31 +472,54 @@ class StableDiffusionLDM3DPipeline(
|
|
439
472
|
return image_embeds, uncond_image_embeds
|
440
473
|
|
441
474
|
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_ip_adapter_image_embeds
|
442
|
-
def prepare_ip_adapter_image_embeds(
|
443
|
-
|
444
|
-
|
475
|
+
def prepare_ip_adapter_image_embeds(
|
476
|
+
self, ip_adapter_image, ip_adapter_image_embeds, device, num_images_per_prompt, do_classifier_free_guidance
|
477
|
+
):
|
478
|
+
if ip_adapter_image_embeds is None:
|
479
|
+
if not isinstance(ip_adapter_image, list):
|
480
|
+
ip_adapter_image = [ip_adapter_image]
|
445
481
|
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
482
|
+
if len(ip_adapter_image) != len(self.unet.encoder_hid_proj.image_projection_layers):
|
483
|
+
raise ValueError(
|
484
|
+
f"`ip_adapter_image` must have same length as the number of IP Adapters. Got {len(ip_adapter_image)} images and {len(self.unet.encoder_hid_proj.image_projection_layers)} IP Adapters."
|
485
|
+
)
|
450
486
|
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
487
|
+
image_embeds = []
|
488
|
+
for single_ip_adapter_image, image_proj_layer in zip(
|
489
|
+
ip_adapter_image, self.unet.encoder_hid_proj.image_projection_layers
|
490
|
+
):
|
491
|
+
output_hidden_state = not isinstance(image_proj_layer, ImageProjection)
|
492
|
+
single_image_embeds, single_negative_image_embeds = self.encode_image(
|
493
|
+
single_ip_adapter_image, device, 1, output_hidden_state
|
494
|
+
)
|
495
|
+
single_image_embeds = torch.stack([single_image_embeds] * num_images_per_prompt, dim=0)
|
496
|
+
single_negative_image_embeds = torch.stack(
|
497
|
+
[single_negative_image_embeds] * num_images_per_prompt, dim=0
|
498
|
+
)
|
461
499
|
|
462
|
-
|
463
|
-
|
464
|
-
|
500
|
+
if do_classifier_free_guidance:
|
501
|
+
single_image_embeds = torch.cat([single_negative_image_embeds, single_image_embeds])
|
502
|
+
single_image_embeds = single_image_embeds.to(device)
|
465
503
|
|
466
|
-
|
504
|
+
image_embeds.append(single_image_embeds)
|
505
|
+
else:
|
506
|
+
repeat_dims = [1]
|
507
|
+
image_embeds = []
|
508
|
+
for single_image_embeds in ip_adapter_image_embeds:
|
509
|
+
if do_classifier_free_guidance:
|
510
|
+
single_negative_image_embeds, single_image_embeds = single_image_embeds.chunk(2)
|
511
|
+
single_image_embeds = single_image_embeds.repeat(
|
512
|
+
num_images_per_prompt, *(repeat_dims * len(single_image_embeds.shape[1:]))
|
513
|
+
)
|
514
|
+
single_negative_image_embeds = single_negative_image_embeds.repeat(
|
515
|
+
num_images_per_prompt, *(repeat_dims * len(single_negative_image_embeds.shape[1:]))
|
516
|
+
)
|
517
|
+
single_image_embeds = torch.cat([single_negative_image_embeds, single_image_embeds])
|
518
|
+
else:
|
519
|
+
single_image_embeds = single_image_embeds.repeat(
|
520
|
+
num_images_per_prompt, *(repeat_dims * len(single_image_embeds.shape[1:]))
|
521
|
+
)
|
522
|
+
image_embeds.append(single_image_embeds)
|
467
523
|
|
468
524
|
return image_embeds
|
469
525
|
|
@@ -510,6 +566,8 @@ class StableDiffusionLDM3DPipeline(
|
|
510
566
|
negative_prompt=None,
|
511
567
|
prompt_embeds=None,
|
512
568
|
negative_prompt_embeds=None,
|
569
|
+
ip_adapter_image=None,
|
570
|
+
ip_adapter_image_embeds=None,
|
513
571
|
callback_on_step_end_tensor_inputs=None,
|
514
572
|
):
|
515
573
|
if height % 8 != 0 or width % 8 != 0:
|
@@ -553,6 +611,21 @@ class StableDiffusionLDM3DPipeline(
|
|
553
611
|
f" {negative_prompt_embeds.shape}."
|
554
612
|
)
|
555
613
|
|
614
|
+
if ip_adapter_image is not None and ip_adapter_image_embeds is not None:
|
615
|
+
raise ValueError(
|
616
|
+
"Provide either `ip_adapter_image` or `ip_adapter_image_embeds`. Cannot leave both `ip_adapter_image` and `ip_adapter_image_embeds` defined."
|
617
|
+
)
|
618
|
+
|
619
|
+
if ip_adapter_image_embeds is not None:
|
620
|
+
if not isinstance(ip_adapter_image_embeds, list):
|
621
|
+
raise ValueError(
|
622
|
+
f"`ip_adapter_image_embeds` has to be of type `list` but is {type(ip_adapter_image_embeds)}"
|
623
|
+
)
|
624
|
+
elif ip_adapter_image_embeds[0].ndim not in [3, 4]:
|
625
|
+
raise ValueError(
|
626
|
+
f"`ip_adapter_image_embeds` has to be a list of 3D or 4D tensors but is {ip_adapter_image_embeds[0].ndim}D"
|
627
|
+
)
|
628
|
+
|
556
629
|
def prepare_latents(self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None):
|
557
630
|
shape = (batch_size, num_channels_latents, height // self.vae_scale_factor, width // self.vae_scale_factor)
|
558
631
|
if isinstance(generator, list) and len(generator) != batch_size:
|
@@ -570,6 +643,66 @@ class StableDiffusionLDM3DPipeline(
|
|
570
643
|
latents = latents * self.scheduler.init_noise_sigma
|
571
644
|
return latents
|
572
645
|
|
646
|
+
# Copied from diffusers.pipelines.latent_consistency_models.pipeline_latent_consistency_text2img.LatentConsistencyModelPipeline.get_guidance_scale_embedding
|
647
|
+
def get_guidance_scale_embedding(self, w, embedding_dim=512, dtype=torch.float32):
|
648
|
+
"""
|
649
|
+
See https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298
|
650
|
+
|
651
|
+
Args:
|
652
|
+
timesteps (`torch.Tensor`):
|
653
|
+
generate embedding vectors at these timesteps
|
654
|
+
embedding_dim (`int`, *optional*, defaults to 512):
|
655
|
+
dimension of the embeddings to generate
|
656
|
+
dtype:
|
657
|
+
data type of the generated embeddings
|
658
|
+
|
659
|
+
Returns:
|
660
|
+
`torch.FloatTensor`: Embedding vectors with shape `(len(timesteps), embedding_dim)`
|
661
|
+
"""
|
662
|
+
assert len(w.shape) == 1
|
663
|
+
w = w * 1000.0
|
664
|
+
|
665
|
+
half_dim = embedding_dim // 2
|
666
|
+
emb = torch.log(torch.tensor(10000.0)) / (half_dim - 1)
|
667
|
+
emb = torch.exp(torch.arange(half_dim, dtype=dtype) * -emb)
|
668
|
+
emb = w.to(dtype)[:, None] * emb[None, :]
|
669
|
+
emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
|
670
|
+
if embedding_dim % 2 == 1: # zero pad
|
671
|
+
emb = torch.nn.functional.pad(emb, (0, 1))
|
672
|
+
assert emb.shape == (w.shape[0], embedding_dim)
|
673
|
+
return emb
|
674
|
+
|
675
|
+
@property
|
676
|
+
def guidance_scale(self):
|
677
|
+
return self._guidance_scale
|
678
|
+
|
679
|
+
@property
|
680
|
+
def guidance_rescale(self):
|
681
|
+
return self._guidance_rescale
|
682
|
+
|
683
|
+
@property
|
684
|
+
def clip_skip(self):
|
685
|
+
return self._clip_skip
|
686
|
+
|
687
|
+
# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
|
688
|
+
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
|
689
|
+
# corresponds to doing no classifier free guidance.
|
690
|
+
@property
|
691
|
+
def do_classifier_free_guidance(self):
|
692
|
+
return self._guidance_scale > 1 and self.unet.config.time_cond_proj_dim is None
|
693
|
+
|
694
|
+
@property
|
695
|
+
def cross_attention_kwargs(self):
|
696
|
+
return self._cross_attention_kwargs
|
697
|
+
|
698
|
+
@property
|
699
|
+
def num_timesteps(self):
|
700
|
+
return self._num_timesteps
|
701
|
+
|
702
|
+
@property
|
703
|
+
def interrupt(self):
|
704
|
+
return self._interrupt
|
705
|
+
|
573
706
|
@torch.no_grad()
|
574
707
|
@replace_example_docstring(EXAMPLE_DOC_STRING)
|
575
708
|
def __call__(
|
@@ -578,6 +711,7 @@ class StableDiffusionLDM3DPipeline(
|
|
578
711
|
height: Optional[int] = None,
|
579
712
|
width: Optional[int] = None,
|
580
713
|
num_inference_steps: int = 49,
|
714
|
+
timesteps: List[int] = None,
|
581
715
|
guidance_scale: float = 5.0,
|
582
716
|
negative_prompt: Optional[Union[str, List[str]]] = None,
|
583
717
|
num_images_per_prompt: Optional[int] = 1,
|
@@ -587,12 +721,15 @@ class StableDiffusionLDM3DPipeline(
|
|
587
721
|
prompt_embeds: Optional[torch.FloatTensor] = None,
|
588
722
|
negative_prompt_embeds: Optional[torch.FloatTensor] = None,
|
589
723
|
ip_adapter_image: Optional[PipelineImageInput] = None,
|
724
|
+
ip_adapter_image_embeds: Optional[List[torch.FloatTensor]] = None,
|
590
725
|
output_type: Optional[str] = "pil",
|
591
726
|
return_dict: bool = True,
|
592
|
-
callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
|
593
|
-
callback_steps: int = 1,
|
594
727
|
cross_attention_kwargs: Optional[Dict[str, Any]] = None,
|
728
|
+
guidance_rescale: float = 0.0,
|
595
729
|
clip_skip: Optional[int] = None,
|
730
|
+
callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
|
731
|
+
callback_on_step_end_tensor_inputs: List[str] = ["latents"],
|
732
|
+
**kwargs,
|
596
733
|
):
|
597
734
|
r"""
|
598
735
|
The call function to the pipeline for generation.
|
@@ -633,23 +770,31 @@ class StableDiffusionLDM3DPipeline(
|
|
633
770
|
not provided, `negative_prompt_embeds` are generated from the `negative_prompt` input argument.
|
634
771
|
ip_adapter_image: (`PipelineImageInput`, *optional*):
|
635
772
|
Optional image input to work with IP Adapters.
|
773
|
+
ip_adapter_image_embeds (`List[torch.FloatTensor]`, *optional*):
|
774
|
+
Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of IP-adapters.
|
775
|
+
Each element should be a tensor of shape `(batch_size, num_images, emb_dim)`. It should contain the negative image embedding
|
776
|
+
if `do_classifier_free_guidance` is set to `True`.
|
777
|
+
If not provided, embeddings are computed from the `ip_adapter_image` input argument.
|
636
778
|
output_type (`str`, *optional*, defaults to `"pil"`):
|
637
779
|
The output format of the generated image. Choose between `PIL.Image` or `np.array`.
|
638
780
|
return_dict (`bool`, *optional*, defaults to `True`):
|
639
781
|
Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
|
640
782
|
plain tuple.
|
641
|
-
callback (`Callable`, *optional*):
|
642
|
-
A function that calls every `callback_steps` steps during inference. The function is called with the
|
643
|
-
following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
|
644
|
-
callback_steps (`int`, *optional*, defaults to 1):
|
645
|
-
The frequency at which the `callback` function is called. If not specified, the callback is called at
|
646
|
-
every step.
|
647
783
|
cross_attention_kwargs (`dict`, *optional*):
|
648
784
|
A kwargs dictionary that if specified is passed along to the [`AttentionProcessor`] as defined in
|
649
785
|
[`self.processor`](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
|
650
786
|
clip_skip (`int`, *optional*):
|
651
787
|
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
|
652
788
|
the output of the pre-final layer will be used for computing the prompt embeddings.
|
789
|
+
callback_on_step_end (`Callable`, *optional*):
|
790
|
+
A function that calls at the end of each denoising steps during the inference. The function is called
|
791
|
+
with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
|
792
|
+
callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
|
793
|
+
`callback_on_step_end_tensor_inputs`.
|
794
|
+
callback_on_step_end_tensor_inputs (`List`, *optional*):
|
795
|
+
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
|
796
|
+
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
|
797
|
+
`._callback_tensor_inputs` attribute of your pipeline class.
|
653
798
|
Examples:
|
654
799
|
|
655
800
|
Returns:
|
@@ -659,15 +804,46 @@ class StableDiffusionLDM3DPipeline(
|
|
659
804
|
second element is a list of `bool`s indicating whether the corresponding generated image contains
|
660
805
|
"not-safe-for-work" (nsfw) content.
|
661
806
|
"""
|
807
|
+
callback = kwargs.pop("callback", None)
|
808
|
+
callback_steps = kwargs.pop("callback_steps", None)
|
809
|
+
|
810
|
+
if callback is not None:
|
811
|
+
deprecate(
|
812
|
+
"callback",
|
813
|
+
"1.0.0",
|
814
|
+
"Passing `callback` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
|
815
|
+
)
|
816
|
+
if callback_steps is not None:
|
817
|
+
deprecate(
|
818
|
+
"callback_steps",
|
819
|
+
"1.0.0",
|
820
|
+
"Passing `callback_steps` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
|
821
|
+
)
|
822
|
+
|
662
823
|
# 0. Default height and width to unet
|
663
824
|
height = height or self.unet.config.sample_size * self.vae_scale_factor
|
664
825
|
width = width or self.unet.config.sample_size * self.vae_scale_factor
|
665
826
|
|
666
827
|
# 1. Check inputs. Raise error if not correct
|
667
828
|
self.check_inputs(
|
668
|
-
prompt,
|
829
|
+
prompt,
|
830
|
+
height,
|
831
|
+
width,
|
832
|
+
callback_steps,
|
833
|
+
negative_prompt,
|
834
|
+
prompt_embeds,
|
835
|
+
negative_prompt_embeds,
|
836
|
+
ip_adapter_image,
|
837
|
+
ip_adapter_image_embeds,
|
838
|
+
callback_on_step_end_tensor_inputs,
|
669
839
|
)
|
670
840
|
|
841
|
+
self._guidance_scale = guidance_scale
|
842
|
+
self._guidance_rescale = guidance_rescale
|
843
|
+
self._clip_skip = clip_skip
|
844
|
+
self._cross_attention_kwargs = cross_attention_kwargs
|
845
|
+
self._interrupt = False
|
846
|
+
|
671
847
|
# 2. Define call parameters
|
672
848
|
if prompt is not None and isinstance(prompt, str):
|
673
849
|
batch_size = 1
|
@@ -677,14 +853,14 @@ class StableDiffusionLDM3DPipeline(
|
|
677
853
|
batch_size = prompt_embeds.shape[0]
|
678
854
|
|
679
855
|
device = self._execution_device
|
680
|
-
# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
|
681
|
-
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
|
682
|
-
# corresponds to doing no classifier free guidance.
|
683
|
-
do_classifier_free_guidance = guidance_scale > 1.0
|
684
856
|
|
685
|
-
if ip_adapter_image is not None:
|
857
|
+
if ip_adapter_image is not None or ip_adapter_image_embeds is not None:
|
686
858
|
image_embeds = self.prepare_ip_adapter_image_embeds(
|
687
|
-
ip_adapter_image,
|
859
|
+
ip_adapter_image,
|
860
|
+
ip_adapter_image_embeds,
|
861
|
+
device,
|
862
|
+
batch_size * num_images_per_prompt,
|
863
|
+
self.do_classifier_free_guidance,
|
688
864
|
)
|
689
865
|
|
690
866
|
# 3. Encode input prompt
|
@@ -692,7 +868,7 @@ class StableDiffusionLDM3DPipeline(
|
|
692
868
|
prompt,
|
693
869
|
device,
|
694
870
|
num_images_per_prompt,
|
695
|
-
do_classifier_free_guidance,
|
871
|
+
self.do_classifier_free_guidance,
|
696
872
|
negative_prompt,
|
697
873
|
prompt_embeds=prompt_embeds,
|
698
874
|
negative_prompt_embeds=negative_prompt_embeds,
|
@@ -701,12 +877,11 @@ class StableDiffusionLDM3DPipeline(
|
|
701
877
|
# For classifier free guidance, we need to do two forward passes.
|
702
878
|
# Here we concatenate the unconditional and text embeddings into a single batch
|
703
879
|
# to avoid doing two forward passes
|
704
|
-
if do_classifier_free_guidance:
|
880
|
+
if self.do_classifier_free_guidance:
|
705
881
|
prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds])
|
706
882
|
|
707
883
|
# 4. Prepare timesteps
|
708
|
-
self.scheduler
|
709
|
-
timesteps = self.scheduler.timesteps
|
884
|
+
timesteps, num_inference_steps = retrieve_timesteps(self.scheduler, num_inference_steps, device, timesteps)
|
710
885
|
|
711
886
|
# 5. Prepare latent variables
|
712
887
|
num_channels_latents = self.unet.config.in_channels
|
@@ -727,12 +902,24 @@ class StableDiffusionLDM3DPipeline(
|
|
727
902
|
# 6.1 Add image embeds for IP-Adapter
|
728
903
|
added_cond_kwargs = {"image_embeds": image_embeds} if ip_adapter_image is not None else None
|
729
904
|
|
905
|
+
# 6.2 Optionally get Guidance Scale Embedding
|
906
|
+
timestep_cond = None
|
907
|
+
if self.unet.config.time_cond_proj_dim is not None:
|
908
|
+
guidance_scale_tensor = torch.tensor(self.guidance_scale - 1).repeat(batch_size * num_images_per_prompt)
|
909
|
+
timestep_cond = self.get_guidance_scale_embedding(
|
910
|
+
guidance_scale_tensor, embedding_dim=self.unet.config.time_cond_proj_dim
|
911
|
+
).to(device=device, dtype=latents.dtype)
|
912
|
+
|
730
913
|
# 7. Denoising loop
|
731
914
|
num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order
|
915
|
+
self._num_timesteps = len(timesteps)
|
732
916
|
with self.progress_bar(total=num_inference_steps) as progress_bar:
|
733
917
|
for i, t in enumerate(timesteps):
|
918
|
+
if self.interrupt:
|
919
|
+
continue
|
920
|
+
|
734
921
|
# expand the latents if we are doing classifier free guidance
|
735
|
-
latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
|
922
|
+
latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance else latents
|
736
923
|
latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
|
737
924
|
|
738
925
|
# predict the noise residual
|
@@ -740,19 +927,34 @@ class StableDiffusionLDM3DPipeline(
|
|
740
927
|
latent_model_input,
|
741
928
|
t,
|
742
929
|
encoder_hidden_states=prompt_embeds,
|
930
|
+
timestep_cond=timestep_cond,
|
743
931
|
cross_attention_kwargs=cross_attention_kwargs,
|
744
932
|
added_cond_kwargs=added_cond_kwargs,
|
745
933
|
return_dict=False,
|
746
934
|
)[0]
|
747
935
|
|
748
936
|
# perform guidance
|
749
|
-
if do_classifier_free_guidance:
|
937
|
+
if self.do_classifier_free_guidance:
|
750
938
|
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
|
751
939
|
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
|
752
940
|
|
941
|
+
if self.do_classifier_free_guidance and self.guidance_rescale > 0.0:
|
942
|
+
# Based on 3.4. in https://arxiv.org/pdf/2305.08891.pdf
|
943
|
+
noise_pred = rescale_noise_cfg(noise_pred, noise_pred_text, guidance_rescale=self.guidance_rescale)
|
944
|
+
|
753
945
|
# compute the previous noisy sample x_t -> x_t-1
|
754
946
|
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
|
755
947
|
|
948
|
+
if callback_on_step_end is not None:
|
949
|
+
callback_kwargs = {}
|
950
|
+
for k in callback_on_step_end_tensor_inputs:
|
951
|
+
callback_kwargs[k] = locals()[k]
|
952
|
+
callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
|
953
|
+
|
954
|
+
latents = callback_outputs.pop("latents", latents)
|
955
|
+
prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
|
956
|
+
negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds)
|
957
|
+
|
756
958
|
# call the callback, if provided
|
757
959
|
if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
|
758
960
|
progress_bar.update()
|