diffusers 0.23.1__py3-none-any.whl → 0.24.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 +16 -2
- diffusers/configuration_utils.py +1 -0
- diffusers/dependency_versions_check.py +0 -1
- diffusers/dependency_versions_table.py +4 -5
- diffusers/image_processor.py +186 -14
- diffusers/loaders/__init__.py +82 -0
- diffusers/loaders/ip_adapter.py +157 -0
- diffusers/loaders/lora.py +1415 -0
- diffusers/loaders/lora_conversion_utils.py +284 -0
- diffusers/loaders/single_file.py +631 -0
- diffusers/loaders/textual_inversion.py +459 -0
- diffusers/loaders/unet.py +735 -0
- diffusers/loaders/utils.py +59 -0
- diffusers/models/__init__.py +12 -1
- diffusers/models/attention.py +165 -14
- diffusers/models/attention_flax.py +9 -1
- diffusers/models/attention_processor.py +286 -1
- diffusers/models/autoencoder_asym_kl.py +14 -9
- diffusers/models/autoencoder_kl.py +3 -18
- diffusers/models/autoencoder_kl_temporal_decoder.py +402 -0
- diffusers/models/autoencoder_tiny.py +20 -24
- diffusers/models/consistency_decoder_vae.py +37 -30
- diffusers/models/controlnet.py +59 -39
- diffusers/models/controlnet_flax.py +19 -18
- diffusers/models/embeddings_flax.py +2 -0
- diffusers/models/lora.py +131 -1
- diffusers/models/modeling_flax_utils.py +2 -1
- diffusers/models/modeling_outputs.py +17 -0
- diffusers/models/modeling_utils.py +27 -19
- diffusers/models/normalization.py +2 -2
- diffusers/models/resnet.py +390 -59
- diffusers/models/transformer_2d.py +20 -3
- diffusers/models/transformer_temporal.py +183 -1
- diffusers/models/unet_2d_blocks_flax.py +5 -0
- diffusers/models/unet_2d_condition.py +9 -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_kandi3.py +589 -0
- diffusers/models/unet_motion_model.py +48 -33
- diffusers/models/unet_spatio_temporal_condition.py +489 -0
- diffusers/models/vae.py +63 -13
- diffusers/models/vae_flax.py +7 -0
- diffusers/models/vq_model.py +3 -1
- diffusers/optimization.py +16 -9
- diffusers/pipelines/__init__.py +65 -12
- diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion.py +93 -23
- diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion_img2img.py +97 -25
- diffusers/pipelines/animatediff/pipeline_animatediff.py +34 -4
- diffusers/pipelines/audioldm/pipeline_audioldm.py +1 -0
- diffusers/pipelines/auto_pipeline.py +6 -0
- diffusers/pipelines/consistency_models/pipeline_consistency_models.py +1 -0
- diffusers/pipelines/controlnet/pipeline_controlnet.py +217 -31
- diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +101 -32
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +136 -39
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +119 -37
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +196 -35
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +102 -31
- 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/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/kandinsky3_pipeline.py +452 -0
- diffusers/pipelines/kandinsky3/kandinsky3img2img_pipeline.py +460 -0
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +65 -6
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +55 -3
- diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py +1 -0
- diffusers/pipelines/musicldm/pipeline_musicldm.py +1 -1
- diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py +7 -2
- diffusers/pipelines/pipeline_flax_utils.py +4 -2
- diffusers/pipelines/pipeline_utils.py +33 -13
- diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +196 -36
- diffusers/pipelines/score_sde_ve/pipeline_score_sde_ve.py +1 -0
- diffusers/pipelines/spectrogram_diffusion/pipeline_spectrogram_diffusion.py +1 -0
- diffusers/pipelines/stable_diffusion/__init__.py +64 -21
- diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +8 -3
- diffusers/pipelines/stable_diffusion/pipeline_cycle_diffusion.py +18 -2
- 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_onnx_stable_diffusion_inpaint_legacy.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +88 -9
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_attend_and_excite.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_depth2img.py +8 -3
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_diffedit.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_gligen.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_gligen_text_image.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_image_variation.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +92 -9
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +92 -9
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint_legacy.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +17 -13
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_k_diffusion.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_latent_upscale.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_ldm3d.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_model_editing.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_panorama.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_paradigms.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_pix2pix_zero.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py +1 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +1 -0
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +103 -8
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +113 -8
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +115 -9
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +16 -12
- diffusers/pipelines/stable_video_diffusion/__init__.py +58 -0
- diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +649 -0
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_adapter.py +108 -12
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +109 -14
- diffusers/pipelines/text_to_video_synthesis/__init__.py +2 -0
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +1 -0
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +18 -3
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +4 -2
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +872 -0
- diffusers/pipelines/versatile_diffusion/modeling_text_unet.py +29 -40
- diffusers/pipelines/versatile_diffusion/pipeline_versatile_diffusion_dual_guided.py +1 -0
- diffusers/pipelines/versatile_diffusion/pipeline_versatile_diffusion_image_variation.py +1 -0
- diffusers/pipelines/versatile_diffusion/pipeline_versatile_diffusion_text_to_image.py +1 -0
- 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 +1 -1
- diffusers/schedulers/__init__.py +2 -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_ddim.py +1 -3
- diffusers/schedulers/scheduling_ddim_inverse.py +1 -3
- diffusers/schedulers/scheduling_ddim_parallel.py +1 -3
- diffusers/schedulers/scheduling_ddpm.py +1 -3
- diffusers/schedulers/scheduling_ddpm_parallel.py +1 -3
- diffusers/schedulers/scheduling_deis_multistep.py +15 -5
- diffusers/schedulers/scheduling_dpmsolver_multistep.py +15 -5
- diffusers/schedulers/scheduling_dpmsolver_multistep_inverse.py +15 -5
- diffusers/schedulers/scheduling_dpmsolver_sde.py +1 -3
- diffusers/schedulers/scheduling_dpmsolver_singlestep.py +15 -5
- diffusers/schedulers/scheduling_euler_ancestral_discrete.py +1 -3
- diffusers/schedulers/scheduling_euler_discrete.py +40 -13
- diffusers/schedulers/scheduling_heun_discrete.py +15 -5
- diffusers/schedulers/scheduling_k_dpm_2_ancestral_discrete.py +15 -5
- diffusers/schedulers/scheduling_k_dpm_2_discrete.py +15 -5
- diffusers/schedulers/scheduling_lcm.py +123 -29
- diffusers/schedulers/scheduling_lms_discrete.py +1 -3
- diffusers/schedulers/scheduling_pndm.py +1 -3
- diffusers/schedulers/scheduling_repaint.py +1 -3
- diffusers/schedulers/scheduling_unipc_multistep.py +15 -5
- diffusers/utils/__init__.py +1 -0
- diffusers/utils/constants.py +8 -7
- diffusers/utils/dummy_pt_objects.py +45 -0
- diffusers/utils/dummy_torch_and_transformers_objects.py +60 -0
- diffusers/utils/dynamic_modules_utils.py +4 -4
- diffusers/utils/export_utils.py +8 -3
- diffusers/utils/logging.py +10 -10
- diffusers/utils/outputs.py +5 -5
- diffusers/utils/peft_utils.py +88 -44
- diffusers/utils/torch_utils.py +2 -2
- {diffusers-0.23.1.dist-info → diffusers-0.24.0.dist-info}/METADATA +38 -22
- {diffusers-0.23.1.dist-info → diffusers-0.24.0.dist-info}/RECORD +175 -157
- diffusers/loaders.py +0 -3336
- {diffusers-0.23.1.dist-info → diffusers-0.24.0.dist-info}/LICENSE +0 -0
- {diffusers-0.23.1.dist-info → diffusers-0.24.0.dist-info}/WHEEL +0 -0
- {diffusers-0.23.1.dist-info → diffusers-0.24.0.dist-info}/entry_points.txt +0 -0
- {diffusers-0.23.1.dist-info → diffusers-0.24.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
|