Skip to content

Fix circular import between utils.bnb and big_modeling - #4174

Open
shubhxho wants to merge 2 commits into
huggingface:mainfrom
shubhxho:fix/circular-import-bnb-big-modeling
Open

Fix circular import between utils.bnb and big_modeling#4174
shubhxho wants to merge 2 commits into
huggingface:mainfrom
shubhxho:fix/circular-import-bnb-big-modeling

Conversation

@shubhxho

Copy link
Copy Markdown

Fixes #4173

Summary

accelerate.utils.bnb imported dispatch_model and init_empty_weights at module level, which closed a cycle:

utilsbnbbig_modelinghooksutils

Single-threaded imports usually survived by accident. Concurrent imports of accelerate.utils and accelerate.big_modeling failed with a partial-initialization ImportError.

Those names are only used inside load_and_quantize_model and get_keys_to_not_convert, so they are imported there instead. Public exports are unchanged.

The diagnosis and this cut are from @adrihans on #4173.

Tests

pytest tests/test_imports.py::ConcurrentImportTester -sv

Passed 4 times locally (Python 3.12, torch 2.13.0, macOS arm64). The same test fails on unpatched main with:

ImportError: cannot import name 'dispatch_model' from partially initialized module 'accelerate.big_modeling'

AI assistance was used to apply the change and write the regression test. I reviewed the diff.

Import dispatch_model/init_empty_weights only inside the functions that
need them so concurrent imports of accelerate.utils and
accelerate.big_modeling no longer hit a partial-initialization
ImportError.

Fixes huggingface#4173
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.

Circular import between accelerate.utils and accelerate.big_modeling breaks concurrent imports

1 participant