/
common.py
314 lines (240 loc) · 8.97 KB
/
common.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import copy
import types
import logging
import inspect
import torch
import torch.nn as nn
import torch.nn.init as init
from . import utils
def unsqueeze_expand_dim(x, dim, k):
dims = x.size()
return x.unsqueeze(dim).expand(*dims[:dim], k, *dims[dim:])
def expand_match(a, b, dim=0):
size = [-1] * len(b.size())
size[dim] = b.size(dim)
return a.unsqueeze(dim).expand(*size)
def resolve_cls(name, module):
if inspect.isclass(name):
return name
else:
return utils.resolve_obj(module, name)
def recursively_reset_parameters(parent):
for module in parent.children():
if hasattr(module, "reset_parameters"):
module.reset_parameters()
class Sequential(nn.Sequential):
def reset_parameters(self):
recursively_reset_parameters(self)
class ModuleList(nn.ModuleList):
def reset_parameters(self):
recursively_reset_parameters(self)
class Linear(nn.Linear):
def reset_parameters(self):
init.xavier_normal_(self.weight.detach())
if self.bias is not None:
self.bias.detach().zero_()
class ArgumentDictionary(dict):
@staticmethod
def from_namespace(namespace):
return ArgumentDictionary(vars(namespace))
def filter(self, key):
return ArgumentDictionary({k[len(key) + 1:]: v for k, v in self.items()
if k.startswith(key) and k != key})
class InitializerCreator(object):
def __init__(self, pkg):
self.pkg = pkg
self.cls = None
self.kwargs = dict()
def initialize(self, *args, **kwargs):
assert self.cls is not None
return self.cls(*args, **kwargs, **self.kwargs)
def __call__(self, argdict):
if isinstance(argdict, str):
name, kwargs = argdict, {}
else:
name, kwargs = argdict.get("type"), argdict.get("vargs", {})
parent_modname = ".".join(__name__.split(".")[:-1])
manager_modname = f"{parent_modname}.manager"
manager = utils.import_module(manager_modname)
self.cls = manager.resolve(name, self.pkg)
assert self.cls is not None, \
f"module type not found in {self.pkg.__name__}: {name}"
self.kwargs = self.cls.process_argdict(kwargs)
return self.initialize
def __repr__(self):
return f"<InitializerCreator for Modules in '{self.pkg}'>"
def is_module_cls(x):
return inspect.isclass(x) and issubclass(x, Module)
def get_caster(value):
if is_module_cls(value):
cls = value
return InitializerCreator(cls.get_package())
else:
return type(value)
class OptionalArgument(object):
def __init__(self, name, caster=str, default=None, islist=False):
assert caster is not None
self.name = name
self.caster = caster
self.islist = islist
self.default = default
def __call__(self, value=None):
"""consumes any acceptable form of value and
returns typed value compatible with the argument
"""
if value is None:
return self.default
if self.islist:
if not isinstance(value, (list, tuple)):
values = [value]
else:
values = value
return [copy.copy(self.caster)(value) for value in values]
else:
return self.caster(value)
def nullable_add(a, b):
if a is None:
return b
if b is None:
return a
return a + b
class Module(nn.Module):
r"""A base module class for managing module parameters.
All module classes whose initializing parameters need to be managed must
inherit this class.
The rule of thumb is that positional arguments are only reserved for
abstract classes whose arguments must be bare minimum for defining forward
behavior; while optional keyword arguments are used for defining
implementation specific parameters. An example of such parameters would be
the number of layers to use in a feed forward network, as the number of
hidden layers does not affect the dimensions of input and output tensors.
"""
name = None
def __init__(self, *args, **kwargs):
super(Module, self).__init__()
if args or kwargs:
logging.warning(f"module '{self.__class__}' received unexpected "
f"keyword arguments: {(args, kwargs)}")
self.loss = None
@classmethod
def get_package(cls):
pkg_name = cls.__module__
return utils.import_module(pkg_name)
@classmethod
def get_optargdefaults(cls, parents=True):
optargs = cls.get_optargs(parents)
return {name: optarg.default for name, optarg in optargs.items()}
@classmethod
def get_optargs(cls, parents=True):
"""module optional arguments"""
if cls == Module:
return dict()
optargs = {}
kwonly = inspect.getfullargspec(cls.__init__).kwonlydefaults
kwonly = dict() if kwonly is None else kwonly
for name, default in kwonly.items():
islist = False
caster = get_caster(default)
if isinstance(default, (list, tuple)):
assert len(default) > 0, \
"list arguments must contain at least one element"
caster = get_caster(default[0])
islist = True
arg = OptionalArgument(
name=name,
caster=caster,
default=default,
islist=islist
)
optargs[arg.name] = arg
if parents:
for base in cls.__bases__:
if not issubclass(base, Module):
continue
optargs.update(base.get_optargs(True))
return optargs
@classmethod
def process_argdict(cls, argdict: dict):
optargs = cls.get_optargs()
return {
name: optarg(argdict.get(name))
for name, optarg in optargs.items()
}
def reset_parameters(self):
recursively_reset_parameters(self)
class MultiModule(Module):
"""A base class for implementing modules that return multiple values
This extension allows modules to generate losses in addition to the
standard forward-returns. All modules will automatically cumulate loss
items from their own children and return the total loss to their calling
modules. Ordinary modules cannot call MultiModule but the reverse is
possible. Implement `forward_multi` instead of the standard `forward`
method.
Take a note of the following example:
import torch.nn as nn
from torchmodels import MultiModule
class RegularizedLinearModule(MultiModule):
def __init__(self, *args, **kwargs):
super(FooModule, self).__init__(*args, **kwargs)
self.linear = nn.Linear(100, 200)
def forward_multi(self, x):
yield "pass", self.linear(x)
# l2-norm loss
yield "loss", (self.linear.weight ** 2).sum()
Use generator syntax to generate different types of values (namely
"inference" and "auxiliary losses"). As seen in the example above, when
trying to return the forward pass results, yield a tuple of `"pass"` and
the forward pass results. When trying to return an auxiliary loss, yield
a tuple of `"loss"` and the scalar loss value. On `forward` call, the base
module class will aggregate of results of the same types from
`forward_multi` and return a `dict` containing the respective aggregated
values.
"""
def invoke(self, module, *args):
ret = module(*args)
if isinstance(ret, dict):
loss = ret.get("loss")
if loss is not None:
if self.loss is not None:
self.loss += loss
else:
self.loss = loss
ret = ret.get("pass")
return ret
def forward_multi(self, *input):
# yield "loss", 0
# yield "pass", 0
# return <pass>
raise NotImplementedError()
def forward(self, *input):
self.loss = None
ret = self.forward_multi(*input)
if isinstance(ret, types.GeneratorType):
ret = dict(ret)
pss = ret.get("pass")
loss = ret.get("loss")
else:
pss = ret
loss = None
if torch.is_grad_enabled() and \
(loss is not None or self.loss is not None):
return {
"pass": pss,
"loss": nullable_add(loss, self.loss)
}
else:
return {"pass": pss}
class Identity(Module):
def __init__(self):
super(Identity, self).__init__()
def forward(self, x):
return x
class Concat(Module):
def __init__(self, dim=1):
super(Concat, self).__init__()
self.dim = dim
def forward(self, *xs):
return torch.cat(xs, self.dim)
class Parameter(nn.Parameter):
def reset_parameters(self):
self.data.detach().zero_()