* Fix SDP attention mask by creating a diagonal mask in the right shape.

This commit is contained in:
gchapman
2024-05-16 09:23:16 +01:00
parent 1e5dc8e10d
commit 0f888945f5
+12 -6
View File
@@ -70,14 +70,20 @@ class MultiHeadCrossAttention(nn.Module):
q, k, v = map(lambda t: t.permute(0, 2, 1, 3),(q, k, v),) q, k, v = map(lambda t: t.permute(0, 2, 1, 3),(q, k, v),)
attn_mask = None attn_mask = None
if mask is not None and len(mask) > 1: if mask is not None and len(mask) > 1:
# This is most definitely wrong, especially for B>1
attn_mask = torch.zeros( # Create equivalent of xformer diagonal block mask, still only correct for square masks
[1, q.shape[1], q.shape[2], v.shape[2]], # But depth doesn't matter as tensors can expand in that dimension
dtype=q.dtype, attn_mask_template = torch.ones(
[q.shape[2] // B, mask[0]],
dtype=torch.bool,
device=q.device device=q.device
) )
attn_mask[:, :, (q.shape[2]//2):, mask[0]:] = True attn_mask = torch.block_diag(attn_mask_template)
attn_mask[:, :, :(q.shape[2]//2), :mask[1]] = True
# create a mask on the diagonal for each mask in the batch
for n in range(B - 1):
attn_mask = torch.block_diag(attn_mask, attn_mask_template)
x = torch.nn.functional.scaled_dot_product_attention( x = torch.nn.functional.scaled_dot_product_attention(
q, k, v, q, k, v,
attn_mask=attn_mask, attn_mask=attn_mask,