Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
e4910213
Unverified
Commit
e4910213
authored
Aug 31, 2022
by
Zachary Mueller
Committed by
GitHub
Aug 31, 2022
Browse files
Warn on TPUs when the custom optimizer and model device are not the same (#18668)
* Check optimizer for device on TPU * Typo
parent
cdde85a0
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
0 deletions
+15
-0
src/transformers/trainer.py
src/transformers/trainer.py
+15
-0
No files found.
src/transformers/trainer.py
View file @
e4910213
...
...
@@ -465,6 +465,21 @@ class Trainer:
"Passing a `model_init` is incompatible with providing the `optimizers` argument. "
"You should subclass `Trainer` and override the `create_optimizer_and_scheduler` method."
)
if
is_torch_tpu_available
()
and
self
.
optimizer
is
not
None
:
for
param
in
self
.
model
.
parameters
():
model_device
=
param
.
device
break
for
param_group
in
self
.
optimizer
.
param_groups
:
if
len
(
param_group
[
"params"
])
>
0
:
optimizer_device
=
param_group
[
"params"
][
0
].
device
break
if
model_device
!=
optimizer_device
:
raise
ValueError
(
"The model and the optimizer parameters are not on the same device, which probably means you"
" created an optimizer around your model **before** putting on the device and passing it to the"
" `Trainer`. Make sure the lines `import torch_xla.core.xla_model as xm` and"
" `model.to(xm.xla_device())` is performed before the optimizer creation in your script."
)
if
((
self
.
sharded_ddp
is
not
None
)
or
args
.
deepspeed
or
(
self
.
fsdp
is
not
None
))
and
(
self
.
optimizer
is
not
None
or
self
.
lr_scheduler
is
not
None
):
...
...
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