-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
218 lines (190 loc) · 7.31 KB
/
Copy pathsetup.py
File metadata and controls
218 lines (190 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import os
import sys
from pathlib import Path
os.environ.setdefault("TORCH_DEVICE_BACKEND_AUTOLOAD", "0")
from setuptools import find_packages, setup
from torch.utils.cpp_extension import BuildExtension, CppExtension
ROOT = Path(__file__).resolve().parent
CSRC = ROOT / "csrc"
def write_compat_pyconfig():
"""Rewrite a pyconfig.h that defaults Py_GIL_DISABLED=1 on a GIL build.
Python.h uses #include \"pyconfig.h\", which prefers the header next to
itself, so we remap that name with MSVC pragma include_alias + /FI.
"""
import sysconfig
src = Path(sysconfig.get_path("include")) / "pyconfig.h"
if not src.exists():
return None
text = src.read_text(encoding="utf-8", errors="replace")
needle_lf = (
"#ifndef Py_GIL_DISABLED\n"
"#define Py_GIL_DISABLED 1\n"
"#endif"
)
needle_crlf = needle_lf.replace("\n", "\r\n")
if needle_lf not in text and needle_crlf not in text:
return None
dest_dir = CSRC / "compat"
dest_dir.mkdir(parents=True, exist_ok=True)
patched = text.replace(needle_crlf, needle_crlf.replace("#define Py_GIL_DISABLED 1\r\n", "/* GIL build */\r\n"), 1)
patched = patched.replace(needle_lf, needle_lf.replace("#define Py_GIL_DISABLED 1\n", "/* GIL build */\n"), 1)
(dest_dir / "tvarant_pyconfig.h").write_text(patched, encoding="utf-8")
(dest_dir / "tvarant_force_include.h").write_text(
"#pragma once\n"
'#pragma include_alias("pyconfig.h", "tvarant_pyconfig.h")\n'
"#pragma include_alias(<pyconfig.h>, <tvarant_pyconfig.h>)\n",
encoding="utf-8",
)
print("Patched pyconfig.h to keep the GIL-enabled Python ABI")
return str(dest_dir)
def ensure_msvc_python_t_lib():
"""This CPython's pyconfig.h #defines Py_GIL_DISABLED and then
`#pragma comment(lib, \"pythonXXXt.lib\")`. Regular installs only ship
pythonXXX.lib, so provide a same-ABI stub name for the linker."""
if sys.platform != "win32":
return None
import shutil
ver = f"{sys.version_info.major}{sys.version_info.minor}"
src = Path(sys.base_prefix) / "libs" / f"python{ver}.lib"
dest_dir = ROOT / "third_party" / "pylibs"
dest = dest_dir / f"python{ver}t.lib"
if not src.exists():
return None
dest_dir.mkdir(parents=True, exist_ok=True)
if not dest.exists() or dest.stat().st_mtime < src.stat().st_mtime:
shutil.copy2(src, dest)
return str(dest_dir)
def find_local_winsdk():
"""MSVC on this machine has no system Windows SDK; use vendored NuGet headers/libs."""
root = ROOT / "third_party" / "winsdk"
inc_root = root / "sdk.cpp" / "c" / "Include" / "10.0.26100.0"
lib_root = root / "sdk.x64" / "c"
ucrt_inc = inc_root / "ucrt"
um_inc = inc_root / "um"
shared_inc = inc_root / "shared"
ucrt_lib = lib_root / "ucrt" / "x64"
um_lib = lib_root / "um" / "x64"
if not ucrt_inc.exists() or not ucrt_lib.exists():
return None
return {
"include": [str(ucrt_inc), str(um_inc), str(shared_inc)],
"lib": [str(ucrt_lib), str(um_lib)],
}
def inject_winsdk_env(sdk):
include = os.pathsep.join(sdk["include"])
lib = os.pathsep.join(sdk["lib"])
os.environ["INCLUDE"] = include + os.pathsep + os.environ.get("INCLUDE", "")
os.environ["LIB"] = lib + os.pathsep + os.environ.get("LIB", "")
os.environ["LIBPATH"] = lib + os.pathsep + os.environ.get("LIBPATH", "")
print("Using local Windows SDK headers from third_party/winsdk")
def find_opencl():
"""Return (include_dir, library_dir, libname) or None."""
roots = []
for key in ("OPENCL_ROOT", "CUDA_PATH", "INTELOCLSDKROOT"):
val = os.environ.get(key)
if val:
roots.append(Path(val))
roots.extend(
[
Path(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.6"),
Path(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.4"),
Path(r"C:\Program Files (x86)\Intel\OpenCL SDK"),
Path("/usr/local/cuda"),
Path("/usr"),
]
)
for root in roots:
inc = root / "include"
if not (inc / "CL" / "cl.h").exists():
continue
for libdir in (root / "lib" / "x64", root / "lib64", root / "lib"):
if libdir.exists():
return str(inc), str(libdir), "OpenCL"
if (Path("/usr/include/CL/cl.h")).exists():
return "/usr/include", "/usr/lib", "OpenCL"
return None
def main():
sources = [
"csrc/Module.cpp",
"csrc/runtime/Runtime.cpp",
"csrc/runtime/CpuSimRuntime.cpp",
"csrc/runtime/OpenCLRuntime.cpp",
"csrc/runtime/Allocator.cpp",
"csrc/runtime/Guard.cpp",
"csrc/runtime/Hooks.cpp",
"csrc/runtime/Generator.cpp",
"csrc/aten/Ops.cpp",
"csrc/aten/ExtendedOps.cpp",
"csrc/aten/LlmOps.cpp",
"csrc/aten/CustomOps.cpp",
"csrc/jit/Jit.cpp",
"csrc/kernels/host/HostKernels.cpp",
]
include_dirs = [str(CSRC), str(CSRC / "runtime"), str(CSRC / "kernels" / "host"), str(CSRC / "jit"), str(CSRC / "aten")]
compat = write_compat_pyconfig()
if compat:
include_dirs.insert(0, compat)
define_macros = []
libraries = []
library_dirs = []
extra_compile_args = []
extra_link_args = []
sdk = find_local_winsdk()
if sdk:
inject_winsdk_env(sdk)
include_dirs.extend(sdk["include"])
library_dirs.extend(sdk["lib"])
pylibs = ensure_msvc_python_t_lib()
if pylibs:
library_dirs.append(pylibs)
if sys.platform == "win32":
extra_compile_args = [
"/std:c++17",
"/EHsc",
"/O2",
"/permissive-",
"/Zc:__cplusplus",
"/DNOMINMAX",
"/D_CRT_SECURE_NO_WARNINGS",
"/wd4251",
"/wd4275",
]
extra_link_args = ["/MANIFEST:NO"]
fi = CSRC / "compat" / "tvarant_force_include.h"
if fi.exists():
extra_compile_args.append(f"/FI{fi}")
else:
extra_compile_args = ["-std=c++17", "-fPIC", "-O3", "-Wno-unused-parameter"]
force_opencl = os.environ.get("USE_OPENCL", "").lower() in {"1", "true", "on", "yes"}
if force_opencl:
ocl = find_opencl()
if ocl is None:
raise RuntimeError("USE_OPENCL=1 but OpenCL headers were not found")
inc, libdir, libname = ocl
include_dirs.append(inc)
library_dirs.append(libdir)
libraries.append(libname)
define_macros.append(("USE_OPENCL", "1"))
print(f"Building torch_tvarant with OpenCL ({inc})")
else:
print("Building torch_tvarant CPU simulator (set USE_OPENCL=1 to enable OpenCL)")
ext = CppExtension(
name="torch_tvarant._C",
sources=sources,
include_dirs=include_dirs,
define_macros=define_macros,
libraries=libraries,
library_dirs=library_dirs,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
)
setup(
name="torch-tvarant",
version="0.1.0",
description="PyTorch device backend for the Tvarant RISC-V SIMT GPGPU",
packages=find_packages(include=["torch_tvarant", "torch_tvarant.*"]),
ext_modules=[ext],
cmdclass={"build_ext": BuildExtension.with_options(no_python_abi_suffix=True)},
zip_safe=False,
)
main()