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

Reduce Memory Cost in Flux Training #9829

Merged
merged 14 commits into from
Nov 1, 2024
Merged

Conversation

leisuzz
Copy link
Contributor

@leisuzz leisuzz commented Nov 1, 2024

What does this PR do?

Reduce memory cost while training flux model, and reduce OOM issues.

Helps to fix issues: #9790, #9732, #9501, #9500, and #9156.

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@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.

@sayakpaul
Copy link
Member

Thanks for your PR. Can you also maybe report the memory it helps in saving?

@leisuzz
Copy link
Contributor Author

leisuzz commented Nov 1, 2024

With the some configs, without these modifications, I faced OOM with batch size = 1. After modification, I can run the test with batch size = 16.
I'm using 8 processors, during the training process, one processor will reach 56362 MB without modification, which is 11000MB+ larger than others (45807MB). With the modification, the largest is 48761MB, which is 3000MB+ larger than others (45807MB) all processors have the similar memory cost.
The issue happens in one processor stores and saves memory will cause OOM.

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

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

Thanks!

@sayakpaul sayakpaul merged commit a98a839 into huggingface:main Nov 1, 2024
7 of 8 checks passed
a-r-r-o-w pushed a commit that referenced this pull request Nov 1, 2024
* Improve NPU performance

* Improve NPU performance

* Improve NPU performance

* Improve NPU performance

* [bugfix] bugfix for npu free memory

* [bugfix] bugfix for npu free memory

* [bugfix] bugfix for npu free memory

* Reduce memory cost for flux training process

---------

Co-authored-by: 蒋硕 <[email protected]>
Co-authored-by: Sayak Paul <[email protected]>
@@ -1908,6 +1911,9 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
ignore_patterns=["step_*", "epoch_*"],
)

images = None
Copy link
Collaborator

@linoytsaban linoytsaban Nov 1, 2024

Choose a reason for hiding this comment

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

@leisuzz @sayakpaul great contribution thanks! I want to add these changes to the advanced flux script too, is this last part specifically helpful? since it happens after the weights are already uploaded/pushed to the hub

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It actually depends on the limit of memory and the user's hardware, but it will clean some memory usages for sure

linoytsaban added a commit to linoytsaban/diffusers that referenced this pull request Nov 1, 2024
sayakpaul added a commit that referenced this pull request Nov 19, 2024
)

* memory improvement as done here: #9829

* fix bug

* fix bug

* style

---------

Co-authored-by: Sayak Paul <[email protected]>
sayakpaul added a commit that referenced this pull request Dec 23, 2024
* Improve NPU performance

* Improve NPU performance

* Improve NPU performance

* Improve NPU performance

* [bugfix] bugfix for npu free memory

* [bugfix] bugfix for npu free memory

* [bugfix] bugfix for npu free memory

* Reduce memory cost for flux training process

---------

Co-authored-by: 蒋硕 <[email protected]>
Co-authored-by: Sayak Paul <[email protected]>
sayakpaul added a commit that referenced this pull request Dec 23, 2024
)

* memory improvement as done here: #9829

* fix bug

* fix bug

* style

---------

Co-authored-by: Sayak Paul <[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