Skip to content

Commit

Permalink
[fbsync] [Windows] Workaround for loading bundled DLLs (#4893)
Browse files Browse the repository at this point in the history
Summary:
* [Windows] Workaround for loading bundled DLLs

Python-3.8+ adds `add_dll_directory` call, see https://docs.python.org/3/whatsnew/3.8.html#ctypes
Simulate this behaviour on older versions of Python runtime by calling
`LoadLibraryExW` with the appropriate flags

Fixes #4787

Reviewed By: prabhat00155

Differential Revision: D33253476

fbshipit-source-id: bc462bf1c3b161646b258abc495c46f32aa1b459
  • Loading branch information
datumbox authored and facebook-github-bot committed Dec 21, 2021
1 parent ca3f6bb commit 66c435a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
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}")


class ImageReadMode(Enum):
Expand Down

0 comments on commit 66c435a

Please sign in to comment.