|
| 1 | +import torch |
| 2 | +import os |
| 3 | +from collections import namedtuple |
| 4 | +from modules import shared, devices, script_callbacks |
| 5 | +from modules.paths import models_path |
| 6 | +import glob |
| 7 | + |
| 8 | + |
| 9 | +model_dir = "Stable-diffusion" |
| 10 | +model_path = os.path.abspath(os.path.join(models_path, model_dir)) |
| 11 | +vae_dir = "VAE" |
| 12 | +vae_path = os.path.abspath(os.path.join(models_path, vae_dir)) |
| 13 | + |
| 14 | + |
| 15 | +vae_ignore_keys = {"model_ema.decay", "model_ema.num_updates"} |
| 16 | + |
| 17 | + |
| 18 | +default_vae_dict = {"auto": "auto", "None": "None"} |
| 19 | +default_vae_list = ["auto", "None"] |
| 20 | + |
| 21 | + |
| 22 | +default_vae_values = [default_vae_dict[x] for x in default_vae_list] |
| 23 | +vae_dict = dict(default_vae_dict) |
| 24 | +vae_list = list(default_vae_list) |
| 25 | +first_load = True |
| 26 | + |
| 27 | + |
| 28 | +base_vae = None |
| 29 | +loaded_vae_file = None |
| 30 | +checkpoint_info = None |
| 31 | + |
| 32 | + |
| 33 | +def get_base_vae(model): |
| 34 | + if base_vae is not None and checkpoint_info == model.sd_checkpoint_info and model: |
| 35 | + return base_vae |
| 36 | + return None |
| 37 | + |
| 38 | + |
| 39 | +def store_base_vae(model): |
| 40 | + global base_vae, checkpoint_info |
| 41 | + if checkpoint_info != model.sd_checkpoint_info: |
| 42 | + base_vae = model.first_stage_model.state_dict().copy() |
| 43 | + checkpoint_info = model.sd_checkpoint_info |
| 44 | + |
| 45 | + |
| 46 | +def delete_base_vae(): |
| 47 | + global base_vae, checkpoint_info |
| 48 | + base_vae = None |
| 49 | + checkpoint_info = None |
| 50 | + |
| 51 | + |
| 52 | +def restore_base_vae(model): |
| 53 | + global base_vae, checkpoint_info |
| 54 | + if base_vae is not None and checkpoint_info == model.sd_checkpoint_info: |
| 55 | + load_vae_dict(model, base_vae) |
| 56 | + delete_base_vae() |
| 57 | + |
| 58 | + |
| 59 | +def get_filename(filepath): |
| 60 | + return os.path.splitext(os.path.basename(filepath))[0] |
| 61 | + |
| 62 | + |
| 63 | +def refresh_vae_list(vae_path=vae_path, model_path=model_path): |
| 64 | + global vae_dict, vae_list |
| 65 | + res = {} |
| 66 | + candidates = [ |
| 67 | + *glob.iglob(os.path.join(model_path, '**/*.vae.ckpt'), recursive=True), |
| 68 | + *glob.iglob(os.path.join(model_path, '**/*.vae.pt'), recursive=True), |
| 69 | + *glob.iglob(os.path.join(vae_path, '**/*.ckpt'), recursive=True), |
| 70 | + *glob.iglob(os.path.join(vae_path, '**/*.pt'), recursive=True) |
| 71 | + ] |
| 72 | + if shared.cmd_opts.vae_path is not None and os.path.isfile(shared.cmd_opts.vae_path): |
| 73 | + candidates.append(shared.cmd_opts.vae_path) |
| 74 | + for filepath in candidates: |
| 75 | + name = get_filename(filepath) |
| 76 | + res[name] = filepath |
| 77 | + vae_list.clear() |
| 78 | + vae_list.extend(default_vae_list) |
| 79 | + vae_list.extend(list(res.keys())) |
| 80 | + vae_dict.clear() |
| 81 | + vae_dict.update(res) |
| 82 | + vae_dict.update(default_vae_dict) |
| 83 | + return vae_list |
| 84 | + |
| 85 | + |
| 86 | +def resolve_vae(checkpoint_file, vae_file="auto"): |
| 87 | + global first_load, vae_dict, vae_list |
| 88 | + |
| 89 | + # if vae_file argument is provided, it takes priority, but not saved |
| 90 | + if vae_file and vae_file not in default_vae_list: |
| 91 | + if not os.path.isfile(vae_file): |
| 92 | + vae_file = "auto" |
| 93 | + print("VAE provided as function argument doesn't exist") |
| 94 | + # for the first load, if vae-path is provided, it takes priority, saved, and failure is reported |
| 95 | + if first_load and shared.cmd_opts.vae_path is not None: |
| 96 | + if os.path.isfile(shared.cmd_opts.vae_path): |
| 97 | + vae_file = shared.cmd_opts.vae_path |
| 98 | + shared.opts.data['sd_vae'] = get_filename(vae_file) |
| 99 | + else: |
| 100 | + print("VAE provided as command line argument doesn't exist") |
| 101 | + # else, we load from settings |
| 102 | + if vae_file == "auto" and shared.opts.sd_vae is not None: |
| 103 | + # if saved VAE settings isn't recognized, fallback to auto |
| 104 | + vae_file = vae_dict.get(shared.opts.sd_vae, "auto") |
| 105 | + # if VAE selected but not found, fallback to auto |
| 106 | + if vae_file not in default_vae_values and not os.path.isfile(vae_file): |
| 107 | + vae_file = "auto" |
| 108 | + print("Selected VAE doesn't exist") |
| 109 | + # vae-path cmd arg takes priority for auto |
| 110 | + if vae_file == "auto" and shared.cmd_opts.vae_path is not None: |
| 111 | + if os.path.isfile(shared.cmd_opts.vae_path): |
| 112 | + vae_file = shared.cmd_opts.vae_path |
| 113 | + print("Using VAE provided as command line argument") |
| 114 | + # if still not found, try look for ".vae.pt" beside model |
| 115 | + model_path = os.path.splitext(checkpoint_file)[0] |
| 116 | + if vae_file == "auto": |
| 117 | + vae_file_try = model_path + ".vae.pt" |
| 118 | + if os.path.isfile(vae_file_try): |
| 119 | + vae_file = vae_file_try |
| 120 | + print("Using VAE found beside selected model") |
| 121 | + # if still not found, try look for ".vae.ckpt" beside model |
| 122 | + if vae_file == "auto": |
| 123 | + vae_file_try = model_path + ".vae.ckpt" |
| 124 | + if os.path.isfile(vae_file_try): |
| 125 | + vae_file = vae_file_try |
| 126 | + print("Using VAE found beside selected model") |
| 127 | + # No more fallbacks for auto |
| 128 | + if vae_file == "auto": |
| 129 | + vae_file = None |
| 130 | + # Last check, just because |
| 131 | + if vae_file and not os.path.exists(vae_file): |
| 132 | + vae_file = None |
| 133 | + |
| 134 | + return vae_file |
| 135 | + |
| 136 | + |
| 137 | +def load_vae(model, vae_file=None): |
| 138 | + global first_load, vae_dict, vae_list, loaded_vae_file |
| 139 | + # save_settings = False |
| 140 | + |
| 141 | + if vae_file: |
| 142 | + print(f"Loading VAE weights from: {vae_file}") |
| 143 | + vae_ckpt = torch.load(vae_file, map_location=shared.weight_load_location) |
| 144 | + 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} |
| 145 | + load_vae_dict(model, vae_dict_1) |
| 146 | + |
| 147 | + # If vae used is not in dict, update it |
| 148 | + # It will be removed on refresh though |
| 149 | + vae_opt = get_filename(vae_file) |
| 150 | + if vae_opt not in vae_dict: |
| 151 | + vae_dict[vae_opt] = vae_file |
| 152 | + vae_list.append(vae_opt) |
| 153 | + |
| 154 | + loaded_vae_file = vae_file |
| 155 | + |
| 156 | + """ |
| 157 | + # Save current VAE to VAE settings, maybe? will it work? |
| 158 | + if save_settings: |
| 159 | + if vae_file is None: |
| 160 | + vae_opt = "None" |
| 161 | +
|
| 162 | + # shared.opts.sd_vae = vae_opt |
| 163 | + """ |
| 164 | + |
| 165 | + first_load = False |
| 166 | + |
| 167 | + |
| 168 | +# don't call this from outside |
| 169 | +def load_vae_dict(model, vae_dict_1=None): |
| 170 | + if vae_dict_1: |
| 171 | + store_base_vae(model) |
| 172 | + model.first_stage_model.load_state_dict(vae_dict_1) |
| 173 | + else: |
| 174 | + restore_base_vae() |
| 175 | + model.first_stage_model.to(devices.dtype_vae) |
| 176 | + |
| 177 | + |
| 178 | +def reload_vae_weights(sd_model=None, vae_file="auto"): |
| 179 | + from modules import lowvram, devices, sd_hijack |
| 180 | + |
| 181 | + if not sd_model: |
| 182 | + sd_model = shared.sd_model |
| 183 | + |
| 184 | + checkpoint_info = sd_model.sd_checkpoint_info |
| 185 | + checkpoint_file = checkpoint_info.filename |
| 186 | + vae_file = resolve_vae(checkpoint_file, vae_file=vae_file) |
| 187 | + |
| 188 | + if loaded_vae_file == vae_file: |
| 189 | + return |
| 190 | + |
| 191 | + if shared.cmd_opts.lowvram or shared.cmd_opts.medvram: |
| 192 | + lowvram.send_everything_to_cpu() |
| 193 | + else: |
| 194 | + sd_model.to(devices.cpu) |
| 195 | + |
| 196 | + sd_hijack.model_hijack.undo_hijack(sd_model) |
| 197 | + |
| 198 | + load_vae(sd_model, vae_file) |
| 199 | + |
| 200 | + sd_hijack.model_hijack.hijack(sd_model) |
| 201 | + script_callbacks.model_loaded_callback(sd_model) |
| 202 | + |
| 203 | + if not shared.cmd_opts.lowvram and not shared.cmd_opts.medvram: |
| 204 | + sd_model.to(devices.device) |
| 205 | + |
| 206 | + print(f"VAE Weights loaded.") |
| 207 | + return sd_model |
0 commit comments