From a341297b0c5b12046e21bf11ff6edc7832105bf9 Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Sat, 26 Nov 2022 14:58:22 +0000 Subject: [PATCH 1/3] stop crash on !import_models call on model inside rootdir - addresses bug report #1546 --- ldm/invoke/model_cache.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ldm/invoke/model_cache.py b/ldm/invoke/model_cache.py index 645a6fd4da..d14c59f39a 100644 --- a/ldm/invoke/model_cache.py +++ b/ldm/invoke/model_cache.py @@ -178,11 +178,12 @@ class ModelCache(object): method will return True. Will fail with an assertion error if provided attributes are incorrect or the model name is missing. ''' + omega = self.config + for field in ('description','weights','height','width','config'): assert field in model_attributes, f'required field {field} is missing' assert (clobber or model_name not in omega), f'attempt to overwrite existing model definition "{model_name}"' - omega = self.config config = omega[model_name] if model_name in omega else {} for field in model_attributes: config[field] = model_attributes[field] From 9adaf8f8adcc8c42dbab9b418dab2b8611171b1c Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Sat, 26 Nov 2022 15:15:10 +0000 Subject: [PATCH 2/3] prevent "!switch state gets confused if model switching fails" - If !switch were to fail on a particular model, then generate got confused and wouldn't try again until you switch to a different working model and back again. - This commit fixes and closes #1547 --- ldm/generate.py | 1 + ldm/invoke/model_cache.py | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ldm/generate.py b/ldm/generate.py index e544cbde4f..ce7e3d3c50 100644 --- a/ldm/generate.py +++ b/ldm/generate.py @@ -832,6 +832,7 @@ class Generate: model_data = cache.get_model(model_name) if model_data is None: # restore previous model_data = cache.get_model(self.model_name) + model_name = self.model_name # addresses Issue #1547 self.model = model_data['model'] self.width = model_data['width'] diff --git a/ldm/invoke/model_cache.py b/ldm/invoke/model_cache.py index d14c59f39a..a9c945a94f 100644 --- a/ldm/invoke/model_cache.py +++ b/ldm/invoke/model_cache.py @@ -78,11 +78,12 @@ class ModelCache(object): else: # we're about to load a new model, so potentially offload the least recently used one try: requested_model, width, height, hash = self._load_model(model_name) - self.models[model_name] = {} - self.models[model_name]['model'] = requested_model - self.models[model_name]['width'] = width - self.models[model_name]['height'] = height - self.models[model_name]['hash'] = hash + self.models[model_name] = { + 'model': requested_model, + 'width': width, + 'height': height, + 'hash': hash, + } except Exception as e: print(f'** model {model_name} could not be loaded: {str(e)}') From a3121b81378e8acf87f88d6037c73ecdc4e69544 Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Mon, 28 Nov 2022 17:44:32 +0000 Subject: [PATCH 3/3] !model_import autocompletes in ROOTDIR --- ldm/invoke/model_cache.py | 2 +- ldm/invoke/readline.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ldm/invoke/model_cache.py b/ldm/invoke/model_cache.py index 645a6fd4da..eaf4364090 100644 --- a/ldm/invoke/model_cache.py +++ b/ldm/invoke/model_cache.py @@ -178,11 +178,11 @@ class ModelCache(object): method will return True. Will fail with an assertion error if provided attributes are incorrect or the model name is missing. ''' + omega = self.config for field in ('description','weights','height','width','config'): assert field in model_attributes, f'required field {field} is missing' assert (clobber or model_name not in omega), f'attempt to overwrite existing model definition "{model_name}"' - omega = self.config config = omega[model_name] if model_name in omega else {} for field in model_attributes: config[field] = model_attributes[field] diff --git a/ldm/invoke/readline.py b/ldm/invoke/readline.py index e0b0c7689b..a0a8124340 100644 --- a/ldm/invoke/readline.py +++ b/ldm/invoke/readline.py @@ -12,6 +12,7 @@ import os import re import atexit from ldm.invoke.args import Args +from ldm.invoke.globals import Globals # ---------------readline utilities--------------------- try: @@ -127,7 +128,12 @@ class Completer(object): self.matches= self._model_completions(text, state) elif re.search(weight_regexp,buffer): - self.matches = self._path_completions(text, state, WEIGHT_EXTENSIONS) + self.matches = self._path_completions( + text, + state, + WEIGHT_EXTENSIONS, + default_dir=Globals.root, + ) elif re.search(text_regexp,buffer): self.matches = self._path_completions(text, state, TEXT_EXTENSIONS) @@ -278,7 +284,7 @@ class Completer(object): readline.redisplay() self.linebuffer = None - def _path_completions(self, text, state, extensions, shortcut_ok=True): + def _path_completions(self, text, state, extensions, shortcut_ok=True, default_dir:str=''): # separate the switch from the partial path match = re.search('^(-\w|--\w+=?)(.*)',text) if match is None: @@ -297,7 +303,7 @@ class Completer(object): elif os.path.dirname(path) != '': dir = os.path.dirname(path) else: - dir = '' + dir = default_dir if os.path.exists(default_dir) else '' path= os.path.join(dir,path) dir_list = os.listdir(dir or '.')