diffsynth 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diffsynth/__init__.py +6 -0
- diffsynth/configs/__init__.py +0 -0
- diffsynth/configs/model_config.py +243 -0
- diffsynth/controlnets/__init__.py +2 -0
- diffsynth/controlnets/controlnet_unit.py +53 -0
- diffsynth/controlnets/processors.py +51 -0
- diffsynth/data/__init__.py +1 -0
- diffsynth/data/simple_text_image.py +35 -0
- diffsynth/data/video.py +148 -0
- diffsynth/extensions/ESRGAN/__init__.py +118 -0
- diffsynth/extensions/FastBlend/__init__.py +63 -0
- diffsynth/extensions/FastBlend/api.py +397 -0
- diffsynth/extensions/FastBlend/cupy_kernels.py +119 -0
- diffsynth/extensions/FastBlend/data.py +146 -0
- diffsynth/extensions/FastBlend/patch_match.py +298 -0
- diffsynth/extensions/FastBlend/runners/__init__.py +4 -0
- diffsynth/extensions/FastBlend/runners/accurate.py +35 -0
- diffsynth/extensions/FastBlend/runners/balanced.py +46 -0
- diffsynth/extensions/FastBlend/runners/fast.py +141 -0
- diffsynth/extensions/FastBlend/runners/interpolation.py +121 -0
- diffsynth/extensions/RIFE/__init__.py +242 -0
- diffsynth/extensions/__init__.py +0 -0
- diffsynth/models/__init__.py +1 -0
- diffsynth/models/attention.py +89 -0
- diffsynth/models/downloader.py +66 -0
- diffsynth/models/hunyuan_dit.py +451 -0
- diffsynth/models/hunyuan_dit_text_encoder.py +163 -0
- diffsynth/models/kolors_text_encoder.py +1363 -0
- diffsynth/models/lora.py +195 -0
- diffsynth/models/model_manager.py +536 -0
- diffsynth/models/sd3_dit.py +798 -0
- diffsynth/models/sd3_text_encoder.py +1107 -0
- diffsynth/models/sd3_vae_decoder.py +81 -0
- diffsynth/models/sd3_vae_encoder.py +95 -0
- diffsynth/models/sd_controlnet.py +588 -0
- diffsynth/models/sd_ipadapter.py +57 -0
- diffsynth/models/sd_motion.py +199 -0
- diffsynth/models/sd_text_encoder.py +321 -0
- diffsynth/models/sd_unet.py +1108 -0
- diffsynth/models/sd_vae_decoder.py +336 -0
- diffsynth/models/sd_vae_encoder.py +282 -0
- diffsynth/models/sdxl_ipadapter.py +122 -0
- diffsynth/models/sdxl_motion.py +104 -0
- diffsynth/models/sdxl_text_encoder.py +759 -0
- diffsynth/models/sdxl_unet.py +1899 -0
- diffsynth/models/sdxl_vae_decoder.py +24 -0
- diffsynth/models/sdxl_vae_encoder.py +24 -0
- diffsynth/models/svd_image_encoder.py +505 -0
- diffsynth/models/svd_unet.py +2004 -0
- diffsynth/models/svd_vae_decoder.py +578 -0
- diffsynth/models/svd_vae_encoder.py +139 -0
- diffsynth/models/tiler.py +106 -0
- diffsynth/pipelines/__init__.py +9 -0
- diffsynth/pipelines/base.py +34 -0
- diffsynth/pipelines/dancer.py +178 -0
- diffsynth/pipelines/hunyuan_image.py +274 -0
- diffsynth/pipelines/pipeline_runner.py +105 -0
- diffsynth/pipelines/sd3_image.py +132 -0
- diffsynth/pipelines/sd_image.py +173 -0
- diffsynth/pipelines/sd_video.py +266 -0
- diffsynth/pipelines/sdxl_image.py +191 -0
- diffsynth/pipelines/sdxl_video.py +223 -0
- diffsynth/pipelines/svd_video.py +297 -0
- diffsynth/processors/FastBlend.py +142 -0
- diffsynth/processors/PILEditor.py +28 -0
- diffsynth/processors/RIFE.py +77 -0
- diffsynth/processors/__init__.py +0 -0
- diffsynth/processors/base.py +6 -0
- diffsynth/processors/sequencial_processor.py +41 -0
- diffsynth/prompters/__init__.py +6 -0
- diffsynth/prompters/base_prompter.py +57 -0
- diffsynth/prompters/hunyuan_dit_prompter.py +69 -0
- diffsynth/prompters/kolors_prompter.py +353 -0
- diffsynth/prompters/prompt_refiners.py +77 -0
- diffsynth/prompters/sd3_prompter.py +92 -0
- diffsynth/prompters/sd_prompter.py +73 -0
- diffsynth/prompters/sdxl_prompter.py +61 -0
- diffsynth/schedulers/__init__.py +3 -0
- diffsynth/schedulers/continuous_ode.py +59 -0
- diffsynth/schedulers/ddim.py +79 -0
- diffsynth/schedulers/flow_match.py +51 -0
- diffsynth/tokenizer_configs/__init__.py +0 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer/special_tokens_map.json +7 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer/tokenizer_config.json +16 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer/vocab.txt +47020 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer/vocab_org.txt +21128 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer_t5/config.json +28 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer_t5/special_tokens_map.json +1 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer_t5/spiece.model +0 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer_t5/tokenizer_config.json +1 -0
- diffsynth/tokenizer_configs/kolors/tokenizer/tokenizer.model +0 -0
- diffsynth/tokenizer_configs/kolors/tokenizer/tokenizer_config.json +12 -0
- diffsynth/tokenizer_configs/kolors/tokenizer/vocab.txt +0 -0
- diffsynth/tokenizer_configs/stable_diffusion/tokenizer/merges.txt +48895 -0
- diffsynth/tokenizer_configs/stable_diffusion/tokenizer/special_tokens_map.json +24 -0
- diffsynth/tokenizer_configs/stable_diffusion/tokenizer/tokenizer_config.json +34 -0
- diffsynth/tokenizer_configs/stable_diffusion/tokenizer/vocab.json +49410 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_1/merges.txt +48895 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_1/special_tokens_map.json +30 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_1/tokenizer_config.json +30 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_1/vocab.json +49410 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_2/merges.txt +48895 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_2/special_tokens_map.json +30 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_2/tokenizer_config.json +38 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_2/vocab.json +49410 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_3/special_tokens_map.json +125 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_3/spiece.model +0 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_3/tokenizer.json +129428 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_3/tokenizer_config.json +940 -0
- diffsynth/tokenizer_configs/stable_diffusion_xl/tokenizer_2/merges.txt +40213 -0
- diffsynth/tokenizer_configs/stable_diffusion_xl/tokenizer_2/special_tokens_map.json +24 -0
- diffsynth/tokenizer_configs/stable_diffusion_xl/tokenizer_2/tokenizer_config.json +38 -0
- diffsynth/tokenizer_configs/stable_diffusion_xl/tokenizer_2/vocab.json +49411 -0
- diffsynth/trainers/__init__.py +0 -0
- diffsynth/trainers/text_to_image.py +253 -0
- diffsynth-1.0.0.dist-info/LICENSE +201 -0
- diffsynth-1.0.0.dist-info/METADATA +23 -0
- diffsynth-1.0.0.dist-info/RECORD +120 -0
- diffsynth-1.0.0.dist-info/WHEEL +5 -0
- diffsynth-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .attention import Attention
|
|
3
|
+
from .sd_unet import ResnetBlock, UpSampler
|
|
4
|
+
from .tiler import TileWorker
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class VAEAttentionBlock(torch.nn.Module):
|
|
8
|
+
|
|
9
|
+
def __init__(self, num_attention_heads, attention_head_dim, in_channels, num_layers=1, norm_num_groups=32, eps=1e-5):
|
|
10
|
+
super().__init__()
|
|
11
|
+
inner_dim = num_attention_heads * attention_head_dim
|
|
12
|
+
|
|
13
|
+
self.norm = torch.nn.GroupNorm(num_groups=norm_num_groups, num_channels=in_channels, eps=eps, affine=True)
|
|
14
|
+
|
|
15
|
+
self.transformer_blocks = torch.nn.ModuleList([
|
|
16
|
+
Attention(
|
|
17
|
+
inner_dim,
|
|
18
|
+
num_attention_heads,
|
|
19
|
+
attention_head_dim,
|
|
20
|
+
bias_q=True,
|
|
21
|
+
bias_kv=True,
|
|
22
|
+
bias_out=True
|
|
23
|
+
)
|
|
24
|
+
for d in range(num_layers)
|
|
25
|
+
])
|
|
26
|
+
|
|
27
|
+
def forward(self, hidden_states, time_emb, text_emb, res_stack):
|
|
28
|
+
batch, _, height, width = hidden_states.shape
|
|
29
|
+
residual = hidden_states
|
|
30
|
+
|
|
31
|
+
hidden_states = self.norm(hidden_states)
|
|
32
|
+
inner_dim = hidden_states.shape[1]
|
|
33
|
+
hidden_states = hidden_states.permute(0, 2, 3, 1).reshape(batch, height * width, inner_dim)
|
|
34
|
+
|
|
35
|
+
for block in self.transformer_blocks:
|
|
36
|
+
hidden_states = block(hidden_states)
|
|
37
|
+
|
|
38
|
+
hidden_states = hidden_states.reshape(batch, height, width, inner_dim).permute(0, 3, 1, 2).contiguous()
|
|
39
|
+
hidden_states = hidden_states + residual
|
|
40
|
+
|
|
41
|
+
return hidden_states, time_emb, text_emb, res_stack
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class SDVAEDecoder(torch.nn.Module):
|
|
45
|
+
def __init__(self):
|
|
46
|
+
super().__init__()
|
|
47
|
+
self.scaling_factor = 0.18215
|
|
48
|
+
self.post_quant_conv = torch.nn.Conv2d(4, 4, kernel_size=1)
|
|
49
|
+
self.conv_in = torch.nn.Conv2d(4, 512, kernel_size=3, padding=1)
|
|
50
|
+
|
|
51
|
+
self.blocks = torch.nn.ModuleList([
|
|
52
|
+
# UNetMidBlock2D
|
|
53
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
54
|
+
VAEAttentionBlock(1, 512, 512, 1, eps=1e-6),
|
|
55
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
56
|
+
# UpDecoderBlock2D
|
|
57
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
58
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
59
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
60
|
+
UpSampler(512),
|
|
61
|
+
# UpDecoderBlock2D
|
|
62
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
63
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
64
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
65
|
+
UpSampler(512),
|
|
66
|
+
# UpDecoderBlock2D
|
|
67
|
+
ResnetBlock(512, 256, eps=1e-6),
|
|
68
|
+
ResnetBlock(256, 256, eps=1e-6),
|
|
69
|
+
ResnetBlock(256, 256, eps=1e-6),
|
|
70
|
+
UpSampler(256),
|
|
71
|
+
# UpDecoderBlock2D
|
|
72
|
+
ResnetBlock(256, 128, eps=1e-6),
|
|
73
|
+
ResnetBlock(128, 128, eps=1e-6),
|
|
74
|
+
ResnetBlock(128, 128, eps=1e-6),
|
|
75
|
+
])
|
|
76
|
+
|
|
77
|
+
self.conv_norm_out = torch.nn.GroupNorm(num_channels=128, num_groups=32, eps=1e-5)
|
|
78
|
+
self.conv_act = torch.nn.SiLU()
|
|
79
|
+
self.conv_out = torch.nn.Conv2d(128, 3, kernel_size=3, padding=1)
|
|
80
|
+
|
|
81
|
+
def tiled_forward(self, sample, tile_size=64, tile_stride=32):
|
|
82
|
+
hidden_states = TileWorker().tiled_forward(
|
|
83
|
+
lambda x: self.forward(x),
|
|
84
|
+
sample,
|
|
85
|
+
tile_size,
|
|
86
|
+
tile_stride,
|
|
87
|
+
tile_device=sample.device,
|
|
88
|
+
tile_dtype=sample.dtype
|
|
89
|
+
)
|
|
90
|
+
return hidden_states
|
|
91
|
+
|
|
92
|
+
def forward(self, sample, tiled=False, tile_size=64, tile_stride=32, **kwargs):
|
|
93
|
+
original_dtype = sample.dtype
|
|
94
|
+
sample = sample.to(dtype=next(iter(self.parameters())).dtype)
|
|
95
|
+
# For VAE Decoder, we do not need to apply the tiler on each layer.
|
|
96
|
+
if tiled:
|
|
97
|
+
return self.tiled_forward(sample, tile_size=tile_size, tile_stride=tile_stride)
|
|
98
|
+
|
|
99
|
+
# 1. pre-process
|
|
100
|
+
sample = sample / self.scaling_factor
|
|
101
|
+
hidden_states = self.post_quant_conv(sample)
|
|
102
|
+
hidden_states = self.conv_in(hidden_states)
|
|
103
|
+
time_emb = None
|
|
104
|
+
text_emb = None
|
|
105
|
+
res_stack = None
|
|
106
|
+
|
|
107
|
+
# 2. blocks
|
|
108
|
+
for i, block in enumerate(self.blocks):
|
|
109
|
+
hidden_states, time_emb, text_emb, res_stack = block(hidden_states, time_emb, text_emb, res_stack)
|
|
110
|
+
|
|
111
|
+
# 3. output
|
|
112
|
+
hidden_states = self.conv_norm_out(hidden_states)
|
|
113
|
+
hidden_states = self.conv_act(hidden_states)
|
|
114
|
+
hidden_states = self.conv_out(hidden_states)
|
|
115
|
+
hidden_states = hidden_states.to(original_dtype)
|
|
116
|
+
|
|
117
|
+
return hidden_states
|
|
118
|
+
|
|
119
|
+
@staticmethod
|
|
120
|
+
def state_dict_converter():
|
|
121
|
+
return SDVAEDecoderStateDictConverter()
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class SDVAEDecoderStateDictConverter:
|
|
125
|
+
def __init__(self):
|
|
126
|
+
pass
|
|
127
|
+
|
|
128
|
+
def from_diffusers(self, state_dict):
|
|
129
|
+
# architecture
|
|
130
|
+
block_types = [
|
|
131
|
+
'ResnetBlock', 'VAEAttentionBlock', 'ResnetBlock',
|
|
132
|
+
'ResnetBlock', 'ResnetBlock', 'ResnetBlock', 'UpSampler',
|
|
133
|
+
'ResnetBlock', 'ResnetBlock', 'ResnetBlock', 'UpSampler',
|
|
134
|
+
'ResnetBlock', 'ResnetBlock', 'ResnetBlock', 'UpSampler',
|
|
135
|
+
'ResnetBlock', 'ResnetBlock', 'ResnetBlock'
|
|
136
|
+
]
|
|
137
|
+
|
|
138
|
+
# Rename each parameter
|
|
139
|
+
local_rename_dict = {
|
|
140
|
+
"post_quant_conv": "post_quant_conv",
|
|
141
|
+
"decoder.conv_in": "conv_in",
|
|
142
|
+
"decoder.mid_block.attentions.0.group_norm": "blocks.1.norm",
|
|
143
|
+
"decoder.mid_block.attentions.0.to_q": "blocks.1.transformer_blocks.0.to_q",
|
|
144
|
+
"decoder.mid_block.attentions.0.to_k": "blocks.1.transformer_blocks.0.to_k",
|
|
145
|
+
"decoder.mid_block.attentions.0.to_v": "blocks.1.transformer_blocks.0.to_v",
|
|
146
|
+
"decoder.mid_block.attentions.0.to_out.0": "blocks.1.transformer_blocks.0.to_out",
|
|
147
|
+
"decoder.mid_block.resnets.0.norm1": "blocks.0.norm1",
|
|
148
|
+
"decoder.mid_block.resnets.0.conv1": "blocks.0.conv1",
|
|
149
|
+
"decoder.mid_block.resnets.0.norm2": "blocks.0.norm2",
|
|
150
|
+
"decoder.mid_block.resnets.0.conv2": "blocks.0.conv2",
|
|
151
|
+
"decoder.mid_block.resnets.1.norm1": "blocks.2.norm1",
|
|
152
|
+
"decoder.mid_block.resnets.1.conv1": "blocks.2.conv1",
|
|
153
|
+
"decoder.mid_block.resnets.1.norm2": "blocks.2.norm2",
|
|
154
|
+
"decoder.mid_block.resnets.1.conv2": "blocks.2.conv2",
|
|
155
|
+
"decoder.conv_norm_out": "conv_norm_out",
|
|
156
|
+
"decoder.conv_out": "conv_out",
|
|
157
|
+
}
|
|
158
|
+
name_list = sorted([name for name in state_dict])
|
|
159
|
+
rename_dict = {}
|
|
160
|
+
block_id = {"ResnetBlock": 2, "DownSampler": 2, "UpSampler": 2}
|
|
161
|
+
last_block_type_with_id = {"ResnetBlock": "", "DownSampler": "", "UpSampler": ""}
|
|
162
|
+
for name in name_list:
|
|
163
|
+
names = name.split(".")
|
|
164
|
+
name_prefix = ".".join(names[:-1])
|
|
165
|
+
if name_prefix in local_rename_dict:
|
|
166
|
+
rename_dict[name] = local_rename_dict[name_prefix] + "." + names[-1]
|
|
167
|
+
elif name.startswith("decoder.up_blocks"):
|
|
168
|
+
block_type = {"resnets": "ResnetBlock", "downsamplers": "DownSampler", "upsamplers": "UpSampler"}[names[3]]
|
|
169
|
+
block_type_with_id = ".".join(names[:5])
|
|
170
|
+
if block_type_with_id != last_block_type_with_id[block_type]:
|
|
171
|
+
block_id[block_type] += 1
|
|
172
|
+
last_block_type_with_id[block_type] = block_type_with_id
|
|
173
|
+
while block_id[block_type] < len(block_types) and block_types[block_id[block_type]] != block_type:
|
|
174
|
+
block_id[block_type] += 1
|
|
175
|
+
block_type_with_id = ".".join(names[:5])
|
|
176
|
+
names = ["blocks", str(block_id[block_type])] + names[5:]
|
|
177
|
+
rename_dict[name] = ".".join(names)
|
|
178
|
+
|
|
179
|
+
# Convert state_dict
|
|
180
|
+
state_dict_ = {}
|
|
181
|
+
for name, param in state_dict.items():
|
|
182
|
+
if name in rename_dict:
|
|
183
|
+
state_dict_[rename_dict[name]] = param
|
|
184
|
+
return state_dict_
|
|
185
|
+
|
|
186
|
+
def from_civitai(self, state_dict):
|
|
187
|
+
rename_dict = {
|
|
188
|
+
"first_stage_model.decoder.conv_in.bias": "conv_in.bias",
|
|
189
|
+
"first_stage_model.decoder.conv_in.weight": "conv_in.weight",
|
|
190
|
+
"first_stage_model.decoder.conv_out.bias": "conv_out.bias",
|
|
191
|
+
"first_stage_model.decoder.conv_out.weight": "conv_out.weight",
|
|
192
|
+
"first_stage_model.decoder.mid.attn_1.k.bias": "blocks.1.transformer_blocks.0.to_k.bias",
|
|
193
|
+
"first_stage_model.decoder.mid.attn_1.k.weight": "blocks.1.transformer_blocks.0.to_k.weight",
|
|
194
|
+
"first_stage_model.decoder.mid.attn_1.norm.bias": "blocks.1.norm.bias",
|
|
195
|
+
"first_stage_model.decoder.mid.attn_1.norm.weight": "blocks.1.norm.weight",
|
|
196
|
+
"first_stage_model.decoder.mid.attn_1.proj_out.bias": "blocks.1.transformer_blocks.0.to_out.bias",
|
|
197
|
+
"first_stage_model.decoder.mid.attn_1.proj_out.weight": "blocks.1.transformer_blocks.0.to_out.weight",
|
|
198
|
+
"first_stage_model.decoder.mid.attn_1.q.bias": "blocks.1.transformer_blocks.0.to_q.bias",
|
|
199
|
+
"first_stage_model.decoder.mid.attn_1.q.weight": "blocks.1.transformer_blocks.0.to_q.weight",
|
|
200
|
+
"first_stage_model.decoder.mid.attn_1.v.bias": "blocks.1.transformer_blocks.0.to_v.bias",
|
|
201
|
+
"first_stage_model.decoder.mid.attn_1.v.weight": "blocks.1.transformer_blocks.0.to_v.weight",
|
|
202
|
+
"first_stage_model.decoder.mid.block_1.conv1.bias": "blocks.0.conv1.bias",
|
|
203
|
+
"first_stage_model.decoder.mid.block_1.conv1.weight": "blocks.0.conv1.weight",
|
|
204
|
+
"first_stage_model.decoder.mid.block_1.conv2.bias": "blocks.0.conv2.bias",
|
|
205
|
+
"first_stage_model.decoder.mid.block_1.conv2.weight": "blocks.0.conv2.weight",
|
|
206
|
+
"first_stage_model.decoder.mid.block_1.norm1.bias": "blocks.0.norm1.bias",
|
|
207
|
+
"first_stage_model.decoder.mid.block_1.norm1.weight": "blocks.0.norm1.weight",
|
|
208
|
+
"first_stage_model.decoder.mid.block_1.norm2.bias": "blocks.0.norm2.bias",
|
|
209
|
+
"first_stage_model.decoder.mid.block_1.norm2.weight": "blocks.0.norm2.weight",
|
|
210
|
+
"first_stage_model.decoder.mid.block_2.conv1.bias": "blocks.2.conv1.bias",
|
|
211
|
+
"first_stage_model.decoder.mid.block_2.conv1.weight": "blocks.2.conv1.weight",
|
|
212
|
+
"first_stage_model.decoder.mid.block_2.conv2.bias": "blocks.2.conv2.bias",
|
|
213
|
+
"first_stage_model.decoder.mid.block_2.conv2.weight": "blocks.2.conv2.weight",
|
|
214
|
+
"first_stage_model.decoder.mid.block_2.norm1.bias": "blocks.2.norm1.bias",
|
|
215
|
+
"first_stage_model.decoder.mid.block_2.norm1.weight": "blocks.2.norm1.weight",
|
|
216
|
+
"first_stage_model.decoder.mid.block_2.norm2.bias": "blocks.2.norm2.bias",
|
|
217
|
+
"first_stage_model.decoder.mid.block_2.norm2.weight": "blocks.2.norm2.weight",
|
|
218
|
+
"first_stage_model.decoder.norm_out.bias": "conv_norm_out.bias",
|
|
219
|
+
"first_stage_model.decoder.norm_out.weight": "conv_norm_out.weight",
|
|
220
|
+
"first_stage_model.decoder.up.0.block.0.conv1.bias": "blocks.15.conv1.bias",
|
|
221
|
+
"first_stage_model.decoder.up.0.block.0.conv1.weight": "blocks.15.conv1.weight",
|
|
222
|
+
"first_stage_model.decoder.up.0.block.0.conv2.bias": "blocks.15.conv2.bias",
|
|
223
|
+
"first_stage_model.decoder.up.0.block.0.conv2.weight": "blocks.15.conv2.weight",
|
|
224
|
+
"first_stage_model.decoder.up.0.block.0.nin_shortcut.bias": "blocks.15.conv_shortcut.bias",
|
|
225
|
+
"first_stage_model.decoder.up.0.block.0.nin_shortcut.weight": "blocks.15.conv_shortcut.weight",
|
|
226
|
+
"first_stage_model.decoder.up.0.block.0.norm1.bias": "blocks.15.norm1.bias",
|
|
227
|
+
"first_stage_model.decoder.up.0.block.0.norm1.weight": "blocks.15.norm1.weight",
|
|
228
|
+
"first_stage_model.decoder.up.0.block.0.norm2.bias": "blocks.15.norm2.bias",
|
|
229
|
+
"first_stage_model.decoder.up.0.block.0.norm2.weight": "blocks.15.norm2.weight",
|
|
230
|
+
"first_stage_model.decoder.up.0.block.1.conv1.bias": "blocks.16.conv1.bias",
|
|
231
|
+
"first_stage_model.decoder.up.0.block.1.conv1.weight": "blocks.16.conv1.weight",
|
|
232
|
+
"first_stage_model.decoder.up.0.block.1.conv2.bias": "blocks.16.conv2.bias",
|
|
233
|
+
"first_stage_model.decoder.up.0.block.1.conv2.weight": "blocks.16.conv2.weight",
|
|
234
|
+
"first_stage_model.decoder.up.0.block.1.norm1.bias": "blocks.16.norm1.bias",
|
|
235
|
+
"first_stage_model.decoder.up.0.block.1.norm1.weight": "blocks.16.norm1.weight",
|
|
236
|
+
"first_stage_model.decoder.up.0.block.1.norm2.bias": "blocks.16.norm2.bias",
|
|
237
|
+
"first_stage_model.decoder.up.0.block.1.norm2.weight": "blocks.16.norm2.weight",
|
|
238
|
+
"first_stage_model.decoder.up.0.block.2.conv1.bias": "blocks.17.conv1.bias",
|
|
239
|
+
"first_stage_model.decoder.up.0.block.2.conv1.weight": "blocks.17.conv1.weight",
|
|
240
|
+
"first_stage_model.decoder.up.0.block.2.conv2.bias": "blocks.17.conv2.bias",
|
|
241
|
+
"first_stage_model.decoder.up.0.block.2.conv2.weight": "blocks.17.conv2.weight",
|
|
242
|
+
"first_stage_model.decoder.up.0.block.2.norm1.bias": "blocks.17.norm1.bias",
|
|
243
|
+
"first_stage_model.decoder.up.0.block.2.norm1.weight": "blocks.17.norm1.weight",
|
|
244
|
+
"first_stage_model.decoder.up.0.block.2.norm2.bias": "blocks.17.norm2.bias",
|
|
245
|
+
"first_stage_model.decoder.up.0.block.2.norm2.weight": "blocks.17.norm2.weight",
|
|
246
|
+
"first_stage_model.decoder.up.1.block.0.conv1.bias": "blocks.11.conv1.bias",
|
|
247
|
+
"first_stage_model.decoder.up.1.block.0.conv1.weight": "blocks.11.conv1.weight",
|
|
248
|
+
"first_stage_model.decoder.up.1.block.0.conv2.bias": "blocks.11.conv2.bias",
|
|
249
|
+
"first_stage_model.decoder.up.1.block.0.conv2.weight": "blocks.11.conv2.weight",
|
|
250
|
+
"first_stage_model.decoder.up.1.block.0.nin_shortcut.bias": "blocks.11.conv_shortcut.bias",
|
|
251
|
+
"first_stage_model.decoder.up.1.block.0.nin_shortcut.weight": "blocks.11.conv_shortcut.weight",
|
|
252
|
+
"first_stage_model.decoder.up.1.block.0.norm1.bias": "blocks.11.norm1.bias",
|
|
253
|
+
"first_stage_model.decoder.up.1.block.0.norm1.weight": "blocks.11.norm1.weight",
|
|
254
|
+
"first_stage_model.decoder.up.1.block.0.norm2.bias": "blocks.11.norm2.bias",
|
|
255
|
+
"first_stage_model.decoder.up.1.block.0.norm2.weight": "blocks.11.norm2.weight",
|
|
256
|
+
"first_stage_model.decoder.up.1.block.1.conv1.bias": "blocks.12.conv1.bias",
|
|
257
|
+
"first_stage_model.decoder.up.1.block.1.conv1.weight": "blocks.12.conv1.weight",
|
|
258
|
+
"first_stage_model.decoder.up.1.block.1.conv2.bias": "blocks.12.conv2.bias",
|
|
259
|
+
"first_stage_model.decoder.up.1.block.1.conv2.weight": "blocks.12.conv2.weight",
|
|
260
|
+
"first_stage_model.decoder.up.1.block.1.norm1.bias": "blocks.12.norm1.bias",
|
|
261
|
+
"first_stage_model.decoder.up.1.block.1.norm1.weight": "blocks.12.norm1.weight",
|
|
262
|
+
"first_stage_model.decoder.up.1.block.1.norm2.bias": "blocks.12.norm2.bias",
|
|
263
|
+
"first_stage_model.decoder.up.1.block.1.norm2.weight": "blocks.12.norm2.weight",
|
|
264
|
+
"first_stage_model.decoder.up.1.block.2.conv1.bias": "blocks.13.conv1.bias",
|
|
265
|
+
"first_stage_model.decoder.up.1.block.2.conv1.weight": "blocks.13.conv1.weight",
|
|
266
|
+
"first_stage_model.decoder.up.1.block.2.conv2.bias": "blocks.13.conv2.bias",
|
|
267
|
+
"first_stage_model.decoder.up.1.block.2.conv2.weight": "blocks.13.conv2.weight",
|
|
268
|
+
"first_stage_model.decoder.up.1.block.2.norm1.bias": "blocks.13.norm1.bias",
|
|
269
|
+
"first_stage_model.decoder.up.1.block.2.norm1.weight": "blocks.13.norm1.weight",
|
|
270
|
+
"first_stage_model.decoder.up.1.block.2.norm2.bias": "blocks.13.norm2.bias",
|
|
271
|
+
"first_stage_model.decoder.up.1.block.2.norm2.weight": "blocks.13.norm2.weight",
|
|
272
|
+
"first_stage_model.decoder.up.1.upsample.conv.bias": "blocks.14.conv.bias",
|
|
273
|
+
"first_stage_model.decoder.up.1.upsample.conv.weight": "blocks.14.conv.weight",
|
|
274
|
+
"first_stage_model.decoder.up.2.block.0.conv1.bias": "blocks.7.conv1.bias",
|
|
275
|
+
"first_stage_model.decoder.up.2.block.0.conv1.weight": "blocks.7.conv1.weight",
|
|
276
|
+
"first_stage_model.decoder.up.2.block.0.conv2.bias": "blocks.7.conv2.bias",
|
|
277
|
+
"first_stage_model.decoder.up.2.block.0.conv2.weight": "blocks.7.conv2.weight",
|
|
278
|
+
"first_stage_model.decoder.up.2.block.0.norm1.bias": "blocks.7.norm1.bias",
|
|
279
|
+
"first_stage_model.decoder.up.2.block.0.norm1.weight": "blocks.7.norm1.weight",
|
|
280
|
+
"first_stage_model.decoder.up.2.block.0.norm2.bias": "blocks.7.norm2.bias",
|
|
281
|
+
"first_stage_model.decoder.up.2.block.0.norm2.weight": "blocks.7.norm2.weight",
|
|
282
|
+
"first_stage_model.decoder.up.2.block.1.conv1.bias": "blocks.8.conv1.bias",
|
|
283
|
+
"first_stage_model.decoder.up.2.block.1.conv1.weight": "blocks.8.conv1.weight",
|
|
284
|
+
"first_stage_model.decoder.up.2.block.1.conv2.bias": "blocks.8.conv2.bias",
|
|
285
|
+
"first_stage_model.decoder.up.2.block.1.conv2.weight": "blocks.8.conv2.weight",
|
|
286
|
+
"first_stage_model.decoder.up.2.block.1.norm1.bias": "blocks.8.norm1.bias",
|
|
287
|
+
"first_stage_model.decoder.up.2.block.1.norm1.weight": "blocks.8.norm1.weight",
|
|
288
|
+
"first_stage_model.decoder.up.2.block.1.norm2.bias": "blocks.8.norm2.bias",
|
|
289
|
+
"first_stage_model.decoder.up.2.block.1.norm2.weight": "blocks.8.norm2.weight",
|
|
290
|
+
"first_stage_model.decoder.up.2.block.2.conv1.bias": "blocks.9.conv1.bias",
|
|
291
|
+
"first_stage_model.decoder.up.2.block.2.conv1.weight": "blocks.9.conv1.weight",
|
|
292
|
+
"first_stage_model.decoder.up.2.block.2.conv2.bias": "blocks.9.conv2.bias",
|
|
293
|
+
"first_stage_model.decoder.up.2.block.2.conv2.weight": "blocks.9.conv2.weight",
|
|
294
|
+
"first_stage_model.decoder.up.2.block.2.norm1.bias": "blocks.9.norm1.bias",
|
|
295
|
+
"first_stage_model.decoder.up.2.block.2.norm1.weight": "blocks.9.norm1.weight",
|
|
296
|
+
"first_stage_model.decoder.up.2.block.2.norm2.bias": "blocks.9.norm2.bias",
|
|
297
|
+
"first_stage_model.decoder.up.2.block.2.norm2.weight": "blocks.9.norm2.weight",
|
|
298
|
+
"first_stage_model.decoder.up.2.upsample.conv.bias": "blocks.10.conv.bias",
|
|
299
|
+
"first_stage_model.decoder.up.2.upsample.conv.weight": "blocks.10.conv.weight",
|
|
300
|
+
"first_stage_model.decoder.up.3.block.0.conv1.bias": "blocks.3.conv1.bias",
|
|
301
|
+
"first_stage_model.decoder.up.3.block.0.conv1.weight": "blocks.3.conv1.weight",
|
|
302
|
+
"first_stage_model.decoder.up.3.block.0.conv2.bias": "blocks.3.conv2.bias",
|
|
303
|
+
"first_stage_model.decoder.up.3.block.0.conv2.weight": "blocks.3.conv2.weight",
|
|
304
|
+
"first_stage_model.decoder.up.3.block.0.norm1.bias": "blocks.3.norm1.bias",
|
|
305
|
+
"first_stage_model.decoder.up.3.block.0.norm1.weight": "blocks.3.norm1.weight",
|
|
306
|
+
"first_stage_model.decoder.up.3.block.0.norm2.bias": "blocks.3.norm2.bias",
|
|
307
|
+
"first_stage_model.decoder.up.3.block.0.norm2.weight": "blocks.3.norm2.weight",
|
|
308
|
+
"first_stage_model.decoder.up.3.block.1.conv1.bias": "blocks.4.conv1.bias",
|
|
309
|
+
"first_stage_model.decoder.up.3.block.1.conv1.weight": "blocks.4.conv1.weight",
|
|
310
|
+
"first_stage_model.decoder.up.3.block.1.conv2.bias": "blocks.4.conv2.bias",
|
|
311
|
+
"first_stage_model.decoder.up.3.block.1.conv2.weight": "blocks.4.conv2.weight",
|
|
312
|
+
"first_stage_model.decoder.up.3.block.1.norm1.bias": "blocks.4.norm1.bias",
|
|
313
|
+
"first_stage_model.decoder.up.3.block.1.norm1.weight": "blocks.4.norm1.weight",
|
|
314
|
+
"first_stage_model.decoder.up.3.block.1.norm2.bias": "blocks.4.norm2.bias",
|
|
315
|
+
"first_stage_model.decoder.up.3.block.1.norm2.weight": "blocks.4.norm2.weight",
|
|
316
|
+
"first_stage_model.decoder.up.3.block.2.conv1.bias": "blocks.5.conv1.bias",
|
|
317
|
+
"first_stage_model.decoder.up.3.block.2.conv1.weight": "blocks.5.conv1.weight",
|
|
318
|
+
"first_stage_model.decoder.up.3.block.2.conv2.bias": "blocks.5.conv2.bias",
|
|
319
|
+
"first_stage_model.decoder.up.3.block.2.conv2.weight": "blocks.5.conv2.weight",
|
|
320
|
+
"first_stage_model.decoder.up.3.block.2.norm1.bias": "blocks.5.norm1.bias",
|
|
321
|
+
"first_stage_model.decoder.up.3.block.2.norm1.weight": "blocks.5.norm1.weight",
|
|
322
|
+
"first_stage_model.decoder.up.3.block.2.norm2.bias": "blocks.5.norm2.bias",
|
|
323
|
+
"first_stage_model.decoder.up.3.block.2.norm2.weight": "blocks.5.norm2.weight",
|
|
324
|
+
"first_stage_model.decoder.up.3.upsample.conv.bias": "blocks.6.conv.bias",
|
|
325
|
+
"first_stage_model.decoder.up.3.upsample.conv.weight": "blocks.6.conv.weight",
|
|
326
|
+
"first_stage_model.post_quant_conv.bias": "post_quant_conv.bias",
|
|
327
|
+
"first_stage_model.post_quant_conv.weight": "post_quant_conv.weight",
|
|
328
|
+
}
|
|
329
|
+
state_dict_ = {}
|
|
330
|
+
for name in state_dict:
|
|
331
|
+
if name in rename_dict:
|
|
332
|
+
param = state_dict[name]
|
|
333
|
+
if "transformer_blocks" in rename_dict[name]:
|
|
334
|
+
param = param.squeeze()
|
|
335
|
+
state_dict_[rename_dict[name]] = param
|
|
336
|
+
return state_dict_
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .sd_unet import ResnetBlock, DownSampler
|
|
3
|
+
from .sd_vae_decoder import VAEAttentionBlock
|
|
4
|
+
from .tiler import TileWorker
|
|
5
|
+
from einops import rearrange
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class SDVAEEncoder(torch.nn.Module):
|
|
9
|
+
def __init__(self):
|
|
10
|
+
super().__init__()
|
|
11
|
+
self.scaling_factor = 0.18215
|
|
12
|
+
self.quant_conv = torch.nn.Conv2d(8, 8, kernel_size=1)
|
|
13
|
+
self.conv_in = torch.nn.Conv2d(3, 128, kernel_size=3, padding=1)
|
|
14
|
+
|
|
15
|
+
self.blocks = torch.nn.ModuleList([
|
|
16
|
+
# DownEncoderBlock2D
|
|
17
|
+
ResnetBlock(128, 128, eps=1e-6),
|
|
18
|
+
ResnetBlock(128, 128, eps=1e-6),
|
|
19
|
+
DownSampler(128, padding=0, extra_padding=True),
|
|
20
|
+
# DownEncoderBlock2D
|
|
21
|
+
ResnetBlock(128, 256, eps=1e-6),
|
|
22
|
+
ResnetBlock(256, 256, eps=1e-6),
|
|
23
|
+
DownSampler(256, padding=0, extra_padding=True),
|
|
24
|
+
# DownEncoderBlock2D
|
|
25
|
+
ResnetBlock(256, 512, eps=1e-6),
|
|
26
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
27
|
+
DownSampler(512, padding=0, extra_padding=True),
|
|
28
|
+
# DownEncoderBlock2D
|
|
29
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
30
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
31
|
+
# UNetMidBlock2D
|
|
32
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
33
|
+
VAEAttentionBlock(1, 512, 512, 1, eps=1e-6),
|
|
34
|
+
ResnetBlock(512, 512, eps=1e-6),
|
|
35
|
+
])
|
|
36
|
+
|
|
37
|
+
self.conv_norm_out = torch.nn.GroupNorm(num_channels=512, num_groups=32, eps=1e-6)
|
|
38
|
+
self.conv_act = torch.nn.SiLU()
|
|
39
|
+
self.conv_out = torch.nn.Conv2d(512, 8, kernel_size=3, padding=1)
|
|
40
|
+
|
|
41
|
+
def tiled_forward(self, sample, tile_size=64, tile_stride=32):
|
|
42
|
+
hidden_states = TileWorker().tiled_forward(
|
|
43
|
+
lambda x: self.forward(x),
|
|
44
|
+
sample,
|
|
45
|
+
tile_size,
|
|
46
|
+
tile_stride,
|
|
47
|
+
tile_device=sample.device,
|
|
48
|
+
tile_dtype=sample.dtype
|
|
49
|
+
)
|
|
50
|
+
return hidden_states
|
|
51
|
+
|
|
52
|
+
def forward(self, sample, tiled=False, tile_size=64, tile_stride=32, **kwargs):
|
|
53
|
+
original_dtype = sample.dtype
|
|
54
|
+
sample = sample.to(dtype=next(iter(self.parameters())).dtype)
|
|
55
|
+
# For VAE Decoder, we do not need to apply the tiler on each layer.
|
|
56
|
+
if tiled:
|
|
57
|
+
return self.tiled_forward(sample, tile_size=tile_size, tile_stride=tile_stride)
|
|
58
|
+
|
|
59
|
+
# 1. pre-process
|
|
60
|
+
hidden_states = self.conv_in(sample)
|
|
61
|
+
time_emb = None
|
|
62
|
+
text_emb = None
|
|
63
|
+
res_stack = None
|
|
64
|
+
|
|
65
|
+
# 2. blocks
|
|
66
|
+
for i, block in enumerate(self.blocks):
|
|
67
|
+
hidden_states, time_emb, text_emb, res_stack = block(hidden_states, time_emb, text_emb, res_stack)
|
|
68
|
+
|
|
69
|
+
# 3. output
|
|
70
|
+
hidden_states = self.conv_norm_out(hidden_states)
|
|
71
|
+
hidden_states = self.conv_act(hidden_states)
|
|
72
|
+
hidden_states = self.conv_out(hidden_states)
|
|
73
|
+
hidden_states = self.quant_conv(hidden_states)
|
|
74
|
+
hidden_states = hidden_states[:, :4]
|
|
75
|
+
hidden_states *= self.scaling_factor
|
|
76
|
+
hidden_states = hidden_states.to(original_dtype)
|
|
77
|
+
|
|
78
|
+
return hidden_states
|
|
79
|
+
|
|
80
|
+
def encode_video(self, sample, batch_size=8):
|
|
81
|
+
B = sample.shape[0]
|
|
82
|
+
hidden_states = []
|
|
83
|
+
|
|
84
|
+
for i in range(0, sample.shape[2], batch_size):
|
|
85
|
+
|
|
86
|
+
j = min(i + batch_size, sample.shape[2])
|
|
87
|
+
sample_batch = rearrange(sample[:,:,i:j], "B C T H W -> (B T) C H W")
|
|
88
|
+
|
|
89
|
+
hidden_states_batch = self(sample_batch)
|
|
90
|
+
hidden_states_batch = rearrange(hidden_states_batch, "(B T) C H W -> B C T H W", B=B)
|
|
91
|
+
|
|
92
|
+
hidden_states.append(hidden_states_batch)
|
|
93
|
+
|
|
94
|
+
hidden_states = torch.concat(hidden_states, dim=2)
|
|
95
|
+
return hidden_states
|
|
96
|
+
|
|
97
|
+
@staticmethod
|
|
98
|
+
def state_dict_converter():
|
|
99
|
+
return SDVAEEncoderStateDictConverter()
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class SDVAEEncoderStateDictConverter:
|
|
103
|
+
def __init__(self):
|
|
104
|
+
pass
|
|
105
|
+
|
|
106
|
+
def from_diffusers(self, state_dict):
|
|
107
|
+
# architecture
|
|
108
|
+
block_types = [
|
|
109
|
+
'ResnetBlock', 'ResnetBlock', 'DownSampler',
|
|
110
|
+
'ResnetBlock', 'ResnetBlock', 'DownSampler',
|
|
111
|
+
'ResnetBlock', 'ResnetBlock', 'DownSampler',
|
|
112
|
+
'ResnetBlock', 'ResnetBlock',
|
|
113
|
+
'ResnetBlock', 'VAEAttentionBlock', 'ResnetBlock'
|
|
114
|
+
]
|
|
115
|
+
|
|
116
|
+
# Rename each parameter
|
|
117
|
+
local_rename_dict = {
|
|
118
|
+
"quant_conv": "quant_conv",
|
|
119
|
+
"encoder.conv_in": "conv_in",
|
|
120
|
+
"encoder.mid_block.attentions.0.group_norm": "blocks.12.norm",
|
|
121
|
+
"encoder.mid_block.attentions.0.to_q": "blocks.12.transformer_blocks.0.to_q",
|
|
122
|
+
"encoder.mid_block.attentions.0.to_k": "blocks.12.transformer_blocks.0.to_k",
|
|
123
|
+
"encoder.mid_block.attentions.0.to_v": "blocks.12.transformer_blocks.0.to_v",
|
|
124
|
+
"encoder.mid_block.attentions.0.to_out.0": "blocks.12.transformer_blocks.0.to_out",
|
|
125
|
+
"encoder.mid_block.resnets.0.norm1": "blocks.11.norm1",
|
|
126
|
+
"encoder.mid_block.resnets.0.conv1": "blocks.11.conv1",
|
|
127
|
+
"encoder.mid_block.resnets.0.norm2": "blocks.11.norm2",
|
|
128
|
+
"encoder.mid_block.resnets.0.conv2": "blocks.11.conv2",
|
|
129
|
+
"encoder.mid_block.resnets.1.norm1": "blocks.13.norm1",
|
|
130
|
+
"encoder.mid_block.resnets.1.conv1": "blocks.13.conv1",
|
|
131
|
+
"encoder.mid_block.resnets.1.norm2": "blocks.13.norm2",
|
|
132
|
+
"encoder.mid_block.resnets.1.conv2": "blocks.13.conv2",
|
|
133
|
+
"encoder.conv_norm_out": "conv_norm_out",
|
|
134
|
+
"encoder.conv_out": "conv_out",
|
|
135
|
+
}
|
|
136
|
+
name_list = sorted([name for name in state_dict])
|
|
137
|
+
rename_dict = {}
|
|
138
|
+
block_id = {"ResnetBlock": -1, "DownSampler": -1, "UpSampler": -1}
|
|
139
|
+
last_block_type_with_id = {"ResnetBlock": "", "DownSampler": "", "UpSampler": ""}
|
|
140
|
+
for name in name_list:
|
|
141
|
+
names = name.split(".")
|
|
142
|
+
name_prefix = ".".join(names[:-1])
|
|
143
|
+
if name_prefix in local_rename_dict:
|
|
144
|
+
rename_dict[name] = local_rename_dict[name_prefix] + "." + names[-1]
|
|
145
|
+
elif name.startswith("encoder.down_blocks"):
|
|
146
|
+
block_type = {"resnets": "ResnetBlock", "downsamplers": "DownSampler", "upsamplers": "UpSampler"}[names[3]]
|
|
147
|
+
block_type_with_id = ".".join(names[:5])
|
|
148
|
+
if block_type_with_id != last_block_type_with_id[block_type]:
|
|
149
|
+
block_id[block_type] += 1
|
|
150
|
+
last_block_type_with_id[block_type] = block_type_with_id
|
|
151
|
+
while block_id[block_type] < len(block_types) and block_types[block_id[block_type]] != block_type:
|
|
152
|
+
block_id[block_type] += 1
|
|
153
|
+
block_type_with_id = ".".join(names[:5])
|
|
154
|
+
names = ["blocks", str(block_id[block_type])] + names[5:]
|
|
155
|
+
rename_dict[name] = ".".join(names)
|
|
156
|
+
|
|
157
|
+
# Convert state_dict
|
|
158
|
+
state_dict_ = {}
|
|
159
|
+
for name, param in state_dict.items():
|
|
160
|
+
if name in rename_dict:
|
|
161
|
+
state_dict_[rename_dict[name]] = param
|
|
162
|
+
return state_dict_
|
|
163
|
+
|
|
164
|
+
def from_civitai(self, state_dict):
|
|
165
|
+
rename_dict = {
|
|
166
|
+
"first_stage_model.encoder.conv_in.bias": "conv_in.bias",
|
|
167
|
+
"first_stage_model.encoder.conv_in.weight": "conv_in.weight",
|
|
168
|
+
"first_stage_model.encoder.conv_out.bias": "conv_out.bias",
|
|
169
|
+
"first_stage_model.encoder.conv_out.weight": "conv_out.weight",
|
|
170
|
+
"first_stage_model.encoder.down.0.block.0.conv1.bias": "blocks.0.conv1.bias",
|
|
171
|
+
"first_stage_model.encoder.down.0.block.0.conv1.weight": "blocks.0.conv1.weight",
|
|
172
|
+
"first_stage_model.encoder.down.0.block.0.conv2.bias": "blocks.0.conv2.bias",
|
|
173
|
+
"first_stage_model.encoder.down.0.block.0.conv2.weight": "blocks.0.conv2.weight",
|
|
174
|
+
"first_stage_model.encoder.down.0.block.0.norm1.bias": "blocks.0.norm1.bias",
|
|
175
|
+
"first_stage_model.encoder.down.0.block.0.norm1.weight": "blocks.0.norm1.weight",
|
|
176
|
+
"first_stage_model.encoder.down.0.block.0.norm2.bias": "blocks.0.norm2.bias",
|
|
177
|
+
"first_stage_model.encoder.down.0.block.0.norm2.weight": "blocks.0.norm2.weight",
|
|
178
|
+
"first_stage_model.encoder.down.0.block.1.conv1.bias": "blocks.1.conv1.bias",
|
|
179
|
+
"first_stage_model.encoder.down.0.block.1.conv1.weight": "blocks.1.conv1.weight",
|
|
180
|
+
"first_stage_model.encoder.down.0.block.1.conv2.bias": "blocks.1.conv2.bias",
|
|
181
|
+
"first_stage_model.encoder.down.0.block.1.conv2.weight": "blocks.1.conv2.weight",
|
|
182
|
+
"first_stage_model.encoder.down.0.block.1.norm1.bias": "blocks.1.norm1.bias",
|
|
183
|
+
"first_stage_model.encoder.down.0.block.1.norm1.weight": "blocks.1.norm1.weight",
|
|
184
|
+
"first_stage_model.encoder.down.0.block.1.norm2.bias": "blocks.1.norm2.bias",
|
|
185
|
+
"first_stage_model.encoder.down.0.block.1.norm2.weight": "blocks.1.norm2.weight",
|
|
186
|
+
"first_stage_model.encoder.down.0.downsample.conv.bias": "blocks.2.conv.bias",
|
|
187
|
+
"first_stage_model.encoder.down.0.downsample.conv.weight": "blocks.2.conv.weight",
|
|
188
|
+
"first_stage_model.encoder.down.1.block.0.conv1.bias": "blocks.3.conv1.bias",
|
|
189
|
+
"first_stage_model.encoder.down.1.block.0.conv1.weight": "blocks.3.conv1.weight",
|
|
190
|
+
"first_stage_model.encoder.down.1.block.0.conv2.bias": "blocks.3.conv2.bias",
|
|
191
|
+
"first_stage_model.encoder.down.1.block.0.conv2.weight": "blocks.3.conv2.weight",
|
|
192
|
+
"first_stage_model.encoder.down.1.block.0.nin_shortcut.bias": "blocks.3.conv_shortcut.bias",
|
|
193
|
+
"first_stage_model.encoder.down.1.block.0.nin_shortcut.weight": "blocks.3.conv_shortcut.weight",
|
|
194
|
+
"first_stage_model.encoder.down.1.block.0.norm1.bias": "blocks.3.norm1.bias",
|
|
195
|
+
"first_stage_model.encoder.down.1.block.0.norm1.weight": "blocks.3.norm1.weight",
|
|
196
|
+
"first_stage_model.encoder.down.1.block.0.norm2.bias": "blocks.3.norm2.bias",
|
|
197
|
+
"first_stage_model.encoder.down.1.block.0.norm2.weight": "blocks.3.norm2.weight",
|
|
198
|
+
"first_stage_model.encoder.down.1.block.1.conv1.bias": "blocks.4.conv1.bias",
|
|
199
|
+
"first_stage_model.encoder.down.1.block.1.conv1.weight": "blocks.4.conv1.weight",
|
|
200
|
+
"first_stage_model.encoder.down.1.block.1.conv2.bias": "blocks.4.conv2.bias",
|
|
201
|
+
"first_stage_model.encoder.down.1.block.1.conv2.weight": "blocks.4.conv2.weight",
|
|
202
|
+
"first_stage_model.encoder.down.1.block.1.norm1.bias": "blocks.4.norm1.bias",
|
|
203
|
+
"first_stage_model.encoder.down.1.block.1.norm1.weight": "blocks.4.norm1.weight",
|
|
204
|
+
"first_stage_model.encoder.down.1.block.1.norm2.bias": "blocks.4.norm2.bias",
|
|
205
|
+
"first_stage_model.encoder.down.1.block.1.norm2.weight": "blocks.4.norm2.weight",
|
|
206
|
+
"first_stage_model.encoder.down.1.downsample.conv.bias": "blocks.5.conv.bias",
|
|
207
|
+
"first_stage_model.encoder.down.1.downsample.conv.weight": "blocks.5.conv.weight",
|
|
208
|
+
"first_stage_model.encoder.down.2.block.0.conv1.bias": "blocks.6.conv1.bias",
|
|
209
|
+
"first_stage_model.encoder.down.2.block.0.conv1.weight": "blocks.6.conv1.weight",
|
|
210
|
+
"first_stage_model.encoder.down.2.block.0.conv2.bias": "blocks.6.conv2.bias",
|
|
211
|
+
"first_stage_model.encoder.down.2.block.0.conv2.weight": "blocks.6.conv2.weight",
|
|
212
|
+
"first_stage_model.encoder.down.2.block.0.nin_shortcut.bias": "blocks.6.conv_shortcut.bias",
|
|
213
|
+
"first_stage_model.encoder.down.2.block.0.nin_shortcut.weight": "blocks.6.conv_shortcut.weight",
|
|
214
|
+
"first_stage_model.encoder.down.2.block.0.norm1.bias": "blocks.6.norm1.bias",
|
|
215
|
+
"first_stage_model.encoder.down.2.block.0.norm1.weight": "blocks.6.norm1.weight",
|
|
216
|
+
"first_stage_model.encoder.down.2.block.0.norm2.bias": "blocks.6.norm2.bias",
|
|
217
|
+
"first_stage_model.encoder.down.2.block.0.norm2.weight": "blocks.6.norm2.weight",
|
|
218
|
+
"first_stage_model.encoder.down.2.block.1.conv1.bias": "blocks.7.conv1.bias",
|
|
219
|
+
"first_stage_model.encoder.down.2.block.1.conv1.weight": "blocks.7.conv1.weight",
|
|
220
|
+
"first_stage_model.encoder.down.2.block.1.conv2.bias": "blocks.7.conv2.bias",
|
|
221
|
+
"first_stage_model.encoder.down.2.block.1.conv2.weight": "blocks.7.conv2.weight",
|
|
222
|
+
"first_stage_model.encoder.down.2.block.1.norm1.bias": "blocks.7.norm1.bias",
|
|
223
|
+
"first_stage_model.encoder.down.2.block.1.norm1.weight": "blocks.7.norm1.weight",
|
|
224
|
+
"first_stage_model.encoder.down.2.block.1.norm2.bias": "blocks.7.norm2.bias",
|
|
225
|
+
"first_stage_model.encoder.down.2.block.1.norm2.weight": "blocks.7.norm2.weight",
|
|
226
|
+
"first_stage_model.encoder.down.2.downsample.conv.bias": "blocks.8.conv.bias",
|
|
227
|
+
"first_stage_model.encoder.down.2.downsample.conv.weight": "blocks.8.conv.weight",
|
|
228
|
+
"first_stage_model.encoder.down.3.block.0.conv1.bias": "blocks.9.conv1.bias",
|
|
229
|
+
"first_stage_model.encoder.down.3.block.0.conv1.weight": "blocks.9.conv1.weight",
|
|
230
|
+
"first_stage_model.encoder.down.3.block.0.conv2.bias": "blocks.9.conv2.bias",
|
|
231
|
+
"first_stage_model.encoder.down.3.block.0.conv2.weight": "blocks.9.conv2.weight",
|
|
232
|
+
"first_stage_model.encoder.down.3.block.0.norm1.bias": "blocks.9.norm1.bias",
|
|
233
|
+
"first_stage_model.encoder.down.3.block.0.norm1.weight": "blocks.9.norm1.weight",
|
|
234
|
+
"first_stage_model.encoder.down.3.block.0.norm2.bias": "blocks.9.norm2.bias",
|
|
235
|
+
"first_stage_model.encoder.down.3.block.0.norm2.weight": "blocks.9.norm2.weight",
|
|
236
|
+
"first_stage_model.encoder.down.3.block.1.conv1.bias": "blocks.10.conv1.bias",
|
|
237
|
+
"first_stage_model.encoder.down.3.block.1.conv1.weight": "blocks.10.conv1.weight",
|
|
238
|
+
"first_stage_model.encoder.down.3.block.1.conv2.bias": "blocks.10.conv2.bias",
|
|
239
|
+
"first_stage_model.encoder.down.3.block.1.conv2.weight": "blocks.10.conv2.weight",
|
|
240
|
+
"first_stage_model.encoder.down.3.block.1.norm1.bias": "blocks.10.norm1.bias",
|
|
241
|
+
"first_stage_model.encoder.down.3.block.1.norm1.weight": "blocks.10.norm1.weight",
|
|
242
|
+
"first_stage_model.encoder.down.3.block.1.norm2.bias": "blocks.10.norm2.bias",
|
|
243
|
+
"first_stage_model.encoder.down.3.block.1.norm2.weight": "blocks.10.norm2.weight",
|
|
244
|
+
"first_stage_model.encoder.mid.attn_1.k.bias": "blocks.12.transformer_blocks.0.to_k.bias",
|
|
245
|
+
"first_stage_model.encoder.mid.attn_1.k.weight": "blocks.12.transformer_blocks.0.to_k.weight",
|
|
246
|
+
"first_stage_model.encoder.mid.attn_1.norm.bias": "blocks.12.norm.bias",
|
|
247
|
+
"first_stage_model.encoder.mid.attn_1.norm.weight": "blocks.12.norm.weight",
|
|
248
|
+
"first_stage_model.encoder.mid.attn_1.proj_out.bias": "blocks.12.transformer_blocks.0.to_out.bias",
|
|
249
|
+
"first_stage_model.encoder.mid.attn_1.proj_out.weight": "blocks.12.transformer_blocks.0.to_out.weight",
|
|
250
|
+
"first_stage_model.encoder.mid.attn_1.q.bias": "blocks.12.transformer_blocks.0.to_q.bias",
|
|
251
|
+
"first_stage_model.encoder.mid.attn_1.q.weight": "blocks.12.transformer_blocks.0.to_q.weight",
|
|
252
|
+
"first_stage_model.encoder.mid.attn_1.v.bias": "blocks.12.transformer_blocks.0.to_v.bias",
|
|
253
|
+
"first_stage_model.encoder.mid.attn_1.v.weight": "blocks.12.transformer_blocks.0.to_v.weight",
|
|
254
|
+
"first_stage_model.encoder.mid.block_1.conv1.bias": "blocks.11.conv1.bias",
|
|
255
|
+
"first_stage_model.encoder.mid.block_1.conv1.weight": "blocks.11.conv1.weight",
|
|
256
|
+
"first_stage_model.encoder.mid.block_1.conv2.bias": "blocks.11.conv2.bias",
|
|
257
|
+
"first_stage_model.encoder.mid.block_1.conv2.weight": "blocks.11.conv2.weight",
|
|
258
|
+
"first_stage_model.encoder.mid.block_1.norm1.bias": "blocks.11.norm1.bias",
|
|
259
|
+
"first_stage_model.encoder.mid.block_1.norm1.weight": "blocks.11.norm1.weight",
|
|
260
|
+
"first_stage_model.encoder.mid.block_1.norm2.bias": "blocks.11.norm2.bias",
|
|
261
|
+
"first_stage_model.encoder.mid.block_1.norm2.weight": "blocks.11.norm2.weight",
|
|
262
|
+
"first_stage_model.encoder.mid.block_2.conv1.bias": "blocks.13.conv1.bias",
|
|
263
|
+
"first_stage_model.encoder.mid.block_2.conv1.weight": "blocks.13.conv1.weight",
|
|
264
|
+
"first_stage_model.encoder.mid.block_2.conv2.bias": "blocks.13.conv2.bias",
|
|
265
|
+
"first_stage_model.encoder.mid.block_2.conv2.weight": "blocks.13.conv2.weight",
|
|
266
|
+
"first_stage_model.encoder.mid.block_2.norm1.bias": "blocks.13.norm1.bias",
|
|
267
|
+
"first_stage_model.encoder.mid.block_2.norm1.weight": "blocks.13.norm1.weight",
|
|
268
|
+
"first_stage_model.encoder.mid.block_2.norm2.bias": "blocks.13.norm2.bias",
|
|
269
|
+
"first_stage_model.encoder.mid.block_2.norm2.weight": "blocks.13.norm2.weight",
|
|
270
|
+
"first_stage_model.encoder.norm_out.bias": "conv_norm_out.bias",
|
|
271
|
+
"first_stage_model.encoder.norm_out.weight": "conv_norm_out.weight",
|
|
272
|
+
"first_stage_model.quant_conv.bias": "quant_conv.bias",
|
|
273
|
+
"first_stage_model.quant_conv.weight": "quant_conv.weight",
|
|
274
|
+
}
|
|
275
|
+
state_dict_ = {}
|
|
276
|
+
for name in state_dict:
|
|
277
|
+
if name in rename_dict:
|
|
278
|
+
param = state_dict[name]
|
|
279
|
+
if "transformer_blocks" in rename_dict[name]:
|
|
280
|
+
param = param.squeeze()
|
|
281
|
+
state_dict_[rename_dict[name]] = param
|
|
282
|
+
return state_dict_
|