-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformer_modules.py
240 lines (198 loc) · 8.13 KB
/
transformer_modules.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import math
import numpy as np
from torch import nn
import torch
import torchvision
import torch.nn.functional as F
from einops import rearrange, repeat
from einops.layers.torch import Rearrange
###############################################################################
#
# Building blocks for transformers
#
###############################################################################
class Norm(nn.Module):
def __init__(self, dim):
super().__init__()
self.norm = nn.LayerNorm(dim)
def forward(self, x):
return self.norm(x)
class Attention(nn.Module):
def __init__(self, dim, num_heads=8, head_output_size=64, dropout=0.0):
super().__init__()
self.num_heads = num_heads
# \sqrt{d_{k}}
self.att_scale = head_output_size ** (-0.5)
self.qkv = nn.Linear(dim, num_heads * head_output_size * 3, bias=False)
# We need to combine the output from all heads
self.output_layer = nn.Sequential(
nn.Linear(num_heads * head_output_size, dim), nn.Dropout(dropout)
)
def forward(self, x, mask=None):
B, N, C = x.shape
qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4)
q, k, v = (qkv[0], qkv[1], qkv[2])
# q.dot(k.transpose)
attn = (q @ k.transpose(-2, -1)) * self.att_scale
if mask is not None:
mask = mask.bool()
if len(mask.shape) == 2: # (B, N)
attn = attn.masked_fill(~mask[:, None, None, :], float("-inf"))
elif len(mask.shape) == 3 and mask.shape[0] == 1: # (1, N, N)
attn = attn.masked_fill(~mask[None, :, :, :], float("-inf"))
elif (
len(mask.shape) == 3
): # Consider the case where each batch has different causal mask, typically useful for MAE implementation
attn = attn.masked_fill(
~mask[:, None, :, :].repeat(1, self.num_heads, 1, 1), float("-inf")
)
else:
raise Exception("mask shape is not correct for attention")
attn = attn.softmax(dim=-1)
self.att_weights = attn
# (..., num_heads, seq_len, head_output_size)
out = rearrange(torch.matmul(attn, v), "b h n d -> b n (h d)")
return self.output_layer(out)
class TransformerFeedForwardNN(nn.Module):
def __init__(self, dim, hidden_dim, dropout=0.0):
super().__init__()
# Remember the residual connection
layers = [
nn.Linear(dim, hidden_dim),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, dim),
nn.Dropout(dropout),
]
self.net = nn.Sequential(*layers)
def forward(self, x):
return self.net(x)
def drop_path(
x, drop_prob: float = 0.0, training: bool = False, scale_by_keep: bool = True
):
"""Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
This is the same as the DropConnect impl I created for EfficientNet, etc networks, however,
the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for
changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use
'survival rate' as the argument.
"""
if drop_prob == 0.0 or not training:
return x
keep_prob = 1 - drop_prob
shape = (x.shape[0],) + (1,) * (
x.ndim - 1
) # work with diff dim tensors, not just 2D ConvNets
random_tensor = x.new_empty(shape).bernoulli_(keep_prob)
if keep_prob > 0.0 and scale_by_keep:
random_tensor.div_(keep_prob)
return x * random_tensor
class DropPath(nn.Module):
"""Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks)."""
def __init__(self, drop_prob=None, scale_by_keep=True):
super(DropPath, self).__init__()
self.drop_prob = drop_prob
self.scale_by_keep = scale_by_keep
def forward(self, x):
return drop_path(x, self.drop_prob, self.training, self.scale_by_keep)
class SinusoidalPositionEncoding(nn.Module):
def __init__(self, input_size, inv_freq_factor=10, factor_ratio=None):
super().__init__()
self.input_size = input_size
self.inv_freq_factor = inv_freq_factor
channels = self.input_size
channels = int(np.ceil(channels / 2) * 2)
inv_freq = 1.0 / (
self.inv_freq_factor ** (torch.arange(0, channels, 2).float() / channels)
)
self.channels = channels
self.register_buffer("inv_freq", inv_freq)
if factor_ratio is None:
self.factor = 1.0
else:
factor = nn.Parameter(torch.ones(1) * factor_ratio)
self.register_parameter("factor", factor)
def forward(self, x):
pos_x = torch.arange(x.shape[1], device=x.device).type(self.inv_freq.type())
sin_inp_x = torch.einsum("i,j->ij", pos_x, self.inv_freq)
emb_x = torch.cat((sin_inp_x.sin(), sin_inp_x.cos()), dim=-1)
return emb_x * self.factor
def output_shape(self, input_shape):
return input_shape
def output_size(self, input_size):
return input_size
###############################################################################
#
# Transformer Decoder (we only use transformer decoder for our policies)
#
###############################################################################
class TransformerDecoder(nn.Module):
def __init__(
self,
input_size,
num_layers,
num_heads,
head_output_size,
mlp_hidden_size,
dropout,
**kwargs
):
super().__init__()
self.layers = nn.ModuleList([])
self.drop_path = DropPath(dropout) if dropout > 0.0 else nn.Identity()
self.attention_output = {}
for _ in range(num_layers):
self.layers.append(
nn.ModuleList(
[
Norm(input_size),
Attention(
input_size,
num_heads=num_heads,
head_output_size=head_output_size,
dropout=dropout,
),
Norm(input_size),
TransformerFeedForwardNN(
input_size, mlp_hidden_size, dropout=dropout
),
]
)
)
self.attention_output[_] = None
self.seq_len = None
self.num_elements = None
self.mask = None
def compute_mask(self, input_shape):
# input_shape = (:, seq_len, num_elements)
if (
(self.num_elements is None)
or (self.seq_len is None)
or (self.num_elements != input_shape[2])
or (self.seq_len != input_shape[1])
):
self.seq_len = input_shape[1]
self.num_elements = input_shape[2]
self.original_mask = (
torch.triu(torch.ones(self.seq_len, self.seq_len))
- torch.eye(self.seq_len, self.seq_len)
).to(self.device)
self.mask = 1 - self.original_mask.repeat_interleave(
self.num_elements, dim=-1
).repeat_interleave(self.num_elements, dim=-2).unsqueeze(0)
# (1, N, N), N = seq_len * num_elements
def forward(self, x, mask=None):
for layer_idx, (att_norm, att, ff_norm, ff) in enumerate(self.layers):
if mask is not None:
x = x + drop_path(att(att_norm(x), mask))
elif self.mask is not None:
x = x + drop_path(att(att_norm(x), self.mask))
else: # no masking, just use full attention
x = x + drop_path(att(att_norm(x)))
if not self.training:
self.attention_output[layer_idx] = att.att_weights
x = x + self.drop_path(ff(ff_norm(x)))
return x
@property
def device(self):
return next(self.parameters()).device