Skip to content

Commit 62690c9

Browse files
committed
Add CI for building wheels
1 parent e299233 commit 62690c9

File tree

6 files changed

+331
-42
lines changed

6 files changed

+331
-42
lines changed

.github/workflows/build-wheels.yml

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
name: Build wheels
2+
3+
on:
4+
push:
5+
branches: [main]
6+
tags: ["*"]
7+
pull_request:
8+
# Check all PR
9+
10+
11+
concurrency:
12+
group: python-wheels-${{ github.ref }}
13+
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
14+
15+
16+
jobs:
17+
build-wheels:
18+
runs-on: ${{ matrix.os }}
19+
name: ${{ matrix.os }} (${{ matrix.arch }}, torch v${{ matrix.torch-version }})
20+
strategy:
21+
matrix:
22+
torch-version: ["2.4", "2.5", "2.6"]
23+
arch: ["arm64", "x86_64"]
24+
os: ["ubuntu-22.04", "macos-14"]
25+
exclude:
26+
# remove mismatched arch/os pairs
27+
- {os: macos-14, arch: x86_64}
28+
- {os: ubuntu-22.04, arch: arm64}
29+
30+
steps:
31+
- uses: actions/checkout@v4
32+
with:
33+
fetch-depth: 0
34+
35+
- name: Set up Python
36+
uses: actions/setup-python@v5
37+
with:
38+
python-version: "3.13"
39+
40+
- name: install dependencies
41+
run: python -m pip install cibuildwheel
42+
43+
- name: build wheel
44+
run: python -m cibuildwheel --output-dir ./wheelhouse
45+
env:
46+
CIBW_BUILD: cp312-*
47+
CIBW_SKIP: "*musllinux*"
48+
CIBW_ARCHS: "${{ matrix.arch }}"
49+
CIBW_BUILD_VERBOSITY: 1
50+
CIBW_ENVIRONMENT: >
51+
PIP_EXTRA_INDEX_URL=https://download.pytorch.org/whl/cpu
52+
MACOSX_DEPLOYMENT_TARGET=11
53+
PETNC_BUILD_WITH_TORCH_VERSION=${{ matrix.torch-version }}.*
54+
# do not complain for missing libtorch.so
55+
CIBW_REPAIR_WHEEL_COMMAND_MACOS: |
56+
delocate-wheel --ignore-missing-dependencies --require-archs {delocate_archs} -w {dest_dir} -v {wheel}
57+
CIBW_REPAIR_WHEEL_COMMAND_LINUX: |
58+
auditwheel repair --exclude libtorch.so --exclude libtorch_cpu.so --exclude libc10.so -w {dest_dir} {wheel}
59+
60+
- uses: actions/upload-artifact@v4
61+
with:
62+
name: single-version-wheel-${{ matrix.torch-version }}-${{ matrix.os }}-${{ matrix.arch }}
63+
path: ./wheelhouse/*.whl
64+
65+
merge-wheels:
66+
needs: build-wheels
67+
runs-on: ubuntu-22.04
68+
name: merge wheels for ${{ matrix.name }}
69+
strategy:
70+
matrix:
71+
include:
72+
- name: x86_64 Linux
73+
os: ubuntu-22.04
74+
arch: x86_64
75+
- name: arm64 macOS
76+
os: macos-14
77+
arch: arm64
78+
steps:
79+
- uses: actions/checkout@v4
80+
81+
- name: Download wheels
82+
uses: actions/download-artifact@v4
83+
with:
84+
pattern: single-version-wheel-*-${{ matrix.os }}-${{ matrix.arch }}
85+
merge-multiple: false
86+
path: dist
87+
88+
- name: Set up Python
89+
uses: actions/setup-python@v5
90+
with:
91+
python-version: "3.13"
92+
93+
- name: install dependencies
94+
run: python -m pip install twine wheel
95+
96+
- name: merge wheels
97+
run: |
98+
set -eux
99+
100+
# collect all torch versions used for the build
101+
REQUIRES_TORCH=$(find dist -name "*.whl" -exec unzip -p {} "pet_neighbors_convert-*.dist-info/METADATA" \; | grep "Requires-Dist: torch")
102+
MERGED_TORCH_REQUIRE=$(python scripts/create-torch-versions-range.py "$REQUIRES_TORCH")
103+
104+
echo MERGED_TORCH_REQUIRE=$MERGED_TORCH_REQUIRE
105+
106+
# unpack all single torch versions wheels in the same directory
107+
mkdir dist/unpacked
108+
find dist -name "*.whl" -print -exec python -m wheel unpack --dest dist/unpacked/ {} ';'
109+
110+
sed -i "s/Requires-Dist: torch.*/$MERGED_TORCH_REQUIRE/" dist/unpacked/pet_neighbors_convert-*/pet_neighbors_convert-*.dist-info/METADATA
111+
112+
echo "\n\n METADATA = \n\n"
113+
cat dist/unpacked/pet_neighbors_convert-*/pet_neighbors_convert-*.dist-info/METADATA
114+
115+
# check the right metadata was added to the file. grep will exit with
116+
# code `1` if the line is not found, which will stop CI
117+
grep "$MERGED_TORCH_REQUIRE" dist/unpacked/pet_neighbors_convert-*/pet_neighbors_convert-*.dist-info/METADATA
118+
119+
# repack the directory as a new wheel
120+
mkdir wheelhouse
121+
python -m wheel pack --dest wheelhouse/ dist/unpacked/*
122+
123+
- name: check wheels with twine
124+
run: twine check wheelhouse/*
125+
126+
- uses: actions/upload-artifact@v4
127+
with:
128+
name: wheel-${{ matrix.os }}-${{ matrix.arch }}
129+
path: ./wheelhouse/*.whl
130+
131+
build-sdist:
132+
name: sdist
133+
runs-on: ubuntu-22.04
134+
steps:
135+
- uses: actions/checkout@v4
136+
with:
137+
fetch-depth: 0
138+
139+
- name: Set up Python
140+
uses: actions/setup-python@v5
141+
with:
142+
python-version: "3.13"
143+
144+
- name: install dependencies
145+
run: python -m pip install build
146+
147+
- name: build sdist
148+
run: python -m build . --outdir=dist/
149+
150+
- uses: actions/upload-artifact@v4
151+
with:
152+
name: sdist
153+
path: dist/*.tar.gz
154+
155+
merge-and-release:
156+
name: Merge and release wheels/sdists
157+
needs: [merge-wheels, build-sdist]
158+
runs-on: ubuntu-22.04
159+
permissions:
160+
contents: write
161+
pull-requests: write
162+
steps:
163+
- name: Download wheels
164+
uses: actions/download-artifact@v4
165+
with:
166+
path: wheels
167+
pattern: wheel-*
168+
merge-multiple: true
169+
170+
- name: Download sdist
171+
uses: actions/download-artifact@v4
172+
with:
173+
path: wheels
174+
name: sdist
175+
176+
- name: Re-upload a single wheels artifact
177+
uses: actions/upload-artifact@v4
178+
with:
179+
name: wheels
180+
path: wheels/*
181+
182+
- name: Comment with download link
183+
uses: PicoCentauri/comment-artifact@v1
184+
if: github.event.pull_request.head.repo.fork == false
185+
with:
186+
name: wheels
187+
description: ⚙️ Download Python wheels for this pull-request (you can install these with pip)
188+
189+
- name: upload to GitHub release
190+
if: startsWith(github.ref, 'refs/tags/v')
191+
uses: softprops/action-gh-release@v2
192+
with:
193+
files: |
194+
wheels/*.tar.gz
195+
wheels/*.whl
196+
prerelease: ${{ contains(github.ref, '-rc') }}
197+
env:
198+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

MANIFEST.in

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ graft src
33
include LICENSE
44
include README.rst
55

6-
prune tests
76
prune .github
87
prune .tox
8+
prune tests
9+
prune scripts
910

1011
exclude .gitignore
1112
exclude tox.ini
13+
recursive-include build-backend *.py
1214

1315
global-exclude *.py[cod] __pycache__/* *.so *.dylib

build-backend/backend.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This is a custom Python build backend wrapping setuptool's to add a build-time
2+
# dependencies on torch when building the wheel and not the sdist
3+
import os
4+
from setuptools import build_meta
5+
6+
FORCED_TORCH_VERSION = os.environ.get("PETNC_BUILD_WITH_TORCH_VERSION")
7+
if FORCED_TORCH_VERSION is not None:
8+
TORCH_DEP = f"torch =={FORCED_TORCH_VERSION}"
9+
else:
10+
TORCH_DEP = "torch >=1.12"
11+
12+
# ==================================================================================== #
13+
# Build backend functions definition #
14+
# ==================================================================================== #
15+
16+
# Use the default version of these
17+
prepare_metadata_for_build_wheel = build_meta.prepare_metadata_for_build_wheel
18+
get_requires_for_build_sdist = build_meta.get_requires_for_build_sdist
19+
build_wheel = build_meta.build_wheel
20+
build_sdist = build_meta.build_sdist
21+
22+
23+
# Special dependencies to build the wheels
24+
def get_requires_for_build_wheel(config_settings=None):
25+
defaults = build_meta.get_requires_for_build_wheel(config_settings)
26+
return defaults + [TORCH_DEP]

pyproject.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[build-system]
2-
requires = ["setuptools >= 68", "setuptools_scm>=8", "wheel", "torch >= 2.3"]
3-
build-backend = "setuptools.build_meta"
2+
requires = ["setuptools >= 68", "setuptools_scm>=8", "wheel >= 0.36"]
3+
build-backend = "backend"
4+
backend-path = ["build-backend"]
45

56
[project]
67
authors = [{name = "lab-cosmo developers"}]
@@ -15,9 +16,8 @@ classifiers = [
1516
"Topic :: Scientific/Engineering",
1617
"Topic :: Software Development :: Libraries :: Python Modules"
1718
]
18-
dependencies = ["torch >= 2.3"]
1919
description = "Extension for PET model for reordering the neighbors during message passing."
20-
dynamic = ["version"]
20+
dynamic = ["version", "dependencies"]
2121
license = {text = "BSD-3-Clause"}
2222
name = "pet-neighbors-convert"
2323
readme = "README.rst"
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
"""
3+
This script updates the `Requires-Dist` information in the wheel METADATA
4+
to contain the range of compatible torch versions. It expects newline separated
5+
`Requires-Dist: torch ==...` information (corresponding to wheels built against a single
6+
torch version) and will print `Requires-Dist: torch >=$MIN_VERSION,<${MAX_VERSION+1}` on
7+
the standard output.
8+
9+
This output can the be used in the merged wheel containing the build against all torch
10+
versions.
11+
"""
12+
import re
13+
import sys
14+
15+
16+
if __name__ == "__main__":
17+
torch_versions_raw = sys.argv[1]
18+
19+
torch_versions = []
20+
for version in torch_versions_raw.split("\n"):
21+
if version.strip() == "":
22+
continue
23+
24+
match = re.match(r"Requires-Dist: torch[ ]?==(\d+)\.(\d+)\.\*", version)
25+
if match is None:
26+
raise ValueError(f"unexpected Requires-Dist format: {version}")
27+
28+
major, minor = match.groups()
29+
major = int(major)
30+
minor = int(minor)
31+
32+
version = (major, minor)
33+
34+
if version in torch_versions:
35+
raise ValueError(f"duplicate torch version: {version}")
36+
37+
torch_versions.append(version)
38+
39+
torch_versions = list(sorted(torch_versions))
40+
41+
min_version = f"{torch_versions[0][0]}.{torch_versions[0][1]}"
42+
max_version = f"{torch_versions[-1][0]}.{torch_versions[-1][1] + 1}"
43+
44+
print(f"Requires-Dist: torch >={min_version},<{max_version}")

0 commit comments

Comments
 (0)