-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
other.py
298 lines (234 loc) · 8.29 KB
/
other.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
289
290
291
292
293
294
295
296
297
298
# Copyright (c) 2019 NVIDIA Corporation
"""Core PyTorch-base Neural Modules"""
__all__ = ['SimpleCombiner',
'ArgMaxSimple',
'TableLookUp',
'TableLookUp2',
'SequenceEmbedding',
'SequenceProjection',
'ZerosLikeNM']
from typing import Iterable, Optional, Mapping, Set, Dict
import torch
import torch.nn as nn
from nemo.backends.pytorch.nm import TrainableNM
from nemo.core import NeuralModule
from nemo.core.neural_types import *
class SimpleCombiner(TrainableNM):
"""Performs simple combination of two NmTensors. For example, it can
perform x1 + x2.
Args:
mode (str): Can be ['add', 'sum', 'max'].
Defaults to 'add'.
"""
@staticmethod
def create_ports():
input_ports = {"x1": NeuralType({}), "x2": NeuralType({})}
output_ports = {"combined": None}
return input_ports, output_ports
def __init__(self, mode="add", **kwargs):
TrainableNM.__init__(self, **kwargs)
self._mode = mode
def forward(self, x1, x2):
if self._mode == "add" or self._mode == "sum":
return x1 + x2
elif self._mode == "max":
return torch.max(x1, x2, out=None)
else:
raise NotImplementedError(
"SimpleCombiner does not have {0} mode".format(self._mode)
)
class ArgMaxSimple(TrainableNM): # Notice TWO base classes
"""
"""
@staticmethod
def create_ports():
input_ports = {
"x": NeuralType({0: AxisType(BatchTag), 1: AxisType(ChannelTag)})
}
output_ports = {
"values": NeuralType({0: AxisType(BatchTag)}),
"indices": NeuralType({0: AxisType(BatchTag)}),
}
return input_ports, output_ports
def __init__(self, **kwargs):
TrainableNM.__init__(self, **kwargs)
# this method is key method you need to overwrite from PyTorch
# nn.Module's API
def forward(self, x):
values, indices = torch.max(x, 1)
return values, indices
class TableLookUp(NeuralModule):
"""Performs a table lookup. For example, convert class ids to names"""
def set_weights(self, name2weight: Dict[(str, bool)],
name2name_and_transform):
pass
def tie_weights_with(self, module, weight_names):
pass
def save_to(self, path):
pass
def restore_from(self, path):
pass
def freeze(self, weights: Set[str] = None):
pass
def unfreeze(self, weights: Set[str] = None):
pass
@staticmethod
def create_ports():
input_ports = {
"indices": NeuralType(
{0: AxisType(TimeTag), 1: AxisType(BatchTag)})
}
output_ports = {
"indices": NeuralType(
{0: AxisType(BatchTag), 1: AxisType(TimeTag)})
}
return input_ports, output_ports
def __init__(self, ids2classes=None, **kwargs):
NeuralModule.__init__(self, **kwargs)
if ids2classes is None:
ids2classes = {}
self._ids2classes = ids2classes
# self._input_ports = {"indices": NeuralType({0: AxisType(BatchTag)})}
def __call__(self, force_pt=False, *input, **kwargs):
pt_call = len(input) > 0 or force_pt
if pt_call:
# [inds] = kwargs.values()
# np_inds = inds.detach().cpu().numpy().reshape(-1)
# result = [self._ids2classes[i] for i in np_inds]
# #result = list(map(lambda x: self._ids2classes[x], np_inds))
# return result
inds = kwargs["indices"]
np_inds = inds.detach().transpose_(1, 0).cpu().numpy().tolist()
result = []
for lst in np_inds:
sublst = []
for tid in lst:
if tid != 1:
sublst.append(tid)
else:
break
result.append(
list(map(lambda x: self._ids2classes[x], sublst)))
return [result]
else:
return NeuralModule.__call__(self, **kwargs)
def parameters(self):
return None
def get_weights(self) -> Iterable[Optional[Mapping]]:
return None
class TableLookUp2(NeuralModule):
"""Performs a table lookup. For example, convert class ids to names"""
def set_weights(self, name2weight: Dict[(str, bool)],
name2name_and_transform):
pass
def tie_weights_with(self, module, weight_names):
pass
def save_to(self, path):
pass
def restore_from(self, path):
pass
def freeze(self, weights: Set[str] = None):
pass
def unfreeze(self, weights: Set[str] = None):
pass
@staticmethod
def create_ports():
input_ports = {
"indices": NeuralType(
{0: AxisType(BatchTag), 1: AxisType(TimeTag)})
}
output_ports = {"classes": None}
return input_ports, output_ports
def __init__(self, detokenizer=None, **kwargs):
NeuralModule.__init__(self, **kwargs)
# self._sp_decoder = self.local_parameters.get("sp_decoder", {})
self._detokenizer = detokenizer
def __call__(self, force_pt=False, *input, **kwargs):
pt_call = len(input) > 0 or force_pt
if pt_call:
# [inds] = kwargs.values()
inds = kwargs["indices"]
np_inds = inds.detach().cpu().numpy().tolist()
result = []
for lst in np_inds:
sublst = []
for tid in lst:
if tid != 1:
sublst.append(tid)
else:
break
result.append(self._detokenizer(sublst))
return result
else:
return NeuralModule.__call__(self, **kwargs)
def parameters(self):
return None
def get_weights(self) -> Iterable[Optional[Mapping]]:
return None
class SequenceEmbedding(TrainableNM):
@staticmethod
def create_ports():
input_ports = {
"input_seq": NeuralType(
{0: AxisType(TimeTag), 1: AxisType(BatchTag)})
}
output_ports = {
"outputs": NeuralType(
{0: AxisType(TimeTag), 1: AxisType(BatchTag),
2: AxisType(ChannelTag)}
)
}
return input_ports, output_ports
def __init__(self, *, voc_size, hidden_size, dropout=0.0, **kwargs):
TrainableNM.__init__(self, **kwargs)
self.voc_size = voc_size
self.hidden_size = hidden_size
self.dropout = dropout
self.embedding = nn.Embedding(self.voc_size, self.hidden_size)
if self.dropout != 0.0:
self.embedding_dropout = nn.Dropout(self.dropout)
def forward(self, input_seq):
embedded = self.embedding(input_seq)
if self.dropout != 0.0:
embedded = self.embedding_dropout(embedded)
return embedded
class SequenceProjection(TrainableNM):
@staticmethod
def create_ports():
input_ports = {"input_seq": NeuralType({})}
output_ports = {"outputs": None}
return input_ports, output_ports
def __init__(self, *, from_dim, to_dim, dropout=0.0, **kwargs):
TrainableNM.__init__(self, **kwargs)
self.from_dim = from_dim
self.to_dim = to_dim
self.dropout = dropout
self.projection = nn.Linear(self.from_dim, self.to_dim, bias=False)
if self.dropout != 0.0:
self.embedding_dropout = nn.Dropout(self.dropout)
def forward(self, input_seq):
p = self.projection(input_seq)
if self.dropout != 0.0:
p = self.dropout(p)
return p
class ZerosLikeNM(TrainableNM):
@staticmethod
def create_ports():
input_ports = {
"input_type_ids": NeuralType({
0: AxisType(BatchTag),
1: AxisType(TimeTag),
})
}
output_ports = {
"input_type_ids":
NeuralType({
0: AxisType(BatchTag),
1: AxisType(TimeTag),
})
}
return input_ports, output_ports
def __init__(self, **kwargs):
TrainableNM.__init__(self, **kwargs)
def forward(self, input_type_ids):
return torch.zeros_like(input_type_ids).long()