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
a2789add
Unverified
Commit
a2789add
authored
May 17, 2023
by
cyy
Committed by
GitHub
May 17, 2023
Browse files
[Reland] search model buffers for dtype as the last resort (#23319)
search model buffers for dtype as the last resort
parent
3d764fe8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
13 deletions
+20
-13
src/transformers/modeling_utils.py
src/transformers/modeling_utils.py
+20
-13
No files found.
src/transformers/modeling_utils.py
View file @
a2789add
...
...
@@ -207,22 +207,29 @@ def get_parameter_dtype(parameter: Union[nn.Module, GenerationMixin, "ModuleUtil
# if no floating dtype was found return whatever the first dtype is
return
last_dtype
else
:
# For nn.DataParallel compatibility in PyTorch > 1.5
def
find_tensor_attributes
(
module
:
nn
.
Module
)
->
List
[
Tuple
[
str
,
T
ensor
]]:
tuples
=
[(
k
,
v
)
for
k
,
v
in
module
.
__dict__
.
items
()
if
torch
.
is_tensor
(
v
)]
return
tuples
gen
=
parameter
.
_named_members
(
get_members_fn
=
find_tensor_attributes
)
last_tuple
=
None
for
tuple
in
gen
:
last_tuple
=
tuple
if
tuple
[
1
].
is_floating_point
():
return
tuple
[
1
].
dtype
# For nn.DataParallel compatibility in PyTorch > 1.5
def
find_tensor_attributes
(
module
:
nn
.
Module
)
->
List
[
Tuple
[
str
,
Tensor
]]:
tuples
=
[(
k
,
v
)
for
k
,
v
in
module
.
__dict__
.
items
()
if
torch
.
is_t
ensor
(
v
)]
return
tuples
gen
=
parameter
.
_named_members
(
get_members_fn
=
find_tensor_attributes
)
last_tuple
=
None
for
tuple
in
gen
:
last_
tuple
=
tuple
if
tuple
[
1
].
is_floating_point
():
return
tuple
[
1
].
dtype
if
last_tuple
is
not
None
:
# fallback to the last dtype
return
last_tuple
[
1
].
dtype
# fallback to buffer dtype
for
t
in
parameter
.
buffers
():
last_dtype
=
t
.
dtype
if
t
.
is_floating_point
():
return
t
.
dtype
return
last_dtype
def
get_state_dict_float_dtype
(
state_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