Skip to content

Commit 726769d

Browse files
committed
Checkpoint cache by combination key of checkpoint and vae
1 parent b96d0c4 commit 726769d

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

modules/sd_models.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,15 @@ def get_state_dict_from_checkpoint(pl_sd):
160160

161161
vae_ignore_keys = {"model_ema.decay", "model_ema.num_updates"}
162162

163-
def load_model_weights(model, checkpoint_info, force=False):
163+
def load_model_weights(model, checkpoint_info, vae_file="auto"):
164164
checkpoint_file = checkpoint_info.filename
165165
sd_model_hash = checkpoint_info.hash
166166

167-
if force or checkpoint_info not in checkpoints_loaded:
167+
vae_file = sd_vae.resolve_vae(checkpoint_file, vae_file=vae_file)
168+
169+
checkpoint_key = (checkpoint_info, vae_file)
170+
171+
if checkpoint_key not in checkpoints_loaded:
168172
print(f"Loading weights [{sd_model_hash}] from {checkpoint_file}")
169173

170174
pl_sd = torch.load(checkpoint_file, map_location=shared.weight_load_location)
@@ -185,24 +189,25 @@ def load_model_weights(model, checkpoint_info, force=False):
185189
devices.dtype = torch.float32 if shared.cmd_opts.no_half else torch.float16
186190
devices.dtype_vae = torch.float32 if shared.cmd_opts.no_half or shared.cmd_opts.no_half_vae else torch.float16
187191

188-
sd_vae.load_vae(model, checkpoint_file)
192+
sd_vae.load_vae(model, vae_file)
189193
model.first_stage_model.to(devices.dtype_vae)
190194

191195
if shared.opts.sd_checkpoint_cache > 0:
192-
checkpoints_loaded[checkpoint_info] = model.state_dict().copy()
196+
checkpoints_loaded[checkpoint_key] = model.state_dict().copy()
193197
while len(checkpoints_loaded) > shared.opts.sd_checkpoint_cache:
194198
checkpoints_loaded.popitem(last=False) # LRU
195199
else:
196-
print(f"Loading weights [{sd_model_hash}] from cache")
197-
checkpoints_loaded.move_to_end(checkpoint_info)
198-
model.load_state_dict(checkpoints_loaded[checkpoint_info])
200+
vae_name = sd_vae.get_filename(vae_file)
201+
print(f"Loading weights [{sd_model_hash}] with {vae_name} VAE from cache")
202+
checkpoints_loaded.move_to_end(checkpoint_key)
203+
model.load_state_dict(checkpoints_loaded[checkpoint_key])
199204

200205
model.sd_model_hash = sd_model_hash
201206
model.sd_model_checkpoint = checkpoint_file
202207
model.sd_checkpoint_info = checkpoint_info
203208

204209

205-
def load_model(checkpoint_info=None, force=False):
210+
def load_model(checkpoint_info=None):
206211
from modules import lowvram, sd_hijack
207212
checkpoint_info = checkpoint_info or select_checkpoint()
208213

@@ -223,7 +228,7 @@ def load_model(checkpoint_info=None, force=False):
223228

224229
do_inpainting_hijack()
225230
sd_model = instantiate_from_config(sd_config.model)
226-
load_model_weights(sd_model, checkpoint_info, force=force)
231+
load_model_weights(sd_model, checkpoint_info)
227232

228233
if shared.cmd_opts.lowvram or shared.cmd_opts.medvram:
229234
lowvram.setup_for_low_vram(sd_model, shared.cmd_opts.medvram)
@@ -250,7 +255,7 @@ def reload_model_weights(sd_model, info=None, force=False):
250255

251256
if sd_model.sd_checkpoint_info.config != checkpoint_info.config or should_hijack_inpainting(checkpoint_info) != should_hijack_inpainting(sd_model.sd_checkpoint_info):
252257
checkpoints_loaded.clear()
253-
load_model(checkpoint_info, force=force)
258+
load_model(checkpoint_info)
254259
return shared.sd_model
255260

256261
if shared.cmd_opts.lowvram or shared.cmd_opts.medvram:
@@ -260,7 +265,7 @@ def reload_model_weights(sd_model, info=None, force=False):
260265

261266
sd_hijack.model_hijack.undo_hijack(sd_model)
262267

263-
load_model_weights(sd_model, checkpoint_info, force=force)
268+
load_model_weights(sd_model, checkpoint_info)
264269

265270
sd_hijack.model_hijack.hijack(sd_model)
266271
script_callbacks.model_loaded_callback(sd_model)

modules/sd_vae.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def refresh_vae_list(vae_path=vae_path, model_path=model_path):
4343
vae_dict.update(res)
4444
return vae_list
4545

46-
def load_vae(model, checkpoint_file, vae_file="auto"):
46+
def resolve_vae(checkpoint_file, vae_file="auto"):
4747
global first_load, vae_dict, vae_list
4848
# save_settings = False
4949

@@ -94,6 +94,12 @@ def load_vae(model, checkpoint_file, vae_file="auto"):
9494
if vae_file and not os.path.exists(vae_file):
9595
vae_file = None
9696

97+
return vae_file
98+
99+
def load_vae(model, vae_file):
100+
global first_load, vae_dict, vae_list
101+
# save_settings = False
102+
97103
if vae_file:
98104
print(f"Loading VAE weights from: {vae_file}")
99105
vae_ckpt = torch.load(vae_file, map_location=shared.weight_load_location)

0 commit comments

Comments
 (0)