aboutsummaryrefslogtreecommitdiff
path: root/extensions-builtin/LDSR/ldsr_model_arch.py
diff options
context:
space:
mode:
authorwywywywy <wywywywy@gmail.com>2022-12-10 13:54:29 +0000
committerwywywywy <wywywywy@gmail.com>2022-12-10 13:54:29 +0000
commit6df316c881b533731faa77494ea01533e35f8dc7 (patch)
treeda508a8ab781e1b759075fe2fd5c2e45f89801ea /extensions-builtin/LDSR/ldsr_model_arch.py
parent7cea280a8fd9b7e3cc65a1719d6371d69013c9bb (diff)
LDSR cache / optimization / opt_channelslast
Diffstat (limited to 'extensions-builtin/LDSR/ldsr_model_arch.py')
-rw-r--r--extensions-builtin/LDSR/ldsr_model_arch.py40
1 files changed, 28 insertions, 12 deletions
diff --git a/extensions-builtin/LDSR/ldsr_model_arch.py b/extensions-builtin/LDSR/ldsr_model_arch.py
index a87d1ef9..9ec4e67e 100644
--- a/extensions-builtin/LDSR/ldsr_model_arch.py
+++ b/extensions-builtin/LDSR/ldsr_model_arch.py
@@ -11,25 +11,41 @@ from omegaconf import OmegaConf
from ldm.models.diffusion.ddim import DDIMSampler
from ldm.util import instantiate_from_config, ismap
+from modules import shared, sd_hijack
warnings.filterwarnings("ignore", category=UserWarning)
+cached_ldsr_model: torch.nn.Module = None
+
# Create LDSR Class
class LDSR:
def load_model_from_config(self, half_attention):
- print(f"Loading model from {self.modelPath}")
- pl_sd = torch.load(self.modelPath, map_location="cpu")
- sd = pl_sd["state_dict"]
- config = OmegaConf.load(self.yamlPath)
- config.model.target = "ldm.models.diffusion.ddpm.LatentDiffusionV1"
- model = instantiate_from_config(config.model)
- model.load_state_dict(sd, strict=False)
- model.cuda()
- if half_attention:
- model = model.half()
-
- model.eval()
+ global cached_ldsr_model
+
+ if shared.opts.ldsr_cached and cached_ldsr_model is not None:
+ print(f"Loading model from cache")
+ model: torch.nn.Module = cached_ldsr_model
+ else:
+ print(f"Loading model from {self.modelPath}")
+ pl_sd = torch.load(self.modelPath, map_location="cpu")
+ sd = pl_sd["state_dict"]
+ config = OmegaConf.load(self.yamlPath)
+ config.model.target = "ldm.models.diffusion.ddpm.LatentDiffusionV1"
+ model: torch.nn.Module = instantiate_from_config(config.model)
+ model.load_state_dict(sd, strict=False)
+ model = model.to(shared.device)
+ if half_attention:
+ model = model.half()
+ if shared.cmd_opts.opt_channelslast:
+ model = model.to(memory_format=torch.channels_last)
+
+ sd_hijack.model_hijack.hijack(model) # apply optimization
+ model.eval()
+
+ if shared.opts.ldsr_cached:
+ cached_ldsr_model = model
+
return {"model": model}
def __init__(self, model_path, yaml_path):