From 3835eba9a6f5157585ab419d075969452d27e29d Mon Sep 17 00:00:00 2001 From: Zhangyi <1109276519@qq.com> Date: Tue, 10 Dec 2024 01:52:52 +0800 Subject: [PATCH 1/8] init setup kernel ci --- .github/workflows/release-pypi-kernel.yml | 36 +++++++++++++++++++++++ sgl-kernel/build.sh | 10 +++++-- 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/release-pypi-kernel.yml diff --git a/.github/workflows/release-pypi-kernel.yml b/.github/workflows/release-pypi-kernel.yml new file mode 100644 index 00000000000..5220391a8d1 --- /dev/null +++ b/.github/workflows/release-pypi-kernel.yml @@ -0,0 +1,36 @@ +name: Build Wheels + +on: + push: + branches: + - main + paths: + - sgl-kernel/pyproject.toml + workflow_dispatch: + +jobs: + build-wheels: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ['3.9', '3.10', '3.11', '3.12'] + cuda-version: ['11.8', '12.1', '12.4'] + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Build wheels for Python ${{ matrix.python-version }} and CUDA ${{ matrix.cuda-version }} + run: | + cd sgl-kernel + chmod +x ./build.sh + ./build.sh "${{ matrix.python-version }}" "${{ matrix.cuda-version }}" + + - name: Upload to pypi + run: | + pip install twine + python -m twine upload dist/* -u __token__ -p ${{ secrets.PYPI_TOKEN }}/* diff --git a/sgl-kernel/build.sh b/sgl-kernel/build.sh index b276f0141c2..dfcbba146b0 100755 --- a/sgl-kernel/build.sh +++ b/sgl-kernel/build.sh @@ -2,12 +2,16 @@ set -ex +PYTHON_VERSION=$1 +CUDA_VERSION=$2 +PYTHON_ROOT_PATH=/opt/python/cp${PYTHON_VERSION//.}-cp${PYTHON_VERSION//.} + docker run --rm -it \ -v "$(pwd)":/sgl-kernel \ - pytorch/manylinux-builder:cuda12.1 \ + pytorch/manylinux-builder:cuda${CUDA_VERSION} \ bash -c " - pip install --no-cache-dir torch==2.4.0 --index-url https://download.pytorch.org/whl/cu121 && \ + ${PYTHON_ROOT_PATH}/bin/pip install --no-cache-dir torch==2.4.0 --index-url https://download.pytorch.org/whl/cu${CUDA_VERSION//.} && \ export TORCH_CUDA_ARCH_LIST='7.5 8.0 8.9 9.0+PTX' && \ cd /sgl-kernel && \ - python setup.py bdist_wheel + ${PYTHON_ROOT_PATH}/bin/python setup.py bdist_wheel " From 2391e718d742cb76de73c91dfe97839a2fff9382 Mon Sep 17 00:00:00 2001 From: Zhangyi <1109276519@qq.com> Date: Tue, 10 Dec 2024 01:54:58 +0800 Subject: [PATCH 2/8] fix workflow title --- .github/workflows/release-pypi-kernel.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release-pypi-kernel.yml b/.github/workflows/release-pypi-kernel.yml index 5220391a8d1..08787c356b2 100644 --- a/.github/workflows/release-pypi-kernel.yml +++ b/.github/workflows/release-pypi-kernel.yml @@ -1,10 +1,10 @@ -name: Build Wheels +name: Build SGLang Kernel for PyPI on: push: branches: - main - paths: + paths: - sgl-kernel/pyproject.toml workflow_dispatch: From 515857a3b40af288626bb3dd9301fa02ea7e0973 Mon Sep 17 00:00:00 2001 From: Zhangyi <1109276519@qq.com> Date: Wed, 11 Dec 2024 00:44:06 +0800 Subject: [PATCH 3/8] fix sgl-kernel workflow title --- .github/workflows/release-pypi-kernel.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release-pypi-kernel.yml b/.github/workflows/release-pypi-kernel.yml index 08787c356b2..cb6591b71e7 100644 --- a/.github/workflows/release-pypi-kernel.yml +++ b/.github/workflows/release-pypi-kernel.yml @@ -1,4 +1,4 @@ -name: Build SGLang Kernel for PyPI +name: Release SGLang Kernel to PyPI on: push: From afbf557c0d2462b90502be38003a64f72e37106a Mon Sep 17 00:00:00 2001 From: Zhangyi <1109276519@qq.com> Date: Wed, 11 Dec 2024 01:45:30 +0800 Subject: [PATCH 4/8] fix build.sh --- .github/workflows/release-pypi-kernel.yml | 2 +- sgl-kernel/build.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release-pypi-kernel.yml b/.github/workflows/release-pypi-kernel.yml index cb6591b71e7..533390424f1 100644 --- a/.github/workflows/release-pypi-kernel.yml +++ b/.github/workflows/release-pypi-kernel.yml @@ -33,4 +33,4 @@ jobs: - name: Upload to pypi run: | pip install twine - python -m twine upload dist/* -u __token__ -p ${{ secrets.PYPI_TOKEN }}/* + python3 -m twine upload dist/* -u __token__ -p ${{ secrets.PYPI_TOKEN }} diff --git a/sgl-kernel/build.sh b/sgl-kernel/build.sh index dfcbba146b0..f1f0077f977 100755 --- a/sgl-kernel/build.sh +++ b/sgl-kernel/build.sh @@ -6,7 +6,7 @@ PYTHON_VERSION=$1 CUDA_VERSION=$2 PYTHON_ROOT_PATH=/opt/python/cp${PYTHON_VERSION//.}-cp${PYTHON_VERSION//.} -docker run --rm -it \ +docker run --rm \ -v "$(pwd)":/sgl-kernel \ pytorch/manylinux-builder:cuda${CUDA_VERSION} \ bash -c " From bb8e86c9e8c0c5b615c54a4c1091580237f1cb2e Mon Sep 17 00:00:00 2001 From: zhyncs Date: Wed, 11 Dec 2024 03:13:21 +0800 Subject: [PATCH 5/8] upd --- .github/workflows/release-pypi-kernel.yml | 1 + sgl-kernel/build.sh | 3 +- sgl-kernel/pyproject.toml | 3 +- sgl-kernel/setup.py | 35 ++++++++++++++++++++++- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/.github/workflows/release-pypi-kernel.yml b/.github/workflows/release-pypi-kernel.yml index 533390424f1..4b09f0854b0 100644 --- a/.github/workflows/release-pypi-kernel.yml +++ b/.github/workflows/release-pypi-kernel.yml @@ -31,6 +31,7 @@ jobs: ./build.sh "${{ matrix.python-version }}" "${{ matrix.cuda-version }}" - name: Upload to pypi + working-directory: sgl-kernel run: | pip install twine python3 -m twine upload dist/* -u __token__ -p ${{ secrets.PYPI_TOKEN }} diff --git a/sgl-kernel/build.sh b/sgl-kernel/build.sh index f1f0077f977..799b724dfe6 100755 --- a/sgl-kernel/build.sh +++ b/sgl-kernel/build.sh @@ -1,7 +1,5 @@ #!/bin/bash - set -ex - PYTHON_VERSION=$1 CUDA_VERSION=$2 PYTHON_ROOT_PATH=/opt/python/cp${PYTHON_VERSION//.}-cp${PYTHON_VERSION//.} @@ -12,6 +10,7 @@ docker run --rm \ bash -c " ${PYTHON_ROOT_PATH}/bin/pip install --no-cache-dir torch==2.4.0 --index-url https://download.pytorch.org/whl/cu${CUDA_VERSION//.} && \ export TORCH_CUDA_ARCH_LIST='7.5 8.0 8.9 9.0+PTX' && \ + export CUDA_VERSION=${CUDA_VERSION} && \ cd /sgl-kernel && \ ${PYTHON_ROOT_PATH}/bin/python setup.py bdist_wheel " diff --git a/sgl-kernel/pyproject.toml b/sgl-kernel/pyproject.toml index 4330d2e19be..ce434bc34ab 100644 --- a/sgl-kernel/pyproject.toml +++ b/sgl-kernel/pyproject.toml @@ -12,8 +12,7 @@ license = { file = "LICENSE" } classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: Apache Software License", - "Programming Language :: C++", - "Programming Language :: CUDA", + "Environment :: GPU :: NVIDIA CUDA" ] dependencies = [ "torch", diff --git a/sgl-kernel/setup.py b/sgl-kernel/setup.py index f2af83643e5..e9ab157dc7a 100644 --- a/sgl-kernel/setup.py +++ b/sgl-kernel/setup.py @@ -1,9 +1,40 @@ +import glob +import os +import shutil + from setuptools import find_packages, setup from torch.utils.cpp_extension import BuildExtension, CUDAExtension + +def get_base_version(): + with open("pyproject.toml") as f: + for line in f: + if line.startswith("version"): + return line.split("=")[1].strip().strip('"') + + +base_version = get_base_version() +cuda_version = os.environ.get("CUDA_VERSION", "").replace(".", "") +final_version = f"{base_version}+cu{cuda_version}" if cuda_version else base_version + + +def rename_wheel_with_cuda_version(dist_dir="dist"): + if not cuda_version: + return + wheel_files = glob.glob(f"{dist_dir}/*.whl") + for wheel_file in wheel_files: + base_name = os.path.basename(wheel_file) + if "+cu" not in base_name: + name_parts = base_name.split("-") + name_parts[1] += f"+cu{cuda_version}" + new_name = "-".join(name_parts) + new_path = os.path.join(dist_dir, new_name) + shutil.move(wheel_file, new_path) + + setup( name="sgl-kernel", - version="0.0.2", + version=final_version, packages=find_packages(where="src"), package_dir={"": "src"}, ext_modules=[ @@ -30,3 +61,5 @@ cmdclass={"build_ext": BuildExtension}, install_requires=["torch"], ) + +rename_wheel_with_cuda_version() From 8d65bdc453cb9a04ed50e5912dbfd890c0fdaed2 Mon Sep 17 00:00:00 2001 From: zhyncs Date: Tue, 10 Dec 2024 20:57:49 +0000 Subject: [PATCH 6/8] upd --- sgl-kernel/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sgl-kernel/build.sh b/sgl-kernel/build.sh index 799b724dfe6..73555531266 100755 --- a/sgl-kernel/build.sh +++ b/sgl-kernel/build.sh @@ -12,5 +12,5 @@ docker run --rm \ export TORCH_CUDA_ARCH_LIST='7.5 8.0 8.9 9.0+PTX' && \ export CUDA_VERSION=${CUDA_VERSION} && \ cd /sgl-kernel && \ - ${PYTHON_ROOT_PATH}/bin/python setup.py bdist_wheel + ${PYTHON_ROOT_PATH}/bin/pip wheel . " From f77700f416dfe1210bec36c7e760382c56ff0f37 Mon Sep 17 00:00:00 2001 From: zhyncs Date: Tue, 10 Dec 2024 22:01:29 +0000 Subject: [PATCH 7/8] upd --- sgl-kernel/build.sh | 2 +- sgl-kernel/setup.py | 69 +++++++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/sgl-kernel/build.sh b/sgl-kernel/build.sh index 73555531266..799b724dfe6 100755 --- a/sgl-kernel/build.sh +++ b/sgl-kernel/build.sh @@ -12,5 +12,5 @@ docker run --rm \ export TORCH_CUDA_ARCH_LIST='7.5 8.0 8.9 9.0+PTX' && \ export CUDA_VERSION=${CUDA_VERSION} && \ cd /sgl-kernel && \ - ${PYTHON_ROOT_PATH}/bin/pip wheel . + ${PYTHON_ROOT_PATH}/bin/python setup.py bdist_wheel " diff --git a/sgl-kernel/setup.py b/sgl-kernel/setup.py index e9ab157dc7a..8023578a182 100644 --- a/sgl-kernel/setup.py +++ b/sgl-kernel/setup.py @@ -1,41 +1,56 @@ -import glob import os import shutil +import zipfile +from pathlib import Path -from setuptools import find_packages, setup +from setuptools import setup from torch.utils.cpp_extension import BuildExtension, CUDAExtension +root = Path(__file__).parent.resolve() -def get_base_version(): - with open("pyproject.toml") as f: + +def get_version(): + with open(root / "pyproject.toml") as f: for line in f: if line.startswith("version"): return line.split("=")[1].strip().strip('"') -base_version = get_base_version() -cuda_version = os.environ.get("CUDA_VERSION", "").replace(".", "") -final_version = f"{base_version}+cu{cuda_version}" if cuda_version else base_version +def rename_wheel(): + if not os.environ.get("CUDA_VERSION"): + return + cuda_version = os.environ["CUDA_VERSION"].replace(".", "") + base_version = get_version() + wheel_dir = Path("dist") + old_wheel = next(wheel_dir.glob("*.whl")) + tmp_dir = wheel_dir / "tmp" + tmp_dir.mkdir(exist_ok=True) -def rename_wheel_with_cuda_version(dist_dir="dist"): - if not cuda_version: - return - wheel_files = glob.glob(f"{dist_dir}/*.whl") - for wheel_file in wheel_files: - base_name = os.path.basename(wheel_file) - if "+cu" not in base_name: - name_parts = base_name.split("-") - name_parts[1] += f"+cu{cuda_version}" - new_name = "-".join(name_parts) - new_path = os.path.join(dist_dir, new_name) - shutil.move(wheel_file, new_path) + with zipfile.ZipFile(old_wheel, "r") as zip_ref: + zip_ref.extractall(tmp_dir) + + old_info = tmp_dir / f"sgl_kernel-{base_version}.dist-info" + new_info = tmp_dir / f"sgl_kernel-{base_version}+cu{cuda_version}.dist-info" + old_info.rename(new_info) + + new_wheel = ( + wheel_dir + / f"sgl_kernel-{base_version}+cu{cuda_version}-{old_wheel.name.split('-', 2)[-1]}" + ) + with zipfile.ZipFile(new_wheel, "w", zipfile.ZIP_DEFLATED) as new_zip: + for file_path in tmp_dir.rglob("*"): + if file_path.is_file(): + new_zip.write(file_path, file_path.relative_to(tmp_dir)) + + old_wheel.unlink() + shutil.rmtree(tmp_dir) setup( name="sgl-kernel", - version=final_version, - packages=find_packages(where="src"), + version=get_version(), + packages=["sgl_kernel"], package_dir={"": "src"}, ext_modules=[ CUDAExtension( @@ -45,15 +60,7 @@ def rename_wheel_with_cuda_version(dist_dir="dist"): "src/sgl-kernel/csrc/warp_reduce_kernel.cu", ], extra_compile_args={ - "nvcc": [ - "-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", - ], + "nvcc": ["-O3", "-Xcompiler", "-fPIC"], "cxx": ["-O3"], }, ) @@ -62,4 +69,4 @@ def rename_wheel_with_cuda_version(dist_dir="dist"): install_requires=["torch"], ) -rename_wheel_with_cuda_version() +rename_wheel() From 7d90428854bb21169f3bff5b68f1e2b23d1bf8ea Mon Sep 17 00:00:00 2001 From: zhyncs Date: Tue, 10 Dec 2024 22:05:31 +0000 Subject: [PATCH 8/8] upd --- sgl-kernel/setup.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sgl-kernel/setup.py b/sgl-kernel/setup.py index 8023578a182..aaaae62bea2 100644 --- a/sgl-kernel/setup.py +++ b/sgl-kernel/setup.py @@ -60,7 +60,15 @@ def rename_wheel(): "src/sgl-kernel/csrc/warp_reduce_kernel.cu", ], extra_compile_args={ - "nvcc": ["-O3", "-Xcompiler", "-fPIC"], + "nvcc": [ + "-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", + ], "cxx": ["-O3"], }, )