Skip to content

Commit

Permalink
Add type annotation UnetWrapperFunction (#3531)
Browse files Browse the repository at this point in the history
* Add type annotation UnetWrapperFunction

* nit

* Add types.py
  • Loading branch information
huchenlei committed May 22, 2024
1 parent 8508df2 commit 7718ada
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion comfy/model_patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import comfy.utils
import comfy.model_management
from comfy.types import UnetWrapperFunction


def apply_weight_decompose(dora_scale, weight):
weight_norm = (
Expand Down Expand Up @@ -117,7 +119,7 @@ def set_model_sampler_post_cfg_function(self, post_cfg_function, disable_cfg1_op
if disable_cfg1_optimization:
self.model_options["disable_cfg1_optimization"] = True

def set_model_unet_function_wrapper(self, unet_wrapper_function):
def set_model_unet_function_wrapper(self, unet_wrapper_function: UnetWrapperFunction):
self.model_options["model_function_wrapper"] = unet_wrapper_function

def set_model_denoise_mask_function(self, denoise_mask_function):
Expand Down
32 changes: 32 additions & 0 deletions comfy/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import torch
from typing import Callable, Protocol, TypedDict, Optional, List


class UnetApplyFunction(Protocol):
"""Function signature protocol on comfy.model_base.BaseModel.apply_model"""

def __call__(self, x: torch.Tensor, t: torch.Tensor, **kwargs) -> torch.Tensor:
pass


class UnetApplyConds(TypedDict):
"""Optional conditions for unet apply function."""

c_concat: Optional[torch.Tensor]
c_crossattn: Optional[torch.Tensor]
control: Optional[torch.Tensor]
transformer_options: Optional[dict]


class UnetParams(TypedDict):
# Tensor of shape [B, C, H, W]
input: torch.Tensor
# Tensor of shape [B]
timestep: torch.Tensor
c: UnetApplyConds
# List of [0, 1], [0], [1], ...
# 0 means unconditional, 1 means conditional
cond_or_uncond: List[int]


UnetWrapperFunction = Callable[[UnetApplyFunction, UnetParams], torch.Tensor]

0 comments on commit 7718ada

Please sign in to comment.