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

Use importlib.util.find_spec to check if lm_eval is installed instead of trying to import it #1023

Merged

Conversation

ringohoffman
Copy link
Contributor

There is a circular dependency when trying to import lm_eval inside torchao. The chain is like this:

torchao -> lm_eval -> transformers.pipelines -> torchao

And results in the following error:

RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback): cannot import name 'quantize_' from partially initialized module 'torchao.quantization'

which

  1. causes _lm_eval_available to be erroneously set to False, even if lm_eval is available
  2. interrupts lm_eval's initialization, leaving it partially initialized

you can observe this with:

>>> import torchao
>>> import lm_eval.__main__
>>> import lm_eval.api.registry
>> lm_eval.api.registry
AttributeError: module 'lm_eval' has no attribute 'api'

Having a bare except clause here was suppressing this circular import error, which from glancing around seems kind of like a general pattern in this code base. It might be worth reconsidering this pattern.

… of trying to import it

There is a circular dependency when trying to import lm_eval inside torchao. The chain is like this:

torchao -> lm_eval -> transformers.pipelines -> torchao

And results in the following error:

RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback):
cannot import name 'quantize_' from partially initialized module 'torchao.quantization'

which

1. causes _lm_eval_available to be erroneously set to False, even if lm_eval is available
2. interrupts lm_eval's initialization, leaving it partially initialized

you can observe this with:

>>> import torchao
>>> import lm_eval.__main__
>>> import lm_eval.api.registry
>> lm_eval.api.registry
AttributeError: module 'lm_eval' has no attribute 'api'

Having a bare except clause here was suppressing this circular import error, which from glancing around seems kind of like a general pattern in this code base. It might be worth reconsidering this pattern.
Copy link

pytorch-bot bot commented Oct 7, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/1023

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 198ac0d with merge base c187f87 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 7, 2024
@msaroufim msaroufim self-requested a review October 7, 2024 01:34
@msaroufim
Copy link
Member

Yeah not a huge fan of naked excepts either, if you wanna submit a few patches, would be happy to merge them

@msaroufim msaroufim merged commit e2301e9 into pytorch:main Oct 7, 2024
17 checks passed
@ringohoffman ringohoffman deleted the fix-lm_eval-circular-dependency branch October 7, 2024 03:06
jainapurva pushed a commit that referenced this pull request Oct 15, 2024
…tead of trying to import it (#1023)

Use importlib.util.find_spec to check if lm_eval is installed instead of trying to import it

There is a circular dependency when trying to import lm_eval inside torchao. The chain is like this:

torchao -> lm_eval -> transformers.pipelines -> torchao

And results in the following error:

RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback):
cannot import name 'quantize_' from partially initialized module 'torchao.quantization'

which

1. causes _lm_eval_available to be erroneously set to False, even if lm_eval is available
2. interrupts lm_eval's initialization, leaving it partially initialized

you can observe this with:

>>> import torchao
>>> import lm_eval.__main__
>>> import lm_eval.api.registry
>> lm_eval.api.registry
AttributeError: module 'lm_eval' has no attribute 'api'

Having a bare except clause here was suppressing this circular import error, which from glancing around seems kind of like a general pattern in this code base. It might be worth reconsidering this pattern.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants