2023-05-11 04:09:19 +00:00
|
|
|
# Copyright (c) 2023 Lincoln D. Stein and the InvokeAI Team
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
from __future__ import annotations
|
2023-05-11 04:09:19 +00:00
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
import torch
|
2023-05-11 04:09:19 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from pathlib import Path
|
2023-06-22 07:34:12 +00:00
|
|
|
from typing import Optional, Union, Callable, List, Tuple, types, TYPE_CHECKING
|
2023-05-12 03:33:24 +00:00
|
|
|
from dataclasses import dataclass
|
2023-05-11 04:09:19 +00:00
|
|
|
|
|
|
|
from invokeai.backend.model_management.model_manager import (
|
|
|
|
ModelManager,
|
2023-06-11 03:12:21 +00:00
|
|
|
BaseModelType,
|
|
|
|
ModelType,
|
|
|
|
SubModelType,
|
2023-06-12 02:52:30 +00:00
|
|
|
ModelInfo,
|
2023-05-11 04:09:19 +00:00
|
|
|
)
|
2023-05-12 04:14:56 +00:00
|
|
|
from invokeai.app.models.exceptions import CanceledException
|
2023-05-18 21:17:45 +00:00
|
|
|
from .config import InvokeAIAppConfig
|
2023-05-12 01:24:29 +00:00
|
|
|
from ...backend.util import choose_precision, choose_torch_device
|
2023-05-11 04:09:19 +00:00
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from ..invocations.baseinvocation import BaseInvocation, InvocationContext
|
|
|
|
|
2023-05-12 03:33:24 +00:00
|
|
|
|
2023-05-11 04:09:19 +00:00
|
|
|
class ModelManagerServiceBase(ABC):
|
|
|
|
"""Responsible for managing models on disk and in memory"""
|
|
|
|
|
2023-05-12 01:24:29 +00:00
|
|
|
@abstractmethod
|
|
|
|
def __init__(
|
2023-05-14 00:06:26 +00:00
|
|
|
self,
|
2023-05-18 21:17:45 +00:00
|
|
|
config: InvokeAIAppConfig,
|
2023-05-14 00:06:26 +00:00
|
|
|
logger: types.ModuleType,
|
2023-05-12 01:24:29 +00:00
|
|
|
):
|
|
|
|
"""
|
|
|
|
Initialize with the path to the models.yaml config file.
|
|
|
|
Optional parameters are the torch device type, precision, max_models,
|
|
|
|
and sequential_offload boolean. Note that the default device
|
|
|
|
type and precision are set up for a CUDA system running at half precision.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2023-05-11 04:09:19 +00:00
|
|
|
@abstractmethod
|
2023-05-14 00:06:26 +00:00
|
|
|
def get_model(
|
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
|
|
|
submodel: Optional[SubModelType] = None,
|
2023-05-14 00:06:26 +00:00
|
|
|
node: Optional[BaseInvocation] = None,
|
|
|
|
context: Optional[InvocationContext] = None,
|
2023-06-12 02:52:30 +00:00
|
|
|
) -> ModelInfo:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""Retrieve the indicated model with name and type.
|
|
|
|
submodel can be used to get a part (such as the vae)
|
|
|
|
of a diffusers pipeline."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def logger(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-05-13 18:44:44 +00:00
|
|
|
def model_exists(
|
2023-05-14 00:06:26 +00:00
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
2023-05-13 18:44:44 +00:00
|
|
|
) -> bool:
|
2023-05-11 04:09:19 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-06-11 03:12:21 +00:00
|
|
|
def model_info(self, model_name: str, base_model: BaseModelType, model_type: ModelType) -> dict:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Given a model name returns a dict-like (OmegaConf) object describing it.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-06-11 03:12:21 +00:00
|
|
|
def model_names(self) -> List[Tuple[str, BaseModelType, ModelType]]:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Returns a list of all the model names known.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-06-11 03:12:21 +00:00
|
|
|
def list_models(self, base_model: Optional[BaseModelType] = None, model_type: Optional[ModelType] = None) -> dict:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Return a dict of models in the format:
|
2023-05-16 03:44:08 +00:00
|
|
|
{ model_type1:
|
|
|
|
{ model_name1: {'status': 'active'|'cached'|'not loaded',
|
|
|
|
'model_name' : name,
|
|
|
|
'model_type' : SDModelType,
|
|
|
|
'description': description,
|
|
|
|
'format': 'folder'|'safetensors'|'ckpt'
|
|
|
|
},
|
|
|
|
model_name2: { etc }
|
|
|
|
},
|
|
|
|
model_type2:
|
|
|
|
{ model_name_n: etc
|
|
|
|
}
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def add_model(
|
2023-05-14 00:06:26 +00:00
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_attributes: dict,
|
|
|
|
clobber: bool = False
|
|
|
|
) -> None:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
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. Will fail
|
|
|
|
with an assertion error if provided attributes are incorrect or
|
|
|
|
the model name is missing. Call commit() to write changes to disk.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-05-14 00:06:26 +00:00
|
|
|
def del_model(
|
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
2023-05-14 00:06:26 +00:00
|
|
|
):
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Delete the named model from configuration. If delete_files is true,
|
|
|
|
then the underlying weight file or diffusers directory will be deleted
|
|
|
|
as well. Call commit() to write to disk.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2023-07-03 23:32:54 +00:00
|
|
|
@abstractmethod
|
|
|
|
def heuristic_import(self,
|
|
|
|
items_to_import: Set[str],
|
|
|
|
prediction_type_helper: Callable[[Path],SchedulerPredictionType]=None,
|
|
|
|
)->Dict[str, AddModelResult]:
|
|
|
|
'''Import a list of paths, repo_ids or URLs. Returns the set of
|
|
|
|
successfully imported items.
|
|
|
|
:param items_to_import: Set of strings corresponding to models to be imported.
|
|
|
|
:param prediction_type_helper: A callback that receives the Path of a Stable Diffusion 2 checkpoint model and returns a SchedulerPredictionType.
|
|
|
|
|
|
|
|
The prediction type helper is necessary to distinguish between
|
|
|
|
models based on Stable Diffusion 2 Base (requiring
|
|
|
|
SchedulerPredictionType.Epsilson) and Stable Diffusion 768
|
|
|
|
(requiring SchedulerPredictionType.VPrediction). It is
|
|
|
|
generally impossible to do this programmatically, so the
|
|
|
|
prediction_type_helper usually asks the user to choose.
|
|
|
|
|
|
|
|
The result is a set of successfully installed models. Each element
|
|
|
|
of the set is a dict corresponding to the newly-created OmegaConf stanza for
|
|
|
|
that model.
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
2023-05-11 04:09:19 +00:00
|
|
|
@abstractmethod
|
2023-05-14 00:06:26 +00:00
|
|
|
def commit(self, conf_file: Path = None) -> None:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Write current configuration out to the indicated file.
|
|
|
|
If no conf_file is provided, then replaces the
|
|
|
|
original file/database used to initialize the object.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
# simple implementation
|
|
|
|
class ModelManagerService(ModelManagerServiceBase):
|
|
|
|
"""Responsible for managing models on disk and in memory"""
|
|
|
|
def __init__(
|
2023-05-14 00:06:26 +00:00
|
|
|
self,
|
2023-05-18 21:17:45 +00:00
|
|
|
config: InvokeAIAppConfig,
|
2023-05-14 00:06:26 +00:00
|
|
|
logger: types.ModuleType,
|
|
|
|
):
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Initialize with the path to the models.yaml config file.
|
|
|
|
Optional parameters are the torch device type, precision, max_models,
|
|
|
|
and sequential_offload boolean. Note that the default device
|
|
|
|
type and precision are set up for a CUDA system running at half precision.
|
|
|
|
"""
|
2023-05-18 21:17:45 +00:00
|
|
|
if config.model_conf_path and config.model_conf_path.exists():
|
|
|
|
config_file = config.model_conf_path
|
2023-05-12 01:24:29 +00:00
|
|
|
else:
|
2023-05-18 21:17:45 +00:00
|
|
|
config_file = config.root_dir / "configs/models.yaml"
|
2023-05-12 01:24:29 +00:00
|
|
|
if not config_file.exists():
|
|
|
|
raise IOError(f"The file {config_file} could not be found.")
|
|
|
|
|
|
|
|
logger.debug(f'config file={config_file}')
|
|
|
|
|
|
|
|
device = torch.device(choose_torch_device())
|
2023-05-23 00:48:22 +00:00
|
|
|
precision = config.precision
|
|
|
|
if precision == "auto":
|
2023-05-12 01:24:29 +00:00
|
|
|
precision = choose_precision(device)
|
2023-05-23 00:48:22 +00:00
|
|
|
dtype = torch.float32 if precision == 'float32' else torch.float16
|
2023-05-12 01:24:29 +00:00
|
|
|
|
|
|
|
# this is transitional backward compatibility
|
|
|
|
# support for the deprecated `max_loaded_models`
|
|
|
|
# configuration value. If present, then the
|
|
|
|
# cache size is set to 2.5 GB times
|
|
|
|
# the number of max_loaded_models. Otherwise
|
|
|
|
# use new `max_cache_size` config setting
|
|
|
|
max_cache_size = config.max_cache_size \
|
|
|
|
if hasattr(config,'max_cache_size') \
|
|
|
|
else config.max_loaded_models * 2.5
|
|
|
|
|
|
|
|
sequential_offload = config.sequential_guidance
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
self.mgr = ModelManager(
|
|
|
|
config=config_file,
|
|
|
|
device_type=device,
|
|
|
|
precision=dtype,
|
|
|
|
max_cache_size=max_cache_size,
|
|
|
|
sequential_offload=sequential_offload,
|
|
|
|
logger=logger,
|
|
|
|
)
|
2023-05-12 01:24:29 +00:00
|
|
|
logger.info('Model manager service initialized')
|
2023-05-11 04:09:19 +00:00
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
def get_model(
|
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
|
|
|
submodel: Optional[SubModelType] = None,
|
2023-05-14 00:06:26 +00:00
|
|
|
node: Optional[BaseInvocation] = None,
|
|
|
|
context: Optional[InvocationContext] = None,
|
2023-06-12 02:52:30 +00:00
|
|
|
) -> ModelInfo:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Retrieve the indicated model. submodel can be used to get a
|
|
|
|
part (such as the vae) of a diffusers mode.
|
|
|
|
"""
|
2023-05-12 04:14:56 +00:00
|
|
|
|
|
|
|
# if we are called from within a node, then we get to emit
|
|
|
|
# load start and complete events
|
|
|
|
if node and context:
|
|
|
|
self._emit_load_event(
|
|
|
|
node=node,
|
|
|
|
context=context,
|
|
|
|
model_name=model_name,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model=base_model,
|
2023-05-12 04:14:56 +00:00
|
|
|
model_type=model_type,
|
2023-06-11 03:12:21 +00:00
|
|
|
submodel=submodel,
|
2023-05-12 04:14:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
model_info = self.mgr.get_model(
|
2023-05-11 04:09:19 +00:00
|
|
|
model_name,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model,
|
2023-05-11 04:09:19 +00:00
|
|
|
model_type,
|
|
|
|
submodel,
|
|
|
|
)
|
|
|
|
|
2023-05-12 04:14:56 +00:00
|
|
|
if node and context:
|
|
|
|
self._emit_load_event(
|
|
|
|
node=node,
|
|
|
|
context=context,
|
|
|
|
model_name=model_name,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model=base_model,
|
2023-05-12 04:14:56 +00:00
|
|
|
model_type=model_type,
|
|
|
|
submodel=submodel,
|
|
|
|
model_info=model_info
|
|
|
|
)
|
|
|
|
|
|
|
|
return model_info
|
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
def model_exists(
|
2023-05-14 00:06:26 +00:00
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
2023-05-13 18:44:44 +00:00
|
|
|
) -> bool:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Given a model name, returns True if it is a valid
|
|
|
|
identifier.
|
|
|
|
"""
|
2023-05-13 18:44:44 +00:00
|
|
|
return self.mgr.model_exists(
|
2023-05-11 04:19:20 +00:00
|
|
|
model_name,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type,
|
|
|
|
)
|
2023-05-11 04:09:19 +00:00
|
|
|
|
2023-06-11 03:12:21 +00:00
|
|
|
def model_info(self, model_name: str, base_model: BaseModelType, model_type: ModelType) -> dict:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Given a model name returns a dict-like (OmegaConf) object describing it.
|
|
|
|
"""
|
2023-06-11 03:12:21 +00:00
|
|
|
return self.mgr.model_info(model_name, base_model, model_type)
|
2023-05-11 04:09:19 +00:00
|
|
|
|
2023-06-11 03:12:21 +00:00
|
|
|
def model_names(self) -> List[Tuple[str, BaseModelType, ModelType]]:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Returns a list of all the model names known.
|
|
|
|
"""
|
|
|
|
return self.mgr.model_names()
|
|
|
|
|
2023-06-11 03:12:21 +00:00
|
|
|
def list_models(
|
|
|
|
self,
|
|
|
|
base_model: Optional[BaseModelType] = None,
|
|
|
|
model_type: Optional[ModelType] = None
|
2023-06-22 07:34:12 +00:00
|
|
|
) -> list[dict]:
|
|
|
|
# ) -> dict:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
2023-06-22 07:34:12 +00:00
|
|
|
Return a list of models.
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
2023-06-11 03:12:21 +00:00
|
|
|
return self.mgr.list_models(base_model, model_type)
|
2023-05-11 04:09:19 +00:00
|
|
|
|
|
|
|
def add_model(
|
2023-05-14 00:06:26 +00:00
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_attributes: dict,
|
|
|
|
clobber: bool = False,
|
|
|
|
)->None:
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
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. Will fail
|
|
|
|
with an assertion error if provided attributes are incorrect or
|
|
|
|
the model name is missing. Call commit() to write changes to disk.
|
|
|
|
"""
|
2023-06-11 03:12:21 +00:00
|
|
|
return self.mgr.add_model(model_name, base_model, model_type, model_attributes, clobber)
|
2023-05-11 04:09:19 +00:00
|
|
|
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
def del_model(
|
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
2023-05-14 00:06:26 +00:00
|
|
|
):
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Delete the named model from configuration. If delete_files is true,
|
|
|
|
then the underlying weight file or diffusers directory will be deleted
|
|
|
|
as well. Call commit() to write to disk.
|
|
|
|
"""
|
2023-06-14 00:12:12 +00:00
|
|
|
self.mgr.del_model(model_name, base_model, model_type)
|
2023-05-11 04:09:19 +00:00
|
|
|
|
|
|
|
|
2023-05-14 00:06:26 +00:00
|
|
|
def commit(self, conf_file: Optional[Path]=None):
|
2023-05-11 04:09:19 +00:00
|
|
|
"""
|
|
|
|
Write current configuration out to the indicated file.
|
|
|
|
If no conf_file is provided, then replaces the
|
|
|
|
original file/database used to initialize the object.
|
|
|
|
"""
|
|
|
|
return self.mgr.commit(conf_file)
|
|
|
|
|
2023-05-12 04:14:56 +00:00
|
|
|
def _emit_load_event(
|
2023-05-14 00:06:26 +00:00
|
|
|
self,
|
|
|
|
node,
|
|
|
|
context,
|
|
|
|
model_name: str,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model: BaseModelType,
|
|
|
|
model_type: ModelType,
|
|
|
|
submodel: SubModelType,
|
2023-06-12 02:52:30 +00:00
|
|
|
model_info: Optional[ModelInfo] = None,
|
2023-05-12 04:14:56 +00:00
|
|
|
):
|
|
|
|
if context.services.queue.is_canceled(context.graph_execution_state_id):
|
2023-05-14 00:06:26 +00:00
|
|
|
raise CanceledException()
|
2023-05-12 04:14:56 +00:00
|
|
|
graph_execution_state = context.services.graph_execution_manager.get(context.graph_execution_state_id)
|
|
|
|
source_node_id = graph_execution_state.prepared_source_mapping[node.id]
|
2023-05-26 03:28:15 +00:00
|
|
|
if model_info:
|
|
|
|
context.services.events.emit_model_load_completed(
|
2023-05-12 04:14:56 +00:00
|
|
|
graph_execution_state_id=context.graph_execution_state_id,
|
|
|
|
node=node.dict(),
|
|
|
|
source_node_id=source_node_id,
|
|
|
|
model_name=model_name,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model=base_model,
|
2023-05-12 04:14:56 +00:00
|
|
|
model_type=model_type,
|
|
|
|
submodel=submodel,
|
2023-05-26 03:28:15 +00:00
|
|
|
model_info=model_info
|
2023-05-12 04:14:56 +00:00
|
|
|
)
|
|
|
|
else:
|
2023-05-26 03:28:15 +00:00
|
|
|
context.services.events.emit_model_load_started(
|
2023-05-12 04:14:56 +00:00
|
|
|
graph_execution_state_id=context.graph_execution_state_id,
|
|
|
|
node=node.dict(),
|
|
|
|
source_node_id=source_node_id,
|
|
|
|
model_name=model_name,
|
2023-06-11 03:12:21 +00:00
|
|
|
base_model=base_model,
|
2023-05-12 04:14:56 +00:00
|
|
|
model_type=model_type,
|
|
|
|
submodel=submodel,
|
|
|
|
)
|
|
|
|
|
2023-05-26 03:28:15 +00:00
|
|
|
|
2023-05-11 04:09:19 +00:00
|
|
|
@property
|
|
|
|
def logger(self):
|
|
|
|
return self.mgr.logger
|
|
|
|
|
2023-07-03 23:32:54 +00:00
|
|
|
def heuristic_import(self,
|
|
|
|
items_to_import: Set[str],
|
|
|
|
prediction_type_helper: Callable[[Path],SchedulerPredictionType]=None,
|
|
|
|
)->Dict[str, AddModelResult]:
|
|
|
|
'''Import a list of paths, repo_ids or URLs. Returns the set of
|
|
|
|
successfully imported items.
|
|
|
|
:param items_to_import: Set of strings corresponding to models to be imported.
|
|
|
|
:param prediction_type_helper: A callback that receives the Path of a Stable Diffusion 2 checkpoint model and returns a SchedulerPredictionType.
|
|
|
|
|
|
|
|
The prediction type helper is necessary to distinguish between
|
|
|
|
models based on Stable Diffusion 2 Base (requiring
|
|
|
|
SchedulerPredictionType.Epsilson) and Stable Diffusion 768
|
|
|
|
(requiring SchedulerPredictionType.VPrediction). It is
|
|
|
|
generally impossible to do this programmatically, so the
|
|
|
|
prediction_type_helper usually asks the user to choose.
|
|
|
|
|
|
|
|
The result is a set of successfully installed models. Each element
|
|
|
|
of the set is a dict corresponding to the newly-created OmegaConf stanza for
|
|
|
|
that model.
|
|
|
|
'''
|
|
|
|
return self.mgr.heuristic_import(items_to_import, prediction_type_helper)
|