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

Fix memory leak in fp8 causing OOM (and potentially 3x vRAM usage) #2089

Merged
merged 5 commits into from
Oct 31, 2023

Conversation

muellerzr
Copy link
Collaborator

@muellerzr muellerzr commented Oct 26, 2023

What does this PR do?

Doing .copy() like we were before leads to a huge memory leak during fp8, and also putting the model on CUDA after converting the layers can reduce some memory.

Example benchmark:
Model: "meta-llama/Llama-2-7b-hf"
Note: on FP8 weights are in bf16 beforehand
Before fix:
Memory in FP32: 25.23 GB
Memory on BF16: 12.61 GB
Memory on FP8: 37.23 GB <- Warning sign that something is amiss!

After fix:
Memory on FP8:

  1. Doing .cuda() before (current implementation in .prepare()): 12.87 GB
  2. Doing .cuda() after: 12.61 GB

Fixes # (issue)

(Finally) fixes #1430

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

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.

@BenjaminBossan @LysandreJik

@muellerzr muellerzr added the bug Something isn't working label Oct 26, 2023
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Oct 26, 2023

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

@muellerzr muellerzr changed the title Fix memory leak in fp8 causing OOM Fix memory leak in fp8 causing OOM (and potentially 3x vRAM usage) Oct 26, 2023
Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

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

Very good find, thanks for investigating and fixing this.

The movement of the code block in accelerator.py is presumably due to the order in which convert_model needs to be called?

(note for later: the 2nd error message in that block is messed up)

@muellerzr
Copy link
Collaborator Author

@BenjaminBossan correct, it's that note about how we can save extra vram by converting to fp8 before moving the model to CUDA

@overvalidated
Copy link

So that's how it should've been done... Thank you, it's amazing to see this is finally resolved.
Even though I have already traded my 4090 for 3090s xD.

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.

Thanks for investigating this @muellerzr!

@muellerzr muellerzr merged commit 2935057 into main Oct 31, 2023
26 checks passed
@muellerzr muellerzr deleted the fix-fp8 branch October 31, 2023 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FP8 training causes OOM
5 participants