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

Lazy import of bitsandbytes #1230

Merged
merged 9 commits into from
Dec 7, 2023

Conversation

BenjaminBossan
Copy link
Member

@BenjaminBossan BenjaminBossan commented Dec 7, 2023

Should fix various issues reported here and elsewhere, e.g. #559, huggingface/accelerate#2216.

Description

Previously, we imported from bitsandbytes eagerly if the package was installed. This caused two major issues:

  • Slow loading time of PEFT (~4 sec) when bnb is installed
  • Errors with multiprocessing because bnb initializes CUDA -- users had to load PEFT lazily or else would get:

RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method

This commit fixes both issues by importing bitsandbytes lazily. PEFT import time is now reduced to ~2sec.

Notes

Implementation-wise, I use a combination of local imports and module-level __getattr__. The latter was introduced in Python 3.7 and should therefore be safe to use.

As for testing, it is not as straightforward as just adding a new unit test. The issue is that when multiple tests are run, depending on the order, bitsandbytes may already be imported once the new test runs. To avoid this issue, I created a completely separate testing script that is run in CI. It is executed on the nightly multi-GPU CI run. Locally, it passes, 🤞 that it also passes there.

If anyone has a better idea, please let me know.

Previously, we imported from bitsandbytes eagerly if the package was
installed. This caused two major issues:

- Slow loading time of PEFT (~4 sec)
- Errors with multiprocessing because bnb initializes CUDA

This commit fixes both issues by importing bitsandbytes lazily. PEFT
import time is now reduced to ~2sec.

Notes

Implementation-wise, I use a combination of local imports and
module-level __getattr__. The latter was introduced in Python 3.7 and
should therefore be safe to use.

As for testing, it is not as straightforward as just adding a new unit
test. The issue is that when multiple tests are run, depending on the
order, bitsandbytes may already be imported once the new test runs. To
avoid this issue, I created a completely separate testing script that is
run in CI.

If anyone has a better idea, please let me know.
Should not be necessary but better safe than sorry.
@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.

@BenjaminBossan BenjaminBossan changed the title Lazy import bnb Lazy import of bitsandbytes Dec 7, 2023
Copy link
Contributor

@pacman100 pacman100 left a comment

Choose a reason for hiding this comment

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

Thank you @BenjaminBossan for fixes the issues as well as making the import faster, LGTM! 🚀

Copy link
Contributor

@younesbelkada younesbelkada 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 making the import faster!

@@ -34,7 +34,7 @@ jobs:
source activate peft
pip install -e . --no-deps
pip install pytest-reportlog

Copy link
Contributor

Choose a reason for hiding this comment

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

Why this diff is here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I removed all trailing whitespace from the yml.

Copy link
Member Author

Choose a reason for hiding this comment

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

Note that I moved the test to the proper unit tests using a trick that Zach showed, so I undid all the changes here (i.e. trailing whitespaces are back).

Copy link
Contributor

@muellerzr muellerzr 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 this! Great job :)

@BenjaminBossan BenjaminBossan merged commit b467e3d into huggingface:main Dec 7, 2023
14 checks passed
@BenjaminBossan
Copy link
Member Author

Thanks a lot @muellerzr for helping with the test.

@BenjaminBossan BenjaminBossan deleted the lazy-import-bnb branch December 7, 2023 15:39
TaoSunVoyage pushed a commit to TaoSunVoyage/peft that referenced this pull request Dec 14, 2023
Previously, we imported from bitsandbytes eagerly if the package was
installed. This caused two major issues:

- Slow loading time of PEFT (~4 sec)
- Errors with multiprocessing because bnb initializes CUDA

This commit fixes both issues by importing bitsandbytes lazily. PEFT
import time is now reduced to ~2sec.

Notes

Implementation-wise, I use a combination of local imports and
module-level __getattr__. The latter was introduced in Python 3.7 and
should therefore be safe to use.
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.

5 participants