rescale values to prevent linear attention from overflowing in fp16 setting

This commit is contained in:
Phil Wang
2022-07-27 12:25:40 -07:00
parent 6b504c4ae9
commit 12f95b33d8
2 changed files with 3 additions and 1 deletions
@@ -211,6 +211,8 @@ class LinearAttention(nn.Module):
k = k.softmax(dim = -1)
q = q * self.scale
v = v / (h * w)
context = torch.einsum('b h d n, b h e n -> b h d e', k, v)
out = torch.einsum('b h d e, b h d n -> b h e n', context, q)