Repository navigation
Expand file tree
/
Copy pathpolicy.py
More file actions
795 lines (681 loc) · 40.2 KB
/
Copy pathpolicy.py
File metadata and controls
795 lines (681 loc) · 40.2 KB
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
#
# Copyright (c) 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Policies for scheduling by a CompressionScheduler instance.
- PruningPolicy: prunning policy
- RegularizationPolicy: regulization scheduling
- LRPolicy: learning-rate decay scheduling
- QuantizationPolicy: quantization scheduling
"""
import torch
import torch.nn as nn
import torch.optim.lr_scheduler
from collections import namedtuple, OrderedDict
import logging
import distiller
#import summary_graph as sg
#import utility
__all__ = ['PruningPolicy', 'RegularizationPolicy', 'QuantizationPolicy', 'LRPolicy', 'ScheduledTrainingPolicy',
'PolicyLoss', 'LossComponent', 'ADMMPolicy', 'FMRPolicy']
msglogger = logging.getLogger()
PolicyLoss = namedtuple('PolicyLoss', ['overall_loss', 'loss_components'])
LossComponent = namedtuple('LossComponent', ['name', 'value'])
def param_name_2_module_name(param_name):
return '.'.join(param_name.split('.')[:-1])
class ScheduledTrainingPolicy(object):
""" Base class for all scheduled training policies.
The CompressionScheduler invokes these methods as the training progresses.
"""
def __init__(self, classes=None, layers=None):
self.classes = classes
self.layers = layers
def on_epoch_begin(self, model, zeros_mask_dict, meta, **kwargs):
"""A new epcoh is about to begin"""
pass
def on_minibatch_begin(self, model, epoch, minibatch_id, minibatches_per_epoch,
zeros_mask_dict, meta, optimizer=None):
"""The forward-pass of a new mini-batch is about to begin"""
pass
def before_backward_pass(self, model, epoch, minibatch_id, minibatches_per_epoch, loss, zeros_mask_dict,
optimizer=None):
"""The mini-batch training pass has completed the forward-pass,
and is about to begin the backward pass.
This callback receives a 'loss' argument. The callback should not modify this argument, but it can
optionally return an instance of 'PolicyLoss' which will be used in place of `loss'.
Note: The 'loss_components' parameter within 'PolicyLoss' should contain any new, individual loss components
the callback contributed to 'overall_loss'. It should not contain the incoming 'loss' argument.
"""
pass
def before_parameter_optimization(self, model, epoch, minibatch_id, minibatches_per_epoch,
zeros_mask_dict, meta, optimizer):
"""The mini-batch training pass has completed the backward-pass,
and the optimizer is about to update the weights."""
pass
def on_minibatch_end(self, model, epoch, minibatch_id, minibatches_per_epoch, zeros_mask_dict, optimizer):
"""The mini-batch training pass has ended"""
pass
def on_epoch_end(self, model, zeros_mask_dict, meta, **kwargs):
"""The current epoch has ended"""
pass
class PruningPolicy(ScheduledTrainingPolicy):
"""
Base class for pruning policies.
"""
def __init__(self, pruner, pruner_args, classes=None, layers=None):
"""
Arguments:
mask_on_forward_only: controls what we do after the weights are updated by the backward pass.
In issue #53 (https://github.com/NervanaSystems/distiller/issues/53) we explain why in some
cases masked weights will be updated to a non-zero value, even if their gradients are masked
(e.g. when using SGD with momentum). Therefore, to circumvent this weights-update performed by
the backward pass, we usually mask the weights again - right after the backward pass. To
disable this masking set:
pruner_args['mask_on_forward_only'] = False
use_double_copies: when set to `True`, two sets of weights are used. In the forward-pass we use
masked weights to compute the loss, but in the backward-pass we update the unmasked weights (using
gradients computed from the masked-weights loss).
mini_batch_pruning_frequency: this controls pruning scheduling at the mini-batch granularity. Every
mini_batch_pruning_frequency training steps (i.e. mini_batches) we perform pruning. This provides more
fine-grained control over pruning than that provided by CompressionScheduler (epoch granularity).
When setting 'mini_batch_pruning_frequency' to a value other than zero, make sure to configure the policy's
schedule to once-every-epoch.
fold_batchnorm: when set to `True`, the weights of BatchNorm modules are folded into the the weights of
Conv-2D modules (if Conv2D->BN edges exist in the model graph). Each weights filter is attenuated using
a different pair of (gamma, beta) coefficients, so `fold_batchnorm` is relevant for fine-grained and
filter-ranking pruning methods. We attenuate using the running values of the mean and variance, as is
done in quantization.
This control argument is only supported for Conv-2D modules (i.e. other convolution operation variants and
Linear operations are not supported).
"""
super(PruningPolicy, self).__init__(classes, layers)
self.pruner = pruner
# Copy external policy configuration, if available
if pruner_args is None:
pruner_args = {}
self.levels = pruner_args.get('levels', None)
self.keep_mask = pruner_args.get('keep_mask', False)
self.mini_batch_pruning_frequency = pruner_args.get('mini_batch_pruning_frequency', 0)
self.mask_on_forward_only = pruner_args.get('mask_on_forward_only', False)
self.mask_gradients = pruner_args.get('mask_gradients', False)
if self.mask_gradients and not self.mask_on_forward_only:
raise ValueError("mask_gradients and (not mask_on_forward_only) are mutually exclusive")
self.backward_hook_handle = None # The backward-callback handle
self.use_double_copies = pruner_args.get('use_double_copies', False)
self.discard_masks_at_minibatch_end = pruner_args.get('discard_masks_at_minibatch_end', False)
self.skip_first_minibatch = pruner_args.get('skip_first_minibatch', False)
self.fold_bn = pruner_args.get('fold_batchnorm', False)
# These are required for BN-folding. We cache them to improve performance
self.named_modules = None
self.sg = None
# Initialize state
self.is_last_epoch = False
self.is_initialized = False
@staticmethod
def _fold_batchnorm(model, param_name, param, named_modules, sg):
def _get_all_parameters(param_module, bn_module):
w, b, gamma, beta = param_module.weight, param_module.bias, bn_module.weight, bn_module.bias
if not bn_module.affine:
gamma = 1.
beta = 0.
return w, b, gamma, beta
def get_bn_folded_weights(conv_module, bn_module):
"""Compute the weights of `conv_module` after folding successor BN layer.
In inference, DL frameworks and graph-compilers fold the batch normalization into
the weights as defined by equations 20 and 21 of https://arxiv.org/pdf/1806.08342.pdf
:param conv_module: nn.Conv2d module
:param bn_module: nn.BatchNorm2d module which succeeds `conv_module`
:return: Folded weights
"""
w, b, gamma, beta = _get_all_parameters(conv_module, bn_module)
with torch.no_grad():
sigma_running = torch.sqrt(bn_module.running_var + bn_module.eps)
w_corrected = w * (gamma / sigma_running).view(-1, 1, 1, 1)
return w_corrected
layer_name = utility.param_name_2_module_name(param_name)
#layer_name = param_name_2_module_name(param_name)
if not isinstance(named_modules[layer_name], nn.Conv2d):
return param
bn_layers = sg.successors_f(layer_name, ['BatchNormalization'])
if bn_layers:
assert len(bn_layers) == 1
bn_module = named_modules[bn_layers[0]]
conv_module = named_modules[layer_name]
param = get_bn_folded_weights(conv_module, bn_module)
return param
def on_epoch_begin(self, model, zeros_mask_dict, meta, **kwargs):
msglogger.debug("Pruner {} is about to prune".format(self.pruner.name))
#print("Pruner {} is about to prune".format(self.pruner.name))
self.is_last_epoch = meta['current_epoch'] == (meta['ending_epoch'] - 1)
if self.levels is not None:
self.pruner.levels = self.levels
meta['model'] = model
is_initialized = self.is_initialized
if self.fold_bn:
# Cache this information (required for BN-folding) to improve performance
self.named_modules = OrderedDict(model.named_modules())
dummy_input = torch.randn(model.input_shape)
self.sg = sg.SummaryGraph(model, dummy_input)
for param_name, param in model.named_parameters():
if self.fold_bn:
param = self._fold_batchnorm(model, param_name, param, self.named_modules, self.sg)
if not is_initialized:
# Initialize the maskers
masker = zeros_mask_dict[param_name]
masker.use_double_copies = self.use_double_copies
masker.mask_on_forward_only = self.mask_on_forward_only
# register for the backward hook of the parameters
if self.mask_gradients:
masker.backward_hook_handle = param.register_hook(masker.mask_gradient)
self.is_initialized = True
if not self.skip_first_minibatch:
self.pruner.set_param_mask(param, param_name, zeros_mask_dict, meta)
else:
self.pruner.set_param_mask(param, param_name, zeros_mask_dict, meta)
def on_minibatch_begin(self, model, epoch, minibatch_id, minibatches_per_epoch,
zeros_mask_dict, meta, optimizer=None):
set_masks = False
global_mini_batch_id = epoch * minibatches_per_epoch + minibatch_id
if ((minibatch_id > 0) and
(self.mini_batch_pruning_frequency != 0) and
(global_mini_batch_id % self.mini_batch_pruning_frequency == 0)):
# This is _not_ the first mini-batch of a new epoch (performed in on_epoch_begin)
# and a pruning step is scheduled
set_masks = True
if self.skip_first_minibatch and global_mini_batch_id == 1:
# Because we skipped the first mini-batch of the first epoch (global_mini_batch_id == 0)
set_masks = True
for param_name, param in model.named_parameters():
if set_masks:
if self.fold_bn:
param = self._fold_batchnorm(model, param_name, param, self.named_modules, self.sg)
# Build mask here, but apply mask in before optimizatioN? or whole of this code apply (why?)
self.pruner.set_param_mask(param, param_name, zeros_mask_dict, meta)
zeros_mask_dict[param_name].apply_mask(param)
def before_parameter_optimization(self, model, epoch, minibatch_id, minibatches_per_epoch,
zeros_mask_dict, meta, optimizer):
for param_name, param in model.named_parameters():
zeros_mask_dict[param_name].revert_weights(param, minibatch_id)
def on_minibatch_end(self, model, epoch, minibatch_id, minibatches_per_epoch, zeros_mask_dict, optimizer):
# Always Flase, we need to keep our masker after our weights updated by SGD.
if self.discard_masks_at_minibatch_end:
for param_name, param in model.named_parameters():
zeros_mask_dict[param_name].mask = None
def on_epoch_end(self, model, zeros_mask_dict, meta, **kwargs):
"""The current epoch has ended"""
if self.is_last_epoch:
for param_name, param in model.named_parameters():
masker = zeros_mask_dict[param_name]
if self.keep_mask:
masker.use_double_copies = False
masker.mask_on_forward_only = False
masker.mask_tensor(param)
if masker.backward_hook_handle is not None:
masker.backward_hook_handle.remove()
masker.backward_hook_handle = None
class RegularizationPolicy(ScheduledTrainingPolicy):
"""
Regularization policy.
"""
def __init__(self, regularizer, keep_mask=False):
super(RegularizationPolicy, self).__init__()
self.regularizer = regularizer
self.keep_mask = keep_mask
self.is_last_epoch = False
def on_epoch_begin(self, model, zeros_mask_dict, meta, **kwargs):
self.is_last_epoch = meta['current_epoch'] == (meta['ending_epoch'] - 1)
def before_backward_pass(self, model, epoch, minibatch_id, minibatches_per_epoch, loss,
zeros_mask_dict, optimizer=None):
regularizer_loss = torch.tensor(0, dtype=torch.float, device=loss.device)
for param_name, param in model.named_parameters():
self.regularizer.loss(param, param_name, regularizer_loss, zeros_mask_dict)
policy_loss = PolicyLoss(loss + regularizer_loss,
[LossComponent(self.regularizer.__class__.__name__ + '_loss', regularizer_loss)])
return policy_loss
def on_minibatch_end(self, model, epoch, minibatch_id, minibatches_per_epoch, zeros_mask_dict, optimizer):
if self.regularizer.threshold_criteria is None:
return
keep_mask = False
if (minibatches_per_epoch-1 == minibatch_id) and self.is_last_epoch and self.keep_mask:
# If this is the last mini_batch in the last epoch, and the scheduler wants to
# keep the regularization mask, then now is the time ;-)
msglogger.info("RegularizationPolicy is keeping the regularization mask")
#print("RegularizationPolicy is keeping the regularization mask.")
keep_mask = True
for param_name, param in model.named_parameters():
self.regularizer.threshold(param, param_name, zeros_mask_dict)
if keep_mask:
zeros_mask_dict[param_name].is_regularization_mask = False
zeros_mask_dict[param_name].apply_mask(param)
class LRPolicy(ScheduledTrainingPolicy):
"""
Learning-rate decay scheduling policy.
"""
def __init__(self, lr_scheduler):
super(LRPolicy, self).__init__()
self.lr_scheduler = lr_scheduler
def on_epoch_end(self, model, zeros_mask_dict, meta, **kwargs):
if isinstance(self.lr_scheduler, torch.optim.lr_scheduler.ReduceLROnPlateau):
# Note: ReduceLROnPlateau doesn't inherit from _LRScheduler
self.lr_scheduler.step(kwargs['metrics'][self.lr_scheduler.mode],
epoch=meta['current_epoch'] + 1)
else:
self.lr_scheduler.step(epoch=meta['current_epoch'] + 1)
# Wrapped the optimizer could cause the started learning rate change to .....0.1...
# How to set muliptle optimizers?
# Bo-Wei adds following codes:
def on_epoch_begin(self, model, zeros_mask_dict, meta, **kwargs):
"""
if isinstance(self.lr_scheduler, torch.optim.lr_scheduler.ReduceLROnPlateau):
# Note: ReduceLROnPlateau doesn't inherit from _LRScheduler
self.lr_scheduler.step(kwargs['metrics'][self.lr_scheduler.mode],
epoch=meta['current_epoch'] + 1)
else:
self.lr_scheduler.step(epoch=meta['current_epoch'] + 1)
"""
# Option if retraining phase..., can reduce the complexity.
for param_name, param in model.named_parameters():
masker = zeros_mask_dict[param_name]
if kwargs['mask_gradients']:
## This is really a option, just test it by the empirical resutls.
if masker.mask_pruner is not None:
masker.backward_hook_handle = param.register_hook(masker.mask_gradient)
def before_parameter_optimization(self, model, epoch, minibatch_id, minibatches_per_epoch,
zeros_mask_dict, meta, optimizer, apply_gradient_mask = False):
# This is the masked gradient method, which is implemented by ADMM.
# We use resister in on epoch begin function instead of it!
if apply_gradient_mask:
with torch.no_grad():
for name, W in model.named_parameters():
mask = zeros_mask_dict[name].mask
if mask is not None:
W.grad *= mask #returns boolean array called mask when weights are above treshhold
class QuantizationPolicy(ScheduledTrainingPolicy):
def __init__(self, quantizer):
super(QuantizationPolicy, self).__init__()
self.quantizer = quantizer
self.quantizer.prepare_model()
self.quantizer.quantize_params()
def on_minibatch_end(self, model, epoch, minibatch_id, minibatches_per_epoch, zeros_mask_dict, optimizer):
# After parameters update, quantize the parameters again
# (Doing this here ensures the model parameters are quantized at training completion (and at validation time)
self.quantizer.quantize_params()
class ADMMPolicy(ScheduledTrainingPolicy):
def __init__(self, pruner, pruner_args, classes=None, layers=None):
super(ADMMPolicy, self).__init__(classes, layers)
"""
Arguments:
mask_on_forward_only: controls what we do after the weights are updated by the backward pass.
In issue #53 (https://github.com/NervanaSystems/distiller/issues/53) we explain why in some
cases masked weights will be updated to a non-zero value, even if their gradients are masked
(e.g. when using SGD with momentum). Therefore, to circumvent this weights-update performed by
the backward pass, we usually mask the weights again - right after the backward pass. To
disable this masking set:
pruner_args['mask_on_forward_only'] = False
use_double_copies: when set to `True`, two sets of weights are used. In the forward-pass we use
masked weights to compute the loss, but in the backward-pass we update the unmasked weights (using
gradients computed from the masked-weights loss).
mini_batch_pruning_frequency: this controls pruning scheduling at the mini-batch granularity. Every
mini_batch_pruning_frequency training steps (i.e. mini_batches) we perform pruning. This provides more
fine-grained control over pruning than that provided by CompressionScheduler (epoch granularity).
When setting 'mini_batch_pruning_frequency' to a value other than zero, make sure to configure the policy's
schedule to once-every-epoch.
fold_batchnorm: when set to `True`, the weights of BatchNorm modules are folded into the the weights of
Conv-2D modules (if Conv2D->BN edges exist in the model graph). Each weights filter is attenuated using
a different pair of (gamma, beta) coefficients, so `fold_batchnorm` is relevant for fine-grained and
filter-ranking pruning methods. We attenuate using the running values of the mean and variance, as is
done in quantization.
This control argument is only supported for Conv-2D modules (i.e. other convolution operation variants and
Linear operations are not supported).
"""
### Pruner aurgment, should be more clearly undertstand.
self.pruner = pruner
# Copy external policy configuration, if available
if pruner_args is None:
pruner_args = {}
self.levels = pruner_args.get('levels', None)
self.keep_mask = pruner_args.get('keep_mask', False)
self.mini_batch_pruning_frequency = pruner_args.get('mini_batch_pruning_frequency', 0)
self.mask_on_forward_only = pruner_args.get('mask_on_forward_only', False)
self.mask_gradients = pruner_args.get('mask_gradients', False)
if self.mask_gradients and not self.mask_on_forward_only:
raise ValueError("mask_gradients and (not mask_on_forward_only) are mutually exclusive")
self.backward_hook_handle = None # The backward-callback handle
self.use_double_copies = pruner_args.get('use_double_copies', False)
self.discard_masks_at_minibatch_end = pruner_args.get('discard_masks_at_minibatch_end', False)
self.skip_first_minibatch = pruner_args.get('skip_first_minibatch', False)
self.fold_bn = pruner_args.get('fold_batchnorm', False)
# These are required for BN-folding. We cache them to improve performance
self.named_modules = None
self.sg = None
# Initialize state
self.is_last_epoch = False
self.is_initialized = False
@staticmethod
def _fold_batchnorm(model, param_name, param, named_modules, sg):
def _get_all_parameters(param_module, bn_module):
w, b, gamma, beta = param_module.weight, param_module.bias, bn_module.weight, bn_module.bias
if not bn_module.affine:
gamma = 1.
beta = 0.
return w, b, gamma, beta
def get_bn_folded_weights(conv_module, bn_module):
"""Compute the weights of `conv_module` after folding successor BN layer.
In inference, DL frameworks and graph-compilers fold the batch normalization into
the weights as defined by equations 20 and 21 of https://arxiv.org/pdf/1806.08342.pdf
:param conv_module: nn.Conv2d module
:param bn_module: nn.BatchNorm2d module which succeeds `conv_module`
:return: Folded weights
"""
w, b, gamma, beta = _get_all_parameters(conv_module, bn_module)
with torch.no_grad():
sigma_running = torch.sqrt(bn_module.running_var + bn_module.eps)
w_corrected = w * (gamma / sigma_running).view(-1, 1, 1, 1)
return w_corrected
layer_name = utility.param_name_2_module_name(param_name)
#layer_name = param_name_2_module_name(param_name)
if not isinstance(named_modules[layer_name], nn.Conv2d):
return param
bn_layers = sg.successors_f(layer_name, ['BatchNormalization'])
if bn_layers:
assert len(bn_layers) == 1
bn_module = named_modules[bn_layers[0]]
conv_module = named_modules[layer_name]
param = get_bn_folded_weights(conv_module, bn_module)
return param
def on_epoch_begin(self, model, zeros_mask_dict, meta, **kwargs):
msglogger.debug("Pruner {} is about to prune".format(self.pruner.name))
self.is_last_epoch = meta['current_epoch'] == (meta['ending_epoch'] - 1)
if self.levels is not None:
self.pruner.levels = self.levels
meta['model'] = model
is_initialized = self.is_initialized
# ***********
# It's always False.
# ***********
if self.fold_bn:
# Cache this information (required for BN-folding) to improve performance
self.named_modules = OrderedDict(model.named_modules())
dummy_input = torch.randn(model.input_shape)
self.sg = sg.SummaryGraph(model, dummy_input)
for param_name, param in model.named_parameters():
# ***********
# It's always False.
# ***********
if self.fold_bn:
param = self._fold_batchnorm(model, param_name, param, self.named_modules, self.sg)
if not is_initialized:
# Initialize the maskers
masker = zeros_mask_dict[param_name]
masker.use_double_copies = self.use_double_copies # False
masker.mask_on_forward_only = self.mask_on_forward_only # False
# In ADMM, training phase doesn't need to apply gradient masking.
# register for the backward hook of the parameters
if self.mask_gradients:
masker.backward_hook_handle = param.register_hook(masker.mask_gradient)
self.is_initialized = True
if not self.skip_first_minibatch:
self.pruner.set_param_mask(param, param_name, zeros_mask_dict, meta)
#pass
else:
self.pruner.set_param_mask(param, param_name, zeros_mask_dict, meta)
#pass
def on_minibatch_begin(self, model, epoch, minibatch_id, minibatches_per_epoch,
zeros_mask_dict, meta, optimizer=None):
self.pruner.admm_adjust_learning_rate(optimizer, epoch)
set_masks = False
global_mini_batch_id = epoch * minibatches_per_epoch + minibatch_id
if ((minibatch_id > 0) and
(self.mini_batch_pruning_frequency != 0) and
(global_mini_batch_id % self.mini_batch_pruning_frequency == 0)):
# This is _not_ the first mini-batch of a new epoch (performed in on_epoch_begin)
# and a pruning step is scheduled
set_masks = True
if self.skip_first_minibatch and global_mini_batch_id == 1:
# Because we skipped the first mini-batch of the first epoch (global_mini_batch_id == 0)
set_masks = True
for param_name, param in model.named_parameters():
if set_masks:
if self.fold_bn:
param = self._fold_batchnorm(model, param_name, param, self.named_modules, self.sg)
#self.pruner.set_param_mask(param, param_name, zeros_mask_dict, meta)
pass
#zeros_mask_dict[param_name].apply_mask(param)
# ****************************************
# ADMM only use the follow two functions:
# Otherwise, should follow the distiller optimization stratetion {Just in pruning.}
# ****************************************
def before_parameter_optimization(self, model, epoch, minibatch_id, minibatches_per_epoch,
zeros_mask_dict, meta, optimizer, **kwargs):
#for param_name, param in model.named_parameters():
# zeros_mask_dict[param_name].revert_weights(param, minibatch_id)
# It seems that the masked progressive mechanism should be more clarified.
if self.pruner.masked_progressive:
with torch.no_grad():
for name, W in model.named_parameters():
zeros_mask_dict[param_name].revert_weights(name, minibatch_id)
if name in zeros_mask_dict:
W.grad *= zero_masks_dict[name]
if self.pruner.masked_retrain:
with torch.no_grad():
for name, W in model.named_parameters():
zeros_mask_dict[param_name].revert_weights(name, minibatch_id)
if name in zeros_mask_dict:
W.grad *= config.masks[name] #returns boolean array called mask when weights are above treshhold
def before_backward_pass(self, model, epoch, minibatch_id, minibatches_per_epoch, loss,
zeros_mask_dict, optimizer=None):
self.pruner.admm_update(model, epoch, minibatch_id, zeros_mask_dict)
ce_loss, admm_loss = self.pruner.append_admm_loss(model, loss, self.pruner.sparsity_type)
#regularizer_loss = torch.tensor(0, dtype=torch.float, device=loss.device)
#for param_name, param in model.named_parameters():
# self.regularizer.loss(param, param_name, regularizer_loss, zeros_mask_dict)
policy_loss = PolicyLoss(ce_loss + admm_loss,
[LossComponent(self.pruner.__class__.__name__ + '_loss', admm_loss)])
return policy_loss
# ****************************************
# ****************************************
# ****************************************
def on_minibatch_end(self, model, epoch, minibatch_id, minibatches_per_epoch, zeros_mask_dict, optimizer):
# It's always False in AGP setting so that we just follow it.
if self.discard_masks_at_minibatch_end:
for param_name, param in model.named_parameters():
zeros_mask_dict[param_name].mask = None
def on_epoch_end(self, model, zeros_mask_dict, meta, **kwargs):
# Finalize mask matrix, weight copies and some arguments.
"""The current epoch has ended"""
if self.is_last_epoch:
for param_name, param in model.named_parameters():
masker = zeros_mask_dict[param_name]
if self.keep_mask:
masker.use_double_copies = False
masker.mask_on_forward_only = False
masker.mask_tensor(param)
if masker.backward_hook_handle is not None:
masker.backward_hook_handle.remove()
masker.backward_hook_handle = None
# Since this is the end of ADMM pruner, so we should update the zeros_mask_dict before
# getting into the retrain phase. Furthermore, we set new learning rate for retraining our pruned model.
# Change the learning rate reset code to scheduler function.@
#self.pruner.set_param_mask(param, param_name, zeros_mask_dict, meta)
self.pruner.masking(model, zeros_mask_dict)
#for group in kwargs['optimizer'].param_groups:
# group['lr'] = 0.1
class FMRPolicy(ScheduledTrainingPolicy):
def __init__(self, pruner, pruner_args, classes=None, layers=None):
super(FMRPolicy, self).__init__(classes, layers)
"""
Arguments:
mask_on_forward_only: controls what we do after the weights are updated by the backward pass.
In issue #53 (https://github.com/NervanaSystems/distiller/issues/53) we explain why in some
cases masked weights will be updated to a non-zero value, even if their gradients are masked
(e.g. when using SGD with momentum). Therefore, to circumvent this weights-update performed by
the backward pass, we usually mask the weights again - right after the backward pass. To
disable this masking set:
pruner_args['mask_on_forward_only'] = False
use_double_copies: when set to `True`, two sets of weights are used. In the forward-pass we use
masked weights to compute the loss, but in the backward-pass we update the unmasked weights (using
gradients computed from the masked-weights loss).
mini_batch_pruning_frequency: this controls pruning scheduling at the mini-batch granularity. Every
mini_batch_pruning_frequency training steps (i.e. mini_batches) we perform pruning. This provides more
fine-grained control over pruning than that provided by CompressionScheduler (epoch granularity).
When setting 'mini_batch_pruning_frequency' to a value other than zero, make sure to configure the policy's
schedule to once-every-epoch.
fold_batchnorm: when set to `True`, the weights of BatchNorm modules are folded into the the weights of
Conv-2D modules (if Conv2D->BN edges exist in the model graph). Each weights filter is attenuated using
a different pair of (gamma, beta) coefficients, so `fold_batchnorm` is relevant for fine-grained and
filter-ranking pruning methods. We attenuate using the running values of the mean and variance, as is
done in quantization.
This control argument is only supported for Conv-2D modules (i.e. other convolution operation variants and
Linear operations are not supported).
"""
### Pruner aurgment, should be more clearly undertstand.
self.pruner = pruner
# Copy external policy configuration, if available
if pruner_args is None:
pruner_args = {}
self.levels = pruner_args.get('levels', None)
self.keep_mask = pruner_args.get('keep_mask', False)
self.mini_batch_pruning_frequency = pruner_args.get('mini_batch_pruning_frequency', 0)
self.mask_on_forward_only = pruner_args.get('mask_on_forward_only', False)
self.mask_gradients = pruner_args.get('mask_gradients', False)
if self.mask_gradients and not self.mask_on_forward_only:
raise ValueError("mask_gradients and (not mask_on_forward_only) are mutually exclusive")
self.backward_hook_handle = None # The backward-callback handle
self.use_double_copies = pruner_args.get('use_double_copies', False)
self.discard_masks_at_minibatch_end = pruner_args.get('discard_masks_at_minibatch_end', False)
self.skip_first_minibatch = pruner_args.get('skip_first_minibatch', False)
self.fold_bn = pruner_args.get('fold_batchnorm', False)
# These are required for BN-folding. We cache them to improve performance
self.named_modules = None
self.sg = None
# Initialize state
self.is_last_epoch = False
self.is_initialized = False
@staticmethod
def _fold_batchnorm(model, param_name, param, named_modules, sg):
def _get_all_parameters(param_module, bn_module):
w, b, gamma, beta = param_module.weight, param_module.bias, bn_module.weight, bn_module.bias
if not bn_module.affine:
gamma = 1.
beta = 0.
return w, b, gamma, beta
def get_bn_folded_weights(conv_module, bn_module):
"""Compute the weights of `conv_module` after folding successor BN layer.
In inference, DL frameworks and graph-compilers fold the batch normalization into
the weights as defined by equations 20 and 21 of https://arxiv.org/pdf/1806.08342.pdf
:param conv_module: nn.Conv2d module
:param bn_module: nn.BatchNorm2d module which succeeds `conv_module`
:return: Folded weights
"""
w, b, gamma, beta = _get_all_parameters(conv_module, bn_module)
with torch.no_grad():
sigma_running = torch.sqrt(bn_module.running_var + bn_module.eps)
w_corrected = w * (gamma / sigma_running).view(-1, 1, 1, 1)
return w_corrected
layer_name = utility.param_name_2_module_name(param_name)
#layer_name = param_name_2_module_name(param_name)
if not isinstance(named_modules[layer_name], nn.Conv2d):
return param
bn_layers = sg.successors_f(layer_name, ['BatchNormalization'])
if bn_layers:
assert len(bn_layers) == 1
bn_module = named_modules[bn_layers[0]]
conv_module = named_modules[layer_name]
param = get_bn_folded_weights(conv_module, bn_module)
return param
def on_epoch_begin(self, model, zeros_mask_dict, meta, **kwargs):
msglogger.debug("Pruner {} is about to prune".format(self.pruner.name))
self.is_last_epoch = meta['current_epoch'] == (meta['ending_epoch'] - 1)
if self.levels is not None:
self.pruner.levels = self.levels
print(meta['current_epoch'])
if meta['current_epoch'] == 0:
print("Delegate the forward function here.")
#model, self.forward_fn,
# self.acceptance_criterion, self.hook_fn
forward_fn = kwargs['forward_fn']
self.pruner.collect_intermediate_featuremap_samples(model, forward_fn,
self.pruner.acceptance_criterion, self.pruner.hook_fn)
self.pruner.forward_fn = forward_fn
meta['model'] = model
is_initialized = self.is_initialized
"""
There are several if conditions here, it may looks strange and confusing, but in our case, nearly all of them
are true, one can just focus on the set param mask function to make sure the pruning procedure will be executed correctly.
"""
if self.fold_bn: # Always false
# Cache this information (required for BN-folding) to improve performance
self.named_modules = OrderedDict(model.named_modules())
dummy_input = torch.randn(model.input_shape)
self.sg = sg.SummaryGraph(model, dummy_input)
for param_name, param in model.named_parameters():
if self.fold_bn: # Alway false
param = self._fold_batchnorm(model, param_name, param, self.named_modules, self.sg)
if not is_initialized:
# Initialize the maskers
masker = zeros_mask_dict[param_name]
masker.use_double_copies = self.use_double_copies # Always false
masker.mask_on_forward_only = self.mask_on_forward_only # Always false
if self.mask_gradients:
masker.backward_hook_handle = param.register_hook(masker.mask_gradient)
self.is_initialized = True
if not self.skip_first_minibatch:
self.pruner.set_param_mask(param, param_name, zeros_mask_dict, meta)
#pass
else:
self.pruner.set_param_mask(param, param_name, zeros_mask_dict, meta)
#pass
def on_minibatch_begin(self, model, epoch, minibatch_id, minibatches_per_epoch,
zeros_mask_dict, meta, optimizer=None):
set_masks = False
global_mini_batch_id = epoch * minibatches_per_epoch + minibatch_id
if ((minibatch_id > 0) and
(self.mini_batch_pruning_frequency != 0) and
(global_mini_batch_id % self.mini_batch_pruning_frequency == 0)):
# This is _not_ the first mini-batch of a new epoch (performed in on_epoch_begin)
# and a pruning step is scheduled
set_masks = True
if self.skip_first_minibatch and global_mini_batch_id == 1:
# Because we skipped the first mini-batch of the first epoch (global_mini_batch_id == 0)
set_masks = True
for param_name, param in model.named_parameters():
if set_masks:
if self.fold_bn: # Always false
param = self._fold_batchnorm(model, param_name, param, self.named_modules, self.sg)
self.pruner.set_param_mask(param, param_name, zeros_mask_dict, meta)
#pass
zeros_mask_dict[param_name].apply_mask(param)
def before_parameter_optimization(self, model, epoch, minibatch_id, minibatches_per_epoch,
zeros_mask_dict, meta, optimizer):
for param_name, param in model.named_parameters():
zeros_mask_dict[param_name].revert_weights(param, minibatch_id)
def on_minibatch_end(self, model, epoch, minibatch_id, minibatches_per_epoch, zeros_mask_dict, optimizer):
# Always Flase, we need to keep our masker after our weights updated by SGD.
if self.discard_masks_at_minibatch_end:
for param_name, param in model.named_parameters():
zeros_mask_dict[param_name].mask = None
def on_epoch_end(self, model, zeros_mask_dict, meta, **kwargs):
"""
The current epoch has ended
"""
if self.is_last_epoch:
for param_name, param in model.named_parameters():
masker = zeros_mask_dict[param_name]
if self.keep_mask:
masker.use_double_copies = False
masker.mask_on_forward_only = False
masker.mask_tensor(param)
if masker.backward_hook_handle is not None:
masker.backward_hook_handle.remove()
masker.backward_hook_handle = None