Skip to content

Commit

Permalink
[Package] Windows and Mac (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterJH5574 authored Nov 17, 2024
1 parent c7ea3bb commit 1dfdb88
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 45 deletions.
109 changes: 109 additions & 0 deletions .github/workflows/wheel_mac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# GH actions.
name: Wheel-Mac

on:
push:
tags:
- '*' # Push events to every tag not containing /

jobs:
Build:
strategy:
matrix:
platform: ['macos-13', 'macos-latest']

runs-on: ${{ matrix.platform }}
environment:
name: pypi
url: https://pypi.org/p/xgrammar
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
defaults:
run:
shell: 'bash -l {0}'

steps:
- uses: actions/checkout@v3
with:
submodules: 'recursive'
- name: Checkout source
run: |
git clone https://github.com/mlc-ai/package package --recursive
- name: Setup script env
run: |
mv scripts/build-environment.yaml package/3rdparty/tlcpack/conda/build-environment.yaml
rm -rf conda
ln -s package/3rdparty/tlcpack/conda conda
- name: Sync XGrammar Package
run: |
python scripts/sync_package.py --package . --package-name xgrammar --version ${{ github.ref_name }}
# Use conda for LLVM dep
- uses: conda-incubator/setup-miniconda@v3
with:
activate-environment: xgrammar-build
channel-priority: strict
environment-file: conda/build-environment.yaml
auto-activate-base: false
- name: Conda info
run: |
conda info
conda list
python --version
- name: Build@MacOS
run: >-
scripts/build_xgrammar_lib_osx.sh ${{ matrix.platform == 'macos-13' && '10.15' || '13.02' }}
# Build wheel for different python versions
- name: Setup@Py39
uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: build-Py39
python-version: 3.9
auto-activate-base: false
- name: Wheel-Build@Py39
run: |
python --version
python -m pip install setuptools Cython wheel
cd python
python setup.py bdist_wheel
- name: Setup@Py310
uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: build-Py310
python-version: '3.10'
auto-activate-base: false
- name: Wheel-Build@Py310
run: |
python --version
python -m pip install setuptools Cython wheel
cd python
python setup.py bdist_wheel
- name: Setup@Py311
uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: build-Py311
python-version: '3.11'
auto-activate-base: false
- name: Wheel-Build@Py311
run: |
python --version
python -m pip install setuptools Cython wheel
cd python
python setup.py bdist_wheel
- name: Setup@Py312
uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: build-Py312
python-version: '3.12'
auto-activate-base: false
- name: Wheel-Build@Py312
run: |
python --version
python -m pip install setuptools Cython wheel
cd python
python setup.py bdist_wheel
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages_dir: python/dist
skip_existing: true
verbose: true
12 changes: 5 additions & 7 deletions .github/workflows/wheel_manylinux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
name: Wheel-Manylinux


on: push
on:
push:
tags:
- '*' # Push events to every tag not containing /

jobs:
Build:
if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
strategy:
matrix:
config:
Expand Down Expand Up @@ -39,16 +41,12 @@ jobs:
ln -s package/3rdparty/tlcpack/conda conda
- name: Sync XGrammar Package
run: |
python scripts/sync_package.py --package . --package-name xgrammar --version ${{ github.ref_name }} --skip-conda
python scripts/sync_package.py --package . --package-name xgrammar --version ${{ github.ref_name }}
- name: Build XGrammar
env:
IMAGE: ${{ matrix.config.image }}
run: |
scripts/docker/bash.sh --no-gpu $IMAGE ./scripts/build_xgrammar_wheel_manylinux.sh
- name: Retrieve all wheels
run: |
mkdir -p dist
cp python/repaired_wheels/* dist/
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
Expand Down
108 changes: 108 additions & 0 deletions .github/workflows/wheel_windows.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# GH actions.
name: Wheel-Windows

on:
push:
tags:
- '*' # Push events to every tag not containing /

jobs:
Build:
runs-on: windows-latest
environment:
name: pypi
url: https://pypi.org/p/xgrammar
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
defaults:
run:
shell: 'cmd /C call {0}'

steps:
- name: Git config
run: >-
git config --system core.longpaths true
- uses: actions/checkout@v3
with:
submodules: 'recursive'
- name: Checkout source
run: |
git clone https://github.com/mlc-ai/package package --recursive
- name: Setup script env
run: |
mv scripts/build-environment.yaml package/3rdparty/tlcpack/conda/build-environment.yaml
rm -rf conda
ln -s package/3rdparty/tlcpack/conda conda
- name: Sync XGrammar Package
run: |
python scripts/sync_package.py --package . --package-name xgrammar --version ${{ github.ref_name }}
# Use conda for LLVM dep
- uses: conda-incubator/setup-miniconda@v3
with:
activate-environment: xgrammar-build
channel-priority: strict
environment-file: conda/build-environment.yaml
auto-activate-base: false
- name: Conda info
run: |
conda info
conda list
python --version
- name: Build@Win
run: >-
scripts/build_xgrammar_lib_win.bat
# Build wheel for different python versions
- name: Setup@Py39
uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: build-Py39
python-version: 3.9
auto-activate-base: false
- name: Wheel-Build@Py39
run: |
python --version
python -m pip install setuptools Cython wheel
cd python
python setup.py bdist_wheel
- name: Setup@Py310
uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: build-Py310
python-version: '3.10'
auto-activate-base: false
- name: Wheel-Build@Py310
run: |
python --version
python -m pip install setuptools Cython wheel
cd python
python setup.py bdist_wheel
- name: Setup@Py311
uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: build-Py311
python-version: '3.11'
auto-activate-base: false
- name: Wheel-Build@Py311
run: |
python --version
python -m pip install setuptools Cython wheel
cd python
python setup.py bdist_wheel
- name: Setup@Py312
uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: build-Py312
python-version: '3.12'
auto-activate-base: false
- name: Wheel-Build@Py312
run: |
python --version
python -m pip install setuptools Cython wheel
cd python
python setup.py bdist_wheel
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages_dir: python/dist
skip_existing: true
verbose: true
8 changes: 5 additions & 3 deletions cpp/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ if(XGRAMMAR_BUILD_KERNELS)
endif()

target_link_libraries(xgrammar_bindings PUBLIC ${TORCH_LIBRARIES} ${TORCH_PYTHON_LIBRARY})
set_target_properties(
xgrammar_bindings PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/python/xgrammar"
)
set(LIB_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/python/xgrammar")
set_target_properties(xgrammar_bindings PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${LIB_OUTPUT_DIRECTORY})
set_target_properties(xgrammar_bindings PROPERTIES LIBRARY_OUTPUT_DIRECTORY_DEBUG ${LIB_OUTPUT_DIRECTORY})
set_target_properties(xgrammar_bindings PROPERTIES LIBRARY_OUTPUT_DIRECTORY_RELEASE ${LIB_OUTPUT_DIRECTORY})
set_target_properties(xgrammar_bindings PROPERTIES LIBRARY_OUTPUT_DIRECTORY_REL_WITH_DEB_INFO ${LIB_OUTPUT_DIRECTORY})

if(MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
Expand Down
20 changes: 20 additions & 0 deletions scripts/build-environment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: xgrammar-build

channels:
- pytorch
- conda-forge

dependencies:
- llvmdev>=15
- cmake>=3.24
- zlib
- zstd-static
- git
- conda-build
- numpy
- pytest
- pip
- cython
- pybind11
- pytorch
- cpuonly
27 changes: 27 additions & 0 deletions scripts/build_xgrammar_lib_osx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

set -e
set -u

# setup config.cmake
rm -f config.cmake
echo set\(XGRAMMAR_BUILD_PYTHON_BINDINGS ON\) >>config.cmake
echo set\(XGRAMMAR_BUILD_KERNELS OFF\) >>config.cmake
echo set\(XGRAMMAR_BUILD_CUDA_KERNELS OFF\) >>config.cmake
echo set\(XGRAMMAR_BUILD_CXX_TESTS OFF\) >>config.cmake


# compile the xgrammar
rm -rf build
mkdir -p build
cd build

MACOSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET:-${1}}

cmake -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_OSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET} \
-DHIDE_PRIVATE_SYMBOLS=ON \
..

make -j3
cd ..
23 changes: 23 additions & 0 deletions scripts/build_xgrammar_lib_win.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
echo on

del /f config.cmake
echo set(XGRAMMAR_BUILD_PYTHON_BINDINGS ON) >> config.cmake
echo set(XGRAMMAR_BUILD_KERNELS OFF) >> config.cmake
echo set(XGRAMMAR_BUILD_CUDA_KERNELS OFF) >> config.cmake
echo set(XGRAMMAR_BUILD_CXX_TESTS OFF) >> config.cmake

rd /s /q build
mkdir build
cd build

cmake -A x64 -Thost=x64 ^
-G "Visual Studio 17 2022" ^
..

if %errorlevel% neq 0 exit %errorlevel%

cmake --build . --parallel 3 --config Release --target xgrammar_bindings -- /m

if %errorlevel% neq 0 exit %errorlevel%

cd ..
6 changes: 3 additions & 3 deletions scripts/build_xgrammar_wheel_manylinux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ echo set\(XGRAMMAR_BUILD_CUDA_KERNELS OFF\) >>config.cmake
echo set\(XGRAMMAR_BUILD_CXX_TESTS OFF\) >>config.cmake

# compile the xgrammar
python3 -m pip install ninja pybind11
python3 -m pip install pybind11
python3 -m pip install torch --index-url https://download.pytorch.org/whl/cpu
mkdir -p build
cd build
cmake .. -G Ninja
ninja -j 4
cmake ..
make -j4
find . -type d -name 'CMakeFiles' -exec rm -rf {} +

UNICODE_WIDTH=32 # Dummy value, irrelevant for Python 3
Expand Down
Loading

0 comments on commit 1dfdb88

Please sign in to comment.