Skip to content

[Windows] Workaround for loading bundled DLLs #4893

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

Merged
merged 5 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions torchvision/extension.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import ctypes
import os
import sys
from warnings import warn

import torch

from ._internally_replaced_utils import _get_extension_path
Expand Down Expand Up @@ -67,4 +72,22 @@ def _check_cuda_version():
return _version


def _load_library(lib_name):
lib_path = _get_extension_path(lib_name)
# On Windows Python-3.8+ has `os.add_dll_directory` call,
# which is called from _get_extension_path to configure dll search path
# Condition below adds a workaround for older versions by
# explicitly calling `LoadLibraryExW` with the following flags:
# - LOAD_LIBRARY_SEARCH_DEFAULT_DIRS (0x1000)
# - LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR (0x100)
if os.name == "nt" and sys.version_info < (3, 8):
_kernel32 = ctypes.WinDLL("kernel32.dll", use_last_error=True)
if hasattr(_kernel32, "LoadLibraryExW"):
_kernel32.LoadLibraryExW(lib_path, None, 0x00001100)
else:
warn("LoadLibraryExW is missing in kernel32.dll")

torch.ops.load_library(lib_path)


_check_cuda_version()
5 changes: 2 additions & 3 deletions torchvision/io/_video_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

import torch

from .._internally_replaced_utils import _get_extension_path
from ..extension import _load_library


try:
lib_path = _get_extension_path("video_reader")
torch.ops.load_library(lib_path)
_load_library("video_reader")
_HAS_VIDEO_OPT = True
except (ImportError, OSError):
_HAS_VIDEO_OPT = False
Expand Down
10 changes: 5 additions & 5 deletions torchvision/io/image.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from enum import Enum
from warnings import warn

import torch

from .._internally_replaced_utils import _get_extension_path
from ..extension import _load_library


try:
lib_path = _get_extension_path("image")
torch.ops.load_library(lib_path)
except (ImportError, OSError):
pass
_load_library("image")
except (ImportError, OSError) as e:
warn(f"Failed to load image Python extension: {e}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason that this was changed from pass to warn? When running pytest on my project, I see warnings like:

../.spack/.spack-env/view/lib/python3.9/site-packages/torchvision/io/image.py:11
  /Users/Adam/.spack/.spack-env/view/lib/python3.9/site-packages/torchvision/io/image.py:11: UserWarning: Failed to load image Python extension: 
    warn(f"Failed to load image Python extension: {e}")

It's unclear if this is a serious bug in my installation that I should fix, or something I can safely ignore. FWIW, I'm using the pillow-SIMD backend.



class ImageReadMode(Enum):
Expand Down