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
14cc50d0
Commit
14cc50d0
authored
Dec 01, 2021
by
Stas Bekman
Browse files
fix autocast for older pytorch
parent
4c0dd199
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
6 deletions
+18
-6
src/transformers/trainer.py
src/transformers/trainer.py
+18
-6
No files found.
src/transformers/trainer.py
View file @
14cc50d0
...
...
@@ -1857,8 +1857,12 @@ class Trainer:
return
loss_mb
.
reduce_mean
().
detach
().
to
(
self
.
args
.
device
)
if
self
.
use_amp
:
if
version
.
parse
(
torch
.
__version__
)
>=
version
.
parse
(
"1.10"
):
with
autocast
(
dtype
=
self
.
amp_dtype
):
loss
=
self
.
compute_loss
(
model
,
inputs
)
else
:
with
autocast
():
loss
=
self
.
compute_loss
(
model
,
inputs
)
else
:
loss
=
self
.
compute_loss
(
model
,
inputs
)
...
...
@@ -2501,8 +2505,12 @@ class Trainer:
else
:
if
has_labels
:
if
self
.
use_amp
:
if
version
.
parse
(
torch
.
__version__
)
>=
version
.
parse
(
"1.10"
):
with
autocast
(
dtype
=
self
.
amp_dtype
):
loss
,
outputs
=
self
.
compute_loss
(
model
,
inputs
,
return_outputs
=
True
)
else
:
with
autocast
():
loss
,
outputs
=
self
.
compute_loss
(
model
,
inputs
,
return_outputs
=
True
)
else
:
loss
,
outputs
=
self
.
compute_loss
(
model
,
inputs
,
return_outputs
=
True
)
loss
=
loss
.
mean
().
detach
()
...
...
@@ -2514,8 +2522,12 @@ class Trainer:
else
:
loss
=
None
if
self
.
use_amp
:
if
version
.
parse
(
torch
.
__version__
)
>=
version
.
parse
(
"1.10"
):
with
autocast
(
dtype
=
self
.
amp_dtype
):
outputs
=
model
(
**
inputs
)
else
:
with
autocast
():
outputs
=
model
(
**
inputs
)
else
:
outputs
=
model
(
**
inputs
)
if
isinstance
(
outputs
,
dict
):
...
...
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