Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
automatic1111
GitHub Repository: automatic1111/stable-diffusion-webui
Path: blob/master/modules/extras.py
3055 views
1
import os
2
import re
3
import shutil
4
import json
5
6
7
import torch
8
import tqdm
9
10
from modules import shared, images, sd_models, sd_vae, sd_models_config, errors
11
from modules.ui_common import plaintext_to_html
12
import gradio as gr
13
import safetensors.torch
14
15
16
def run_pnginfo(image):
17
if image is None:
18
return '', '', ''
19
20
geninfo, items = images.read_info_from_image(image)
21
items = {**{'parameters': geninfo}, **items}
22
23
info = ''
24
for key, text in items.items():
25
info += f"""
26
<div>
27
<p><b>{plaintext_to_html(str(key))}</b></p>
28
<p>{plaintext_to_html(str(text))}</p>
29
</div>
30
""".strip()+"\n"
31
32
if len(info) == 0:
33
message = "Nothing found in the image."
34
info = f"<div><p>{message}<p></div>"
35
36
return '', geninfo, info
37
38
39
def create_config(ckpt_result, config_source, a, b, c):
40
def config(x):
41
res = sd_models_config.find_checkpoint_config_near_filename(x) if x else None
42
return res if res != shared.sd_default_config else None
43
44
if config_source == 0:
45
cfg = config(a) or config(b) or config(c)
46
elif config_source == 1:
47
cfg = config(b)
48
elif config_source == 2:
49
cfg = config(c)
50
else:
51
cfg = None
52
53
if cfg is None:
54
return
55
56
filename, _ = os.path.splitext(ckpt_result)
57
checkpoint_filename = filename + ".yaml"
58
59
print("Copying config:")
60
print(" from:", cfg)
61
print(" to:", checkpoint_filename)
62
shutil.copyfile(cfg, checkpoint_filename)
63
64
65
checkpoint_dict_skip_on_merge = ["cond_stage_model.transformer.text_model.embeddings.position_ids"]
66
67
68
def to_half(tensor, enable):
69
if enable and tensor.dtype == torch.float:
70
return tensor.half()
71
72
return tensor
73
74
75
def read_metadata(primary_model_name, secondary_model_name, tertiary_model_name):
76
metadata = {}
77
78
for checkpoint_name in [primary_model_name, secondary_model_name, tertiary_model_name]:
79
checkpoint_info = sd_models.checkpoints_list.get(checkpoint_name, None)
80
if checkpoint_info is None:
81
continue
82
83
metadata.update(checkpoint_info.metadata)
84
85
return json.dumps(metadata, indent=4, ensure_ascii=False)
86
87
88
def run_modelmerger(id_task, primary_model_name, secondary_model_name, tertiary_model_name, interp_method, multiplier, save_as_half, custom_name, checkpoint_format, config_source, bake_in_vae, discard_weights, save_metadata, add_merge_recipe, copy_metadata_fields, metadata_json):
89
shared.state.begin(job="model-merge")
90
91
def fail(message):
92
shared.state.textinfo = message
93
shared.state.end()
94
return [*[gr.update() for _ in range(4)], message]
95
96
def weighted_sum(theta0, theta1, alpha):
97
return ((1 - alpha) * theta0) + (alpha * theta1)
98
99
def get_difference(theta1, theta2):
100
return theta1 - theta2
101
102
def add_difference(theta0, theta1_2_diff, alpha):
103
return theta0 + (alpha * theta1_2_diff)
104
105
def filename_weighted_sum():
106
a = primary_model_info.model_name
107
b = secondary_model_info.model_name
108
Ma = round(1 - multiplier, 2)
109
Mb = round(multiplier, 2)
110
111
return f"{Ma}({a}) + {Mb}({b})"
112
113
def filename_add_difference():
114
a = primary_model_info.model_name
115
b = secondary_model_info.model_name
116
c = tertiary_model_info.model_name
117
M = round(multiplier, 2)
118
119
return f"{a} + {M}({b} - {c})"
120
121
def filename_nothing():
122
return primary_model_info.model_name
123
124
theta_funcs = {
125
"Weighted sum": (filename_weighted_sum, None, weighted_sum),
126
"Add difference": (filename_add_difference, get_difference, add_difference),
127
"No interpolation": (filename_nothing, None, None),
128
}
129
filename_generator, theta_func1, theta_func2 = theta_funcs[interp_method]
130
shared.state.job_count = (1 if theta_func1 else 0) + (1 if theta_func2 else 0)
131
132
if not primary_model_name:
133
return fail("Failed: Merging requires a primary model.")
134
135
primary_model_info = sd_models.checkpoints_list[primary_model_name]
136
137
if theta_func2 and not secondary_model_name:
138
return fail("Failed: Merging requires a secondary model.")
139
140
secondary_model_info = sd_models.checkpoints_list[secondary_model_name] if theta_func2 else None
141
142
if theta_func1 and not tertiary_model_name:
143
return fail(f"Failed: Interpolation method ({interp_method}) requires a tertiary model.")
144
145
tertiary_model_info = sd_models.checkpoints_list[tertiary_model_name] if theta_func1 else None
146
147
result_is_inpainting_model = False
148
result_is_instruct_pix2pix_model = False
149
150
if theta_func2:
151
shared.state.textinfo = "Loading B"
152
print(f"Loading {secondary_model_info.filename}...")
153
theta_1 = sd_models.read_state_dict(secondary_model_info.filename, map_location='cpu')
154
else:
155
theta_1 = None
156
157
if theta_func1:
158
shared.state.textinfo = "Loading C"
159
print(f"Loading {tertiary_model_info.filename}...")
160
theta_2 = sd_models.read_state_dict(tertiary_model_info.filename, map_location='cpu')
161
162
shared.state.textinfo = 'Merging B and C'
163
shared.state.sampling_steps = len(theta_1.keys())
164
for key in tqdm.tqdm(theta_1.keys()):
165
if key in checkpoint_dict_skip_on_merge:
166
continue
167
168
if 'model' in key:
169
if key in theta_2:
170
t2 = theta_2.get(key, torch.zeros_like(theta_1[key]))
171
theta_1[key] = theta_func1(theta_1[key], t2)
172
else:
173
theta_1[key] = torch.zeros_like(theta_1[key])
174
175
shared.state.sampling_step += 1
176
del theta_2
177
178
shared.state.nextjob()
179
180
shared.state.textinfo = f"Loading {primary_model_info.filename}..."
181
print(f"Loading {primary_model_info.filename}...")
182
theta_0 = sd_models.read_state_dict(primary_model_info.filename, map_location='cpu')
183
184
print("Merging...")
185
shared.state.textinfo = 'Merging A and B'
186
shared.state.sampling_steps = len(theta_0.keys())
187
for key in tqdm.tqdm(theta_0.keys()):
188
if theta_1 and 'model' in key and key in theta_1:
189
190
if key in checkpoint_dict_skip_on_merge:
191
continue
192
193
a = theta_0[key]
194
b = theta_1[key]
195
196
# this enables merging an inpainting model (A) with another one (B);
197
# where normal model would have 4 channels, for latenst space, inpainting model would
198
# have another 4 channels for unmasked picture's latent space, plus one channel for mask, for a total of 9
199
if a.shape != b.shape and a.shape[0:1] + a.shape[2:] == b.shape[0:1] + b.shape[2:]:
200
if a.shape[1] == 4 and b.shape[1] == 9:
201
raise RuntimeError("When merging inpainting model with a normal one, A must be the inpainting model.")
202
if a.shape[1] == 4 and b.shape[1] == 8:
203
raise RuntimeError("When merging instruct-pix2pix model with a normal one, A must be the instruct-pix2pix model.")
204
205
if a.shape[1] == 8 and b.shape[1] == 4:#If we have an Instruct-Pix2Pix model...
206
theta_0[key][:, 0:4, :, :] = theta_func2(a[:, 0:4, :, :], b, multiplier)#Merge only the vectors the models have in common. Otherwise we get an error due to dimension mismatch.
207
result_is_instruct_pix2pix_model = True
208
else:
209
assert a.shape[1] == 9 and b.shape[1] == 4, f"Bad dimensions for merged layer {key}: A={a.shape}, B={b.shape}"
210
theta_0[key][:, 0:4, :, :] = theta_func2(a[:, 0:4, :, :], b, multiplier)
211
result_is_inpainting_model = True
212
else:
213
theta_0[key] = theta_func2(a, b, multiplier)
214
215
theta_0[key] = to_half(theta_0[key], save_as_half)
216
217
shared.state.sampling_step += 1
218
219
del theta_1
220
221
bake_in_vae_filename = sd_vae.vae_dict.get(bake_in_vae, None)
222
if bake_in_vae_filename is not None:
223
print(f"Baking in VAE from {bake_in_vae_filename}")
224
shared.state.textinfo = 'Baking in VAE'
225
vae_dict = sd_vae.load_vae_dict(bake_in_vae_filename, map_location='cpu')
226
227
for key in vae_dict.keys():
228
theta_0_key = 'first_stage_model.' + key
229
if theta_0_key in theta_0:
230
theta_0[theta_0_key] = to_half(vae_dict[key], save_as_half)
231
232
del vae_dict
233
234
if save_as_half and not theta_func2:
235
for key in theta_0.keys():
236
theta_0[key] = to_half(theta_0[key], save_as_half)
237
238
if discard_weights:
239
regex = re.compile(discard_weights)
240
for key in list(theta_0):
241
if re.search(regex, key):
242
theta_0.pop(key, None)
243
244
ckpt_dir = shared.cmd_opts.ckpt_dir or sd_models.model_path
245
246
filename = filename_generator() if custom_name == '' else custom_name
247
filename += ".inpainting" if result_is_inpainting_model else ""
248
filename += ".instruct-pix2pix" if result_is_instruct_pix2pix_model else ""
249
filename += "." + checkpoint_format
250
251
output_modelname = os.path.join(ckpt_dir, filename)
252
253
shared.state.nextjob()
254
shared.state.textinfo = "Saving"
255
print(f"Saving to {output_modelname}...")
256
257
metadata = {}
258
259
if save_metadata and copy_metadata_fields:
260
if primary_model_info:
261
metadata.update(primary_model_info.metadata)
262
if secondary_model_info:
263
metadata.update(secondary_model_info.metadata)
264
if tertiary_model_info:
265
metadata.update(tertiary_model_info.metadata)
266
267
if save_metadata:
268
try:
269
metadata.update(json.loads(metadata_json))
270
except Exception as e:
271
errors.display(e, "readin metadata from json")
272
273
metadata["format"] = "pt"
274
275
if save_metadata and add_merge_recipe:
276
merge_recipe = {
277
"type": "webui", # indicate this model was merged with webui's built-in merger
278
"primary_model_hash": primary_model_info.sha256,
279
"secondary_model_hash": secondary_model_info.sha256 if secondary_model_info else None,
280
"tertiary_model_hash": tertiary_model_info.sha256 if tertiary_model_info else None,
281
"interp_method": interp_method,
282
"multiplier": multiplier,
283
"save_as_half": save_as_half,
284
"custom_name": custom_name,
285
"config_source": config_source,
286
"bake_in_vae": bake_in_vae,
287
"discard_weights": discard_weights,
288
"is_inpainting": result_is_inpainting_model,
289
"is_instruct_pix2pix": result_is_instruct_pix2pix_model
290
}
291
292
sd_merge_models = {}
293
294
def add_model_metadata(checkpoint_info):
295
checkpoint_info.calculate_shorthash()
296
sd_merge_models[checkpoint_info.sha256] = {
297
"name": checkpoint_info.name,
298
"legacy_hash": checkpoint_info.hash,
299
"sd_merge_recipe": checkpoint_info.metadata.get("sd_merge_recipe", None)
300
}
301
302
sd_merge_models.update(checkpoint_info.metadata.get("sd_merge_models", {}))
303
304
add_model_metadata(primary_model_info)
305
if secondary_model_info:
306
add_model_metadata(secondary_model_info)
307
if tertiary_model_info:
308
add_model_metadata(tertiary_model_info)
309
310
metadata["sd_merge_recipe"] = json.dumps(merge_recipe)
311
metadata["sd_merge_models"] = json.dumps(sd_merge_models)
312
313
_, extension = os.path.splitext(output_modelname)
314
if extension.lower() == ".safetensors":
315
safetensors.torch.save_file(theta_0, output_modelname, metadata=metadata if len(metadata)>0 else None)
316
else:
317
torch.save(theta_0, output_modelname)
318
319
sd_models.list_models()
320
created_model = next((ckpt for ckpt in sd_models.checkpoints_list.values() if ckpt.name == filename), None)
321
if created_model:
322
created_model.calculate_shorthash()
323
324
create_config(output_modelname, config_source, primary_model_info, secondary_model_info, tertiary_model_info)
325
326
print(f"Checkpoint saved to {output_modelname}.")
327
shared.state.textinfo = "Checkpoint saved"
328
shared.state.end()
329
330
return [*[gr.Dropdown.update(choices=sd_models.checkpoint_tiles()) for _ in range(4)], "Checkpoint saved to " + output_modelname]
331
332