forked from RL-Align/RL-Kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
164 lines (144 loc) · 4.86 KB
/
Copy pathsetup.py
File metadata and controls
164 lines (144 loc) · 4.86 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
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2026 RL-Kernel Contributors
import os
from setuptools import find_packages, setup
def _load_torch_extension_tools():
try:
import torch
from torch.utils.cpp_extension import BuildExtension, CUDAExtension
except ImportError:
return None, None, None, None
try:
from torch.utils.cpp_extension import ROCMExtension
except ImportError:
ROCMExtension = None
return torch, BuildExtension, CUDAExtension, ROCMExtension
def _cuda_define_from_env(name: str, macro: str) -> list[str]:
value = os.environ.get(name)
if value is None:
return []
parsed = int(value)
if parsed <= 0:
raise ValueError(f"{name} must be positive, got {value!r}")
return [f"-D{macro}={parsed}"]
def get_extensions():
torch, _, CUDAExtension, ROCMExtension = _load_torch_extension_tools()
if torch is None:
return []
extensions = []
is_rocm = torch.version.hip is not None
if is_rocm and ROCMExtension is not None:
extensions.append(
ROCMExtension(
name="rl_engine._C",
sources=[
"csrc/ops.cpp",
"csrc/fused_logp_kernel.cpp",
],
extra_compile_args={
"cxx": ["-O3", "-std=c++17"],
"hipcc": ["-O3", "--use_fast_math", "-Xhipcc", "-compress-all"],
},
)
)
elif torch.cuda.is_available():
cuda_sources = [
"csrc/ops.cpp",
"csrc/fused_logp_kernel.cu",
"csrc/cuda/attention/prefix_shared_attention.cu",
]
cc_major, cc_minor = torch.cuda.get_device_capability()
nvcc_flags = ["-O3", "--use_fast_math", "-Xfatbin", "-compress-all"]
nvcc_flags.extend(
_cuda_define_from_env(
"FUSED_LOGP_TWOPASS_BLOCK_SIZE",
"FUSED_LOGP_TWOPASS_BLOCK_SIZE",
)
)
nvcc_flags.extend(
_cuda_define_from_env(
"FUSED_LOGP_ONLINE_BLOCK_SIZE",
"FUSED_LOGP_ONLINE_BLOCK_SIZE",
)
)
nvcc_flags.extend(
_cuda_define_from_env(
"FUSED_LOGP_ONLINE_SPARSE_LARGE_VOCAB_BLOCK_SIZE",
"FUSED_LOGP_ONLINE_SPARSE_LARGE_VOCAB_BLOCK_SIZE",
)
)
nvcc_flags.extend(
_cuda_define_from_env(
"FUSED_LOGP_ONLINE_LARGE_ROW_BYTES_THRESHOLD",
"FUSED_LOGP_ONLINE_LARGE_ROW_BYTES_THRESHOLD",
)
)
nvcc_flags.extend(
_cuda_define_from_env(
"FUSED_LOGP_ONLINE_SPARSE_DENSITY_NUMERATOR",
"FUSED_LOGP_ONLINE_SPARSE_DENSITY_NUMERATOR",
)
)
nvcc_flags.extend(
_cuda_define_from_env(
"FUSED_LOGP_ONLINE_SPARSE_DENSITY_DENOMINATOR",
"FUSED_LOGP_ONLINE_SPARSE_DENSITY_DENOMINATOR",
)
)
nvcc_flags.extend(
_cuda_define_from_env(
"FUSED_LOGP_ONLINE_MIN_BLOCKS_PER_SM",
"FUSED_LOGP_ONLINE_MIN_BLOCKS_PER_SM",
)
)
if os.environ.get("KERNEL_ALIGN_NCU_LINEINFO") == "1":
nvcc_flags.append("-lineinfo")
cxx_flags = ["-O3", "-std=c++17", "-DKERNEL_ALIGN_WITH_CUDA"]
extra_link_args = []
tma_src = "csrc/cuda/fused_logp_sm90.cu"
enable_sm90 = os.environ.get("KERNEL_ALIGN_FORCE_SM90") == "1"
if enable_sm90 and os.path.exists(tma_src):
tma_arch = f"{cc_major}{cc_minor}a"
cuda_sources.append(tma_src)
nvcc_flags.append(f"-gencode=arch=compute_{tma_arch},code=sm_{tma_arch}")
cxx_flags.append("-DKERNEL_ALIGN_WITH_SM90")
extra_link_args.append("-lcuda")
extensions.append(
CUDAExtension(
name="rl_engine._C",
sources=cuda_sources,
extra_compile_args={
"cxx": cxx_flags,
"nvcc": nvcc_flags,
},
extra_link_args=extra_link_args,
)
)
return extensions
def get_cmdclass():
_, BuildExtension, _, _ = _load_torch_extension_tools()
if BuildExtension is None:
return {}
return {"build_ext": BuildExtension}
setup(
name="rl-engine",
version="0.1.0",
packages=find_packages(include=["rl_engine", "rl_engine.*"]),
install_requires=[
"torch>=2.4.1",
"tabulate",
"numpy",
"accelerate",
"transformers",
],
ext_modules=get_extensions(),
cmdclass=get_cmdclass(),
extras_require={
"cuda": ["flashinfer"],
"rocm": ["aiter"],
"vllm": ["vllm>=0.6.0"],
},
python_requires=">=3.10",
include_package_data=True,
zip_safe=False,
)