Skip to content

Commit 7718ada

Browse files
authored
Add type annotation UnetWrapperFunction (comfyanonymous#3531)
* Add type annotation UnetWrapperFunction * nit * Add types.py
1 parent 8508df2 commit 7718ada

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

comfy/model_patcher.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import comfy.utils
88
import comfy.model_management
9+
from comfy.types import UnetWrapperFunction
10+
911

1012
def apply_weight_decompose(dora_scale, weight):
1113
weight_norm = (
@@ -117,7 +119,7 @@ def set_model_sampler_post_cfg_function(self, post_cfg_function, disable_cfg1_op
117119
if disable_cfg1_optimization:
118120
self.model_options["disable_cfg1_optimization"] = True
119121

120-
def set_model_unet_function_wrapper(self, unet_wrapper_function):
122+
def set_model_unet_function_wrapper(self, unet_wrapper_function: UnetWrapperFunction):
121123
self.model_options["model_function_wrapper"] = unet_wrapper_function
122124

123125
def set_model_denoise_mask_function(self, denoise_mask_function):

comfy/types.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import torch
2+
from typing import Callable, Protocol, TypedDict, Optional, List
3+
4+
5+
class UnetApplyFunction(Protocol):
6+
"""Function signature protocol on comfy.model_base.BaseModel.apply_model"""
7+
8+
def __call__(self, x: torch.Tensor, t: torch.Tensor, **kwargs) -> torch.Tensor:
9+
pass
10+
11+
12+
class UnetApplyConds(TypedDict):
13+
"""Optional conditions for unet apply function."""
14+
15+
c_concat: Optional[torch.Tensor]
16+
c_crossattn: Optional[torch.Tensor]
17+
control: Optional[torch.Tensor]
18+
transformer_options: Optional[dict]
19+
20+
21+
class UnetParams(TypedDict):
22+
# Tensor of shape [B, C, H, W]
23+
input: torch.Tensor
24+
# Tensor of shape [B]
25+
timestep: torch.Tensor
26+
c: UnetApplyConds
27+
# List of [0, 1], [0], [1], ...
28+
# 0 means unconditional, 1 means conditional
29+
cond_or_uncond: List[int]
30+
31+
32+
UnetWrapperFunction = Callable[[UnetApplyFunction, UnetParams], torch.Tensor]

0 commit comments

Comments
 (0)