Skip to content

Commit

Permalink
Publish pre-built jni libs for windows and osx (#1056)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Jun 25, 2024
1 parent 5cce159 commit 2f8c489
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 22 deletions.
35 changes: 31 additions & 4 deletions .github/workflows/linux-jni.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: linux-jni
on:
push:
branches:
- linux-jni
- jni
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'
workflow_dispatch:
Expand Down Expand Up @@ -33,6 +33,30 @@ jobs:
ls -lh
du -h -d1 .
- name: Build jar
shell: bash
run: |
SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
cd sherpa-onnx/java-api
make
ls -lh build/
cp build/sherpa-onnx.jar ../../sherpa-onnx-$SHERPA_ONNX_VERSION.jar
cd ../..
ls -lh *.jar
- uses: actions/upload-artifact@v4
with:
name: release-jni-linux-jar
path: ./*.jar

- name: Release jar
if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
uses: svenstaro/upload-release-action@v2
with:
file_glob: true
overwrite: true
file: ./*.jar

- name: Build sherpa-onnx
uses: addnab/docker-run-action@v3
with:
Expand Down Expand Up @@ -78,6 +102,8 @@ jobs:
ls -lh lib
ls -lh bin
rm -rf ./install/pkgconfig
rm -rf ./install/share
- name: Display dependencies of sherpa-onnx for linux
shell: bash
Expand All @@ -99,9 +125,8 @@ jobs:
- uses: actions/upload-artifact@v4
with:
name: release-jni
path: install/*

name: release-jni-linux
path: build/install/*

- name: Copy files
shell: bash
Expand Down Expand Up @@ -142,6 +167,7 @@ jobs:
mkdir -p jni
cp -v ../sherpa-onnx-*.tar.bz2 ./jni
cp -v ../*.jar ./jni
git status
git lfs track "*.bz2"
Expand All @@ -159,3 +185,4 @@ jobs:
file_glob: true
overwrite: true
file: sherpa-onnx-*.tar.bz2

140 changes: 140 additions & 0 deletions .github/workflows/macos-jni.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
name: macos-jni

on:
push:
branches:
- jni
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'

workflow_dispatch:

concurrency:
group: macos-jni-${{ github.ref }}
cancel-in-progress: true

jobs:
macos_jni:
runs-on: ${{ matrix.os }}
name: ${{ matrix.arch }}
strategy:
fail-fast: false
matrix:
os: [macos-latest]
arch: [arm64, x86_64]

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-java@v4
with:
distribution: 'temurin' # See 'Supported distributions' for available options
java-version: '21'

- name: ccache
uses: hendrikmuhs/[email protected]
with:
key: ${{ matrix.os }}-${{ matrix.arch }}

- name: Configure CMake
shell: bash
run: |
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
cmake --version
mkdir build
cd build
arch=${{ matrix.arch }}
cmake \
-D BUILD_SHARED_LIBS=ON \
-D CMAKE_BUILD_TYPE=Release \
-DCMAKE_OSX_ARCHITECTURES=$arch \
-DSHERPA_ONNX_ENABLE_JNI=ON \
-DCMAKE_INSTALL_PREFIX=./install \
..
- name: Build sherpa-onnx for macos
shell: bash
run: |
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
cd build
make -j2
make install
ls -lh lib
ls -lh bin
file ./bin/sherpa-onnx
rm -rf ./install/pkgconfig
rm -rf ./install/share
- uses: actions/upload-artifact@v4
with:
name: release-jni-macos-${{ matrix.arch }}
path: build/install/*

- name: Copy files
shell: bash
run: |
du -h -d1 .
SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
arch=${{ matrix.arch }}
dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-osx-$arch-jni
mkdir -p $dst
cp -a build/install/bin $dst/
cp -a build/install/lib $dst/
cp -a build/install/include $dst/
brew install tree
tree $dst
tar cjvf ${dst}.tar.bz2 $dst
du -h -d1 .
- name: Publish to huggingface
if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
uses: nick-fields/retry@v3
with:
max_attempts: 20
timeout_seconds: 200
shell: bash
command: |
git config --global user.email "[email protected]"
git config --global user.name "Fangjun Kuang"
rm -rf huggingface
export GIT_CLONE_PROTECTION_ACTIVE=false
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface
cd huggingface
mkdir -p jni
cp -v ../sherpa-onnx-*.tar.bz2 ./jni
git status
git lfs track "*.bz2"
git add .
git commit -m "add more files"
git push https://csukuangfj:[email protected]/csukuangfj/sherpa-onnx-libs main
- name: Release pre-compiled binaries and libs for linux x64
if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
uses: svenstaro/upload-release-action@v2
with:
file_glob: true
overwrite: true
file: sherpa-onnx-*.tar.bz2
110 changes: 110 additions & 0 deletions .github/workflows/windows-x64-jni.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
name: windows-x64-jni

on:
push:
branches:
- jni
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'

workflow_dispatch:

concurrency:
group: windows-x64-jni-${{ github.ref }}
cancel-in-progress: true

jobs:
windows_x64_jni:
name: windows x64 jni
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [windows-latest]

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-java@v4
with:
distribution: 'temurin' # See 'Supported distributions' for available options
java-version: '21'

- name: Configure CMake
shell: bash
run: |
mkdir build
cd build
cmake -A x64 -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=ON -DSHERPA_ONNX_ENABLE_JNI=ON -DCMAKE_INSTALL_PREFIX=./install ..
- name: Build sherpa-onnx for windows
shell: bash
run: |
cd build
cmake --build . --config Release -- -m:2
cmake --build . --config Release --target install -- -m:2
ls -lh ./bin/Release/sherpa-onnx.exe
rm -rf install/share
rm -rf install/lib/pkgconfig
- uses: actions/upload-artifact@v4
with:
name: release-jni-windows-x64
path: build/install/*

- name: Copy files
shell: bash
run: |
SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-win-x64-jni
mkdir -p $dst
cp -a build/install/bin $dst/
cp -a build/install/lib $dst/
cp -a build/install/include $dst/
tar cjvf ${dst}.tar.bz2 $dst
# https://huggingface.co/docs/hub/spaces-github-actions
- name: Publish to huggingface
if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
uses: nick-fields/retry@v3
with:
max_attempts: 20
timeout_seconds: 200
shell: bash
command: |
git config --global user.email "[email protected]"
git config --global user.name "Fangjun Kuang"
rm -rf huggingface
export GIT_CLONE_PROTECTION_ACTIVE=false
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface
cd huggingface
mkdir -p jni
cp -v ../sherpa-onnx-*.tar.bz2 ./jni
git status
git lfs track "*.bz2"
git add .
git commit -m "upload sherpa-onnx-${SHERPA_ONNX_VERSION}"
git push https://csukuangfj:[email protected]/csukuangfj/sherpa-onnx-libs main
- name: Release pre-compiled binaries and libs for Windows x64
if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
uses: svenstaro/upload-release-action@v2
with:
file_glob: true
overwrite: true
file: sherpa-onnx-*.tar.bz2
35 changes: 17 additions & 18 deletions sherpa-onnx/jni/offline-tts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ Java_com_k2fsa_sherpa_onnx_OfflineTts_generateWithCallbackImpl(
SHERPA_ONNX_LOGE("string is: %s", p_text);

std::function<int32_t(const float *, int32_t, float)> callback_wrapper =
[env, callback](const float *samples, int32_t n, float /*progress*/) {
jclass cls = env->GetObjectClass(callback);
[env, callback](const float *samples, int32_t n,
float /*progress*/) -> int {
jclass cls = env->GetObjectClass(callback);

#if 0
// this block is for debugging only
Expand All @@ -207,22 +208,20 @@ Java_com_k2fsa_sherpa_onnx_OfflineTts_generateWithCallbackImpl(
env->ReleaseStringUTFChars(classString, className);
#endif

jmethodID mid =
env->GetMethodID(cls, "invoke", "([F)Ljava/lang/Integer;");
if (mid == nullptr) {
SHERPA_ONNX_LOGE("Failed to get the callback. Ignore it.");
return 1;
}

jfloatArray samples_arr = env->NewFloatArray(n);
env->SetFloatArrayRegion(samples_arr, 0, n, samples);

jobject should_continue =
env->CallObjectMethod(callback, mid, samples_arr);
jclass jklass = env->GetObjectClass(should_continue);
jmethodID int_value_mid = env->GetMethodID(jklass, "intValue", "()I");
return env->CallIntMethod(should_continue, int_value_mid);
};
jmethodID mid = env->GetMethodID(cls, "invoke", "([F)Ljava/lang/Integer;");
if (mid == nullptr) {
SHERPA_ONNX_LOGE("Failed to get the callback. Ignore it.");
return 1;
}

jfloatArray samples_arr = env->NewFloatArray(n);
env->SetFloatArrayRegion(samples_arr, 0, n, samples);

jobject should_continue = env->CallObjectMethod(callback, mid, samples_arr);
jclass jklass = env->GetObjectClass(should_continue);
jmethodID int_value_mid = env->GetMethodID(jklass, "intValue", "()I");
return env->CallIntMethod(should_continue, int_value_mid);
};

auto audio = reinterpret_cast<sherpa_onnx::OfflineTts *>(ptr)->Generate(
p_text, sid, speed, callback_wrapper);
Expand Down

0 comments on commit 2f8c489

Please sign in to comment.