Skip to content

Commit efb704c

Browse files
Support attention masking in CLIP implementation.
1 parent 248d912 commit efb704c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

comfy/clip_model.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,12 @@ def __init__(self, config_dict, dtype, device, operations):
100100

101101
def forward(self, input_tokens, attention_mask=None, intermediate_output=None, final_layer_norm_intermediate=True):
102102
x = self.embeddings(input_tokens)
103-
#TODO: attention_mask
104-
x, i = self.encoder(x, intermediate_output=intermediate_output)
103+
mask = None
104+
if attention_mask is not None:
105+
mask = 1.0 - attention_mask.to(x.dtype).unsqueeze(1).unsqueeze(1).expand(attention_mask.shape[0], 1, attention_mask.shape[-1], attention_mask.shape[-1])
106+
mask = mask.masked_fill(mask.to(torch.bool), float("-inf"))
107+
108+
x, i = self.encoder(x, mask=mask, intermediate_output=intermediate_output)
105109
x = self.final_layer_norm(x)
106110
if i is not None and final_layer_norm_intermediate:
107111
i = self.final_layer_norm(i)

0 commit comments

Comments
 (0)