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
@@ -0,0 +1,82 @@
|
|
1
|
+
from typing import TYPE_CHECKING
|
2
|
+
|
3
|
+
from ..utils import DIFFUSERS_SLOW_IMPORT, _LazyModule, deprecate
|
4
|
+
from ..utils.import_utils import is_torch_available, is_transformers_available
|
5
|
+
|
6
|
+
|
7
|
+
def text_encoder_lora_state_dict(text_encoder):
|
8
|
+
deprecate(
|
9
|
+
"text_encoder_load_state_dict in `models`",
|
10
|
+
"0.27.0",
|
11
|
+
"`text_encoder_lora_state_dict` is deprecated and will be removed in 0.27.0. Make sure to retrieve the weights using `get_peft_model`. See https://huggingface.co/docs/peft/v0.6.2/en/quicktour#peftmodel for more information.",
|
12
|
+
)
|
13
|
+
state_dict = {}
|
14
|
+
|
15
|
+
for name, module in text_encoder_attn_modules(text_encoder):
|
16
|
+
for k, v in module.q_proj.lora_linear_layer.state_dict().items():
|
17
|
+
state_dict[f"{name}.q_proj.lora_linear_layer.{k}"] = v
|
18
|
+
|
19
|
+
for k, v in module.k_proj.lora_linear_layer.state_dict().items():
|
20
|
+
state_dict[f"{name}.k_proj.lora_linear_layer.{k}"] = v
|
21
|
+
|
22
|
+
for k, v in module.v_proj.lora_linear_layer.state_dict().items():
|
23
|
+
state_dict[f"{name}.v_proj.lora_linear_layer.{k}"] = v
|
24
|
+
|
25
|
+
for k, v in module.out_proj.lora_linear_layer.state_dict().items():
|
26
|
+
state_dict[f"{name}.out_proj.lora_linear_layer.{k}"] = v
|
27
|
+
|
28
|
+
return state_dict
|
29
|
+
|
30
|
+
|
31
|
+
if is_transformers_available():
|
32
|
+
|
33
|
+
def text_encoder_attn_modules(text_encoder):
|
34
|
+
deprecate(
|
35
|
+
"text_encoder_attn_modules in `models`",
|
36
|
+
"0.27.0",
|
37
|
+
"`text_encoder_lora_state_dict` is deprecated and will be removed in 0.27.0. Make sure to retrieve the weights using `get_peft_model`. See https://huggingface.co/docs/peft/v0.6.2/en/quicktour#peftmodel for more information.",
|
38
|
+
)
|
39
|
+
from transformers import CLIPTextModel, CLIPTextModelWithProjection
|
40
|
+
|
41
|
+
attn_modules = []
|
42
|
+
|
43
|
+
if isinstance(text_encoder, (CLIPTextModel, CLIPTextModelWithProjection)):
|
44
|
+
for i, layer in enumerate(text_encoder.text_model.encoder.layers):
|
45
|
+
name = f"text_model.encoder.layers.{i}.self_attn"
|
46
|
+
mod = layer.self_attn
|
47
|
+
attn_modules.append((name, mod))
|
48
|
+
else:
|
49
|
+
raise ValueError(f"do not know how to get attention modules for: {text_encoder.__class__.__name__}")
|
50
|
+
|
51
|
+
return attn_modules
|
52
|
+
|
53
|
+
|
54
|
+
_import_structure = {}
|
55
|
+
|
56
|
+
if is_torch_available():
|
57
|
+
_import_structure["single_file"] = ["FromOriginalControlnetMixin", "FromOriginalVAEMixin"]
|
58
|
+
_import_structure["unet"] = ["UNet2DConditionLoadersMixin"]
|
59
|
+
_import_structure["utils"] = ["AttnProcsLayers"]
|
60
|
+
|
61
|
+
if is_transformers_available():
|
62
|
+
_import_structure["single_file"].extend(["FromSingleFileMixin"])
|
63
|
+
_import_structure["lora"] = ["LoraLoaderMixin", "StableDiffusionXLLoraLoaderMixin"]
|
64
|
+
_import_structure["textual_inversion"] = ["TextualInversionLoaderMixin"]
|
65
|
+
_import_structure["ip_adapter"] = ["IPAdapterMixin"]
|
66
|
+
|
67
|
+
|
68
|
+
if TYPE_CHECKING or DIFFUSERS_SLOW_IMPORT:
|
69
|
+
if is_torch_available():
|
70
|
+
from .single_file import FromOriginalControlnetMixin, FromOriginalVAEMixin
|
71
|
+
from .unet import UNet2DConditionLoadersMixin
|
72
|
+
from .utils import AttnProcsLayers
|
73
|
+
|
74
|
+
if is_transformers_available():
|
75
|
+
from .ip_adapter import IPAdapterMixin
|
76
|
+
from .lora import LoraLoaderMixin, StableDiffusionXLLoraLoaderMixin
|
77
|
+
from .single_file import FromSingleFileMixin
|
78
|
+
from .textual_inversion import TextualInversionLoaderMixin
|
79
|
+
else:
|
80
|
+
import sys
|
81
|
+
|
82
|
+
sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
|
@@ -0,0 +1,159 @@
|
|
1
|
+
# Copyright 2023 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
|
+
import os
|
15
|
+
from typing import Dict, Union
|
16
|
+
|
17
|
+
import torch
|
18
|
+
from huggingface_hub.utils import validate_hf_hub_args
|
19
|
+
from safetensors import safe_open
|
20
|
+
|
21
|
+
from ..utils import (
|
22
|
+
_get_model_file,
|
23
|
+
is_transformers_available,
|
24
|
+
logging,
|
25
|
+
)
|
26
|
+
|
27
|
+
|
28
|
+
if is_transformers_available():
|
29
|
+
from transformers import (
|
30
|
+
CLIPImageProcessor,
|
31
|
+
CLIPVisionModelWithProjection,
|
32
|
+
)
|
33
|
+
|
34
|
+
from ..models.attention_processor import (
|
35
|
+
IPAdapterAttnProcessor,
|
36
|
+
IPAdapterAttnProcessor2_0,
|
37
|
+
)
|
38
|
+
|
39
|
+
logger = logging.get_logger(__name__)
|
40
|
+
|
41
|
+
|
42
|
+
class IPAdapterMixin:
|
43
|
+
"""Mixin for handling IP Adapters."""
|
44
|
+
|
45
|
+
@validate_hf_hub_args
|
46
|
+
def load_ip_adapter(
|
47
|
+
self,
|
48
|
+
pretrained_model_name_or_path_or_dict: Union[str, Dict[str, torch.Tensor]],
|
49
|
+
subfolder: str,
|
50
|
+
weight_name: str,
|
51
|
+
**kwargs,
|
52
|
+
):
|
53
|
+
"""
|
54
|
+
Parameters:
|
55
|
+
pretrained_model_name_or_path_or_dict (`str` or `os.PathLike` or `dict`):
|
56
|
+
Can be either:
|
57
|
+
|
58
|
+
- A string, the *model id* (for example `google/ddpm-celebahq-256`) of a pretrained model hosted on
|
59
|
+
the Hub.
|
60
|
+
- A path to a *directory* (for example `./my_model_directory`) containing the model weights saved
|
61
|
+
with [`ModelMixin.save_pretrained`].
|
62
|
+
- A [torch state
|
63
|
+
dict](https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict).
|
64
|
+
|
65
|
+
cache_dir (`Union[str, os.PathLike]`, *optional*):
|
66
|
+
Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
|
67
|
+
is not used.
|
68
|
+
force_download (`bool`, *optional*, defaults to `False`):
|
69
|
+
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
|
70
|
+
cached versions if they exist.
|
71
|
+
resume_download (`bool`, *optional*, defaults to `False`):
|
72
|
+
Whether or not to resume downloading the model weights and configuration files. If set to `False`, any
|
73
|
+
incompletely downloaded files are deleted.
|
74
|
+
proxies (`Dict[str, str]`, *optional*):
|
75
|
+
A dictionary of proxy servers to use by protocol or endpoint, for example, `{'http': 'foo.bar:3128',
|
76
|
+
'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.
|
77
|
+
local_files_only (`bool`, *optional*, defaults to `False`):
|
78
|
+
Whether to only load local model weights and configuration files or not. If set to `True`, the model
|
79
|
+
won't be downloaded from the Hub.
|
80
|
+
token (`str` or *bool*, *optional*):
|
81
|
+
The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from
|
82
|
+
`diffusers-cli login` (stored in `~/.huggingface`) is used.
|
83
|
+
revision (`str`, *optional*, defaults to `"main"`):
|
84
|
+
The specific model version to use. It can be a branch name, a tag name, a commit id, or any identifier
|
85
|
+
allowed by Git.
|
86
|
+
subfolder (`str`, *optional*, defaults to `""`):
|
87
|
+
The subfolder location of a model file within a larger model repository on the Hub or locally.
|
88
|
+
"""
|
89
|
+
|
90
|
+
# Load the main state dict first.
|
91
|
+
cache_dir = kwargs.pop("cache_dir", None)
|
92
|
+
force_download = kwargs.pop("force_download", False)
|
93
|
+
resume_download = kwargs.pop("resume_download", False)
|
94
|
+
proxies = kwargs.pop("proxies", None)
|
95
|
+
local_files_only = kwargs.pop("local_files_only", None)
|
96
|
+
token = kwargs.pop("token", None)
|
97
|
+
revision = kwargs.pop("revision", None)
|
98
|
+
|
99
|
+
user_agent = {
|
100
|
+
"file_type": "attn_procs_weights",
|
101
|
+
"framework": "pytorch",
|
102
|
+
}
|
103
|
+
|
104
|
+
if not isinstance(pretrained_model_name_or_path_or_dict, dict):
|
105
|
+
model_file = _get_model_file(
|
106
|
+
pretrained_model_name_or_path_or_dict,
|
107
|
+
weights_name=weight_name,
|
108
|
+
cache_dir=cache_dir,
|
109
|
+
force_download=force_download,
|
110
|
+
resume_download=resume_download,
|
111
|
+
proxies=proxies,
|
112
|
+
local_files_only=local_files_only,
|
113
|
+
token=token,
|
114
|
+
revision=revision,
|
115
|
+
subfolder=subfolder,
|
116
|
+
user_agent=user_agent,
|
117
|
+
)
|
118
|
+
if weight_name.endswith(".safetensors"):
|
119
|
+
state_dict = {"image_proj": {}, "ip_adapter": {}}
|
120
|
+
with safe_open(model_file, framework="pt", device="cpu") as f:
|
121
|
+
for key in f.keys():
|
122
|
+
if key.startswith("image_proj."):
|
123
|
+
state_dict["image_proj"][key.replace("image_proj.", "")] = f.get_tensor(key)
|
124
|
+
elif key.startswith("ip_adapter."):
|
125
|
+
state_dict["ip_adapter"][key.replace("ip_adapter.", "")] = f.get_tensor(key)
|
126
|
+
else:
|
127
|
+
state_dict = torch.load(model_file, map_location="cpu")
|
128
|
+
else:
|
129
|
+
state_dict = pretrained_model_name_or_path_or_dict
|
130
|
+
|
131
|
+
keys = list(state_dict.keys())
|
132
|
+
if keys != ["image_proj", "ip_adapter"]:
|
133
|
+
raise ValueError("Required keys are (`image_proj` and `ip_adapter`) missing from the state dict.")
|
134
|
+
|
135
|
+
# load CLIP image encoer here if it has not been registered to the pipeline yet
|
136
|
+
if hasattr(self, "image_encoder") and getattr(self, "image_encoder", None) is None:
|
137
|
+
if not isinstance(pretrained_model_name_or_path_or_dict, dict):
|
138
|
+
logger.info(f"loading image_encoder from {pretrained_model_name_or_path_or_dict}")
|
139
|
+
image_encoder = CLIPVisionModelWithProjection.from_pretrained(
|
140
|
+
pretrained_model_name_or_path_or_dict,
|
141
|
+
subfolder=os.path.join(subfolder, "image_encoder"),
|
142
|
+
).to(self.device, dtype=self.dtype)
|
143
|
+
self.image_encoder = image_encoder
|
144
|
+
else:
|
145
|
+
raise ValueError("`image_encoder` cannot be None when using IP Adapters.")
|
146
|
+
|
147
|
+
# create feature extractor if it has not been registered to the pipeline yet
|
148
|
+
if hasattr(self, "feature_extractor") and getattr(self, "feature_extractor", None) is None:
|
149
|
+
self.feature_extractor = CLIPImageProcessor()
|
150
|
+
|
151
|
+
# load ip-adapter into unet
|
152
|
+
unet = getattr(self, self.unet_name) if not hasattr(self, "unet") else self.unet
|
153
|
+
unet._load_ip_adapter_weights(state_dict)
|
154
|
+
|
155
|
+
def set_ip_adapter_scale(self, scale):
|
156
|
+
unet = getattr(self, self.unet_name) if not hasattr(self, "unet") else self.unet
|
157
|
+
for attn_processor in unet.attn_processors.values():
|
158
|
+
if isinstance(attn_processor, (IPAdapterAttnProcessor, IPAdapterAttnProcessor2_0)):
|
159
|
+
attn_processor.scale = scale
|