Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
147 lines (125 loc) · 5.91 KB
/
Copy pathsetup.py
File metadata and controls
147 lines (125 loc) · 5.91 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
from __future__ import annotations
import importlib.util
import os
from pathlib import Path
import sys
from setuptools import setup
from torch.utils.cpp_extension import BuildExtension, CUDA_HOME, CppExtension, ROCM_HOME
ROOT = Path(__file__).parent
KERNEL_INCLUDE = str(ROOT / "python" / "freetoken" / "kernel" / "csrc" / "include")
def _check_toolchain() -> None:
path = ROOT / "python" / "freetoken" / "kernel" / "_toolchain.py"
spec = importlib.util.spec_from_file_location("_freetoken_toolchain", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
module.check_nvcc_matches_torch()
def _is_rocm() -> bool:
import torch
return getattr(torch.version, "hip", None) is not None
def _rocm_paths() -> tuple[list[str], list[str], str]:
candidates: list[Path] = []
if os.getenv("ROCM_HOME"):
candidates.append(Path(os.environ["ROCM_HOME"]))
if ROCM_HOME:
candidates.append(Path(ROCM_HOME))
# ROCm 7.14 PyTorch images ship the SDK as a Python package instead of
# installing it at /opt/rocm.
spec = importlib.util.find_spec("_rocm_sdk_core")
if spec and spec.submodule_search_locations:
candidates.append(Path(next(iter(spec.submodule_search_locations))))
candidates.append(Path("/opt/rocm"))
for rocm_home in dict.fromkeys(candidates):
include_dir = rocm_home / "include"
library_dir = rocm_home / "lib"
if not (include_dir / "hip" / "hip_runtime.h").exists():
continue
if (library_dir / "libamdhip64.so").exists():
return [str(include_dir)], [str(library_dir)], "amdhip64"
versioned = sorted(library_dir.glob("libamdhip64.so.*"))
if versioned:
return [str(include_dir)], [str(library_dir)], f":{versioned[-1].name}"
searched = ", ".join(str(path) for path in dict.fromkeys(candidates))
raise RuntimeError(
"A ROCm SDK with HIP headers and libamdhip64 is required to build on ROCm; "
f"searched: {searched}. Set ROCM_HOME to override."
)
def _cuda_runtime_paths() -> tuple[list[str], list[str]]:
if CUDA_HOME is None:
raise RuntimeError(
"CUDA_HOME is required to build freetoken.kernel._pinned_tensor "
"because it links against the CUDA runtime API."
)
cuda_home = Path(CUDA_HOME)
library_dirs = [str(cuda_home / "lib64")]
if (cuda_home / "lib").exists():
library_dirs.append(str(cuda_home / "lib"))
return [str(cuda_home / "include")], library_dirs
IS_ROCM = _is_rocm()
if IS_ROCM:
runtime_include_dirs, runtime_library_dirs, runtime_lib = _rocm_paths()
runtime_link_args = [f"-Wl,-rpath,{runtime_library_dirs[0]}"]
# Host sources. BuildExtension supplies the ROCm
# platform defines to the C++ compiler; offload architecture flags belong on
# HIP device sources and would be rejected by the host compiler here.
extra_compile = ["-O3", "-std=c++20"]
# Device sources (.cu, built by hipcc); BuildExtension adds the offload architectures.
device_compile = ["-O3", "-std=c++20"]
else:
runtime_include_dirs, runtime_library_dirs = _cuda_runtime_paths()
runtime_lib = "cudart"
runtime_link_args = []
extra_compile = ["-O3", "-std=c++20"]
# Device sources (.cu, built by nvcc): SASS for each major arch, which also runs on the newer minors of that major,
# plus PTX for the oldest arch nvcc 13 supports, which the driver builds for any other GPU at load.
# Naming archs also stops torch from picking them, which fails on a build machine without a GPU.
device_compile = ["-O3", "-std=c++20"] + [f"-gencode=arch=compute_{a},code=sm_{a}" for a in (75, 80, 90, 100, 120)]
device_compile.append("-gencode=arch=compute_75,code=compute_75")
_check_toolchain()
setup(
ext_modules=[
CppExtension(
name="freetoken.kernel._pinned_tensor",
sources=[
"python/freetoken/kernel/csrc/pinned_tensor.cpp",
],
include_dirs=[KERNEL_INCLUDE, *runtime_include_dirs],
library_dirs=runtime_library_dirs,
libraries=[runtime_lib],
extra_compile_args=extra_compile,
extra_link_args=runtime_link_args,
),
# CPU-compute MoE executor for --moe-backend cpu. Links cudart/amdhip64 for the
# cudaLaunchHostFunc submit/sync graph nodes; the bf16 GEMV microkernels
# use per-function target attributes (avx512bf16/avx512f) + a runtime
# __builtin_cpu_supports dispatch, so the single binary stays portable
# (scalar fallback) -- no global -march is set.
CppExtension(
name="freetoken.kernel._cpu_moe",
sources=[
"python/freetoken/kernel/csrc/cpu_moe/cpu_moe_ext.cpp",
f"python/freetoken/kernel/csrc/cpu_moe/gpu_sync_{'hip' if IS_ROCM else 'cuda'}.cpp",
"python/freetoken/kernel/csrc/cpu_moe/gpu_sync_io.cu",
*sorted(
str(p.relative_to(ROOT))
for p in (ROOT / "python/freetoken/kernel/csrc/cpu_moe/kernels").glob("*.cpp")
),
],
include_dirs=[KERNEL_INCLUDE, *runtime_include_dirs],
library_dirs=runtime_library_dirs,
libraries=[runtime_lib],
extra_compile_args={"cxx": extra_compile + ["-pthread"], "nvcc": device_compile},
extra_link_args=runtime_link_args,
),
# disk-backed row store (PLE / Engram tables); Linux-only until the TableFile/BatchReader seams grow Windows bodies
*([
CppExtension(
name="freetoken.kernel._row_store",
sources=[
"python/freetoken/kernel/csrc/row_store/row_store_ext.cpp",
],
extra_compile_args=["-O3", "-std=c++20"],
)
] if sys.platform == "linux" else []),
],
cmdclass={"build_ext": BuildExtension.with_options(use_ninja=True)},
)