-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.py
65 lines (41 loc) · 1.53 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
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