setup_cpu.py 3.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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.
# ==============================================================================

import os
17
import platform
18
19
20
21
22
23
24
25
26
27
import shutil
import sys
from pathlib import Path

import torch
from setuptools import find_packages, setup
from setuptools.command.build_py import build_py
from torch.utils.cpp_extension import BuildExtension, CppExtension

root = Path(__file__).parent.resolve()
28
29
30
31
32
33
34
35
36
37
arch = platform.machine().lower()

if arch in ("x86_64", "amd64"):
    plat_name = "manylinux2014_x86_64"
elif arch in ("aarch64", "arm64"):
    plat_name = "manylinux2014_aarch64"
elif arch.startswith("ppc"):
    plat_name = "manylinux2014_ppc64le"
else:
    plat_name = f"manylinux2014_{arch}"
38
39

if "bdist_wheel" in sys.argv and "--plat-name" not in sys.argv:
40
    sys.argv.extend(["--plat-name", plat_name])
41
42
43
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
cpu_fp8_ftz = os.getenv("SGLANG_CPU_FP8_CVT_FTZ", "1") == "1"

52
53
54
55
56
57
58
59
60
operator_namespace = "sgl_kernel"
include_dirs = []

sources = [
    "csrc/cpu/activation.cpp",
    "csrc/cpu/bmm.cpp",
    "csrc/cpu/decode.cpp",
    "csrc/cpu/extend.cpp",
    "csrc/cpu/gemm.cpp",
61
    "csrc/cpu/gemm_fp8.cpp",
62
63
64
65
66
67
68
69
    "csrc/cpu/gemm_int8.cpp",
    "csrc/cpu/moe.cpp",
    "csrc/cpu/moe_int8.cpp",
    "csrc/cpu/norm.cpp",
    "csrc/cpu/qkv_proj.cpp",
    "csrc/cpu/topk.cpp",
    "csrc/cpu/interface.cpp",
    "csrc/cpu/shm.cpp",
70
    "csrc/cpu/rope.cpp",
71
72
73
74
75
76
77
78
79
80
81
    "csrc/cpu/torch_extension_cpu.cpp",
]

extra_compile_args = {
    "cxx": [
        "-O3",
        "-Wno-unknown-pragmas",
        "-march=native",
        "-fopenmp",
    ]
}
82
83
84
if cpu_fp8_ftz:
    extra_compile_args["cxx"].append("-DSGLANG_CPU_FP8_CVT_FTZ")

85
86
87
88
89
90
libraries = ["c10", "torch", "torch_python"]
cmdclass = {
    "build_ext": BuildExtension.with_options(use_ninja=True),
}
Extension = CppExtension

91
extra_link_args = ["-Wl,-rpath,$ORIGIN/../../torch/lib", f"-L/usr/lib/{arch}-linux-gnu"]
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

ext_modules = [
    Extension(
        name="sgl_kernel.common_ops",
        sources=sources,
        include_dirs=include_dirs,
        extra_compile_args=extra_compile_args,
        libraries=libraries,
        extra_link_args=extra_link_args,
        py_limited_api=True,
    ),
]

setup(
    name="sgl-kernel",
    version=_get_version(),
    packages=find_packages(where="python"),
    package_dir={"": "python"},
    ext_modules=ext_modules,
    cmdclass=cmdclass,
    options={"bdist_wheel": {"py_limited_api": "cp39"}},
)