From: Nicolas Patry Date: Fri, 25 Aug 2023 07:26:20 +0000 (+0200) Subject: Trying out maturin. (#344) X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=73db0c8592f1a2380cfe5c4d7ab3de4e700df69a;p=safetensors.git Trying out maturin. (#344) * Trying out maturin. * Yaml. * We don't need lock right now. * Dont skip. * Working dir. * Default working dir. * Wroking dir here ? * Fix ls * Maturin makes things simpler. * Clippy. * New version number * Update conda build. * Conda. * Conda. * Drop 3.7, add 3.11 * Linux update 3.11 * Version * Done. * Remove afl * Put rust code also in dev mode. * Pre-release requires explicit everywhere. * Remove manual sdist * Remove custom builds * Requires lock * Fix sdist. * Sed. --- diff --git a/.github/conda/bld.bat b/.github/conda/bld.bat index 6f70044..57bd809 100644 --- a/.github/conda/bld.bat +++ b/.github/conda/bld.bat @@ -1,2 +1,2 @@ cd bindings\python -%PYTHON% setup.py install --prefix=%PREFIX% +%PYTHON% -m pip install . --prefix=%PREFIX% diff --git a/.github/conda/build.sh b/.github/conda/build.sh index 76f2cf3..df4e908 100644 --- a/.github/conda/build.sh +++ b/.github/conda/build.sh @@ -1,2 +1,2 @@ cd bindings/python -$PYTHON setup.py install --prefix=$PREFIX +$PYTHON -m pip install . --prefix=$PREFIX diff --git a/.github/conda/meta.yaml b/.github/conda/meta.yaml index 644ec17..7d46895 100644 --- a/.github/conda/meta.yaml +++ b/.github/conda/meta.yaml @@ -13,6 +13,7 @@ requirements: - python x.x - setuptools - setuptools-rust + - maturin run: - python x.x diff --git a/.github/workflows/python-release-conda.yml b/.github/workflows/python-release-conda.yml index ddb68aa..2c81be9 100644 --- a/.github/workflows/python-release-conda.yml +++ b/.github/workflows/python-release-conda.yml @@ -15,7 +15,7 @@ jobs: matrix: os: [windows-latest, macos-latest] # 3.11 not available on Conda yet. - python: ["3.7", "3.8", "3.9", "3.10"] + python: ["3.8", "3.9", "3.10", "3.11"] steps: - name: Checkout repository @@ -45,7 +45,7 @@ jobs: - name: Extract version shell: bash -l {0} working-directory: ./bindings/python - run: echo "SAFETENSORS_VERSION=`python setup.py --version`" >> $GITHUB_ENV + run: echo "SAFETENSORS_VERSION=`grep -m 1 version Cargo.toml | grep -e '".*"' -o | tr -d '"' | sed s/-/./ `" >> $GITHUB_ENV - name: Build conda packages shell: bash -l {0} @@ -66,14 +66,16 @@ jobs: strategy: fail-fast: false matrix: - python: [37, 38, 39] + python: [38, 39, 310, 311] include: - - python: 37 - checksum: a1a7285dea0edc430b2bc7951d89bb30a2a1b32026d2a7b02aacaaa95cf69c7c - python: 38 - checksum: 935d72deb16e42739d69644977290395561b7a6db059b316958d97939e9bdf3d + checksum: e2a4438671e0e42c5bba14cb51de6ce9763938184d6ca2967340bbe972bbe7e6 - python: 39 - checksum: 1ea2f885b4dbc3098662845560bc64271eb17085387a70c2ba3f29fff6f8d52f + checksum: 9829d95f639bd0053b2ed06d1204e60644617bf37dd5cc57523732e0e8d64516 + - python: 310 + checksum: ea5e6e8a3d5a0247b9df85382d27220fac8e59b5778fd313c5913879cd9baafc + - python: 311 + checksum: 634d76df5e489c44ade4085552b97bebc786d49245ed1a830022b0b406de5817 steps: - name: Checkout repository @@ -82,7 +84,7 @@ jobs: - name: Install miniconda run: | yum install -y wget openssl-devel - export FILENAME=Miniconda3-py${{ matrix.python }}_4.10.3-Linux-x86_64.sh + export FILENAME=Miniconda3-py${{ matrix.python }}_23.5.2-0-Linux-x86_64.sh wget https://repo.anaconda.com/miniconda/$FILENAME sha256sum $FILENAME | awk '$1=="${{ matrix.checksum}}"{print"good to go"}' bash $FILENAME -b -p $HOME/miniconda @@ -115,7 +117,7 @@ jobs: working-directory: ./bindings/python run: | source $HOME/miniconda/bin/activate - echo "SAFETENSORS_VERSION=`python setup.py --version`" >> $GITHUB_ENV + echo "SAFETENSORS_VERSION=`grep -m 1 version Cargo.toml | grep -e '".*"' -o | tr -d '"' | sed s/-/./ `" >> $GITHUB_ENV - name: Build conda packages shell: bash -l {0} diff --git a/.github/workflows/python-release-extra.yml b/.github/workflows/python-release-extra.yml deleted file mode 100644 index 06dc253..0000000 --- a/.github/workflows/python-release-extra.yml +++ /dev/null @@ -1,120 +0,0 @@ -name: Python Release extra - -on: - push: - tags: - - v* - -env: - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - AWS_DEFAULT_REGION: us-east-1 - PYPI_TOKEN: ${{ secrets.PYPI_TOKEN_DIST }} - DIST_DIR: '${{ github.sha }}_extra' - -jobs: - - create_wheels_manylinux_2014_ppc64le: - runs-on: ubuntu-latest - name: Create wheels for manylinux2014 - PowerPC - steps: - - uses: actions/checkout@v3 - - - name: Upgrade libssl - run: sudo apt-get install -y libssl-dev - - - name: Pull images - run: | - docker pull multiarch/qemu-user-static - docker pull quay.io/pypa/manylinux2014_ppc64le:latest - - - name: Install QEMU - run: | - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - - - name: Build and audit wheels - working-directory: ./bindings/python - run: | - docker run -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION -e DIST_DIR \ - --rm -v `pwd`/../..:/io quay.io/pypa/manylinux2014_ppc64le \ - /bin/bash -c "yum install -y openssl-devel && cd /io/bindings/python; sh build-wheels.sh" - - create_wheels_manylinux_2014_aarch64: - runs-on: ubuntu-latest - name: Create wheels for manylinux2014 - Aarch64 - steps: - - uses: actions/checkout@v2 - - - name: Upgrade libssl - run: sudo apt-get install -y libssl-dev - - - name: Pull images - run: | - docker pull multiarch/qemu-user-static - docker pull quay.io/pypa/manylinux2014_aarch64:latest - - - name: Install QEMU - run: | - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - - - name: Build and audit wheels - working-directory: ./bindings/python - run: | - docker run -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION -e DIST_DIR \ - --rm -v `pwd`/../..:/io quay.io/pypa/manylinux2014_aarch64 \ - /bin/bash -c "yum install -y openssl-devel && cd /io/bindings/python; sh build-wheels.sh" - - create_wheels_manylinux_2014_x390x: - runs-on: ubuntu-latest - name: Create wheels for manylinux2014 - S390X - steps: - - uses: actions/checkout@v2 - - - name: Upgrade libssl - run: sudo apt-get install -y libssl-dev - - - name: Pull images - run: | - docker pull multiarch/qemu-user-static - docker pull quay.io/pypa/manylinux2014_s390x:latest - - - name: Install QEMU - run: | - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - - - name: Build and audit wheels - working-directory: ./bindings/python - run: | - docker run -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION -e DIST_DIR \ - --rm -v `pwd`/../..:/io quay.io/pypa/manylinux2014_s390x \ - /bin/bash -c "yum install -y openssl-devel && cd /io/bindings/python; sh build-wheels.sh" - - upload_package: - name: Upload package to PyPi - runs-on: ubuntu-latest - needs: - - create_wheels_manylinux_2014_ppc64le - - create_wheels_manylinux_2014_aarch64 - - create_wheels_manylinux_2014_x390x - - steps: - - uses: actions/checkout@v2 - - - name: Install Python - uses: actions/setup-python@v4 - - - name: Retrieve all wheels - shell: bash - run: | - pip install awscli - aws s3 sync "s3://safetensors-releases/python/$DIST_DIR" ./bindings/python/dist - - - name: Install dependencies - run: | - pip install setuptools wheel setuptools-rust - - - name: Upload to PyPi - working-directory: ./bindings/python - run: | - pip install twine - twine upload dist/* -u __token__ -p "$PYPI_TOKEN" diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index 31fd801..bf9ca1e 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -21,109 +21,103 @@ jobs: run: cat Cargo.lock working-directory: ./bindings/python - create_wheels_manylinux: - runs-on: ubuntu-latest - needs: [lock_exists] - name: Create wheels for manylinux2014 - container: quay.io/pypa/manylinux2014_x86_64 - steps: - - uses: actions/checkout@v3 - - - name: Install dependencies - run: yum install -y openssl-devel - - - name: Build and audit wheels - working-directory: ./bindings/python - run: sh build-wheels.sh - - create_wheels_windows: - name: Windows - runs-on: windows-latest + build: + name: build on ${{ matrix.platform || matrix.os }} (${{ matrix.target }} - ${{ matrix.manylinux || 'auto' }}) + # only run on push to main and on release needs: [lock_exists] + if: startsWith(github.ref, 'refs/tags/') || github.ref == 'refs/heads/main' || contains(github.event.pull_request.labels.*.name, 'Full Build') strategy: + fail-fast: false matrix: - python: ["3.7", "3.8", "3.9", "3.10", "3.11"] - bits: ["32", "64"] + os: [ubuntu, macos, windows] + target: [x86_64, aarch64] + manylinux: [auto] + include: + - os: ubuntu + platform: linux + - os: windows + ls: dir + interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 pypy3.8 pypy3.9 pypy3.10 + - os: windows + ls: dir + target: i686 + python-architecture: x86 + interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 + - os: windows + ls: dir + target: aarch64 + interpreter: 3.11 3.12 + - os: macos + target: aarch64 + interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 pypy3.8 pypy3.9 pypy3.10 + - os: ubuntu + platform: linux + target: i686 + - os: ubuntu + platform: linux + target: aarch64 + + - os: ubuntu + platform: linux + target: armv7 + interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 + # musllinux + - os: ubuntu + platform: linux + target: x86_64 + manylinux: musllinux_1_1 + - os: ubuntu + platform: linux + target: aarch64 + manylinux: musllinux_1_1 + - os: ubuntu + platform: linux + target: ppc64le + interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 + - os: ubuntu + platform: linux + target: s390x + interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 + # exclude: + # # Optimized PGO builds for x86_64 manylinux and windows follow a different matrix, + # # maybe in future maturin-action can support this automatically + # - os: ubuntu + # target: x86_64 + # manylinux: auto + # - os: windows + # target: x86_64 + # # Windows on arm64 only supports Python 3.11+ + # - os: windows + # target: aarch64 + + runs-on: ${{ matrix.os }}-latest steps: - - name: Checkout repository - uses: actions/checkout@v2 - - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: ${{ matrix.bits == '32' && 'stable-i686-pc-windows-msvc' || 'stable-x86_64-pc-windows-msvc' }} - override: true - - - name: Override toolchain - shell: bash - working-directory: ./bindings/python - run: echo ${{ matrix.bits == '32' && 'stable-i686-pc-windows-msvc' || 'stable-x86_64-pc-windows-msvc' }} > rust-toolchain + - uses: actions/checkout@v3 - - name: Install Python + - name: set up python uses: actions/setup-python@v4 with: - python-version: ${{ matrix.python }} - architecture: ${{ matrix.bits == '32' && 'x86' || 'x64' }} - - - name: Install dependencies - run: | - # On old versions of python there is an old version of setuptools already installed - pip install setuptools wheel setuptools-rust --ignore-installed --force-reinstall - - - name: Build wheel - working-directory: ./bindings/python - run: python setup.py bdist_wheel - - - name: Upload wheels - shell: bash - run: | - pip install awscli - aws s3 sync --exact-timestamps ./bindings/python/dist "s3://safetensors-releases/python/$DIST_DIR" + python-version: '3.11' + architecture: ${{ matrix.python-architecture || 'x64' }} - create_wheels_windows: - name: Windows - runs-on: windows-latest - needs: [lock_exists] - strategy: - matrix: - python: ["3.7", "3.8", "3.9", "3.10", "3.11"] - bits: ["32", "64"] - steps: - - name: Checkout repository - uses: actions/checkout@v2 + - run: pip install -U twine - - name: Install Rust 32bits - if: ${{ matrix.os == '32' }} - uses: actions-rs/toolchain@v1 + - name: build wheels + uses: PyO3/maturin-action@v1 with: - toolchain: stable-i686-pc-windows-msvc - override: true - - - name: Override toolchain - if: ${{ matrix.os == '32' }} - shell: bash + target: ${{ matrix.target }} + working-directory: ./bindings/python + manylinux: ${{ matrix.manylinux || 'auto' }} + container: ${{ matrix.container }} + args: --release --out dist --interpreter ${{ matrix.interpreter || '3.7 3.8 3.9 3.10 3.11 3.12 pypy3.7 pypy3.8 pypy3.9 pypy3.10' }} ${{ matrix.extra-build-args }} + rust-toolchain: stable + docker-options: -e CI + + - run: ${{ matrix.ls || 'ls -lh' }} dist/ working-directory: ./bindings/python - run: echo "stable-i686-pc-windows-msvc" > rust-toolchain - - - name: Install Rust 64bits - if: ${{ matrix.os == '64' }} - uses: actions-rs/toolchain@v1 - - - - name: Install Python - uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.python }} - architecture: x86 - - - name: Install dependencies - run: | - # On old versions of python there is an old version of setuptools already installed - pip install setuptools wheel setuptools-rust --ignore-installed --force-reinstall - - name: Build wheel + - run: twine check --strict dist/* working-directory: ./bindings/python - run: python setup.py bdist_wheel - name: Upload wheels shell: bash @@ -131,96 +125,23 @@ jobs: pip install awscli aws s3 sync --exact-timestamps ./bindings/python/dist "s3://safetensors-releases/python/$DIST_DIR" - create_wheels_macos_conda: - name: MacOS - Conda - runs-on: ${{ matrix.os }} + # - uses: actions/upload-artifact@v3 + # working-directory: ./bindings/python/ + # with: + # name: pypi_files + # path: dist + build-sdist: + name: build sdist needs: [lock_exists] - strategy: - matrix: - os: [macos-latest] - # 3.11 not available on Conda yet. - python: ["3.7", "3.8", "3.9", "3.10"] - - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - name: Install miniconda - uses: conda-incubator/setup-miniconda@v2 - with: - auto-update-conda: true - python-version: ${{ matrix.python }} - - - name: Conda info - shell: bash -l {0} - run: conda info - - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - - - name: Setup conda env - shell: bash -l {0} - run: | - conda install setuptools-rust - conda install -c defaults anaconda-client conda-build - - - name: Extract version - shell: bash -l {0} - working-directory: ./bindings/python - run: echo "SAFETENSORS_VERSION=`python setup.py --version`" >> $GITHUB_ENV - - - name: Build conda packages - shell: bash -l {0} - working-directory: ./bindings/python - run: | - MACOSX_DEPLOYMENT_TARGET=10.11 python setup.py bdist_wheel - - - name: Upload wheels - shell: bash - run: | - pip install awscli - aws s3 sync --exact-timestamps ./bindings/python/dist "s3://safetensors-releases/python/$DIST_DIR" - - create_wheels_macos: - name: MacOS - runs-on: ${{ matrix.os.os }} - needs: [lock_exists] - strategy: - matrix: - python: ["3.7", "3.8", "3.9", "3.10", "3.11"] - # os: [{os: "macos-11", target: "11.0"}, {os: "macos-12"}, {os: "macos-13"}, {os: "macos-13", target: "14.0"}] - os: [{os: "macos-11", target: "11.0"}, {os: "macos-12"}, {os: "macos-13"}] + runs-on: ubuntu-latest steps: - - name: Checkout repository - uses: actions/checkout@v2 - - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - - - name: Install Python - uses: actions/setup-python@v4 + - uses: actions/checkout@v3 + - uses: PyO3/maturin-action@v1 with: - python-version: ${{ matrix.python }} - - - name: Install dependencies - run: | - # On old versions of python there is an old version of setuptools already installed - pip install setuptools wheel setuptools-rust --ignore-installed --force-reinstall - - - name: Override target - if: ${{ matrix.os.target }} - working-directory: ./bindings/python - run: echo "MACOSX_DEPLOYMENT_TARGET=${{ matrix.os.target }}" >> $GITHUB_ENV - - - name: Build wheel - working-directory: ./bindings/python - run: echo $MACOX_DEPLOYMENT_TARGET && python setup.py bdist_wheel - + working-directory: ./bindings/python + command: sdist + args: --out dist + rust-toolchain: stable - name: Upload wheels shell: bash run: | @@ -228,45 +149,10 @@ jobs: aws s3 sync --exact-timestamps ./bindings/python/dist "s3://safetensors-releases/python/$DIST_DIR" - create_wheels_macos_arm64: - name: MacOS M1 - runs-on: macos-arm64 - needs: [lock_exists] - strategy: - matrix: - python: ["3.8.16", "3.9.13", "3.10.6", "3.11.0"] - # target: ["12.0", "13.0", "14.0"] - target: ["12.0", "13.0"] - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - - - name: Install Python - shell: bash - run: | - echo $HOME - export PYENV_ROOT="$HOME/.pyenv" - command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH" - eval "$(pyenv init -)" - pyenv shell ${{ matrix.python }} - which pyenv - which python - pip install -U setuptools wheel setuptools-rust awscli - cd ./bindings/python - MACOSX_DEPLOYMENT_TARGET=${{ matrix.target }} python setup.py bdist_wheel - cd ../../ - aws s3 sync --exact-timestamps ./bindings/python/dist "s3://safetensors-releases/python/$DIST_DIR" - - Upload_package: + upload_package: name: Upload package to PyPi runs-on: ubuntu-latest - needs: [create_wheels_manylinux, create_wheels_windows, create_wheels_macos, create_wheels_macos_arm64, create_wheels_macos_conda] + needs: [build, build-sdist] steps: - uses: actions/checkout@v3 @@ -283,14 +169,6 @@ jobs: pip install awscli aws s3 sync "s3://safetensors-releases/python/$DIST_DIR" ./bindings/python/dist - - name: Install dependencies - run: | - pip install setuptools wheel setuptools-rust - - - name: Create source distribution - working-directory: ./bindings/python - run: sh build-sdist.sh - - name: Upload to PyPi working-directory: ./bindings/python run: | diff --git a/bindings/python/Cargo.toml b/bindings/python/Cargo.toml index 212ddaf..22bd82c 100644 --- a/bindings/python/Cargo.toml +++ b/bindings/python/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "safetensors-python" -version = "0.3.4" +version = "0.3.4-dev.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -9,10 +9,10 @@ name = "safetensors_rust" crate-type = ["cdylib"] [dependencies] -pyo3 = { version = "0.18.1", features = ["extension-module"] } +pyo3 = { version = "0.19.2", features = ["extension-module"] } memmap2 = "0.5" serde_json = "1.0" [dependencies.safetensors] -version = "*" +version = "0.3.4-dev.0" path = "../../safetensors" diff --git a/bindings/python/build-sdist.sh b/bindings/python/build-sdist.sh deleted file mode 100755 index c8a29d8..0000000 --- a/bindings/python/build-sdist.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -set -ex - -# Create a symlink for safetensors-lib -ln -sf ../../safetensors safetensors-lib -# Modify cargo.toml to include this symlink -sed -i 's/\.\.\/\.\.\/safetensors/\.\/safetensors-lib/' Cargo.toml -# Build the source distribution -python setup.py sdist diff --git a/bindings/python/build-wheels.sh b/bindings/python/build-wheels.sh deleted file mode 100755 index c9d7bf8..0000000 --- a/bindings/python/build-wheels.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash -set -ex - -if ! command -v cargo &> /dev/null -then - curl https://sh.rustup.rs -sSf | sh -s -- -y -fi - -export PATH="$HOME/.cargo/bin:$PATH" -# https://users.rust-lang.org/t/cargo-uses-too-much-memory-being-run-in-qemu/76531 -echo -e "[net]\ngit-fetch-with-cli = true" > "$HOME/.cargo/config" - -for PYBIN in /opt/python/cp{37,38,39,310,311}*/bin; do - export PYTHON_SYS_EXECUTABLE="$PYBIN/python" - - "${PYBIN}/pip" install -U setuptools-rust setuptools wheel - "${PYBIN}/python" setup.py bdist_wheel - rm -rf build/* -done - -for whl in ./dist/*.whl; do - auditwheel repair "$whl" -w dist/ -done - -# Keep only manylinux wheels -rm ./dist/*-linux_* - - -# Upload wheels -/opt/python/cp37-cp37m/bin/pip install -U awscli -/opt/python/cp37-cp37m/bin/python -m awscli s3 sync --exact-timestamps ./dist "s3://safetensors-releases/python/$DIST_DIR" diff --git a/bindings/python/py_src/safetensors/__init__.py b/bindings/python/py_src/safetensors/__init__.py index 50f5763..c9a5d2c 100644 --- a/bindings/python/py_src/safetensors/__init__.py +++ b/bindings/python/py_src/safetensors/__init__.py @@ -1,5 +1,9 @@ -__version__ = "0.3.4.dev0" - - # Re-export this -from ._safetensors_rust import SafetensorError, deserialize, safe_open, serialize, serialize_file # noqa: F401 +from ._safetensors_rust import ( # noqa: F401 + SafetensorError, + __version__, + deserialize, + safe_open, + serialize, + serialize_file, +) diff --git a/bindings/python/pyproject.toml b/bindings/python/pyproject.toml index 554dd83..4849af9 100644 --- a/bindings/python/pyproject.toml +++ b/bindings/python/pyproject.toml @@ -1,7 +1,101 @@ +[project] +name = 'safetensors' +requires-python = '>=3.7' +authors = [ + {name = 'Nicolas Patry', email = 'patry.nicolas@protonmail.com'} +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Developers", + "Intended Audience :: Education", + "Intended Audience :: Science/Research", + "License :: OSI Approved :: Apache Software License", + "Operating System :: OS Independent", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Topic :: Scientific/Engineering :: Artificial Intelligence", +] +dynamic = [ + 'description', + 'license', + 'readme', +] + +[project.urls] +Homepage = 'https://github.com/huggingface/safetensors' +Source = 'https://github.com/huggingface/safetensors' + +[project.optional-dependencies] +numpy = ["numpy>=1.21.6"] +torch = [ + "safetensors[numpy]", + "torch>=1.10", +] +tensorflow = [ + "safetensors[numpy]", + "tensorflow>=2.11.0", +] +# pinning tf version 2.11.0 for doc-builder +pinned-tf = [ + "safetensors[numpy]", + "tensorflow==2.11.0", +] +jax = [ + "safetensors[numpy]", + "flax>=0.6.3", + "jax>=0.3.25", + "jaxlib>=0.3.25", +] +paddlepaddle = [ + "safetensors[numpy]", + "paddlepaddle>=2.4.1", +] +quality = [ + "black==22.3", # after updating to black 2023, also update Python version in pyproject.toml to 3.7 + "click==8.0.4", + "isort>=5.5.4", + "flake8>=3.8.3", +] +testing = [ + "safetensors[numpy]", + "h5py>=3.7.0", + "huggingface_hub>=0.12.1", + "setuptools_rust>=1.5.2", + "pytest>=7.2.0", + "pytest-benchmark>=4.0.0", + # "python-afl>=0.7.3", + "hypothesis>=6.70.2", +] +all = [ + "safetensors[torch]", + "safetensors[numpy]", + "safetensors[pinned-tf]", + "safetensors[jax]", + "safetensors[paddlepaddle]", + "safetensors[quality]", + "safetensors[testing]", +] +dev = [ + "safetensors[all]", +] + + [build-system] -requires = ["setuptools", "wheel", "setuptools-rust"] -build-backend = "setuptools.build_meta" +requires = ["maturin>=1.0,<2.0"] +build-backend = "maturin" + +[tool.maturin] +python-source = "py_src" +module-name = "safetensors._safetensors_rust" +bindings = 'pyo3' +features = ["pyo3/extension-module"] [tool.black] line-length = 119 target-version = ['py35'] + +[tool.setuptools.dynamic] +readme = {file = ["README.rst"]} diff --git a/bindings/python/setup.py b/bindings/python/setup.py deleted file mode 100644 index 324632c..0000000 --- a/bindings/python/setup.py +++ /dev/null @@ -1,97 +0,0 @@ -import re - -from setuptools import setup -from setuptools_rust import Binding, RustExtension - - -# IMPORTANT: -# 1. all dependencies should be listed here with their version requirements if any -_deps = [ - "black==22.3", # after updating to black 2023, also update Python version in pyproject.toml to 3.7 - "click==8.0.4", - "flake8>=3.8.3", - "flax>=0.6.3", - "h5py>=3.7.0", - "huggingface_hub>=0.12.1", - "isort>=5.5.4", - "jax>=0.3.25", - "jaxlib>=0.3.25", - "numpy>=1.21.6", - "setuptools_rust>=1.5.2", - "pytest>=7.2.0", - "pytest-benchmark>=4.0.0", - "tensorflow>=2.11.0", - "torch>=1.10", - "paddlepaddle>=2.4.1", - "python-afl>=0.7.3", - "hypothesis>=6.70.2", -] - - -deps = {b: a for a, b in (re.findall(r"^(([^!=<>~ ]+)(?:[!=<>~ ].*)?$)", x)[0] for x in _deps)} - - -def deps_list(*pkgs): - return [deps[pkg] for pkg in pkgs] - - -extras = {} -extras["numpy"] = deps_list("numpy") -extras["torch"] = deps_list("torch") + extras["numpy"] -extras["tensorflow"] = deps_list("tensorflow") + extras["numpy"] -extras["jax"] = deps_list("jax", "flax", "jaxlib") + extras["numpy"] -extras["paddlepaddle"] = deps_list("paddlepaddle") + extras["numpy"] -extras["quality"] = deps_list("black", "isort", "flake8", "click") -extras["testing"] = ( - deps_list("setuptools_rust", "huggingface_hub", "pytest", "pytest-benchmark", "h5py") + extras["numpy"] -) -# pinning tf version 2.11.0 for doc-builder -extras["pinned-tf"] = ["tensorflow==2.11.0"] -extras["all"] = ( - extras["torch"] - + extras["numpy"] - + extras["pinned-tf"] - + extras["jax"] - + extras["paddlepaddle"] - + extras["quality"] - + extras["testing"] -) -extras["dev"] = extras["all"] - -with open("py_src/safetensors/__init__.py", "r") as f: - version = f.readline().split("=")[-1].strip().strip('"') - -setup( - name="safetensors", - version=version, - description="Fast and Safe Tensor serialization", - long_description=open("README.md", "r", encoding="utf-8").read(), - long_description_content_type="text/markdown", - keywords="", - author="", - author_email="", - url="https://github.com/huggingface/safetensors", - license="Apache License 2.0", - rust_extensions=[RustExtension("safetensors._safetensors_rust", binding=Binding.PyO3, debug=False)], - extras_require=extras, - classifiers=[ - "Development Status :: 5 - Production/Stable", - "Intended Audience :: Developers", - "Intended Audience :: Education", - "Intended Audience :: Science/Research", - "License :: OSI Approved :: Apache Software License", - "Operating System :: OS Independent", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Topic :: Scientific/Engineering :: Artificial Intelligence", - ], - package_dir={"": "py_src"}, - packages=[ - "safetensors", - ], - package_data={}, - zip_safe=False, -) diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index f0d9dc6..ce66fba 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -95,7 +95,7 @@ fn serialize<'b>( metadata: Option>, ) -> PyResult<&'b PyBytes> { let tensors = prepare(tensor_dict)?; - let metadata_map = metadata.map(|data| HashMap::from_iter(data.into_iter())); + let metadata_map = metadata.map(HashMap::from_iter); let out = safetensors::tensor::serialize(&tensors, &metadata_map) .map_err(|e| SafetensorError::new_err(format!("Error while serializing: {e:?}")))?; let pybytes = PyBytes::new(py, &out); @@ -590,7 +590,6 @@ impl Open { /// The device on which you want the tensors. #[pyclass] #[allow(non_camel_case_types)] -#[pyo3(text_signature = "(self, filename, framework, device=\"cpu\")")] struct safe_open { inner: Option, } @@ -608,6 +607,7 @@ impl safe_open { #[pymethods] impl safe_open { #[new] + #[pyo3(text_signature = "(self, filename, framework, device=\"cpu\")")] fn new(filename: PathBuf, framework: Framework, device: Option) -> PyResult { let inner = Some(Open::new(filename, framework, device)?); Ok(Self { inner }) @@ -1000,6 +1000,7 @@ fn _safetensors_rust(py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(deserialize, m)?)?; m.add_class::()?; m.add("SafetensorError", py.get_type::())?; + m.add("__version__", env!("CARGO_PKG_VERSION"))?; Ok(()) } diff --git a/safetensors/Cargo.toml b/safetensors/Cargo.toml index 605a7e3..879ef86 100644 --- a/safetensors/Cargo.toml +++ b/safetensors/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "safetensors" -version = "0.3.4" +version = "0.3.4-dev.0" edition = "2021" homepage = "https://github.com/huggingface/safetensors" repository = "https://github.com/huggingface/safetensors" diff --git a/safetensors/src/tensor.rs b/safetensors/src/tensor.rs index 2c1925e..464d09a 100644 --- a/safetensors/src/tensor.rs +++ b/safetensors/src/tensor.rs @@ -726,7 +726,7 @@ mod tests { let mut start = 0; let tensors: Vec = dtypes .iter() - .zip(shapes.into_iter()) + .zip(shapes) .map(|(dtype, shape)| { // This cannot overflow because the size of // the vector and elements are so small.