diffusers 0.23.1__py3-none-any.whl → 0.25.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 +26 -2
- diffusers/commands/fp16_safetensors.py +10 -11
- diffusers/configuration_utils.py +13 -8
- diffusers/dependency_versions_check.py +0 -1
- diffusers/dependency_versions_table.py +5 -5
- diffusers/experimental/rl/value_guided_sampling.py +1 -1
- diffusers/image_processor.py +463 -51
- diffusers/loaders/__init__.py +82 -0
- diffusers/loaders/ip_adapter.py +159 -0
- diffusers/loaders/lora.py +1553 -0
- diffusers/loaders/lora_conversion_utils.py +284 -0
- diffusers/loaders/single_file.py +637 -0
- diffusers/loaders/textual_inversion.py +455 -0
- diffusers/loaders/unet.py +828 -0
- diffusers/loaders/utils.py +59 -0
- diffusers/models/__init__.py +26 -9
- diffusers/models/activations.py +9 -6
- diffusers/models/attention.py +301 -29
- diffusers/models/attention_flax.py +9 -1
- diffusers/models/attention_processor.py +378 -6
- diffusers/models/autoencoders/__init__.py +5 -0
- diffusers/models/{autoencoder_asym_kl.py → autoencoders/autoencoder_asym_kl.py} +17 -12
- diffusers/models/{autoencoder_kl.py → autoencoders/autoencoder_kl.py} +47 -23
- diffusers/models/autoencoders/autoencoder_kl_temporal_decoder.py +402 -0
- diffusers/models/{autoencoder_tiny.py → autoencoders/autoencoder_tiny.py} +24 -28
- diffusers/models/{consistency_decoder_vae.py → autoencoders/consistency_decoder_vae.py} +51 -44
- diffusers/models/{vae.py → autoencoders/vae.py} +71 -17
- diffusers/models/controlnet.py +59 -39
- diffusers/models/controlnet_flax.py +19 -18
- diffusers/models/downsampling.py +338 -0
- diffusers/models/embeddings.py +112 -29
- diffusers/models/embeddings_flax.py +2 -0
- diffusers/models/lora.py +131 -1
- diffusers/models/modeling_flax_utils.py +14 -8
- diffusers/models/modeling_outputs.py +17 -0
- diffusers/models/modeling_utils.py +37 -29
- diffusers/models/normalization.py +110 -4
- diffusers/models/resnet.py +299 -652
- diffusers/models/transformer_2d.py +22 -5
- diffusers/models/transformer_temporal.py +183 -1
- diffusers/models/unet_2d_blocks_flax.py +5 -0
- diffusers/models/unet_2d_condition.py +46 -0
- diffusers/models/unet_2d_condition_flax.py +13 -13
- diffusers/models/unet_3d_blocks.py +957 -173
- diffusers/models/unet_3d_condition.py +16 -8
- diffusers/models/unet_kandinsky3.py +535 -0
- diffusers/models/unet_motion_model.py +48 -33
- diffusers/models/unet_spatio_temporal_condition.py +489 -0
- diffusers/models/upsampling.py +454 -0
- diffusers/models/uvit_2d.py +471 -0
- diffusers/models/vae_flax.py +7 -0
- diffusers/models/vq_model.py +12 -3
- diffusers/optimization.py +16 -9
- diffusers/pipelines/__init__.py +137 -76
- diffusers/pipelines/amused/__init__.py +62 -0
- diffusers/pipelines/amused/pipeline_amused.py +328 -0
- diffusers/pipelines/amused/pipeline_amused_img2img.py +347 -0
- diffusers/pipelines/amused/pipeline_amused_inpaint.py +378 -0
- diffusers/pipelines/animatediff/pipeline_animatediff.py +66 -8
- diffusers/pipelines/audioldm/pipeline_audioldm.py +1 -0
- diffusers/pipelines/auto_pipeline.py +23 -13
- diffusers/pipelines/consistency_models/pipeline_consistency_models.py +1 -0
- diffusers/pipelines/controlnet/pipeline_controlnet.py +238 -35
- diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +148 -37
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +155 -41
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +123 -43
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +216 -39
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +106 -34
- diffusers/pipelines/dance_diffusion/pipeline_dance_diffusion.py +1 -0
- diffusers/pipelines/ddim/pipeline_ddim.py +1 -0
- diffusers/pipelines/ddpm/pipeline_ddpm.py +1 -0
- diffusers/pipelines/deepfloyd_if/pipeline_if.py +13 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img.py +13 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img_superresolution.py +13 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.py +13 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting_superresolution.py +13 -1
- diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.py +13 -1
- diffusers/pipelines/deprecated/__init__.py +153 -0
- diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/__init__.py +3 -3
- diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/pipeline_alt_diffusion.py +177 -34
- diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/pipeline_alt_diffusion_img2img.py +182 -37
- diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/pipeline_output.py +1 -1
- diffusers/pipelines/{audio_diffusion → deprecated/audio_diffusion}/__init__.py +1 -1
- diffusers/pipelines/{audio_diffusion → deprecated/audio_diffusion}/mel.py +2 -2
- diffusers/pipelines/{audio_diffusion → deprecated/audio_diffusion}/pipeline_audio_diffusion.py +4 -4
- diffusers/pipelines/{latent_diffusion_uncond → deprecated/latent_diffusion_uncond}/__init__.py +1 -1
- diffusers/pipelines/{latent_diffusion_uncond → deprecated/latent_diffusion_uncond}/pipeline_latent_diffusion_uncond.py +4 -4
- diffusers/pipelines/{pndm → deprecated/pndm}/__init__.py +1 -1
- diffusers/pipelines/{pndm → deprecated/pndm}/pipeline_pndm.py +4 -4
- diffusers/pipelines/{repaint → deprecated/repaint}/__init__.py +1 -1
- diffusers/pipelines/{repaint → deprecated/repaint}/pipeline_repaint.py +5 -5
- diffusers/pipelines/{score_sde_ve → deprecated/score_sde_ve}/__init__.py +1 -1
- diffusers/pipelines/{score_sde_ve → deprecated/score_sde_ve}/pipeline_score_sde_ve.py +5 -4
- diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/__init__.py +6 -6
- diffusers/pipelines/{spectrogram_diffusion/continous_encoder.py → deprecated/spectrogram_diffusion/continuous_encoder.py} +2 -2
- diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/midi_utils.py +1 -1
- diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/notes_encoder.py +2 -2
- diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/pipeline_spectrogram_diffusion.py +8 -7
- diffusers/pipelines/deprecated/stable_diffusion_variants/__init__.py +55 -0
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_cycle_diffusion.py +34 -13
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_onnx_stable_diffusion_inpaint_legacy.py +7 -6
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_inpaint_legacy.py +12 -11
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_model_editing.py +17 -11
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_paradigms.py +11 -10
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_pix2pix_zero.py +14 -13
- diffusers/pipelines/{stochastic_karras_ve → deprecated/stochastic_karras_ve}/__init__.py +1 -1
- diffusers/pipelines/{stochastic_karras_ve → deprecated/stochastic_karras_ve}/pipeline_stochastic_karras_ve.py +4 -4
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/__init__.py +3 -3
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/modeling_text_unet.py +83 -51
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion.py +4 -4
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion_dual_guided.py +7 -6
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion_image_variation.py +7 -6
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion_text_to_image.py +7 -6
- diffusers/pipelines/{vq_diffusion → deprecated/vq_diffusion}/__init__.py +3 -3
- diffusers/pipelines/{vq_diffusion → deprecated/vq_diffusion}/pipeline_vq_diffusion.py +5 -5
- diffusers/pipelines/dit/pipeline_dit.py +1 -0
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py +3 -3
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpainting.py +1 -1
- diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior.py +1 -1
- diffusers/pipelines/kandinsky3/__init__.py +49 -0
- diffusers/pipelines/kandinsky3/convert_kandinsky3_unet.py +98 -0
- diffusers/pipelines/kandinsky3/pipeline_kandinsky3.py +589 -0
- diffusers/pipelines/kandinsky3/pipeline_kandinsky3_img2img.py +654 -0
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +111 -11
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +102 -9
- diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py +1 -0
- diffusers/pipelines/musicldm/pipeline_musicldm.py +1 -1
- diffusers/pipelines/onnx_utils.py +8 -5
- diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py +7 -2
- diffusers/pipelines/pipeline_flax_utils.py +11 -8
- diffusers/pipelines/pipeline_utils.py +63 -42
- diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +247 -38
- diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py +3 -3
- diffusers/pipelines/stable_diffusion/__init__.py +37 -65
- diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +75 -78
- diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion.py +2 -2
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_img2img.py +2 -4
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_inpaint.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +174 -11
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_depth2img.py +8 -3
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_image_variation.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +178 -11
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +224 -13
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +74 -20
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_latent_upscale.py +4 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +7 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip.py +5 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip_img2img.py +5 -0
- diffusers/pipelines/stable_diffusion_attend_and_excite/__init__.py +48 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_attend_and_excite}/pipeline_stable_diffusion_attend_and_excite.py +6 -2
- diffusers/pipelines/stable_diffusion_diffedit/__init__.py +48 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_diffedit}/pipeline_stable_diffusion_diffedit.py +3 -3
- diffusers/pipelines/stable_diffusion_gligen/__init__.py +50 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_gligen}/pipeline_stable_diffusion_gligen.py +3 -2
- diffusers/pipelines/{stable_diffusion → stable_diffusion_gligen}/pipeline_stable_diffusion_gligen_text_image.py +4 -3
- diffusers/pipelines/stable_diffusion_k_diffusion/__init__.py +60 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_k_diffusion}/pipeline_stable_diffusion_k_diffusion.py +7 -1
- diffusers/pipelines/stable_diffusion_ldm3d/__init__.py +48 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_ldm3d}/pipeline_stable_diffusion_ldm3d.py +51 -7
- diffusers/pipelines/stable_diffusion_panorama/__init__.py +48 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_panorama}/pipeline_stable_diffusion_panorama.py +57 -8
- diffusers/pipelines/stable_diffusion_safe/pipeline_stable_diffusion_safe.py +58 -6
- diffusers/pipelines/stable_diffusion_sag/__init__.py +48 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_sag}/pipeline_stable_diffusion_sag.py +68 -10
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +194 -17
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +205 -16
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +206 -17
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +23 -17
- diffusers/pipelines/stable_video_diffusion/__init__.py +58 -0
- diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +652 -0
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_adapter.py +108 -12
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +115 -14
- diffusers/pipelines/text_to_video_synthesis/__init__.py +2 -0
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +6 -0
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +23 -3
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +334 -10
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +1331 -0
- diffusers/pipelines/unclip/pipeline_unclip.py +2 -1
- diffusers/pipelines/unclip/pipeline_unclip_image_variation.py +1 -0
- diffusers/pipelines/wuerstchen/modeling_paella_vq_model.py +1 -1
- diffusers/pipelines/wuerstchen/modeling_wuerstchen_common.py +14 -4
- diffusers/pipelines/wuerstchen/modeling_wuerstchen_prior.py +9 -5
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen.py +1 -1
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen_combined.py +2 -2
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen_prior.py +5 -1
- diffusers/schedulers/__init__.py +4 -4
- diffusers/schedulers/deprecated/__init__.py +50 -0
- diffusers/schedulers/{scheduling_karras_ve.py → deprecated/scheduling_karras_ve.py} +4 -4
- diffusers/schedulers/{scheduling_sde_vp.py → deprecated/scheduling_sde_vp.py} +4 -6
- diffusers/schedulers/scheduling_amused.py +162 -0
- diffusers/schedulers/scheduling_consistency_models.py +2 -0
- diffusers/schedulers/scheduling_ddim.py +1 -3
- diffusers/schedulers/scheduling_ddim_inverse.py +2 -7
- diffusers/schedulers/scheduling_ddim_parallel.py +1 -3
- diffusers/schedulers/scheduling_ddpm.py +47 -3
- diffusers/schedulers/scheduling_ddpm_parallel.py +47 -3
- diffusers/schedulers/scheduling_deis_multistep.py +28 -6
- diffusers/schedulers/scheduling_dpmsolver_multistep.py +28 -6
- diffusers/schedulers/scheduling_dpmsolver_multistep_inverse.py +28 -6
- diffusers/schedulers/scheduling_dpmsolver_sde.py +3 -3
- diffusers/schedulers/scheduling_dpmsolver_singlestep.py +28 -6
- diffusers/schedulers/scheduling_euler_ancestral_discrete.py +59 -3
- diffusers/schedulers/scheduling_euler_discrete.py +102 -16
- diffusers/schedulers/scheduling_heun_discrete.py +17 -5
- diffusers/schedulers/scheduling_k_dpm_2_ancestral_discrete.py +17 -5
- diffusers/schedulers/scheduling_k_dpm_2_discrete.py +17 -5
- diffusers/schedulers/scheduling_lcm.py +123 -29
- diffusers/schedulers/scheduling_lms_discrete.py +3 -3
- diffusers/schedulers/scheduling_pndm.py +1 -3
- diffusers/schedulers/scheduling_repaint.py +1 -3
- diffusers/schedulers/scheduling_unipc_multistep.py +28 -6
- diffusers/schedulers/scheduling_utils.py +3 -1
- diffusers/schedulers/scheduling_utils_flax.py +3 -1
- diffusers/training_utils.py +1 -1
- diffusers/utils/__init__.py +1 -2
- diffusers/utils/constants.py +10 -12
- diffusers/utils/dummy_pt_objects.py +75 -0
- diffusers/utils/dummy_torch_and_transformers_objects.py +105 -0
- diffusers/utils/dynamic_modules_utils.py +18 -22
- diffusers/utils/export_utils.py +8 -3
- diffusers/utils/hub_utils.py +24 -36
- diffusers/utils/logging.py +11 -11
- diffusers/utils/outputs.py +5 -5
- diffusers/utils/peft_utils.py +88 -44
- diffusers/utils/state_dict_utils.py +8 -0
- diffusers/utils/testing_utils.py +199 -1
- diffusers/utils/torch_utils.py +4 -4
- {diffusers-0.23.1.dist-info → diffusers-0.25.0.dist-info}/METADATA +86 -69
- diffusers-0.25.0.dist-info/RECORD +360 -0
- {diffusers-0.23.1.dist-info → diffusers-0.25.0.dist-info}/WHEEL +1 -1
- {diffusers-0.23.1.dist-info → diffusers-0.25.0.dist-info}/entry_points.txt +0 -1
- diffusers/loaders.py +0 -3336
- diffusers-0.23.1.dist-info/RECORD +0 -323
- /diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/modeling_roberta_series.py +0 -0
- {diffusers-0.23.1.dist-info → diffusers-0.25.0.dist-info}/LICENSE +0 -0
- {diffusers-0.23.1.dist-info → diffusers-0.25.0.dist-info}/top_level.txt +0 -0
@@ -17,15 +17,16 @@ from typing import Any, Callable, Dict, List, Optional, Union
|
|
17
17
|
|
18
18
|
import torch
|
19
19
|
from packaging import version
|
20
|
-
from transformers import CLIPImageProcessor, XLMRobertaTokenizer
|
21
|
-
|
22
|
-
from
|
23
|
-
from
|
24
|
-
from
|
25
|
-
from
|
26
|
-
from
|
27
|
-
from
|
28
|
-
from
|
20
|
+
from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection, XLMRobertaTokenizer
|
21
|
+
|
22
|
+
from ....configuration_utils import FrozenDict
|
23
|
+
from ....image_processor import PipelineImageInput, VaeImageProcessor
|
24
|
+
from ....loaders import FromSingleFileMixin, IPAdapterMixin, LoraLoaderMixin, TextualInversionLoaderMixin
|
25
|
+
from ....models import AutoencoderKL, ImageProjection, UNet2DConditionModel
|
26
|
+
from ....models.attention_processor import FusedAttnProcessor2_0
|
27
|
+
from ....models.lora import adjust_lora_scale_text_encoder
|
28
|
+
from ....schedulers import KarrasDiffusionSchedulers
|
29
|
+
from ....utils import (
|
29
30
|
USE_PEFT_BACKEND,
|
30
31
|
deprecate,
|
31
32
|
logging,
|
@@ -33,9 +34,9 @@ from ...utils import (
|
|
33
34
|
scale_lora_layers,
|
34
35
|
unscale_lora_layers,
|
35
36
|
)
|
36
|
-
from
|
37
|
-
from
|
38
|
-
from
|
37
|
+
from ....utils.torch_utils import randn_tensor
|
38
|
+
from ...pipeline_utils import DiffusionPipeline
|
39
|
+
from ...stable_diffusion.safety_checker import StableDiffusionSafetyChecker
|
39
40
|
from .modeling_roberta_series import RobertaSeriesModelWithTransformation
|
40
41
|
from .pipeline_output import AltDiffusionPipelineOutput
|
41
42
|
|
@@ -73,8 +74,54 @@ def rescale_noise_cfg(noise_cfg, noise_pred_text, guidance_rescale=0.0):
|
|
73
74
|
return noise_cfg
|
74
75
|
|
75
76
|
|
76
|
-
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.
|
77
|
-
|
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
|
+
|
122
|
+
class AltDiffusionPipeline(
|
123
|
+
DiffusionPipeline, TextualInversionLoaderMixin, LoraLoaderMixin, IPAdapterMixin, FromSingleFileMixin
|
124
|
+
):
|
78
125
|
r"""
|
79
126
|
Pipeline for text-to-image generation using Alt Diffusion.
|
80
127
|
|
@@ -86,6 +133,7 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
86
133
|
- [`~loaders.LoraLoaderMixin.load_lora_weights`] for loading LoRA weights
|
87
134
|
- [`~loaders.LoraLoaderMixin.save_lora_weights`] for saving LoRA weights
|
88
135
|
- [`~loaders.FromSingleFileMixin.from_single_file`] for loading `.ckpt` files
|
136
|
+
- [`~loaders.IPAdapterMixin.load_ip_adapter`] for loading IP Adapters
|
89
137
|
|
90
138
|
Args:
|
91
139
|
vae ([`AutoencoderKL`]):
|
@@ -107,8 +155,8 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
107
155
|
A `CLIPImageProcessor` to extract features from generated images; used as inputs to the `safety_checker`.
|
108
156
|
"""
|
109
157
|
|
110
|
-
model_cpu_offload_seq = "text_encoder->unet->vae"
|
111
|
-
_optional_components = ["safety_checker", "feature_extractor"]
|
158
|
+
model_cpu_offload_seq = "text_encoder->image_encoder->unet->vae"
|
159
|
+
_optional_components = ["safety_checker", "feature_extractor", "image_encoder"]
|
112
160
|
_exclude_from_cpu_offload = ["safety_checker"]
|
113
161
|
_callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"]
|
114
162
|
|
@@ -121,6 +169,7 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
121
169
|
scheduler: KarrasDiffusionSchedulers,
|
122
170
|
safety_checker: StableDiffusionSafetyChecker,
|
123
171
|
feature_extractor: CLIPImageProcessor,
|
172
|
+
image_encoder: CLIPVisionModelWithProjection = None,
|
124
173
|
requires_safety_checker: bool = True,
|
125
174
|
):
|
126
175
|
super().__init__()
|
@@ -197,6 +246,7 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
197
246
|
scheduler=scheduler,
|
198
247
|
safety_checker=safety_checker,
|
199
248
|
feature_extractor=feature_extractor,
|
249
|
+
image_encoder=image_encoder,
|
200
250
|
)
|
201
251
|
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
|
202
252
|
self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor)
|
@@ -243,10 +293,7 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
243
293
|
lora_scale: Optional[float] = None,
|
244
294
|
**kwargs,
|
245
295
|
):
|
246
|
-
deprecation_message = (
|
247
|
-
"`_encode_prompt()` is deprecated and it will be removed in a future version. Use `encode_prompt()`"
|
248
|
-
" instead. Also, be aware that the output format changed from a concatenated tensor to a tuple."
|
249
|
-
)
|
296
|
+
deprecation_message = "`_encode_prompt()` is deprecated and it will be removed in a future version. Use `encode_prompt()` instead. Also, be aware that the output format changed from a concatenated tensor to a tuple."
|
250
297
|
deprecate("_encode_prompt()", "1.0.0", deprecation_message, standard_warn=False)
|
251
298
|
|
252
299
|
prompt_embeds_tuple = self.encode_prompt(
|
@@ -447,6 +494,30 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
447
494
|
|
448
495
|
return prompt_embeds, negative_prompt_embeds
|
449
496
|
|
497
|
+
def encode_image(self, image, device, num_images_per_prompt, output_hidden_states=None):
|
498
|
+
dtype = next(self.image_encoder.parameters()).dtype
|
499
|
+
|
500
|
+
if not isinstance(image, torch.Tensor):
|
501
|
+
image = self.feature_extractor(image, return_tensors="pt").pixel_values
|
502
|
+
|
503
|
+
image = image.to(device=device, dtype=dtype)
|
504
|
+
if output_hidden_states:
|
505
|
+
image_enc_hidden_states = self.image_encoder(image, output_hidden_states=True).hidden_states[-2]
|
506
|
+
image_enc_hidden_states = image_enc_hidden_states.repeat_interleave(num_images_per_prompt, dim=0)
|
507
|
+
uncond_image_enc_hidden_states = self.image_encoder(
|
508
|
+
torch.zeros_like(image), output_hidden_states=True
|
509
|
+
).hidden_states[-2]
|
510
|
+
uncond_image_enc_hidden_states = uncond_image_enc_hidden_states.repeat_interleave(
|
511
|
+
num_images_per_prompt, dim=0
|
512
|
+
)
|
513
|
+
return image_enc_hidden_states, uncond_image_enc_hidden_states
|
514
|
+
else:
|
515
|
+
image_embeds = self.image_encoder(image).image_embeds
|
516
|
+
image_embeds = image_embeds.repeat_interleave(num_images_per_prompt, dim=0)
|
517
|
+
uncond_image_embeds = torch.zeros_like(image_embeds)
|
518
|
+
|
519
|
+
return image_embeds, uncond_image_embeds
|
520
|
+
|
450
521
|
def run_safety_checker(self, image, device, dtype):
|
451
522
|
if self.safety_checker is None:
|
452
523
|
has_nsfw_concept = None
|
@@ -462,10 +533,7 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
462
533
|
return image, has_nsfw_concept
|
463
534
|
|
464
535
|
def decode_latents(self, latents):
|
465
|
-
deprecation_message = (
|
466
|
-
"The decode_latents method is deprecated and will be removed in 1.0.0. Please use"
|
467
|
-
" VaeImageProcessor.postprocess(...) instead"
|
468
|
-
)
|
536
|
+
deprecation_message = "The decode_latents method is deprecated and will be removed in 1.0.0. Please use VaeImageProcessor.postprocess(...) instead"
|
469
537
|
deprecate("decode_latents", "1.0.0", deprecation_message, standard_warn=False)
|
470
538
|
|
471
539
|
latents = 1 / self.vae.config.scaling_factor * latents
|
@@ -515,8 +583,7 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
515
583
|
k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs
|
516
584
|
):
|
517
585
|
raise ValueError(
|
518
|
-
f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found"
|
519
|
-
f" {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}"
|
586
|
+
f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}"
|
520
587
|
)
|
521
588
|
|
522
589
|
if prompt is not None and prompt_embeds is not None:
|
@@ -588,6 +655,65 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
588
655
|
"""Disables the FreeU mechanism if enabled."""
|
589
656
|
self.unet.disable_freeu()
|
590
657
|
|
658
|
+
def fuse_qkv_projections(self, unet: bool = True, vae: bool = True):
|
659
|
+
"""
|
660
|
+
Enables fused QKV projections. For self-attention modules, all projection matrices (i.e., query,
|
661
|
+
key, value) are fused. For cross-attention modules, key and value projection matrices are fused.
|
662
|
+
|
663
|
+
<Tip warning={true}>
|
664
|
+
|
665
|
+
This API is 🧪 experimental.
|
666
|
+
|
667
|
+
</Tip>
|
668
|
+
|
669
|
+
Args:
|
670
|
+
unet (`bool`, defaults to `True`): To apply fusion on the UNet.
|
671
|
+
vae (`bool`, defaults to `True`): To apply fusion on the VAE.
|
672
|
+
"""
|
673
|
+
self.fusing_unet = False
|
674
|
+
self.fusing_vae = False
|
675
|
+
|
676
|
+
if unet:
|
677
|
+
self.fusing_unet = True
|
678
|
+
self.unet.fuse_qkv_projections()
|
679
|
+
self.unet.set_attn_processor(FusedAttnProcessor2_0())
|
680
|
+
|
681
|
+
if vae:
|
682
|
+
if not isinstance(self.vae, AutoencoderKL):
|
683
|
+
raise ValueError("`fuse_qkv_projections()` is only supported for the VAE of type `AutoencoderKL`.")
|
684
|
+
|
685
|
+
self.fusing_vae = True
|
686
|
+
self.vae.fuse_qkv_projections()
|
687
|
+
self.vae.set_attn_processor(FusedAttnProcessor2_0())
|
688
|
+
|
689
|
+
def unfuse_qkv_projections(self, unet: bool = True, vae: bool = True):
|
690
|
+
"""Disable QKV projection fusion if enabled.
|
691
|
+
|
692
|
+
<Tip warning={true}>
|
693
|
+
|
694
|
+
This API is 🧪 experimental.
|
695
|
+
|
696
|
+
</Tip>
|
697
|
+
|
698
|
+
Args:
|
699
|
+
unet (`bool`, defaults to `True`): To apply fusion on the UNet.
|
700
|
+
vae (`bool`, defaults to `True`): To apply fusion on the VAE.
|
701
|
+
|
702
|
+
"""
|
703
|
+
if unet:
|
704
|
+
if not self.fusing_unet:
|
705
|
+
logger.warning("The UNet was not initially fused for QKV projections. Doing nothing.")
|
706
|
+
else:
|
707
|
+
self.unet.unfuse_qkv_projections()
|
708
|
+
self.fusing_unet = False
|
709
|
+
|
710
|
+
if vae:
|
711
|
+
if not self.fusing_vae:
|
712
|
+
logger.warning("The VAE was not initially fused for QKV projections. Doing nothing.")
|
713
|
+
else:
|
714
|
+
self.vae.unfuse_qkv_projections()
|
715
|
+
self.fusing_vae = False
|
716
|
+
|
591
717
|
def get_guidance_scale_embedding(self, w, embedding_dim=512, dtype=torch.float32):
|
592
718
|
"""
|
593
719
|
See https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298
|
@@ -651,6 +777,7 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
651
777
|
height: Optional[int] = None,
|
652
778
|
width: Optional[int] = None,
|
653
779
|
num_inference_steps: int = 50,
|
780
|
+
timesteps: List[int] = None,
|
654
781
|
guidance_scale: float = 7.5,
|
655
782
|
negative_prompt: Optional[Union[str, List[str]]] = None,
|
656
783
|
num_images_per_prompt: Optional[int] = 1,
|
@@ -659,6 +786,7 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
659
786
|
latents: Optional[torch.FloatTensor] = None,
|
660
787
|
prompt_embeds: Optional[torch.FloatTensor] = None,
|
661
788
|
negative_prompt_embeds: Optional[torch.FloatTensor] = None,
|
789
|
+
ip_adapter_image: Optional[PipelineImageInput] = None,
|
662
790
|
output_type: Optional[str] = "pil",
|
663
791
|
return_dict: bool = True,
|
664
792
|
cross_attention_kwargs: Optional[Dict[str, Any]] = None,
|
@@ -681,6 +809,10 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
681
809
|
num_inference_steps (`int`, *optional*, defaults to 50):
|
682
810
|
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
|
683
811
|
expense of slower inference.
|
812
|
+
timesteps (`List[int]`, *optional*):
|
813
|
+
Custom timesteps to use for the denoising process with schedulers which support a `timesteps` argument
|
814
|
+
in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is
|
815
|
+
passed will be used. Must be in descending order.
|
684
816
|
guidance_scale (`float`, *optional*, defaults to 7.5):
|
685
817
|
A higher guidance scale value encourages the model to generate images closely linked to the text
|
686
818
|
`prompt` at the expense of lower image quality. Guidance scale is enabled when `guidance_scale > 1`.
|
@@ -705,6 +837,7 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
705
837
|
negative_prompt_embeds (`torch.FloatTensor`, *optional*):
|
706
838
|
Pre-generated negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
|
707
839
|
not provided, `negative_prompt_embeds` are generated from the `negative_prompt` input argument.
|
840
|
+
ip_adapter_image: (`PipelineImageInput`, *optional*): Optional image input to work with IP Adapters.
|
708
841
|
output_type (`str`, *optional*, defaults to `"pil"`):
|
709
842
|
The output format of the generated image. Choose between `PIL.Image` or `np.array`.
|
710
843
|
return_dict (`bool`, *optional*, defaults to `True`):
|
@@ -728,7 +861,7 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
728
861
|
callback_on_step_end_tensor_inputs (`List`, *optional*):
|
729
862
|
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
|
730
863
|
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
|
731
|
-
`._callback_tensor_inputs` attribute of your
|
864
|
+
`._callback_tensor_inputs` attribute of your pipeline class.
|
732
865
|
|
733
866
|
Examples:
|
734
867
|
|
@@ -747,15 +880,13 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
747
880
|
deprecate(
|
748
881
|
"callback",
|
749
882
|
"1.0.0",
|
750
|
-
"Passing `callback` as an input argument to `__call__` is deprecated, consider using"
|
751
|
-
" `callback_on_step_end`",
|
883
|
+
"Passing `callback` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
|
752
884
|
)
|
753
885
|
if callback_steps is not None:
|
754
886
|
deprecate(
|
755
887
|
"callback_steps",
|
756
888
|
"1.0.0",
|
757
|
-
"Passing `callback_steps` as an input argument to `__call__` is deprecated, consider using"
|
758
|
-
" `callback_on_step_end`",
|
889
|
+
"Passing `callback_steps` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
|
759
890
|
)
|
760
891
|
|
761
892
|
# 0. Default height and width to unet
|
@@ -806,15 +937,23 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
806
937
|
lora_scale=lora_scale,
|
807
938
|
clip_skip=self.clip_skip,
|
808
939
|
)
|
940
|
+
|
809
941
|
# For classifier free guidance, we need to do two forward passes.
|
810
942
|
# Here we concatenate the unconditional and text embeddings into a single batch
|
811
943
|
# to avoid doing two forward passes
|
812
944
|
if self.do_classifier_free_guidance:
|
813
945
|
prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds])
|
814
946
|
|
947
|
+
if ip_adapter_image is not None:
|
948
|
+
output_hidden_state = False if isinstance(self.unet.encoder_hid_proj, ImageProjection) else True
|
949
|
+
image_embeds, negative_image_embeds = self.encode_image(
|
950
|
+
ip_adapter_image, device, num_images_per_prompt, output_hidden_state
|
951
|
+
)
|
952
|
+
if self.do_classifier_free_guidance:
|
953
|
+
image_embeds = torch.cat([negative_image_embeds, image_embeds])
|
954
|
+
|
815
955
|
# 4. Prepare timesteps
|
816
|
-
self.scheduler
|
817
|
-
timesteps = self.scheduler.timesteps
|
956
|
+
timesteps, num_inference_steps = retrieve_timesteps(self.scheduler, num_inference_steps, device, timesteps)
|
818
957
|
|
819
958
|
# 5. Prepare latent variables
|
820
959
|
num_channels_latents = self.unet.config.in_channels
|
@@ -832,7 +971,10 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
832
971
|
# 6. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
|
833
972
|
extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
|
834
973
|
|
835
|
-
# 6.
|
974
|
+
# 6.1 Add image embeds for IP-Adapter
|
975
|
+
added_cond_kwargs = {"image_embeds": image_embeds} if ip_adapter_image is not None else None
|
976
|
+
|
977
|
+
# 6.2 Optionally get Guidance Scale Embedding
|
836
978
|
timestep_cond = None
|
837
979
|
if self.unet.config.time_cond_proj_dim is not None:
|
838
980
|
guidance_scale_tensor = torch.tensor(self.guidance_scale - 1).repeat(batch_size * num_images_per_prompt)
|
@@ -856,6 +998,7 @@ class AltDiffusionPipeline(DiffusionPipeline, TextualInversionLoaderMixin, LoraL
|
|
856
998
|
encoder_hidden_states=prompt_embeds,
|
857
999
|
timestep_cond=timestep_cond,
|
858
1000
|
cross_attention_kwargs=self.cross_attention_kwargs,
|
1001
|
+
added_cond_kwargs=added_cond_kwargs,
|
859
1002
|
return_dict=False,
|
860
1003
|
)[0]
|
861
1004
|
|