huggingface / pytorch-image-models

The largest collection of PyTorch image encoders / backbones. Including train, eval, inference, export scripts, and pretrained weights -- ResNet, ResNeXT, EfficientNet, NFNet, Vision Transformer (ViT), MobileNetV4, MobileNet-V3 & V2, RegNet, DPN, CSPNet, Swin Transformer, MaxViT, CoAtNet, ConvNeXt, and more
https://huggingface.co/docs/timm
Apache License 2.0
32.3k stars 4.76k forks source link

[FEATURE] Allow tensoreflow not to be loaded when timm is imported #1460

Closed mosheliv closed 2 years ago

mosheliv commented 2 years ago

Is your feature request related to a problem? Please describe. Currently, at least on my computer, importing timm cause tensorflow to load. This cause several problems, like taking over gpu, extended load time, confusing messages etc. To overcome this, i set CUDA_VISIBLE_DEVICES to "" before loading timm and restore it afterwards, but this is awkward.

Describe the solution you'd like I am not sure how this should be handled. perhaps an optional module or init that will load the tf related things? or "lazy" loading that will load tf only if you use something tf?

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

rwightman commented 2 years ago

@mosheliv you'll have to be more specific as to how/what is importing tensorflow (as in what you are doing when it happens) ...

There are two locations where tensorflow is imported in timm, and they are both lazy. One is if you use TFDS datasets https://github.com/rwightman/pytorch-image-models/blob/master/timm/data/parsers/parser_tfds.py#L14-L33 ... but it prevents TF from eating the GPUs

the other is if you enable an old TF pre-processing option https://github.com/rwightman/pytorch-image-models/blob/master/timm/data/tf_preprocessing.py

But both of those files are only imported if the feature is used... https://github.com/rwightman/pytorch-image-models/blob/master/timm/data/transforms_factory.py#L194-L199 https://github.com/rwightman/pytorch-image-models/blob/master/timm/data/parsers/parser_factory.py#L17-L19

mosheliv commented 2 years ago

here is a sample code:

import timm
print(timm.__version__)

here is the output:

python3 test.py 
2022-09-07 15:46:09.919410: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2022-09-07 15:46:11.214101: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-09-07 15:46:11.214193: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-09-07 15:46:11.214209: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
2022-09-07 15:46:19.793729: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1616] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 4911 MB memory:  -> device: 0, name: NVIDIA Quadro GV100, pci bus id: 0000:04:00.0, compute capability: 7.0
2022-09-07 15:46:19.794886: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1616] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 11124 MB memory:  -> device: 1, name: NVIDIA GeForce GTX TITAN X, pci bus id: 0000:03:00.0, compute capability: 5.2
0.6.7
rwightman commented 2 years ago

@mosheliv that does not happen from 0.6.7 as installed from pypi or the current master, are there any extra imports or code added somewhere?

mosheliv commented 2 years ago

OK, thanks for the prompt reply... I'll check what's causing it. Very perplexing. must be something timm is importing.

On Wed, 7 Sept 2022 at 15:59, Ross Wightman @.***> wrote:

@mosheliv https://github.com/mosheliv that does not happen from 0.6.7 as installed from pypi or the current master

— Reply to this email directly, view it on GitHub https://github.com/rwightman/pytorch-image-models/issues/1460#issuecomment-1238879479, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC7IWC5ALC4OAWMRDS5YTVTV5AHJNANCNFSM6AAAAAAQGK6TFA . You are receiving this because you were mentioned.Message ID: @.***>

rwightman commented 2 years ago

@mosheliv timm is very light on external dependencies ... seems more likely to be some sort of environment issue, ie python env that's somehow fully loading all models despite the__init__, etc .... you could try commenting out the two tensorflow imports that do exist, and then re-add and maybe throw and dump a callstack from them if removing them fixes the problem, you might see what the import chain is in the callstack

In a normal Python3 environment I never see this issue. I would fix it if I did as I agree it's a problem, and that's why I prevented it being imported unless the TF specific features are used. I also run in environments with both TF and PyTorch quite often.

mosheliv commented 2 years ago

I put a raise in the tf init module... so, this is what I got:

  File "test.py", line 1, in <module>
    import timm
  File "/home/m/.local/lib/python3.7/site-packages/timm/__init__.py", line
2, in <module>
    from .models import create_model, list_models, is_model, list_modules,
model_entrypoint, \
  File
"/home/m/.local/lib/python3.7/site-packages/timm/models/__init__.py", line
1, in <module>
    from .beit import *
  File "/home/m/.local/lib/python3.7/site-packages/timm/models/beit.py",
line 30, in <module>
    from .helpers import build_model_with_cfg
  File "/home/m/.local/lib/python3.7/site-packages/timm/models/helpers.py",
line 22, in <module>
    from .hub import has_hf_hub, download_cached_file,
load_state_dict_from_hf
  File "/home/m/.local/lib/python3.7/site-packages/timm/models/hub.py",
line 17, in <module>
    from huggingface_hub import HfApi, HfFolder, Repository,
hf_hub_download, hf_hub_url
  File
"/home/m/.local/lib/python3.7/site-packages/huggingface_hub/__init__.py",
line 37, in <module>
    from .keras_mixin import (
  File
"/home/m/.local/lib/python3.7/site-packages/huggingface_hub/keras_mixin.py",
line 19, in <module>
    import tensorflow as tf
  File "/home/m/.local/lib/python3.7/site-packages/tensorflow/__init__.py",
line 29, in <module>

seems like you are calling pytorch hub that loads keras that loads tf... I'll try perhaps and update the hub module

On Wed, 7 Sept 2022 at 16:16, Ross Wightman @.***> wrote:

@mosheliv https://github.com/mosheliv timm is very light on external dependencies ... seems more likely to be some sort of environment issue, ie python env that's fully loading all models despite the init, etc .... you could try commenting out the two tensorflow imports that do exist, and then maybe throw and dump a callstack from them if that is the case

In a normal Python3 environment I never see this issue. I would fix it if I did as I agree it's a problem, and that's why I prevented it being imported unless the TF specific features are used. I also run in environments with both TF and PyTorch quite often.

— Reply to this email directly, view it on GitHub https://github.com/rwightman/pytorch-image-models/issues/1460#issuecomment-1238888163, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC7IWC57LWCMAY2YBBFUKW3V5AJILANCNFSM6AAAAAAQGK6TFA . You are receiving this because you were mentioned.Message ID: @.***>

mosheliv commented 2 years ago

updated huggingface_hub, all is well. Thank you again!

rwightman commented 2 years ago

@mosheliv good to know, and surprising that slipped through hugginface_hub, looks they added tests to prevent it from happening in future

mosheliv commented 2 years ago

It was a very old version. I just didn't realise it was loaded by timm. Same happened with transformers. Both are fixed in current version. Who would have thought such a small fix will bring me so much happiness lol

On Wed, 7 Sep 2022, 17:05 Ross Wightman, @.***> wrote:

@mosheliv https://github.com/mosheliv good to know, and surprising that slipped through hugginface_hub, looks they added tests to prevent it from happening in future

— Reply to this email directly, view it on GitHub https://github.com/rwightman/pytorch-image-models/issues/1460#issuecomment-1238911998, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC7IWC2MVY7J4YMNSPC2KUDV5APB7ANCNFSM6AAAAAAQGK6TFA . You are receiving this because you were mentioned.Message ID: @.***>

mosheliv commented 2 years ago

sorry, huggingface hub

On Wed, 7 Sept 2022 at 16:22, Moshe Livne @.***> wrote:

I put a raise in the tf init module... so, this is what I got:

  File "test.py", line 1, in <module>
    import timm
  File "/home/m/.local/lib/python3.7/site-packages/timm/__init__.py", line
2, in <module>
    from .models import create_model, list_models, is_model, list_modules,
model_entrypoint, \
  File
"/home/m/.local/lib/python3.7/site-packages/timm/models/__init__.py", line
1, in <module>
    from .beit import *
  File "/home/m/.local/lib/python3.7/site-packages/timm/models/beit.py",
line 30, in <module>
    from .helpers import build_model_with_cfg
  File
"/home/m/.local/lib/python3.7/site-packages/timm/models/helpers.py", line
22, in <module>
    from .hub import has_hf_hub, download_cached_file,
load_state_dict_from_hf
  File "/home/m/.local/lib/python3.7/site-packages/timm/models/hub.py",
line 17, in <module>
    from huggingface_hub import HfApi, HfFolder, Repository,
hf_hub_download, hf_hub_url
  File
"/home/m/.local/lib/python3.7/site-packages/huggingface_hub/__init__.py",
line 37, in <module>
    from .keras_mixin import (
  File
"/home/m/.local/lib/python3.7/site-packages/huggingface_hub/keras_mixin.py",
line 19, in <module>
    import tensorflow as tf
  File
"/home/m/.local/lib/python3.7/site-packages/tensorflow/__init__.py", line
29, in <module>

seems like you are calling pytorch hub that loads keras that loads tf... I'll try perhaps and update the hub module

On Wed, 7 Sept 2022 at 16:16, Ross Wightman @.***> wrote:

@mosheliv https://github.com/mosheliv timm is very light on external dependencies ... seems more likely to be some sort of environment issue, ie python env that's fully loading all models despite the init, etc .... you could try commenting out the two tensorflow imports that do exist, and then maybe throw and dump a callstack from them if that is the case

In a normal Python3 environment I never see this issue. I would fix it if I did as I agree it's a problem, and that's why I prevented it being imported unless the TF specific features are used. I also run in environments with both TF and PyTorch quite often.

— Reply to this email directly, view it on GitHub https://github.com/rwightman/pytorch-image-models/issues/1460#issuecomment-1238888163, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC7IWC57LWCMAY2YBBFUKW3V5AJILANCNFSM6AAAAAAQGK6TFA . You are receiving this because you were mentioned.Message ID: @.***>