|
5 | 5 | import sysconfig |
6 | 6 | from pathlib import Path |
7 | 7 |
|
8 | | -global torch |
9 | 8 | import torch |
10 | 9 |
|
11 | 10 | from openequivariance.benchmark.logging_utils import getLogger |
12 | 11 |
|
13 | 12 | oeq_root = str(Path(__file__).parent.parent.parent) |
14 | 13 |
|
15 | | -build_ext = True |
16 | | -TORCH_COMPILE = True |
17 | | -TORCH_VERSION_CUDA_OR_HIP = torch.version.cuda or torch.version.hip |
18 | | -torch_module, generic_module = None, None |
19 | | -postprocess_kernel = lambda kernel: kernel # noqa : E731 |
| 14 | +BUILT_EXTENSION = False |
| 15 | +BUILT_EXTENSION_ERROR = None |
| 16 | + |
| 17 | +TORCH_COMPILE = False |
| 18 | +TORCH_COMPILE_ERROR = None |
20 | 19 |
|
21 | 20 | LINKED_LIBPYTHON = False |
22 | 21 | LINKED_LIBPYTHON_ERROR = None |
| 22 | + |
| 23 | +torch_module, generic_module = None, None |
| 24 | +postprocess_kernel = lambda kernel: kernel # noqa : E731 |
| 25 | + |
| 26 | + |
23 | 27 | try: |
24 | 28 | python_lib_dir = sysconfig.get_config_var("LIBDIR") |
25 | 29 | major, minor = sys.version_info.major, sys.version_info.minor |
|
33 | 37 | ) |
34 | 38 |
|
35 | 39 | LINKED_LIBPYTHON = True |
36 | | - |
37 | 40 | except Exception as e: |
38 | 41 | LINKED_LIBPYTHON_ERROR = f"Error linking libpython:\n{e}\nSysconfig variables:\n{sysconfig.get_config_vars()}" |
39 | 42 |
|
40 | | -generic_module = None |
41 | | -if not build_ext: |
42 | | - import openequivariance._torch.extlib.generic_module |
43 | 43 |
|
| 44 | +if BUILT_EXTENSION: |
| 45 | + import openequivariance._torch.extlib.generic_module |
44 | 46 | generic_module = openequivariance._torch.extlib.generic_module |
45 | | - |
46 | | -elif TORCH_VERSION_CUDA_OR_HIP: |
47 | | - from torch.utils.cpp_extension import library_paths, include_paths |
48 | | - |
49 | | - extra_cflags = ["-O3"] |
50 | | - generic_sources = ["generic_module.cpp"] |
51 | | - torch_sources = ["libtorch_tp_jit.cpp"] |
52 | | - |
53 | | - include_dirs, extra_link_args = (["util"], ["-Wl,--no-as-needed"]) |
54 | | - |
55 | | - if LINKED_LIBPYTHON: |
56 | | - extra_link_args.pop() |
57 | | - extra_link_args.extend( |
58 | | - [ |
59 | | - f"-Wl,--no-as-needed,-rpath,{python_lib_dir}", |
60 | | - f"-L{python_lib_dir}", |
61 | | - f"-l{python_lib_name}", |
62 | | - ], |
63 | | - ) |
64 | | - |
65 | | - if torch.version.cuda: |
66 | | - extra_link_args.extend(["-lcuda", "-lcudart", "-lnvrtc"]) |
67 | | - |
68 | | - try: |
69 | | - torch_libs, cuda_libs = library_paths("cuda") |
| 47 | +elif torch.version.cuda or torch.version.hip: |
| 48 | + try: |
| 49 | + from torch.utils.cpp_extension import library_paths, include_paths |
| 50 | + |
| 51 | + extra_cflags = ["-O3"] |
| 52 | + generic_sources = ["generic_module.cpp"] |
| 53 | + torch_sources = ["libtorch_tp_jit.cpp"] |
| 54 | + |
| 55 | + include_dirs, extra_link_args = (["util"], ["-Wl,--no-as-needed"]) |
| 56 | + |
| 57 | + if LINKED_LIBPYTHON: |
| 58 | + extra_link_args.pop() |
| 59 | + extra_link_args.extend( |
| 60 | + [ |
| 61 | + f"-Wl,--no-as-needed,-rpath,{python_lib_dir}", |
| 62 | + f"-L{python_lib_dir}", |
| 63 | + f"-l{python_lib_name}", |
| 64 | + ], |
| 65 | + ) |
| 66 | + if torch.version.cuda: |
| 67 | + extra_link_args.extend(["-lcuda", "-lcudart", "-lnvrtc"]) |
| 68 | + |
| 69 | + try: |
| 70 | + torch_libs, cuda_libs = library_paths("cuda") |
| 71 | + extra_link_args.append("-Wl,-rpath," + torch_libs) |
| 72 | + extra_link_args.append("-L" + cuda_libs) |
| 73 | + if os.path.exists(cuda_libs + "/stubs"): |
| 74 | + extra_link_args.append("-L" + cuda_libs + "/stubs") |
| 75 | + except Exception as e: |
| 76 | + getLogger().info(str(e)) |
| 77 | + |
| 78 | + extra_cflags.append("-DCUDA_BACKEND") |
| 79 | + elif torch.version.hip: |
| 80 | + extra_link_args.extend(["-lhiprtc"]) |
| 81 | + torch_libs = library_paths("cuda")[0] |
70 | 82 | extra_link_args.append("-Wl,-rpath," + torch_libs) |
71 | | - extra_link_args.append("-L" + cuda_libs) |
72 | | - if os.path.exists(cuda_libs + "/stubs"): |
73 | | - extra_link_args.append("-L" + cuda_libs + "/stubs") |
74 | | - except Exception as e: |
75 | | - getLogger().info(str(e)) |
76 | | - |
77 | | - extra_cflags.append("-DCUDA_BACKEND") |
78 | | - elif torch.version.hip: |
79 | | - extra_link_args.extend(["-lhiprtc"]) |
80 | | - torch_libs = library_paths("cuda")[0] |
81 | | - extra_link_args.append("-Wl,-rpath," + torch_libs) |
82 | | - |
83 | | - def postprocess(kernel): |
84 | | - kernel = kernel.replace("__syncwarp();", "__threadfence_block();") |
85 | | - kernel = kernel.replace("__shfl_down_sync(FULL_MASK,", "__shfl_down(") |
86 | | - kernel = kernel.replace("atomicAdd", "unsafeAtomicAdd") |
87 | | - return kernel |
88 | | - |
89 | | - postprocess_kernel = postprocess |
90 | | - |
91 | | - extra_cflags.append("-DHIP_BACKEND") |
92 | | - |
93 | | - generic_sources = [oeq_root + "/extension/" + src for src in generic_sources] |
94 | | - torch_sources = [oeq_root + "/extension/" + src for src in torch_sources] |
95 | | - include_dirs = [oeq_root + "/extension/" + d for d in include_dirs] + include_paths( |
96 | | - "cuda" |
97 | | - ) |
98 | | - |
99 | | - torch_compile_exception = None |
100 | | - with warnings.catch_warnings(): |
101 | | - warnings.simplefilter("ignore") |
102 | 83 |
|
103 | | - try: |
104 | | - torch_module = torch.utils.cpp_extension.load( |
105 | | - "libtorch_tp_jit", |
106 | | - torch_sources, |
| 84 | + def postprocess(kernel): |
| 85 | + kernel = kernel.replace("__syncwarp();", "__threadfence_block();") |
| 86 | + kernel = kernel.replace("__shfl_down_sync(FULL_MASK,", "__shfl_down(") |
| 87 | + kernel = kernel.replace("atomicAdd", "unsafeAtomicAdd") |
| 88 | + return kernel |
| 89 | + |
| 90 | + postprocess_kernel = postprocess |
| 91 | + |
| 92 | + extra_cflags.append("-DHIP_BACKEND") |
| 93 | + |
| 94 | + generic_sources = [oeq_root + "/extension/" + src for src in generic_sources] |
| 95 | + torch_sources = [oeq_root + "/extension/" + src for src in torch_sources] |
| 96 | + include_dirs = [ |
| 97 | + oeq_root + "/extension/" + d for d in include_dirs |
| 98 | + ] + include_paths("cuda") |
| 99 | + |
| 100 | + with warnings.catch_warnings(): |
| 101 | + warnings.simplefilter("ignore") |
| 102 | + |
| 103 | + try: |
| 104 | + torch_module = torch.utils.cpp_extension.load( |
| 105 | + "libtorch_tp_jit", |
| 106 | + torch_sources, |
| 107 | + extra_cflags=extra_cflags, |
| 108 | + extra_include_paths=include_dirs, |
| 109 | + extra_ldflags=extra_link_args, |
| 110 | + ) |
| 111 | + torch.ops.load_library(torch_module.__file__) |
| 112 | + TORCH_COMPILE = True |
| 113 | + except Exception as e: |
| 114 | + # If compiling torch fails (e.g. low gcc version), we should fall back to the |
| 115 | + # version that takes integer pointers as args (but is untraceable to PyTorch JIT / export). |
| 116 | + TORCH_COMPILE_ERROR = e |
| 117 | + |
| 118 | + generic_module = torch.utils.cpp_extension.load( |
| 119 | + "generic_module", |
| 120 | + generic_sources, |
107 | 121 | extra_cflags=extra_cflags, |
108 | 122 | extra_include_paths=include_dirs, |
109 | 123 | extra_ldflags=extra_link_args, |
110 | 124 | ) |
111 | | - torch.ops.load_library(torch_module.__file__) |
112 | | - except Exception as e: |
113 | | - # If compiling torch fails (e.g. low gcc version), we should fall back to the |
114 | | - # version that takes integer pointers as args (but is untraceable to PyTorch JIT / export). |
115 | | - TORCH_COMPILE = False |
116 | | - torch_compile_exception = e |
117 | | - |
118 | | - generic_module = torch.utils.cpp_extension.load( |
119 | | - "generic_module", |
120 | | - generic_sources, |
121 | | - extra_cflags=extra_cflags, |
122 | | - extra_include_paths=include_dirs, |
123 | | - extra_ldflags=extra_link_args, |
124 | | - ) |
125 | | - if "generic_module" not in sys.modules: |
126 | | - sys.modules["generic_module"] = generic_module |
| 125 | + if "generic_module" not in sys.modules: |
| 126 | + sys.modules["generic_module"] = generic_module |
127 | 127 |
|
128 | | - if not TORCH_COMPILE: |
129 | | - warnings.warn( |
130 | | - "Could not compile integrated PyTorch wrapper. Falling back to Pybind11" |
131 | | - + f", but JITScript, compile fullgraph, and export will fail.\n {torch_compile_exception}" |
132 | | - ) |
| 128 | + if not TORCH_COMPILE: |
| 129 | + warnings.warn( |
| 130 | + "Could not compile integrated PyTorch wrapper. Falling back to Pybind11" |
| 131 | + + f", but JITScript, compile fullgraph, and export will fail.\n {TORCH_COMPILE_ERROR}" |
| 132 | + ) |
| 133 | + BUILT_EXTENSION = True |
| 134 | + except Exception as e: |
| 135 | + BUILT_EXTENSION_ERROR = f"Error building OpenEquivariance Extension: {e}" |
133 | 136 | else: |
134 | | - TORCH_COMPILE = False |
| 137 | + BUILT_EXTENSION_ERROR = "OpenEquivariance extension build not attempted" |
135 | 138 |
|
136 | 139 |
|
137 | 140 | def _raise_import_error_helper(import_target: str): |
138 | | - if not TORCH_VERSION_CUDA_OR_HIP: |
139 | | - raise ImportError( |
140 | | - f"Could not import {import_target}: OpenEquivariance's torch extension was not built because torch.version.cuda || torch.version.hip is false" |
141 | | - ) |
| 141 | + if not BUILT_EXTENSION: |
| 142 | + raise ImportError(f"Could not import {import_target}: {BUILT_EXTENSION_ERROR}") |
142 | 143 |
|
143 | 144 |
|
144 | 145 | def torch_ext_so_path(): |
|
0 commit comments