forked from FlashML-org/FreeToken
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.py
More file actions
48 lines (35 loc) · 1.49 KB
/
Copy pathsetup.py
File metadata and controls
48 lines (35 loc) · 1.49 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
from __future__ import annotations
import importlib.util
from pathlib import Path
from setuptools import setup
from torch.utils.cpp_extension import BuildExtension, CUDA_HOME, CppExtension
ROOT = Path(__file__).parent
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 _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
# patched: CUDA-only extensions are optional; skip them when no CUDA toolchain
# is present (e.g. ROCm builds use torch's own pinned-memory path instead).
import os
cuda_include_dirs, cuda_library_dirs = [], []
ext_modules = []
if os.environ.get("FREETOKEN_SKIP_CUDA_EXT") != "1" and CUDA_HOME is not None:
_check_toolchain()
cuda_include_dirs, cuda_library_dirs = _cuda_runtime_paths()
setup(
ext_modules=ext_modules,
cmdclass={"build_ext": BuildExtension.with_options(use_ninja=True)},
)