-
Notifications
You must be signed in to change notification settings - Fork 186
/
attention.py
30 lines (24 loc) · 975 Bytes
/
attention.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
""" attention functions """
from torch.nn import functional as F
def dot_attention_score(key, query):
"""[B, Tk, D], [(Bs), B, Tq, D] -> [(Bs), B, Tq, Tk]"""
return query.matmul(key.transpose(1, 2))
def prob_normalize(score, mask):
""" [(...), T]
user should handle mask shape"""
score = score.masked_fill(mask == 0, -1e18)
norm_score = F.softmax(score, dim=-1)
return norm_score
def attention_aggregate(value, score):
"""[B, Tv, D], [(Bs), B, Tq, Tv] -> [(Bs), B, Tq, D]"""
output = score.matmul(value)
return output
def step_attention(query, key, value, mem_mask=None):
""" query[(Bs), B, D], key[B, T, D], value[B, T, D]"""
score = dot_attention_score(key, query.unsqueeze(-2))
if mem_mask is None:
norm_score = F.softmax(score, dim=-1)
else:
norm_score = prob_normalize(score, mem_mask)
output = attention_aggregate(value, norm_score)
return output.squeeze(-2), norm_score.squeeze(-2)