2023-11-05 03:03:26 +00:00
|
|
|
# Copyright (c) 2023 Lincoln D. Stein and the InvokeAI Development Team
|
|
|
|
"""
|
|
|
|
Configuration definitions for image generation models.
|
|
|
|
|
|
|
|
Typical usage:
|
|
|
|
|
|
|
|
from invokeai.backend.model_manager import ModelConfigFactory
|
|
|
|
raw = dict(path='models/sd-1/main/foo.ckpt',
|
|
|
|
name='foo',
|
2023-11-06 23:08:57 +00:00
|
|
|
base='sd-1',
|
|
|
|
type='main',
|
2023-11-05 03:03:26 +00:00
|
|
|
config='configs/stable-diffusion/v1-inference.yaml',
|
|
|
|
variant='normal',
|
|
|
|
format='checkpoint'
|
|
|
|
)
|
|
|
|
config = ModelConfigFactory.make_config(raw)
|
|
|
|
print(config.name)
|
|
|
|
|
|
|
|
Validation errors will raise an InvalidModelConfigException error.
|
|
|
|
|
|
|
|
"""
|
2024-02-04 03:55:09 +00:00
|
|
|
import time
|
|
|
|
import torch
|
2023-11-05 03:03:26 +00:00
|
|
|
from enum import Enum
|
|
|
|
from typing import Literal, Optional, Type, Union
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
from pydantic import BaseModel, ConfigDict, Field, TypeAdapter
|
2024-02-04 03:55:09 +00:00
|
|
|
from diffusers import ModelMixin
|
2023-11-24 04:15:32 +00:00
|
|
|
from typing_extensions import Annotated, Any, Dict
|
2024-02-04 03:55:09 +00:00
|
|
|
from .onnx_runtime import IAIOnnxRuntimeModel
|
2023-11-05 03:03:26 +00:00
|
|
|
|
|
|
|
class InvalidModelConfigException(Exception):
|
|
|
|
"""Exception for when config parser doesn't recognized this combination of model type and format."""
|
|
|
|
|
|
|
|
|
|
|
|
class BaseModelType(str, Enum):
|
|
|
|
"""Base model type."""
|
|
|
|
|
|
|
|
Any = "any"
|
|
|
|
StableDiffusion1 = "sd-1"
|
|
|
|
StableDiffusion2 = "sd-2"
|
|
|
|
StableDiffusionXL = "sdxl"
|
|
|
|
StableDiffusionXLRefiner = "sdxl-refiner"
|
|
|
|
# Kandinsky2_1 = "kandinsky-2.1"
|
|
|
|
|
|
|
|
|
|
|
|
class ModelType(str, Enum):
|
|
|
|
"""Model type."""
|
|
|
|
|
|
|
|
ONNX = "onnx"
|
|
|
|
Main = "main"
|
|
|
|
Vae = "vae"
|
|
|
|
Lora = "lora"
|
|
|
|
ControlNet = "controlnet" # used by model_probe
|
|
|
|
TextualInversion = "embedding"
|
|
|
|
IPAdapter = "ip_adapter"
|
|
|
|
CLIPVision = "clip_vision"
|
|
|
|
T2IAdapter = "t2i_adapter"
|
|
|
|
|
|
|
|
|
|
|
|
class SubModelType(str, Enum):
|
|
|
|
"""Submodel type."""
|
|
|
|
|
|
|
|
UNet = "unet"
|
|
|
|
TextEncoder = "text_encoder"
|
|
|
|
TextEncoder2 = "text_encoder_2"
|
|
|
|
Tokenizer = "tokenizer"
|
|
|
|
Tokenizer2 = "tokenizer_2"
|
|
|
|
Vae = "vae"
|
|
|
|
VaeDecoder = "vae_decoder"
|
|
|
|
VaeEncoder = "vae_encoder"
|
|
|
|
Scheduler = "scheduler"
|
|
|
|
SafetyChecker = "safety_checker"
|
|
|
|
|
|
|
|
|
|
|
|
class ModelVariantType(str, Enum):
|
|
|
|
"""Variant type."""
|
|
|
|
|
|
|
|
Normal = "normal"
|
|
|
|
Inpaint = "inpaint"
|
|
|
|
Depth = "depth"
|
|
|
|
|
|
|
|
|
|
|
|
class ModelFormat(str, Enum):
|
|
|
|
"""Storage format of model."""
|
|
|
|
|
|
|
|
Diffusers = "diffusers"
|
|
|
|
Checkpoint = "checkpoint"
|
|
|
|
Lycoris = "lycoris"
|
|
|
|
Onnx = "onnx"
|
|
|
|
Olive = "olive"
|
|
|
|
EmbeddingFile = "embedding_file"
|
|
|
|
EmbeddingFolder = "embedding_folder"
|
|
|
|
InvokeAI = "invokeai"
|
|
|
|
|
|
|
|
|
|
|
|
class SchedulerPredictionType(str, Enum):
|
|
|
|
"""Scheduler prediction type."""
|
|
|
|
|
|
|
|
Epsilon = "epsilon"
|
|
|
|
VPrediction = "v_prediction"
|
|
|
|
Sample = "sample"
|
|
|
|
|
|
|
|
|
2024-01-14 19:54:53 +00:00
|
|
|
class ModelRepoVariant(str, Enum):
|
|
|
|
"""Various hugging face variants on the diffusers format."""
|
|
|
|
|
|
|
|
DEFAULT = "default" # model files without "fp16" or other qualifier
|
|
|
|
FP16 = "fp16"
|
|
|
|
FP32 = "fp32"
|
|
|
|
ONNX = "onnx"
|
|
|
|
OPENVINO = "openvino"
|
|
|
|
FLAX = "flax"
|
|
|
|
|
|
|
|
|
2023-11-05 03:03:26 +00:00
|
|
|
class ModelConfigBase(BaseModel):
|
|
|
|
"""Base class for model configuration information."""
|
|
|
|
|
|
|
|
path: str
|
|
|
|
name: str
|
2023-11-06 23:08:57 +00:00
|
|
|
base: BaseModelType
|
2023-11-05 03:03:26 +00:00
|
|
|
type: ModelType
|
|
|
|
format: ModelFormat
|
|
|
|
key: str = Field(description="unique key for model", default="<NOKEY>")
|
|
|
|
original_hash: Optional[str] = Field(
|
|
|
|
description="original fasthash of model contents", default=None
|
|
|
|
) # this is assigned at install time and will not change
|
|
|
|
current_hash: Optional[str] = Field(
|
|
|
|
description="current fasthash of model contents", default=None
|
|
|
|
) # if model is converted or otherwise modified, this will hold updated hash
|
2023-11-13 00:03:39 +00:00
|
|
|
description: Optional[str] = Field(default=None)
|
2023-11-13 23:15:17 +00:00
|
|
|
source: Optional[str] = Field(description="Model download source (URL or repo_id)", default=None)
|
2024-02-04 03:55:09 +00:00
|
|
|
last_modified: Optional[float] = Field(description="Timestamp for modification time", default_factory=time.time)
|
2023-11-05 03:03:26 +00:00
|
|
|
|
|
|
|
model_config = ConfigDict(
|
|
|
|
use_enum_values=False,
|
|
|
|
validate_assignment=True,
|
|
|
|
)
|
|
|
|
|
2023-11-24 04:15:32 +00:00
|
|
|
def update(self, attributes: Dict[str, Any]) -> None:
|
2023-11-05 03:03:26 +00:00
|
|
|
"""Update the object with fields in dict."""
|
|
|
|
for key, value in attributes.items():
|
|
|
|
setattr(self, key, value) # may raise a validation error
|
|
|
|
|
|
|
|
|
2023-11-10 23:25:37 +00:00
|
|
|
class _CheckpointConfig(ModelConfigBase):
|
2023-11-05 03:03:26 +00:00
|
|
|
"""Model config for checkpoint-style models."""
|
|
|
|
|
|
|
|
format: Literal[ModelFormat.Checkpoint] = ModelFormat.Checkpoint
|
|
|
|
config: str = Field(description="path to the checkpoint model config file")
|
|
|
|
|
|
|
|
|
2023-11-10 23:25:37 +00:00
|
|
|
class _DiffusersConfig(ModelConfigBase):
|
2023-11-05 03:03:26 +00:00
|
|
|
"""Model config for diffusers-style models."""
|
|
|
|
|
|
|
|
format: Literal[ModelFormat.Diffusers] = ModelFormat.Diffusers
|
2024-01-22 19:37:23 +00:00
|
|
|
repo_variant: Optional[ModelRepoVariant] = ModelRepoVariant.DEFAULT
|
2023-11-05 03:03:26 +00:00
|
|
|
|
2024-02-01 04:37:59 +00:00
|
|
|
|
2023-11-05 03:03:26 +00:00
|
|
|
class LoRAConfig(ModelConfigBase):
|
|
|
|
"""Model config for LoRA/Lycoris models."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.Lora] = ModelType.Lora
|
2023-11-05 03:03:26 +00:00
|
|
|
format: Literal[ModelFormat.Lycoris, ModelFormat.Diffusers]
|
|
|
|
|
|
|
|
|
|
|
|
class VaeCheckpointConfig(ModelConfigBase):
|
|
|
|
"""Model config for standalone VAE models."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.Vae] = ModelType.Vae
|
2023-11-05 03:03:26 +00:00
|
|
|
format: Literal[ModelFormat.Checkpoint] = ModelFormat.Checkpoint
|
|
|
|
|
|
|
|
|
|
|
|
class VaeDiffusersConfig(ModelConfigBase):
|
|
|
|
"""Model config for standalone VAE models (diffusers version)."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.Vae] = ModelType.Vae
|
2023-11-05 03:03:26 +00:00
|
|
|
format: Literal[ModelFormat.Diffusers] = ModelFormat.Diffusers
|
|
|
|
|
|
|
|
|
2023-11-10 23:25:37 +00:00
|
|
|
class ControlNetDiffusersConfig(_DiffusersConfig):
|
2023-11-05 03:03:26 +00:00
|
|
|
"""Model config for ControlNet models (diffusers version)."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.ControlNet] = ModelType.ControlNet
|
2023-11-05 03:03:26 +00:00
|
|
|
format: Literal[ModelFormat.Diffusers] = ModelFormat.Diffusers
|
|
|
|
|
2024-02-01 04:37:59 +00:00
|
|
|
|
2023-11-10 23:25:37 +00:00
|
|
|
class ControlNetCheckpointConfig(_CheckpointConfig):
|
2023-11-05 03:03:26 +00:00
|
|
|
"""Model config for ControlNet models (diffusers version)."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.ControlNet] = ModelType.ControlNet
|
2023-11-05 03:03:26 +00:00
|
|
|
format: Literal[ModelFormat.Checkpoint] = ModelFormat.Checkpoint
|
|
|
|
|
|
|
|
|
|
|
|
class TextualInversionConfig(ModelConfigBase):
|
|
|
|
"""Model config for textual inversion embeddings."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.TextualInversion] = ModelType.TextualInversion
|
2023-11-05 03:03:26 +00:00
|
|
|
format: Literal[ModelFormat.EmbeddingFile, ModelFormat.EmbeddingFolder]
|
|
|
|
|
|
|
|
|
2023-11-10 23:25:37 +00:00
|
|
|
class _MainConfig(ModelConfigBase):
|
2023-11-05 03:03:26 +00:00
|
|
|
"""Model config for main models."""
|
|
|
|
|
2023-11-10 23:25:37 +00:00
|
|
|
vae: Optional[str] = Field(default=None)
|
2023-11-05 03:03:26 +00:00
|
|
|
variant: ModelVariantType = ModelVariantType.Normal
|
2023-11-06 23:08:57 +00:00
|
|
|
ztsnr_training: bool = False
|
2023-11-05 03:03:26 +00:00
|
|
|
|
|
|
|
|
2023-11-10 23:25:37 +00:00
|
|
|
class MainCheckpointConfig(_CheckpointConfig, _MainConfig):
|
2023-11-05 03:03:26 +00:00
|
|
|
"""Model config for main checkpoint models."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.Main] = ModelType.Main
|
2023-11-06 23:08:57 +00:00
|
|
|
|
2023-11-05 03:03:26 +00:00
|
|
|
|
2023-11-10 23:25:37 +00:00
|
|
|
class MainDiffusersConfig(_DiffusersConfig, _MainConfig):
|
2023-11-05 03:03:26 +00:00
|
|
|
"""Model config for main diffusers models."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.Main] = ModelType.Main
|
2023-11-06 23:08:57 +00:00
|
|
|
prediction_type: SchedulerPredictionType = SchedulerPredictionType.Epsilon
|
|
|
|
upcast_attention: bool = False
|
|
|
|
|
2024-02-01 04:37:59 +00:00
|
|
|
|
2023-11-10 23:25:37 +00:00
|
|
|
class ONNXSD1Config(_MainConfig):
|
2023-11-05 03:03:26 +00:00
|
|
|
"""Model config for ONNX format models based on sd-1."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.ONNX] = ModelType.ONNX
|
2023-11-05 03:03:26 +00:00
|
|
|
format: Literal[ModelFormat.Onnx, ModelFormat.Olive]
|
2023-11-11 00:14:15 +00:00
|
|
|
base: Literal[BaseModelType.StableDiffusion1] = BaseModelType.StableDiffusion1
|
2023-11-06 23:08:57 +00:00
|
|
|
prediction_type: SchedulerPredictionType = SchedulerPredictionType.Epsilon
|
|
|
|
upcast_attention: bool = False
|
2023-11-05 03:03:26 +00:00
|
|
|
|
|
|
|
|
2023-11-10 23:25:37 +00:00
|
|
|
class ONNXSD2Config(_MainConfig):
|
2023-11-05 03:03:26 +00:00
|
|
|
"""Model config for ONNX format models based on sd-2."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.ONNX] = ModelType.ONNX
|
2023-11-05 03:03:26 +00:00
|
|
|
format: Literal[ModelFormat.Onnx, ModelFormat.Olive]
|
|
|
|
# No yaml config file for ONNX, so these are part of config
|
2023-11-11 00:14:15 +00:00
|
|
|
base: Literal[BaseModelType.StableDiffusion2] = BaseModelType.StableDiffusion2
|
2023-11-06 23:08:57 +00:00
|
|
|
prediction_type: SchedulerPredictionType = SchedulerPredictionType.VPrediction
|
|
|
|
upcast_attention: bool = True
|
2023-11-05 03:03:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IPAdapterConfig(ModelConfigBase):
|
|
|
|
"""Model config for IP Adaptor format models."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.IPAdapter] = ModelType.IPAdapter
|
2023-11-05 03:03:26 +00:00
|
|
|
format: Literal[ModelFormat.InvokeAI]
|
|
|
|
|
|
|
|
|
|
|
|
class CLIPVisionDiffusersConfig(ModelConfigBase):
|
|
|
|
"""Model config for ClipVision."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.CLIPVision] = ModelType.CLIPVision
|
2023-11-05 03:03:26 +00:00
|
|
|
format: Literal[ModelFormat.Diffusers]
|
|
|
|
|
|
|
|
|
|
|
|
class T2IConfig(ModelConfigBase):
|
|
|
|
"""Model config for T2I."""
|
|
|
|
|
2023-11-11 00:14:15 +00:00
|
|
|
type: Literal[ModelType.T2IAdapter] = ModelType.T2IAdapter
|
2023-11-05 03:03:26 +00:00
|
|
|
format: Literal[ModelFormat.Diffusers]
|
|
|
|
|
|
|
|
|
2023-11-13 23:15:17 +00:00
|
|
|
_ONNXConfig = Annotated[Union[ONNXSD1Config, ONNXSD2Config], Field(discriminator="base")]
|
2023-11-11 00:14:29 +00:00
|
|
|
_ControlNetConfig = Annotated[
|
2023-11-13 23:12:45 +00:00
|
|
|
Union[ControlNetDiffusersConfig, ControlNetCheckpointConfig],
|
|
|
|
Field(discriminator="format"),
|
|
|
|
]
|
2023-11-13 23:15:17 +00:00
|
|
|
_VaeConfig = Annotated[Union[VaeDiffusersConfig, VaeCheckpointConfig], Field(discriminator="format")]
|
|
|
|
_MainModelConfig = Annotated[Union[MainDiffusersConfig, MainCheckpointConfig], Field(discriminator="format")]
|
2023-11-11 00:14:15 +00:00
|
|
|
|
2023-11-11 17:22:38 +00:00
|
|
|
AnyModelConfig = Union[
|
|
|
|
_MainModelConfig,
|
|
|
|
_ONNXConfig,
|
|
|
|
_VaeConfig,
|
|
|
|
_ControlNetConfig,
|
|
|
|
LoRAConfig,
|
|
|
|
TextualInversionConfig,
|
|
|
|
IPAdapterConfig,
|
|
|
|
CLIPVisionDiffusersConfig,
|
|
|
|
T2IConfig,
|
2023-11-05 03:03:26 +00:00
|
|
|
]
|
|
|
|
|
2023-11-12 21:50:05 +00:00
|
|
|
AnyModelConfigValidator = TypeAdapter(AnyModelConfig)
|
2024-02-04 03:55:09 +00:00
|
|
|
AnyModel = Union[ModelMixin, torch.nn.Module, IAIOnnxRuntimeModel]
|
2023-11-12 21:50:05 +00:00
|
|
|
|
|
|
|
# IMPLEMENTATION NOTE:
|
|
|
|
# The preferred alternative to the above is a discriminated Union as shown
|
|
|
|
# below. However, it breaks FastAPI when used as the input Body parameter in a route.
|
|
|
|
# This is a known issue. Please see:
|
|
|
|
# https://github.com/tiangolo/fastapi/discussions/9761 and
|
|
|
|
# https://github.com/tiangolo/fastapi/discussions/9287
|
2023-11-11 17:22:38 +00:00
|
|
|
# AnyModelConfig = Annotated[
|
|
|
|
# Union[
|
|
|
|
# _MainModelConfig,
|
|
|
|
# _ONNXConfig,
|
|
|
|
# _VaeConfig,
|
|
|
|
# _ControlNetConfig,
|
|
|
|
# LoRAConfig,
|
|
|
|
# TextualInversionConfig,
|
|
|
|
# IPAdapterConfig,
|
|
|
|
# CLIPVisionDiffusersConfig,
|
|
|
|
# T2IConfig,
|
|
|
|
# ],
|
|
|
|
# Field(discriminator="type"),
|
|
|
|
# ]
|
|
|
|
|
2023-11-11 00:14:29 +00:00
|
|
|
|
2023-11-05 03:03:26 +00:00
|
|
|
class ModelConfigFactory(object):
|
|
|
|
"""Class for parsing config dicts into StableDiffusion Config obects."""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def make_config(
|
|
|
|
cls,
|
2023-11-13 22:05:01 +00:00
|
|
|
model_data: Union[dict, AnyModelConfig],
|
2023-11-05 03:03:26 +00:00
|
|
|
key: Optional[str] = None,
|
|
|
|
dest_class: Optional[Type] = None,
|
2024-02-04 03:55:09 +00:00
|
|
|
timestamp: Optional[float] = None
|
2023-11-05 03:03:26 +00:00
|
|
|
) -> AnyModelConfig:
|
|
|
|
"""
|
|
|
|
Return the appropriate config object from raw dict values.
|
|
|
|
|
|
|
|
:param model_data: A raw dict corresponding the obect fields to be
|
|
|
|
parsed into a ModelConfigBase obect (or descendent), or a ModelConfigBase
|
|
|
|
object, which will be passed through unchanged.
|
|
|
|
:param dest_class: The config class to be returned. If not provided, will
|
|
|
|
be selected automatically.
|
|
|
|
"""
|
|
|
|
if isinstance(model_data, ModelConfigBase):
|
2023-11-11 17:22:38 +00:00
|
|
|
model = model_data
|
|
|
|
elif dest_class:
|
|
|
|
model = dest_class.validate_python(model_data)
|
2023-11-11 00:14:15 +00:00
|
|
|
else:
|
|
|
|
model = AnyModelConfigValidator.validate_python(model_data)
|
2023-11-11 17:22:38 +00:00
|
|
|
if key:
|
|
|
|
model.key = key
|
2024-02-04 03:55:09 +00:00
|
|
|
if timestamp:
|
|
|
|
model.last_modified = timestamp
|
2023-11-11 17:22:38 +00:00
|
|
|
return model
|