fix for an error caused by skipping initialization, for realsies this time: TypeError: expected str, bytes or os.PathLike object, not NoneType
This commit is contained in:
parent
45a8b758a7
commit
4bd490727e
|
@ -20,6 +20,19 @@ class DisableInitialization:
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.replaced = []
|
||||||
|
|
||||||
|
def replace(self, obj, field, func):
|
||||||
|
original = getattr(obj, field, None)
|
||||||
|
if original is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
self.replaced.append((obj, field, original))
|
||||||
|
setattr(obj, field, func)
|
||||||
|
|
||||||
|
return original
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
def do_nothing(*args, **kwargs):
|
def do_nothing(*args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
@ -37,11 +50,14 @@ class DisableInitialization:
|
||||||
def transformers_utils_hub_get_file_from_cache(original, url, *args, **kwargs):
|
def transformers_utils_hub_get_file_from_cache(original, url, *args, **kwargs):
|
||||||
|
|
||||||
# this file is always 404, prevent making request
|
# this file is always 404, prevent making request
|
||||||
if url == 'https://huggingface.co/openai/clip-vit-large-patch14/resolve/main/added_tokens.json':
|
if url == 'https://huggingface.co/openai/clip-vit-large-patch14/resolve/main/added_tokens.json' or url == 'openai/clip-vit-large-patch14' and args[0] == 'added_tokens.json':
|
||||||
raise transformers.utils.hub.EntryNotFoundError
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return original(url, *args, local_files_only=True, **kwargs)
|
res = original(url, *args, local_files_only=True, **kwargs)
|
||||||
|
if res is None:
|
||||||
|
res = original(url, *args, local_files_only=False, **kwargs)
|
||||||
|
return res
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return original(url, *args, local_files_only=False, **kwargs)
|
return original(url, *args, local_files_only=False, **kwargs)
|
||||||
|
|
||||||
|
@ -54,42 +70,19 @@ class DisableInitialization:
|
||||||
def transformers_configuration_utils_cached_file(url, *args, local_files_only=False, **kwargs):
|
def transformers_configuration_utils_cached_file(url, *args, local_files_only=False, **kwargs):
|
||||||
return transformers_utils_hub_get_file_from_cache(self.transformers_configuration_utils_cached_file, url, *args, **kwargs)
|
return transformers_utils_hub_get_file_from_cache(self.transformers_configuration_utils_cached_file, url, *args, **kwargs)
|
||||||
|
|
||||||
self.init_kaiming_uniform = torch.nn.init.kaiming_uniform_
|
self.replace(torch.nn.init, 'kaiming_uniform_', do_nothing)
|
||||||
self.init_no_grad_normal = torch.nn.init._no_grad_normal_
|
self.replace(torch.nn.init, '_no_grad_normal_', do_nothing)
|
||||||
self.init_no_grad_uniform_ = torch.nn.init._no_grad_uniform_
|
self.replace(torch.nn.init, '_no_grad_uniform_', do_nothing)
|
||||||
self.create_model_and_transforms = open_clip.create_model_and_transforms
|
self.create_model_and_transforms = self.replace(open_clip, 'create_model_and_transforms', create_model_and_transforms_without_pretrained)
|
||||||
self.CLIPTextModel_from_pretrained = ldm.modules.encoders.modules.CLIPTextModel.from_pretrained
|
self.CLIPTextModel_from_pretrained = self.replace(ldm.modules.encoders.modules.CLIPTextModel, 'from_pretrained', CLIPTextModel_from_pretrained)
|
||||||
self.transformers_modeling_utils_load_pretrained_model = getattr(transformers.modeling_utils.PreTrainedModel, '_load_pretrained_model', None)
|
self.transformers_modeling_utils_load_pretrained_model = self.replace(transformers.modeling_utils.PreTrainedModel, '_load_pretrained_model', transformers_modeling_utils_load_pretrained_model)
|
||||||
self.transformers_tokenization_utils_base_cached_file = getattr(transformers.tokenization_utils_base, 'cached_file', None)
|
self.transformers_tokenization_utils_base_cached_file = self.replace(transformers.tokenization_utils_base, 'cached_file', transformers_tokenization_utils_base_cached_file)
|
||||||
self.transformers_configuration_utils_cached_file = getattr(transformers.configuration_utils, 'cached_file', None)
|
self.transformers_configuration_utils_cached_file = self.replace(transformers.configuration_utils, 'cached_file', transformers_configuration_utils_cached_file)
|
||||||
self.transformers_utils_hub_get_from_cache = getattr(transformers.utils.hub, 'get_from_cache', None)
|
self.transformers_utils_hub_get_from_cache = self.replace(transformers.utils.hub, 'get_from_cache', transformers_utils_hub_get_from_cache)
|
||||||
|
|
||||||
torch.nn.init.kaiming_uniform_ = do_nothing
|
|
||||||
torch.nn.init._no_grad_normal_ = do_nothing
|
|
||||||
torch.nn.init._no_grad_uniform_ = do_nothing
|
|
||||||
open_clip.create_model_and_transforms = create_model_and_transforms_without_pretrained
|
|
||||||
ldm.modules.encoders.modules.CLIPTextModel.from_pretrained = CLIPTextModel_from_pretrained
|
|
||||||
if self.transformers_modeling_utils_load_pretrained_model is not None:
|
|
||||||
transformers.modeling_utils.PreTrainedModel._load_pretrained_model = transformers_modeling_utils_load_pretrained_model
|
|
||||||
if self.transformers_tokenization_utils_base_cached_file is not None:
|
|
||||||
transformers.tokenization_utils_base.cached_file = transformers_tokenization_utils_base_cached_file
|
|
||||||
if self.transformers_configuration_utils_cached_file is not None:
|
|
||||||
transformers.configuration_utils.cached_file = transformers_configuration_utils_cached_file
|
|
||||||
if self.transformers_utils_hub_get_from_cache is not None:
|
|
||||||
transformers.utils.hub.get_from_cache = transformers_utils_hub_get_from_cache
|
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
torch.nn.init.kaiming_uniform_ = self.init_kaiming_uniform
|
for obj, field, original in self.replaced:
|
||||||
torch.nn.init._no_grad_normal_ = self.init_no_grad_normal
|
setattr(obj, field, original)
|
||||||
torch.nn.init._no_grad_uniform_ = self.init_no_grad_uniform_
|
|
||||||
open_clip.create_model_and_transforms = self.create_model_and_transforms
|
self.replaced.clear()
|
||||||
ldm.modules.encoders.modules.CLIPTextModel.from_pretrained = self.CLIPTextModel_from_pretrained
|
|
||||||
if self.transformers_modeling_utils_load_pretrained_model is not None:
|
|
||||||
transformers.modeling_utils.PreTrainedModel._load_pretrained_model = self.transformers_modeling_utils_load_pretrained_model
|
|
||||||
if self.transformers_tokenization_utils_base_cached_file is not None:
|
|
||||||
transformers.utils.hub.cached_file = self.transformers_tokenization_utils_base_cached_file
|
|
||||||
if self.transformers_configuration_utils_cached_file is not None:
|
|
||||||
transformers.utils.hub.cached_file = self.transformers_configuration_utils_cached_file
|
|
||||||
if self.transformers_utils_hub_get_from_cache is not None:
|
|
||||||
transformers.utils.hub.get_from_cache = self.transformers_utils_hub_get_from_cache
|
|
||||||
|
|
||||||
|
|
|
@ -334,6 +334,7 @@ def load_model(checkpoint_info=None):
|
||||||
timer = Timer()
|
timer = Timer()
|
||||||
|
|
||||||
sd_model = None
|
sd_model = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with sd_disable_initialization.DisableInitialization():
|
with sd_disable_initialization.DisableInitialization():
|
||||||
sd_model = instantiate_from_config(sd_config.model)
|
sd_model = instantiate_from_config(sd_config.model)
|
||||||
|
|
Loading…
Reference in New Issue