Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
f13f1f8f
Unverified
Commit
f13f1f8f
authored
May 11, 2021
by
Sylvain Gugger
Committed by
GitHub
May 11, 2021
Browse files
Test checkpointing (#11682)
* Add test and see where CI is unhappy * Load with strict=False
parent
d9b28627
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
1 deletion
+19
-1
src/transformers/trainer.py
src/transformers/trainer.py
+12
-1
tests/test_modeling_common.py
tests/test_modeling_common.py
+7
-0
No files found.
src/transformers/trainer.py
View file @
f13f1f8f
...
...
@@ -1059,7 +1059,18 @@ class Trainer:
# We load the model state dict on the CPU to avoid an OOM error.
state_dict
=
torch
.
load
(
os
.
path
.
join
(
resume_from_checkpoint
,
WEIGHTS_NAME
),
map_location
=
"cpu"
)
# If the model is on the GPU, it still works!
self
.
model
.
load_state_dict
(
state_dict
)
load_result
=
self
.
model
.
load_state_dict
(
state_dict
,
strict
=
False
)
if
len
(
load_result
.
missing_keys
)
!=
0
:
if
load_result
.
missing_keys
==
self
.
model
.
_keys_to_ignore_on_save
:
self
.
model
.
tie_weights
()
else
:
logger
.
warn
(
f
"There were missing keys in the checkpoint model loaded:
{
load_result
.
missing_keys
}
."
)
if
len
(
load_result
.
unexpected_keys
)
!=
0
:
logger
.
warn
(
f
"There were unexpected keys in the checkpoint model loaded:
{
load_result
.
unexpected_keys
}
."
)
# If model was re-initialized, put it on the right device and update self.model_wrapped
if
model_reloaded
:
...
...
tests/test_modeling_common.py
View file @
f13f1f8f
...
...
@@ -177,6 +177,13 @@ class ModelTesterMixin:
for
k
in
_keys_to_ignore_on_save
:
self
.
assertNotIn
(
k
,
state_dict_saved
)
# Test we can load the state dict in the model, necessary for the checkpointing API in Trainer.
load_result
=
model
.
load_state_dict
(
state_dict_saved
,
strict
=
False
)
self
.
assertTrue
(
len
(
load_result
.
missing_keys
)
==
0
or
load_result
.
missing_keys
==
model
.
_keys_to_ignore_on_save
)
self
.
assertTrue
(
len
(
load_result
.
unexpected_keys
)
==
0
)
def
_mock_init_weights
(
self
,
module
):
if
hasattr
(
module
,
"weight"
)
and
module
.
weight
is
not
None
:
module
.
weight
.
data
.
fill_
(
3
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment