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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve empty import time of torchvision #8497

Open
bschindler opened this issue Jun 18, 2024 · 2 comments
Open

Improve empty import time of torchvision #8497

bschindler opened this issue Jun 18, 2024 · 2 comments

Comments

@bschindler
Copy link

馃殌 The feature

When importing torchvision, a number of libraries are imported by default for more niche functionality of the library. To improve import time, I would favor delaying those imports to when they are needed

Motivation, pitch

In my case, it is the av library in particular that contributes to the import time:
image

(this assumes that torch, dynamo and onnx are already imported).

The import of av can easily be avoided as it is not needed by default.

Alternatives

No response

Additional context

I checked the code and I found this code here:

try:
    import av

    av.logging.set_level(av.logging.ERROR)
    if not hasattr(av.video.frame.VideoFrame, "pict_type"):
        av = ImportError(
            """\
Your version of PyAV is too old for the necessary video operations in torchvision.
If you are on Python 3.5, you will have to build from source (the conda-forge
packages are not up-to-date).  See
https://github.com/mikeboers/PyAV#installation for instructions on how to
install PyAV on your system.
"""
        )
except ImportError:
    av = ImportError(
        """\
PyAV is not installed, and is necessary for the video operations in torchvision.
See https://github.com/mikeboers/PyAV#installation for instructions on how to
install PyAV on your system.
"""
    )

The pict_type got added somewhere in the 0.5 range (released around 2020), 6.0 followed shortly. So I would suggest to change this test to not import av but the use importlib to check the version which would make this go away. This applies both to torchvision/io/video_reader.py as well as torchvision/io/video.py. I also wonder whether the logging call is still required given so much has changed since this code was written.

@NicolasHug
Copy link
Member

Hi @bschindler , thanks for the report. Happy to consider a PR to import av only when the io module is imported. Or... to only import io when io is needed. Or both...

Considering the cluncky way by which we load the extensions and make them available, I suspect this is the kind of work that sounds a lot easier than it actually is, but we'll see.

@bschindler
Copy link
Author

I briefly looked at it, a major issue is this line in the init file of torchvision:

from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils

This unconditionally imports io, making it part of the public interface. I could remove it from there but that would be a breaking change. Would you be in favor of that?

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

No branches or pull requests

2 participants