-
Notifications
You must be signed in to change notification settings - Fork 16
/
san_modules.py
185 lines (160 loc) · 7.15 KB
/
san_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
import torch
import torch.nn as nn
import torch.nn.functional as F
def _normalize(tensor, dim):
denom = tensor.norm(p=2.0, dim=dim, keepdim=True).clamp_min(1e-12)
return tensor / denom
class SANLinear(nn.Linear):
def __init__(self,
in_features,
out_features,
bias=True,
device=None,
dtype=None
):
super(SANLinear, self).__init__(
in_features, out_features, bias=bias, device=device, dtype=dtype)
scale = self.weight.norm(p=2.0, dim=1, keepdim=True).clamp_min(1e-12)
self.weight = nn.parameter.Parameter(self.weight / scale.expand_as(self.weight))
self.scale = nn.parameter.Parameter(scale.view(out_features))
if bias:
self.bias = nn.parameter.Parameter(torch.zeros(in_features, device=device, dtype=dtype))
else:
self.register_parameter('bias', None)
def forward(self, input, flg_train=False):
if self.bias is not None:
input = input + self.bias
normalized_weight = self._get_normalized_weight()
scale = self.scale
if flg_train:
out_fun = F.linear(input, normalized_weight.detach(), None)
out_dir = F.linear(input.detach(), normalized_weight, None)
out = [out_fun * scale, out_dir * scale.detach()]
else:
out = F.linear(input, normalized_weight, None)
out = out * scale
return out
@torch.no_grad()
def normalize_weight(self):
self.weight.data = self._get_normalized_weight()
def _get_normalized_weight(self):
return _normalize(self.weight, dim=1)
class SANConv1d(nn.Conv1d):
def __init__(self,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
dilation=1,
bias=True,
padding_mode='zeros',
device=None,
dtype=None
):
super(SANConv1d, self).__init__(
in_channels, out_channels, kernel_size, stride, padding=padding, dilation=dilation,
groups=1, bias=bias, padding_mode=padding_mode, device=device, dtype=dtype)
scale = self.weight.norm(p=2.0, dim=[1, 2], keepdim=True).clamp_min(1e-12)
self.weight = nn.parameter.Parameter(self.weight / scale.expand_as(self.weight))
self.scale = nn.parameter.Parameter(scale.view(out_channels))
if bias:
self.bias = nn.parameter.Parameter(torch.zeros(in_channels, device=device, dtype=dtype))
else:
self.register_parameter('bias', None)
def forward(self, input, flg_train=False):
if self.bias is not None:
input = input + self.bias.view(self.in_channels, 1)
normalized_weight = self._get_normalized_weight()
scale = self.scale.view(self.out_channels, 1)
if flg_train:
out_fun = F.conv1d(input, normalized_weight.detach(), None, self.stride,
self.padding, self.dilation, self.groups)
out_dir = F.conv1d(input.detach(), normalized_weight, None, self.stride,
self.padding, self.dilation, self.groups)
out = [out_fun * scale, out_dir * scale.detach()]
else:
out = F.conv1d(input, normalized_weight, None, self.stride,
self.padding, self.dilation, self.groups)
out = out * scale
return out
@torch.no_grad()
def normalize_weight(self):
self.weight.data = self._get_normalized_weight()
def _get_normalized_weight(self):
return _normalize(self.weight, dim=[1, 2])
class SANConv2d(nn.Conv2d):
def __init__(self,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
dilation=1,
bias=True,
padding_mode='zeros',
device=None,
dtype=None
):
super(SANConv2d, self).__init__(
in_channels, out_channels, kernel_size, stride, padding=padding, dilation=dilation,
groups=1, bias=bias, padding_mode=padding_mode, device=device, dtype=dtype)
scale = self.weight.norm(p=2.0, dim=[1, 2, 3], keepdim=True).clamp_min(1e-12)
self.weight = nn.parameter.Parameter(self.weight / scale.expand_as(self.weight))
self.scale = nn.parameter.Parameter(scale.view(out_channels))
if bias:
self.bias = nn.parameter.Parameter(torch.zeros(in_channels, device=device, dtype=dtype))
else:
self.register_parameter('bias', None)
def forward(self, input, flg_train=False):
if self.bias is not None:
input = input + self.bias.view(self.in_channels, 1, 1)
normalized_weight = self._get_normalized_weight()
scale = self.scale.view(self.out_channels, 1, 1)
if flg_train:
out_fun = F.conv2d(input, normalized_weight.detach(), None, self.stride,
self.padding, self.dilation, self.groups)
out_dir = F.conv2d(input.detach(), normalized_weight, None, self.stride,
self.padding, self.dilation, self.groups)
out = [out_fun * scale, out_dir * scale.detach()]
else:
out = F.conv2d(input, normalized_weight, None, self.stride,
self.padding, self.dilation, self.groups)
out = out * scale
return out
@torch.no_grad()
def normalize_weight(self):
self.weight.data = self._get_normalized_weight()
def _get_normalized_weight(self):
return _normalize(self.weight, dim=[1, 2, 3])
class SANEmbedding(nn.Embedding):
def __init__(self, num_embeddings, embedding_dim,
scale_grad_by_freq=False,
sparse=False, _weight=None,
device=None, dtype=None):
super(SANEmbedding, self).__init__(
num_embeddings, embedding_dim, padding_idx=None,
max_norm=None, norm_type=2., scale_grad_by_freq=scale_grad_by_freq,
sparse=sparse, _weight=_weight,
device=device, dtype=dtype)
scale = self.weight.norm(p=2.0, dim=1, keepdim=True).clamp_min(1e-12)
self.weight = nn.parameter.Parameter(self.weight / scale.expand_as(self.weight))
self.scale = nn.parameter.Parameter(scale)
def forward(self, input, flg_train=False):
out = F.embedding(
input, self.weight, self.padding_idx, self.max_norm,
self.norm_type, self.scale_grad_by_freq, self.sparse)
out = _normalize(out, dim=-1)
scale = F.embedding(
input, self.scale, self.padding_idx, self.max_norm,
self.norm_type, self.scale_grad_by_freq, self.sparse)
if flg_train:
out_fun = out.detach()
out_dir = out
out = [out_fun * scale, out_dir * scale.detach()]
else:
out = out * scale
return out
@torch.no_grad()
def normalize_weight(self):
self.weight.data = _normalize(self.weight, dim=1)