2023-05-05 23:32:28 +00:00
|
|
|
"""This module manages the InvokeAI `models.yaml` file, mapping
|
|
|
|
symbolic diffusers model names to the paths and repo_ids used
|
|
|
|
by the underlying `from_pretrained()` call.
|
|
|
|
|
|
|
|
For fetching models, use manager.get_model('symbolic name'). This will
|
|
|
|
return a SDModelInfo object that contains the following attributes:
|
|
|
|
|
|
|
|
* context -- a context manager Generator that loads and locks the
|
|
|
|
model into GPU VRAM and returns the model for use.
|
|
|
|
See below for usage.
|
|
|
|
* name -- symbolic name of the model
|
2023-05-13 18:44:44 +00:00
|
|
|
* type -- SDModelType of the model
|
2023-05-05 23:32:28 +00:00
|
|
|
* hash -- unique hash for the model
|
|
|
|
* location -- path or repo_id of the model
|
|
|
|
* revision -- revision of the model if coming from a repo id,
|
|
|
|
e.g. 'fp16'
|
|
|
|
* precision -- torch precision of the model
|
|
|
|
|
|
|
|
Typical usage:
|
|
|
|
|
|
|
|
from invokeai.backend import ModelManager
|
2023-05-08 03:18:17 +00:00
|
|
|
|
|
|
|
manager = ModelManager(
|
|
|
|
config='./configs/models.yaml',
|
|
|
|
max_cache_size=8
|
|
|
|
) # gigabytes
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
model_info = manager.get_model('stable-diffusion-1.5', SDModelType.Diffusers)
|
2023-05-05 23:32:28 +00:00
|
|
|
with model_info.context as my_model:
|
|
|
|
my_model.latents_from_embeddings(...)
|
|
|
|
|
|
|
|
The manager uses the underlying ModelCache class to keep
|
|
|
|
frequently-used models in RAM and move them into GPU as needed for
|
2023-05-08 03:18:17 +00:00
|
|
|
generation operations. The optional `max_cache_size` argument
|
|
|
|
indicates the maximum size the cache can grow to, in gigabytes. The
|
|
|
|
underlying ModelCache object can be accessed using the manager's "cache"
|
|
|
|
attribute.
|
2023-05-05 23:32:28 +00:00
|
|
|
|
2023-05-08 03:18:17 +00:00
|
|
|
Because the model manager can return multiple different types of
|
|
|
|
models, you may wish to add additional type checking on the class
|
|
|
|
of model returned. To do this, provide the option `model_type`
|
|
|
|
parameter:
|
|
|
|
|
|
|
|
model_info = manager.get_model(
|
|
|
|
'clip-tokenizer',
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type=SDModelType.Tokenizer
|
2023-05-08 03:18:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
This will raise an InvalidModelError if the format defined in the
|
|
|
|
config file doesn't match the requested model type.
|
|
|
|
|
|
|
|
MODELS.YAML
|
2023-05-06 19:58:44 +00:00
|
|
|
|
|
|
|
The general format of a models.yaml section is:
|
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
type-of-model/name-of-model:
|
2023-05-06 19:58:44 +00:00
|
|
|
path: /path/to/local/file/or/directory
|
2023-06-09 03:11:53 +00:00
|
|
|
description: a description
|
|
|
|
format: folder|ckpt|safetensors|pt
|
|
|
|
base: SD-1|SD-2
|
2023-05-06 19:58:44 +00:00
|
|
|
subfolder: subfolder-name
|
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
The type of model is given in the stanza key, and is one of
|
|
|
|
{diffusers, ckpt, vae, text_encoder, tokenizer, unet, scheduler,
|
2023-06-09 03:11:53 +00:00
|
|
|
safety_checker, feature_extractor, lora, textual_inversion,
|
|
|
|
controlnet}, and correspond to items in the SDModelType enum defined
|
|
|
|
in model_cache.py
|
2023-05-13 18:44:44 +00:00
|
|
|
|
|
|
|
The format indicates whether the model is organized as a folder with
|
|
|
|
model subdirectories, or is contained in a single checkpoint or
|
|
|
|
safetensors file.
|
2023-05-06 19:58:44 +00:00
|
|
|
|
|
|
|
One, but not both, of repo_id and path are provided. repo_id is the
|
|
|
|
HuggingFace repository ID of the model, and path points to the file or
|
|
|
|
directory on disk.
|
|
|
|
|
|
|
|
If subfolder is provided, then the model exists in a subdirectory of
|
|
|
|
the main model. These are usually named after the model type, such as
|
|
|
|
"unet".
|
|
|
|
|
2023-05-08 03:18:17 +00:00
|
|
|
This example summarizes the two ways of getting a non-diffuser model:
|
2023-05-06 19:58:44 +00:00
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
text_encoder/clip-test-1:
|
|
|
|
format: folder
|
2023-06-09 03:11:53 +00:00
|
|
|
path: /path/to/folder
|
2023-05-06 19:58:44 +00:00
|
|
|
description: Returns standalone CLIPTextModel
|
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
text_encoder/clip-test-2:
|
|
|
|
format: folder
|
2023-06-09 03:11:53 +00:00
|
|
|
repo_id: /path/to/folder
|
2023-05-06 19:58:44 +00:00
|
|
|
subfolder: text_encoder
|
|
|
|
description: Returns the text_encoder in the subfolder of the diffusers model (just the encoder in RAM)
|
|
|
|
|
2023-05-08 03:18:17 +00:00
|
|
|
SUBMODELS:
|
|
|
|
|
|
|
|
It is also possible to fetch an isolated submodel from a diffusers
|
|
|
|
model. Use the `submodel` parameter to select which part:
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
vae = manager.get_model('stable-diffusion-1.5',submodel=SDModelType.Vae)
|
2023-05-08 03:18:17 +00:00
|
|
|
with vae.context as my_vae:
|
|
|
|
print(type(my_vae))
|
|
|
|
# "AutoencoderKL"
|
|
|
|
|
2023-06-09 03:11:53 +00:00
|
|
|
DIRECTORY_SCANNING:
|
|
|
|
|
|
|
|
Loras, textual_inversion and controlnet models are usually not listed
|
|
|
|
explicitly in models.yaml, but are added to the in-memory data
|
|
|
|
structure at initialization time by scanning the models directory. The
|
|
|
|
in-memory data structure can be resynchronized by calling
|
|
|
|
`manager.scan_models_directory`.
|
|
|
|
|
2023-05-08 03:18:17 +00:00
|
|
|
DISAMBIGUATION:
|
|
|
|
|
|
|
|
You may wish to use the same name for a related family of models. To
|
|
|
|
do this, disambiguate the stanza key with the model and and format
|
|
|
|
separated by "/". Example:
|
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
tokenizer/clip-large:
|
2023-05-06 19:58:44 +00:00
|
|
|
format: tokenizer
|
2023-06-09 03:11:53 +00:00
|
|
|
path: /path/to/folder
|
2023-05-06 19:58:44 +00:00
|
|
|
description: Returns standalone tokenizer
|
2023-05-08 03:18:17 +00:00
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
text_encoder/clip-large:
|
2023-05-08 03:18:17 +00:00
|
|
|
format: text_encoder
|
2023-06-09 03:11:53 +00:00
|
|
|
path: /path/to/folder
|
2023-05-08 03:18:17 +00:00
|
|
|
description: Returns standalone text encoder
|
|
|
|
|
|
|
|
You can now use the `model_type` argument to indicate which model you
|
|
|
|
want:
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
tokenizer = mgr.get('clip-large',model_type=SDModelType.Tokenizer)
|
|
|
|
encoder = mgr.get('clip-large',model_type=SDModelType.TextEncoder)
|
2023-05-08 03:18:17 +00:00
|
|
|
|
|
|
|
OTHER FUNCTIONS:
|
|
|
|
|
|
|
|
Other methods provided by ModelManager support importing, editing,
|
|
|
|
converting and deleting models.
|
|
|
|
|
2023-06-09 03:11:53 +00:00
|
|
|
IMPORTANT CHANGES AND LIMITATIONS SINCE 2.3:
|
|
|
|
|
|
|
|
1. Only local paths are supported. Repo_ids are no longer accepted. This
|
|
|
|
simplifies the logic.
|
|
|
|
|
|
|
|
2. VAEs can't be swapped in and out at load time. They must be baked
|
|
|
|
into the model when downloaded or converted.
|
|
|
|
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import textwrap
|
2023-06-01 04:31:46 +00:00
|
|
|
import shutil
|
2023-05-22 21:51:47 +00:00
|
|
|
import traceback
|
2023-05-05 23:32:28 +00:00
|
|
|
from dataclasses import dataclass
|
2023-04-05 21:25:42 +00:00
|
|
|
from enum import Enum, auto
|
2023-05-13 18:44:44 +00:00
|
|
|
from packaging import version
|
2023-02-28 05:31:15 +00:00
|
|
|
from pathlib import Path
|
2023-06-09 03:11:53 +00:00
|
|
|
from typing import Callable, Dict, Optional, List, Tuple, Union, types
|
|
|
|
from shutil import rmtree
|
2023-02-28 05:31:15 +00:00
|
|
|
|
|
|
|
import safetensors
|
|
|
|
import safetensors.torch
|
|
|
|
import torch
|
2023-05-14 22:09:38 +00:00
|
|
|
from diffusers import AutoencoderKL
|
2023-02-28 05:31:15 +00:00
|
|
|
from huggingface_hub import scan_cache_dir
|
|
|
|
from omegaconf import OmegaConf
|
|
|
|
from omegaconf.dictconfig import DictConfig
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
import invokeai.backend.util.logging as logger
|
2023-05-26 00:41:26 +00:00
|
|
|
from invokeai.app.services.config import InvokeAIAppConfig
|
2023-06-09 03:11:53 +00:00
|
|
|
from invokeai.backend.util import CUDA_DEVICE, download_with_resume
|
2023-06-09 01:08:43 +00:00
|
|
|
from ..install.model_install_backend import Dataset_path, hf_download_with_resume
|
2023-06-10 00:14:10 +00:00
|
|
|
from .model_cache import ModelCache, ModelLocker, SilenceWarnings
|
|
|
|
from .models import BaseModelType, ModelType, SubModelType, MODEL_CLASSES
|
2023-05-13 18:44:44 +00:00
|
|
|
# We are only starting to number the config file with release 3.
|
|
|
|
# The config file version doesn't have to start at release version, but it will help
|
|
|
|
# reduce confusion.
|
|
|
|
CONFIG_FILE_VERSION='3.0.0'
|
2023-05-05 23:32:28 +00:00
|
|
|
|
|
|
|
# wanted to use pydantic here, but Generator objects not supported
|
|
|
|
@dataclass
|
|
|
|
class SDModelInfo():
|
2023-05-06 19:58:44 +00:00
|
|
|
context: ModelLocker
|
2023-05-05 23:32:28 +00:00
|
|
|
name: str
|
2023-05-13 18:44:44 +00:00
|
|
|
type: SDModelType
|
2023-05-05 23:32:28 +00:00
|
|
|
hash: str
|
|
|
|
location: Union[Path,str]
|
|
|
|
precision: torch.dtype
|
|
|
|
revision: str = None
|
|
|
|
_cache: ModelCache = None
|
|
|
|
|
2023-05-13 20:29:18 +00:00
|
|
|
def __enter__(self):
|
|
|
|
return self.context.__enter__()
|
|
|
|
|
|
|
|
def __exit__(self,*args, **kwargs):
|
|
|
|
self.context.__exit__(*args, **kwargs)
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-05-05 23:32:28 +00:00
|
|
|
class InvalidModelError(Exception):
|
|
|
|
"Raised when an invalid model is requested"
|
|
|
|
pass
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-05-07 22:07:28 +00:00
|
|
|
MAX_CACHE_SIZE = 6.0 # GB
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-06-09 03:11:53 +00:00
|
|
|
|
|
|
|
# layout of the models directory:
|
|
|
|
# models
|
|
|
|
# ├── SD-1
|
|
|
|
# │ ├── controlnet
|
|
|
|
# │ ├── lora
|
|
|
|
# │ ├── diffusers
|
|
|
|
# │ └── textual_inversion
|
|
|
|
# ├── SD-2
|
|
|
|
# │ ├── controlnet
|
|
|
|
# │ ├── lora
|
|
|
|
# │ ├── diffusers
|
|
|
|
# │ └── textual_inversion
|
|
|
|
# └── support
|
|
|
|
# ├── codeformer
|
|
|
|
# ├── gfpgan
|
|
|
|
# └── realesrgan
|
|
|
|
|
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
class ConfigMeta(BaseModel):
|
|
|
|
version: str
|
|
|
|
|
2023-02-28 05:31:15 +00:00
|
|
|
class ModelManager(object):
|
2023-04-05 21:25:42 +00:00
|
|
|
"""
|
2023-05-05 23:32:28 +00:00
|
|
|
High-level interface to model management.
|
2023-04-05 21:25:42 +00:00
|
|
|
"""
|
|
|
|
|
2023-04-29 14:48:50 +00:00
|
|
|
logger: types.ModuleType = logger
|
|
|
|
|
2023-02-28 05:31:15 +00:00
|
|
|
def __init__(
|
2023-05-12 20:13:34 +00:00
|
|
|
self,
|
|
|
|
config: Union[Path, DictConfig, str],
|
|
|
|
device_type: torch.device = CUDA_DEVICE,
|
|
|
|
precision: torch.dtype = torch.float16,
|
|
|
|
max_cache_size=MAX_CACHE_SIZE,
|
|
|
|
sequential_offload=False,
|
|
|
|
logger: types.ModuleType = logger,
|
2023-02-28 05:31:15 +00:00
|
|
|
):
|
|
|
|
"""
|
2023-05-05 23:32:28 +00:00
|
|
|
Initialize with the path to the models.yaml config file.
|
|
|
|
Optional parameters are the torch device type, precision, max_models,
|
2023-04-05 21:25:42 +00:00
|
|
|
and sequential_offload boolean. Note that the default device
|
2023-03-09 06:09:54 +00:00
|
|
|
type and precision are set up for a CUDA system running at half precision.
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
2023-06-11 01:49:09 +00:00
|
|
|
|
|
|
|
self.config_path = None
|
|
|
|
if isinstance(config, (str, Path)):
|
|
|
|
self.config_path = Path(config)
|
|
|
|
config = OmegaConf.load(self.config_path)
|
|
|
|
|
|
|
|
elif not isinstance(config, DictConfig):
|
2023-05-08 03:18:17 +00:00
|
|
|
raise ValueError('config argument must be an OmegaConf object, a Path or a string')
|
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
config_meta = ConfigMeta(config.pop("__metadata__")) # TODO: naming
|
|
|
|
# TODO: metadata not found
|
|
|
|
|
|
|
|
self.models = dict()
|
|
|
|
for model_key, model_config in config.items():
|
|
|
|
model_name, base_model, model_type = self.parse_key(model_key)
|
|
|
|
model_class = MODEL_CLASSES[base_model][model_type]
|
|
|
|
self.models[model_key] = model_class.build_config(**model_config)
|
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
# check config version number and update on disk/RAM if necessary
|
2023-05-26 00:41:26 +00:00
|
|
|
self.globals = InvokeAIAppConfig.get_config()
|
2023-05-18 21:17:45 +00:00
|
|
|
self._update_config_file_version()
|
2023-04-29 14:48:50 +00:00
|
|
|
self.logger = logger
|
2023-05-05 23:32:28 +00:00
|
|
|
self.cache = ModelCache(
|
2023-05-07 22:07:28 +00:00
|
|
|
max_cache_size=max_cache_size,
|
2023-05-05 23:32:28 +00:00
|
|
|
execution_device = device_type,
|
|
|
|
precision = precision,
|
|
|
|
sequential_offload = sequential_offload,
|
|
|
|
logger = logger,
|
|
|
|
)
|
2023-05-06 19:58:44 +00:00
|
|
|
self.cache_keys = dict()
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-06-09 03:11:53 +00:00
|
|
|
# add controlnet, lora and textual_inversion models from disk
|
|
|
|
self.scan_models_directory(include_diffusers=False)
|
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
def model_exists(
|
2023-05-12 20:13:34 +00:00
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-10 00:14:10 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
2023-05-12 20:13:34 +00:00
|
|
|
) -> bool:
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
|
|
|
Given a model name, returns True if it is a valid
|
|
|
|
identifier.
|
|
|
|
"""
|
2023-06-10 00:14:10 +00:00
|
|
|
model_key = self.create_key(model_name, base_model, model_type)
|
2023-06-11 01:49:09 +00:00
|
|
|
return model_key in self.models
|
2023-05-12 20:13:34 +00:00
|
|
|
|
2023-06-10 00:14:10 +00:00
|
|
|
def create_key(
|
|
|
|
self,
|
|
|
|
model_name: str,
|
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
|
|
|
) -> str:
|
|
|
|
return f"{base_model}/{model_type}/{model_name}"
|
2023-05-12 20:13:34 +00:00
|
|
|
|
2023-06-10 00:14:10 +00:00
|
|
|
def parse_key(self, model_key: str) -> Tuple[str, BaseModelType, ModelType]:
|
|
|
|
base_model_str, model_type_str, model_name = model_key.split('/', 2)
|
2023-05-14 00:06:26 +00:00
|
|
|
try:
|
|
|
|
model_type = SDModelType(model_type_str)
|
|
|
|
except:
|
2023-05-13 18:44:44 +00:00
|
|
|
raise Exception(f"Unknown model type: {model_type_str}")
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-06-10 00:14:10 +00:00
|
|
|
try:
|
|
|
|
base_model = BaseModelType(base_model_str)
|
|
|
|
except:
|
|
|
|
raise Exception(f"Unknown base model: {base_model_str}")
|
|
|
|
|
|
|
|
return (model_name, base_model, model_type)
|
|
|
|
|
2023-05-12 20:13:34 +00:00
|
|
|
def get_model(
|
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-10 00:14:10 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
|
|
|
submodel_type: Optional[SubModelType] = None
|
|
|
|
):
|
2023-05-05 23:32:28 +00:00
|
|
|
"""Given a model named identified in models.yaml, return
|
|
|
|
an SDModelInfo object describing it.
|
|
|
|
:param model_name: symbolic name of the model in models.yaml
|
2023-05-08 03:18:17 +00:00
|
|
|
:param model_type: SDModelType enum indicating the type of model to return
|
2023-05-05 23:32:28 +00:00
|
|
|
:param submodel: an SDModelType enum indicating the portion of
|
2023-05-14 00:06:26 +00:00
|
|
|
the model to retrieve (e.g. SDModelType.Vae)
|
2023-05-08 03:18:17 +00:00
|
|
|
|
|
|
|
If not provided, the model_type will be read from the `format` field
|
|
|
|
of the corresponding stanza. If provided, the model_type will be used
|
|
|
|
to disambiguate stanzas in the configuration file. The default is to
|
|
|
|
assume a diffusers pipeline. The behavior is illustrated here:
|
|
|
|
|
|
|
|
[models.yaml]
|
2023-05-12 20:13:34 +00:00
|
|
|
diffusers/test1:
|
2023-05-08 03:18:17 +00:00
|
|
|
repo_id: foo/bar
|
|
|
|
description: Typical diffusers pipeline
|
|
|
|
|
2023-05-12 20:13:34 +00:00
|
|
|
lora/test1:
|
2023-05-08 03:18:17 +00:00
|
|
|
repo_id: /tmp/loras/test1.safetensors
|
|
|
|
description: Typical lora file
|
|
|
|
|
|
|
|
test1_pipeline = mgr.get_model('test1')
|
|
|
|
# returns a StableDiffusionGeneratorPipeline
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
test1_vae1 = mgr.get_model('test1', submodel=SDModelType.Vae)
|
2023-05-08 03:18:17 +00:00
|
|
|
# returns the VAE part of a diffusers model as an AutoencoderKL
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
test1_vae2 = mgr.get_model('test1', model_type=SDModelType.Diffusers, submodel=SDModelType.Vae)
|
2023-05-08 03:18:17 +00:00
|
|
|
# does the same thing as the previous statement. Note that model_type
|
|
|
|
# is for the parent model, and submodel is for the part
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
test1_lora = mgr.get_model('test1', model_type=SDModelType.Lora)
|
2023-05-08 03:18:17 +00:00
|
|
|
# returns a LoRA embed (as a 'dict' of tensors)
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
test1_encoder = mgr.get_modelI('test1', model_type=SDModelType.TextEncoder)
|
2023-05-08 03:18:17 +00:00
|
|
|
# raises an InvalidModelError
|
|
|
|
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
2023-06-10 00:14:10 +00:00
|
|
|
|
|
|
|
model_class = MODEL_CLASSES[base_model][model_type]
|
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
model_key = self.create_key(model_name, base_model, model_type)
|
|
|
|
|
|
|
|
# if model not found try to find it (maybe file just pasted)
|
|
|
|
if model_key not in self.models:
|
|
|
|
# TODO: find by mask or try rescan?
|
|
|
|
path_mask = f"/models/{base_model}/{model_type}/{model_name}*"
|
|
|
|
if False: # model_path = next(find_by_mask(path_mask)):
|
|
|
|
model_path = None # TODO:
|
|
|
|
model_config = model_class.build_config(
|
|
|
|
path=model_path,
|
|
|
|
)
|
|
|
|
self.models[model_key] = model_config
|
2023-05-09 03:39:44 +00:00
|
|
|
else:
|
2023-06-11 01:49:09 +00:00
|
|
|
raise Exception(f"Model not found - {model_key}")
|
2023-06-10 00:14:10 +00:00
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
# if it known model check that target path exists (if manualy deleted)
|
2023-05-29 22:11:00 +00:00
|
|
|
else:
|
2023-06-11 01:49:09 +00:00
|
|
|
# logic repeated twice(in rescan too) any way to optimize?
|
|
|
|
if not os.path.exists(self.models[model_key].path):
|
|
|
|
if model_class.save_to_config:
|
|
|
|
self.models[model_key].error = ModelError.NotFound
|
|
|
|
raise Exception(f"Files for model \"{model_key}\" not found")
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.models.pop(model_key, None)
|
|
|
|
raise Exception(f"Model not found - {model_key}")
|
|
|
|
|
|
|
|
# reset model errors?
|
|
|
|
|
2023-06-10 00:14:10 +00:00
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
|
|
|
|
model_config = self.models[model_key]
|
2023-06-10 00:14:10 +00:00
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
# /models/{base_model}/{model_type}/{name}.ckpt or .safentesors
|
|
|
|
# /models/{base_model}/{model_type}/{name}/
|
|
|
|
model_path = model_config.path
|
2023-06-10 00:14:10 +00:00
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
# vae/movq override
|
|
|
|
# TODO:
|
|
|
|
if submodel is not None and submodel in model_config:
|
|
|
|
model_path = model_config[submodel]
|
|
|
|
model_type = submodel
|
|
|
|
submodel = None
|
2023-06-10 00:14:10 +00:00
|
|
|
|
|
|
|
dst_convert_path = None # TODO:
|
|
|
|
model_path = model_class.convert_if_required(
|
|
|
|
model_path,
|
|
|
|
dst_convert_path,
|
|
|
|
model_config,
|
|
|
|
)
|
2023-05-18 00:56:52 +00:00
|
|
|
|
2023-05-05 23:32:28 +00:00
|
|
|
model_context = self.cache.get_model(
|
2023-06-10 00:14:10 +00:00
|
|
|
model_path,
|
|
|
|
model_class,
|
|
|
|
submodel,
|
2023-05-05 23:32:28 +00:00
|
|
|
)
|
2023-05-06 19:58:44 +00:00
|
|
|
|
2023-06-10 00:14:10 +00:00
|
|
|
hash = "<NO_HASH>" # TODO:
|
|
|
|
|
2023-05-05 23:32:28 +00:00
|
|
|
return SDModelInfo(
|
|
|
|
context = model_context,
|
|
|
|
name = model_name,
|
2023-06-10 00:14:10 +00:00
|
|
|
base_model = base_model,
|
2023-05-13 18:44:44 +00:00
|
|
|
type = submodel or model_type,
|
2023-05-05 23:32:28 +00:00
|
|
|
hash = hash,
|
2023-06-10 00:14:10 +00:00
|
|
|
location = model_path, # TODO:
|
2023-05-05 23:32:28 +00:00
|
|
|
precision = self.cache.precision,
|
2023-06-10 00:14:10 +00:00
|
|
|
_cache = self.cache,
|
2023-05-05 23:32:28 +00:00
|
|
|
)
|
2023-04-05 21:25:42 +00:00
|
|
|
|
2023-06-10 00:14:10 +00:00
|
|
|
def default_model(self) -> Optional[Tuple[str, BaseModelType, ModelType]]:
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
|
|
|
Returns the name of the default model, or None
|
|
|
|
if none is defined.
|
|
|
|
"""
|
2023-06-11 01:49:09 +00:00
|
|
|
for model_key, model_config in self.models.items():
|
|
|
|
if model_config.default:
|
2023-06-10 00:14:10 +00:00
|
|
|
return self.parse_key(model_key)
|
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
for model_key, _ in self.models.items():
|
2023-06-10 00:14:10 +00:00
|
|
|
return self.parse_key(model_key)
|
|
|
|
else:
|
|
|
|
return None # TODO: or redo as (None, None, None)
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-06-10 00:14:10 +00:00
|
|
|
def set_default_model(
|
|
|
|
self,
|
|
|
|
model_name: str,
|
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
|
|
|
) -> None:
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
|
|
|
Set the default model. The change will not take
|
|
|
|
effect until you call model_manager.commit()
|
|
|
|
"""
|
|
|
|
|
2023-06-10 00:14:10 +00:00
|
|
|
model_key = self.model_key(model_name, base_model, model_type)
|
2023-06-11 01:49:09 +00:00
|
|
|
if model_key not in self.models:
|
2023-06-10 00:14:10 +00:00
|
|
|
raise Exception(f"Unknown model: {model_key}")
|
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
for cur_model_key, config in self.models.items():
|
|
|
|
config.default = cur_model_key == model_key
|
2023-05-13 18:44:44 +00:00
|
|
|
|
|
|
|
def model_info(
|
2023-05-14 00:06:26 +00:00
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-10 00:14:10 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
2023-05-13 18:44:44 +00:00
|
|
|
) -> dict:
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
|
|
|
Given a model name returns the OmegaConf (dict-like) object describing it.
|
|
|
|
"""
|
2023-06-10 00:14:10 +00:00
|
|
|
model_key = self.create_key(model_name, base_model, model_type)
|
2023-06-11 01:49:09 +00:00
|
|
|
if model_key in self.models:
|
|
|
|
return self.models[model_key].dict(exclude_defaults=True)
|
|
|
|
else:
|
|
|
|
return None # TODO: None or empty dict on not found
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-06-10 00:14:10 +00:00
|
|
|
def model_names(self) -> List[Tuple[str, BaseModelType, ModelType]]:
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
2023-06-10 00:14:10 +00:00
|
|
|
Return a list of (str, BaseModelType, ModelType) corresponding to all models
|
2023-05-13 18:44:44 +00:00
|
|
|
known to the configuration.
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
2023-06-11 01:49:09 +00:00
|
|
|
return [(self.parse_key(x)) for x in self.models.keys()]
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-06-10 00:14:10 +00:00
|
|
|
def list_models(
|
|
|
|
self,
|
|
|
|
base_model: Optional[BaseModelType] = None,
|
|
|
|
model_type: Optional[SDModelType] = None,
|
|
|
|
) -> Dict[str, Dict[str, str]]:
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
2023-06-10 00:14:10 +00:00
|
|
|
Return a dict of models, in format [base_model][model_type][model_name]
|
2023-05-16 03:44:08 +00:00
|
|
|
|
2023-02-28 05:31:15 +00:00
|
|
|
Please use model_manager.models() to get all the model names,
|
|
|
|
model_manager.model_info('model-name') to get the stanza for the model
|
|
|
|
named 'model-name', and model_manager.config to get the full OmegaConf
|
|
|
|
object derived from models.yaml
|
|
|
|
"""
|
2023-06-10 00:14:10 +00:00
|
|
|
assert not(model_type is not None and base_model is None), "model_type must be provided with base_model"
|
|
|
|
|
|
|
|
models = dict()
|
2023-06-11 01:49:09 +00:00
|
|
|
for model_key in sorted(self.models, key=str.casefold):
|
|
|
|
model_config = self.models[model_key]
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
cur_model_name, cur_base_model, cur_model_type = self.parse_key(model_key)
|
|
|
|
if base_model is not None and cur_base_model != base_model:
|
2023-06-10 00:14:10 +00:00
|
|
|
continue
|
2023-06-11 01:49:09 +00:00
|
|
|
if model_type is not None and cur_model_type != model_type:
|
2023-05-12 20:13:34 +00:00
|
|
|
continue
|
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
if cur_base_model not in models:
|
|
|
|
models[cur_base_model] = dict()
|
|
|
|
if cur_model_type not in models[cur_base_model]:
|
|
|
|
models[cur_base_model][cur_model_type] = dict()
|
2023-06-10 00:14:10 +00:00
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
models[m_base_model][stanza_type][model_name] = dict(
|
|
|
|
**model_config.dict(exclude_defaults=True),
|
2023-06-10 00:14:10 +00:00
|
|
|
name=model_name,
|
2023-06-11 01:49:09 +00:00
|
|
|
base_model=cur_base_model,
|
|
|
|
type=cur_model_type,
|
2023-02-28 05:31:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return models
|
|
|
|
|
|
|
|
def print_models(self) -> None:
|
|
|
|
"""
|
2023-06-11 01:49:09 +00:00
|
|
|
Print a table of models, their descriptions
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
2023-06-11 01:49:09 +00:00
|
|
|
# TODO: redo
|
2023-05-16 03:44:08 +00:00
|
|
|
for model_type, model_dict in self.list_models().items():
|
|
|
|
for model_name, model_info in model_dict.items():
|
2023-06-11 01:49:09 +00:00
|
|
|
line = f'{model_info["name"]:25s} {model_info["type"]:10s} {model_info["description"]}'
|
2023-05-16 03:44:08 +00:00
|
|
|
print(line)
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-05-12 20:13:34 +00:00
|
|
|
def del_model(
|
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type: SDModelType.Diffusers,
|
|
|
|
delete_files: bool = False,
|
2023-05-12 20:13:34 +00:00
|
|
|
):
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
|
|
|
Delete the named model.
|
|
|
|
"""
|
2023-05-12 20:13:34 +00:00
|
|
|
model_key = self.create_key(model_name, model_type)
|
|
|
|
model_cfg = self.pop(model_key, None)
|
|
|
|
|
|
|
|
if model_cfg is None:
|
|
|
|
self.logger.error(
|
2023-05-14 00:06:26 +00:00
|
|
|
f"Unknown model {model_key}"
|
|
|
|
)
|
|
|
|
return
|
2023-05-12 20:13:34 +00:00
|
|
|
|
|
|
|
# TODO: some legacy?
|
|
|
|
#if model_name in self.stack:
|
|
|
|
# self.stack.remove(model_name)
|
|
|
|
|
2023-02-28 05:31:15 +00:00
|
|
|
if delete_files:
|
2023-05-13 18:44:44 +00:00
|
|
|
repo_id = model_cfg.get("repo_id", None)
|
|
|
|
path = self._abs_path(model_cfg.get("path", None))
|
|
|
|
weights = self._abs_path(model_cfg.get("weights", None))
|
2023-05-12 20:13:34 +00:00
|
|
|
if "weights" in model_cfg:
|
|
|
|
weights = self._abs_path(model_cfg["weights"])
|
2023-04-29 14:48:50 +00:00
|
|
|
self.logger.info(f"Deleting file {weights}")
|
2023-02-28 05:31:15 +00:00
|
|
|
Path(weights).unlink(missing_ok=True)
|
2023-05-12 20:13:34 +00:00
|
|
|
|
|
|
|
elif "path" in model_cfg:
|
|
|
|
path = self._abs_path(model_cfg["path"])
|
2023-04-29 14:48:50 +00:00
|
|
|
self.logger.info(f"Deleting directory {path}")
|
2023-02-28 05:31:15 +00:00
|
|
|
rmtree(path, ignore_errors=True)
|
2023-05-12 20:13:34 +00:00
|
|
|
|
|
|
|
elif "repo_id" in model_cfg:
|
|
|
|
repo_id = model_cfg["repo_id"]
|
2023-04-29 14:48:50 +00:00
|
|
|
self.logger.info(f"Deleting the cached model directory for {repo_id}")
|
2023-02-28 05:31:15 +00:00
|
|
|
self._delete_model_from_cache(repo_id)
|
|
|
|
|
|
|
|
def add_model(
|
2023-05-12 20:13:34 +00:00
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-10 00:14:10 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
2023-05-12 20:13:34 +00:00
|
|
|
model_attributes: dict,
|
2023-05-14 00:06:26 +00:00
|
|
|
clobber: bool = False,
|
2023-02-28 05:31:15 +00:00
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Update the named model with a dictionary of attributes. Will fail with an
|
|
|
|
assertion error if the name already exists. Pass clobber=True to overwrite.
|
|
|
|
On a successful update, the config will be changed in memory and the
|
|
|
|
method will return True. Will fail with an assertion error if provided
|
|
|
|
attributes are incorrect or the model name is missing.
|
|
|
|
"""
|
2023-05-12 20:13:34 +00:00
|
|
|
|
2023-06-10 00:14:10 +00:00
|
|
|
model_class = MODEL_CLASSES[base_model][model_type]
|
2023-06-11 01:49:09 +00:00
|
|
|
model_config = model_class.build_config(**model_attributes)
|
2023-06-10 00:14:10 +00:00
|
|
|
model_key = self.create_key(model_name, base_model, model_type)
|
2023-05-08 03:18:17 +00:00
|
|
|
|
2023-02-28 05:31:15 +00:00
|
|
|
assert (
|
2023-06-11 01:49:09 +00:00
|
|
|
clobber or model_key not in self.models
|
2023-05-08 03:18:17 +00:00
|
|
|
), f'attempt to overwrite existing model definition "{model_key}"'
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
self.models[model_key] = model_config
|
2023-05-06 19:58:44 +00:00
|
|
|
|
2023-05-08 03:18:17 +00:00
|
|
|
if clobber and model_key in self.cache_keys:
|
2023-06-10 00:14:10 +00:00
|
|
|
# TODO:
|
2023-05-08 03:18:17 +00:00
|
|
|
self.cache.uncache_model(self.cache_keys[model_key])
|
|
|
|
del self.cache_keys[model_key]
|
2023-02-28 05:31:15 +00:00
|
|
|
|
|
|
|
def import_diffuser_model(
|
|
|
|
self,
|
|
|
|
repo_or_path: Union[str, Path],
|
|
|
|
model_name: str = None,
|
2023-03-06 04:37:59 +00:00
|
|
|
description: str = None,
|
2023-02-28 05:31:15 +00:00
|
|
|
vae: dict = None,
|
|
|
|
commit_to_conf: Path = None,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Attempts to install the indicated diffuser model and returns True if successful.
|
|
|
|
|
|
|
|
"repo_or_path" can be either a repo-id or a path-like object corresponding to the
|
|
|
|
top of a downloaded diffusers directory.
|
|
|
|
|
|
|
|
You can optionally provide a model name and/or description. If not provided,
|
|
|
|
then these will be derived from the repo name. If you provide a commit_to_conf
|
|
|
|
path to the configuration file, then the new entry will be committed to the
|
|
|
|
models.yaml file.
|
|
|
|
"""
|
|
|
|
model_name = model_name or Path(repo_or_path).stem
|
2023-04-05 21:25:42 +00:00
|
|
|
model_description = description or f"Imported diffusers model {model_name}"
|
2023-02-28 05:31:15 +00:00
|
|
|
new_config = dict(
|
|
|
|
description=model_description,
|
|
|
|
vae=vae,
|
|
|
|
format="diffusers",
|
|
|
|
)
|
|
|
|
if isinstance(repo_or_path, Path) and repo_or_path.exists():
|
|
|
|
new_config.update(path=str(repo_or_path))
|
|
|
|
else:
|
|
|
|
new_config.update(repo_id=repo_or_path)
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
self.add_model(model_name, SDModelType.Diffusers, new_config, True)
|
2023-02-28 05:31:15 +00:00
|
|
|
if commit_to_conf:
|
|
|
|
self.commit(commit_to_conf)
|
2023-05-14 00:06:26 +00:00
|
|
|
return self.create_key(model_name, SDModelType.Diffusers)
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-05-06 19:58:44 +00:00
|
|
|
def import_lora(
|
2023-05-12 20:13:34 +00:00
|
|
|
self,
|
|
|
|
path: Path,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_name: Optional[str] = None,
|
|
|
|
description: Optional[str] = None,
|
2023-05-06 19:58:44 +00:00
|
|
|
):
|
|
|
|
"""
|
|
|
|
Creates an entry for the indicated lora file. Call
|
|
|
|
mgr.commit() to write out the configuration to models.yaml
|
|
|
|
"""
|
|
|
|
path = Path(path)
|
|
|
|
model_name = model_name or path.stem
|
|
|
|
model_description = description or f"LoRA model {model_name}"
|
2023-05-12 20:13:34 +00:00
|
|
|
self.add_model(
|
2023-05-13 18:44:44 +00:00
|
|
|
model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
SDModelType.Lora,
|
2023-05-12 20:13:34 +00:00
|
|
|
dict(
|
|
|
|
format="lora",
|
|
|
|
weights=str(path),
|
|
|
|
description=model_description,
|
|
|
|
),
|
|
|
|
True
|
|
|
|
)
|
2023-05-06 19:58:44 +00:00
|
|
|
|
|
|
|
def import_embedding(
|
2023-05-12 20:13:34 +00:00
|
|
|
self,
|
|
|
|
path: Path,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_name: Optional[str] = None,
|
|
|
|
description: Optional[str] = None,
|
2023-05-06 19:58:44 +00:00
|
|
|
):
|
|
|
|
"""
|
|
|
|
Creates an entry for the indicated lora file. Call
|
|
|
|
mgr.commit() to write out the configuration to models.yaml
|
|
|
|
"""
|
|
|
|
path = Path(path)
|
|
|
|
if path.is_directory() and (path / "learned_embeds.bin").exists():
|
|
|
|
weights = path / "learned_embeds.bin"
|
|
|
|
else:
|
|
|
|
weights = path
|
|
|
|
|
|
|
|
model_name = model_name or path.stem
|
|
|
|
model_description = description or f"Textual embedding model {model_name}"
|
2023-05-12 20:13:34 +00:00
|
|
|
self.add_model(
|
2023-05-13 18:44:44 +00:00
|
|
|
model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
SDModelType.TextualInversion,
|
2023-05-12 20:13:34 +00:00
|
|
|
dict(
|
|
|
|
format="textual_inversion",
|
|
|
|
weights=str(weights),
|
|
|
|
description=model_description,
|
|
|
|
),
|
|
|
|
True
|
|
|
|
)
|
2023-05-09 03:39:44 +00:00
|
|
|
|
2023-02-28 05:31:15 +00:00
|
|
|
def convert_and_import(
|
2023-04-05 21:25:42 +00:00
|
|
|
self,
|
|
|
|
ckpt_path: Path,
|
|
|
|
diffusers_path: Path,
|
|
|
|
model_name=None,
|
|
|
|
model_description=None,
|
|
|
|
vae: dict = None,
|
|
|
|
vae_path: Path = None,
|
|
|
|
original_config_file: Path = None,
|
|
|
|
commit_to_conf: Path = None,
|
|
|
|
scan_needed: bool = True,
|
2023-02-28 05:31:15 +00:00
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Convert a legacy ckpt weights file to diffuser model and import
|
|
|
|
into models.yaml.
|
|
|
|
"""
|
|
|
|
ckpt_path = self._resolve_path(ckpt_path, "models/ldm/stable-diffusion-v1")
|
|
|
|
if original_config_file:
|
|
|
|
original_config_file = self._resolve_path(
|
|
|
|
original_config_file, "configs/stable-diffusion"
|
|
|
|
)
|
|
|
|
|
|
|
|
new_config = None
|
|
|
|
|
|
|
|
if diffusers_path.exists():
|
2023-04-29 14:48:50 +00:00
|
|
|
self.logger.error(
|
2023-04-11 13:33:28 +00:00
|
|
|
f"The path {str(diffusers_path)} already exists. Please move or remove it and try again."
|
2023-02-28 05:31:15 +00:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
model_name = model_name or diffusers_path.name
|
improve importation and conversion of legacy checkpoint files
A long-standing issue with importing legacy checkpoints (both ckpt and
safetensors) is that the user has to identify the correct config file,
either by providing its path or by selecting which type of model the
checkpoint is (e.g. "v1 inpainting"). In addition, some users wish to
provide custom VAEs for use with the model. Currently this is done in
the WebUI by importing the model, editing it, and then typing in the
path to the VAE.
To improve the user experience, the model manager's
`heuristic_import()` method has been enhanced as follows:
1. When initially called, the caller can pass a config file path, in
which case it will be used.
2. If no config file provided, the method looks for a .yaml file in the
same directory as the model which bears the same basename. e.g.
```
my-new-model.safetensors
my-new-model.yaml
```
The yaml file is then used as the configuration file for
importation and conversion.
3. If no such file is found, then the method opens up the checkpoint
and probes it to determine whether it is V1, V1-inpaint or V2.
If it is a V1 format, then the appropriate v1-inference.yaml config
file is used. Unfortunately there are two V2 variants that cannot be
distinguished by introspection.
4. If the probe algorithm is unable to determine the model type, then its
last-ditch effort is to execute an optional callback function that can
be provided by the caller. This callback, named `config_file_callback`
receives the path to the legacy checkpoint and returns the path to the
config file to use. The CLI uses to put up a multiple choice prompt to
the user. The WebUI **could** use this to prompt the user to choose
from a radio-button selection.
5. If the config file cannot be determined, then the import is abandoned.
The user can attach a custom VAE to the imported and converted model
by copying the desired VAE into the same directory as the file to be
imported, and giving it the same basename. E.g.:
```
my-new-model.safetensors
my-new-model.vae.pt
```
For this to work, the VAE must end with ".vae.pt", ".vae.ckpt", or
".vae.safetensors". The indicated VAE will be converted into diffusers
format and stored with the converted models file, so the ".pt" file
can be deleted after conversion.
No facility is currently provided to swap a diffusers VAE at import
time, but this can be done after the fact using the WebUI and CLI's
model editing functions.
2023-03-27 15:27:45 +00:00
|
|
|
model_description = model_description or f"Converted version of {model_name}"
|
2023-04-29 14:48:50 +00:00
|
|
|
self.logger.debug(f"Converting {model_name} to diffusers (30-60s)")
|
2023-05-05 23:32:28 +00:00
|
|
|
|
|
|
|
# to avoid circular import errors
|
|
|
|
from .convert_ckpt_to_diffusers import convert_ckpt_to_diffusers
|
|
|
|
|
2023-02-28 05:31:15 +00:00
|
|
|
try:
|
|
|
|
# By passing the specified VAE to the conversion function, the autoencoder
|
|
|
|
# will be built into the model rather than tacked on afterward via the config file
|
2023-04-05 21:25:42 +00:00
|
|
|
vae_model = None
|
2023-03-23 17:14:19 +00:00
|
|
|
if vae:
|
2023-05-18 18:31:47 +00:00
|
|
|
vae_location = self.globals.root_dir / vae.get('path') \
|
|
|
|
if vae.get('path') \
|
|
|
|
else vae.get('repo_id')
|
2023-05-14 00:06:26 +00:00
|
|
|
vae_model = self.cache.get_model(vae_location, SDModelType.Vae).model
|
2023-04-05 21:25:42 +00:00
|
|
|
vae_path = None
|
2023-03-03 06:02:00 +00:00
|
|
|
convert_ckpt_to_diffusers(
|
2023-02-28 05:31:15 +00:00
|
|
|
ckpt_path,
|
|
|
|
diffusers_path,
|
|
|
|
extract_ema=True,
|
|
|
|
original_config_file=original_config_file,
|
|
|
|
vae=vae_model,
|
2023-03-23 17:14:19 +00:00
|
|
|
vae_path=vae_path,
|
2023-03-23 19:03:30 +00:00
|
|
|
scan_needed=scan_needed,
|
2023-02-28 05:31:15 +00:00
|
|
|
)
|
2023-04-29 14:48:50 +00:00
|
|
|
self.logger.debug(
|
2023-04-11 13:33:28 +00:00
|
|
|
f"Success. Converted model is now located at {str(diffusers_path)}"
|
2023-02-28 05:31:15 +00:00
|
|
|
)
|
2023-04-29 14:48:50 +00:00
|
|
|
self.logger.debug(f"Writing new config file entry for {model_name}")
|
2023-02-28 05:31:15 +00:00
|
|
|
new_config = dict(
|
|
|
|
path=str(diffusers_path),
|
|
|
|
description=model_description,
|
|
|
|
format="diffusers",
|
|
|
|
)
|
2023-05-14 00:06:26 +00:00
|
|
|
if self.model_exists(model_name, SDModelType.Diffusers):
|
|
|
|
self.del_model(model_name, SDModelType.Diffusers)
|
2023-05-13 18:44:44 +00:00
|
|
|
self.add_model(
|
|
|
|
model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
SDModelType.Diffusers,
|
2023-05-13 18:44:44 +00:00
|
|
|
new_config,
|
|
|
|
True
|
|
|
|
)
|
2023-02-28 05:31:15 +00:00
|
|
|
if commit_to_conf:
|
|
|
|
self.commit(commit_to_conf)
|
2023-06-04 18:55:51 +00:00
|
|
|
self.logger.debug(f"Model {model_name} installed")
|
2023-02-28 05:31:15 +00:00
|
|
|
except Exception as e:
|
2023-04-29 14:48:50 +00:00
|
|
|
self.logger.warning(f"Conversion failed: {str(e)}")
|
2023-05-22 21:51:47 +00:00
|
|
|
self.logger.warning(traceback.format_exc())
|
2023-04-29 14:48:50 +00:00
|
|
|
self.logger.warning(
|
2023-04-11 13:33:28 +00:00
|
|
|
"If you are trying to convert an inpainting or 2.X model, please indicate the correct config file (e.g. v1-inpainting-inference.yaml)"
|
2023-02-28 05:31:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return model_name
|
|
|
|
|
|
|
|
def search_models(self, search_folder):
|
2023-04-29 14:48:50 +00:00
|
|
|
self.logger.info(f"Finding Models In: {search_folder}")
|
2023-02-28 05:31:15 +00:00
|
|
|
models_folder_ckpt = Path(search_folder).glob("**/*.ckpt")
|
|
|
|
models_folder_safetensors = Path(search_folder).glob("**/*.safetensors")
|
|
|
|
|
|
|
|
ckpt_files = [x for x in models_folder_ckpt if x.is_file()]
|
|
|
|
safetensor_files = [x for x in models_folder_safetensors if x.is_file()]
|
|
|
|
|
|
|
|
files = ckpt_files + safetensor_files
|
|
|
|
|
|
|
|
found_models = []
|
|
|
|
for file in files:
|
|
|
|
location = str(file.resolve()).replace("\\", "/")
|
|
|
|
if (
|
|
|
|
"model.safetensors" not in location
|
|
|
|
and "diffusion_pytorch_model.safetensors" not in location
|
|
|
|
):
|
|
|
|
found_models.append({"name": file.stem, "location": location})
|
|
|
|
|
|
|
|
return search_folder, found_models
|
|
|
|
|
2023-05-07 02:41:19 +00:00
|
|
|
def commit(self, conf_file: Path=None) -> None:
|
2023-02-28 05:31:15 +00:00
|
|
|
"""
|
|
|
|
Write current configuration out to the indicated file.
|
|
|
|
"""
|
2023-06-11 01:49:09 +00:00
|
|
|
data_to_save = dict()
|
|
|
|
for model_key, model_config in self.models.items():
|
|
|
|
model_name, base_model, model_type = self.parse_key(model_key)
|
|
|
|
model_class = MODEL_CLASSES[base_model][model_type]
|
|
|
|
if model_class.save_to_config:
|
|
|
|
# TODO: or exclude_unset better fits here?
|
|
|
|
data_to_save[model_key] = model_config.dict(exclude_defaults=True)
|
|
|
|
|
|
|
|
yaml_str = OmegaConf.to_yaml(data_to_save)
|
2023-05-07 02:41:19 +00:00
|
|
|
config_file_path = conf_file or self.config_path
|
2023-05-08 03:18:17 +00:00
|
|
|
assert config_file_path is not None,'no config file path to write to'
|
2023-05-18 18:31:47 +00:00
|
|
|
config_file_path = self.globals.root_dir / config_file_path
|
2023-02-28 05:31:15 +00:00
|
|
|
tmpfile = os.path.join(os.path.dirname(config_file_path), "new_config.tmp")
|
|
|
|
with open(tmpfile, "w", encoding="utf-8") as outfile:
|
|
|
|
outfile.write(self.preamble())
|
|
|
|
outfile.write(yaml_str)
|
|
|
|
os.replace(tmpfile, config_file_path)
|
|
|
|
|
|
|
|
def preamble(self) -> str:
|
|
|
|
"""
|
|
|
|
Returns the preamble for the config file.
|
|
|
|
"""
|
|
|
|
return textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
# This file describes the alternative machine learning models
|
|
|
|
# available to InvokeAI script.
|
|
|
|
#
|
|
|
|
# To add a new model, follow the examples below. Each
|
|
|
|
# model requires a model config file, a weights file,
|
|
|
|
# and the width and height of the images it
|
|
|
|
# was trained on.
|
|
|
|
"""
|
2023-05-08 03:18:17 +00:00
|
|
|
)
|
|
|
|
|
2023-04-29 14:48:50 +00:00
|
|
|
@classmethod
|
|
|
|
def _delete_model_from_cache(cls,repo_id):
|
2023-05-26 00:41:26 +00:00
|
|
|
cache_info = scan_cache_dir(InvokeAIAppConfig.get_config().cache_dir)
|
2023-02-28 05:31:15 +00:00
|
|
|
|
|
|
|
# I'm sure there is a way to do this with comprehensions
|
|
|
|
# but the code quickly became incomprehensible!
|
|
|
|
hashes_to_delete = set()
|
|
|
|
for repo in cache_info.repos:
|
|
|
|
if repo.repo_id == repo_id:
|
|
|
|
for revision in repo.revisions:
|
|
|
|
hashes_to_delete.add(revision.commit_hash)
|
|
|
|
strategy = cache_info.delete_revisions(*hashes_to_delete)
|
2023-04-29 14:48:50 +00:00
|
|
|
cls.logger.warning(
|
2023-04-11 13:33:28 +00:00
|
|
|
f"Deletion of this model is expected to free {strategy.expected_freed_size_str}"
|
2023-02-28 05:31:15 +00:00
|
|
|
)
|
|
|
|
strategy.execute()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _abs_path(path: str | Path) -> Path:
|
2023-05-26 00:41:26 +00:00
|
|
|
globals = InvokeAIAppConfig.get_config()
|
2023-02-28 05:31:15 +00:00
|
|
|
if path is None or Path(path).is_absolute():
|
|
|
|
return path
|
2023-05-18 14:48:23 +00:00
|
|
|
return Path(globals.root_dir, path).resolve()
|
2023-02-28 05:31:15 +00:00
|
|
|
|
2023-05-09 03:39:44 +00:00
|
|
|
# This is not the same as global_resolve_path(), which prepends
|
|
|
|
# Globals.root.
|
|
|
|
def _resolve_path(
|
|
|
|
self, source: Union[str, Path], dest_directory: str
|
|
|
|
) -> Optional[Path]:
|
|
|
|
resolved_path = None
|
|
|
|
if str(source).startswith(("http:", "https:", "ftp:")):
|
2023-05-18 18:31:47 +00:00
|
|
|
dest_directory = self.globals.root_dir / dest_directory
|
2023-05-09 03:39:44 +00:00
|
|
|
dest_directory.mkdir(parents=True, exist_ok=True)
|
|
|
|
resolved_path = download_with_resume(str(source), dest_directory)
|
|
|
|
else:
|
2023-05-18 18:31:47 +00:00
|
|
|
resolved_path = self.globals.root_dir / source
|
2023-05-09 03:39:44 +00:00
|
|
|
return resolved_path
|
2023-05-13 18:44:44 +00:00
|
|
|
|
|
|
|
def _update_config_file_version(self):
|
2023-06-11 01:49:09 +00:00
|
|
|
# TODO:
|
|
|
|
raise Exception("TODO: ")
|
2023-05-13 18:44:44 +00:00
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
def scan_models_directory(self):
|
2023-05-13 18:44:44 +00:00
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
for model_key in list(self.models.keys()):
|
|
|
|
model_name, base_model, model_type = self.parse_key(model_key)
|
|
|
|
if not os.path.exists(model_config.path):
|
|
|
|
if model_class.save_to_config:
|
|
|
|
self.models[model_key].error = ModelError.NotFound
|
2023-05-13 18:44:44 +00:00
|
|
|
else:
|
2023-06-11 01:49:09 +00:00
|
|
|
self.models.pop(model_key, None)
|
2023-05-13 18:44:44 +00:00
|
|
|
|
2023-06-09 03:11:53 +00:00
|
|
|
|
2023-06-11 01:49:09 +00:00
|
|
|
for base_model in BaseModelType:
|
|
|
|
for model_type in ModelType:
|
|
|
|
|
|
|
|
model_class = MODEL_CLASSES[base_model][model_type]
|
|
|
|
models_dir = os.path.join(self.globals.models_path, base_model, model_type)
|
|
|
|
|
|
|
|
for entry_name in os.listdir(models_dir):
|
|
|
|
model_path = os.path.join(models_dir, entry_name)
|
|
|
|
model_name = Path(model_path).stem
|
|
|
|
model_config: ModelConfigBase = model_class.build_config(
|
|
|
|
path=model_path,
|
|
|
|
)
|
|
|
|
|
|
|
|
model_key = self.create_key(model_name, base_model, model_type)
|
|
|
|
if model_key not in self.models:
|
|
|
|
self.models[model_key] = model_config
|
|
|
|
|
2023-06-09 03:11:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
##### NONE OF THE METHODS BELOW WORK NOW BECAUSE OF MODEL DIRECTORY REORGANIZATION
|
2023-06-11 01:49:09 +00:00
|
|
|
##### AND NEED TO BE REWRITTEN
|
2023-06-02 21:20:50 +00:00
|
|
|
def install_lora_models(self, model_names: list[str], access_token:str=None):
|
2023-06-02 04:40:15 +00:00
|
|
|
'''Download list of LoRA/LyCORIS models'''
|
2023-06-02 21:20:50 +00:00
|
|
|
|
2023-06-02 04:40:15 +00:00
|
|
|
short_names = OmegaConf.load(Dataset_path).get('lora') or {}
|
|
|
|
for name in model_names:
|
2023-06-02 21:20:50 +00:00
|
|
|
name = short_names.get(name) or name
|
|
|
|
|
|
|
|
# HuggingFace style LoRA
|
|
|
|
if re.match(r"^[\w.+-]+/([\w.+-]+)$", name):
|
|
|
|
self.logger.info(f'Downloading LoRA/LyCORIS model {name}')
|
|
|
|
_,dest_dir = name.split("/")
|
|
|
|
|
|
|
|
hf_download_with_resume(
|
|
|
|
repo_id = name,
|
|
|
|
model_dir = self.globals.lora_path / dest_dir,
|
|
|
|
model_name = 'pytorch_lora_weights.bin',
|
|
|
|
access_token = access_token,
|
|
|
|
)
|
|
|
|
|
|
|
|
elif name.startswith(("http:", "https:", "ftp:")):
|
|
|
|
download_with_resume(name, self.globals.lora_path)
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.logger.error(f"Unknown repo_id or URL: {name}")
|
2023-06-02 04:40:15 +00:00
|
|
|
|
2023-06-02 21:20:50 +00:00
|
|
|
def install_ti_models(self, model_names: list[str], access_token: str=None):
|
2023-06-02 04:40:15 +00:00
|
|
|
'''Download list of textual inversion embeddings'''
|
2023-06-02 21:20:50 +00:00
|
|
|
|
2023-06-02 04:40:15 +00:00
|
|
|
short_names = OmegaConf.load(Dataset_path).get('textual_inversion') or {}
|
|
|
|
for name in model_names:
|
2023-06-02 21:20:50 +00:00
|
|
|
name = short_names.get(name) or name
|
|
|
|
|
|
|
|
if re.match(r"^[\w.+-]+/([\w.+-]+)$", name):
|
|
|
|
self.logger.info(f'Downloading Textual Inversion embedding {name}')
|
|
|
|
_,dest_dir = name.split("/")
|
|
|
|
hf_download_with_resume(
|
|
|
|
repo_id = name,
|
|
|
|
model_dir = self.globals.embedding_path / dest_dir,
|
|
|
|
model_name = 'learned_embeds.bin',
|
|
|
|
access_token = access_token
|
|
|
|
)
|
|
|
|
elif name.startswith(('http:','https:','ftp:')):
|
|
|
|
download_with_resume(name, self.globals.embedding_path)
|
|
|
|
else:
|
|
|
|
self.logger.error(f'{name} does not look like either a HuggingFace repo_id or a downloadable URL')
|
2023-06-01 04:31:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def install_controlnet_models(self, model_names: list[str], access_token: str=None):
|
|
|
|
'''Download list of controlnet models; provide either repo_id or short name listed in INITIAL_MODELS.yaml'''
|
|
|
|
short_names = OmegaConf.load(Dataset_path).get('controlnet') or {}
|
|
|
|
dest_dir = self.globals.controlnet_path
|
|
|
|
dest_dir.mkdir(parents=True,exist_ok=True)
|
|
|
|
|
|
|
|
# The model file may be fp32 or fp16, and may be either a
|
|
|
|
# .bin file or a .safetensors. We try each until we get one,
|
|
|
|
# preferring 'fp16' if using half precision, and preferring
|
|
|
|
# safetensors over over bin.
|
|
|
|
precisions = ['.fp16',''] if self.precision=='float16' else ['']
|
|
|
|
formats = ['.safetensors','.bin']
|
|
|
|
possible_filenames = list()
|
|
|
|
for p in precisions:
|
|
|
|
for f in formats:
|
|
|
|
possible_filenames.append(Path(f'diffusion_pytorch_model{p}{f}'))
|
|
|
|
|
|
|
|
for directory_name in model_names:
|
|
|
|
repo_id = short_names.get(directory_name) or directory_name
|
|
|
|
safe_name = directory_name.replace('/','--')
|
|
|
|
self.logger.info(f'Downloading ControlNet model {directory_name} ({repo_id})')
|
|
|
|
hf_download_with_resume(
|
|
|
|
repo_id = repo_id,
|
|
|
|
model_dir = dest_dir / safe_name,
|
|
|
|
model_name = 'config.json',
|
|
|
|
access_token = access_token
|
|
|
|
)
|
|
|
|
|
|
|
|
path = None
|
|
|
|
for filename in possible_filenames:
|
|
|
|
suffix = filename.suffix
|
|
|
|
dest_filename = Path(f'diffusion_pytorch_model{suffix}')
|
|
|
|
self.logger.info(f'Checking availability of {directory_name}/{filename}...')
|
|
|
|
path = hf_download_with_resume(
|
|
|
|
repo_id = repo_id,
|
|
|
|
model_dir = dest_dir / safe_name,
|
|
|
|
model_name = str(filename),
|
|
|
|
access_token = access_token,
|
|
|
|
model_dest = Path(dest_dir, safe_name, dest_filename),
|
|
|
|
)
|
|
|
|
if path:
|
|
|
|
(path.parent / '.download_complete').touch()
|
|
|
|
break
|
|
|
|
|
|
|
|
|