_internally_replaced_utils.py 1.36 KB
Newer Older
1
import importlib.machinery
2
import os
3

4
5
6
7
8
9
from torch.hub import _get_torch_home


_HOME = os.path.join(_get_torch_home(), "datasets", "vision")
_USE_SHARDED_DATASETS = False

10

11
12
13
14
15
16
17
18
19
def _download_file_from_remote_location(fpath: str, url: str) -> None:
    pass


def _is_remote_location_available() -> bool:
    return False


try:
20
    from torch.hub import load_state_dict_from_url  # noqa: 401
21
except ImportError:
22
    from torch.utils.model_zoo import load_url as load_state_dict_from_url  # noqa: 401
23
24


25
26
27
def _get_extension_path(lib_name):

    lib_dir = os.path.dirname(__file__)
28
    if os.name == "nt":
29
30
31
        # Register the main torchvision library location on the default DLL path
        import ctypes

32
33
        kernel32 = ctypes.WinDLL("kernel32.dll", use_last_error=True)
        with_load_library_flags = hasattr(kernel32, "AddDllDirectory")
34
35
36
37
38
        prev_error_mode = kernel32.SetErrorMode(0x0001)

        if with_load_library_flags:
            kernel32.AddDllDirectory.restype = ctypes.c_void_p

39
        os.add_dll_directory(lib_dir)
40
41
42

        kernel32.SetErrorMode(prev_error_mode)

43
    loader_details = (importlib.machinery.ExtensionFileLoader, importlib.machinery.EXTENSION_SUFFIXES)
44
45
46
47
48
49
50

    extfinder = importlib.machinery.FileFinder(lib_dir, loader_details)
    ext_specs = extfinder.find_spec(lib_name)
    if ext_specs is None:
        raise ImportError

    return ext_specs.origin