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,284 @@
|
|
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
|
+
|
15
|
+
import re
|
16
|
+
|
17
|
+
from ..utils import logging
|
18
|
+
|
19
|
+
|
20
|
+
logger = logging.get_logger(__name__)
|
21
|
+
|
22
|
+
|
23
|
+
def _maybe_map_sgm_blocks_to_diffusers(state_dict, unet_config, delimiter="_", block_slice_pos=5):
|
24
|
+
# 1. get all state_dict_keys
|
25
|
+
all_keys = list(state_dict.keys())
|
26
|
+
sgm_patterns = ["input_blocks", "middle_block", "output_blocks"]
|
27
|
+
|
28
|
+
# 2. check if needs remapping, if not return original dict
|
29
|
+
is_in_sgm_format = False
|
30
|
+
for key in all_keys:
|
31
|
+
if any(p in key for p in sgm_patterns):
|
32
|
+
is_in_sgm_format = True
|
33
|
+
break
|
34
|
+
|
35
|
+
if not is_in_sgm_format:
|
36
|
+
return state_dict
|
37
|
+
|
38
|
+
# 3. Else remap from SGM patterns
|
39
|
+
new_state_dict = {}
|
40
|
+
inner_block_map = ["resnets", "attentions", "upsamplers"]
|
41
|
+
|
42
|
+
# Retrieves # of down, mid and up blocks
|
43
|
+
input_block_ids, middle_block_ids, output_block_ids = set(), set(), set()
|
44
|
+
|
45
|
+
for layer in all_keys:
|
46
|
+
if "text" in layer:
|
47
|
+
new_state_dict[layer] = state_dict.pop(layer)
|
48
|
+
else:
|
49
|
+
layer_id = int(layer.split(delimiter)[:block_slice_pos][-1])
|
50
|
+
if sgm_patterns[0] in layer:
|
51
|
+
input_block_ids.add(layer_id)
|
52
|
+
elif sgm_patterns[1] in layer:
|
53
|
+
middle_block_ids.add(layer_id)
|
54
|
+
elif sgm_patterns[2] in layer:
|
55
|
+
output_block_ids.add(layer_id)
|
56
|
+
else:
|
57
|
+
raise ValueError(f"Checkpoint not supported because layer {layer} not supported.")
|
58
|
+
|
59
|
+
input_blocks = {
|
60
|
+
layer_id: [key for key in state_dict if f"input_blocks{delimiter}{layer_id}" in key]
|
61
|
+
for layer_id in input_block_ids
|
62
|
+
}
|
63
|
+
middle_blocks = {
|
64
|
+
layer_id: [key for key in state_dict if f"middle_block{delimiter}{layer_id}" in key]
|
65
|
+
for layer_id in middle_block_ids
|
66
|
+
}
|
67
|
+
output_blocks = {
|
68
|
+
layer_id: [key for key in state_dict if f"output_blocks{delimiter}{layer_id}" in key]
|
69
|
+
for layer_id in output_block_ids
|
70
|
+
}
|
71
|
+
|
72
|
+
# Rename keys accordingly
|
73
|
+
for i in input_block_ids:
|
74
|
+
block_id = (i - 1) // (unet_config.layers_per_block + 1)
|
75
|
+
layer_in_block_id = (i - 1) % (unet_config.layers_per_block + 1)
|
76
|
+
|
77
|
+
for key in input_blocks[i]:
|
78
|
+
inner_block_id = int(key.split(delimiter)[block_slice_pos])
|
79
|
+
inner_block_key = inner_block_map[inner_block_id] if "op" not in key else "downsamplers"
|
80
|
+
inner_layers_in_block = str(layer_in_block_id) if "op" not in key else "0"
|
81
|
+
new_key = delimiter.join(
|
82
|
+
key.split(delimiter)[: block_slice_pos - 1]
|
83
|
+
+ [str(block_id), inner_block_key, inner_layers_in_block]
|
84
|
+
+ key.split(delimiter)[block_slice_pos + 1 :]
|
85
|
+
)
|
86
|
+
new_state_dict[new_key] = state_dict.pop(key)
|
87
|
+
|
88
|
+
for i in middle_block_ids:
|
89
|
+
key_part = None
|
90
|
+
if i == 0:
|
91
|
+
key_part = [inner_block_map[0], "0"]
|
92
|
+
elif i == 1:
|
93
|
+
key_part = [inner_block_map[1], "0"]
|
94
|
+
elif i == 2:
|
95
|
+
key_part = [inner_block_map[0], "1"]
|
96
|
+
else:
|
97
|
+
raise ValueError(f"Invalid middle block id {i}.")
|
98
|
+
|
99
|
+
for key in middle_blocks[i]:
|
100
|
+
new_key = delimiter.join(
|
101
|
+
key.split(delimiter)[: block_slice_pos - 1] + key_part + key.split(delimiter)[block_slice_pos:]
|
102
|
+
)
|
103
|
+
new_state_dict[new_key] = state_dict.pop(key)
|
104
|
+
|
105
|
+
for i in output_block_ids:
|
106
|
+
block_id = i // (unet_config.layers_per_block + 1)
|
107
|
+
layer_in_block_id = i % (unet_config.layers_per_block + 1)
|
108
|
+
|
109
|
+
for key in output_blocks[i]:
|
110
|
+
inner_block_id = int(key.split(delimiter)[block_slice_pos])
|
111
|
+
inner_block_key = inner_block_map[inner_block_id]
|
112
|
+
inner_layers_in_block = str(layer_in_block_id) if inner_block_id < 2 else "0"
|
113
|
+
new_key = delimiter.join(
|
114
|
+
key.split(delimiter)[: block_slice_pos - 1]
|
115
|
+
+ [str(block_id), inner_block_key, inner_layers_in_block]
|
116
|
+
+ key.split(delimiter)[block_slice_pos + 1 :]
|
117
|
+
)
|
118
|
+
new_state_dict[new_key] = state_dict.pop(key)
|
119
|
+
|
120
|
+
if len(state_dict) > 0:
|
121
|
+
raise ValueError("At this point all state dict entries have to be converted.")
|
122
|
+
|
123
|
+
return new_state_dict
|
124
|
+
|
125
|
+
|
126
|
+
def _convert_kohya_lora_to_diffusers(state_dict, unet_name="unet", text_encoder_name="text_encoder"):
|
127
|
+
unet_state_dict = {}
|
128
|
+
te_state_dict = {}
|
129
|
+
te2_state_dict = {}
|
130
|
+
network_alphas = {}
|
131
|
+
|
132
|
+
# every down weight has a corresponding up weight and potentially an alpha weight
|
133
|
+
lora_keys = [k for k in state_dict.keys() if k.endswith("lora_down.weight")]
|
134
|
+
for key in lora_keys:
|
135
|
+
lora_name = key.split(".")[0]
|
136
|
+
lora_name_up = lora_name + ".lora_up.weight"
|
137
|
+
lora_name_alpha = lora_name + ".alpha"
|
138
|
+
|
139
|
+
if lora_name.startswith("lora_unet_"):
|
140
|
+
diffusers_name = key.replace("lora_unet_", "").replace("_", ".")
|
141
|
+
|
142
|
+
if "input.blocks" in diffusers_name:
|
143
|
+
diffusers_name = diffusers_name.replace("input.blocks", "down_blocks")
|
144
|
+
else:
|
145
|
+
diffusers_name = diffusers_name.replace("down.blocks", "down_blocks")
|
146
|
+
|
147
|
+
if "middle.block" in diffusers_name:
|
148
|
+
diffusers_name = diffusers_name.replace("middle.block", "mid_block")
|
149
|
+
else:
|
150
|
+
diffusers_name = diffusers_name.replace("mid.block", "mid_block")
|
151
|
+
if "output.blocks" in diffusers_name:
|
152
|
+
diffusers_name = diffusers_name.replace("output.blocks", "up_blocks")
|
153
|
+
else:
|
154
|
+
diffusers_name = diffusers_name.replace("up.blocks", "up_blocks")
|
155
|
+
|
156
|
+
diffusers_name = diffusers_name.replace("transformer.blocks", "transformer_blocks")
|
157
|
+
diffusers_name = diffusers_name.replace("to.q.lora", "to_q_lora")
|
158
|
+
diffusers_name = diffusers_name.replace("to.k.lora", "to_k_lora")
|
159
|
+
diffusers_name = diffusers_name.replace("to.v.lora", "to_v_lora")
|
160
|
+
diffusers_name = diffusers_name.replace("to.out.0.lora", "to_out_lora")
|
161
|
+
diffusers_name = diffusers_name.replace("proj.in", "proj_in")
|
162
|
+
diffusers_name = diffusers_name.replace("proj.out", "proj_out")
|
163
|
+
diffusers_name = diffusers_name.replace("emb.layers", "time_emb_proj")
|
164
|
+
|
165
|
+
# SDXL specificity.
|
166
|
+
if "emb" in diffusers_name and "time.emb.proj" not in diffusers_name:
|
167
|
+
pattern = r"\.\d+(?=\D*$)"
|
168
|
+
diffusers_name = re.sub(pattern, "", diffusers_name, count=1)
|
169
|
+
if ".in." in diffusers_name:
|
170
|
+
diffusers_name = diffusers_name.replace("in.layers.2", "conv1")
|
171
|
+
if ".out." in diffusers_name:
|
172
|
+
diffusers_name = diffusers_name.replace("out.layers.3", "conv2")
|
173
|
+
if "downsamplers" in diffusers_name or "upsamplers" in diffusers_name:
|
174
|
+
diffusers_name = diffusers_name.replace("op", "conv")
|
175
|
+
if "skip" in diffusers_name:
|
176
|
+
diffusers_name = diffusers_name.replace("skip.connection", "conv_shortcut")
|
177
|
+
|
178
|
+
# LyCORIS specificity.
|
179
|
+
if "time.emb.proj" in diffusers_name:
|
180
|
+
diffusers_name = diffusers_name.replace("time.emb.proj", "time_emb_proj")
|
181
|
+
if "conv.shortcut" in diffusers_name:
|
182
|
+
diffusers_name = diffusers_name.replace("conv.shortcut", "conv_shortcut")
|
183
|
+
|
184
|
+
# General coverage.
|
185
|
+
if "transformer_blocks" in diffusers_name:
|
186
|
+
if "attn1" in diffusers_name or "attn2" in diffusers_name:
|
187
|
+
diffusers_name = diffusers_name.replace("attn1", "attn1.processor")
|
188
|
+
diffusers_name = diffusers_name.replace("attn2", "attn2.processor")
|
189
|
+
unet_state_dict[diffusers_name] = state_dict.pop(key)
|
190
|
+
unet_state_dict[diffusers_name.replace(".down.", ".up.")] = state_dict.pop(lora_name_up)
|
191
|
+
elif "ff" in diffusers_name:
|
192
|
+
unet_state_dict[diffusers_name] = state_dict.pop(key)
|
193
|
+
unet_state_dict[diffusers_name.replace(".down.", ".up.")] = state_dict.pop(lora_name_up)
|
194
|
+
elif any(key in diffusers_name for key in ("proj_in", "proj_out")):
|
195
|
+
unet_state_dict[diffusers_name] = state_dict.pop(key)
|
196
|
+
unet_state_dict[diffusers_name.replace(".down.", ".up.")] = state_dict.pop(lora_name_up)
|
197
|
+
else:
|
198
|
+
unet_state_dict[diffusers_name] = state_dict.pop(key)
|
199
|
+
unet_state_dict[diffusers_name.replace(".down.", ".up.")] = state_dict.pop(lora_name_up)
|
200
|
+
|
201
|
+
elif lora_name.startswith("lora_te_"):
|
202
|
+
diffusers_name = key.replace("lora_te_", "").replace("_", ".")
|
203
|
+
diffusers_name = diffusers_name.replace("text.model", "text_model")
|
204
|
+
diffusers_name = diffusers_name.replace("self.attn", "self_attn")
|
205
|
+
diffusers_name = diffusers_name.replace("q.proj.lora", "to_q_lora")
|
206
|
+
diffusers_name = diffusers_name.replace("k.proj.lora", "to_k_lora")
|
207
|
+
diffusers_name = diffusers_name.replace("v.proj.lora", "to_v_lora")
|
208
|
+
diffusers_name = diffusers_name.replace("out.proj.lora", "to_out_lora")
|
209
|
+
if "self_attn" in diffusers_name:
|
210
|
+
te_state_dict[diffusers_name] = state_dict.pop(key)
|
211
|
+
te_state_dict[diffusers_name.replace(".down.", ".up.")] = state_dict.pop(lora_name_up)
|
212
|
+
elif "mlp" in diffusers_name:
|
213
|
+
# Be aware that this is the new diffusers convention and the rest of the code might
|
214
|
+
# not utilize it yet.
|
215
|
+
diffusers_name = diffusers_name.replace(".lora.", ".lora_linear_layer.")
|
216
|
+
te_state_dict[diffusers_name] = state_dict.pop(key)
|
217
|
+
te_state_dict[diffusers_name.replace(".down.", ".up.")] = state_dict.pop(lora_name_up)
|
218
|
+
|
219
|
+
# (sayakpaul): Duplicate code. Needs to be cleaned.
|
220
|
+
elif lora_name.startswith("lora_te1_"):
|
221
|
+
diffusers_name = key.replace("lora_te1_", "").replace("_", ".")
|
222
|
+
diffusers_name = diffusers_name.replace("text.model", "text_model")
|
223
|
+
diffusers_name = diffusers_name.replace("self.attn", "self_attn")
|
224
|
+
diffusers_name = diffusers_name.replace("q.proj.lora", "to_q_lora")
|
225
|
+
diffusers_name = diffusers_name.replace("k.proj.lora", "to_k_lora")
|
226
|
+
diffusers_name = diffusers_name.replace("v.proj.lora", "to_v_lora")
|
227
|
+
diffusers_name = diffusers_name.replace("out.proj.lora", "to_out_lora")
|
228
|
+
if "self_attn" in diffusers_name:
|
229
|
+
te_state_dict[diffusers_name] = state_dict.pop(key)
|
230
|
+
te_state_dict[diffusers_name.replace(".down.", ".up.")] = state_dict.pop(lora_name_up)
|
231
|
+
elif "mlp" in diffusers_name:
|
232
|
+
# Be aware that this is the new diffusers convention and the rest of the code might
|
233
|
+
# not utilize it yet.
|
234
|
+
diffusers_name = diffusers_name.replace(".lora.", ".lora_linear_layer.")
|
235
|
+
te_state_dict[diffusers_name] = state_dict.pop(key)
|
236
|
+
te_state_dict[diffusers_name.replace(".down.", ".up.")] = state_dict.pop(lora_name_up)
|
237
|
+
|
238
|
+
# (sayakpaul): Duplicate code. Needs to be cleaned.
|
239
|
+
elif lora_name.startswith("lora_te2_"):
|
240
|
+
diffusers_name = key.replace("lora_te2_", "").replace("_", ".")
|
241
|
+
diffusers_name = diffusers_name.replace("text.model", "text_model")
|
242
|
+
diffusers_name = diffusers_name.replace("self.attn", "self_attn")
|
243
|
+
diffusers_name = diffusers_name.replace("q.proj.lora", "to_q_lora")
|
244
|
+
diffusers_name = diffusers_name.replace("k.proj.lora", "to_k_lora")
|
245
|
+
diffusers_name = diffusers_name.replace("v.proj.lora", "to_v_lora")
|
246
|
+
diffusers_name = diffusers_name.replace("out.proj.lora", "to_out_lora")
|
247
|
+
if "self_attn" in diffusers_name:
|
248
|
+
te2_state_dict[diffusers_name] = state_dict.pop(key)
|
249
|
+
te2_state_dict[diffusers_name.replace(".down.", ".up.")] = state_dict.pop(lora_name_up)
|
250
|
+
elif "mlp" in diffusers_name:
|
251
|
+
# Be aware that this is the new diffusers convention and the rest of the code might
|
252
|
+
# not utilize it yet.
|
253
|
+
diffusers_name = diffusers_name.replace(".lora.", ".lora_linear_layer.")
|
254
|
+
te2_state_dict[diffusers_name] = state_dict.pop(key)
|
255
|
+
te2_state_dict[diffusers_name.replace(".down.", ".up.")] = state_dict.pop(lora_name_up)
|
256
|
+
|
257
|
+
# Rename the alphas so that they can be mapped appropriately.
|
258
|
+
if lora_name_alpha in state_dict:
|
259
|
+
alpha = state_dict.pop(lora_name_alpha).item()
|
260
|
+
if lora_name_alpha.startswith("lora_unet_"):
|
261
|
+
prefix = "unet."
|
262
|
+
elif lora_name_alpha.startswith(("lora_te_", "lora_te1_")):
|
263
|
+
prefix = "text_encoder."
|
264
|
+
else:
|
265
|
+
prefix = "text_encoder_2."
|
266
|
+
new_name = prefix + diffusers_name.split(".lora.")[0] + ".alpha"
|
267
|
+
network_alphas.update({new_name: alpha})
|
268
|
+
|
269
|
+
if len(state_dict) > 0:
|
270
|
+
raise ValueError(f"The following keys have not been correctly be renamed: \n\n {', '.join(state_dict.keys())}")
|
271
|
+
|
272
|
+
logger.info("Kohya-style checkpoint detected.")
|
273
|
+
unet_state_dict = {f"{unet_name}.{module_name}": params for module_name, params in unet_state_dict.items()}
|
274
|
+
te_state_dict = {f"{text_encoder_name}.{module_name}": params for module_name, params in te_state_dict.items()}
|
275
|
+
te2_state_dict = (
|
276
|
+
{f"text_encoder_2.{module_name}": params for module_name, params in te2_state_dict.items()}
|
277
|
+
if len(te2_state_dict) > 0
|
278
|
+
else None
|
279
|
+
)
|
280
|
+
if te2_state_dict is not None:
|
281
|
+
te_state_dict.update(te2_state_dict)
|
282
|
+
|
283
|
+
new_state_dict = {**unet_state_dict, **te_state_dict}
|
284
|
+
return new_state_dict, network_alphas
|