-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta_module.py
599 lines (472 loc) · 20.6 KB
/
meta_module.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import torchvision
from torch.autograd import Variable
import itertools
import operator
from itertools import islice
from collections import OrderedDict
import collections.abc as container_abcs
def to_var(x, requires_grad=True):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x, requires_grad=requires_grad)
class MetaModule(nn.Module):
# adopted from: Adrien Ecoffet https://github.com/AdrienLE
def parameters(self, recurse=False):
for name, param in self.named_params(self):
yield param
def named_parameters(self, recurse=False):
for name, param in self.named_params(self):
yield name, param
def named_leaves(self):
return []
def named_submodules(self):
return []
def named_params(self, curr_module=None, memo=None, prefix=''):
if memo is None:
memo = set()
if hasattr(curr_module, 'named_leaves'):
for name, p in curr_module.named_leaves():
if p is not None and p not in memo:
memo.add(p)
yield prefix + ('.' if prefix else '') + name, p
else:
for name, p in curr_module._parameters.items():
if p is not None and p not in memo:
memo.add(p)
yield prefix + ('.' if prefix else '') + name, p
for mname, module in curr_module.named_children():
submodule_prefix = prefix + ('.' if prefix else '') + mname
for name, p in self.named_params(module, memo, submodule_prefix):
yield name, p
def update_params(self, lr_inner, first_order=False, source_params=None, detach=False):
if source_params is not None:
for tgt, src in zip(self.named_params(self), source_params):
name_t, param_t = tgt
# name_s, param_s = src
# grad = param_s.grad
# name_s, param_s = src
grad = src
if first_order:
grad = to_var(grad.detach().data)
tmp = param_t - lr_inner * grad
self.set_param(self, name_t, tmp)
else:
for name, param in self.named_params(self):
if not detach:
grad = param.grad
if first_order:
grad = to_var(grad.detach().data)
tmp = param - lr_inner * grad
self.set_param(self, name, tmp)
else:
param = param.detach_()
self.set_param(self, name, param)
def set_param(self,curr_mod, name, param):
if '.' in name:
n = name.split('.')
module_name = n[0]
rest = '.'.join(n[1:])
for name, mod in curr_mod.named_children():
if module_name == name:
self.set_param(mod, rest, param)
break
else:
setattr(curr_mod, name, param)
def detach_params(self):
for name, param in self.named_params(self):
self.set_param(self, name, param.detach())
def copy(self, other, same_var=False):
for name, param in other.named_params():
if not same_var:
param = to_var(param.data.clone(), requires_grad=True)
self.set_param(name, param)
class MetaLinear(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.Linear(*args, **kwargs)
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
if ignore.bias is not None:
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
else:
self.register_buffer('bias', None)
self.in_features = ignore.weight.size(1)
self.out_features = ignore.weight.size(0)
def forward(self, x):
return F.linear(x, self.weight, self.bias)
def named_leaves(self):
if self.bias is not None:
return [('weight', self.weight), ('bias', self.bias)]
else:
return [('weight', self.weight)]
class MetaConv2d(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.Conv2d(*args, **kwargs)
self.stride = ignore.stride
self.padding = ignore.padding
self.dilation = ignore.dilation
self.groups = ignore.groups
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
if ignore.bias is not None:
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
else:
self.register_buffer('bias', None)
def forward(self, x):
return F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class MetaConvTranspose2d(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.ConvTranspose2d(*args, **kwargs)
self.stride = ignore.stride
self.padding = ignore.padding
self.dilation = ignore.dilation
self.groups = ignore.groups
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
if ignore.bias is not None:
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
else:
self.register_buffer('bias', None)
def forward(self, x, output_size=None):
output_padding = self._output_padding(x, output_size)
return F.conv_transpose2d(x, self.weight, self.bias, self.stride, self.padding,
output_padding, self.groups, self.dilation)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class MetaBatchNorm2d(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.BatchNorm2d(*args, **kwargs)
self.num_features = ignore.num_features
self.eps = ignore.eps
self.momentum = ignore.momentum
self.affine = ignore.affine
self.track_running_stats = ignore.track_running_stats
if self.affine:
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
#if self.track_running_stats:
if True:
self.register_buffer('running_mean', torch.zeros(self.num_features))
self.register_buffer('running_var', torch.ones(self.num_features))
self.register_buffer('num_batches_tracked', torch.zeros([]))
else:
self.register_parameter('running_mean', None)
self.register_parameter('running_var', None)
def forward(self, x):
return F.batch_norm(x, self.running_mean, self.running_var, self.weight, self.bias,
self.training or not self.track_running_stats, self.momentum, self.eps)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class MetaLayerNorm(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.LayerNorm(*args, **kwargs)
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
self.eps = ignore.eps
self.normalized_shape = ignore.normalized_shape
def forward(self, x):
return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class MetaSequential(MetaModule):
r"""A sequential container.
Modules will be added to it in the order they are passed in the constructor.
Alternatively, an ordered dict of modules can also be passed in.
To make it easier to understand, here is a small example::
# Example of using Sequential
model = MetaSequential(
MetaConv2d(1,20,5),
nn.ReLU(),
MetaConv2d(20,64,5),
nn.ReLU()
)
# Example of using Sequential with OrderedDict
model = MetaSequential(OrderedDict([
('conv1', MetaConv2d(1,20,5)),
('relu1', nn.ReLU()),
('conv2', MetaConv2d(20,64,5)),
('relu2', nn.ReLU())
]))
"""
def __init__(self, *args):
super(MetaSequential, self).__init__()
if len(args) == 1 and isinstance(args[0], OrderedDict):
for key, module in args[0].items():
self.add_module(key, module)
else:
for idx, module in enumerate(args):
self.add_module(str(idx), module)
def _get_item_by_idx(self, iterator, idx):
"""Get the idx-th item of the iterator"""
size = len(self)
idx = operator.index(idx)
if not -size <= idx < size:
raise IndexError('index {} is out of range'.format(idx))
idx %= size
return next(islice(iterator, idx, None))
def __getitem__(self, idx):
if isinstance(idx, slice):
return self.__class__(OrderedDict(list(self._modules.items())[idx]))
else:
return self._get_item_by_idx(self._modules.values(), idx)
def __setitem__(self, idx, module):
key = self._get_item_by_idx(self._modules.keys(), idx)
return setattr(self, key, module)
def __delitem__(self, idx):
if isinstance(idx, slice):
for key in list(self._modules.keys())[idx]:
delattr(self, key)
else:
key = self._get_item_by_idx(self._modules.keys(), idx)
delattr(self, key)
def __len__(self):
return len(self._modules)
def __dir__(self):
keys = super(Sequential, self).__dir__()
keys = [key for key in keys if not key.isdigit()]
return keys
def forward(self, input):
for module in self._modules.values():
input = module(input)
return input
class MetaModuleList(MetaModule):
r"""Holds submodules in a list.
:class:`~MetaModuleList` can be indexed like a regular Python list, but
modules it contains are properly registered, and will be visible by all
:class:`~MetaModule` methods.
Arguments:
modules (iterable, optional): an iterable of modules to add
Example::
class MyModule(MetaModule):
def __init__(self):
super(MyModule, self).__init__()
self.linears = MetaModuleList([MetaLinear(10, 10) for i in range(10)])
def forward(self, x):
# ModuleList can act as an iterable, or be indexed using ints
for i, l in enumerate(self.linears):
x = self.linears[i // 2](x) + l(x)
return x
"""
def __init__(self, modules=None):
super(MetaModuleList, self).__init__()
if modules is not None:
self += modules
def _get_abs_string_index(self, idx):
"""Get the absolute index for the list of modules"""
idx = operator.index(idx)
if not (-len(self) <= idx < len(self)):
raise IndexError('index {} is out of range'.format(idx))
if idx < 0:
idx += len(self)
return str(idx)
def __getitem__(self, idx):
if isinstance(idx, slice):
return self.__class__(list(self._modules.values())[idx])
else:
return self._modules[self._get_abs_string_index(idx)]
def __setitem__(self, idx, module):
idx = self._get_abs_string_index(idx)
return setattr(self, str(idx), module)
def __delitem__(self, idx):
if isinstance(idx, slice):
for k in range(len(self._modules))[idx]:
delattr(self, str(k))
else:
delattr(self, self._get_abs_string_index(idx))
# To preserve numbering, self._modules is being reconstructed with modules after deletion
str_indices = [str(i) for i in range(len(self._modules))]
self._modules = OrderedDict(list(zip(str_indices, self._modules.values())))
def __len__(self):
return len(self._modules)
def __iter__(self):
return iter(self._modules.values())
def __iadd__(self, modules):
return self.extend(modules)
def __dir__(self):
keys = super(ModuleList, self).__dir__()
keys = [key for key in keys if not key.isdigit()]
return keys
def insert(self, index, module):
r"""Insert a given module before a given index in the list.
Arguments:
index (int): index to insert.
module (MetaModule): module to insert
"""
for i in range(len(self._modules), index, -1):
self._modules[str(i)] = self._modules[str(i - 1)]
self._modules[str(index)] = module
def append(self, module):
r"""Appends a given module to the end of the list.
Arguments:
module (MetaModule): module to append
"""
self.add_module(str(len(self)), module)
return self
def extend(self, modules):
r"""Appends modules from a Python iterable to the end of the list.
Arguments:
modules (iterable): iterable of modules to append
"""
if not isinstance(modules, container_abcs.Iterable):
raise TypeError("ModuleList.extend should be called with an "
"iterable, but got " + type(modules).__name__)
offset = len(self)
for i, module in enumerate(modules):
self.add_module(str(offset + i), module)
return self
class MetaEmbedding(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.Embedding(*args, **kwargs)
self.num_embeddings = ignore.num_embeddings
self.embedding_dim = ignore.embedding_dim
if ignore.padding_idx is not None:
if ignore.padding_idx > 0:
assert ignore.padding_idx < self.num_embeddings, 'ignore.Padding_idx must be within num_embeddings'
elif ignore.padding_idx < 0:
assert ignore.padding_idx >= -self.num_embeddings, 'ignore.Padding_idx must be within num_embeddings'
ignore.padding_idx = self.num_embeddings + ignore.padding_idx
self.padding_idx = ignore.padding_idx
self.max_norm = ignore.max_norm
self.norm_type = ignore.norm_type
self.scale_grad_by_freq = ignore.scale_grad_by_freq
self.register_buffer('weight', to_var(torch.empty(self.num_embeddings, self.embedding_dim), requires_grad=True))
def forward(self, x):
return F.embedding(x, self.weight)
def named_leaves(self):
return [('weight', self.weight)]
class MetaModuleDict(MetaModule):
r"""Holds submodules in a dictionary.
:class:`~MetaModuleDict` can be indexed like a regular Python dictionary,
but modules it contains are properly registered, and will be visible by all
:class:`~MetaModule` methods.
:class:`~MetaModuleDict` is an **ordered** dictionary that respects
* the order of insertion, and
* in :meth:`~MetaModuleDict.update`, the order of the merged ``OrderedDict``
or another :class:`~MetaModuleDict` (the argument to :meth:`~MetaModuleDict.update`).
Note that :meth:`~MetaModuleDict.update` with other unordered mapping
types (e.g., Python's plain ``dict``) does not preserve the order of the
merged mapping.
Arguments:
modules (iterable, optional): a mapping (dictionary) of (string: module)
or an iterable of key-value pairs of type (string, module)
Example::
class MyModule(MetaModule):
def __init__(self):
super(MyModule, self).__init__()
self.choices = MetaModuleDict({
'conv': MetaConv2d(10, 10, 3),
'pool': nn.MaxPool2d(3)
})
self.activations = MetaModuleDict([
['lrelu', nn.LeakyReLU()],
['prelu', nn.PReLU()]
])
def forward(self, x, choice, act):
x = self.choices[choice](x)
x = self.activations[act](x)
return x
"""
def __init__(self, modules=None):
super(MetaModuleDict, self).__init__()
if modules is not None:
self.update(modules)
def __getitem__(self, key):
return self._modules[key]
def __setitem__(self, key, module):
self.add_module(key, module)
def __delitem__(self, key):
del self._modules[key]
def __len__(self):
return len(self._modules)
def __iter__(self):
return iter(self._modules)
def __contains__(self, key):
return key in self._modules
def clear(self):
"""Remove all items from the ModuleDict.
"""
self._modules.clear()
def pop(self, key):
r"""Remove key from the ModuleDict and return its module.
Arguments:
key (string): key to pop from the ModuleDict
"""
v = self[key]
del self[key]
return v
def keys(self):
r"""Return an iterable of the ModuleDict keys.
"""
return self._modules.keys()
def items(self):
r"""Return an iterable of the ModuleDict key/value pairs.
"""
return self._modules.items()
def values(self):
r"""Return an iterable of the ModuleDict values.
"""
return self._modules.values()
def update(self, modules):
r"""Update the :class:`~MetaModuleDict` with the key-value pairs from a
mapping or an iterable, overwriting existing keys.
.. note::
If :attr:`modules` is an ``OrderedDict``, a :class:`~MetaModuleDict`, or
an iterable of key-value pairs, the order of new elements in it is preserved.
Arguments:
modules (iterable): a mapping (dictionary) from string to :class:`~MetaModule`,
or an iterable of key-value pairs of type (string, :class:`~MetaModule`)
"""
if not isinstance(modules, container_abcs.Iterable):
raise TypeError("ModuleDict.update should be called with an "
"iterable of key/value pairs, but got " +
type(modules).__name__)
if isinstance(modules, container_abcs.Mapping):
if isinstance(modules, (OrderedDict, ModuleDict)):
for key, module in modules.items():
self[key] = module
else:
for key, module in sorted(modules.items()):
self[key] = module
else:
for j, m in enumerate(modules):
if not isinstance(m, container_abcs.Iterable):
raise TypeError("ModuleDict update sequence element "
"#" + str(j) + " should be Iterable; is" +
type(m).__name__)
if not len(m) == 2:
raise ValueError("ModuleDict update sequence element "
"#" + str(j) + " has length " + str(len(m)) +
"; 2 is required")
self[m[0]] = m[1]
def forward(self):
raise NotImplementedError()
class LeNet(MetaModule):
def __init__(self, n_out):
super(LeNet, self).__init__()
layers = []
layers.append(MetaConv2d(1, 6, kernel_size=5))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.MaxPool2d(kernel_size=2,stride=2))
layers.append(MetaConv2d(6, 16, kernel_size=5))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.MaxPool2d(kernel_size=2,stride=2))
layers.append(MetaConv2d(16, 120, kernel_size=5))
layers.append(nn.ReLU(inplace=True))
self.main = nn.Sequential(*layers)
layers = []
layers.append(MetaLinear(120, 84))
layers.append(nn.ReLU(inplace=True))
layers.append(MetaLinear(84, n_out))
self.fc_layers = nn.Sequential(*layers)
def forward(self, x):
x = self.main(x)
x = x.view(-1, 120)
return self.fc_layers(x).squeeze()