Skip to content

import lightning takes 6 seconds #19261

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

Closed
Natithan opened this issue Jan 10, 2024 · 6 comments
Closed

import lightning takes 6 seconds #19261

Natithan opened this issue Jan 10, 2024 · 6 comments
Labels
3rd party Related to a 3rd-party bug Something isn't working ver: 2.0.x

Comments

@Natithan
Copy link

Natithan commented Jan 10, 2024

Bug description

Hi,

Import lightning takes 6 seconds for me. Give this other issue that complained about 2 second loading being slow, and one of the replies indicating that newer versions are faster, I'm assuming something is going wrong for it to be so slow. Version is 2.0.9.

I also tried in google colab (version 2.1.3), where it takes about 11 seconds: link to colab

This is not a major issue, but it is annoying during debugging: there are two things that increase the loading time even more for me personally (up to 30 seconds). First, my libraries (including lightning) are on a shared server different from the one on which the code is running (this so I can share the same library files while using different servers of our lab to run code), which increases time to 15 seconds. Second, when using Pycharm debugger, it doubles the time again to 30 seconds.

Having to wait 30 seconds with every script restart really slows down my efficiency, hence I was hoping there is some way to fix this :).

Thanks in advance!

Nathan

What version are you seeing the problem on?

v2.0

How to reproduce the bug

from time import time; print("Importing lightning ..."); s = time(); import lightning; print(f"Imported lightning in {time() - s} seconds")

Error messages and logs

Importing lightning ...
Imported lightning in 5.911113023757935 seconds

Environment

Current environment
  • CUDA:
    - GPU:
    - NVIDIA GeForce RTX 3080 Ti
    - NVIDIA GeForce RTX 3080 Ti
    - NVIDIA GeForce RTX 3080 Ti
    - NVIDIA GeForce RTX 3080 Ti
    - available: True
    - version: 12.1
  • Lightning:
    - lightning: 2.0.9
    - lightning-cloud: 0.5.38
    - lightning-utilities: 0.9.0
    - pytorch-lightning: 2.0.9
    - torch: 2.1.0
    - torch2jax: 0.0.1
    - torchaudio: 2.1.0
    - torchdata: 0.7.0
    - torchmetrics: 1.2.0
    - torchtext: 0.16.0
    - torchvision: 0.16.0
  • Packages:
    - absl-py: 2.0.0
    - accelerate: 0.23.0
    - aiohttp: 3.8.5
    - aiosignal: 1.3.1
    - ale-py: 0.8.1
    - annotated-types: 0.5.0
    - anyio: 3.7.1
    - appdirs: 1.4.4
    - argon2-cffi: 23.1.0
    - argon2-cffi-bindings: 21.2.0
    - arrow: 1.2.3
    - asttokens: 2.4.1
    - async-lru: 2.0.4
    - async-timeout: 4.0.3
    - attrs: 23.1.0
    - autorom: 0.4.2
    - autorom.accept-rom-license: 0.6.1
    - babel: 2.14.0
    - backoff: 2.2.1
    - beautifulsoup4: 4.12.2
    - bitmath: 1.3.3.1
    - bitsandbytes: 0.41.1
    - bleach: 6.1.0
    - blessed: 1.20.0
    - blis: 0.7.10
    - box2d-py: 2.3.5
    - build: 1.0.3
    - cachetools: 5.3.2
    - catalogue: 2.0.9
    - certifi: 2023.7.22
    - cffi: 1.16.0
    - chardet: 4.0.0
    - charset-normalizer: 3.2.0
    - chex: 0.1.83
    - click: 7.1.2
    - cloudpickle: 3.0.0
    - cmake: 3.27.4.1
    - comm: 0.2.0
    - confection: 0.1.3
    - contourpy: 1.1.1
    - croniter: 1.4.1
    - cycler: 0.11.0
    - cymem: 2.0.7
    - cython: 3.0.5
    - datasets: 2.15.0
    - dateutils: 0.6.12
    - debugpy: 1.8.0
    - decorator: 5.1.1
    - deepdiff: 6.5.0
    - defusedxml: 0.7.1
    - deprecation: 2.1.0
    - di-engine: 0.4.9
    - di-toolkit: 0.2.0
    - di-treetensor: 0.4.1
    - dill: 0.3.7
    - docker-pycreds: 0.4.0
    - easydict: 1.9
    - en-core-web-sm: 3.6.0
    - enum-tools: 0.11.0
    - equinox: 0.11.1
    - executing: 2.0.1
    - farama-notifications: 0.0.4
    - fastapi: 0.103.1
    - fastjsonschema: 2.18.0
    - filelock: 3.12.3
    - flask: 1.1.4
    - fonttools: 4.42.1
    - fqdn: 1.5.1
    - frozenlist: 1.4.0
    - fsspec: 2023.6.0
    - gitdb: 4.0.10
    - gitpython: 3.1.37
    - google-auth: 2.23.4
    - google-auth-oauthlib: 1.1.0
    - gpustat: 1.1.1
    - graphviz: 0.20.1
    - grpcio: 1.59.2
    - gym: 0.25.1
    - gym-notices: 0.0.8
    - gymnasium: 0.29.1
    - h11: 0.14.0
    - h5py: 3.10.0
    - hbutils: 0.9.3
    - hickle: 5.0.2
    - huggingface-hub: 0.19.4
    - idna: 3.4
    - inquirer: 3.1.3
    - ipykernel: 6.27.1
    - ipython: 8.18.1
    - ipywidgets: 8.1.1
    - isoduration: 20.11.0
    - itsdangerous: 2.0.1
    - jax: 0.4.20
    - jaxlib: 0.4.20+cuda12.cudnn89
    - jaxtyping: 0.2.23
    - jedi: 0.19.1
    - jinja2: 3.1.2
    - joblib: 1.3.2
    - json5: 0.9.14
    - jsonargparse: 4.24.1
    - jsonpointer: 2.4
    - jsonschema: 4.19.1
    - jsonschema-specifications: 2023.7.1
    - jupyter: 1.0.0
    - jupyter-client: 8.6.0
    - jupyter-console: 6.6.3
    - jupyter-core: 5.3.1
    - jupyter-events: 0.9.0
    - jupyter-lsp: 2.2.1
    - jupyter-server: 2.12.1
    - jupyter-server-terminals: 0.5.0
    - jupyterlab: 4.0.9
    - jupyterlab-pygments: 0.3.0
    - jupyterlab-server: 2.25.2
    - jupyterlab-widgets: 3.0.9
    - kiwisolver: 1.4.5
    - langcodes: 3.3.0
    - lightning: 2.0.9
    - lightning-cloud: 0.5.38
    - lightning-utilities: 0.9.0
    - lightzero: 0.0.2
    - lit: 16.0.6
    - llvmlite: 0.41.1
    - markdown: 3.5.1
    - markdown-it-py: 3.0.0
    - markupsafe: 2.0.1
    - matplotlib: 3.8.1
    - matplotlib-inline: 0.1.6
    - mctx: 0.0.3
    - mdurl: 0.1.2
    - mistune: 3.0.2
    - ml-dtypes: 0.3.1
    - mpire: 2.8.0
    - mpmath: 1.3.0
    - multidict: 6.0.4
    - multiprocess: 0.70.15
    - murmurhash: 1.0.9
    - nbclient: 0.9.0
    - nbconvert: 7.12.0
    - nbformat: 5.9.2
    - nest-asyncio: 1.5.8
    - networkx: 3.1
    - nltk: 3.8.1
    - notebook: 7.0.6
    - notebook-shim: 0.2.3
    - numba: 0.58.1
    - numpy: 1.25.2
    - nvidia-cublas-cu11: 11.10.3.66
    - nvidia-cublas-cu12: 12.3.2.9
    - nvidia-cuda-cupti-cu11: 11.7.101
    - nvidia-cuda-cupti-cu12: 12.3.52
    - nvidia-cuda-nvcc-cu11: 11.8.89
    - nvidia-cuda-nvcc-cu12: 12.3.52
    - nvidia-cuda-nvrtc-cu11: 11.7.99
    - nvidia-cuda-nvrtc-cu12: 12.1.105
    - nvidia-cuda-runtime-cu11: 11.7.99
    - nvidia-cuda-runtime-cu12: 12.3.52
    - nvidia-cudnn-cu12: 8.9.2.26
    - nvidia-cufft-cu11: 10.9.0.58
    - nvidia-cufft-cu12: 11.0.11.19
    - nvidia-curand-cu11: 10.2.10.91
    - nvidia-curand-cu12: 10.3.2.106
    - nvidia-cusolver-cu11: 11.4.0.1
    - nvidia-cusolver-cu12: 11.5.3.52
    - nvidia-cusparse-cu11: 11.7.4.91
    - nvidia-cusparse-cu12: 12.1.3.153
    - nvidia-ml-py: 12.535.108
    - nvidia-nccl-cu11: 2.14.3
    - nvidia-nccl-cu12: 2.19.3
    - nvidia-nvjitlink-cu12: 12.3.52
    - nvidia-nvtx-cu11: 11.7.91
    - nvidia-nvtx-cu12: 12.1.105
    - oauthlib: 3.2.2
    - openai: 0.28.0
    - opencv-python: 4.8.1.78
    - opt-einsum: 3.3.0
    - ordered-set: 4.1.0
    - overrides: 7.4.0
    - packaging: 23.1
    - pandas: 2.1.2
    - pandocfilters: 1.5.0
    - parso: 0.8.3
    - pathtools: 0.1.2
    - pathy: 0.10.2
    - pbr: 5.11.1
    - pexpect: 4.9.0
    - pillow: 10.0.1
    - pip: 23.3.2
    - platformdirs: 3.10.0
    - portalocker: 2.8.2
    - preshed: 3.0.8
    - prometheus-client: 0.19.0
    - prompt-toolkit: 3.0.43
    - protobuf: 4.23.4
    - psutil: 5.9.6
    - ptyprocess: 0.7.0
    - pure-eval: 0.2.2
    - pyarrow: 14.0.2
    - pyarrow-hotfix: 0.6
    - pyasn1: 0.5.0
    - pyasn1-modules: 0.3.0
    - pycparser: 2.21
    - pydantic: 2.1.1
    - pydantic-core: 2.4.0
    - pygame: 2.5.2
    - pygments: 2.16.1
    - pyjwt: 2.8.0
    - pympler: 1.0.1
    - pynng: 0.7.2
    - pyparsing: 3.1.1
    - pyproject-hooks: 1.0.0
    - pyqt5: 5.15.10
    - pyqt5-qt5: 5.15.2
    - pyqt5-sip: 12.13.0
    - python-dateutil: 2.8.2
    - python-editor: 1.0.4
    - python-json-logger: 2.0.7
    - python-multipart: 0.0.6
    - pytimeparse: 1.1.8
    - pytorch-lightning: 2.0.9
    - pytz: 2023.3.post1
    - pyyaml: 6.0.1
    - pyzmq: 25.1.2
    - qtconsole: 5.5.1
    - qtpy: 2.4.1
    - readchar: 4.0.5
    - redis: 5.0.1
    - referencing: 0.30.2
    - regex: 2023.8.8
    - requests: 2.31.0
    - requests-oauthlib: 1.3.1
    - responses: 0.12.1
    - rfc3339-validator: 0.1.4
    - rfc3986-validator: 0.1.1
    - rich: 13.5.3
    - rpds-py: 0.10.3
    - rsa: 4.9
    - safetensors: 0.3.3
    - scikit-learn: 1.3.2
    - scipy: 1.11.3
    - seaborn: 0.13.0
    - send2trash: 1.8.2
    - sentence-transformers: 2.2.2
    - sentencepiece: 0.1.99
    - sentry-sdk: 1.31.0
    - setproctitle: 1.3.2
    - setuptools: 66.1.1
    - six: 1.16.0
    - smart-open: 6.4.0
    - smmap: 5.0.1
    - sniffio: 1.3.0
    - soupsieve: 2.5
    - spacy: 3.6.1
    - spacy-legacy: 3.0.12
    - spacy-loggers: 1.0.5
    - srsly: 2.4.7
    - stack-data: 0.6.3
    - starlette: 0.27.0
    - starsessions: 1.3.0
    - swig: 4.1.1
    - sympy: 1.12
    - tabulate: 0.9.0
    - tensorboard: 2.15.1
    - tensorboard-data-server: 0.7.2
    - tensorboardx: 2.6.2.2
    - terminado: 0.18.0
    - testresources: 2.0.1
    - thinc: 8.1.12
    - threadpoolctl: 3.2.0
    - tinycss2: 1.2.1
    - tokenizers: 0.13.3
    - toolz: 0.12.0
    - torch: 2.1.0
    - torch2jax: 0.0.1
    - torchaudio: 2.1.0
    - torchdata: 0.7.0
    - torchmetrics: 1.2.0
    - torchtext: 0.16.0
    - torchvision: 0.16.0
    - tornado: 6.4
    - tqdm: 4.66.1
    - traitlets: 5.10.0
    - transformers: 4.33.1
    - treevalue: 1.4.12
    - triton: 2.1.0
    - trueskill: 0.4.5
    - typeguard: 2.13.3
    - typer: 0.9.0
    - typing-extensions: 4.8.0
    - tzdata: 2023.3
    - uri-template: 1.3.0
    - urllib3: 1.26.18
    - urlobject: 2.4.3
    - uvicorn: 0.23.2
    - wandb: 0.15.11
    - wasabi: 1.1.2
    - wcwidth: 0.2.6
    - webcolors: 1.13
    - webencodings: 0.5.1
    - websocket-client: 1.6.3
    - websockets: 11.0.3
    - werkzeug: 1.0.1
    - wheel: 0.38.4
    - widgetsnbextension: 4.0.9
    - xxhash: 3.3.0
    - yapf: 0.29.0
    - yarl: 1.9.2
    - yattag: 1.15.2
    - zipp: 3.17.0
  • System:
    - OS: Linux
    - architecture:
    - 64bit
    - ELF
    - processor: x86_64
    - python: 3.11.5
    - release: 5.4.0-167-generic
    - version: Add an easy way to manipulate progressbar metrics #184-Ubuntu SMP Tue Oct 31 09:21:49 UTC 2023

More info

No response

@Natithan Natithan added bug Something isn't working needs triage Waiting to be triaged by maintainers labels Jan 10, 2024
@awaelchli
Copy link
Contributor

@Natithan Could you take a look here please: #12786
There you will find my comments on how to measure the import time and determine which package is causing it. With Lightning 2.1, all imports of optional packages are lazy, so the slowdown must come from a third-party package.

@awaelchli awaelchli added repro needed The issue is missing a reproducible example and removed needs triage Waiting to be triaged by maintainers repro needed The issue is missing a reproducible example labels Jan 10, 2024
@awaelchli
Copy link
Contributor

Note that in your colab, if I replace the lightning import with torch I get 3.56 seconds. And torchmetrics 2 seconds.

@awaelchli awaelchli added the 3rd party Related to a 3rd-party label Jan 10, 2024
@Natithan
Copy link
Author

Natithan commented Jan 10, 2024

@awaelchli indeed in the various locations I try (colab, terminal, Pycharm), torch is around 1/3rd time of lightning (so 2 seconds in terminal, and 7-8 seconds in Pycharm debugger).

Related to lightning itself, I had the impression that lighting should load faster than 6 seconds given the other issue.
But besides that, if anyone would have advice (presumably unrelated to lightning itself) on how to get my specific personal setup (taking a thumb-twiddling 30 seconds for the import statement with Pycharm debugger + loading from shared server) to be faster that would also be great! :)

E. I'll have a look at #12786 !

@Natithan
Copy link
Author

Using tuna as in #12786 (but doing import lightning instead of import pytorch_lightning as pl I get this:
image

@awaelchli
Copy link
Contributor

awaelchli commented Jan 10, 2024

Thanks.
That looks about right, and what I measured back then. If you install Lightning 2.1, you will also no longer get the "app" part. PyTorch is obviously a hard dependency, so we can't do anything about the load times there. And torchmetrics recently made an effort to do lazy imports too.

But besides that, if anyone would have advice (presumably unrelated to lightning itself) on how to get my specific personal setup (taking a thumb-twiddling 30 seconds for the import statement with Pycharm debugger + loading from shared server)

Don't know how to speed up the debugger. Generally I would say the only thing guaranteed to help here is to uninstall depencencies you don't need (i.e. transformers, wandb mabye?)

@awaelchli
Copy link
Contributor

awaelchli commented Jan 21, 2024

Some improvements carried out here Lightning-AI/torchmetrics#2171 (torchmetrics >= 1.3) and one additional PR I just sent as well. Beyond that, I don't think we can do much more. The initial import will be slow, but all subsequent ones are fast because of Python's caching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3rd party Related to a 3rd-party bug Something isn't working ver: 2.0.x
Projects
None yet
Development

No branches or pull requests

2 participants