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