2024-02-01 04:37:59 +00:00
|
|
|
# Copyright (c) 2024, Lincoln D. Stein and the InvokeAI Development Team
|
|
|
|
"""
|
|
|
|
Base class for model loading in InvokeAI.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from logging import Logger
|
|
|
|
from pathlib import Path
|
2024-02-18 06:27:42 +00:00
|
|
|
from typing import Any, Optional
|
2024-02-01 04:37:59 +00:00
|
|
|
|
|
|
|
from invokeai.app.services.config import InvokeAIAppConfig
|
2024-02-10 23:09:45 +00:00
|
|
|
from invokeai.backend.model_manager.config import (
|
|
|
|
AnyModel,
|
|
|
|
AnyModelConfig,
|
|
|
|
SubModelType,
|
|
|
|
)
|
2024-02-04 03:55:09 +00:00
|
|
|
from invokeai.backend.model_manager.load.convert_cache.convert_cache_base import ModelConvertCacheBase
|
2024-02-04 22:23:10 +00:00
|
|
|
from invokeai.backend.model_manager.load.model_cache.model_cache_base import ModelCacheBase, ModelLockerBase
|
|
|
|
|
2024-02-01 04:37:59 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class LoadedModel:
|
|
|
|
"""Context manager object that mediates transfer from RAM<->VRAM."""
|
|
|
|
|
|
|
|
config: AnyModelConfig
|
2024-02-16 03:41:29 +00:00
|
|
|
_locker: ModelLockerBase
|
2024-02-01 04:37:59 +00:00
|
|
|
|
2024-02-10 23:09:45 +00:00
|
|
|
def __enter__(self) -> AnyModel:
|
2024-02-01 04:37:59 +00:00
|
|
|
"""Context entry."""
|
2024-02-16 03:41:29 +00:00
|
|
|
self._locker.lock()
|
2024-02-01 04:37:59 +00:00
|
|
|
return self.model
|
|
|
|
|
|
|
|
def __exit__(self, *args: Any, **kwargs: Any) -> None:
|
|
|
|
"""Context exit."""
|
2024-02-16 03:41:29 +00:00
|
|
|
self._locker.unlock()
|
2024-02-01 04:37:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def model(self) -> AnyModel:
|
|
|
|
"""Return the model without locking it."""
|
2024-02-16 03:41:29 +00:00
|
|
|
return self._locker.model
|
2024-02-01 04:37:59 +00:00
|
|
|
|
|
|
|
|
2024-02-18 06:27:42 +00:00
|
|
|
# TODO(MM2):
|
|
|
|
# Some "intermediary" subclasses in the ModelLoaderBase class hierarchy define methods that their subclasses don't
|
|
|
|
# know about. I think the problem may be related to this class being an ABC.
|
|
|
|
#
|
|
|
|
# For example, GenericDiffusersLoader defines `get_hf_load_class()`, and StableDiffusionDiffusersModel attempts to
|
|
|
|
# call it. However, the method is not defined in the ABC, so it is not guaranteed to be implemented.
|
|
|
|
|
|
|
|
|
2024-02-01 04:37:59 +00:00
|
|
|
class ModelLoaderBase(ABC):
|
|
|
|
"""Abstract base class for loading models into RAM/VRAM."""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
app_config: InvokeAIAppConfig,
|
|
|
|
logger: Logger,
|
2024-02-04 22:23:10 +00:00
|
|
|
ram_cache: ModelCacheBase[AnyModel],
|
2024-02-01 04:37:59 +00:00
|
|
|
convert_cache: ModelConvertCacheBase,
|
|
|
|
):
|
|
|
|
"""Initialize the loader."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2024-02-18 06:27:42 +00:00
|
|
|
def load_model(self, model_config: AnyModelConfig, submodel_type: Optional[SubModelType] = None) -> LoadedModel:
|
2024-02-01 04:37:59 +00:00
|
|
|
"""
|
2024-02-04 03:55:09 +00:00
|
|
|
Return a model given its confguration.
|
2024-02-01 04:37:59 +00:00
|
|
|
|
2024-02-04 03:55:09 +00:00
|
|
|
Given a model identified in the model configuration backend,
|
2024-02-01 04:37:59 +00:00
|
|
|
return a ModelInfo object that can be used to retrieve the model.
|
|
|
|
|
|
|
|
:param model_config: Model configuration, as returned by ModelConfigRecordStore
|
|
|
|
:param submodel_type: an ModelType enum indicating the portion of
|
|
|
|
the model to retrieve (e.g. ModelType.Vae)
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_size_fs(
|
|
|
|
self, config: AnyModelConfig, model_path: Path, submodel_type: Optional[SubModelType] = None
|
|
|
|
) -> int:
|
|
|
|
"""Return size in bytes of the model, calculated before loading."""
|
|
|
|
pass
|