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
"git@developer.sourcefind.cn:orangecat/ollama.git" did not exist on "197e420a97167c702973243563b72eb70b0e6786"
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
Hide 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:
self
.
state
.
log_history
.
append
(
output
)
self
.
control
=
self
.
callback_handler
.
on_log
(
self
.
args
,
self
.
state
,
self
.
control
,
logs
)
def
_prepare_input
(
self
,
data
:
Union
[
torch
.
Tensor
,
Any
])
->
Union
[
torch
.
Tensor
,
Any
]:
"""
Prepares one :obj:`data` before feeding it to the model, be it a tensor or a nested list/dictionary of tensors.
"""
if
isinstance
(
data
,
dict
):
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
)
if
self
.
deepspeed
and
data
.
dtype
!=
torch
.
int64
:
# 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
# may need special handling to match the dtypes of the model
kwargs
.
update
(
dict
(
dtype
=
self
.
args
.
hf_deepspeed_config
.
dtype
()))
return
data
.
to
(
**
kwargs
)
return
data
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.
"""
for
k
,
v
in
inputs
.
items
():
if
isinstance
(
v
,
torch
.
Tensor
):
kwargs
=
dict
(
device
=
self
.
args
.
device
)
if
self
.
deepspeed
and
inputs
[
k
].
dtype
!=
torch
.
int64
:
# 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
# may need special handling to match the dtypes of the model
kwargs
.
update
(
dict
(
dtype
=
self
.
args
.
hf_deepspeed_config
.
dtype
()))
inputs
[
k
]
=
v
.
to
(
**
kwargs
)
inputs
=
self
.
_prepare_input
(
inputs
)
if
self
.
args
.
past_index
>=
0
and
self
.
_past
is
not
None
:
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