mem_transformer.py 49.2 KB
Newer Older
Zhilin Yang's avatar
init  
Zhilin Yang committed
1
2
3
4
5
6
7
8
9
import sys
import math
import functools

import numpy as np

import torch
import torch.nn as nn
import torch.nn.functional as F
Jiezhong Qiu's avatar
Jiezhong Qiu committed
10
#  import torch_sparse
Zhilin Yang's avatar
init  
Zhilin Yang committed
11

12
13
from cuda.moe import MOELayer

Zhilin Yang's avatar
init  
Zhilin Yang committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
sys.path.append('utils')
from proj_adaptive_softmax import ProjectedAdaptiveLogSoftmax
from log_uniform_sampler import LogUniformSampler, sample_logits

class PositionalEmbedding(nn.Module):
    def __init__(self, demb):
        super(PositionalEmbedding, self).__init__()

        self.demb = demb

        inv_freq = 1 / (10000 ** (torch.arange(0.0, demb, 2.0) / demb))
        self.register_buffer('inv_freq', inv_freq)

    def forward(self, pos_seq, bsz=None):
        sinusoid_inp = torch.ger(pos_seq, self.inv_freq)
        pos_emb = torch.cat([sinusoid_inp.sin(), sinusoid_inp.cos()], dim=-1)

        if bsz is not None:
            return pos_emb[:,None,:].expand(-1, bsz, -1)
        else:
            return pos_emb[:,None,:]

36
37
38
39
40
41
42
43
44
45
46
47
class CustomizedMoEPositionwiseFF(nn.Module):
    def __init__(self, d_model, d_inner, dropout, pre_lnorm=False, top_k=2, num_expert=4):
        super(CustomizedMoEPositionwiseFF, self).__init__()
        print("CustomizedMoEPositionwiseFF num_expert=%d top_k=%d" % (num_expert, top_k))

        self.top_k = top_k
        assert num_expert >= top_k
        
        self.d_model = d_model
        self.d_inner = d_inner
        self.dropout = dropout

48
        self.gate = nn.Linear(d_model, num_expert)
49

Jiezhong Qiu's avatar
Jiezhong Qiu committed
50
51
        self.moe1 = MOELayer(num_expert=num_expert, in_feat=d_model+1, out_feat=d_inner)
        self.moe2 = MOELayer(num_expert=num_expert, in_feat=d_inner+1, out_feat=d_model)
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

        self.layer_norm = nn.LayerNorm(d_model)

        self.pre_lnorm = pre_lnorm

        self.dropout = nn.Dropout(dropout)

        self.reset_parameter()

    def reset_parameter(self):
        pass

    def forward(self, inp):
        residual = inp
        if self.pre_lnorm:
            inp = self.layer_norm(inp)

        gate = self.gate(inp)
        gate_top_k_val, gate_top_k_idx = torch.topk(gate, k=self.top_k, dim=-1, largest=True, sorted=False) # [.. x top_k]
        
        gate_top_k_val = gate_top_k_val.view(-1, self.top_k)
        gate_score = F.softmax(gate_top_k_val, dim=-1).unsqueeze(1) # (BxL) x 1 x top_k 
        gate_top_k_idx = gate_top_k_idx.view(-1, self.top_k)

        core_out = []

        inp = inp.view(-1, self.d_model)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
79
        inp = F.pad(inp, pad=(0, 1), mode='constant', value=1.0)
80
81
82
83
84

        for i in range(self.top_k):
            gate_idx = gate_top_k_idx[:, i].contiguous()
            x = self.moe1(inp, gate_idx)
            x = self.dropout(F.relu(x))
Jiezhong Qiu's avatar
Jiezhong Qiu committed
85
            x = F.pad(x, pad=(0, 1), mode='constant', value=1.0)
86
87
            x = self.moe2(x, gate_idx)
            x = self.dropout(x) # (BxL) x d_model
Jiezhong Qiu's avatar
fix bug  
Jiezhong Qiu committed
88
            core_out.append(x.unsqueeze(1)) # (BxL) x 1 x d_model
89
90
91
92
93
94
95
96
97
98
99
100
101
        
        core_out = torch.cat(core_out, dim=1) # (BxL) x top_k x d_model 
        core_out = torch.bmm(gate_score, core_out) # (BxL) x 1 x d_model
        core_out = core_out.view(residual.size(0), residual.size(1), self.d_model)

        output = core_out + residual
        if not self.pre_lnorm:
            output = self.layer_norm(output)

        return output


class MoEPositionwiseFFRaw(nn.Module):
Jiezhong Qiu's avatar
Jiezhong Qiu committed
102
    def __init__(self, d_model, d_inner, dropout, pre_lnorm=False, top_k=64):
103
        super(MoEPositionwiseFFRaw, self).__init__()
Jiezhong Qiu's avatar
Jiezhong Qiu committed
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
        print("MoEPositionwiseFF")

        self.top_k = top_k
        self.d_model = d_model
        self.d_inner = d_inner
        self.dropout = dropout

        self.gate = nn.Linear(d_model, d_inner)

        self.W2 = nn.Parameter(torch.Tensor(d_inner, d_model))
        self.b2 = nn.Parameter(torch.Tensor(d_model))

        self.layer_norm = nn.LayerNorm(d_model)

        self.pre_lnorm = pre_lnorm

        ratio = top_k / d_inner
        self.dropout_middle = nn.Dropout(dropout * ratio)
        self.dropout_final = nn.Dropout(dropout)

        self.reset_parameter()

    def reset_parameter(self):
        temp_Linear = nn.Linear(self.d_inner, self.d_model)
        self.W2.data = temp_Linear.weight.data.transpose(0, 1)
        self.b2.data = temp_Linear.bias.data

    def forward(self, inp):
        residual = inp
        if self.pre_lnorm:
            inp = self.layer_norm(inp)

        gate = self.gate(inp)
        gate_top_k_val, gate_top_k_idx = torch.topk(gate, k=self.top_k, dim=-1, largest=True, sorted=False) # [.. x top_k]
        relu_out = F.relu(gate_top_k_val)

        x = self.dropout_middle(relu_out)

        W2_select = self.W2[gate_top_k_idx] # [.. x top_k x d_model]
        core_out = torch.einsum('ijk,ijkd->ijd', (x, W2_select)) + self.b2 # [.. x d_model]
        core_out = self.dropout_final(core_out)

        output = core_out + residual
        if not self.pre_lnorm:
            output = self.layer_norm(output)

        return output
        #  return output, relu_out.detach()

Jiezhong Qiu's avatar
Jiezhong Qiu committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
def my_topk(x, k, inplace=True):
    y = x if inplace else x.clone()
    top1_val, top1_idx = torch.max(y, dim=-1)
    top1_val = top1_val.unsqueeze(-1)
    top1_idx = top1_idx.unsqueeze(-1)
    if k == 1:
        return top1_val, top1_idx
    y.scatter_(-1, top1_idx, value=float('-inf'))
    top2_val, top2_idx = torch.max(y, dim=-1)
    top2_val = top2_val.unsqueeze(-1)
    top2_idx = top2_idx.unsqueeze(-1)

    top_val = torch.cat((top1_val, top2_val), dim=-1)
    top_idx = torch.cat((top1_idx, top2_idx), dim=-1)
    return top_val, top_idx

Jiezhong Qiu's avatar
Jiezhong Qiu committed
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
class MultiHeadHierarchicalMoEPositionwiseFF(nn.Module):
    def __init__(self, d_model, d_inner, dropout, pre_lnorm=False, n_block=16, top_block=2):
        super(MultiHeadHierarchicalMoEPositionwiseFF, self).__init__()
        print("MultiHeadHierarchicalMoEPositionwiseFF")

        assert d_inner % n_block == 0
        assert top_block in [1, 2]
        self.top_block = top_block
        self.n_block = n_block

        d_block = d_inner // n_block
        self.d_block = d_block
        self.d_model = d_model
        self.d_inner = d_inner
        self.dropout = dropout

        self.block_net_W = nn.Parameter(torch.Tensor(d_model, top_block, n_block))
        self.block_net_b = nn.Parameter(torch.Tensor(top_block, n_block))

        self.W1 = nn.Parameter(torch.Tensor(n_block, d_block, d_model))
        self.b1 = nn.Parameter(torch.Tensor(n_block, d_block))

        self.W2 = nn.Parameter(torch.Tensor(n_block, d_block, d_model))
        self.b2 = nn.Parameter(torch.Tensor(d_model))

        self.layer_norm = nn.LayerNorm(d_model)

        self.pre_lnorm = pre_lnorm

        ratio = top_block / n_block
        self.dropout_middle = nn.Dropout(dropout * ratio)
        self.dropout_final = nn.Dropout(dropout)

        #  self.scale = 1 / (d_model ** 0.5)
        self.reset_parameter()

    def reset_parameter(self):
        temp = nn.Linear(self.d_model, self.d_inner)
        self.W1.data = temp.weight.data.view(self.n_block, self.d_block, self.d_model)
        self.b1.data = temp.bias.data.view(self.n_block, self.d_block)
        temp = nn.Linear(self.d_inner, self.d_model)
        self.W2.data = temp.weight.data.transpose(0, 1).contiguous().view(self.n_block, self.d_block, self.d_model)
        self.b2.data = temp.bias.data
        for i in range(self.top_block):
            temp = nn.Linear(self.d_model, self.n_block)
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
214
215
            self.block_net_W.data[:, i] = temp.weight.data.transpose(0, 1).contiguous()
            self.block_net_b.data[i] = temp.bias.data
Jiezhong Qiu's avatar
Jiezhong Qiu committed
216
217
218
219
220
221
222
223

    def forward(self, inp):
        residual = inp
        if self.pre_lnorm:
            inp = self.layer_norm(inp)

        block = torch.einsum("ibd,dan->iban", (inp, self.block_net_W)) + self.block_net_b # [.. x top_block x n_block ]

Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
224
225
        block_val, block_idx = my_topk(block, k=1, inplace=True)
        # block_val, block_idx = torch.topk(block, k=1, dim=-1, largest=True, sorted=False) # [.. x top_k x 1]
Jiezhong Qiu's avatar
Jiezhong Qiu committed
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
        block_val = block_val.squeeze(-1)
        block_idx = block_idx.squeeze(-1)

        gate = F.softmax(block_val, dim=-1)

        W1_block = self.W1[block_idx] # [.. x top_k x d_block x d_model]
        b1_block = self.b1[block_idx] # [.. x top_k x d_block]

        x = torch.einsum('ibd,ibnhd->ibnh', (inp, W1_block)) + b1_block # [.. x top_k x d_block]
        #  x = x + block_val.unsqueeze(-1) # somehow like residual

        x = x * gate.unsqueeze(-1)

        relu_out = F.relu(x)
        relu_out = self.dropout_middle(relu_out)

        W2_block = self.W2[block_idx] # [.. x top_k x d_model]

        core_out = torch.einsum('ibnh,ibnhd->ibd', (x, W2_block)) + self.b2 # [.. x d_model]
        core_out = self.dropout_final(core_out)

        output = core_out + residual
        if not self.pre_lnorm:
            output = self.layer_norm(output)

        return output


Jiezhong Qiu's avatar
hmoe  
Jiezhong Qiu committed
254
class HierarchicalMoEPositionwiseFF(nn.Module):
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
255
    def __init__(self, d_model, d_inner, dropout, pre_lnorm=False, n_block=16, top_block=2):
Jiezhong Qiu's avatar
hmoe  
Jiezhong Qiu committed
256
257
258
        super(HierarchicalMoEPositionwiseFF, self).__init__()
        print("HierarchicalMoEPositionwiseFF")

Jiezhong Qiu's avatar
fix  
Jiezhong Qiu committed
259
        assert d_inner % n_block == 0
Jiezhong Qiu's avatar
Jiezhong Qiu committed
260
        assert top_block in [1, 2]
Jiezhong Qiu's avatar
hmoe  
Jiezhong Qiu committed
261
262
263
264
265
266
267
268
269
        self.top_block = top_block
        self.n_block = n_block

        d_block = d_inner // n_block
        self.d_block = d_block
        self.d_model = d_model
        self.d_inner = d_inner
        self.dropout = dropout

Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
270
        self.block_net = nn.Linear(d_model, n_block, bias=True)
Jiezhong Qiu's avatar
hmoe  
Jiezhong Qiu committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285

        self.W1 = nn.Parameter(torch.Tensor(n_block, d_block, d_model))
        self.b1 = nn.Parameter(torch.Tensor(n_block, d_block))

        self.W2 = nn.Parameter(torch.Tensor(n_block, d_block, d_model))
        self.b2 = nn.Parameter(torch.Tensor(d_model))

        self.layer_norm = nn.LayerNorm(d_model)

        self.pre_lnorm = pre_lnorm

        ratio = top_block / n_block
        self.dropout_middle = nn.Dropout(dropout * ratio)
        self.dropout_final = nn.Dropout(dropout)

Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
286
        #  self.scale = 1 / (d_model ** 0.5)
Jiezhong Qiu's avatar
hmoe  
Jiezhong Qiu committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
        self.reset_parameter()

    def reset_parameter(self):
        temp = nn.Linear(self.d_model, self.d_inner)
        self.W1.data = temp.weight.data.view(self.n_block, self.d_block, self.d_model)
        self.b1.data = temp.bias.data.view(self.n_block, self.d_block)
        temp = nn.Linear(self.d_inner, self.d_model)
        self.W2.data = temp.weight.data.transpose(0, 1).contiguous().view(self.n_block, self.d_block, self.d_model)
        self.b2.data = temp.bias.data

    def forward(self, inp):
        residual = inp
        if self.pre_lnorm:
            inp = self.layer_norm(inp)

        block = self.block_net(inp)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
303

Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
304
305
        #  block_val, block_idx = my_topk(block, k=self.top_block)
        block_val, block_idx = torch.topk(block, k=self.top_block, dim=-1, largest=True, sorted=False) # [.. x top_k]
Jiezhong Qiu's avatar
Jiezhong Qiu committed
306

Jiezhong Qiu's avatar
Jiezhong Qiu committed
307
        gate = F.softmax(block_val, dim=-1)
Jiezhong Qiu's avatar
hmoe  
Jiezhong Qiu committed
308
309
310
311

        W1_block = self.W1[block_idx] # [.. x top_k x d_block x d_model]
        b1_block = self.b1[block_idx] # [.. x top_k x d_block]

Jiezhong Qiu's avatar
Jiezhong Qiu committed
312
        x = torch.einsum('ibd,ibnhd->ibnh', (inp, W1_block)) + b1_block # [.. x top_k x d_block]
Jiezhong Qiu's avatar
update  
Jiezhong Qiu committed
313
        #  x = x + block_val.unsqueeze(-1) # somehow like residual
Jiezhong Qiu's avatar
Jiezhong Qiu committed
314

Jiezhong Qiu's avatar
Jiezhong Qiu committed
315
        x = x * gate.unsqueeze(-1)
Jiezhong Qiu's avatar
hmoe  
Jiezhong Qiu committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

        relu_out = F.relu(x)
        relu_out = self.dropout_middle(relu_out)

        W2_block = self.W2[block_idx] # [.. x top_k x d_model]

        core_out = torch.einsum('ibnh,ibnhd->ibd', (x, W2_block)) + self.b2 # [.. x d_model]
        core_out = self.dropout_final(core_out)

        output = core_out + residual
        if not self.pre_lnorm:
            output = self.layer_norm(output)

        return output

Jiezhong Qiu's avatar
Jiezhong Qiu committed
331
332
333
334
class SparsePositionwiseFF(nn.Module):
    def __init__(self, d_model, d_inner, dropout, pre_lnorm=False):
        super(SparsePositionwiseFF, self).__init__()
        print("SparsePositionwiseFF")
Jiezhong Qiu's avatar
Jiezhong Qiu committed
335

Jiezhong Qiu's avatar
Jiezhong Qiu committed
336
337
338
        self.d_model = d_model
        self.d_inner = d_inner
        self.dropout = dropout
Jiezhong Qiu's avatar
Jiezhong Qiu committed
339

Jiezhong Qiu's avatar
Jiezhong Qiu committed
340
341
342
343
344
        self.CoreNet_1 = nn.Sequential(
            nn.Linear(d_model, d_inner),
            nn.ReLU(inplace=True),
            nn.Dropout(dropout)
            )
Jiezhong Qiu's avatar
Jiezhong Qiu committed
345

Jiezhong Qiu's avatar
Jiezhong Qiu committed
346
347
        self.W2 = nn.Parameter(torch.Tensor(d_inner, d_model))
        self.b2 = nn.Parameter(torch.Tensor(d_model))
Jiezhong Qiu's avatar
Jiezhong Qiu committed
348

Jiezhong Qiu's avatar
Jiezhong Qiu committed
349
350
        self.layer_norm = nn.LayerNorm(d_model)
        self.pre_lnorm = pre_lnorm
Jiezhong Qiu's avatar
Jiezhong Qiu committed
351

Jiezhong Qiu's avatar
Jiezhong Qiu committed
352
353
        self.dropout_final = nn.Dropout(dropout)
        self.reset_parameter()
Jiezhong Qiu's avatar
Jiezhong Qiu committed
354

Jiezhong Qiu's avatar
Jiezhong Qiu committed
355
356
357
358
    def reset_parameter(self):
        temp_Linear = nn.Linear(self.d_inner, self.d_model)
        self.W2.data = temp_Linear.weight.data.transpose(0, 1)
        self.b2.data = temp_Linear.bias.data
Jiezhong Qiu's avatar
Jiezhong Qiu committed
359

Jiezhong Qiu's avatar
Jiezhong Qiu committed
360
361
362
363
    def forward(self, inp):
        residual = inp
        if self.pre_lnorm:
            inp = self.layer_norm(inp)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
364

Jiezhong Qiu's avatar
Jiezhong Qiu committed
365
366
367
368
369
        relu_out = self.CoreNet_1(inp).view(-1, self.d_inner)
        sparse_relu_out = torch_sparse.SparseTensor.from_dense(relu_out)
        core_out = torch_sparse.matmul(sparse_relu_out, self.W2) + self.b2
        core_out = core_out.view(inp.size(0), inp.size(1), self.d_model)
        core_out = self.dropout_final(core_out)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
370

Jiezhong Qiu's avatar
Jiezhong Qiu committed
371
372
373
        output = core_out + residual
        if not self.pre_lnorm:
            output = self.layer_norm(output)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
374

Jiezhong Qiu's avatar
Jiezhong Qiu committed
375
        return output
Jiezhong Qiu's avatar
Jiezhong Qiu committed
376
377
378
379
380
381
382
383

class MultiHeadPositionwiseFF(nn.Module):
    def __init__(self, d_model, d_inner, dropout, pre_lnorm=False, n_head=2):
        super(MultiHeadPositionwiseFF, self).__init__()
        print("MultiHeadPositionwiseFF")

        assert d_model % n_head == 0
        self.n_head = n_head
Jiezhong Qiu's avatar
fix  
Jiezhong Qiu committed
384
        d_head = d_model // n_head
Jiezhong Qiu's avatar
Jiezhong Qiu committed
385
386
387
388
389
        self.d_head = d_head
        self.d_model = d_model
        self.d_inner = d_inner
        self.dropout = dropout

Jiezhong Qiu's avatar
Jiezhong Qiu committed
390
        self.q_net = nn.Linear(d_model, d_model)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
391
392
393
394
395
396
397

        self.k_weight = nn.Parameter(torch.Tensor(n_head, d_inner, d_head))
        self.k_bias = nn.Parameter(torch.Tensor(n_head, d_inner))

        self.v_weight = nn.Parameter(torch.Tensor(n_head, d_head, d_inner))
        self.v_bias = nn.Parameter(torch.Tensor(n_head, d_head))

Jiezhong Qiu's avatar
fix bug  
Jiezhong Qiu committed
398
        #self.o_net = nn.Linear(d_model, d_model)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422

        self.layer_norm = nn.LayerNorm(d_model)

        self.pre_lnorm = pre_lnorm

        self.dropout = nn.Dropout(dropout)

        self.reset_parameter()

    def reset_parameter(self):
        for i in range(self.n_head):
            tmp = nn.Linear(self.d_head, self.d_inner)
            self.k_weight.data[i] = tmp.weight.data
            self.k_bias.data[i] = tmp.bias.data

            tmp = nn.Linear(self.d_inner, self.d_head)
            self.v_weight.data[i] = tmp.weight.data
            self.v_bias.data[i] = tmp.bias.data

    def forward(self, inp):
        residual = inp
        if self.pre_lnorm:
            inp = self.layer_norm(inp)

Jiezhong Qiu's avatar
Jiezhong Qiu committed
423
424
        head_q = self.q_net(inp)
        head_q = head_q.view(inp.size(0), inp.size(1), self.n_head, self.d_head) # [.. x n_head x d_head]
Jiezhong Qiu's avatar
Jiezhong Qiu committed
425
426
427
428
429
430
431

        attn_score = torch.einsum('ibnd,nhd->ibnh', (head_q, self.k_weight)) + self.k_bias # [.. x n_head x d_inner]
        attn_score = F.relu(attn_score)
        attn_score = self.dropout(attn_score)

        attn_vec = torch.einsum('ibnh,ndh->ibnd', (attn_score, self.v_weight)) + self.v_bias

Jiezhong Qiu's avatar
fix  
Jiezhong Qiu committed
432
        attn_vec = attn_vec.contiguous().view(inp.size(0), inp.size(1), self.d_model)
Jiezhong Qiu's avatar
fix bug  
Jiezhong Qiu committed
433
434
        # core_out = self.o_net(attn_vec)
        core_out = self.dropout(attn_vec)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
435
436
437
438
439
440
441

        output = core_out + residual
        if not self.pre_lnorm:
            output = self.layer_norm(output)

        return output

Zhilin Yang's avatar
init  
Zhilin Yang committed
442
class PositionwiseFF(nn.Module):
Jiezhong Qiu's avatar
Jiezhong Qiu committed
443
    def __init__(self, d_model, d_inner, dropout, pre_lnorm=False, use_softmax=True):
Zhilin Yang's avatar
init  
Zhilin Yang committed
444
445
446
447
448
449
        super(PositionwiseFF, self).__init__()

        self.d_model = d_model
        self.d_inner = d_inner
        self.dropout = dropout

450
451
        self.CoreNet_1 = nn.Sequential(
            nn.Linear(d_model, d_inner),
Jiezhong Qiu's avatar
Jiezhong Qiu committed
452
            nn.Softmax(dim=-1) if use_softmax else nn.ReLU(inplace=True)
453
454
        )
        self.CoreNet_2 = nn.Sequential(
Zhilin Yang's avatar
init  
Zhilin Yang committed
455
456
457
458
459
460
461
462
463
464
465
466
            nn.Dropout(dropout),
            nn.Linear(d_inner, d_model),
            nn.Dropout(dropout),
        )

        self.layer_norm = nn.LayerNorm(d_model)

        self.pre_lnorm = pre_lnorm

    def forward(self, inp):
        if self.pre_lnorm:
            ##### layer normalization + positionwise feed-forward
467
468
            relu_out = self.CoreNet_1(self.layer_norm(inp))
            core_out = self.CoreNet_2(relu_out)
Zhilin Yang's avatar
init  
Zhilin Yang committed
469
470
471
472
473

            ##### residual connection
            output = core_out + inp
        else:
            ##### positionwise feed-forward
474
475
            relu_out = self.CoreNet_1(inp)
            core_out = self.CoreNet_2(relu_out)
Zhilin Yang's avatar
init  
Zhilin Yang committed
476
477
478
479

            ##### residual connection + layer normalization
            output = self.layer_norm(inp + core_out)

Jiezhong Qiu's avatar
Jiezhong Qiu committed
480
481
        return output
        #  return output, relu_out.detach()
Zhilin Yang's avatar
init  
Zhilin Yang committed
482

Jiezhong Qiu's avatar
Jiezhong Qiu committed
483
484
485
class ExtendedMultiHeadAttn(nn.Module):
    def __init__(self, n_head, d_model, d_head, dropout, dropatt=0,
                 pre_lnorm=False):
486
487
        super(ExtendedMultiHeadAttn, self).__init__()
        print("ExtendedMultiHeadAttn")
Jiezhong Qiu's avatar
Jiezhong Qiu committed
488
489
490
491
492
493
494
495
496
497
498

        self.n_head = n_head
        self.d_model = d_model
        self.d_head = d_head
        self.dropout = dropout

        self.q_net = nn.Linear(d_model, n_head * d_head, bias=False)
        self.kv_net = nn.Linear(d_model, 2 * n_head * d_head, bias=False)

        self.drop = nn.Dropout(dropout)
        self.dropatt = nn.Dropout(dropatt)
499
        self.o_net = nn.Linear(n_head * d_head * 2, d_model, bias=False)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
500
501
502
503
504
505
506

        self.layer_norm = nn.LayerNorm(d_model)

        self.scale = 1 / (d_head ** 0.5)

        self.pre_lnorm = pre_lnorm

507
508
509
        #  self.coeff = nn.Parameter(torch.Tensor(n_head, 2))
        #  nn.init.uniform_(self.coeff, a=-1, b=1)

Jiezhong Qiu's avatar
Jiezhong Qiu committed
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
    def forward(self, h, attn_mask=None, mems=None):
        ##### multihead attention
        # [hlen x bsz x n_head x d_head]

        if mems is not None:
            c = torch.cat([mems, h], 0)
            mem_len = mems.size(0)
        else:
            c = h
            mem_len = 0

        if self.pre_lnorm:
            ##### layer normalization
            c = self.layer_norm(c)

        head_q = self.q_net(c)
        head_k, head_v = torch.chunk(self.kv_net(c), 2, -1)

        head_q = head_q.view(c.size(0), c.size(1), self.n_head, self.d_head)
        head_k = head_k.view(c.size(0), c.size(1), self.n_head, self.d_head)
        head_v = head_v.view(c.size(0), c.size(1), self.n_head, self.d_head)

        # [qlen x klen x bsz x n_head]
        attn_score = torch.einsum('ibnd,jbnd->ijbn', (head_q, head_k))
        attn_score.mul_(self.scale)
        if attn_mask is not None and attn_mask.any().item():
            if attn_mask.dim() == 2:
Jiezhong Qiu's avatar
Jiezhong Qiu committed
537
                attn_score[mem_len:].masked_fill_(attn_mask[None,:,:,None].bool(), -float('inf'))
Jiezhong Qiu's avatar
Jiezhong Qiu committed
538
            elif attn_mask.dim() == 3:
Jiezhong Qiu's avatar
Jiezhong Qiu committed
539
                attn_score[mem_len:].masked_fill_(attn_mask[:,:,:,None].bool(), -float('inf'))
Jiezhong Qiu's avatar
Jiezhong Qiu committed
540
541


542
        mem2other_attn = attn_mask.new_ones(mem_len, c.size(0))
Jiezhong Qiu's avatar
Jiezhong Qiu committed
543
        mem2other_attn[:, :mem_len] = 0
Jiezhong Qiu's avatar
Jiezhong Qiu committed
544
        attn_score[:mem_len].masked_fill_(mem2other_attn[:, :, None, None].bool(), -float('inf'))
Jiezhong Qiu's avatar
Jiezhong Qiu committed
545
546
547
548
549
550
551

        # [qlen x klen x bsz x n_head]
        attn_prob = F.softmax(attn_score, dim=1)
        attn_prob = self.dropatt(attn_prob)

        # [qlen x klen x bsz x n_head] + [klen x bsz x n_head x d_head] -> [qlen x bsz x n_head x d_head]
        attn_vec = torch.einsum('ijbn,jbnd->ibnd', (attn_prob, head_v))
552
553
554
555
556
557
558
        attn_vec_quad = torch.einsum('ijbn,jbnd->ibnd', (attn_prob, attn_vec))
        # [qlen x bsz x n_head x d_head x 2]
        attn_vecs = torch.cat([attn_vec.unsqueeze(-1), attn_vec_quad.unsqueeze(-1)], dim=-1)

        #  attn_vec = torch.einsum('ibndt,nt->ibnd', (attn_vecs, self.coeff))
        attn_vec = attn_vecs.contiguous().view(
            attn_vec.size(0), attn_vec.size(1), self.n_head * self.d_head * 2)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574

        attn_vec = attn_vec[mem_len:]

        ##### linear projection
        attn_out = self.o_net(attn_vec)
        attn_out = self.drop(attn_out)

        if self.pre_lnorm:
            ##### residual connection
            output = h + attn_out
        else:
            ##### residual connection + layer normalization
            output = self.layer_norm(h + attn_out)

        return output

Zhilin Yang's avatar
init  
Zhilin Yang committed
575
class MultiHeadAttn(nn.Module):
Jiezhong Qiu's avatar
Jiezhong Qiu committed
576
    def __init__(self, n_head, d_model, d_head, dropout, dropatt=0,
Zhilin Yang's avatar
init  
Zhilin Yang committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
                 pre_lnorm=False):
        super(MultiHeadAttn, self).__init__()

        self.n_head = n_head
        self.d_model = d_model
        self.d_head = d_head
        self.dropout = dropout

        self.q_net = nn.Linear(d_model, n_head * d_head, bias=False)
        self.kv_net = nn.Linear(d_model, 2 * n_head * d_head, bias=False)

        self.drop = nn.Dropout(dropout)
        self.dropatt = nn.Dropout(dropatt)
        self.o_net = nn.Linear(n_head * d_head, d_model, bias=False)

        self.layer_norm = nn.LayerNorm(d_model)

        self.scale = 1 / (d_head ** 0.5)

        self.pre_lnorm = pre_lnorm

    def forward(self, h, attn_mask=None, mems=None):
        ##### multihead attention
        # [hlen x bsz x n_head x d_head]

        if mems is not None:
            c = torch.cat([mems, h], 0)
        else:
            c = h

        if self.pre_lnorm:
            ##### layer normalization
            c = self.layer_norm(c)

        head_q = self.q_net(h)
        head_k, head_v = torch.chunk(self.kv_net(c), 2, -1)

        head_q = head_q.view(h.size(0), h.size(1), self.n_head, self.d_head)
        head_k = head_k.view(c.size(0), c.size(1), self.n_head, self.d_head)
        head_v = head_v.view(c.size(0), c.size(1), self.n_head, self.d_head)

        # [qlen x klen x bsz x n_head]
        attn_score = torch.einsum('ibnd,jbnd->ijbn', (head_q, head_k))
        attn_score.mul_(self.scale)
        if attn_mask is not None and attn_mask.any().item():
            if attn_mask.dim() == 2:
Jiezhong Qiu's avatar
Jiezhong Qiu committed
623
                attn_score.masked_fill_(attn_mask[None,:,:,None].bool(), -float('inf'))
Zhilin Yang's avatar
init  
Zhilin Yang committed
624
            elif attn_mask.dim() == 3:
Jiezhong Qiu's avatar
Jiezhong Qiu committed
625
                attn_score.masked_fill_(attn_mask[:,:,:,None].bool(), -float('inf'))
Zhilin Yang's avatar
init  
Zhilin Yang committed
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769

        # [qlen x klen x bsz x n_head]
        attn_prob = F.softmax(attn_score, dim=1)
        attn_prob = self.dropatt(attn_prob)

        # [qlen x klen x bsz x n_head] + [klen x bsz x n_head x d_head] -> [qlen x bsz x n_head x d_head]
        attn_vec = torch.einsum('ijbn,jbnd->ibnd', (attn_prob, head_v))
        attn_vec = attn_vec.contiguous().view(
            attn_vec.size(0), attn_vec.size(1), self.n_head * self.d_head)

        ##### linear projection
        attn_out = self.o_net(attn_vec)
        attn_out = self.drop(attn_out)

        if self.pre_lnorm:
            ##### residual connection
            output = h + attn_out
        else:
            ##### residual connection + layer normalization
            output = self.layer_norm(h + attn_out)

        return output

class RelMultiHeadAttn(nn.Module):
    def __init__(self, n_head, d_model, d_head, dropout, dropatt=0,
                 tgt_len=None, ext_len=None, mem_len=None, pre_lnorm=False):
        super(RelMultiHeadAttn, self).__init__()

        self.n_head = n_head
        self.d_model = d_model
        self.d_head = d_head
        self.dropout = dropout

        self.qkv_net = nn.Linear(d_model, 3 * n_head * d_head, bias=False)

        self.drop = nn.Dropout(dropout)
        self.dropatt = nn.Dropout(dropatt)
        self.o_net = nn.Linear(n_head * d_head, d_model, bias=False)

        self.layer_norm = nn.LayerNorm(d_model)

        self.scale = 1 / (d_head ** 0.5)

        self.pre_lnorm = pre_lnorm

    def _parallelogram_mask(self, h, w, left=False):
        mask = torch.ones((h, w)).byte()
        m = min(h, w)
        mask[:m,:m] = torch.triu(mask[:m,:m])
        mask[-m:,-m:] = torch.tril(mask[-m:,-m:])

        if left:
            return mask
        else:
            return mask.flip(0)

    def _shift(self, x, qlen, klen, mask, left=False):
        if qlen > 1:
            zero_pad = torch.zeros((x.size(0), qlen-1, x.size(2), x.size(3)),
                                    device=x.device, dtype=x.dtype)
        else:
            zero_pad = torch.zeros(0, device=x.device, dtype=x.dtype)

        if left:
            mask = mask.flip(1)
            x_padded = torch.cat([zero_pad, x], dim=1).expand(qlen, -1, -1, -1)
        else:
            x_padded = torch.cat([x, zero_pad], dim=1).expand(qlen, -1, -1, -1)

        x = x_padded.masked_select(mask[:,:,None,None]) \
                    .view(qlen, klen, x.size(2), x.size(3))

        return x

    def _rel_shift(self, x, zero_triu=False):
        zero_pad = torch.zeros((x.size(0), 1, *x.size()[2:]),
                               device=x.device, dtype=x.dtype)
        x_padded = torch.cat([zero_pad, x], dim=1)

        x_padded = x_padded.view(x.size(1) + 1, x.size(0), *x.size()[2:])

        x = x_padded[1:].view_as(x)

        if zero_triu:
            ones = torch.ones((x.size(0), x.size(1)))
            x = x * torch.tril(ones, x.size(1) - x.size(0))[:,:,None,None]

        return x

    def forward(self, w, r, attn_mask=None, mems=None):
        raise NotImplementedError

class RelPartialLearnableMultiHeadAttn(RelMultiHeadAttn):
    def __init__(self, *args, **kwargs):
        super(RelPartialLearnableMultiHeadAttn, self).__init__(*args, **kwargs)

        self.r_net = nn.Linear(self.d_model, self.n_head * self.d_head, bias=False)

    def forward(self, w, r, r_w_bias, r_r_bias, attn_mask=None, mems=None):
        qlen, rlen, bsz = w.size(0), r.size(0), w.size(1)

        if mems is not None:
            cat = torch.cat([mems, w], 0)
            if self.pre_lnorm:
                w_heads = self.qkv_net(self.layer_norm(cat))
            else:
                w_heads = self.qkv_net(cat)
            r_head_k = self.r_net(r)

            w_head_q, w_head_k, w_head_v = torch.chunk(w_heads, 3, dim=-1)
            w_head_q = w_head_q[-qlen:]
        else:
            if self.pre_lnorm:
                w_heads = self.qkv_net(self.layer_norm(w))
            else:
                w_heads = self.qkv_net(w)
            r_head_k = self.r_net(r)

            w_head_q, w_head_k, w_head_v = torch.chunk(w_heads, 3, dim=-1)

        klen = w_head_k.size(0)

        w_head_q = w_head_q.view(qlen, bsz, self.n_head, self.d_head)           # qlen x bsz x n_head x d_head
        w_head_k = w_head_k.view(klen, bsz, self.n_head, self.d_head)           # qlen x bsz x n_head x d_head
        w_head_v = w_head_v.view(klen, bsz, self.n_head, self.d_head)           # qlen x bsz x n_head x d_head

        r_head_k = r_head_k.view(rlen, self.n_head, self.d_head)                # qlen x n_head x d_head

        #### compute attention score
        rw_head_q = w_head_q + r_w_bias                                         # qlen x bsz x n_head x d_head
        AC = torch.einsum('ibnd,jbnd->ijbn', (rw_head_q, w_head_k))             # qlen x klen x bsz x n_head

        rr_head_q = w_head_q + r_r_bias
        BD = torch.einsum('ibnd,jnd->ijbn', (rr_head_q, r_head_k))              # qlen x klen x bsz x n_head
        BD = self._rel_shift(BD)

        # [qlen x klen x bsz x n_head]
        attn_score = AC + BD
        attn_score.mul_(self.scale)

        #### compute attention probability
        if attn_mask is not None and attn_mask.any().item():
            if attn_mask.dim() == 2:
                attn_score = attn_score.float().masked_fill(
Jiezhong Qiu's avatar
Jiezhong Qiu committed
770
                    attn_mask[None,:,:,None].bool(), -float('inf')).type_as(attn_score)
Zhilin Yang's avatar
init  
Zhilin Yang committed
771
772
            elif attn_mask.dim() == 3:
                attn_score = attn_score.float().masked_fill(
Jiezhong Qiu's avatar
Jiezhong Qiu committed
773
                    attn_mask[:,:,:,None].bool(), -float('inf')).type_as(attn_score)
Zhilin Yang's avatar
init  
Zhilin Yang committed
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855

        # [qlen x klen x bsz x n_head]
        attn_prob = F.softmax(attn_score, dim=1)
        attn_prob = self.dropatt(attn_prob)

        #### compute attention vector
        attn_vec = torch.einsum('ijbn,jbnd->ibnd', (attn_prob, w_head_v))

        # [qlen x bsz x n_head x d_head]
        attn_vec = attn_vec.contiguous().view(
            attn_vec.size(0), attn_vec.size(1), self.n_head * self.d_head)

        ##### linear projection
        attn_out = self.o_net(attn_vec)
        attn_out = self.drop(attn_out)

        if self.pre_lnorm:
            ##### residual connection
            output = w + attn_out
        else:
            ##### residual connection + layer normalization
            output = self.layer_norm(w + attn_out)

        return output

class RelLearnableMultiHeadAttn(RelMultiHeadAttn):
    def __init__(self, *args, **kwargs):
        super(RelLearnableMultiHeadAttn, self).__init__(*args, **kwargs)

    def forward(self, w, r_emb, r_w_bias, r_bias, attn_mask=None, mems=None):
        # r_emb: [klen, n_head, d_head], used for term B
        # r_w_bias: [n_head, d_head], used for term C
        # r_bias: [klen, n_head], used for term D

        qlen, bsz = w.size(0), w.size(1)

        if mems is not None:
            cat = torch.cat([mems, w], 0)
            if self.pre_lnorm:
                w_heads = self.qkv_net(self.layer_norm(cat))
            else:
                w_heads = self.qkv_net(cat)
            w_head_q, w_head_k, w_head_v = torch.chunk(w_heads, 3, dim=-1)

            w_head_q = w_head_q[-qlen:]
        else:
            if self.pre_lnorm:
                w_heads = self.qkv_net(self.layer_norm(w))
            else:
                w_heads = self.qkv_net(w)
            w_head_q, w_head_k, w_head_v = torch.chunk(w_heads, 3, dim=-1)

        klen = w_head_k.size(0)

        w_head_q = w_head_q.view(qlen, bsz, self.n_head, self.d_head)
        w_head_k = w_head_k.view(klen, bsz, self.n_head, self.d_head)
        w_head_v = w_head_v.view(klen, bsz, self.n_head, self.d_head)

        if klen > r_emb.size(0):
            r_emb_pad = r_emb[0:1].expand(klen-r_emb.size(0), -1, -1)
            r_emb = torch.cat([r_emb_pad, r_emb], 0)
            r_bias_pad = r_bias[0:1].expand(klen-r_bias.size(0), -1)
            r_bias = torch.cat([r_bias_pad, r_bias], 0)
        else:
            r_emb = r_emb[-klen:]
            r_bias = r_bias[-klen:]

        #### compute attention score
        rw_head_q = w_head_q + r_w_bias[None]                                   # qlen x bsz x n_head x d_head

        AC = torch.einsum('ibnd,jbnd->ijbn', (rw_head_q, w_head_k))             # qlen x klen x bsz x n_head
        B_ = torch.einsum('ibnd,jnd->ijbn', (w_head_q, r_emb))                  # qlen x klen x bsz x n_head
        D_ = r_bias[None, :, None]                                              # 1    x klen x 1   x n_head
        BD = self._rel_shift(B_ + D_)

        # [qlen x klen x bsz x n_head]
        attn_score = AC + BD
        attn_score.mul_(self.scale)

        #### compute attention probability
        if attn_mask is not None and attn_mask.any().item():
            if attn_mask.dim() == 2:
Jiezhong Qiu's avatar
Jiezhong Qiu committed
856
                attn_score.masked_fill_(attn_mask[None,:,:,None].bool(), -float('inf'))
Zhilin Yang's avatar
init  
Zhilin Yang committed
857
            elif attn_mask.dim() == 3:
Jiezhong Qiu's avatar
Jiezhong Qiu committed
858
                attn_score.masked_fill_(attn_mask[:,:,:,None].bool(), -float('inf'))
Zhilin Yang's avatar
init  
Zhilin Yang committed
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888

        # [qlen x klen x bsz x n_head]
        attn_prob = F.softmax(attn_score, dim=1)
        attn_prob = self.dropatt(attn_prob)

        #### compute attention vector
        attn_vec = torch.einsum('ijbn,jbnd->ibnd', (attn_prob, w_head_v))

        # [qlen x bsz x n_head x d_head]
        attn_vec = attn_vec.contiguous().view(
            attn_vec.size(0), attn_vec.size(1), self.n_head * self.d_head)

        ##### linear projection
        attn_out = self.o_net(attn_vec)
        attn_out = self.drop(attn_out)

        if self.pre_lnorm:
            ##### residual connection
            output = w + attn_out
        else:
            ##### residual connection + layer normalization
            output = self.layer_norm(w + attn_out)

        return output

class DecoderLayer(nn.Module):
    def __init__(self, n_head, d_model, d_head, d_inner, dropout, **kwargs):
        super(DecoderLayer, self).__init__()

        self.dec_attn = MultiHeadAttn(n_head, d_model, d_head, dropout, **kwargs)
889
        #  self.dec_attn = ExtendedMultiHeadAttn(n_head, d_model, d_head, dropout, **kwargs)
890
        self.pos_ff = CustomizedMoEPositionwiseFF(d_model, d_inner, dropout,
Zhilin Yang's avatar
init  
Zhilin Yang committed
891
892
893
894
895
896
                                     pre_lnorm=kwargs.get('pre_lnorm'))

    def forward(self, dec_inp, dec_attn_mask=None, mems=None):

        output = self.dec_attn(dec_inp, attn_mask=dec_attn_mask,
                               mems=mems)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
897
898
        output = self.pos_ff(output)
        #  output, relu_out = self.pos_ff(output)
Zhilin Yang's avatar
init  
Zhilin Yang committed
899

Jiezhong Qiu's avatar
Jiezhong Qiu committed
900
901
        return output
        #  return output, relu_out
Zhilin Yang's avatar
init  
Zhilin Yang committed
902
903
904
905
906
907
908
909

class RelLearnableDecoderLayer(nn.Module):
    def __init__(self, n_head, d_model, d_head, d_inner, dropout,
                 **kwargs):
        super(RelLearnableDecoderLayer, self).__init__()

        self.dec_attn = RelLearnableMultiHeadAttn(n_head, d_model, d_head, dropout,
                                         **kwargs)
910
        self.pos_ff = CustomizedMoEPositionwiseFF(d_model, d_inner, dropout,
Zhilin Yang's avatar
init  
Zhilin Yang committed
911
912
913
914
915
916
917
                                     pre_lnorm=kwargs.get('pre_lnorm'))

    def forward(self, dec_inp, r_emb, r_w_bias, r_bias, dec_attn_mask=None, mems=None):

        output = self.dec_attn(dec_inp, r_emb, r_w_bias, r_bias,
                               attn_mask=dec_attn_mask,
                               mems=mems)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
918
919
        output = self.pos_ff(output)
        #  output, relu_out = self.pos_ff(output)
Zhilin Yang's avatar
init  
Zhilin Yang committed
920

Jiezhong Qiu's avatar
Jiezhong Qiu committed
921
922
        return output
        #  return output, relu_out
Zhilin Yang's avatar
init  
Zhilin Yang committed
923
924
925
926
927
928
929
930

class RelPartialLearnableDecoderLayer(nn.Module):
    def __init__(self, n_head, d_model, d_head, d_inner, dropout,
                 **kwargs):
        super(RelPartialLearnableDecoderLayer, self).__init__()

        self.dec_attn = RelPartialLearnableMultiHeadAttn(n_head, d_model,
                            d_head, dropout, **kwargs)
931
        self.pos_ff = CustomizedMoEPositionwiseFF(d_model, d_inner, dropout,
Zhilin Yang's avatar
init  
Zhilin Yang committed
932
933
934
935
936
937
938
                                     pre_lnorm=kwargs.get('pre_lnorm'))

    def forward(self, dec_inp, r, r_w_bias, r_r_bias, dec_attn_mask=None, mems=None):

        output = self.dec_attn(dec_inp, r, r_w_bias, r_r_bias,
                               attn_mask=dec_attn_mask,
                               mems=mems)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
939
940
        output = self.pos_ff(output)
        #  output, relu_out = self.pos_ff(output)
Zhilin Yang's avatar
init  
Zhilin Yang committed
941

Jiezhong Qiu's avatar
Jiezhong Qiu committed
942
943
        return output
        #  return output, relu_out
Zhilin Yang's avatar
init  
Zhilin Yang committed
944
945
946


class AdaptiveEmbedding(nn.Module):
Jiezhong Qiu's avatar
Jiezhong Qiu committed
947
    def __init__(self, n_token, d_embed, d_proj, cutoffs, div_val=1,
Zhilin Yang's avatar
init  
Zhilin Yang committed
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
                 sample_softmax=False):
        super(AdaptiveEmbedding, self).__init__()

        self.n_token = n_token
        self.d_embed = d_embed

        self.cutoffs = cutoffs + [n_token]
        self.div_val = div_val
        self.d_proj = d_proj

        self.emb_scale = d_proj ** 0.5

        self.cutoff_ends = [0] + self.cutoffs

        self.emb_layers = nn.ModuleList()
        self.emb_projs = nn.ParameterList()
        if div_val == 1:
            self.emb_layers.append(
                nn.Embedding(n_token, d_embed, sparse=sample_softmax>0)
            )
            if d_proj != d_embed:
                self.emb_projs.append(nn.Parameter(torch.Tensor(d_proj, d_embed)))
        else:
            for i in range(len(self.cutoffs)):
                l_idx, r_idx = self.cutoff_ends[i], self.cutoff_ends[i+1]
                d_emb_i = d_embed // (div_val ** i)
                self.emb_layers.append(nn.Embedding(r_idx-l_idx, d_emb_i))
                self.emb_projs.append(nn.Parameter(torch.Tensor(d_proj, d_emb_i)))

    def forward(self, inp):
        if self.div_val == 1:
            embed = self.emb_layers[0](inp)
            if self.d_proj != self.d_embed:
                embed  = F.linear(embed, self.emb_projs[0])
        else:
            param = next(self.parameters())
            inp_flat = inp.view(-1)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
985
            emb_flat = torch.zeros([inp_flat.size(0), self.d_proj],
Zhilin Yang's avatar
init  
Zhilin Yang committed
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
                dtype=param.dtype, device=param.device)
            for i in range(len(self.cutoffs)):
                l_idx, r_idx = self.cutoff_ends[i], self.cutoff_ends[i + 1]

                mask_i = (inp_flat >= l_idx) & (inp_flat < r_idx)
                indices_i = mask_i.nonzero().squeeze()

                if indices_i.numel() == 0:
                    continue

                inp_i = inp_flat.index_select(0, indices_i) - l_idx
                emb_i = self.emb_layers[i](inp_i)
                emb_i = F.linear(emb_i, self.emb_projs[i])

                emb_flat.index_copy_(0, indices_i, emb_i)

            embed = emb_flat.view(*inp.size(), self.d_proj)

        embed.mul_(self.emb_scale)

        return embed

class MemTransformerLM(nn.Module):
    def __init__(self, n_token, n_layer, n_head, d_model, d_head, d_inner,
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1010
                 dropout, dropatt, tie_weight=True, d_embed=None,
Zhilin Yang's avatar
init  
Zhilin Yang committed
1011
                 div_val=1, tie_projs=[False], pre_lnorm=False,
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1012
                 tgt_len=None, ext_len=None, mem_len=None,
Zhilin Yang's avatar
init  
Zhilin Yang committed
1013
                 cutoffs=[], adapt_inp=False,
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1014
                 same_length=False, attn_type=0, clamp_len=-1,
Zhilin Yang's avatar
init  
Zhilin Yang committed
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
                 sample_softmax=-1):
        super(MemTransformerLM, self).__init__()
        self.n_token = n_token

        d_embed = d_model if d_embed is None else d_embed
        self.d_embed = d_embed
        self.d_model = d_model
        self.n_head = n_head
        self.d_head = d_head

Jiezhong Qiu's avatar
Jiezhong Qiu committed
1025
        self.word_emb = AdaptiveEmbedding(n_token, d_embed, d_model, cutoffs,
Zhilin Yang's avatar
init  
Zhilin Yang committed
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
                                          div_val=div_val)

        self.drop = nn.Dropout(dropout)

        self.n_layer = n_layer

        self.tgt_len = tgt_len
        self.mem_len = mem_len
        self.ext_len = ext_len
        self.max_klen = tgt_len + ext_len + mem_len

        self.attn_type = attn_type

        self.layers = nn.ModuleList()
        if attn_type == 0: # the default attention
            for i in range(n_layer):
                self.layers.append(
                    RelPartialLearnableDecoderLayer(
                        n_head, d_model, d_head, d_inner, dropout,
                        tgt_len=tgt_len, ext_len=ext_len, mem_len=mem_len,
                        dropatt=dropatt, pre_lnorm=pre_lnorm)
                )
        elif attn_type == 1: # learnable embeddings
            for i in range(n_layer):
                self.layers.append(
                    RelLearnableDecoderLayer(
                        n_head, d_model, d_head, d_inner, dropout,
                        tgt_len=tgt_len, ext_len=ext_len, mem_len=mem_len,
                        dropatt=dropatt, pre_lnorm=pre_lnorm)
                )
        elif attn_type in [2, 3]: # absolute embeddings
            for i in range(n_layer):
                self.layers.append(
                    DecoderLayer(
                        n_head, d_model, d_head, d_inner, dropout,
                        dropatt=dropatt, pre_lnorm=pre_lnorm)
                )

        self.sample_softmax = sample_softmax
        # use sampled softmax
        if sample_softmax > 0:
            self.out_layer = nn.Linear(d_model, n_token)
            if tie_weight:
                self.out_layer.weight = self.word_emb.weight
            self.tie_weight = tie_weight
            self.sampler = LogUniformSampler(n_token, sample_softmax)

        # use adaptive softmax (including standard softmax)
        else:
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1075
            self.crit = ProjectedAdaptiveLogSoftmax(n_token, d_embed, d_model,
Zhilin Yang's avatar
init  
Zhilin Yang committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
                                                    cutoffs, div_val=div_val)

            if tie_weight:
                for i in range(len(self.crit.out_layers)):
                    self.crit.out_layers[i].weight = self.word_emb.emb_layers[i].weight

            if tie_projs:
                for i, tie_proj in enumerate(tie_projs):
                    if tie_proj and div_val == 1 and d_model != d_embed:
                        self.crit.out_projs[i] = self.word_emb.emb_projs[0]
                    elif tie_proj and div_val != 1:
                        self.crit.out_projs[i] = self.word_emb.emb_projs[i]

        self.same_length = same_length
        self.clamp_len = clamp_len

        self._create_params()

    def backward_compatible(self):
        self.sample_softmax = -1

    def _create_params(self):
        if self.attn_type == 0: # default attention
            self.pos_emb = PositionalEmbedding(self.d_model)
            self.r_w_bias = nn.Parameter(torch.Tensor(self.n_head, self.d_head))
            self.r_r_bias = nn.Parameter(torch.Tensor(self.n_head, self.d_head))
        elif self.attn_type == 1: # learnable
            self.r_emb = nn.Parameter(torch.Tensor(
                    self.n_layer, self.max_klen, self.n_head, self.d_head))
            self.r_w_bias = nn.Parameter(torch.Tensor(
                    self.n_layer, self.n_head, self.d_head))
            self.r_bias = nn.Parameter(torch.Tensor(
                    self.n_layer, self.max_klen, self.n_head))
        elif self.attn_type == 2: # absolute standard
            self.pos_emb = PositionalEmbedding(self.d_model)
        elif self.attn_type == 3: # absolute deeper SA
            self.r_emb = nn.Parameter(torch.Tensor(
                    self.n_layer, self.max_klen, self.n_head, self.d_head))

    def reset_length(self, tgt_len, ext_len, mem_len):
        self.tgt_len = tgt_len
        self.mem_len = mem_len
        self.ext_len = ext_len

    def init_mems(self):
        if self.mem_len > 0:
            mems = []
            param = next(self.parameters())
            for i in range(self.n_layer+1):
                empty = torch.empty(0, dtype=param.dtype, device=param.device)
                mems.append(empty)

            return mems
        else:
            return None

    def _update_mems(self, hids, mems, qlen, mlen):
        # does not deal with None
        if mems is None: return None

        # mems is not None
        assert len(hids) == len(mems), 'len(hids) != len(mems)'

        # There are `mlen + qlen` steps that can be cached into mems
        # For the next step, the last `ext_len` of the `qlen` tokens
        # will be used as the extended context. Hence, we only cache
        # the tokens from `mlen + qlen - self.ext_len - self.mem_len`
        # to `mlen + qlen - self.ext_len`.
        with torch.no_grad():
            new_mems = []
            end_idx = mlen + max(0, qlen - 0 - self.ext_len)
            beg_idx = max(0, end_idx - self.mem_len)
            for i in range(len(hids)):

                cat = torch.cat([mems[i], hids[i]], dim=0)
                new_mems.append(cat[beg_idx:end_idx].detach())

        return new_mems

    def _forward(self, dec_inp, mems=None):
        qlen, bsz = dec_inp.size()

        word_emb = self.word_emb(dec_inp)

        mlen = mems[0].size(0) if mems is not None else 0
        klen = mlen + qlen
        if self.same_length:
            all_ones = word_emb.new_ones(qlen, klen)
            mask_len = klen - self.mem_len
            if mask_len > 0:
                mask_shift_len = qlen - mask_len
            else:
                mask_shift_len = qlen
            dec_attn_mask = (torch.triu(all_ones, 1+mlen)
                    + torch.tril(all_ones, -mask_shift_len)).byte()[:, :, None] # -1
        else:
            dec_attn_mask = torch.triu(
                word_emb.new_ones(qlen, klen), diagonal=1+mlen).byte()[:,:,None]

        hids = []
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1176
        #  relu_outs = []
Zhilin Yang's avatar
init  
Zhilin Yang committed
1177
        if self.attn_type == 0: # default
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1178
            pos_seq = torch.arange(klen-1, -1, -1.0, device=word_emb.device,
Zhilin Yang's avatar
init  
Zhilin Yang committed
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
                                   dtype=word_emb.dtype)
            if self.clamp_len > 0:
                pos_seq.clamp_(max=self.clamp_len)
            pos_emb = self.pos_emb(pos_seq)

            core_out = self.drop(word_emb)
            pos_emb = self.drop(pos_emb)

            hids.append(core_out)
            for i, layer in enumerate(self.layers):
                mems_i = None if mems is None else mems[i]
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1190
1191
                #  core_out, relu_out = layer(core_out, pos_emb, self.r_w_bias,
                core_out = layer(core_out, pos_emb, self.r_w_bias,
Zhilin Yang's avatar
init  
Zhilin Yang committed
1192
1193
                        self.r_r_bias, dec_attn_mask=dec_attn_mask, mems=mems_i)
                hids.append(core_out)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1194
                #  relu_outs.append(relu_out)
Zhilin Yang's avatar
init  
Zhilin Yang committed
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
        elif self.attn_type == 1: # learnable
            core_out = self.drop(word_emb)
            hids.append(core_out)
            for i, layer in enumerate(self.layers):
                if self.clamp_len > 0:
                    r_emb = self.r_emb[i][-self.clamp_len :]
                    r_bias = self.r_bias[i][-self.clamp_len :]
                else:
                    r_emb, r_bias = self.r_emb[i], self.r_bias[i]

                mems_i = None if mems is None else mems[i]
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1206
1207
                #  core_out, relu_out = layer(core_out, r_emb, self.r_w_bias[i],
                core_out = layer(core_out, r_emb, self.r_w_bias[i],
Zhilin Yang's avatar
init  
Zhilin Yang committed
1208
1209
                        r_bias, dec_attn_mask=dec_attn_mask, mems=mems_i)
                hids.append(core_out)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1210
                #  relu_outs.append(relu_out)
Zhilin Yang's avatar
init  
Zhilin Yang committed
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
        elif self.attn_type == 2: # absolute
            pos_seq = torch.arange(klen - 1, -1, -1.0, device=word_emb.device,
                                   dtype=word_emb.dtype)
            if self.clamp_len > 0:
                pos_seq.clamp_(max=self.clamp_len)
            pos_emb = self.pos_emb(pos_seq)

            core_out = self.drop(word_emb + pos_emb[-qlen:])

            hids.append(core_out)
            for i, layer in enumerate(self.layers):
                mems_i = None if mems is None else mems[i]
                if mems_i is not None and i == 0:
                    mems_i += pos_emb[:mlen]
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1225
1226
                #  core_out, relu_out = layer(core_out, dec_attn_mask=dec_attn_mask,
                core_out = layer(core_out, dec_attn_mask=dec_attn_mask,
Zhilin Yang's avatar
init  
Zhilin Yang committed
1227
1228
                                 mems=mems_i)
                hids.append(core_out)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1229
                #  relu_outs.append(relu_out)
Zhilin Yang's avatar
init  
Zhilin Yang committed
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
        elif self.attn_type == 3:
            core_out = self.drop(word_emb)

            hids.append(core_out)
            for i, layer in enumerate(self.layers):
                mems_i = None if mems is None else mems[i]
                if mems_i is not None and mlen > 0:
                    cur_emb = self.r_emb[i][:-qlen]
                    cur_size = cur_emb.size(0)
                    if cur_size < mlen:
                        cur_emb_pad = cur_emb[0:1].expand(mlen-cur_size, -1, -1)
                        cur_emb = torch.cat([cur_emb_pad, cur_emb], 0)
                    else:
                        cur_emb = cur_emb[-mlen:]
                    mems_i += cur_emb.view(mlen, 1, -1)
                core_out += self.r_emb[i][-qlen:].view(qlen, 1, -1)

Jiezhong Qiu's avatar
Jiezhong Qiu committed
1247
1248
                #  core_out, relu_out = layer(core_out, dec_attn_mask=dec_attn_mask,
                core_out = layer(core_out, dec_attn_mask=dec_attn_mask,
Zhilin Yang's avatar
init  
Zhilin Yang committed
1249
1250
                                 mems=mems_i)
                hids.append(core_out)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1251
                #  relu_outs.append(relu_out)
Zhilin Yang's avatar
init  
Zhilin Yang committed
1252
1253
1254
1255
1256

        core_out = self.drop(core_out)

        new_mems = self._update_mems(hids, mems, mlen, qlen)

Jiezhong Qiu's avatar
Jiezhong Qiu committed
1257
1258
        return core_out, new_mems
        #  return core_out, new_mems, relu_outs
Zhilin Yang's avatar
init  
Zhilin Yang committed
1259
1260
1261
1262
1263
1264
1265
1266
1267

    def forward(self, data, target, *mems):
        # nn.DataParallel does not allow size(0) tensors to be broadcasted.
        # So, have to initialize size(0) mems inside the model forward.
        # Moreover, have to return new_mems to allow nn.DataParallel to piece
        # them together.
        if not mems: mems = self.init_mems()

        tgt_len = target.size(0)
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1268
1269
        hidden, new_mems = self._forward(data, mems=mems)
        #  hidden, new_mems, relu_outs = self._forward(data, mems=mems)
1270
1271

        #  relu_outs = torch.cat([relu_out.unsqueeze(-1) for relu_out in relu_outs], dim=-1)
Zhilin Yang's avatar
init  
Zhilin Yang committed
1272
1273
1274
1275
1276
1277
1278
1279

        pred_hid = hidden[-tgt_len:]
        if self.sample_softmax > 0 and self.training:
            assert self.tie_weight
            logit = sample_logits(self.word_emb,
                self.out_layer.bias, target, pred_hid, self.sampler)
            loss = -F.log_softmax(logit, -1)[:, :, 0]
        else:
Jiezhong Qiu's avatar
fix  
Jiezhong Qiu committed
1280
            loss = self.crit(pred_hid.view(-1, pred_hid.size(-1)), target.contiguous().view(-1))
Zhilin Yang's avatar
init  
Zhilin Yang committed
1281
1282
1283
            loss = loss.view(tgt_len, -1)

        if new_mems is None:
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1284
1285
            return [loss]
            #  return [relu_outs, loss]
Zhilin Yang's avatar
init  
Zhilin Yang committed
1286
        else:
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1287
1288
            return [loss] + new_mems
            #  return [relu_outs, loss] + new_mems
Zhilin Yang's avatar
init  
Zhilin Yang committed
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='unit test')

    parser.add_argument('--n_layer', type=int, default=4, help='')
    parser.add_argument('--n_rel_layer', type=int, default=4, help='')
    parser.add_argument('--n_head', type=int, default=2, help='')
    parser.add_argument('--d_head', type=int, default=2, help='')
    parser.add_argument('--d_model', type=int, default=200, help='')
    parser.add_argument('--d_embed', type=int, default=200, help='')
    parser.add_argument('--d_inner', type=int, default=200, help='')
    parser.add_argument('--dropout', type=float, default=0.0, help='')
    parser.add_argument('--cuda', action='store_true', help='')
    parser.add_argument('--seed', type=int, default=1111, help='')
    parser.add_argument('--multi_gpu', action='store_true', help='')

    args = parser.parse_args()

    device = torch.device("cuda" if args.cuda else "cpu")

    B = 4
    tgt_len, mem_len, ext_len = 36, 36, 0
    data_len = tgt_len * 20
    args.n_token = 10000

    import data_utils

    data = torch.LongTensor(data_len*B).random_(0, args.n_token).to(device)
    diter = data_utils.LMOrderedIterator(data, B, tgt_len, device=device, ext_len=ext_len)

    cutoffs = [args.n_token // 2]
    tie_projs = [False] + [True] * len(cutoffs)

    for div_val in [1, 2]:
        for d_embed in [200, 100]:
            model = MemTransformerLM(args.n_token, args.n_layer, args.n_head,
                            args.d_model, args.d_head, args.d_inner, args.dropout,
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1328
1329
                            dropatt=args.dropout, tie_weight=True,
                            d_embed=d_embed, div_val=div_val,
Zhilin Yang's avatar
init  
Zhilin Yang committed
1330
                            tie_projs=tie_projs, pre_lnorm=True,
Jiezhong Qiu's avatar
Jiezhong Qiu committed
1331
                            tgt_len=tgt_len, ext_len=ext_len, mem_len=mem_len,
Zhilin Yang's avatar
init  
Zhilin Yang committed
1332
1333
1334
1335
1336
1337
1338
1339
1340
                            cutoffs=cutoffs, attn_type=0).to(device)

            print(sum(p.numel() for p in model.parameters()))

            mems = tuple()
            for idx, (inp, tgt, seqlen) in enumerate(diter):
                print('batch {}'.format(idx))
                out = model(inp, tgt, *mems)
                mems = out[1:]