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

avoid calling gc.collect and cuda.empty_cache #34514

Merged
merged 5 commits into from
Oct 31, 2024
Merged

avoid calling gc.collect and cuda.empty_cache #34514

merged 5 commits into from
Oct 31, 2024

Conversation

ydshieh
Copy link
Collaborator

@ydshieh ydshieh commented Oct 30, 2024

What does this PR do?

Let's avoid calling gc.collect and cuda.empty_cache while the tests are running on CPU:

  • those operations are slow
  • (actually, in most cases, they are only relevant for integration tests which use large models)

Running on GPT2 tests,

60 seconds on main, 20 seconds on this PR

@ydshieh ydshieh requested a review from Rocketknight1 October 30, 2024 16:44
Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

Yes, this seems like a good speed fix! cc @LysandreJik @ArthurZucker for core maintainer review

@ydshieh ydshieh changed the title Speed no empty avoid calling gc.collect and cuda.empty_cache Oct 30, 2024
@ydshieh ydshieh changed the title avoid calling gc.collect and cuda.empty_cache avoid calling gc.collect and cuda.empty_cache Oct 30, 2024
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.

Smart! Should a helper method be made that only runs on CPU?

Both the gc.collect and the torch device checks could be moved into the backend_empty_cache method (or an other method that wraps both)

@ydshieh
Copy link
Collaborator Author

ydshieh commented Oct 31, 2024

Yes, a helper method is nice. Will update

@ydshieh
Copy link
Collaborator Author

ydshieh commented Oct 31, 2024

updated.

So far it doesn't call gc.collect() at all (default value False).
I would like to see if this would cause issue.
In general, we don't need to call it after each test method (so in tearDown) as it is slow.

@ydshieh ydshieh merged commit ab98f0b into main Oct 31, 2024
25 of 27 checks passed
@ydshieh ydshieh deleted the speed_no_empty branch October 31, 2024 15:36
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

2015aroras pushed a commit to 2015aroras/transformers that referenced this pull request Nov 15, 2024
* update

* update

* update

* update

* update

---------

Co-authored-by: ydshieh <[email protected]>
BernardZach pushed a commit to BernardZach/transformers that referenced this pull request Dec 5, 2024
* update

* update

* update

* update

* update

---------

Co-authored-by: ydshieh <[email protected]>
BernardZach pushed a commit to innovationcore/transformers that referenced this pull request Dec 6, 2024
* update

* update

* update

* update

* update

---------

Co-authored-by: ydshieh <[email protected]>
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.

4 participants