Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 26 additions & 123 deletions .github/workflows/build-jax-wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,73 +49,47 @@ jobs:

build-jax-wheels:
runs-on: ${{ matrix.os }}
name: ${{ matrix.name }}
strategy:
matrix:
jax-version:
- "0.6.2"
- "0.7.0"
- "0.7.1"
- "0.7.2"
- "0.8.0"
- "0.8.1"
- "0.8.2"
- "0.8.3"
- "0.9.0"
- "0.9.1"
- "0.9.2"
- "0.10.0"
- "0.10.1"
os: [ubuntu-24.04, macos-15, ubuntu-24.04-arm, windows-2022]
include:
- name: x86_64 Linux
os: ubuntu-24.04
arch: x86_64
- name: x86_64 macOS
os: macos-15
arch: x86_64
- name: arm64 macOS
os: macos-15
arch: arm64
- name: aarch64 Linux
os: ubuntu-24.04-arm
arch: aarch64
- name: arm64 macOS
os: macos-15
arch: arm64
- name: x86_64 Windows
os: windows-2022
arch: AMD64

name: "JAX ${{matrix.jax-version}} ${{matrix.os}} ${{matrix.arch}}"

steps:
- name: Checkout code
uses: actions/checkout@v7

- name: Build custom manylinux Docker Image with CUDA
if: startsWith(matrix.os, 'ubuntu-')
run: |
docker build --no-cache \
-t sphericart_manylinux_2_28_"${{ matrix.arch }}" \
scripts/manylinux_2_28_"${{ matrix.arch }}"

- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: "3.15"

- name: Install cibuildwheel
run: python -m pip install cibuildwheel
- name: Install dependencies
run: python -m pip install cibuildwheel twine

- name: Build jax wheels
run: python -m cibuildwheel ./sphericart-jax
env:
CIBW_BUILD_VERBOSITY: 1
# the wheel does not depend on the Python version, any version
# supported by the oldest jaxlib works to build it
CIBW_BUILD: "cp312-*"
CIBW_SKIP: "*-musllinux*"
CIBW_ARCHS: "${{ matrix.arch }}"
CIBW_MANYLINUX_X86_64_IMAGE: sphericart_manylinux_2_28_x86_64
CIBW_MANYLINUX_AARCH64_IMAGE: sphericart_manylinux_2_28_aarch64
CIBW_REPAIR_WHEEL_COMMAND_LINUX: |
auditwheel repair \
-w {dest_dir} {wheel}
CIBW_BUILD_VERBOSITY: 1
# jaxlib wheels require glibc >= 2.27.
CIBW_MANYLINUX_X86_64_IMAGE: quay.io/pypa/manylinux_2_28_x86_64
CIBW_MANYLINUX_AARCH64_IMAGE: quay.io/pypa/manylinux_2_28_aarch64
CIBW_REPAIR_WHEEL_COMMAND_MACOS: |
delocate-wheel --ignore-missing-dependencies \
--require-archs {delocate_archs} \
Expand All @@ -125,93 +99,22 @@ jobs:
CIBW_ENVIRONMENT: >
SPHERICART_ARCH_NATIVE=OFF
SPHERICART_NO_LOCAL_DEPS=1
SPHERICART_JAX_BUILD_WITH_JAX_VERSION=${{ matrix.jax-version }}.*
MACOSX_DEPLOYMENT_TARGET=11

- name: check wheels with twine
run: twine check wheelhouse/*

- uses: actions/upload-artifact@v7
with:
name: jax-single-version-wheel-${{ matrix.os }}-${{ matrix.arch }}-jax-${{ matrix.jax-version }}
name: sphericart-jax-wheel-${{ matrix.os }}-${{ matrix.arch }}
path: ./wheelhouse/*.whl

merge-jax-wheels:
needs: build-jax-wheels
runs-on: ubuntu-24.04
name: merge wheels for ${{ matrix.name }}
strategy:
matrix:
include:
- name: x86_64 Linux
os: ubuntu-24.04
arch: x86_64
- name: arm64 macOS
os: macos-15
arch: arm64
- name: aarch64 Linux
os: ubuntu-24.04-arm
arch: aarch64
- name: x86_64 Windows
os: windows-2022
arch: AMD64
steps:
- uses: actions/checkout@v7

- name: Download wheels
uses: actions/download-artifact@v8
with:
pattern: jax-single-version-wheel-${{ matrix.os }}-${{ matrix.arch }}-*
merge-multiple: false
path: dist

- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: "3.12"

- name: install dependencies
run: python -m pip install twine wheel

- name: merge wheels
run: |
set -euxo pipefail

ls dist/

# collect all jax versions used for the build
REQUIRES_JAX=$(find dist -name "*.whl" -exec unzip -p {} "sphericart_jax-*.dist-info/METADATA" \; | grep "Requires-Dist: jax")
MERGED_JAX_REQUIRE=$(python scripts/create-jax-versions-range.py "$REQUIRES_JAX")

echo MERGED_JAX_REQUIRE=$MERGED_JAX_REQUIRE

# unpack all single jax 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: jax.*/$MERGED_JAX_REQUIRE/" dist/unpacked/sphericart_jax-*/sphericart_jax-*.dist-info/METADATA

echo "\n\n METADATA = \n\n"
cat dist/unpacked/sphericart_jax-*/sphericart_jax-*.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_JAX_REQUIRE" dist/unpacked/sphericart_jax-*/sphericart_jax-*.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@v7
with:
name: sphericart-jax-wheel-${{ matrix.os }}-${{ matrix.arch }}
path: ./wheelhouse/*.whl

- name: upload to GitHub release
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v3
with:
files: |
./wheelhouse/*.whl
prerelease: ${{ contains(github.ref, '-rc') }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: upload to GitHub release
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v3
with:
files: |
./wheelhouse/*.whl
prerelease: ${{ contains(github.ref, '-rc') }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
46 changes: 0 additions & 46 deletions scripts/create-jax-versions-range.py

This file was deleted.

2 changes: 2 additions & 0 deletions sphericart-jax/MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ recursive-include include *

include pyproject.toml
include README.md

recursive-include build-backend *.py
15 changes: 5 additions & 10 deletions sphericart-jax/build-backend/backend.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
# This is a custom Python build backend wrapping setuptool's to add a build-time
# dependencies on jax/cmake when building the wheel and not the sdist
import os
from setuptools import build_meta


ROOT = os.path.realpath(os.path.dirname(__file__))

FORCED_JAX_VERSION = os.environ.get("SPHERICART_JAX_BUILD_WITH_JAX_VERSION")
if FORCED_JAX_VERSION is not None:
JAX_DEP = f"jax =={FORCED_JAX_VERSION}"
else:
JAX_DEP = "jax >=0.6.0"

# ==================================================================================== #
# Build backend functions definition #
# ==================================================================================== #
Expand All @@ -26,4 +17,8 @@
# Special dependencies to build the wheels
def get_requires_for_build_wheel(config_settings=None):
defaults = build_meta.get_requires_for_build_wheel(config_settings)
return defaults + ["cmake >=3.30", JAX_DEP]
return defaults + [
"cmake >=3.30",
# This should be the lowest supported version of `install_requires` in setup.py
"jaxlib ==0.10.*",
]
53 changes: 3 additions & 50 deletions sphericart-jax/python/sphericart/jax/__init__.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,22 @@
import ctypes
import glob
import os
import re
import sys
from collections import namedtuple

import jax

from .spherical_harmonics import solid_harmonics, spherical_harmonics # noqa: F401


Version = namedtuple("Version", ["major", "minor", "patch"])


def parse_version(version_string):
match = re.match(r"(\d+)\.(\d+)\.(\d+).*", version_string)
if match:
return Version(*map(int, match.groups()))
else:
raise ValueError(f"Invalid version string format: {version_string}")


_HERE = os.path.realpath(os.path.dirname(__file__))


def _get_lib_dir():
jax_version = parse_version(jax.__version__)
expected_prefix = os.path.join(
_HERE, f"jax-{jax_version.major}.{jax_version.minor}.{jax_version.patch}"
)
if os.path.exists(expected_prefix):
return expected_prefix

# gather which jax version(s) the current install was built
# with to create the error message
existing_versions = []
for prefix in glob.glob(os.path.join(_HERE, "jax-*")):
existing_versions.append(os.path.basename(prefix)[4:])

if len(existing_versions) == 1:
raise ImportError(
f"Trying to load sphericart-jax with jax v{jax.__version__}, "
f"but it was compiled against jax v{existing_versions[0]}, which "
"is not ABI compatible"
)
else:
all_versions = ", ".join(map(lambda v: f"v{v}", existing_versions))
raise ImportError(
f"Trying to load sphericart-jax with jax v{jax.__version__}, "
f"we found builds for jax {all_versions}; which are not ABI compatible.\n"
"You can try to re-install from source with "
"`pip install sphericart-jax --no-binary=sphericart-jax`"
)


_LIB_DIR = _get_lib_dir()


def _lib_path(name: str) -> str:
if sys.platform.startswith("darwin"):
path = os.path.join(_LIB_DIR, "lib", f"lib{name}.dylib")
path = os.path.join(_HERE, "lib", f"lib{name}.dylib")
elif sys.platform.startswith("linux"):
path = os.path.join(_LIB_DIR, "lib", f"lib{name}.so")
path = os.path.join(_HERE, "lib", f"lib{name}.so")
elif sys.platform.startswith("win"):
path = os.path.join(_LIB_DIR, "bin", f"{name}.dll")
path = os.path.join(_HERE, "bin", f"{name}.dll")
else:
raise ImportError("Unknown platform. Please edit this file")

Expand Down
28 changes: 9 additions & 19 deletions sphericart-jax/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,9 @@ class cmake_ext(build_ext):
"""Build the native libraries using cmake"""

def run(self):
import jax

jax_major, jax_minor, jax_patch, *_ = jax.__version__.split(".")

source_dir = ROOT
build_dir = os.path.join(ROOT, "build", "cmake-build")
install_dir = os.path.join(
os.path.realpath(self.build_lib),
f"sphericart/jax/jax-{jax_major}.{jax_minor}.{jax_patch}",
)
install_dir = os.path.join(os.path.realpath(self.build_lib), "sphericart/jax")

os.makedirs(build_dir, exist_ok=True)

Expand Down Expand Up @@ -100,17 +93,14 @@ def run(self):


if __name__ == "__main__":
try:
import jax

# if we have jax, we are building a wheel - requires specific jax version
jax_v_major, jax_v_minor, jax_v_patch, *_ = jax.__version__.split(".")
jax_version = f"== {jax_v_major}.{jax_v_minor}.{jax_v_patch}.*"
except ImportError:
# otherwise we are building a sdist
jax_version = ">=0.6.0"

install_requires = [f"jax {jax_version}"]
# The wheels are built against the oldest supported jaxlib, and the XLA FFI ABI is
# stable across versions, so the same wheel works with all the versions in this
# range. The upper bound should be updated after checking that new versions of JAX
# work, even if the wheel is built with the oldest version.
install_requires = [
"jax >=0.10,<0.13",
"jaxlib >=0.10,<0.13",
]

setup(
version=open(os.path.join(ROOT, "sphericart", "VERSION")).readline().strip(),
Expand Down
Loading
Loading