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
4d10474f
Unverified
Commit
4d10474f
authored
Aug 31, 2021
by
Sylvain Gugger
Committed by
GitHub
Aug 31, 2021
Browse files
Handle nested dict/lists of tensors as inputs in the Trainer (#13338)
parent
3efcfeab
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
11 deletions
+19
-11
src/transformers/trainer.py
src/transformers/trainer.py
+19
-11
No files found.
src/transformers/trainer.py
View file @
4d10474f
...
@@ -1727,22 +1727,30 @@ class Trainer:
...
@@ -1727,22 +1727,30 @@ class Trainer:
self
.
state
.
log_history
.
append
(
output
)
self
.
state
.
log_history
.
append
(
output
)
self
.
control
=
self
.
callback_handler
.
on_log
(
self
.
args
,
self
.
state
,
self
.
control
,
logs
)
self
.
control
=
self
.
callback_handler
.
on_log
(
self
.
args
,
self
.
state
,
self
.
control
,
logs
)
def
_prepare_input
s
(
self
,
inputs
:
Dict
[
str
,
Union
[
torch
.
Tensor
,
Any
]
]
)
->
Dict
[
str
,
Union
[
torch
.
Tensor
,
Any
]
]
:
def
_prepare_input
(
self
,
data
:
Union
[
torch
.
Tensor
,
Any
])
->
Union
[
torch
.
Tensor
,
Any
]:
"""
"""
Prepare :obj:`inputs` before feeding them to the model, converting them to tensors if they are not already and
Prepares one :obj:`data` before feeding it to the model, be it a tensor or a nested list/dictionary of tensors.
handling potential state.
"""
"""
for
k
,
v
in
inputs
.
items
():
if
isinstance
(
data
,
dict
):
if
isinstance
(
v
,
torch
.
Tensor
):
return
type
(
data
)(
**
{
k
:
self
.
_prepare_input
(
v
)
for
k
,
v
in
data
.
items
()})
elif
isinstance
(
data
,
(
tuple
,
list
)):
return
type
(
data
)(
self
.
_prepare_input
(
v
)
for
v
in
data
)
elif
isinstance
(
data
,
torch
.
Tensor
):
kwargs
=
dict
(
device
=
self
.
args
.
device
)
kwargs
=
dict
(
device
=
self
.
args
.
device
)
if
self
.
deepspeed
and
inputs
[
k
]
.
dtype
!=
torch
.
int64
:
if
self
.
deepspeed
and
data
.
dtype
!=
torch
.
int64
:
# NLP models inputs are int64 and those get adjusted to the right dtype of the
# NLP models inputs are int64 and those get adjusted to the right dtype of the
# embedding. Other models such as wav2vec2's inputs are already float and thus
# embedding. Other models such as wav2vec2's inputs are already float and thus
# may need special handling to match the dtypes of the model
# may need special handling to match the dtypes of the model
kwargs
.
update
(
dict
(
dtype
=
self
.
args
.
hf_deepspeed_config
.
dtype
()))
kwargs
.
update
(
dict
(
dtype
=
self
.
args
.
hf_deepspeed_config
.
dtype
()))
return
data
.
to
(
**
kwargs
)
return
data
inputs
[
k
]
=
v
.
to
(
**
kwargs
)
def
_prepare_inputs
(
self
,
inputs
:
Dict
[
str
,
Union
[
torch
.
Tensor
,
Any
]])
->
Dict
[
str
,
Union
[
torch
.
Tensor
,
Any
]]:
"""
Prepare :obj:`inputs` before feeding them to the model, converting them to tensors if they are not already and
handling potential state.
"""
inputs
=
self
.
_prepare_input
(
inputs
)
if
self
.
args
.
past_index
>=
0
and
self
.
_past
is
not
None
:
if
self
.
args
.
past_index
>=
0
and
self
.
_past
is
not
None
:
inputs
[
"mems"
]
=
self
.
_past
inputs
[
"mems"
]
=
self
.
_past
...
...
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