-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy path_workarounds.py
More file actions
36 lines (28 loc) · 1.17 KB
/
Copy path_workarounds.py
File metadata and controls
36 lines (28 loc) · 1.17 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
"""This module implements some workarounds for dependencies
Authors
* Aku Rouhe 2022
"""
import warnings
import weakref
import torch
WEAKREF_MARKER = "WEAKREF"
def _cycliclrsaver(obj, path):
state_dict = obj.state_dict()
if state_dict.get("_scale_fn_ref") is not None:
state_dict["_scale_fn_ref"] = WEAKREF_MARKER
torch.save(state_dict, path)
def _cycliclrloader(obj, path, end_of_epoch):
del end_of_epoch # Unused
device = "cpu"
state_dict = torch.load(path, map_location=device)
if state_dict.get("_scale_fn_ref") == WEAKREF_MARKER:
if not isinstance(obj._scale_fn_ref, weakref.WeakMethod):
MSG = "Loading CyclicLR scheduler and the _scale_ref_fn did not exist in instance."
MSG += " You did not construct it with the same parameters it was created!"
MSG += " Looks like you changed the scale function!"
MSG += " If this was not intentional, the scheduler might not work correctly."
warnings.warn(MSG)
try:
obj.load_state_dict(torch.load(path, map_location=device), strict=True)
except TypeError:
obj.load_state_dict(torch.load(path, map_location=device))