2023-05-29 23:12:33 +00:00
|
|
|
import copy
|
2024-08-21 19:37:27 +00:00
|
|
|
from typing import List, Literal, Optional
|
2023-05-13 01:37:20 +00:00
|
|
|
|
2024-02-06 03:56:32 +00:00
|
|
|
from pydantic import BaseModel, Field
|
2023-05-13 01:37:20 +00:00
|
|
|
|
2024-07-03 16:20:35 +00:00
|
|
|
from invokeai.app.invocations.baseinvocation import (
|
2023-08-14 03:23:09 +00:00
|
|
|
BaseInvocation,
|
|
|
|
BaseInvocationOutput,
|
2024-05-19 09:26:17 +00:00
|
|
|
Classification,
|
feat(nodes): move all invocation metadata (type, title, tags, category) to decorator
All invocation metadata (type, title, tags and category) are now defined in decorators.
The decorators add the `type: Literal["invocation_type"]: "invocation_type"` field to the invocation.
Category is a new invocation metadata, but it is not used by the frontend just yet.
- `@invocation()` decorator for invocations
```py
@invocation(
"sdxl_compel_prompt",
title="SDXL Prompt",
tags=["sdxl", "compel", "prompt"],
category="conditioning",
)
class SDXLCompelPromptInvocation(BaseInvocation, SDXLPromptInvocationBase):
...
```
- `@invocation_output()` decorator for invocation outputs
```py
@invocation_output("clip_skip_output")
class ClipSkipInvocationOutput(BaseInvocationOutput):
...
```
- update invocation docs
- add category to decorator
- regen frontend types
2023-08-30 08:35:12 +00:00
|
|
|
invocation,
|
|
|
|
invocation_output,
|
2023-08-14 03:23:09 +00:00
|
|
|
)
|
2024-07-03 16:20:35 +00:00
|
|
|
from invokeai.app.invocations.fields import FieldDescriptions, Input, InputField, OutputField, UIType
|
|
|
|
from invokeai.app.services.shared.invocation_context import InvocationContext
|
|
|
|
from invokeai.app.shared.models import FreeUConfig
|
2024-08-26 14:09:02 +00:00
|
|
|
from invokeai.backend.flux.util import max_seq_lengths
|
2024-08-20 17:05:31 +00:00
|
|
|
from invokeai.backend.model_manager.config import (
|
|
|
|
AnyModelConfig,
|
|
|
|
BaseModelType,
|
|
|
|
CheckpointConfigBase,
|
|
|
|
ModelType,
|
|
|
|
SubModelType,
|
|
|
|
)
|
2023-06-28 20:13:36 +00:00
|
|
|
|
2023-11-07 05:06:18 +00:00
|
|
|
|
2024-03-09 08:43:24 +00:00
|
|
|
class ModelIdentifierField(BaseModel):
|
|
|
|
key: str = Field(description="The model's unique key")
|
|
|
|
hash: str = Field(description="The model's BLAKE3 hash")
|
|
|
|
name: str = Field(description="The model's name")
|
|
|
|
base: BaseModelType = Field(description="The model's base model type")
|
|
|
|
type: ModelType = Field(description="The model's type")
|
|
|
|
submodel_type: Optional[SubModelType] = Field(
|
|
|
|
description="The submodel to load, if this is a main model", default=None
|
|
|
|
)
|
2023-07-04 11:11:50 +00:00
|
|
|
|
2024-03-10 22:18:10 +00:00
|
|
|
@classmethod
|
|
|
|
def from_config(
|
|
|
|
cls, config: "AnyModelConfig", submodel_type: Optional[SubModelType] = None
|
|
|
|
) -> "ModelIdentifierField":
|
|
|
|
return cls(
|
|
|
|
key=config.key,
|
|
|
|
hash=config.hash,
|
|
|
|
name=config.name,
|
|
|
|
base=config.base,
|
|
|
|
type=config.type,
|
|
|
|
submodel_type=submodel_type,
|
|
|
|
)
|
|
|
|
|
2023-05-29 22:11:00 +00:00
|
|
|
|
2024-03-06 08:37:15 +00:00
|
|
|
class LoRAField(BaseModel):
|
2024-03-09 08:43:24 +00:00
|
|
|
lora: ModelIdentifierField = Field(description="Info to load lora model")
|
2024-03-06 08:37:15 +00:00
|
|
|
weight: float = Field(description="Weight to apply to lora model")
|
2023-05-13 01:37:20 +00:00
|
|
|
|
2023-07-04 11:11:50 +00:00
|
|
|
|
2023-05-13 01:37:20 +00:00
|
|
|
class UNetField(BaseModel):
|
2024-03-09 08:43:24 +00:00
|
|
|
unet: ModelIdentifierField = Field(description="Info to load unet submodel")
|
|
|
|
scheduler: ModelIdentifierField = Field(description="Info to load scheduler submodel")
|
2024-03-06 08:42:47 +00:00
|
|
|
loras: List[LoRAField] = Field(description="LoRAs to apply on model loading")
|
2023-08-28 11:10:00 +00:00
|
|
|
seamless_axes: List[str] = Field(default_factory=list, description='Axes("x" and "y") to which apply seamless')
|
2023-10-11 02:49:28 +00:00
|
|
|
freeu_config: Optional[FreeUConfig] = Field(default=None, description="FreeU configuration")
|
2023-05-13 01:37:20 +00:00
|
|
|
|
2023-07-04 11:11:50 +00:00
|
|
|
|
2024-03-06 08:42:47 +00:00
|
|
|
class CLIPField(BaseModel):
|
2024-03-09 08:43:24 +00:00
|
|
|
tokenizer: ModelIdentifierField = Field(description="Info to load tokenizer submodel")
|
|
|
|
text_encoder: ModelIdentifierField = Field(description="Info to load text_encoder submodel")
|
2023-07-06 14:39:49 +00:00
|
|
|
skipped_layers: int = Field(description="Number of skipped layers in text_encoder")
|
2024-03-06 08:42:47 +00:00
|
|
|
loras: List[LoRAField] = Field(description="LoRAs to apply on model loading")
|
2023-05-13 01:37:20 +00:00
|
|
|
|
2023-07-28 13:46:44 +00:00
|
|
|
|
2024-08-12 18:04:23 +00:00
|
|
|
class TransformerField(BaseModel):
|
|
|
|
transformer: ModelIdentifierField = Field(description="Info to load Transformer submodel")
|
|
|
|
|
2024-08-14 15:53:07 +00:00
|
|
|
|
2024-08-12 22:01:42 +00:00
|
|
|
class T5EncoderField(BaseModel):
|
|
|
|
tokenizer: ModelIdentifierField = Field(description="Info to load tokenizer submodel")
|
|
|
|
text_encoder: ModelIdentifierField = Field(description="Info to load text_encoder submodel")
|
|
|
|
|
2024-08-12 18:04:23 +00:00
|
|
|
|
2024-03-06 08:42:47 +00:00
|
|
|
class VAEField(BaseModel):
|
2024-03-09 08:43:24 +00:00
|
|
|
vae: ModelIdentifierField = Field(description="Info to load vae submodel")
|
2023-08-28 11:10:00 +00:00
|
|
|
seamless_axes: List[str] = Field(default_factory=list, description='Axes("x" and "y") to which apply seamless')
|
2023-05-13 01:37:20 +00:00
|
|
|
|
|
|
|
|
2023-10-11 02:49:28 +00:00
|
|
|
@invocation_output("unet_output")
|
|
|
|
class UNetOutput(BaseInvocationOutput):
|
2024-02-06 03:56:32 +00:00
|
|
|
"""Base class for invocations that output a UNet field."""
|
2023-05-13 01:37:20 +00:00
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
unet: UNetField = OutputField(description=FieldDescriptions.unet, title="UNet")
|
2023-10-11 02:49:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@invocation_output("vae_output")
|
|
|
|
class VAEOutput(BaseInvocationOutput):
|
|
|
|
"""Base class for invocations that output a VAE field"""
|
|
|
|
|
2024-03-06 08:42:47 +00:00
|
|
|
vae: VAEField = OutputField(description=FieldDescriptions.vae, title="VAE")
|
2023-05-13 01:37:20 +00:00
|
|
|
|
2023-07-28 13:46:44 +00:00
|
|
|
|
2023-10-11 02:49:28 +00:00
|
|
|
@invocation_output("clip_output")
|
|
|
|
class CLIPOutput(BaseInvocationOutput):
|
|
|
|
"""Base class for invocations that output a CLIP field"""
|
|
|
|
|
2024-03-06 08:42:47 +00:00
|
|
|
clip: CLIPField = OutputField(description=FieldDescriptions.clip, title="CLIP")
|
2023-10-11 02:49:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@invocation_output("model_loader_output")
|
|
|
|
class ModelLoaderOutput(UNetOutput, CLIPOutput, VAEOutput):
|
|
|
|
"""Model loader output"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2024-05-17 10:47:46 +00:00
|
|
|
@invocation_output("model_identifier_output")
|
|
|
|
class ModelIdentifierOutput(BaseInvocationOutput):
|
|
|
|
"""Model identifier output"""
|
|
|
|
|
|
|
|
model: ModelIdentifierField = OutputField(description="Model identifier", title="Model")
|
|
|
|
|
|
|
|
|
|
|
|
@invocation(
|
|
|
|
"model_identifier",
|
|
|
|
title="Model identifier",
|
|
|
|
tags=["model"],
|
|
|
|
category="model",
|
|
|
|
version="1.0.0",
|
2024-05-19 09:26:17 +00:00
|
|
|
classification=Classification.Prototype,
|
2024-05-17 10:47:46 +00:00
|
|
|
)
|
|
|
|
class ModelIdentifierInvocation(BaseInvocation):
|
2024-05-19 09:26:17 +00:00
|
|
|
"""Selects any model, outputting it its identifier. Be careful with this one! The identifier will be accepted as
|
|
|
|
input for any model, even if the model types don't match. If you connect this to a mismatched input, you'll get an
|
|
|
|
error."""
|
2024-05-17 10:47:46 +00:00
|
|
|
|
|
|
|
model: ModelIdentifierField = InputField(description="The model to select", title="Model")
|
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> ModelIdentifierOutput:
|
|
|
|
if not context.models.exists(self.model.key):
|
|
|
|
raise Exception(f"Unknown model {self.model.key}")
|
|
|
|
|
|
|
|
return ModelIdentifierOutput(model=self.model)
|
|
|
|
|
2024-08-19 14:14:58 +00:00
|
|
|
|
2024-08-12 18:04:23 +00:00
|
|
|
@invocation_output("flux_model_loader_output")
|
|
|
|
class FluxModelLoaderOutput(BaseInvocationOutput):
|
|
|
|
"""Flux base model loader output"""
|
|
|
|
|
|
|
|
transformer: TransformerField = OutputField(description=FieldDescriptions.transformer, title="Transformer")
|
2024-08-12 22:01:42 +00:00
|
|
|
clip: CLIPField = OutputField(description=FieldDescriptions.clip, title="CLIP")
|
2024-08-21 13:27:54 +00:00
|
|
|
t5_encoder: T5EncoderField = OutputField(description=FieldDescriptions.t5_encoder, title="T5 Encoder")
|
2024-08-12 18:04:23 +00:00
|
|
|
vae: VAEField = OutputField(description=FieldDescriptions.vae, title="VAE")
|
2024-08-21 19:17:39 +00:00
|
|
|
max_seq_len: Literal[256, 512] = OutputField(
|
|
|
|
description="The max sequence length to used for the T5 encoder. (256 for schnell transformer, 512 for dev transformer)",
|
|
|
|
title="Max Seq Length",
|
|
|
|
)
|
2024-08-12 18:04:23 +00:00
|
|
|
|
|
|
|
|
2024-08-22 15:29:59 +00:00
|
|
|
@invocation(
|
|
|
|
"flux_model_loader",
|
|
|
|
title="Flux Main Model",
|
|
|
|
tags=["model", "flux"],
|
|
|
|
category="model",
|
2024-08-28 19:57:49 +00:00
|
|
|
version="1.0.4",
|
2024-08-22 15:29:59 +00:00
|
|
|
classification=Classification.Prototype,
|
|
|
|
)
|
2024-08-12 18:04:23 +00:00
|
|
|
class FluxModelLoaderInvocation(BaseInvocation):
|
|
|
|
"""Loads a flux base model, outputting its submodels."""
|
|
|
|
|
|
|
|
model: ModelIdentifierField = InputField(
|
|
|
|
description=FieldDescriptions.flux_model,
|
|
|
|
ui_type=UIType.FluxMainModel,
|
|
|
|
input=Input.Direct,
|
|
|
|
)
|
2024-08-19 14:14:58 +00:00
|
|
|
|
2024-08-28 18:54:36 +00:00
|
|
|
t5_encoder_model: ModelIdentifierField = InputField(
|
|
|
|
description=FieldDescriptions.t5_encoder, ui_type=UIType.T5EncoderModel, input=Input.Direct, title="T5 Encoder"
|
|
|
|
)
|
|
|
|
|
|
|
|
clip_embed_model: ModelIdentifierField = InputField(
|
|
|
|
description=FieldDescriptions.clip_embed_model,
|
|
|
|
ui_type=UIType.CLIPEmbedModel,
|
2024-08-21 18:25:53 +00:00
|
|
|
input=Input.Direct,
|
2024-08-28 18:54:36 +00:00
|
|
|
title="CLIP Embed",
|
|
|
|
)
|
|
|
|
|
|
|
|
vae_model: ModelIdentifierField = InputField(
|
|
|
|
description=FieldDescriptions.vae_model, ui_type=UIType.FluxVAEModel, title="VAE"
|
2024-08-21 18:25:53 +00:00
|
|
|
)
|
2024-08-12 18:04:23 +00:00
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> FluxModelLoaderOutput:
|
2024-08-28 18:54:36 +00:00
|
|
|
for key in [self.model.key, self.t5_encoder_model.key, self.clip_embed_model.key, self.vae_model.key]:
|
|
|
|
if not context.models.exists(key):
|
|
|
|
raise ValueError(f"Unknown model: {key}")
|
|
|
|
|
|
|
|
transformer = self.model.model_copy(update={"submodel_type": SubModelType.Transformer})
|
|
|
|
vae = self.vae_model.model_copy(update={"submodel_type": SubModelType.VAE})
|
|
|
|
|
|
|
|
tokenizer = self.clip_embed_model.model_copy(update={"submodel_type": SubModelType.Tokenizer})
|
|
|
|
clip_encoder = self.clip_embed_model.model_copy(update={"submodel_type": SubModelType.TextEncoder})
|
|
|
|
|
|
|
|
tokenizer2 = self.t5_encoder_model.model_copy(update={"submodel_type": SubModelType.Tokenizer2})
|
|
|
|
t5_encoder = self.t5_encoder_model.model_copy(update={"submodel_type": SubModelType.TextEncoder2})
|
|
|
|
|
2024-08-19 18:41:28 +00:00
|
|
|
transformer_config = context.models.get_config(transformer)
|
|
|
|
assert isinstance(transformer_config, CheckpointConfigBase)
|
2024-08-12 18:04:23 +00:00
|
|
|
|
|
|
|
return FluxModelLoaderOutput(
|
2024-08-16 21:04:48 +00:00
|
|
|
transformer=TransformerField(transformer=transformer),
|
|
|
|
clip=CLIPField(tokenizer=tokenizer, text_encoder=clip_encoder, loras=[], skipped_layers=0),
|
2024-08-19 18:41:28 +00:00
|
|
|
t5_encoder=T5EncoderField(tokenizer=tokenizer2, text_encoder=t5_encoder),
|
2024-08-12 18:04:23 +00:00
|
|
|
vae=VAEField(vae=vae),
|
2024-08-25 06:41:13 +00:00
|
|
|
max_seq_len=max_seq_lengths[transformer_config.config_path],
|
2024-08-12 18:04:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
@invocation(
|
|
|
|
"main_model_loader",
|
|
|
|
title="Main Model",
|
|
|
|
tags=["model"],
|
|
|
|
category="model",
|
2024-05-17 10:15:04 +00:00
|
|
|
version="1.0.3",
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
)
|
2023-06-28 20:13:36 +00:00
|
|
|
class MainModelLoaderInvocation(BaseInvocation):
|
|
|
|
"""Loads a main model, outputting its submodels."""
|
2023-06-13 15:05:12 +00:00
|
|
|
|
2024-05-17 10:15:04 +00:00
|
|
|
model: ModelIdentifierField = InputField(description=FieldDescriptions.main_model, ui_type=UIType.MainModel)
|
2023-06-13 15:05:12 +00:00
|
|
|
# TODO: precision?
|
|
|
|
|
2024-02-05 06:16:35 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> ModelLoaderOutput:
|
2023-05-13 01:37:20 +00:00
|
|
|
# TODO: not found exceptions
|
2024-03-06 08:37:15 +00:00
|
|
|
if not context.models.exists(self.model.key):
|
|
|
|
raise Exception(f"Unknown model {self.model.key}")
|
|
|
|
|
|
|
|
unet = self.model.model_copy(update={"submodel_type": SubModelType.UNet})
|
|
|
|
scheduler = self.model.model_copy(update={"submodel_type": SubModelType.Scheduler})
|
|
|
|
tokenizer = self.model.model_copy(update={"submodel_type": SubModelType.Tokenizer})
|
|
|
|
text_encoder = self.model.model_copy(update={"submodel_type": SubModelType.TextEncoder})
|
|
|
|
vae = self.model.model_copy(update={"submodel_type": SubModelType.VAE})
|
2023-05-13 01:37:20 +00:00
|
|
|
|
|
|
|
return ModelLoaderOutput(
|
2024-03-06 08:37:15 +00:00
|
|
|
unet=UNetField(unet=unet, scheduler=scheduler, loras=[]),
|
2024-03-06 08:42:47 +00:00
|
|
|
clip=CLIPField(tokenizer=tokenizer, text_encoder=text_encoder, loras=[], skipped_layers=0),
|
|
|
|
vae=VAEField(vae=vae),
|
2023-05-13 01:37:20 +00:00
|
|
|
)
|
2023-05-29 23:12:33 +00:00
|
|
|
|
2023-07-28 13:46:44 +00:00
|
|
|
|
feat(nodes): move all invocation metadata (type, title, tags, category) to decorator
All invocation metadata (type, title, tags and category) are now defined in decorators.
The decorators add the `type: Literal["invocation_type"]: "invocation_type"` field to the invocation.
Category is a new invocation metadata, but it is not used by the frontend just yet.
- `@invocation()` decorator for invocations
```py
@invocation(
"sdxl_compel_prompt",
title="SDXL Prompt",
tags=["sdxl", "compel", "prompt"],
category="conditioning",
)
class SDXLCompelPromptInvocation(BaseInvocation, SDXLPromptInvocationBase):
...
```
- `@invocation_output()` decorator for invocation outputs
```py
@invocation_output("clip_skip_output")
class ClipSkipInvocationOutput(BaseInvocationOutput):
...
```
- update invocation docs
- add category to decorator
- regen frontend types
2023-08-30 08:35:12 +00:00
|
|
|
@invocation_output("lora_loader_output")
|
2024-03-06 08:42:47 +00:00
|
|
|
class LoRALoaderOutput(BaseInvocationOutput):
|
2023-05-29 23:12:33 +00:00
|
|
|
"""Model loader output"""
|
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
unet: Optional[UNetField] = OutputField(default=None, description=FieldDescriptions.unet, title="UNet")
|
2024-03-06 08:42:47 +00:00
|
|
|
clip: Optional[CLIPField] = OutputField(default=None, description=FieldDescriptions.clip, title="CLIP")
|
2023-07-04 11:11:50 +00:00
|
|
|
|
2023-05-29 23:12:33 +00:00
|
|
|
|
2024-05-17 10:15:04 +00:00
|
|
|
@invocation("lora_loader", title="LoRA", tags=["model"], category="model", version="1.0.3")
|
2024-03-06 08:42:47 +00:00
|
|
|
class LoRALoaderInvocation(BaseInvocation):
|
2023-05-29 23:12:33 +00:00
|
|
|
"""Apply selected lora to unet and text_encoder."""
|
|
|
|
|
2024-03-09 08:43:24 +00:00
|
|
|
lora: ModelIdentifierField = InputField(
|
2024-05-17 10:15:04 +00:00
|
|
|
description=FieldDescriptions.lora_model, title="LoRA", ui_type=UIType.LoRAModel
|
2024-03-08 11:30:50 +00:00
|
|
|
)
|
2023-08-14 03:23:09 +00:00
|
|
|
weight: float = InputField(default=0.75, description=FieldDescriptions.lora_weight)
|
|
|
|
unet: Optional[UNetField] = InputField(
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.unet,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="UNet",
|
2023-08-14 03:23:09 +00:00
|
|
|
)
|
2024-03-06 08:42:47 +00:00
|
|
|
clip: Optional[CLIPField] = InputField(
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.clip,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="CLIP",
|
2023-08-14 03:23:09 +00:00
|
|
|
)
|
2023-06-30 23:10:46 +00:00
|
|
|
|
2024-03-06 08:42:47 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> LoRALoaderOutput:
|
2024-02-06 03:56:32 +00:00
|
|
|
lora_key = self.lora.key
|
2023-05-29 23:12:33 +00:00
|
|
|
|
2024-02-15 09:43:41 +00:00
|
|
|
if not context.models.exists(lora_key):
|
2024-02-06 03:56:32 +00:00
|
|
|
raise Exception(f"Unkown lora: {lora_key}!")
|
2023-05-29 23:12:33 +00:00
|
|
|
|
2024-03-06 08:37:15 +00:00
|
|
|
if self.unet is not None and any(lora.lora.key == lora_key for lora in self.unet.loras):
|
2024-03-06 08:42:47 +00:00
|
|
|
raise Exception(f'LoRA "{lora_key}" already applied to unet')
|
2023-05-29 23:12:33 +00:00
|
|
|
|
2024-03-06 08:37:15 +00:00
|
|
|
if self.clip is not None and any(lora.lora.key == lora_key for lora in self.clip.loras):
|
2024-03-06 08:42:47 +00:00
|
|
|
raise Exception(f'LoRA "{lora_key}" already applied to clip')
|
2023-05-29 23:12:33 +00:00
|
|
|
|
2024-03-06 08:42:47 +00:00
|
|
|
output = LoRALoaderOutput()
|
2023-05-29 23:12:33 +00:00
|
|
|
|
|
|
|
if self.unet is not None:
|
2024-03-06 08:37:15 +00:00
|
|
|
output.unet = self.unet.model_copy(deep=True)
|
2023-05-29 23:12:33 +00:00
|
|
|
output.unet.loras.append(
|
2024-03-06 08:37:15 +00:00
|
|
|
LoRAField(
|
|
|
|
lora=self.lora,
|
2023-05-29 23:12:33 +00:00
|
|
|
weight=self.weight,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.clip is not None:
|
2024-03-06 08:37:15 +00:00
|
|
|
output.clip = self.clip.model_copy(deep=True)
|
2023-05-29 23:12:33 +00:00
|
|
|
output.clip.loras.append(
|
2024-03-06 08:37:15 +00:00
|
|
|
LoRAField(
|
|
|
|
lora=self.lora,
|
2023-05-29 23:12:33 +00:00
|
|
|
weight=self.weight,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
2023-07-04 11:11:50 +00:00
|
|
|
|
2024-05-09 09:05:34 +00:00
|
|
|
@invocation_output("lora_selector_output")
|
|
|
|
class LoRASelectorOutput(BaseInvocationOutput):
|
|
|
|
"""Model loader output"""
|
|
|
|
|
|
|
|
lora: LoRAField = OutputField(description="LoRA model and weight", title="LoRA")
|
|
|
|
|
|
|
|
|
2024-05-17 10:15:04 +00:00
|
|
|
@invocation("lora_selector", title="LoRA Selector", tags=["model"], category="model", version="1.0.1")
|
2024-05-09 09:05:34 +00:00
|
|
|
class LoRASelectorInvocation(BaseInvocation):
|
|
|
|
"""Selects a LoRA model and weight."""
|
|
|
|
|
|
|
|
lora: ModelIdentifierField = InputField(
|
2024-05-17 10:15:04 +00:00
|
|
|
description=FieldDescriptions.lora_model, title="LoRA", ui_type=UIType.LoRAModel
|
2024-05-09 09:05:34 +00:00
|
|
|
)
|
|
|
|
weight: float = InputField(default=0.75, description=FieldDescriptions.lora_weight)
|
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> LoRASelectorOutput:
|
|
|
|
return LoRASelectorOutput(lora=LoRAField(lora=self.lora, weight=self.weight))
|
|
|
|
|
|
|
|
|
|
|
|
@invocation("lora_collection_loader", title="LoRA Collection Loader", tags=["model"], category="model", version="1.0.0")
|
|
|
|
class LoRACollectionLoader(BaseInvocation):
|
|
|
|
"""Applies a collection of LoRAs to the provided UNet and CLIP models."""
|
|
|
|
|
|
|
|
loras: LoRAField | list[LoRAField] = InputField(
|
|
|
|
description="LoRA models and weights. May be a single LoRA or collection.", title="LoRAs"
|
|
|
|
)
|
|
|
|
unet: Optional[UNetField] = InputField(
|
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.unet,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="UNet",
|
|
|
|
)
|
|
|
|
clip: Optional[CLIPField] = InputField(
|
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.clip,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="CLIP",
|
|
|
|
)
|
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> LoRALoaderOutput:
|
|
|
|
output = LoRALoaderOutput()
|
|
|
|
loras = self.loras if isinstance(self.loras, list) else [self.loras]
|
|
|
|
added_loras: list[str] = []
|
|
|
|
|
|
|
|
for lora in loras:
|
|
|
|
if lora.lora.key in added_loras:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not context.models.exists(lora.lora.key):
|
|
|
|
raise Exception(f"Unknown lora: {lora.lora.key}!")
|
|
|
|
|
|
|
|
assert lora.lora.base in (BaseModelType.StableDiffusion1, BaseModelType.StableDiffusion2)
|
|
|
|
|
|
|
|
added_loras.append(lora.lora.key)
|
|
|
|
|
|
|
|
if self.unet is not None:
|
|
|
|
if output.unet is None:
|
|
|
|
output.unet = self.unet.model_copy(deep=True)
|
|
|
|
output.unet.loras.append(lora)
|
|
|
|
|
|
|
|
if self.clip is not None:
|
|
|
|
if output.clip is None:
|
|
|
|
output.clip = self.clip.model_copy(deep=True)
|
|
|
|
output.clip.loras.append(lora)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
feat(nodes): move all invocation metadata (type, title, tags, category) to decorator
All invocation metadata (type, title, tags and category) are now defined in decorators.
The decorators add the `type: Literal["invocation_type"]: "invocation_type"` field to the invocation.
Category is a new invocation metadata, but it is not used by the frontend just yet.
- `@invocation()` decorator for invocations
```py
@invocation(
"sdxl_compel_prompt",
title="SDXL Prompt",
tags=["sdxl", "compel", "prompt"],
category="conditioning",
)
class SDXLCompelPromptInvocation(BaseInvocation, SDXLPromptInvocationBase):
...
```
- `@invocation_output()` decorator for invocation outputs
```py
@invocation_output("clip_skip_output")
class ClipSkipInvocationOutput(BaseInvocationOutput):
...
```
- update invocation docs
- add category to decorator
- regen frontend types
2023-08-30 08:35:12 +00:00
|
|
|
@invocation_output("sdxl_lora_loader_output")
|
2024-03-06 08:42:47 +00:00
|
|
|
class SDXLLoRALoaderOutput(BaseInvocationOutput):
|
2023-08-14 03:23:09 +00:00
|
|
|
"""SDXL LoRA Loader Output"""
|
2023-07-31 20:18:02 +00:00
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
unet: Optional[UNetField] = OutputField(default=None, description=FieldDescriptions.unet, title="UNet")
|
2024-03-06 08:42:47 +00:00
|
|
|
clip: Optional[CLIPField] = OutputField(default=None, description=FieldDescriptions.clip, title="CLIP 1")
|
|
|
|
clip2: Optional[CLIPField] = OutputField(default=None, description=FieldDescriptions.clip, title="CLIP 2")
|
2023-07-31 20:18:02 +00:00
|
|
|
|
|
|
|
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
@invocation(
|
|
|
|
"sdxl_lora_loader",
|
|
|
|
title="SDXL LoRA",
|
|
|
|
tags=["lora", "model"],
|
|
|
|
category="model",
|
2024-05-17 10:15:04 +00:00
|
|
|
version="1.0.3",
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
)
|
2024-03-06 08:42:47 +00:00
|
|
|
class SDXLLoRALoaderInvocation(BaseInvocation):
|
2023-07-31 20:18:02 +00:00
|
|
|
"""Apply selected lora to unet and text_encoder."""
|
|
|
|
|
2024-03-09 08:43:24 +00:00
|
|
|
lora: ModelIdentifierField = InputField(
|
2024-05-17 10:15:04 +00:00
|
|
|
description=FieldDescriptions.lora_model, title="LoRA", ui_type=UIType.LoRAModel
|
2024-03-08 11:30:50 +00:00
|
|
|
)
|
2023-09-01 22:47:55 +00:00
|
|
|
weight: float = InputField(default=0.75, description=FieldDescriptions.lora_weight)
|
2023-09-02 01:34:17 +00:00
|
|
|
unet: Optional[UNetField] = InputField(
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.unet,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="UNet",
|
2023-08-14 03:23:09 +00:00
|
|
|
)
|
2024-03-06 08:42:47 +00:00
|
|
|
clip: Optional[CLIPField] = InputField(
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.clip,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="CLIP 1",
|
2023-08-14 03:23:09 +00:00
|
|
|
)
|
2024-03-06 08:42:47 +00:00
|
|
|
clip2: Optional[CLIPField] = InputField(
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.clip,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="CLIP 2",
|
2023-08-14 03:23:09 +00:00
|
|
|
)
|
2023-07-31 20:18:02 +00:00
|
|
|
|
2024-03-06 08:42:47 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> SDXLLoRALoaderOutput:
|
2024-02-06 03:56:32 +00:00
|
|
|
lora_key = self.lora.key
|
2023-07-31 20:18:02 +00:00
|
|
|
|
2024-02-15 09:43:41 +00:00
|
|
|
if not context.models.exists(lora_key):
|
2024-02-06 03:56:32 +00:00
|
|
|
raise Exception(f"Unknown lora: {lora_key}!")
|
2023-07-31 20:18:02 +00:00
|
|
|
|
2024-03-06 08:37:15 +00:00
|
|
|
if self.unet is not None and any(lora.lora.key == lora_key for lora in self.unet.loras):
|
2024-03-06 08:42:47 +00:00
|
|
|
raise Exception(f'LoRA "{lora_key}" already applied to unet')
|
2023-07-31 20:18:02 +00:00
|
|
|
|
2024-03-06 08:37:15 +00:00
|
|
|
if self.clip is not None and any(lora.lora.key == lora_key for lora in self.clip.loras):
|
2024-03-06 08:42:47 +00:00
|
|
|
raise Exception(f'LoRA "{lora_key}" already applied to clip')
|
2023-07-31 20:18:02 +00:00
|
|
|
|
2024-03-06 08:37:15 +00:00
|
|
|
if self.clip2 is not None and any(lora.lora.key == lora_key for lora in self.clip2.loras):
|
2024-03-06 08:42:47 +00:00
|
|
|
raise Exception(f'LoRA "{lora_key}" already applied to clip2')
|
2023-07-31 20:18:02 +00:00
|
|
|
|
2024-03-06 08:42:47 +00:00
|
|
|
output = SDXLLoRALoaderOutput()
|
2023-07-31 20:18:02 +00:00
|
|
|
|
|
|
|
if self.unet is not None:
|
2024-03-06 08:37:15 +00:00
|
|
|
output.unet = self.unet.model_copy(deep=True)
|
2023-07-31 20:18:02 +00:00
|
|
|
output.unet.loras.append(
|
2024-03-06 08:37:15 +00:00
|
|
|
LoRAField(
|
|
|
|
lora=self.lora,
|
2023-07-31 20:18:02 +00:00
|
|
|
weight=self.weight,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.clip is not None:
|
2024-03-06 08:37:15 +00:00
|
|
|
output.clip = self.clip.model_copy(deep=True)
|
2023-07-31 20:18:02 +00:00
|
|
|
output.clip.loras.append(
|
2024-03-06 08:37:15 +00:00
|
|
|
LoRAField(
|
|
|
|
lora=self.lora,
|
2023-07-31 20:18:02 +00:00
|
|
|
weight=self.weight,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.clip2 is not None:
|
2024-03-06 08:37:15 +00:00
|
|
|
output.clip2 = self.clip2.model_copy(deep=True)
|
2023-07-31 20:18:02 +00:00
|
|
|
output.clip2.loras.append(
|
2024-03-06 08:37:15 +00:00
|
|
|
LoRAField(
|
|
|
|
lora=self.lora,
|
2023-07-31 20:18:02 +00:00
|
|
|
weight=self.weight,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2024-05-09 09:05:34 +00:00
|
|
|
@invocation(
|
|
|
|
"sdxl_lora_collection_loader",
|
|
|
|
title="SDXL LoRA Collection Loader",
|
|
|
|
tags=["model"],
|
|
|
|
category="model",
|
|
|
|
version="1.0.0",
|
|
|
|
)
|
|
|
|
class SDXLLoRACollectionLoader(BaseInvocation):
|
|
|
|
"""Applies a collection of SDXL LoRAs to the provided UNet and CLIP models."""
|
|
|
|
|
|
|
|
loras: LoRAField | list[LoRAField] = InputField(
|
|
|
|
description="LoRA models and weights. May be a single LoRA or collection.", title="LoRAs"
|
|
|
|
)
|
|
|
|
unet: Optional[UNetField] = InputField(
|
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.unet,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="UNet",
|
|
|
|
)
|
|
|
|
clip: Optional[CLIPField] = InputField(
|
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.clip,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="CLIP",
|
|
|
|
)
|
|
|
|
clip2: Optional[CLIPField] = InputField(
|
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.clip,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="CLIP 2",
|
|
|
|
)
|
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> SDXLLoRALoaderOutput:
|
|
|
|
output = SDXLLoRALoaderOutput()
|
|
|
|
loras = self.loras if isinstance(self.loras, list) else [self.loras]
|
|
|
|
added_loras: list[str] = []
|
|
|
|
|
|
|
|
for lora in loras:
|
|
|
|
if lora.lora.key in added_loras:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not context.models.exists(lora.lora.key):
|
|
|
|
raise Exception(f"Unknown lora: {lora.lora.key}!")
|
|
|
|
|
|
|
|
assert lora.lora.base is BaseModelType.StableDiffusionXL
|
|
|
|
|
|
|
|
added_loras.append(lora.lora.key)
|
|
|
|
|
|
|
|
if self.unet is not None:
|
|
|
|
if output.unet is None:
|
|
|
|
output.unet = self.unet.model_copy(deep=True)
|
|
|
|
output.unet.loras.append(lora)
|
|
|
|
|
|
|
|
if self.clip is not None:
|
|
|
|
if output.clip is None:
|
|
|
|
output.clip = self.clip.model_copy(deep=True)
|
|
|
|
output.clip.loras.append(lora)
|
|
|
|
|
|
|
|
if self.clip2 is not None:
|
|
|
|
if output.clip2 is None:
|
|
|
|
output.clip2 = self.clip2.model_copy(deep=True)
|
|
|
|
output.clip2.loras.append(lora)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2024-05-17 10:15:04 +00:00
|
|
|
@invocation("vae_loader", title="VAE", tags=["vae", "model"], category="model", version="1.0.3")
|
2024-03-06 08:42:47 +00:00
|
|
|
class VAELoaderInvocation(BaseInvocation):
|
2023-06-30 22:15:04 +00:00
|
|
|
"""Loads a VAE model, outputting a VaeLoaderOutput"""
|
2023-07-04 11:11:50 +00:00
|
|
|
|
2024-03-09 08:43:24 +00:00
|
|
|
vae_model: ModelIdentifierField = InputField(
|
2024-05-17 10:15:04 +00:00
|
|
|
description=FieldDescriptions.vae_model, title="VAE", ui_type=UIType.VAEModel
|
2023-08-14 03:23:09 +00:00
|
|
|
)
|
2023-07-04 11:11:50 +00:00
|
|
|
|
2024-02-05 06:16:35 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> VAEOutput:
|
2024-02-06 03:56:32 +00:00
|
|
|
key = self.vae_model.key
|
|
|
|
|
2024-02-15 09:43:41 +00:00
|
|
|
if not context.models.exists(key):
|
2024-02-06 03:56:32 +00:00
|
|
|
raise Exception(f"Unkown vae: {key}!")
|
|
|
|
|
2024-03-06 08:42:47 +00:00
|
|
|
return VAEOutput(vae=VAEField(vae=self.vae_model))
|
2023-08-27 18:13:00 +00:00
|
|
|
|
|
|
|
|
feat(nodes): move all invocation metadata (type, title, tags, category) to decorator
All invocation metadata (type, title, tags and category) are now defined in decorators.
The decorators add the `type: Literal["invocation_type"]: "invocation_type"` field to the invocation.
Category is a new invocation metadata, but it is not used by the frontend just yet.
- `@invocation()` decorator for invocations
```py
@invocation(
"sdxl_compel_prompt",
title="SDXL Prompt",
tags=["sdxl", "compel", "prompt"],
category="conditioning",
)
class SDXLCompelPromptInvocation(BaseInvocation, SDXLPromptInvocationBase):
...
```
- `@invocation_output()` decorator for invocation outputs
```py
@invocation_output("clip_skip_output")
class ClipSkipInvocationOutput(BaseInvocationOutput):
...
```
- update invocation docs
- add category to decorator
- regen frontend types
2023-08-30 08:35:12 +00:00
|
|
|
@invocation_output("seamless_output")
|
2023-08-27 18:13:00 +00:00
|
|
|
class SeamlessModeOutput(BaseInvocationOutput):
|
|
|
|
"""Modified Seamless Model output"""
|
|
|
|
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
unet: Optional[UNetField] = OutputField(default=None, description=FieldDescriptions.unet, title="UNet")
|
2024-03-06 08:42:47 +00:00
|
|
|
vae: Optional[VAEField] = OutputField(default=None, description=FieldDescriptions.vae, title="VAE")
|
2023-08-27 18:13:00 +00:00
|
|
|
|
2023-08-28 11:10:00 +00:00
|
|
|
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
@invocation(
|
|
|
|
"seamless",
|
|
|
|
title="Seamless",
|
|
|
|
tags=["seamless", "model"],
|
|
|
|
category="model",
|
2024-03-19 11:08:16 +00:00
|
|
|
version="1.0.1",
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
)
|
2023-08-27 18:13:00 +00:00
|
|
|
class SeamlessModeInvocation(BaseInvocation):
|
2023-08-28 04:10:46 +00:00
|
|
|
"""Applies the seamless transformation to the Model UNet and VAE."""
|
2023-08-27 18:13:00 +00:00
|
|
|
|
2023-08-28 16:04:03 +00:00
|
|
|
unet: Optional[UNetField] = InputField(
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.unet,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="UNet",
|
2023-08-28 16:04:03 +00:00
|
|
|
)
|
2024-03-06 08:42:47 +00:00
|
|
|
vae: Optional[VAEField] = InputField(
|
feat(api): chore: pydantic & fastapi upgrade
Upgrade pydantic and fastapi to latest.
- pydantic~=2.4.2
- fastapi~=103.2
- fastapi-events~=0.9.1
**Big Changes**
There are a number of logic changes needed to support pydantic v2. Most changes are very simple, like using the new methods to serialized and deserialize models, but there are a few more complex changes.
**Invocations**
The biggest change relates to invocation creation, instantiation and validation.
Because pydantic v2 moves all validation logic into the rust pydantic-core, we may no longer directly stick our fingers into the validation pie.
Previously, we (ab)used models and fields to allow invocation fields to be optional at instantiation, but required when `invoke()` is called. We directly manipulated the fields and invocation models when calling `invoke()`.
With pydantic v2, this is much more involved. Changes to the python wrapper do not propagate down to the rust validation logic - you have to rebuild the model. This causes problem with concurrent access to the invocation classes and is not a free operation.
This logic has been totally refactored and we do not need to change the model any more. The details are in `baseinvocation.py`, in the `InputField` function and `BaseInvocation.invoke_internal()` method.
In the end, this implementation is cleaner.
**Invocation Fields**
In pydantic v2, you can no longer directly add or remove fields from a model.
Previously, we did this to add the `type` field to invocations.
**Invocation Decorators**
With pydantic v2, we instead use the imperative `create_model()` API to create a new model with the additional field. This is done in `baseinvocation.py` in the `invocation()` wrapper.
A similar technique is used for `invocation_output()`.
**Minor Changes**
There are a number of minor changes around the pydantic v2 models API.
**Protected `model_` Namespace**
All models' pydantic-provided methods and attributes are prefixed with `model_` and this is considered a protected namespace. This causes some conflict, because "model" means something to us, and we have a ton of pydantic models with attributes starting with "model_".
Forunately, there are no direct conflicts. However, in any pydantic model where we define an attribute or method that starts with "model_", we must tell set the protected namespaces to an empty tuple.
```py
class IPAdapterModelField(BaseModel):
model_name: str = Field(description="Name of the IP-Adapter model")
base_model: BaseModelType = Field(description="Base model")
model_config = ConfigDict(protected_namespaces=())
```
**Model Serialization**
Pydantic models no longer have `Model.dict()` or `Model.json()`.
Instead, we use `Model.model_dump()` or `Model.model_dump_json()`.
**Model Deserialization**
Pydantic models no longer have `Model.parse_obj()` or `Model.parse_raw()`, and there are no `parse_raw_as()` or `parse_obj_as()` functions.
Instead, you need to create a `TypeAdapter` object to parse python objects or JSON into a model.
```py
adapter_graph = TypeAdapter(Graph)
deserialized_graph_from_json = adapter_graph.validate_json(graph_json)
deserialized_graph_from_dict = adapter_graph.validate_python(graph_dict)
```
**Field Customisation**
Pydantic `Field`s no longer accept arbitrary args.
Now, you must put all additional arbitrary args in a `json_schema_extra` arg on the field.
**Schema Customisation**
FastAPI and pydantic schema generation now follows the OpenAPI version 3.1 spec.
This necessitates two changes:
- Our schema customization logic has been revised
- Schema parsing to build node templates has been revised
The specific aren't important, but this does present additional surface area for bugs.
**Performance Improvements**
Pydantic v2 is a full rewrite with a rust backend. This offers a substantial performance improvement (pydantic claims 5x to 50x depending on the task). We'll notice this the most during serialization and deserialization of sessions/graphs, which happens very very often - a couple times per node.
I haven't done any benchmarks, but anecdotally, graph execution is much faster. Also, very larges graphs - like with massive iterators - are much, much faster.
2023-09-24 08:11:07 +00:00
|
|
|
default=None,
|
|
|
|
description=FieldDescriptions.vae_model,
|
|
|
|
input=Input.Connection,
|
|
|
|
title="VAE",
|
2023-08-28 16:04:03 +00:00
|
|
|
)
|
2023-08-27 18:13:00 +00:00
|
|
|
seamless_y: bool = InputField(default=True, input=Input.Any, description="Specify whether Y axis is seamless")
|
|
|
|
seamless_x: bool = InputField(default=True, input=Input.Any, description="Specify whether X axis is seamless")
|
|
|
|
|
2024-02-05 06:16:35 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> SeamlessModeOutput:
|
2023-08-28 11:10:00 +00:00
|
|
|
# Conditionally append 'x' and 'y' based on seamless_x and seamless_y
|
2023-08-27 18:13:00 +00:00
|
|
|
unet = copy.deepcopy(self.unet)
|
2023-08-27 18:53:57 +00:00
|
|
|
vae = copy.deepcopy(self.vae)
|
2023-08-28 11:10:00 +00:00
|
|
|
|
2023-08-27 18:13:00 +00:00
|
|
|
seamless_axes_list = []
|
|
|
|
|
|
|
|
if self.seamless_x:
|
2023-08-28 11:10:00 +00:00
|
|
|
seamless_axes_list.append("x")
|
2023-08-27 18:13:00 +00:00
|
|
|
if self.seamless_y:
|
2023-08-28 11:10:00 +00:00
|
|
|
seamless_axes_list.append("y")
|
2023-08-27 18:13:00 +00:00
|
|
|
|
2023-08-28 12:43:08 +00:00
|
|
|
if unet is not None:
|
|
|
|
unet.seamless_axes = seamless_axes_list
|
|
|
|
if vae is not None:
|
|
|
|
vae.seamless_axes = seamless_axes_list
|
2023-08-28 11:10:00 +00:00
|
|
|
|
|
|
|
return SeamlessModeOutput(unet=unet, vae=vae)
|
2023-10-11 02:49:28 +00:00
|
|
|
|
|
|
|
|
2024-03-19 11:08:16 +00:00
|
|
|
@invocation("freeu", title="FreeU", tags=["freeu"], category="unet", version="1.0.1")
|
2023-10-11 02:49:28 +00:00
|
|
|
class FreeUInvocation(BaseInvocation):
|
|
|
|
"""
|
|
|
|
Applies FreeU to the UNet. Suggested values (b1/b2/s1/s2):
|
|
|
|
|
|
|
|
SD1.5: 1.2/1.4/0.9/0.2,
|
|
|
|
SD2: 1.1/1.2/0.9/0.2,
|
|
|
|
SDXL: 1.1/1.2/0.6/0.4,
|
|
|
|
"""
|
|
|
|
|
|
|
|
unet: UNetField = InputField(description=FieldDescriptions.unet, input=Input.Connection, title="UNet")
|
|
|
|
b1: float = InputField(default=1.2, ge=-1, le=3, description=FieldDescriptions.freeu_b1)
|
|
|
|
b2: float = InputField(default=1.4, ge=-1, le=3, description=FieldDescriptions.freeu_b2)
|
|
|
|
s1: float = InputField(default=0.9, ge=-1, le=3, description=FieldDescriptions.freeu_s1)
|
|
|
|
s2: float = InputField(default=0.2, ge=-1, le=3, description=FieldDescriptions.freeu_s2)
|
|
|
|
|
2024-02-05 06:16:35 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> UNetOutput:
|
2023-10-11 02:49:28 +00:00
|
|
|
self.unet.freeu_config = FreeUConfig(s1=self.s1, s2=self.s2, b1=self.b1, b2=self.b2)
|
|
|
|
return UNetOutput(unet=self.unet)
|