Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete state_dict to release memory as early as possible #18832

Merged
merged 4 commits into from
Sep 1, 2022

Conversation

ydshieh
Copy link
Collaborator

@ydshieh ydshieh commented Aug 31, 2022

What does this PR do?

Fix #18782.

Note that this is not a real memory issue. A call to gc.collect() at the end of from_pretrained() works well too.

However this PR finds and simply del state_dict at the end of _load_state_dict_into_model(), and GC is able to perform housekeeping on its own at a earlier time.

@ydshieh ydshieh requested a review from LysandreJik August 31, 2022 12:24
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Aug 31, 2022

The documentation is not available anymore as the PR was closed or merged.

@ydshieh
Copy link
Collaborator Author

ydshieh commented Aug 31, 2022

The change regarding having a new argument state_dict in the nested function load is to pass black check, otherwise we get

src/transformers/modeling_utils.py:422:17: F821 undefined name 'state_dict'

with the new line del state_dict. (It's quite strange though)

@ydshieh ydshieh force-pushed the delete_state_dict_to_release_mem branch from b4e52bd to 45db5e6 Compare August 31, 2022 14:29
@ydshieh ydshieh marked this pull request as ready for review August 31, 2022 14:29
@ydshieh
Copy link
Collaborator Author

ydshieh commented Aug 31, 2022

Ready for review.

@ydshieh
Copy link
Collaborator Author

ydshieh commented Aug 31, 2022

The failing test is test_encodings_from_xnli_dataset which is irrelevant to this PR.

Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing!

src/transformers/modeling_utils.py Outdated Show resolved Hide resolved
Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks @ydshieh!

Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
@ydshieh ydshieh merged commit 563a8d5 into main Sep 1, 2022
@ydshieh ydshieh deleted the delete_state_dict_to_release_mem branch September 1, 2022 08:55
oneraghavan pushed a commit to oneraghavan/transformers that referenced this pull request Sep 26, 2022
…ce#18832)

Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory increment and release when loading model via PretrainedModel.from_pretrained
4 participants