2022-10-30 14:54:31 +00:00
|
|
|
import torch
|
|
|
|
import os
|
2022-12-25 12:49:25 +00:00
|
|
|
import collections
|
2022-10-30 14:54:31 +00:00
|
|
|
from collections import namedtuple
|
2022-11-02 05:51:46 +00:00
|
|
|
from modules import shared, devices, script_callbacks
|
2022-10-30 14:54:31 +00:00
|
|
|
from modules.paths import models_path
|
|
|
|
import glob
|
2022-11-17 11:04:10 +00:00
|
|
|
from copy import deepcopy
|
2022-10-30 14:54:31 +00:00
|
|
|
|
2022-11-02 05:51:46 +00:00
|
|
|
|
2022-10-30 14:54:31 +00:00
|
|
|
model_dir = "Stable-diffusion"
|
|
|
|
model_path = os.path.abspath(os.path.join(models_path, model_dir))
|
|
|
|
vae_dir = "VAE"
|
|
|
|
vae_path = os.path.abspath(os.path.join(models_path, vae_dir))
|
|
|
|
|
2022-11-02 05:51:46 +00:00
|
|
|
|
2022-10-30 14:54:31 +00:00
|
|
|
vae_ignore_keys = {"model_ema.decay", "model_ema.num_updates"}
|
2022-11-02 05:51:46 +00:00
|
|
|
|
|
|
|
|
2022-11-03 04:10:53 +00:00
|
|
|
default_vae_dict = {"auto": "auto", "None": None, None: None}
|
2022-10-30 14:54:31 +00:00
|
|
|
default_vae_list = ["auto", "None"]
|
2022-11-02 05:51:46 +00:00
|
|
|
|
|
|
|
|
2022-10-30 14:54:31 +00:00
|
|
|
default_vae_values = [default_vae_dict[x] for x in default_vae_list]
|
|
|
|
vae_dict = dict(default_vae_dict)
|
|
|
|
vae_list = list(default_vae_list)
|
|
|
|
first_load = True
|
|
|
|
|
2022-11-02 05:51:46 +00:00
|
|
|
|
|
|
|
base_vae = None
|
|
|
|
loaded_vae_file = None
|
|
|
|
checkpoint_info = None
|
|
|
|
|
2022-12-25 12:49:25 +00:00
|
|
|
checkpoints_loaded = collections.OrderedDict()
|
2022-11-02 05:51:46 +00:00
|
|
|
|
|
|
|
def get_base_vae(model):
|
|
|
|
if base_vae is not None and checkpoint_info == model.sd_checkpoint_info and model:
|
|
|
|
return base_vae
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def store_base_vae(model):
|
|
|
|
global base_vae, checkpoint_info
|
|
|
|
if checkpoint_info != model.sd_checkpoint_info:
|
2022-11-03 04:10:53 +00:00
|
|
|
assert not loaded_vae_file, "Trying to store non-base VAE!"
|
2022-11-17 11:04:10 +00:00
|
|
|
base_vae = deepcopy(model.first_stage_model.state_dict())
|
2022-11-02 05:51:46 +00:00
|
|
|
checkpoint_info = model.sd_checkpoint_info
|
|
|
|
|
|
|
|
|
|
|
|
def delete_base_vae():
|
|
|
|
global base_vae, checkpoint_info
|
|
|
|
base_vae = None
|
|
|
|
checkpoint_info = None
|
|
|
|
|
|
|
|
|
|
|
|
def restore_base_vae(model):
|
2022-11-03 04:10:53 +00:00
|
|
|
global loaded_vae_file
|
2022-11-02 05:51:46 +00:00
|
|
|
if base_vae is not None and checkpoint_info == model.sd_checkpoint_info:
|
2022-11-02 15:54:09 +00:00
|
|
|
print("Restoring base VAE")
|
2022-11-18 18:27:54 +00:00
|
|
|
_load_vae_dict(model, base_vae)
|
2022-11-03 04:10:53 +00:00
|
|
|
loaded_vae_file = None
|
2022-11-02 05:51:46 +00:00
|
|
|
delete_base_vae()
|
|
|
|
|
|
|
|
|
2022-10-30 14:54:31 +00:00
|
|
|
def get_filename(filepath):
|
|
|
|
return os.path.splitext(os.path.basename(filepath))[0]
|
|
|
|
|
2022-11-02 05:51:46 +00:00
|
|
|
|
2022-10-30 14:54:31 +00:00
|
|
|
def refresh_vae_list(vae_path=vae_path, model_path=model_path):
|
|
|
|
global vae_dict, vae_list
|
|
|
|
res = {}
|
|
|
|
candidates = [
|
|
|
|
*glob.iglob(os.path.join(model_path, '**/*.vae.ckpt'), recursive=True),
|
2022-10-30 15:11:45 +00:00
|
|
|
*glob.iglob(os.path.join(model_path, '**/*.vae.pt'), recursive=True),
|
2022-10-31 07:42:28 +00:00
|
|
|
*glob.iglob(os.path.join(vae_path, '**/*.ckpt'), recursive=True),
|
|
|
|
*glob.iglob(os.path.join(vae_path, '**/*.pt'), recursive=True)
|
2022-10-30 14:54:31 +00:00
|
|
|
]
|
|
|
|
if shared.cmd_opts.vae_path is not None and os.path.isfile(shared.cmd_opts.vae_path):
|
|
|
|
candidates.append(shared.cmd_opts.vae_path)
|
|
|
|
for filepath in candidates:
|
|
|
|
name = get_filename(filepath)
|
|
|
|
res[name] = filepath
|
|
|
|
vae_list.clear()
|
|
|
|
vae_list.extend(default_vae_list)
|
|
|
|
vae_list.extend(list(res.keys()))
|
|
|
|
vae_dict.clear()
|
|
|
|
vae_dict.update(res)
|
2022-11-02 07:37:22 +00:00
|
|
|
vae_dict.update(default_vae_dict)
|
2022-10-30 14:54:31 +00:00
|
|
|
return vae_list
|
|
|
|
|
2022-11-02 05:51:46 +00:00
|
|
|
|
2022-11-03 06:47:03 +00:00
|
|
|
def get_vae_from_settings(vae_file="auto"):
|
|
|
|
# else, we load from settings, if not set to be default
|
|
|
|
if vae_file == "auto" and shared.opts.sd_vae is not None:
|
|
|
|
# if saved VAE settings isn't recognized, fallback to auto
|
|
|
|
vae_file = vae_dict.get(shared.opts.sd_vae, "auto")
|
|
|
|
# if VAE selected but not found, fallback to auto
|
|
|
|
if vae_file not in default_vae_values and not os.path.isfile(vae_file):
|
|
|
|
vae_file = "auto"
|
2022-11-19 08:20:07 +00:00
|
|
|
print(f"Selected VAE doesn't exist: {vae_file}")
|
2022-11-03 06:47:03 +00:00
|
|
|
return vae_file
|
|
|
|
|
|
|
|
|
|
|
|
def resolve_vae(checkpoint_file=None, vae_file="auto"):
|
2022-10-30 14:54:31 +00:00
|
|
|
global first_load, vae_dict, vae_list
|
|
|
|
|
2022-11-02 07:37:22 +00:00
|
|
|
# if vae_file argument is provided, it takes priority, but not saved
|
2022-10-30 14:54:31 +00:00
|
|
|
if vae_file and vae_file not in default_vae_list:
|
|
|
|
if not os.path.isfile(vae_file):
|
2022-11-13 03:58:15 +00:00
|
|
|
print(f"VAE provided as function argument doesn't exist: {vae_file}")
|
2022-10-30 14:54:31 +00:00
|
|
|
vae_file = "auto"
|
2022-11-02 07:37:22 +00:00
|
|
|
# for the first load, if vae-path is provided, it takes priority, saved, and failure is reported
|
2022-10-30 14:54:31 +00:00
|
|
|
if first_load and shared.cmd_opts.vae_path is not None:
|
|
|
|
if os.path.isfile(shared.cmd_opts.vae_path):
|
|
|
|
vae_file = shared.cmd_opts.vae_path
|
2022-11-02 07:37:22 +00:00
|
|
|
shared.opts.data['sd_vae'] = get_filename(vae_file)
|
2022-10-30 14:54:31 +00:00
|
|
|
else:
|
2022-11-13 03:58:15 +00:00
|
|
|
print(f"VAE provided as command line argument doesn't exist: {vae_file}")
|
2022-11-03 06:47:03 +00:00
|
|
|
# fallback to selector in settings, if vae selector not set to act as default fallback
|
|
|
|
if not shared.opts.sd_vae_as_default:
|
|
|
|
vae_file = get_vae_from_settings(vae_file)
|
2022-10-30 14:54:31 +00:00
|
|
|
# vae-path cmd arg takes priority for auto
|
|
|
|
if vae_file == "auto" and shared.cmd_opts.vae_path is not None:
|
|
|
|
if os.path.isfile(shared.cmd_opts.vae_path):
|
|
|
|
vae_file = shared.cmd_opts.vae_path
|
2022-11-13 03:58:15 +00:00
|
|
|
print(f"Using VAE provided as command line argument: {vae_file}")
|
2022-10-30 14:54:31 +00:00
|
|
|
# if still not found, try look for ".vae.pt" beside model
|
|
|
|
model_path = os.path.splitext(checkpoint_file)[0]
|
|
|
|
if vae_file == "auto":
|
|
|
|
vae_file_try = model_path + ".vae.pt"
|
|
|
|
if os.path.isfile(vae_file_try):
|
|
|
|
vae_file = vae_file_try
|
2022-11-13 03:58:15 +00:00
|
|
|
print(f"Using VAE found similar to selected model: {vae_file}")
|
2022-10-30 14:54:31 +00:00
|
|
|
# if still not found, try look for ".vae.ckpt" beside model
|
|
|
|
if vae_file == "auto":
|
|
|
|
vae_file_try = model_path + ".vae.ckpt"
|
|
|
|
if os.path.isfile(vae_file_try):
|
|
|
|
vae_file = vae_file_try
|
2022-11-13 03:58:15 +00:00
|
|
|
print(f"Using VAE found similar to selected model: {vae_file}")
|
2022-10-30 14:54:31 +00:00
|
|
|
# No more fallbacks for auto
|
|
|
|
if vae_file == "auto":
|
|
|
|
vae_file = None
|
|
|
|
# Last check, just because
|
|
|
|
if vae_file and not os.path.exists(vae_file):
|
|
|
|
vae_file = None
|
|
|
|
|
2022-10-31 08:19:34 +00:00
|
|
|
return vae_file
|
|
|
|
|
2022-11-02 05:51:46 +00:00
|
|
|
|
|
|
|
def load_vae(model, vae_file=None):
|
|
|
|
global first_load, vae_dict, vae_list, loaded_vae_file
|
2022-10-31 08:19:34 +00:00
|
|
|
# save_settings = False
|
|
|
|
|
2022-12-25 12:49:25 +00:00
|
|
|
cache_enabled = shared.opts.sd_vae_checkpoint_cache > 0
|
|
|
|
|
2022-10-30 14:54:31 +00:00
|
|
|
if vae_file:
|
2022-12-25 12:49:25 +00:00
|
|
|
if cache_enabled and vae_file in checkpoints_loaded:
|
|
|
|
# use vae checkpoint cache
|
|
|
|
print(f"Loading VAE weights [{get_filename(vae_file)}] from cache")
|
|
|
|
store_base_vae(model)
|
|
|
|
_load_vae_dict(model, checkpoints_loaded[vae_file])
|
|
|
|
else:
|
|
|
|
assert os.path.isfile(vae_file), f"VAE file doesn't exist: {vae_file}"
|
|
|
|
print(f"Loading VAE weights from: {vae_file}")
|
|
|
|
store_base_vae(model)
|
|
|
|
vae_ckpt = torch.load(vae_file, map_location=shared.weight_load_location)
|
|
|
|
vae_dict_1 = {k: v for k, v in vae_ckpt["state_dict"].items() if k[0:4] != "loss" and k not in vae_ignore_keys}
|
|
|
|
_load_vae_dict(model, vae_dict_1)
|
|
|
|
|
|
|
|
if cache_enabled:
|
|
|
|
# cache newly loaded vae
|
|
|
|
checkpoints_loaded[vae_file] = vae_dict_1.copy()
|
|
|
|
|
|
|
|
# clean up cache if limit is reached
|
|
|
|
if cache_enabled:
|
|
|
|
while len(checkpoints_loaded) > shared.opts.sd_vae_checkpoint_cache + 1: # we need to count the current model
|
|
|
|
checkpoints_loaded.popitem(last=False) # LRU
|
2022-10-30 14:54:31 +00:00
|
|
|
|
2022-11-02 05:51:46 +00:00
|
|
|
# If vae used is not in dict, update it
|
|
|
|
# It will be removed on refresh though
|
2022-10-30 14:54:31 +00:00
|
|
|
vae_opt = get_filename(vae_file)
|
|
|
|
if vae_opt not in vae_dict:
|
|
|
|
vae_dict[vae_opt] = vae_file
|
|
|
|
vae_list.append(vae_opt)
|
2022-11-13 04:11:14 +00:00
|
|
|
elif loaded_vae_file:
|
2022-11-02 15:54:09 +00:00
|
|
|
restore_base_vae(model)
|
2022-10-30 14:54:31 +00:00
|
|
|
|
2022-11-02 05:51:46 +00:00
|
|
|
loaded_vae_file = vae_file
|
|
|
|
|
2022-10-30 14:54:31 +00:00
|
|
|
first_load = False
|
2022-11-02 05:51:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
# don't call this from outside
|
2022-11-18 18:27:54 +00:00
|
|
|
def _load_vae_dict(model, vae_dict_1):
|
2022-11-02 15:54:09 +00:00
|
|
|
model.first_stage_model.load_state_dict(vae_dict_1)
|
2022-10-30 14:54:31 +00:00
|
|
|
model.first_stage_model.to(devices.dtype_vae)
|
2022-11-02 05:51:46 +00:00
|
|
|
|
2022-11-03 04:10:53 +00:00
|
|
|
def clear_loaded_vae():
|
|
|
|
global loaded_vae_file
|
|
|
|
loaded_vae_file = None
|
2022-11-02 05:51:46 +00:00
|
|
|
|
|
|
|
def reload_vae_weights(sd_model=None, vae_file="auto"):
|
|
|
|
from modules import lowvram, devices, sd_hijack
|
|
|
|
|
|
|
|
if not sd_model:
|
|
|
|
sd_model = shared.sd_model
|
|
|
|
|
|
|
|
checkpoint_info = sd_model.sd_checkpoint_info
|
|
|
|
checkpoint_file = checkpoint_info.filename
|
|
|
|
vae_file = resolve_vae(checkpoint_file, vae_file=vae_file)
|
|
|
|
|
|
|
|
if loaded_vae_file == vae_file:
|
|
|
|
return
|
|
|
|
|
|
|
|
if shared.cmd_opts.lowvram or shared.cmd_opts.medvram:
|
|
|
|
lowvram.send_everything_to_cpu()
|
|
|
|
else:
|
|
|
|
sd_model.to(devices.cpu)
|
|
|
|
|
|
|
|
sd_hijack.model_hijack.undo_hijack(sd_model)
|
|
|
|
|
|
|
|
load_vae(sd_model, vae_file)
|
|
|
|
|
|
|
|
sd_hijack.model_hijack.hijack(sd_model)
|
|
|
|
script_callbacks.model_loaded_callback(sd_model)
|
|
|
|
|
|
|
|
if not shared.cmd_opts.lowvram and not shared.cmd_opts.medvram:
|
|
|
|
sd_model.to(devices.device)
|
|
|
|
|
2022-12-24 19:35:29 +00:00
|
|
|
print("VAE Weights loaded.")
|
2022-11-02 05:51:46 +00:00
|
|
|
return sd_model
|