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
f1732e13
Unverified
Commit
f1732e13
authored
Jul 12, 2023
by
Zach Mueller
Committed by
GitHub
Jul 12, 2023
Browse files
Rm duplicate pad_across_processes (#24780)
Rm duplicate
parent
cfc8a053
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
0 additions
and
35 deletions
+0
-35
src/transformers/trainer.py
src/transformers/trainer.py
+0
-35
No files found.
src/transformers/trainer.py
View file @
f1732e13
...
...
@@ -3257,41 +3257,6 @@ class Trainer:
tensors
=
distributed_concat
(
tensors
)
return
tensors
# Copied from Accelerate.
def
_pad_across_processes
(
self
,
tensor
,
pad_index
=-
100
):
"""
Recursively pad the tensors in a nested list/tuple/dictionary of tensors from all devices to the same size so
they can safely be gathered.
"""
if
isinstance
(
tensor
,
(
list
,
tuple
)):
return
type
(
tensor
)(
self
.
_pad_across_processes
(
t
,
pad_index
=
pad_index
)
for
t
in
tensor
)
elif
isinstance
(
tensor
,
dict
):
return
type
(
tensor
)({
k
:
self
.
_pad_across_processes
(
v
,
pad_index
=
pad_index
)
for
k
,
v
in
tensor
.
items
()})
elif
not
isinstance
(
tensor
,
torch
.
Tensor
):
raise
TypeError
(
f
"Can't pad the values of type
{
type
(
tensor
)
}
, only of nested list/tuple/dicts of tensors."
)
if
len
(
tensor
.
shape
)
<
2
:
return
tensor
# Gather all sizes
size
=
torch
.
tensor
(
tensor
.
shape
,
device
=
tensor
.
device
)[
None
]
sizes
=
self
.
_nested_gather
(
size
).
cpu
()
max_size
=
max
(
s
[
1
]
for
s
in
sizes
)
# When extracting XLA graphs for compilation, max_size is 0,
# so use inequality to avoid errors.
if
tensor
.
shape
[
1
]
>=
max_size
:
return
tensor
# Then pad to the maximum size
old_size
=
tensor
.
shape
new_size
=
list
(
old_size
)
new_size
[
1
]
=
max_size
new_tensor
=
tensor
.
new_zeros
(
tuple
(
new_size
))
+
pad_index
new_tensor
[:,
:
old_size
[
1
]]
=
tensor
return
new_tensor
def
prediction_step
(
self
,
model
:
nn
.
Module
,
...
...
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