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

Avoid circular imports when lightning-habana or lightning-graphcore is installed #18226

Merged
merged 4 commits into from
Aug 3, 2023

Conversation

awaelchli
Copy link
Contributor

@awaelchli awaelchli commented Aug 3, 2023

What does this PR do?

Fixes #17897
Fixes #17810

Our CI has several warnings about the circular import. To reproduce:

  1. pip install lightning-habana or lightning-graphcore
  2. import lightning as L

This PR makes the import checks lazy, so they don't run during import resolution, and only when they are needed.

cc @carmocca @Borda

@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Aug 3, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 3, 2023

⚡ Required checks status: All passing 🟢

Groups summary

🟢 pytorch_lightning: Tests workflow
Check ID Status
pl-cpu (macOS-11, lightning, 3.8, 1.11) success
pl-cpu (macOS-11, lightning, 3.9, 1.12) success
pl-cpu (macOS-11, lightning, 3.10, 1.13) success
pl-cpu (macOS-11, lightning, 3.10, 2.0) success
pl-cpu (macOS-11, lightning, 3.8, 1.11, oldest) success
pl-cpu (ubuntu-20.04, lightning, 3.8, 1.11) success
pl-cpu (ubuntu-20.04, lightning, 3.9, 1.12) success
pl-cpu (ubuntu-20.04, lightning, 3.10, 1.13) success
pl-cpu (ubuntu-20.04, lightning, 3.10, 2.0) success
pl-cpu (ubuntu-20.04, lightning, 3.8, 1.11, oldest) success
pl-cpu (windows-2022, lightning, 3.8, 1.11) success
pl-cpu (windows-2022, lightning, 3.9, 1.12) success
pl-cpu (windows-2022, lightning, 3.10, 1.13) success
pl-cpu (windows-2022, lightning, 3.10, 2.0) success
pl-cpu (windows-2022, lightning, 3.8, 1.11, oldest) success
pl-cpu (macOS-11, pytorch, 3.8, 1.13) success
pl-cpu (ubuntu-20.04, pytorch, 3.8, 1.13) success
pl-cpu (windows-2022, pytorch, 3.8, 1.13) success

These checks are required after the changes to src/lightning/pytorch/trainer/configuration_validator.py, src/lightning/pytorch/trainer/connectors/accelerator_connector.py, src/lightning/pytorch/trainer/connectors/data_connector.py, src/lightning/pytorch/trainer/setup.py, src/lightning/pytorch/utilities/imports.py, tests/tests_pytorch/trainer/connectors/test_accelerator_connector.py.

🟢 pytorch_lightning: Azure GPU
Check ID Status
pytorch-lightning (GPUs) success

These checks are required after the changes to src/lightning/pytorch/trainer/configuration_validator.py, src/lightning/pytorch/trainer/connectors/accelerator_connector.py, src/lightning/pytorch/trainer/connectors/data_connector.py, src/lightning/pytorch/trainer/setup.py, src/lightning/pytorch/utilities/imports.py, tests/tests_pytorch/trainer/connectors/test_accelerator_connector.py.

🟢 pytorch_lightning: Benchmarks
Check ID Status
lightning.Benchmarks success

These checks are required after the changes to src/lightning/pytorch/trainer/configuration_validator.py, src/lightning/pytorch/trainer/connectors/accelerator_connector.py, src/lightning/pytorch/trainer/connectors/data_connector.py, src/lightning/pytorch/trainer/setup.py, src/lightning/pytorch/utilities/imports.py.

🟢 pytorch_lightning: Docs
Check ID Status
make-doctest (pytorch) success
make-html (pytorch) success

These checks are required after the changes to src/lightning/pytorch/trainer/configuration_validator.py, src/lightning/pytorch/trainer/connectors/accelerator_connector.py, src/lightning/pytorch/trainer/connectors/data_connector.py, src/lightning/pytorch/trainer/setup.py, src/lightning/pytorch/utilities/imports.py.

🟢 mypy
Check ID Status
mypy success

These checks are required after the changes to src/lightning/pytorch/trainer/configuration_validator.py, src/lightning/pytorch/trainer/connectors/accelerator_connector.py, src/lightning/pytorch/trainer/connectors/data_connector.py, src/lightning/pytorch/trainer/setup.py, src/lightning/pytorch/utilities/imports.py.

🟢 install
Check ID Status
install-pkg (ubuntu-22.04, app, 3.8) success
install-pkg (ubuntu-22.04, app, 3.10) success
install-pkg (ubuntu-22.04, fabric, 3.8) success
install-pkg (ubuntu-22.04, fabric, 3.10) success
install-pkg (ubuntu-22.04, pytorch, 3.8) success
install-pkg (ubuntu-22.04, pytorch, 3.10) success
install-pkg (ubuntu-22.04, lightning, 3.8) success
install-pkg (ubuntu-22.04, lightning, 3.10) success
install-pkg (ubuntu-22.04, notset, 3.8) success
install-pkg (ubuntu-22.04, notset, 3.10) success
install-pkg (macOS-12, app, 3.8) success
install-pkg (macOS-12, app, 3.10) success
install-pkg (macOS-12, fabric, 3.8) success
install-pkg (macOS-12, fabric, 3.10) success
install-pkg (macOS-12, pytorch, 3.8) success
install-pkg (macOS-12, pytorch, 3.10) success
install-pkg (macOS-12, lightning, 3.8) success
install-pkg (macOS-12, lightning, 3.10) success
install-pkg (macOS-12, notset, 3.8) success
install-pkg (macOS-12, notset, 3.10) success
install-pkg (windows-2022, app, 3.8) success
install-pkg (windows-2022, app, 3.10) success
install-pkg (windows-2022, fabric, 3.8) success
install-pkg (windows-2022, fabric, 3.10) success
install-pkg (windows-2022, pytorch, 3.8) success
install-pkg (windows-2022, pytorch, 3.10) success
install-pkg (windows-2022, lightning, 3.8) success
install-pkg (windows-2022, lightning, 3.10) success
install-pkg (windows-2022, notset, 3.8) success
install-pkg (windows-2022, notset, 3.10) success

These checks are required after the changes to src/lightning/pytorch/trainer/configuration_validator.py, src/lightning/pytorch/trainer/connectors/accelerator_connector.py, src/lightning/pytorch/trainer/connectors/data_connector.py, src/lightning/pytorch/trainer/setup.py, src/lightning/pytorch/utilities/imports.py.


Thank you for your contribution! 💜

Note
This comment is automatically generated and updates for 60 minutes every 180 seconds. If you have any other questions, contact carmocca for help.

@awaelchli awaelchli added ci Continuous Integration bug Something isn't working labels Aug 3, 2023
@awaelchli awaelchli added this to the 2.0.x milestone Aug 3, 2023
@github-actions github-actions bot removed the ci Continuous Integration label Aug 3, 2023
@awaelchli awaelchli added the ci Continuous Integration label Aug 3, 2023
def _lightning_graphcore_available() -> bool:
# This is defined as a function instead of a constant to avoid circular imports, because `lightning_graphcore`
# also imports Lightning
return RequirementCache("lightning-graphcore") and _try_import_module("lightning_graphcore")
Copy link
Contributor

Choose a reason for hiding this comment

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

All of this could probably be replaced with RequirementCache("lightning-graphcore", "lightning_graphcore") which is already lazy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The try_import_module came from #17829 which relaxes the import error with a warning, so I assumed we would want to keep it.

Copy link
Contributor

Choose a reason for hiding this comment

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

The second argument in RequirementCache will try to import the module by name if the package check fails. It was added for cases like this one where the package is properly installed but not importable or viceversa. You'd want to test it though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, I tested and this still results in errors. Until these packages resolve the broken API calls or add logic to be compatible with multiple lightning versions, I think we need to go with this relaxed import for now.

/Users/adrian/miniconda3/envs/lightning/bin/python /Users/adrian/repositories/lightning/examples/pytorch/bug_report/bug_report_model.py 
Traceback (most recent call last):
  File "/Users/adrian/repositories/lightning/examples/pytorch/bug_report/bug_report_model.py", line 66, in <module>
    run()
  File "/Users/adrian/repositories/lightning/examples/pytorch/bug_report/bug_report_model.py", line 52, in run
    trainer = Trainer(
  File "/Users/adrian/repositories/lightning/src/lightning/pytorch/utilities/argparse.py", line 69, in insert_env_defaults
    return fn(self, **kwargs)
  File "/Users/adrian/repositories/lightning/src/lightning/pytorch/trainer/trainer.py", line 401, in __init__
    self._accelerator_connector = _AcceleratorConnector(
  File "/Users/adrian/repositories/lightning/src/lightning/pytorch/trainer/connectors/accelerator_connector.py", line 123, in __init__
    _register_external_accelerators_and_strategies()
  File "/Users/adrian/repositories/lightning/src/lightning/pytorch/trainer/connectors/accelerator_connector.py", line 698, in _register_external_accelerators_and_strategies
    from lightning_habana import HPUAccelerator, HPUParallelStrategy, SingleHPUStrategy
  File "/Users/adrian/miniconda3/envs/lightning/lib/python3.10/site-packages/lightning_habana/__init__.py", line 19, in <module>
    from lightning_habana.pytorch.accelerator import HPUAccelerator
  File "/Users/adrian/miniconda3/envs/lightning/lib/python3.10/site-packages/lightning_habana/pytorch/__init__.py", line 20, in <module>
    from lightning_habana.pytorch.strategies.deepspeed import HPUDeepSpeedStrategy
  File "/Users/adrian/miniconda3/envs/lightning/lib/python3.10/site-packages/lightning_habana/pytorch/strategies/__init__.py", line 15, in <module>
    from lightning_habana.pytorch.strategies.deepspeed import HPUDeepSpeedStrategy
  File "/Users/adrian/miniconda3/envs/lightning/lib/python3.10/site-packages/lightning_habana/pytorch/strategies/deepspeed.py", line 42, in <module>
    from lightning.pytorch.overrides.base import _LightningModuleWrapperBase, _LightningPrecisionModuleWrapperBase
ModuleNotFoundError: No module named 'lightning.pytorch.overrides.base'

@mergify mergify bot added the has conflicts label Aug 3, 2023
@github-actions github-actions bot removed the ci Continuous Integration label Aug 3, 2023
@mergify mergify bot removed the has conflicts label Aug 3, 2023
@awaelchli awaelchli force-pushed the bugfix/circular-imports branch 2 times, most recently from ecf8889 to 76e4c10 Compare August 3, 2023 20:12
@mergify mergify bot added the ready PRs ready to be merged label Aug 3, 2023
@awaelchli awaelchli merged commit 0aeeb60 into master Aug 3, 2023
@awaelchli awaelchli deleted the bugfix/circular-imports branch August 3, 2023 20:57
@Borda Borda modified the milestones: 2.0.x, 2.1 Aug 14, 2023
@Borda
Copy link
Member

Borda commented Aug 29, 2023

@awaelchli could you pls check and eventually cherry-pick this into #18420

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pl Generic label for PyTorch Lightning package ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Circular import prevents using IPU Backend "String", "auto" support with trainer for external plugins
3 participants