Skip to content

Commit

Permalink
Merge pull request #1037 from Nagico2/feat/win-arm-build
Browse files Browse the repository at this point in the history
Add support for windows arm64
  • Loading branch information
taku910 authored Aug 17, 2024
2 parents e29db12 + 6b1c886 commit e963f99
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/wheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
cmake --build ${{github.workspace}}/build_win32 --config Release --target install --parallel 8
cmake -A x64 -B ${{github.workspace}}/build_amd64 -DSPM_ENABLE_SHARED=OFF -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/build/root_amd64
cmake --build ${{github.workspace}}/build_amd64 --config Release --target install --parallel 8
cmake -A arm64 -B ${{github.workspace}}/build_arm64 -DSPM_ENABLE_SHARED=OFF -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/build/root_arm64
cmake --build ${{github.workspace}}/build_arm64 --config Release --target install --parallel 8
- name: Build for Mac
if: runner.os == 'macOS'
Expand All @@ -66,6 +68,7 @@ jobs:
env:
CIBW_ARCHS_LINUX: auto aarch64
CIBW_ARCHS_MACOS: x86_64 universal2 arm64
CIBW_ARCHS_WINDOWS: auto ARM64
CIBW_SKIP: "pp* *-musllinux_*"
CIBW_BUILD_VERBOSITY: 1

Expand Down
16 changes: 15 additions & 1 deletion python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Python wrapper for SentencePiece. This API will offer the encoding, decoding and training of Sentencepiece.

## Build and Install SentencePiece
For Linux (x64/i686), macOS, and Windows(win32/x64) environment, you can simply use pip command to install SentencePiece python module.
For Linux (x64/i686), macOS, and Windows(win32/x64/arm64) environment, you can simply use pip command to install SentencePiece python module.

```
% pip install sentencepiece
Expand All @@ -27,6 +27,20 @@ If you don’t have write permission to the global site-packages directory or do
% python setup.py install --user
```

For Windows users who want to build from source, you can build and install the Python wrapper using Visual Studio. First, you need to install the `pwsh.exe` (Powershell 7). Use `winget install --id Microsoft.Powershell --source winget` to install directly. Then open the `Developer PowerShell for VS 2022`, and execute the following commands.
```
git clone https:/google/sentencepiece.git
cd sentencepiece
mkdir build
cd build
cmake .. -DSPM_ENABLE_SHARED=OFF -DCMAKE_INSTALL_PREFIX=".\root"
cmake --build . --config Release --target install
cd ../python
pip install wheel
python setup.py bdist_wheel
Get-ChildItem .\dist\sentencepiece*.whl | ForEach-Object { pip install $_.FullName }
```

## Usage

See [this google colab page](https:/google/sentencepiece/blob/master/python/sentencepiece_python_module_example.ipynb) to run sentencepiece interactively.
Expand Down
17 changes: 15 additions & 2 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import string
import subprocess
import sys
import platform
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.command.build_py import build_py as _build_py
Expand Down Expand Up @@ -103,11 +104,21 @@ def build_extension(self, ext):
_build_ext.build_extension(self, ext)


if os.name == 'nt':
# Must pre-install sentencepice into build directory.
def get_win_arch():
arch = 'win32'
if sys.maxsize > 2**32:
arch = 'amd64'
if 'arm' in platform.machine().lower():
arch = 'arm64'
if os.getenv('PYTHON_ARCH', '') == 'ARM64':
# Special check for arm64 under ciwheelbuild, see https:/pypa/cibuildwheel/issues/1942
arch = 'arm64'
return arch


if os.name == 'nt':
# Must pre-install sentencepice into build directory.
arch = get_win_arch()
if os.path.exists('..\\build\\root_{}\\lib'.format(arch)):
cflags = ['/std:c++17', '/I..\\build\\root_{}\\include'.format(arch)]
libs = [
Expand All @@ -125,6 +136,8 @@ def build_extension(self, ext):
cmake_arch = 'Win32'
if arch == 'amd64':
cmake_arch = 'x64'
elif arch == "arm64":
cmake_arch = "ARM64"
subprocess.check_call([
'cmake',
'sentencepiece',
Expand Down

0 comments on commit e963f99

Please sign in to comment.