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

[model loading] don't gc.collect() if only 1 shard is used #36721

Merged
merged 3 commits into from
Mar 14, 2025

Conversation

gante
Copy link
Member

@gante gante commented Mar 14, 2025

What does this PR do?

Fixes a test-related regression introduced by #36033. Despite speeding up from_pretrained in general, most tests that relied on from_pretrained became much slower (see tests section below).

Measuring time, the entire slowdown can be traced to a single line, a gc.collect(). This line was not added in #36033, it was moved. Before #36033, if the checkpoint was not sharded, a state_dict would have been passed to _load_pretrained_model, and the gc.collect() branch would not be reached.

This PR adds a tiny if to skip gc.collect() if the checkpoint is not sharded. Since many tests rely on unsharded from_pretrained, we can immediately observe faster tests.

Tests

py.test tests/models/gpt2/test_modeling_gpt2.py times, which includes a mix of tests with and without from_pretrained, on my machine:

@github-actions github-actions bot marked this pull request as draft March 14, 2025 12:31
Copy link

Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. When it is ready for review, please click the Ready for review button (at the bottom of the PR page).

@gante gante marked this pull request as ready for review March 14, 2025 12:34
@gante gante changed the title [model loading] don't gc.collect if 1 shard is used [model loading] don't gc.collect if only 1 shard is used Mar 14, 2025
@gante gante changed the title [model loading] don't gc.collect if only 1 shard is used [model loading] don't gc.collect() if only 1 shard is used Mar 14, 2025
@gante gante requested review from Cyrilvallez and removed request for Rocketknight1 March 14, 2025 12:40
Copy link
Member

@Cyrilvallez Cyrilvallez left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks!

@gante gante merged commit 3bd1a0d into huggingface:main Mar 14, 2025
23 checks passed
@gante gante deleted the dont_gc_collect_on_single_shard branch March 14, 2025 12:57
@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.

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.

3 participants