2023-05-13 01:37:20 +00:00
|
|
|
from typing import Literal, Optional, Union
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
|
|
from .baseinvocation import BaseInvocation, BaseInvocationOutput, InvocationContext, InvocationConfig
|
|
|
|
|
|
|
|
from ...backend.util.devices import choose_torch_device, torch_dtype
|
|
|
|
from ...backend.model_management import SDModelType
|
|
|
|
|
|
|
|
class ModelInfo(BaseModel):
|
|
|
|
model_name: str = Field(description="Info to load unet submodel")
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type: SDModelType = Field(description="Info to load unet submodel")
|
|
|
|
submodel: Optional[SDModelType] = Field(description="Info to load unet submodel")
|
2023-05-13 01:37:20 +00:00
|
|
|
|
|
|
|
class UNetField(BaseModel):
|
|
|
|
unet: ModelInfo = Field(description="Info to load unet submodel")
|
|
|
|
scheduler: ModelInfo = Field(description="Info to load scheduler submodel")
|
|
|
|
# loras: List[ModelInfo]
|
|
|
|
|
|
|
|
class ClipField(BaseModel):
|
|
|
|
tokenizer: ModelInfo = Field(description="Info to load tokenizer submodel")
|
|
|
|
text_encoder: ModelInfo = Field(description="Info to load text_encoder submodel")
|
|
|
|
# loras: List[ModelInfo]
|
|
|
|
|
|
|
|
class VaeField(BaseModel):
|
|
|
|
# TODO: better naming?
|
|
|
|
vae: ModelInfo = Field(description="Info to load vae submodel")
|
|
|
|
|
|
|
|
|
|
|
|
class ModelLoaderOutput(BaseInvocationOutput):
|
|
|
|
"""Model loader output"""
|
|
|
|
|
|
|
|
#fmt: off
|
|
|
|
type: Literal["model_loader_output"] = "model_loader_output"
|
|
|
|
|
|
|
|
unet: UNetField = Field(default=None, description="UNet submodel")
|
|
|
|
clip: ClipField = Field(default=None, description="Tokenizer and text_encoder submodels")
|
|
|
|
vae: VaeField = Field(default=None, description="Vae submodel")
|
|
|
|
#fmt: on
|
|
|
|
|
|
|
|
|
|
|
|
class ModelLoaderInvocation(BaseInvocation):
|
|
|
|
"""Loading submodels of selected model."""
|
|
|
|
|
|
|
|
type: Literal["model_loader"] = "model_loader"
|
|
|
|
|
|
|
|
model_name: str = Field(default="", description="Model to load")
|
|
|
|
# TODO: precision?
|
|
|
|
|
|
|
|
# Schema customisation
|
|
|
|
class Config(InvocationConfig):
|
|
|
|
schema_extra = {
|
|
|
|
"ui": {
|
|
|
|
"tags": ["model", "loader"],
|
|
|
|
"type_hints": {
|
|
|
|
"model_name": "model" # TODO: rename to model_name?
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> ModelLoaderOutput:
|
|
|
|
|
|
|
|
# TODO: not found exceptions
|
2023-05-13 18:44:44 +00:00
|
|
|
if not context.services.model_manager.model_exists(
|
2023-05-13 01:37:20 +00:00
|
|
|
model_name=self.model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type=SDModelType.Diffusers,
|
2023-05-13 01:37:20 +00:00
|
|
|
):
|
|
|
|
raise Exception(f"Unkown model name: {self.model_name}!")
|
|
|
|
|
|
|
|
"""
|
2023-05-13 18:44:44 +00:00
|
|
|
if not context.services.model_manager.model_exists(
|
2023-05-13 01:37:20 +00:00
|
|
|
model_name=self.model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type=SDModelType.Diffusers,
|
|
|
|
submodel=SDModelType.Tokenizer,
|
2023-05-13 01:37:20 +00:00
|
|
|
):
|
|
|
|
raise Exception(
|
|
|
|
f"Failed to find tokenizer submodel in {self.model_name}! Check if model corrupted"
|
|
|
|
)
|
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
if not context.services.model_manager.model_exists(
|
2023-05-13 01:37:20 +00:00
|
|
|
model_name=self.model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type=SDModelType.Diffusers,
|
|
|
|
submodel=SDModelType.TextEncoder,
|
2023-05-13 01:37:20 +00:00
|
|
|
):
|
|
|
|
raise Exception(
|
|
|
|
f"Failed to find text_encoder submodel in {self.model_name}! Check if model corrupted"
|
|
|
|
)
|
|
|
|
|
2023-05-13 18:44:44 +00:00
|
|
|
if not context.services.model_manager.model_exists(
|
2023-05-13 01:37:20 +00:00
|
|
|
model_name=self.model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type=SDModelType.Diffusers,
|
|
|
|
submodel=SDModelType.UNet,
|
2023-05-13 01:37:20 +00:00
|
|
|
):
|
|
|
|
raise Exception(
|
|
|
|
f"Failed to find unet submodel from {self.model_name}! Check if model corrupted"
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
return ModelLoaderOutput(
|
|
|
|
unet=UNetField(
|
|
|
|
unet=ModelInfo(
|
|
|
|
model_name=self.model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type=SDModelType.Diffusers,
|
|
|
|
submodel=SDModelType.UNet,
|
2023-05-13 01:37:20 +00:00
|
|
|
),
|
|
|
|
scheduler=ModelInfo(
|
|
|
|
model_name=self.model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type=SDModelType.Diffusers,
|
|
|
|
submodel=SDModelType.Scheduler,
|
2023-05-13 01:37:20 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
clip=ClipField(
|
|
|
|
tokenizer=ModelInfo(
|
|
|
|
model_name=self.model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type=SDModelType.Diffusers,
|
|
|
|
submodel=SDModelType.Tokenizer,
|
2023-05-13 01:37:20 +00:00
|
|
|
),
|
|
|
|
text_encoder=ModelInfo(
|
|
|
|
model_name=self.model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type=SDModelType.Diffusers,
|
|
|
|
submodel=SDModelType.TextEncoder,
|
2023-05-13 01:37:20 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
vae=VaeField(
|
|
|
|
vae=ModelInfo(
|
|
|
|
model_name=self.model_name,
|
2023-05-14 00:06:26 +00:00
|
|
|
model_type=SDModelType.Diffusers,
|
|
|
|
submodel=SDModelType.Vae,
|
2023-05-13 01:37:20 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|