-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckpoint_utils.py
More file actions
26 lines (21 loc) · 989 Bytes
/
Copy pathcheckpoint_utils.py
File metadata and controls
26 lines (21 loc) · 989 Bytes
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
def adapt_state_dict_for_model(model, state_dict):
model_keys = model.state_dict().keys()
model_wants_prefix = any(k.startswith("_orig_mod.") for k in model_keys)
state_has_prefix = any(k.startswith("_orig_mod.") for k in state_dict.keys())
if model_wants_prefix == state_has_prefix:
return state_dict
if model_wants_prefix:
return {
k if k.startswith("_orig_mod.") else f"_orig_mod.{k}": v
for k, v in state_dict.items()
}
prefix_len = len("_orig_mod.")
return {
k[prefix_len:] if k.startswith("_orig_mod.") else k: v
for k, v in state_dict.items()
}
def load_model_state(model, checkpoint, key="model_state_dict", strict=True):
state_dict = checkpoint.get(key, checkpoint.get("model"))
if state_dict is None:
raise KeyError(f"Checkpoint does not contain '{key}' or 'model'")
return model.load_state_dict(adapt_state_dict_for_model(model, state_dict), strict=strict)