Build Python wheels #132
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build Python wheels | |
on: | |
push: | |
branches: [main] | |
tags: ["*"] | |
pull_request: | |
paths: | |
# build wheels in PR if this file changed | |
- '.github/workflows/build-wheels.yml' | |
# build wheels in PR if any of the build system files changed | |
- '**/VERSION' | |
- '**/setup.py' | |
- '**/pyproject.toml' | |
- '**/MANIFEST.in' | |
- '**/CMakeLists.txt' | |
schedule: | |
# check the build once a week on mondays | |
- cron: '0 10 * * 1' | |
concurrency: | |
group: wheels-${{ github.ref }} | |
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} | |
jobs: | |
build-wheels: | |
runs-on: ${{ matrix.os }} | |
name: ${{ matrix.name }} | |
strategy: | |
matrix: | |
include: | |
- name: x86_64 Linux | |
os: ubuntu-22.04 | |
cibw_arch: x86_64 | |
- name: arm64 Linux | |
os: ubuntu-22.04 | |
cibw_arch: aarch64 | |
- name: x86_64 macOS | |
os: macos-13 | |
cibw_arch: x86_64 | |
- name: M1 macOS | |
os: macos-14 | |
cibw_arch: arm64 | |
- name: x86_64 Windows | |
os: windows-2019 | |
cibw_arch: AMD64 | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.12" | |
- name: Set up QEMU for docker | |
if: matrix.os == 'ubuntu-22.04' | |
uses: docker/setup-qemu-action@v3 | |
- name: install dependencies | |
run: python -m pip install cibuildwheel twine | |
- name: build wheel | |
run: python -m cibuildwheel python/vesin | |
env: | |
CIBW_BUILD: cp312-* | |
CIBW_SKIP: "*musllinux*" | |
CIBW_ARCHS: ${{ matrix.cibw_arch }} | |
CIBW_BUILD_FRONTEND: build | |
CIBW_MANYLINUX_X86_64_IMAGE: quay.io/pypa/manylinux2014_x86_64 | |
- name: check wheels with twine | |
run: twine check wheelhouse/* | |
- uses: actions/upload-artifact@v4 | |
with: | |
name: wheel-${{ matrix.os }}-${{ matrix.cibw_arch }} | |
path: ./wheelhouse/*.whl | |
build-torch-wheels: | |
runs-on: ${{ matrix.os }} | |
name: ${{ matrix.name }} (torch v${{ matrix.torch-version }}) | |
strategy: | |
matrix: | |
torch-version: ['2.3', '2.4', '2.5'] | |
arch: ['arm64', 'x86_64'] | |
os: ['ubuntu-22.04', 'macos-14'] | |
exclude: | |
# x86_64 is only supported for torch <2.3 in macOS | |
- {os: macos-14, arch: x86_64} | |
include: | |
# add `cibw-arch` to the different configurations | |
- name: x86_64 Linux | |
os: ubuntu-22.04 | |
arch: x86_64 | |
cibw-arch: x86_64 | |
- name: arm64 Linux | |
os: ubuntu-22.04 | |
arch: arm64 | |
cibw-arch: aarch64 | |
- name: arm64 macOS | |
os: macos-14 | |
arch: arm64 | |
cibw-arch: arm64 | |
# add the right python version for each torch version | |
- {torch-version: '2.3', python-version: '3.12', cibw-python: 'cp312-*'} | |
- {torch-version: '2.4', python-version: '3.12', cibw-python: 'cp312-*'} | |
- {torch-version: '2.5', python-version: '3.12', cibw-python: 'cp312-*'} | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: ${{ matrix.python-version }} | |
- name: install dependencies | |
run: python -m pip install cibuildwheel | |
- name: Set up QEMU for docker | |
if: matrix.os == 'ubuntu-22.04' | |
uses: docker/setup-qemu-action@v3 | |
- name: build vesin-torch wheel | |
run: python -m cibuildwheel python/vesin-torch | |
env: | |
CIBW_BUILD: ${{ matrix.cibw-python}} | |
CIBW_SKIP: "*musllinux*" | |
CIBW_ARCHS: ${{ matrix.cibw-arch }} | |
CIBW_BUILD_VERBOSITY: 1 | |
CIBW_MANYLINUX_X86_64_IMAGE: quay.io/pypa/manylinux2014_x86_64 | |
CIBW_ENVIRONMENT: > | |
VESIN_TORCH_BUILD_WITH_TORCH_VERSION=${{ matrix.torch-version }}.* | |
PIP_EXTRA_INDEX_URL=https://download.pytorch.org/whl/cpu | |
MACOSX_DEPLOYMENT_TARGET=11 | |
# do not complain for missing libtorch.so | |
CIBW_REPAIR_WHEEL_COMMAND_MACOS: | | |
delocate-wheel --ignore-missing-dependencies --require-archs {delocate_archs} -w {dest_dir} -v {wheel} | |
CIBW_REPAIR_WHEEL_COMMAND_LINUX: | | |
auditwheel repair --exclude libtorch.so --exclude libtorch_cpu.so --exclude libc10.so -w {dest_dir} {wheel} | |
- uses: actions/upload-artifact@v4 | |
with: | |
name: torch-single-version-wheel-${{ matrix.torch-version }}-${{ matrix.os }}-${{ matrix.arch }} | |
path: ./wheelhouse/*.whl | |
merge-torch-wheels: | |
needs: build-torch-wheels | |
runs-on: ubuntu-22.04 | |
name: merge vesin-torch ${{ matrix.name }} | |
strategy: | |
matrix: | |
include: | |
- name: x86_64 Linux | |
os: ubuntu-22.04 | |
arch: x86_64 | |
- name: arm64 Linux | |
os: ubuntu-22.04 | |
arch: arm64 | |
- name: arm64 macOS | |
os: macos-14 | |
arch: arm64 | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Download wheels | |
uses: actions/download-artifact@v4 | |
with: | |
pattern: torch-single-version-wheel-*-${{ matrix.os }}-${{ matrix.arch }} | |
merge-multiple: false | |
path: dist | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.12" | |
- name: install dependencies | |
run: python -m pip install twine wheel | |
- name: merge wheels | |
run: | | |
# collect all torch versions used for the build | |
REQUIRES_TORCH=$(find dist -name "*.whl" -exec unzip -p {} "vesin_torch-*.dist-info/METADATA" \; | grep "Requires-Dist: torch") | |
MERGED_TORCH_REQUIRE=$(python scripts/create-torch-versions-range.py "$REQUIRES_TORCH") | |
echo MERGED_TORCH_REQUIRE=$MERGED_TORCH_REQUIRE | |
# unpack all single torch versions wheels in the same directory | |
mkdir dist/unpacked | |
find dist -name "*.whl" -print -exec python -m wheel unpack --dest dist/unpacked/ {} ';' | |
sed -i "s/Requires-Dist: torch.*/$MERGED_TORCH_REQUIRE/" dist/unpacked/vesin_torch-*/vesin_torch-*.dist-info/METADATA | |
echo "\n\n METADATA = \n\n" | |
cat dist/unpacked/vesin_torch-*/vesin_torch-*.dist-info/METADATA | |
# check the right metadata was added to the file. grep will exit with | |
# code `1` if the line is not found, which will stop CI | |
grep "$MERGED_TORCH_REQUIRE" dist/unpacked/vesin_torch-*/vesin_torch-*.dist-info/METADATA | |
# repack the directory as a new wheel | |
mkdir wheelhouse | |
python -m wheel pack --dest wheelhouse/ dist/unpacked/* | |
- name: check wheels with twine | |
run: twine check wheelhouse/* | |
- uses: actions/upload-artifact@v4 | |
with: | |
name: torch-wheel-${{ matrix.os }}-${{ matrix.arch }} | |
path: ./wheelhouse/*.whl | |
build-sdist: | |
runs-on: ubuntu-22.04 | |
name: sdist | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.12" | |
- name: build sdist | |
run: | | |
pip install build | |
python -m build --sdist python/vesin --outdir dist | |
python -m build --sdist python/vesin-torch --outdir dist | |
- uses: actions/upload-artifact@v4 | |
with: | |
name: sdist | |
path: dist/*.tar.gz | |
merge-and-release: | |
name: Merge and release wheels/sdists | |
needs: [build-wheels, merge-torch-wheels, build-sdist] | |
runs-on: ubuntu-22.04 | |
permissions: | |
contents: write | |
steps: | |
- name: Download wheels | |
uses: actions/download-artifact@v4 | |
with: | |
path: dist | |
pattern: wheel-* | |
merge-multiple: true | |
- name: Download metatensor-torch wheels | |
uses: actions/download-artifact@v4 | |
with: | |
path: dist | |
pattern: torch-wheel-* | |
merge-multiple: true | |
- name: Download sdist | |
uses: actions/download-artifact@v4 | |
with: | |
path: dist | |
name: sdist | |
- name: Re-upload a single wheels artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: wheels | |
path: | | |
dist/* | |
- name: upload to GitHub release | |
if: startsWith(github.ref, 'refs/tags/') | |
uses: softprops/action-gh-release@v2 | |
with: | |
files: | | |
dist/* | |
prerelease: ${{ contains(github.ref, '-rc') }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |