-
Notifications
You must be signed in to change notification settings - Fork 13
/
quant_transformer.py
290 lines (212 loc) · 11.1 KB
/
quant_transformer.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
from utils_quantization import QuantizedLinear, QuantizedLinear_cons, AveragedRangeTracker, AsymmetricQuantizer
class PositionalEncoding(nn.Module):
"Implement the PE function."
def __init__(self, d_model, dropout, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
# Compute the positional encodings once in log space.
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len).unsqueeze(1) # [max_len, 1]
div_term = torch.exp(torch.arange(0, d_model, 2) *
-(math.log(10000.0) / d_model)) #math.log(math.exp(1)) = 1
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0) #[1, max_len, d_model]
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:, :x.size(1)]
x = self.dropout(x)
return x
class MultiHeadedAttention(nn.Module):
def __init__(self, num_heads, d_model, dropout=0.1, a_bits = 8, w_bits = 8):
"Take in model size and number of heads."
super(MultiHeadedAttention, self).__init__()
assert d_model % num_heads == 0
# We assume d_v always equals d_k
self.d_k = d_model // num_heads
self.num_heads = num_heads
self.wq = QuantizedLinear(d_model, d_model,
a_bits = a_bits, w_bits = w_bits)
self.wk = QuantizedLinear(d_model, d_model,
a_bits = a_bits, w_bits = w_bits)
self.wv = QuantizedLinear(d_model, d_model,
a_bits = a_bits, w_bits = w_bits)
self.dense = QuantizedLinear(d_model, d_model,
a_bits = a_bits, w_bits = w_bits)
#self.linears = clones(nn.Linear(d_model, d_model), 4)
self.attn = None
self.dropout = nn.Dropout(p=dropout)
def attention(self, query, key, value, mask=None):
"Compute 'Scaled Dot Product Attention'"
d_k = query.size(-1)
scores = torch.matmul(query, key.transpose(-2, -1)) \
/ math.sqrt(d_k)
#print(mask.shape)
if mask is not None:
# 根据mask,指定位置填充 -1e9
scores += (mask * -1e9)
# attention weights
p_attn = F.softmax(scores, dim = -1)
return torch.matmul(p_attn, value), p_attn
def forward(self, query, key, value, mask=None):
"Implements Figure 2"
if mask is not None:
# Same mask applied to all h heads.
mask = mask.unsqueeze(1)
nbatches = query.size(0)
# 1) Do all the linear projections in batch from d_model => h x d_k
query = self.wq(query).view(nbatches, -1, self.num_heads, self.d_k)
query = query.transpose(1, 2)
key = self.wk(key).view(nbatches, -1, self.num_heads, self.d_k)
key = key.transpose(1, 2)
value = self.wv(value).view(nbatches, -1, self.num_heads, self.d_k)
value = value.transpose(1, 2)
# query, key, value = \
# [l(x).view(nbatches, -1, self.h, self.d_k).transpose(1, 2)
# for l, x in zip(self.linears, (query, key, value))]
# 2) Apply attention on all the projected vectors in batch.
x, self.attn = self.attention(query, key, value, mask=mask)
# 3) "Concat" using a view and apply a final linear.
x = x.transpose(1, 2).contiguous() \
.view(nbatches, -1, self.num_heads * self.d_k)
x = self.dense(x)
x = self.dropout(x)
return x
class PositionwiseFeedForward(nn.Module):
"Implements FFN equation."
def __init__(self, d_model, d_ff, dropout=0.1, a_bits = 8, w_bits = 8):
super(PositionwiseFeedForward, self).__init__()
self.w_1 = QuantizedLinear(d_model, d_ff,
a_bits = a_bits, w_bits = w_bits)
self.w_2 = QuantizedLinear(d_ff, d_model,
a_bits = a_bits, w_bits = w_bits)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
x = self.w_1(x)
x = F.relu(x)
x = self.w_2(x)
x = self.dropout(x)
return x
#class LayerNorm(nn.Module):
# "Construct a layernorm module (See citation for details)."
# # features = d_model
# def __init__(self, features, eps=1e-6):
# super(LayerNorm, self).__init__()
# self.a_2 = nn.Parameter(torch.ones(features))
# self.b_2 = nn.Parameter(torch.zeros(features))
# self.eps = eps
#
# def forward(self, x):
# mean = x.mean(-1, keepdim=True)
# std = x.std(-1, keepdim=True)
# return self.a_2 * (x - mean) / (std + self.eps) + self.b_2
class EncoderLayer(nn.Module):
"Encoder is made up of self-attn and feed forward (defined below)"
def __init__(self, d_model, num_heads, dff, dropout = 0.1, a_bits = 8, w_bits = 8):
super(EncoderLayer, self).__init__()
self.mha = MultiHeadedAttention(num_heads, d_model, dropout, a_bits, w_bits)
self.ffn = PositionwiseFeedForward(d_model, dff, dropout, a_bits, w_bits)
self.layernorm1 = nn.LayerNorm(d_model, eps=1e-6)
self.layernorm2 = nn.LayerNorm(d_model, eps=1e-6)
def forward(self, x, mask):
"Follow Figure 1 (left) for connections."
attn_output = self.mha(x, x, x, mask)
x = self.layernorm1(x + attn_output)
ffn_output = self.ffn(x)
x = self.layernorm2(x + ffn_output)
return x
class DecoderLayer(nn.Module):
"Decoder is made of self-attn, src-attn, and feed forward (defined below)"
def __init__(self, d_model, num_heads, dff, dropout, a_bits, w_bits):
super(DecoderLayer, self).__init__()
self.self_mha = MultiHeadedAttention(num_heads, d_model, dropout, a_bits, w_bits)
self.src_mha = MultiHeadedAttention(num_heads, d_model, dropout, a_bits, w_bits)
self.ffn = PositionwiseFeedForward(d_model, dff, dropout, a_bits, w_bits)
self.layernorm1 = nn.LayerNorm(d_model, eps=1e-6)
self.layernorm2 = nn.LayerNorm(d_model, eps=1e-6)
self.layernorm3 = nn.LayerNorm(d_model, eps=1e-6)
#self.sublayer = clones(SublayerConnection(size, dropout), 3)
def forward(self, x, memory, look_ahead_mask, trg_padding_mask):
"Follow Figure 1 (right) for connections."
#m = memory
attn_output = self.self_mha(x, x, x, look_ahead_mask)
x = self.layernorm1(x + attn_output)
src_output = self.src_mha(x, memory, memory, trg_padding_mask) # q, k, v
x = self.layernorm2(x + src_output)
fnn_output = self.ffn(x)
x = self.layernorm3(x + fnn_output)
return x
class Encoder(nn.Module):
"Core encoder is a stack of N layers"
def __init__(self, num_layers, src_vocab_size, max_len, d_model,
num_heads, dff, dropout = 0.1, a_bits = 8, w_bits = 8):
super(Encoder, self).__init__()
self.d_model = d_model
self.embedding = nn.Embedding(src_vocab_size, d_model)
self.pos_encoding = PositionalEncoding(d_model, dropout, max_len)
self.enc_layers = nn.ModuleList([EncoderLayer(d_model, num_heads, dff, dropout, a_bits, w_bits)
for _ in range(num_layers)])
def forward(self, x, src_mask):
"Pass the input (and mask) through each layer in turn."
# the input size of x is [batch_size, seq_len]
x = self.embedding(x) * math.sqrt(self.d_model)
x = self.pos_encoding(x)
for enc_layer in self.enc_layers:
x = enc_layer(x, src_mask)
return x
class Decoder(nn.Module):
def __init__(self, num_layers, trg_vocab_size, max_len, d_model,
num_heads, dff, dropout = 0.1, a_bits = 8, w_bits = 8):
super(Decoder, self).__init__()
self.d_model = d_model
self.embedding = nn.Embedding(trg_vocab_size, d_model)
self.pos_encoding = PositionalEncoding(d_model, dropout, max_len)
self.dec_layers = nn.ModuleList([DecoderLayer(d_model, num_heads, dff, dropout, a_bits, w_bits)
for _ in range(num_layers)])
def forward(self, x, memory, look_ahead_mask, trg_padding_mask):
x = self.embedding(x) * math.sqrt(self.d_model)
x = self.pos_encoding(x)
for dec_layer in self.dec_layers:
x = dec_layer(x, memory, look_ahead_mask, trg_padding_mask)
return x
class ChannelDecoder(nn.Module):
def __init__(self, in_features, size1, size2, a_bits, w_bits):
super(ChannelDecoder, self).__init__()
self.linear1 = QuantizedLinear_cons(in_features, size1,
a_bits = a_bits, w_bits = w_bits)
self.linear2 = QuantizedLinear(size1, size2,
a_bits = a_bits, w_bits = w_bits)
self.linear3 = QuantizedLinear(size2, size1,
a_bits = a_bits, w_bits = w_bits)
# self.linear4 = nn.Linear(size1, d_model)
self.layernorm = nn.LayerNorm(size1, eps=1e-6)
def forward(self, x):
x1 = self.linear1(x)
x2 = F.relu(x1)
x3 = self.linear2(x2)
x4 = F.relu(x3)
x5 = self.linear3(x4)
output = self.layernorm(x1 + x5)
# output = self.linear4(output)
return output
class QuantTransformer(nn.Module):
def __init__(self, num_layers, src_vocab_size, trg_vocab_size, src_max_len,
trg_max_len, d_model, num_heads, dff, dropout = 0.1, a_bits = 8, w_bits = 8):
super(QuantTransformer, self).__init__()
self.encoder = Encoder(num_layers, src_vocab_size, src_max_len,
d_model, num_heads, dff, dropout, a_bits, w_bits)
self.channel_encoder = nn.Sequential(QuantizedLinear(d_model, 256,
a_bits = a_bits, w_bits = w_bits),
nn.ReLU(inplace = True),
QuantizedLinear(256, 16,
a_bits = a_bits, w_bits = w_bits))
self.quant_constellation = AsymmetricQuantizer(bits = 4,
range_tracker = AveragedRangeTracker(q_level='L'))
self.channel_decoder = ChannelDecoder(16, d_model, 512, a_bits = a_bits, w_bits = w_bits)
self.decoder = Decoder(num_layers, trg_vocab_size, trg_max_len,
d_model, num_heads, dff, dropout, a_bits, w_bits)
self.dense = QuantizedLinear(d_model, trg_vocab_size, a_bits = a_bits, w_bits = w_bits)