-
Notifications
You must be signed in to change notification settings - Fork 14
/
snippets_pytorch.py
140 lines (95 loc) · 3.64 KB
/
snippets_pytorch.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import torch
import torch.nn.functional as F
import sys
sys.path.append('../')
from tsalib import dim_vars as dvs, get_dim_vars
from tsalib import permute_transform as pt, warp, dot, alignto
from tsalib import reduce_dims as rd
B, H, T, D = dvs('Batch(b):4 H(h):7 T(t):100 D(d):300')
# `merge_heads` function in Transformer network (original)
def merge_heads_old(x: (B,H,T,D)):
x = x.permute(0, 2, 1, 3).contiguous()
new_x_shape = x.size()[:-2] + (x.size(-2) * x.size(-1),)
res = x.view(*new_x_shape)
return res
# `merge_heads` using tsalib (weaker integration)
def merge_heads1(x: (B,H,T,D)):
x: (B,T,H,D) = x.permute(pt('bhtd -> bthd')).contiguous()
res: (B,T,H*D) = x.view((B,T,H*D))
return res
# 'merge_heads' using tsalib's warp (deeper integration)
from tsalib import warp
def merge_heads2(x: (B,H,T,D)):
res: (B,T,H*D) = warp(x, 'bhtd -> bthd -> b,t,h*d', 'pcv', debug=False)
return res
def test_merge_heads():
x = torch.randn( (B,H,T,D) )
y = merge_heads_old(x)
assert y.size() == (B,T,H*D)
y = merge_heads1(x)
assert y.size() == (B,T,H*D)
y = merge_heads2(x)
assert y.size() == (B,T,H*D)
print ('all merge_heads assertions hold')
'''
Einsum attention
Originally from https://rockt.github.io/2018/04/30/einsum
Revisited at http://nlp.seas.harvard.edu/NamedTensor
'''
def random_tensors(shape, num=1):
tensors = [torch.randn(shape) for i in range(0, num)]
return tensors[0] if num == 1 else tensors
def make_params (l):
bM, br, w = random_tensors([l], num=3)
# -- [hidden_dimension x hidden_dimension]
WY, Wh, Wr, Wt = random_tensors([l, l], num=4)
return (bM, br, w), (WY, Wh, Wr, Wt)
def einsum_attn(Y, ht, rt1):
(bM, br, w), (WY, Wh, Wr, Wt) = make_params(7)
# -- [batch_size x hidden_dimension]
tmp = torch.einsum("ik,kl->il", [ht, Wh]) + \
torch.einsum("ik,kl->il", [rt1, Wr])
Mt = torch.tanh(torch.einsum("ijk,kl->ijl", [Y, WY]) + \
tmp.unsqueeze(1).expand_as(Y) + bM)
# -- [batch_size x sequence_length]
at = F.softmax(torch.einsum("ijk,k->ij", [Mt, w]), dim=-1)
# -- [batch_size x hidden_dimension]
rt = torch.einsum("ijk,ij->ik", [Y, at]) + \
torch.tanh(torch.einsum("ij,jk->ik", [rt1, Wt]) +
br)
# -- [batch_size x hidden_dimension], [batch_size x sequence_dimension]
return rt, at
def test_einsum_attn():
# -- [batch_size x sequence_length x hidden_dimension]
Y = random_tensors([3, 5, 7])
# -- [batch_size x hidden_dimension]
ht, rt1 = random_tensors([3, 7], num=2)
rt, at = einsum_attn(Y, ht, rt1)
assert rt.size() == (3, 7) and at.size() == (3, 5)
print ('einsum attn: assertions hold')
'''
========= With tsalib =====
'''
def tsa_attn(Y, ht, rt1):
B, L, D = get_dim_vars('b l d')
Y: 'bld' ; ht: 'b,d'; rt1: 'b,d'
#bM, br, w: 'd,'
#WY, Wh, Wr, Wt: 'd,d'
(bM, br, w), (WY, Wh, Wr, Wt) = make_params(D)
tmp: 'bd' = dot('_d.d_', ht, Wh) + dot('_d.d_', rt1, Wr)
tmpa: 'bld' = alignto((tmp,'bd'), 'bld')
Mt: 'bld' = torch.tanh(dot('__d.d_', Y, WY) + tmpa + bM)
at: 'bl' = F.softmax(dot('__d.d', Mt, w), dim=-1)
rt: 'bd' = dot('bld,bl->bd', Y, at) + torch.tanh(dot('_d.d_', rt1, Wt) + br)
return rt, at
def test_tsa_attn():
B, L, D = dvs('Batch(b):3, sequence_length(l):5 hidden_dimension(d):300', exists_ok=True)
Y = random_tensors([B, L, D])
ht, rt1 = random_tensors([B, D], num=2)
rt, at = tsa_attn(Y, ht, rt1)
assert rt.size() == (B, D) and at.size() == (B, L)
print ('tsa attn: assertions hold')
if __name__ == '__main__':
test_merge_heads()
#test_einsum_attn()
test_tsa_attn()