forked from BorealisAI/advertorch
/
context.py
70 lines (47 loc) · 1.73 KB
/
context.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
# Copyright (c) 2018-present, Royal Bank of Canada.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
from contextlib import contextmanager
class ctx_noparamgrad(object):
def __init__(self, module):
self.prev_grad_state = get_param_grad_state(module)
self.module = module
set_param_grad_off(module)
def __enter__(self):
pass
def __exit__(self, *args):
set_param_grad_state(self.module, self.prev_grad_state)
return False
class ctx_eval(object):
def __init__(self, module):
self.prev_training_state = get_module_training_state(module)
self.module = module
set_module_training_off(module)
def __enter__(self):
pass
def __exit__(self, *args):
set_module_training_state(self.module, self.prev_training_state)
return False
@contextmanager
def ctx_noparamgrad_and_eval(module):
with ctx_noparamgrad(module) as a, ctx_eval(module) as b:
yield (a, b)
def get_module_training_state(module):
return {mod: mod.training for mod in module.modules()}
def set_module_training_state(module, training_state):
for mod in module.modules():
mod.training = training_state[mod]
def set_module_training_off(module):
for mod in module.modules():
mod.training = False
def get_param_grad_state(module):
return {param: param.requires_grad for param in module.parameters()}
def set_param_grad_state(module, grad_state):
for param in module.parameters():
param.requires_grad = grad_state[param]
def set_param_grad_off(module):
for param in module.parameters():
param.requires_grad = False