extension.py 2.59 KB
Newer Older
1
_HAS_OPS = False
2
3


4
5
6
7
def _has_ops():
    return False


8
9
def _register_extensions():
    import os
10
    import importlib
11
12
13
14
    import torch

    # load the custom_op_library and register the custom ops
    lib_dir = os.path.dirname(__file__)
15
16
17
18
19
20
21
22
23
24
    loader_details = (
        importlib.machinery.ExtensionFileLoader,
        importlib.machinery.EXTENSION_SUFFIXES
    )

    extfinder = importlib.machinery.FileFinder(lib_dir, loader_details)
    ext_specs = extfinder.find_spec("_C")
    if ext_specs is None:
        raise ImportError
    torch.ops.load_library(ext_specs.origin)
25
26
27
28
29


try:
    _register_extensions()
    _HAS_OPS = True
30
31
32

    def _has_ops():  # noqa: F811
        return True
33
34
35
36
except (ImportError, OSError):
    pass


37
38
39
40
41
42
43
44
45
46
47
48
49
def _assert_has_ops():
    if not _has_ops():
        raise RuntimeError(
            "Couldn't load custom C++ ops. This can happen if your PyTorch and "
            "torchvision versions are incompatible, or if you had errors while compiling "
            "torchvision from source. For further information on the compatible versions, check "
            "https://github.com/pytorch/vision#installation for the compatibility matrix. "
            "Please check your PyTorch version with torch.__version__ and your torchvision "
            "version with torchvision.__version__ and verify if they are compatible, and if not "
            "please reinstall torchvision so that it matches your PyTorch install."
        )


50
def _check_cuda_version():
51
52
53
    """
    Make sure that CUDA versions match between the pytorch install and torchvision install
    """
54
55
    if not _HAS_OPS:
        return -1
56
    import torch
57
58
59
    _version = torch.ops.torchvision._cuda_version()
    if _version != -1 and torch.version.cuda is not None:
        tv_version = str(_version)
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        if int(tv_version) < 10000:
            tv_major = int(tv_version[0])
            tv_minor = int(tv_version[2])
        else:
            tv_major = int(tv_version[0:2])
            tv_minor = int(tv_version[3])
        t_version = torch.version.cuda
        t_version = t_version.split('.')
        t_major = int(t_version[0])
        t_minor = int(t_version[1])
        if t_major != tv_major or t_minor != tv_minor:
            raise RuntimeError("Detected that PyTorch and torchvision were compiled with different CUDA versions. "
                               "PyTorch has CUDA Version={}.{} and torchvision has CUDA Version={}.{}. "
                               "Please reinstall the torchvision that matches your PyTorch install."
                               .format(t_major, t_minor, tv_major, tv_minor))
75
76
77
78
    return _version


_check_cuda_version()