forked from pytorch/ELF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_base.py
188 lines (152 loc) · 5.93 KB
/
model_base.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
# Copyright (c) 2018-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import os
from collections import OrderedDict
from copy import deepcopy
from time import sleep
import torch
import torch.nn as nn
from torch.autograd import Variable
torch.backends.cudnn.benchmark = True
class Model(nn.Module):
''' Base class for an RL model, it is a wrapper for ``nn.Module``'''
def __init__(self, option_map, params):
"""Initialize model with ``args``.
Set ``step`` to ``0`` and ``volatile`` to ```false``.
``step`` records the number of times the weight has been updated.
``volatile`` indicates that the Variable should be used in
inference mode, i.e. don't save the history.
"""
super(Model, self).__init__()
self.option_map = option_map
self.params = params
self.step = 0
self.volatile = False
def clone(self, gpu=None):
"""Deep copy an existing model.
``options``, ``step`` and ``state_dict`` are copied.
Args:
gpu(int): gpu id to be put the model on
Returns:
Cloned model
"""
model = type(self)(self.option_map, self.params)
model.load_state_dict(deepcopy(self.state_dict()))
model.step = self.step
if gpu is not None:
model.cuda(gpu)
return model
def set_volatile(self, volatile):
"""Set model to ``volatile``.
Args:
volatile(bool): indicating that the Variable should be used in
inference mode, i.e. don't save the history.
"""
self.volatile = volatile
def _var(self, x):
''' Convert tensor x to a pytorch Variable.
Returns:
Variable for x
'''
if not isinstance(x, Variable):
return Variable(x, volatile=self.volatile)
else:
return x
def before_update(self):
"""Customized operations for each model before update.
To be extended.
"""
pass
def save(self, filename, num_trial=10):
"""Save current model, step and args to ``filename``
Args:
filename(str): filename to be saved.
num_trial(int): maximum number of retries to save a model.
"""
state_dict = self.clone().cpu().state_dict()
# Note that the save might experience issues, so if we encounter
# errors, try a few times and then give up.
content = {
'state_dict': state_dict,
'step': self.step,
'options': vars(self.options),
}
for i in range(num_trial):
try:
torch.save(content, filename)
return
except BaseException:
sleep(1)
print(
"Failed to save %s after %d trials, giving up ..." %
(filename, num_trial))
def load(
self, filename,
omit_keys=[], replace_prefix=[], check_loaded_options=True):
''' Load current model, step and args from ``filename``
Args:
filename(str): model filename to load from
omit_keys(list): list of omitted keys.
Sometimes model will have extra keys and weights
(e.g. due to extra tasks during training).
We should omit them;
otherwise loading will not work.
'''
data = torch.load(filename)
if isinstance(data, OrderedDict):
self.load_state_dict(data)
else:
for k in omit_keys:
del data["state_dict"][k + ".weight"]
del data["state_dict"][k + ".bias"]
sd = data["state_dict"]
keys = list(sd.keys())
for key in keys:
for src, dst in replace_prefix:
if key.startswith(src):
sd[dst + key[len(src):]] = sd[key]
del sd[key]
self.load_state_dict(sd)
self.step = data.get("step", 0)
self.filename = os.path.realpath(data.get("filename", filename))
if check_loaded_options:
# Ensure that for options defined in both the current model
# options and the loaded model options, the values match between
# current model and loaded model.
loaded_options = data.get('options', {})
current_options = vars(self.options)
for option_name in \
(set(loaded_options.keys()) & set(current_options.keys())):
if loaded_options[option_name] != current_options[option_name]:
raise ValueError(
f'Discrepancy between current and loaded model '
f'parameter: {option_name} '
f'loaded: {loaded_options[option_name]}, '
f'current: {current_options[option_name]}'
)
def load_from(self, model):
''' Load from an existing model. State is not deep copied.
To deep copy the model, uss ``clone``.
'''
if hasattr(model, 'option_map'):
self.option_map = model.option_map
if hasattr(model, 'params'):
self.params = deepcopy(model.params)
self.load_state_dict(model.state_dict())
self.step = model.step
def inc_step(self):
''' increment the step.
``step`` records the number of times the weight has been updated.'''
self.step += 1
def signature(self):
'''Get model's signature.
Returns:
the model's signature string, specified by step.
'''
return "Model[%d]" % self.step
def prepare_cooldown(self):
"""Prepare for "cooldown" forward passes (useful for batchnorm)."""
pass