diff --git a/.github/workflows/build-jax-wheels.yml b/.github/workflows/build-jax-wheels.yml index e94362384..8621f8b83 100644 --- a/.github/workflows/build-jax-wheels.yml +++ b/.github/workflows/build-jax-wheels.yml @@ -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} \ @@ -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 }} diff --git a/scripts/create-jax-versions-range.py b/scripts/create-jax-versions-range.py deleted file mode 100644 index d615504c1..000000000 --- a/scripts/create-jax-versions-range.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 -""" -This script updates the `Requires-Dist` information in sphericart-jax wheel METADATA -to contain the range of compatible jax versions. It expects newline separated -`Requires-Dist: jax ==...` information (corresponding to wheels built against a single -jax version) and will print `Requires-Dist: jax >=$MIN_VERSION,<${MAX_VERSION+1}` on -the standard output. - -This output can the be used in the merged wheel containing the build against all jax -versions. -""" -import re -import sys - - -if __name__ == "__main__": - jax_versions_raw = sys.argv[1] - - jax_versions = [] - for version in jax_versions_raw.split("\n"): - if version.strip() == "": - continue - - match = re.match( - r"Requires-Dist: jax[ ]?==(\d+)\.(\d+)(?:\.(\d+))?(?:\.\*)?", version) - if match is None: - raise ValueError(f"unexpected Requires-Dist format: {version}") - - major, minor, patch, *_ = match.groups() - major = int(major) - minor = int(minor) - patch = int(patch) if patch is not None else 0 - - version = (major, minor, patch) - - if version in jax_versions: - raise ValueError(f"duplicate jax version: {version}") - - jax_versions.append(version) - - jax_versions = list(sorted(jax_versions)) - - min_version = f"{jax_versions[0][0]}.{jax_versions[0][1]}.{jax_versions[0][2]}" - max_version = f"{jax_versions[-1][0]}.{jax_versions[-1][1]}.{jax_versions[-1][2] + 1}" - - print(f"Requires-Dist: jax >={min_version},<{max_version}") diff --git a/sphericart-jax/MANIFEST.in b/sphericart-jax/MANIFEST.in index 03d9e9b42..f6b3e3178 100644 --- a/sphericart-jax/MANIFEST.in +++ b/sphericart-jax/MANIFEST.in @@ -9,3 +9,5 @@ recursive-include include * include pyproject.toml include README.md + +recursive-include build-backend *.py diff --git a/sphericart-jax/build-backend/backend.py b/sphericart-jax/build-backend/backend.py index bd4b5a8f6..92367ad27 100644 --- a/sphericart-jax/build-backend/backend.py +++ b/sphericart-jax/build-backend/backend.py @@ -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 # # ==================================================================================== # @@ -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.*", + ] diff --git a/sphericart-jax/python/sphericart/jax/__init__.py b/sphericart-jax/python/sphericart/jax/__init__.py index f7e9560fd..4b019b297 100644 --- a/sphericart-jax/python/sphericart/jax/__init__.py +++ b/sphericart-jax/python/sphericart/jax/__init__.py @@ -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") diff --git a/sphericart-jax/setup.py b/sphericart-jax/setup.py index dde870e33..77da0a6c3 100644 --- a/sphericart-jax/setup.py +++ b/sphericart-jax/setup.py @@ -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) @@ -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(), diff --git a/tox.ini b/tox.ini index d35597db8..6eda8ccfa 100644 --- a/tox.ini +++ b/tox.ini @@ -113,7 +113,7 @@ deps = absl-py # jax uses this package but not installed by jax[cpu] pytest equinox - jax[cpu]==0.6.2 + jax[cpu]==0.10.0 allowlist_externals = bash