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
b5be744d
Unverified
Commit
b5be744d
authored
Jan 12, 2023
by
Susnato Dhar
Committed by
GitHub
Jan 12, 2023
Browse files
Fixed issue #21039 (#21062)
Fixed issue #21039 and added test for low_cpu_mem_usage
parent
e849e5bb
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
1 deletion
+26
-1
src/transformers/modeling_utils.py
src/transformers/modeling_utils.py
+5
-1
tests/test_modeling_common.py
tests/test_modeling_common.py
+21
-0
No files found.
src/transformers/modeling_utils.py
View file @
b5be744d
...
...
@@ -2629,7 +2629,11 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix
# This is not ideal in terms of memory, but if we don't do that not, we can't initialize them in the next step
if
low_cpu_mem_usage
:
for
key
in
missing_keys
:
if
key
.
startswith
(
prefix
):
if
key
in
list
(
model_state_dict
.
keys
()):
key
=
key
elif
f
"
{
prefix
}
.key"
in
list
(
model_state_dict
.
keys
()):
key
=
f
"
{
prefix
}
.key"
elif
key
.
startswith
(
prefix
)
and
"."
.
join
(
key
.
split
(
"."
)[
1
:])
in
list
(
model_state_dict
.
keys
()):
key
=
"."
.
join
(
key
.
split
(
"."
)[
1
:])
param
=
model_state_dict
[
key
]
...
...
tests/test_modeling_common.py
View file @
b5be744d
...
...
@@ -3166,6 +3166,27 @@ class ModelUtilsTest(TestCasePlus):
):
_
=
ModelWithHead
.
from_pretrained
(
tmp_dir
)
@
require_torch_gpu
def
test_pretrained_low_mem_new_config
(
self
):
# Checking for 1 model(the same one which was described in the issue) .
model_ids
=
[
"gpt2"
]
for
model_id
in
model_ids
:
model_config
=
AutoConfig
.
from_pretrained
(
pretrained_model_name_or_path
=
model_id
)
model_config
.
n_layer
=
48
model_config
.
n_head
=
25
model_config
.
n_embd
=
1600
model
=
AutoModelForCausalLM
.
from_pretrained
(
pretrained_model_name_or_path
=
model_id
,
config
=
model_config
,
ignore_mismatched_sizes
=
True
,
torch_dtype
=
torch
.
float16
,
low_cpu_mem_usage
=
True
,
)
model_ref
=
AutoModelForCausalLM
.
from_pretrained
(
pretrained_model_name_or_path
=
model_id
)
self
.
assertEqual
(
model
.
__class__
.
__name__
,
model_ref
.
__class__
.
__name__
)
@
require_torch
@
is_staging_test
...
...
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