setup.py 5.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Copyright 2025 SGLang Team. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

16
import os
17
import sys
18
19
from pathlib import Path

20
import torch
lukec's avatar
lukec committed
21
from setuptools import find_packages, setup
22
23
from torch.utils.cpp_extension import BuildExtension, CUDAExtension

24
25
26
root = Path(__file__).parent.resolve()


27
28
if "bdist_wheel" in sys.argv and "--plat-name" not in sys.argv:
    sys.argv.extend(["--plat-name", "manylinux2014_x86_64"])
Yineng Zhang's avatar
Yineng Zhang committed
29
30


31
def _get_cuda_version():
32
33
34
35
36
    if torch.version.cuda:
        return tuple(map(int, torch.version.cuda.split(".")))
    return (0, 0)


37
def _get_device_sm():
38
39
40
41
42
43
    if torch.cuda.is_available():
        major, minor = torch.cuda.get_device_capability()
        return major * 10 + minor
    return 0


44
45
46
47
48
49
def _get_version():
    with open(root / "pyproject.toml") as f:
        for line in f:
            if line.startswith("version"):
                return line.split("=")[1].strip().strip('"')

50

51
operator_namespace = "sgl_kernel"
52
53
cutlass_default = root / "3rdparty" / "cutlass"
cutlass = Path(os.environ.get("CUSTOM_CUTLASS_SRC_DIR", default=cutlass_default))
54
flashinfer = root / "3rdparty" / "flashinfer"
55
include_dirs = [
56
57
    root / "include",
    root / "csrc",
58
59
    cutlass.resolve() / "include",
    cutlass.resolve() / "tools" / "util" / "include",
60
    flashinfer.resolve() / "include",
61
    flashinfer.resolve() / "include" / "gemm",
62
    flashinfer.resolve() / "csrc",
63
    "cublas",
64
]
65

Ke Bao's avatar
Ke Bao committed
66
nvcc_flags = [
67
    "-DNDEBUG",
68
    f"-DOPERATOR_NAMESPACE={operator_namespace}",
Ke Bao's avatar
Ke Bao committed
69
70
71
72
73
74
75
    "-O3",
    "-Xcompiler",
    "-fPIC",
    "-gencode=arch=compute_75,code=sm_75",
    "-gencode=arch=compute_80,code=sm_80",
    "-gencode=arch=compute_89,code=sm_89",
    "-gencode=arch=compute_90,code=sm_90",
76
77
78
    "-std=c++17",
    "-use_fast_math",
    "-DFLASHINFER_ENABLE_F16",
79
80
81
82
83
84
    "-DCUTLASS_VERSIONS_GENERATED",
    "-DCUTE_USE_PACKED_TUPLE=1",
    "-DCUTLASS_TEST_LEVEL=0",
    "-DCUTLASS_TEST_ENABLE_CACHED_RESULTS=1",
    "-DCUTLASS_DEBUG_TRACE_LEVEL=0",
    "--ptxas-options=-v",
85
    "--expt-relaxed-constexpr",
86
87
    "-Xcompiler=-Wconversion",
    "-Xcompiler=-fno-strict-aliasing",
Ke Bao's avatar
Ke Bao committed
88
]
89
90
91
92
93
nvcc_flags_fp8 = [
    "-DFLASHINFER_ENABLE_FP8",
    "-DFLASHINFER_ENABLE_FP8_E4M3",
    "-DFLASHINFER_ENABLE_FP8_E5M2",
]
94

95
sources = [
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    "csrc/allreduce/trt_reduce_internal.cu",
    "csrc/allreduce/trt_reduce_kernel.cu",
    "csrc/attention/lightning_attention_decode_kernel.cu",
    "csrc/elementwise/fused_add_rms_norm_kernel.cu",
    "csrc/gemm/cublas_grouped_gemm.cu",
    "csrc/gemm/fp8_gemm_kernel.cu",
    "csrc/gemm/fp8_blockwise_gemm_kernel.cu",
    "csrc/gemm/int8_gemm_kernel.cu",
    "csrc/gemm/per_token_group_quant_fp8.cu",
    "csrc/gemm/per_token_quant_fp8.cu",
    "csrc/gemm/per_tensor_quant_fp8.cu",
    "csrc/moe/moe_align_kernel.cu",
    "csrc/speculative/eagle_utils.cu",
    "csrc/speculative/speculative_sampling.cu",
    "csrc/torch_extension.cc",
111
112
113
114
115
    "3rdparty/flashinfer/csrc/activation.cu",
    "3rdparty/flashinfer/csrc/bmm_fp8.cu",
    "3rdparty/flashinfer/csrc/norm.cu",
    "3rdparty/flashinfer/csrc/sampling.cu",
    "3rdparty/flashinfer/csrc/renorm.cu",
116
    "3rdparty/flashinfer/csrc/rope.cu",
117
118
]

119
120
121
122
123
124
125
126
127
128
enable_bf16 = os.getenv("SGL_KERNEL_ENABLE_BF16", "0") == "1"
enable_fp8 = os.getenv("SGL_KERNEL_ENABLE_FP8", "0") == "1"
enable_sm90a = os.getenv("SGL_KERNEL_ENABLE_SM90A", "0") == "1"
cuda_version = _get_cuda_version()
sm_version = _get_device_sm()

if torch.cuda.is_available():
    if cuda_version >= (12, 0) and sm_version >= 90:
        nvcc_flags.append("-gencode=arch=compute_90a,code=sm_90a")
    if sm_version >= 90:
129
        nvcc_flags.extend(nvcc_flags_fp8)
130
131
132
133
134
135
136
    if sm_version >= 80:
        nvcc_flags.append("-DFLASHINFER_ENABLE_BF16")
else:
    # compilation environment without GPU
    if enable_sm90a:
        nvcc_flags.append("-gencode=arch=compute_90a,code=sm_90a")
    if enable_fp8:
137
        nvcc_flags.extend(nvcc_flags_fp8)
138
139
    if enable_bf16:
        nvcc_flags.append("-DFLASHINFER_ENABLE_BF16")
140

141
142
143
144
145
146
147
148
149
150
for flag in [
    "-D__CUDA_NO_HALF_OPERATORS__",
    "-D__CUDA_NO_HALF_CONVERSIONS__",
    "-D__CUDA_NO_BFLOAT16_CONVERSIONS__",
    "-D__CUDA_NO_HALF2_OPERATORS__",
]:
    try:
        torch.utils.cpp_extension.COMMON_NVCC_FLAGS.remove(flag)
    except ValueError:
        pass
151

Ke Bao's avatar
Ke Bao committed
152
cxx_flags = ["-O3"]
Yineng Zhang's avatar
Yineng Zhang committed
153
libraries = ["c10", "torch", "torch_python", "cuda", "cublas"]
154
extra_link_args = ["-Wl,-rpath,$ORIGIN/../../torch/lib", "-L/usr/lib/x86_64-linux-gnu"]
155

Ke Bao's avatar
Ke Bao committed
156
157
ext_modules = [
    CUDAExtension(
158
        name="sgl_kernel.common_ops",
159
        sources=sources,
160
        include_dirs=include_dirs,
Ke Bao's avatar
Ke Bao committed
161
162
163
164
165
166
        extra_compile_args={
            "nvcc": nvcc_flags,
            "cxx": cxx_flags,
        },
        libraries=libraries,
        extra_link_args=extra_link_args,
167
        py_limited_api=True,
Ke Bao's avatar
Ke Bao committed
168
169
170
    ),
]

171
172
setup(
    name="sgl-kernel",
173
    version=_get_version(),
174
175
    packages=find_packages(where="python"),
    package_dir={"": "python"},
Ke Bao's avatar
Ke Bao committed
176
    ext_modules=ext_modules,
Liu Jinjie's avatar
Liu Jinjie committed
177
    cmdclass={"build_ext": BuildExtension.with_options(use_ninja=True)},
178
    options={"bdist_wheel": {"py_limited_api": "cp39"}},
179
)