2022-10-30 08:54:31 -06:00
|
|
|
import os
|
2022-12-25 05:49:25 -07:00
|
|
|
import collections
|
2023-08-06 23:07:09 -06:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2023-08-10 07:22:10 -06:00
|
|
|
from modules import paths, shared, devices, script_callbacks, sd_models, extra_networks, lowvram, sd_hijack, hashes
|
2023-08-09 01:25:35 -06:00
|
|
|
|
2022-10-30 08:54:31 -06:00
|
|
|
import glob
|
2022-11-17 04:04:10 -07:00
|
|
|
from copy import deepcopy
|
2022-10-30 08:54:31 -06:00
|
|
|
|
2022-11-01 23:51:46 -06:00
|
|
|
|
2023-01-25 09:15:42 -07:00
|
|
|
vae_path = os.path.abspath(os.path.join(paths.models_path, "VAE"))
|
2022-10-30 08:54:31 -06:00
|
|
|
vae_ignore_keys = {"model_ema.decay", "model_ema.num_updates"}
|
2023-01-14 09:56:09 -07:00
|
|
|
vae_dict = {}
|
2022-10-30 08:54:31 -06:00
|
|
|
|
2022-11-01 23:51:46 -06:00
|
|
|
|
|
|
|
base_vae = None
|
|
|
|
loaded_vae_file = None
|
|
|
|
checkpoint_info = None
|
|
|
|
|
2022-12-25 05:49:25 -07:00
|
|
|
checkpoints_loaded = collections.OrderedDict()
|
2022-11-01 23:51:46 -06:00
|
|
|
|
2023-08-04 13:05:40 -06:00
|
|
|
|
2023-08-10 07:22:10 -06:00
|
|
|
def get_loaded_vae_name():
|
|
|
|
if loaded_vae_file is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return os.path.basename(loaded_vae_file)
|
|
|
|
|
|
|
|
|
|
|
|
def get_loaded_vae_hash():
|
|
|
|
if loaded_vae_file is None:
|
|
|
|
return None
|
|
|
|
|
2023-08-10 14:04:49 -06:00
|
|
|
sha256 = hashes.sha256(loaded_vae_file, 'vae')
|
|
|
|
|
|
|
|
return sha256[0:10] if sha256 else None
|
2023-08-10 07:22:10 -06:00
|
|
|
|
|
|
|
|
2022-11-01 23:51:46 -06: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-02 22:10:53 -06:00
|
|
|
assert not loaded_vae_file, "Trying to store non-base VAE!"
|
2022-11-17 04:04:10 -07:00
|
|
|
base_vae = deepcopy(model.first_stage_model.state_dict())
|
2022-11-01 23:51:46 -06: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-02 22:10:53 -06:00
|
|
|
global loaded_vae_file
|
2022-11-01 23:51:46 -06:00
|
|
|
if base_vae is not None and checkpoint_info == model.sd_checkpoint_info:
|
2022-11-02 09:54:09 -06:00
|
|
|
print("Restoring base VAE")
|
2022-11-18 11:27:54 -07:00
|
|
|
_load_vae_dict(model, base_vae)
|
2022-11-02 22:10:53 -06:00
|
|
|
loaded_vae_file = None
|
2022-11-01 23:51:46 -06:00
|
|
|
delete_base_vae()
|
|
|
|
|
|
|
|
|
2022-10-30 08:54:31 -06:00
|
|
|
def get_filename(filepath):
|
2023-01-14 09:56:09 -07:00
|
|
|
return os.path.basename(filepath)
|
|
|
|
|
|
|
|
|
|
|
|
def refresh_vae_list():
|
2023-08-03 21:01:28 -06:00
|
|
|
global vae_dict
|
2023-01-14 09:56:09 -07:00
|
|
|
vae_dict.clear()
|
|
|
|
|
|
|
|
paths = [
|
|
|
|
os.path.join(sd_models.model_path, '**/*.vae.ckpt'),
|
|
|
|
os.path.join(sd_models.model_path, '**/*.vae.pt'),
|
|
|
|
os.path.join(sd_models.model_path, '**/*.vae.safetensors'),
|
|
|
|
os.path.join(vae_path, '**/*.ckpt'),
|
|
|
|
os.path.join(vae_path, '**/*.pt'),
|
|
|
|
os.path.join(vae_path, '**/*.safetensors'),
|
2022-10-30 08:54:31 -06:00
|
|
|
]
|
2023-01-14 09:56:09 -07:00
|
|
|
|
|
|
|
if shared.cmd_opts.ckpt_dir is not None and os.path.isdir(shared.cmd_opts.ckpt_dir):
|
|
|
|
paths += [
|
|
|
|
os.path.join(shared.cmd_opts.ckpt_dir, '**/*.vae.ckpt'),
|
|
|
|
os.path.join(shared.cmd_opts.ckpt_dir, '**/*.vae.pt'),
|
|
|
|
os.path.join(shared.cmd_opts.ckpt_dir, '**/*.vae.safetensors'),
|
|
|
|
]
|
|
|
|
|
2023-01-17 07:50:41 -07:00
|
|
|
if shared.cmd_opts.vae_dir is not None and os.path.isdir(shared.cmd_opts.vae_dir):
|
|
|
|
paths += [
|
|
|
|
os.path.join(shared.cmd_opts.vae_dir, '**/*.ckpt'),
|
|
|
|
os.path.join(shared.cmd_opts.vae_dir, '**/*.pt'),
|
|
|
|
os.path.join(shared.cmd_opts.vae_dir, '**/*.safetensors'),
|
|
|
|
]
|
|
|
|
|
2023-01-14 09:56:09 -07:00
|
|
|
candidates = []
|
|
|
|
for path in paths:
|
|
|
|
candidates += glob.iglob(path, recursive=True)
|
|
|
|
|
2022-10-30 08:54:31 -06:00
|
|
|
for filepath in candidates:
|
|
|
|
name = get_filename(filepath)
|
2023-01-14 09:56:09 -07:00
|
|
|
vae_dict[name] = filepath
|
|
|
|
|
2023-08-03 21:01:28 -06:00
|
|
|
vae_dict = dict(sorted(vae_dict.items(), key=lambda item: shared.natural_sort_key(item[0])))
|
|
|
|
|
2023-01-14 09:56:09 -07:00
|
|
|
|
|
|
|
def find_vae_near_checkpoint(checkpoint_file):
|
2023-05-11 00:29:52 -06:00
|
|
|
checkpoint_path = os.path.basename(checkpoint_file).rsplit('.', 1)[0]
|
2023-05-09 07:35:57 -06:00
|
|
|
for vae_file in vae_dict.values():
|
2023-05-11 00:29:52 -06:00
|
|
|
if os.path.basename(vae_file).startswith(checkpoint_path):
|
2023-05-09 07:35:57 -06:00
|
|
|
return vae_file
|
2023-01-14 09:56:09 -07:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-08-06 23:07:09 -06:00
|
|
|
@dataclass
|
|
|
|
class VaeResolution:
|
|
|
|
vae: str = None
|
|
|
|
source: str = None
|
|
|
|
resolved: bool = True
|
|
|
|
|
|
|
|
def tuple(self):
|
|
|
|
return self.vae, self.source
|
|
|
|
|
|
|
|
|
|
|
|
def is_automatic():
|
|
|
|
return shared.opts.sd_vae in {"Automatic", "auto"} # "auto" for people with old config
|
|
|
|
|
|
|
|
|
|
|
|
def resolve_vae_from_setting() -> VaeResolution:
|
|
|
|
if shared.opts.sd_vae == "None":
|
|
|
|
return VaeResolution()
|
|
|
|
|
|
|
|
vae_from_options = vae_dict.get(shared.opts.sd_vae, None)
|
|
|
|
if vae_from_options is not None:
|
|
|
|
return VaeResolution(vae_from_options, 'specified in settings')
|
|
|
|
|
|
|
|
if not is_automatic():
|
|
|
|
print(f"Couldn't find VAE named {shared.opts.sd_vae}; using None instead")
|
2023-01-14 09:56:09 -07:00
|
|
|
|
2023-08-06 23:07:09 -06:00
|
|
|
return VaeResolution(resolved=False)
|
|
|
|
|
|
|
|
|
|
|
|
def resolve_vae_from_user_metadata(checkpoint_file) -> VaeResolution:
|
2023-08-04 13:05:40 -06:00
|
|
|
metadata = extra_networks.get_user_metadata(checkpoint_file)
|
|
|
|
vae_metadata = metadata.get("vae", None)
|
|
|
|
if vae_metadata is not None and vae_metadata != "Automatic":
|
|
|
|
if vae_metadata == "None":
|
2023-08-06 23:07:09 -06:00
|
|
|
return VaeResolution()
|
2023-08-04 13:05:40 -06:00
|
|
|
|
|
|
|
vae_from_metadata = vae_dict.get(vae_metadata, None)
|
|
|
|
if vae_from_metadata is not None:
|
2023-08-06 23:07:09 -06:00
|
|
|
return VaeResolution(vae_from_metadata, "from user metadata")
|
|
|
|
|
|
|
|
return VaeResolution(resolved=False)
|
2023-08-04 13:05:40 -06:00
|
|
|
|
2023-01-15 14:44:46 -07:00
|
|
|
|
2023-08-06 23:07:09 -06:00
|
|
|
def resolve_vae_near_checkpoint(checkpoint_file) -> VaeResolution:
|
2023-01-14 09:56:09 -07:00
|
|
|
vae_near_checkpoint = find_vae_near_checkpoint(checkpoint_file)
|
2023-08-15 04:29:39 -06:00
|
|
|
if vae_near_checkpoint is not None and (not shared.opts.sd_vae_overrides_per_model_preferences or is_automatic):
|
2023-08-06 23:07:09 -06:00
|
|
|
return VaeResolution(vae_near_checkpoint, 'found near the checkpoint')
|
2023-01-14 09:56:09 -07:00
|
|
|
|
2023-08-06 23:07:09 -06:00
|
|
|
return VaeResolution(resolved=False)
|
2023-01-14 09:56:09 -07:00
|
|
|
|
|
|
|
|
2023-08-06 23:07:09 -06:00
|
|
|
def resolve_vae(checkpoint_file) -> VaeResolution:
|
|
|
|
if shared.cmd_opts.vae_path is not None:
|
|
|
|
return VaeResolution(shared.cmd_opts.vae_path, 'from commandline argument')
|
|
|
|
|
|
|
|
if shared.opts.sd_vae_overrides_per_model_preferences and not is_automatic():
|
|
|
|
return resolve_vae_from_setting()
|
|
|
|
|
|
|
|
res = resolve_vae_from_user_metadata(checkpoint_file)
|
|
|
|
if res.resolved:
|
|
|
|
return res
|
|
|
|
|
|
|
|
res = resolve_vae_near_checkpoint(checkpoint_file)
|
|
|
|
if res.resolved:
|
|
|
|
return res
|
|
|
|
|
|
|
|
res = resolve_vae_from_setting()
|
2023-01-14 09:56:09 -07:00
|
|
|
|
2023-08-06 23:07:09 -06:00
|
|
|
return res
|
2023-01-14 09:56:09 -07:00
|
|
|
|
|
|
|
|
2023-01-19 00:39:51 -07:00
|
|
|
def load_vae_dict(filename, map_location):
|
|
|
|
vae_ckpt = sd_models.read_state_dict(filename, map_location=map_location)
|
|
|
|
vae_dict_1 = {k: v for k, v in vae_ckpt.items() if k[0:4] != "loss" and k not in vae_ignore_keys}
|
|
|
|
return vae_dict_1
|
|
|
|
|
|
|
|
|
2023-01-14 09:56:09 -07:00
|
|
|
def load_vae(model, vae_file=None, vae_source="from unknown source"):
|
|
|
|
global vae_dict, loaded_vae_file
|
2022-10-31 02:19:34 -06:00
|
|
|
# save_settings = False
|
|
|
|
|
2022-12-25 05:49:25 -07:00
|
|
|
cache_enabled = shared.opts.sd_vae_checkpoint_cache > 0
|
|
|
|
|
2022-10-30 08:54:31 -06:00
|
|
|
if vae_file:
|
2022-12-25 05:49:25 -07:00
|
|
|
if cache_enabled and vae_file in checkpoints_loaded:
|
|
|
|
# use vae checkpoint cache
|
2023-01-14 09:56:09 -07:00
|
|
|
print(f"Loading VAE weights {vae_source}: cached {get_filename(vae_file)}")
|
2022-12-25 05:49:25 -07:00
|
|
|
store_base_vae(model)
|
|
|
|
_load_vae_dict(model, checkpoints_loaded[vae_file])
|
|
|
|
else:
|
2023-01-14 09:56:09 -07:00
|
|
|
assert os.path.isfile(vae_file), f"VAE {vae_source} doesn't exist: {vae_file}"
|
|
|
|
print(f"Loading VAE weights {vae_source}: {vae_file}")
|
2022-12-25 05:49:25 -07:00
|
|
|
store_base_vae(model)
|
2023-01-09 09:58:35 -07:00
|
|
|
|
2023-01-19 00:39:51 -07:00
|
|
|
vae_dict_1 = load_vae_dict(vae_file, map_location=shared.weight_load_location)
|
2022-12-25 05:49:25 -07:00
|
|
|
_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 08:54:31 -06:00
|
|
|
|
2022-11-01 23:51:46 -06:00
|
|
|
# If vae used is not in dict, update it
|
|
|
|
# It will be removed on refresh though
|
2022-10-30 08:54:31 -06:00
|
|
|
vae_opt = get_filename(vae_file)
|
|
|
|
if vae_opt not in vae_dict:
|
|
|
|
vae_dict[vae_opt] = vae_file
|
2023-01-14 09:56:09 -07:00
|
|
|
|
2022-11-12 21:11:14 -07:00
|
|
|
elif loaded_vae_file:
|
2022-11-02 09:54:09 -06:00
|
|
|
restore_base_vae(model)
|
2022-10-30 08:54:31 -06:00
|
|
|
|
2022-11-01 23:51:46 -06:00
|
|
|
loaded_vae_file = vae_file
|
|
|
|
|
|
|
|
|
|
|
|
# don't call this from outside
|
2022-11-18 11:27:54 -07:00
|
|
|
def _load_vae_dict(model, vae_dict_1):
|
2022-11-02 09:54:09 -06:00
|
|
|
model.first_stage_model.load_state_dict(vae_dict_1)
|
2022-10-30 08:54:31 -06:00
|
|
|
model.first_stage_model.to(devices.dtype_vae)
|
2022-11-01 23:51:46 -06:00
|
|
|
|
2023-01-09 09:58:35 -07:00
|
|
|
|
2022-11-02 22:10:53 -06:00
|
|
|
def clear_loaded_vae():
|
|
|
|
global loaded_vae_file
|
|
|
|
loaded_vae_file = None
|
2022-11-01 23:51:46 -06:00
|
|
|
|
2023-01-09 09:58:35 -07:00
|
|
|
|
2023-01-14 09:56:09 -07:00
|
|
|
unspecified = object()
|
|
|
|
|
|
|
|
|
|
|
|
def reload_vae_weights(sd_model=None, vae_file=unspecified):
|
2022-11-01 23:51:46 -06:00
|
|
|
if not sd_model:
|
|
|
|
sd_model = shared.sd_model
|
|
|
|
|
|
|
|
checkpoint_info = sd_model.sd_checkpoint_info
|
|
|
|
checkpoint_file = checkpoint_info.filename
|
2023-01-14 09:56:09 -07:00
|
|
|
|
|
|
|
if vae_file == unspecified:
|
2023-08-06 23:07:09 -06:00
|
|
|
vae_file, vae_source = resolve_vae(checkpoint_file).tuple()
|
2023-01-14 09:56:09 -07:00
|
|
|
else:
|
|
|
|
vae_source = "from function argument"
|
2022-11-01 23:51:46 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-01-14 09:56:09 -07:00
|
|
|
load_vae(sd_model, vae_file, vae_source)
|
2022-11-01 23:51:46 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-01-14 09:56:09 -07:00
|
|
|
print("VAE weights loaded.")
|
2022-11-01 23:51:46 -06:00
|
|
|
return sd_model
|