1919import torch .nn .functional as F
2020from torch import nn
2121
22+ from ..image_processor import IPAdapterMaskProcessor
2223from ..utils import USE_PEFT_BACKEND , deprecate , logging
2324from ..utils .import_utils import is_xformers_available
2425from ..utils .torch_utils import maybe_allow_in_graph
@@ -2107,12 +2108,13 @@ def __init__(self, hidden_size, cross_attention_dim=None, num_tokens=(4,), scale
21072108
21082109 def __call__ (
21092110 self ,
2110- attn ,
2111- hidden_states ,
2112- encoder_hidden_states = None ,
2113- attention_mask = None ,
2114- temb = None ,
2115- scale = 1.0 ,
2111+ attn : Attention ,
2112+ hidden_states : torch .FloatTensor ,
2113+ encoder_hidden_states : Optional [torch .FloatTensor ] = None ,
2114+ attention_mask : Optional [torch .FloatTensor ] = None ,
2115+ temb : Optional [torch .FloatTensor ] = None ,
2116+ scale : float = 1.0 ,
2117+ ip_adapter_masks : Optional [torch .FloatTensor ] = None ,
21162118 ):
21172119 residual = hidden_states
21182120
@@ -2167,9 +2169,22 @@ def __call__(
21672169 hidden_states = torch .bmm (attention_probs , value )
21682170 hidden_states = attn .batch_to_head_dim (hidden_states )
21692171
2172+ if ip_adapter_masks is not None :
2173+ if not isinstance (ip_adapter_masks , torch .Tensor ) or ip_adapter_masks .ndim != 4 :
2174+ raise ValueError (
2175+ " ip_adapter_mask should be a tensor with shape [num_ip_adapter, 1, height, width]."
2176+ " Please use `IPAdapterMaskProcessor` to preprocess your mask"
2177+ )
2178+ if len (ip_adapter_masks ) != len (self .scale ):
2179+ raise ValueError (
2180+ f"Number of ip_adapter_masks ({ len (ip_adapter_masks )} ) must match number of IP-Adapters ({ len (self .scale )} )"
2181+ )
2182+ else :
2183+ ip_adapter_masks = [None ] * len (self .scale )
2184+
21702185 # for ip-adapter
2171- for current_ip_hidden_states , scale , to_k_ip , to_v_ip in zip (
2172- ip_hidden_states , self .scale , self .to_k_ip , self .to_v_ip
2186+ for current_ip_hidden_states , scale , to_k_ip , to_v_ip , mask in zip (
2187+ ip_hidden_states , self .scale , self .to_k_ip , self .to_v_ip , ip_adapter_masks
21732188 ):
21742189 ip_key = to_k_ip (current_ip_hidden_states )
21752190 ip_value = to_v_ip (current_ip_hidden_states )
@@ -2181,6 +2196,15 @@ def __call__(
21812196 current_ip_hidden_states = torch .bmm (ip_attention_probs , ip_value )
21822197 current_ip_hidden_states = attn .batch_to_head_dim (current_ip_hidden_states )
21832198
2199+ if mask is not None :
2200+ mask_downsample = IPAdapterMaskProcessor .downsample (
2201+ mask , batch_size , current_ip_hidden_states .shape [1 ], current_ip_hidden_states .shape [2 ]
2202+ )
2203+
2204+ mask_downsample = mask_downsample .to (dtype = query .dtype , device = query .device )
2205+
2206+ current_ip_hidden_states = current_ip_hidden_states * mask_downsample
2207+
21842208 hidden_states = hidden_states + scale * current_ip_hidden_states
21852209
21862210 # linear proj
@@ -2244,12 +2268,13 @@ def __init__(self, hidden_size, cross_attention_dim=None, num_tokens=(4,), scale
22442268
22452269 def __call__ (
22462270 self ,
2247- attn ,
2248- hidden_states ,
2249- encoder_hidden_states = None ,
2250- attention_mask = None ,
2251- temb = None ,
2252- scale = 1.0 ,
2271+ attn : Attention ,
2272+ hidden_states : torch .FloatTensor ,
2273+ encoder_hidden_states : Optional [torch .FloatTensor ] = None ,
2274+ attention_mask : Optional [torch .FloatTensor ] = None ,
2275+ temb : Optional [torch .FloatTensor ] = None ,
2276+ scale : float = 1.0 ,
2277+ ip_adapter_masks : Optional [torch .FloatTensor ] = None ,
22532278 ):
22542279 residual = hidden_states
22552280
@@ -2318,9 +2343,22 @@ def __call__(
23182343 hidden_states = hidden_states .transpose (1 , 2 ).reshape (batch_size , - 1 , attn .heads * head_dim )
23192344 hidden_states = hidden_states .to (query .dtype )
23202345
2346+ if ip_adapter_masks is not None :
2347+ if not isinstance (ip_adapter_masks , torch .Tensor ) or ip_adapter_masks .ndim != 4 :
2348+ raise ValueError (
2349+ " ip_adapter_mask should be a tensor with shape [num_ip_adapter, 1, height, width]."
2350+ " Please use `IPAdapterMaskProcessor` to preprocess your mask"
2351+ )
2352+ if len (ip_adapter_masks ) != len (self .scale ):
2353+ raise ValueError (
2354+ f"Number of ip_adapter_masks ({ len (ip_adapter_masks )} ) must match number of IP-Adapters ({ len (self .scale )} )"
2355+ )
2356+ else :
2357+ ip_adapter_masks = [None ] * len (self .scale )
2358+
23212359 # for ip-adapter
2322- for current_ip_hidden_states , scale , to_k_ip , to_v_ip in zip (
2323- ip_hidden_states , self .scale , self .to_k_ip , self .to_v_ip
2360+ for current_ip_hidden_states , scale , to_k_ip , to_v_ip , mask in zip (
2361+ ip_hidden_states , self .scale , self .to_k_ip , self .to_v_ip , ip_adapter_masks
23242362 ):
23252363 ip_key = to_k_ip (current_ip_hidden_states )
23262364 ip_value = to_v_ip (current_ip_hidden_states )
@@ -2339,6 +2377,15 @@ def __call__(
23392377 )
23402378 current_ip_hidden_states = current_ip_hidden_states .to (query .dtype )
23412379
2380+ if mask is not None :
2381+ mask_downsample = IPAdapterMaskProcessor .downsample (
2382+ mask , batch_size , current_ip_hidden_states .shape [1 ], current_ip_hidden_states .shape [2 ]
2383+ )
2384+
2385+ mask_downsample = mask_downsample .to (dtype = query .dtype , device = query .device )
2386+
2387+ current_ip_hidden_states = current_ip_hidden_states * mask_downsample
2388+
23422389 hidden_states = hidden_states + scale * current_ip_hidden_states
23432390
23442391 # linear proj
0 commit comments