forked from yoniLc/ECCT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.py
173 lines (141 loc) · 5.9 KB
/
Model.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
"""
@author: Yoni Choukroun, [email protected]
Error Correction Code Transformer
https://arxiv.org/abs/2203.14966
"""
from torch.nn import LayerNorm
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import copy
import logging
from Codes import sign_to_bin
def clones(module, N):
return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])
class Encoder(nn.Module):
def __init__(self, layer, N):
super(Encoder, self).__init__()
self.layers = clones(layer, N)
self.norm = LayerNorm(layer.size)
if N > 1:
self.norm2 = LayerNorm(layer.size)
def forward(self, x, mask):
for idx, layer in enumerate(self.layers, start=1):
x = layer(x, mask)
if idx == len(self.layers)//2 and len(self.layers) > 1:
x = self.norm2(x)
return self.norm(x)
class SublayerConnection(nn.Module):
def __init__(self, size, dropout):
super(SublayerConnection, self).__init__()
self.norm = LayerNorm(size)
self.dropout = nn.Dropout(dropout)
def forward(self, x, sublayer):
return x + self.dropout(sublayer(self.norm(x)))
class EncoderLayer(nn.Module):
def __init__(self, size, self_attn, feed_forward, dropout):
super(EncoderLayer, self).__init__()
self.self_attn = self_attn
self.feed_forward = feed_forward
self.sublayer = clones(SublayerConnection(size, dropout), 2)
self.size = size
def forward(self, x, mask):
x = self.sublayer[0](x, lambda x: self.self_attn(x, x, x, mask))
return self.sublayer[1](x, self.feed_forward)
class MultiHeadedAttention(nn.Module):
def __init__(self, h, d_model, dropout=0.1):
super(MultiHeadedAttention, self).__init__()
assert d_model % h == 0
self.d_k = d_model // h
self.h = h
self.linears = clones(nn.Linear(d_model, d_model), 4)
self.attn = None
self.dropout = nn.Dropout(p=dropout)
def forward(self, query, key, value, mask=None):
nbatches = query.size(0)
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))]
x, self.attn = self.attention(query, key, value, mask=mask)
x = x.transpose(1, 2).contiguous() \
.view(nbatches, -1, self.h * self.d_k)
return self.linears[-1](x)
def attention(self, query, key, value, mask=None):
d_k = query.size(-1)
scores = torch.matmul(query, key.transpose(-2, -1)) \
/ math.sqrt(d_k)
if mask is not None:
scores = scores.masked_fill(mask, -1e9)
p_attn = F.softmax(scores, dim=-1)
if self.dropout is not None:
p_attn = self.dropout(p_attn)
return torch.matmul(p_attn, value), p_attn
class PositionwiseFeedForward(nn.Module):
def __init__(self, d_model, d_ff, dropout=0):
super(PositionwiseFeedForward, self).__init__()
self.w_1 = nn.Linear(d_model, d_ff)
self.w_2 = nn.Linear(d_ff, d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
return self.w_2(self.dropout(F.gelu(self.w_1(x))))
############################################################
class ECC_Transformer(nn.Module):
def __init__(self, args, dropout=0):
super(ECC_Transformer, self).__init__()
####
code = args.code
c = copy.deepcopy
attn = MultiHeadedAttention(args.h, args.d_model)
ff = PositionwiseFeedForward(args.d_model, args.d_model*4, dropout)
self.src_embed = torch.nn.Parameter(torch.empty(
(code.n + code.pc_matrix.size(0), args.d_model)))
self.decoder = Encoder(EncoderLayer(
args.d_model, c(attn), c(ff), dropout), args.N_dec)
self.oned_final_embed = torch.nn.Sequential(
*[nn.Linear(args.d_model, 1)])
self.out_fc = nn.Linear(code.n + code.pc_matrix.size(0), code.n)
self.get_mask(code)
logging.info(f'Mask:\n {self.src_mask}')
###
for p in self.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
def forward(self, magnitude, syndrome):
emb = torch.cat([magnitude, syndrome], -1).unsqueeze(-1)
emb = self.src_embed.unsqueeze(0) * emb
emb = self.decoder(emb, self.src_mask)
return self.out_fc(self.oned_final_embed(emb).squeeze(-1))
def loss(self, z_pred, z2, y):
loss = F.binary_cross_entropy_with_logits(
z_pred, sign_to_bin(torch.sign(z2)))
x_pred = sign_to_bin(torch.sign(-z_pred * torch.sign(y)))
return loss, x_pred
def get_mask(self, code, no_mask=False):
if no_mask:
self.src_mask = None
return
def build_mask(code):
mask_size = code.n + code.pc_matrix.size(0)
mask = torch.eye(mask_size, mask_size)
for ii in range(code.pc_matrix.size(0)):
idx = torch.where(code.pc_matrix[ii] > 0)[0]
for jj in idx:
for kk in idx:
if jj != kk:
mask[jj, kk] += 1
mask[kk, jj] += 1
mask[code.n + ii, jj] += 1
mask[jj, code.n + ii] += 1
src_mask = ~ (mask > 0).unsqueeze(0).unsqueeze(0)
return src_mask
src_mask = build_mask(code)
mask_size = code.n + code.pc_matrix.size(0)
a = mask_size ** 2
logging.info(
f'Self-Attention Sparsity Ratio={100 * torch.sum((src_mask).int()) / a:0.2f}%, Self-Attention Complexity Ratio={100 * torch.sum((~src_mask).int())//2 / a:0.2f}%')
self.register_buffer('src_mask', src_mask)
############################################################
############################################################
if __name__ == '__main__':
pass