2023-06-22 17:03:50 +00:00
|
|
|
import torch
|
|
|
|
from . import model_base
|
|
|
|
from . import utils
|
2023-08-30 03:58:32 +00:00
|
|
|
from . import latent_formats
|
2023-06-22 17:03:50 +00:00
|
|
|
|
|
|
|
class ClipTarget:
|
|
|
|
def __init__(self, tokenizer, clip):
|
|
|
|
self.clip = clip
|
|
|
|
self.tokenizer = tokenizer
|
|
|
|
self.params = {}
|
|
|
|
|
|
|
|
class BASE:
|
|
|
|
unet_config = {}
|
|
|
|
unet_extra_config = {
|
|
|
|
"num_heads": -1,
|
|
|
|
"num_head_channels": 64,
|
|
|
|
}
|
|
|
|
|
|
|
|
clip_prefix = []
|
|
|
|
clip_vision_prefix = None
|
|
|
|
noise_aug_config = None
|
2023-11-22 22:23:37 +00:00
|
|
|
sampling_settings = {}
|
2023-08-30 03:58:32 +00:00
|
|
|
latent_format = latent_formats.LatentFormat
|
2023-06-22 17:03:50 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def matches(s, unet_config):
|
|
|
|
for k in s.unet_config:
|
|
|
|
if s.unet_config[k] != unet_config[k]:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-07-17 05:22:12 +00:00
|
|
|
def model_type(self, state_dict, prefix=""):
|
|
|
|
return model_base.ModelType.EPS
|
2023-06-22 17:03:50 +00:00
|
|
|
|
|
|
|
def inpaint_model(self):
|
|
|
|
return self.unet_config["in_channels"] > 4
|
|
|
|
|
|
|
|
def __init__(self, unet_config):
|
|
|
|
self.unet_config = unet_config
|
2023-06-23 06:14:12 +00:00
|
|
|
self.latent_format = self.latent_format()
|
2023-06-22 17:03:50 +00:00
|
|
|
for x in self.unet_extra_config:
|
|
|
|
self.unet_config[x] = self.unet_extra_config[x]
|
|
|
|
|
2023-07-29 18:51:56 +00:00
|
|
|
def get_model(self, state_dict, prefix="", device=None):
|
2023-09-01 19:18:25 +00:00
|
|
|
if self.noise_aug_config is not None:
|
|
|
|
out = model_base.SD21UNCLIP(self, self.noise_aug_config, model_type=self.model_type(state_dict, prefix), device=device)
|
2023-06-22 17:03:50 +00:00
|
|
|
else:
|
2023-09-01 19:18:25 +00:00
|
|
|
out = model_base.BaseModel(self, model_type=self.model_type(state_dict, prefix), device=device)
|
|
|
|
if self.inpaint_model():
|
|
|
|
out.set_inpaint()
|
|
|
|
return out
|
2023-06-22 17:03:50 +00:00
|
|
|
|
|
|
|
def process_clip_state_dict(self, state_dict):
|
|
|
|
return state_dict
|
|
|
|
|
2023-11-21 03:27:36 +00:00
|
|
|
def process_unet_state_dict(self, state_dict):
|
|
|
|
return state_dict
|
|
|
|
|
2023-11-21 21:29:18 +00:00
|
|
|
def process_vae_state_dict(self, state_dict):
|
|
|
|
return state_dict
|
|
|
|
|
2023-06-26 16:21:07 +00:00
|
|
|
def process_clip_state_dict_for_saving(self, state_dict):
|
|
|
|
replace_prefix = {"": "cond_stage_model."}
|
2023-09-03 02:33:37 +00:00
|
|
|
return utils.state_dict_prefix_replace(state_dict, replace_prefix)
|
2023-06-26 16:21:07 +00:00
|
|
|
|
|
|
|
def process_unet_state_dict_for_saving(self, state_dict):
|
|
|
|
replace_prefix = {"": "model.diffusion_model."}
|
2023-09-03 02:33:37 +00:00
|
|
|
return utils.state_dict_prefix_replace(state_dict, replace_prefix)
|
2023-06-26 16:21:07 +00:00
|
|
|
|
|
|
|
def process_vae_state_dict_for_saving(self, state_dict):
|
|
|
|
replace_prefix = {"": "first_stage_model."}
|
2023-09-03 02:33:37 +00:00
|
|
|
return utils.state_dict_prefix_replace(state_dict, replace_prefix)
|
2023-06-26 16:21:07 +00:00
|
|
|
|