From 3b90e85ef250f9ae41e6dd748aa098e8a05e5bcc Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Mon, 4 Dec 2023 19:27:55 +0800 Subject: [PATCH] Fix building for .Net (#463) --- .github/workflows/dot-net.yaml | 123 +++--------------- .github/workflows/test-dot-net-nuget.yaml | 57 ++++++++ .github/workflows/test-dot-net.yaml | 122 +++++++++++++---- scripts/dotnet/examples/README.md | 3 + .../examples/offline-decode-files.csproj | 20 +++ .../examples/online-decode-files.csproj | 21 +++ .../speech-recognition-from-microphone.csproj | 21 +++ scripts/dotnet/generate.py | 45 ++++--- scripts/dotnet/offline.cs | 7 + scripts/dotnet/run.sh | 72 ++++++++++ 10 files changed, 341 insertions(+), 150 deletions(-) create mode 100644 .github/workflows/test-dot-net-nuget.yaml create mode 100644 scripts/dotnet/examples/README.md create mode 100644 scripts/dotnet/examples/offline-decode-files.csproj create mode 100644 scripts/dotnet/examples/online-decode-files.csproj create mode 100644 scripts/dotnet/examples/speech-recognition-from-microphone.csproj diff --git a/.github/workflows/dot-net.yaml b/.github/workflows/dot-net.yaml index 12bbc0150..289e907b1 100644 --- a/.github/workflows/dot-net.yaml +++ b/.github/workflows/dot-net.yaml @@ -1,130 +1,45 @@ -name: dot-net +name: release-nuget-package on: - push: - branches: - - dot-net - - fix-dot-net - tags: - - '*' - workflow_dispatch: concurrency: - group: dot-net-${{ github.ref }} + group: release-nuget-package cancel-in-progress: true +permissions: + contents: read + jobs: - build-libs: - name: dot-net for ${{ matrix.os }} + release-nuget-package: runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - os: [ubuntu-latest, windows-latest, macos-latest] - - steps: - - uses: actions/checkout@v4 - # see https://cibuildwheel.readthedocs.io/en/stable/changelog/ - # for a list of versions - - name: Build wheels - uses: pypa/cibuildwheel@v2.11.4 - env: - CIBW_BEFORE_BUILD: "pip install -U cmake numpy" - CIBW_BUILD: "cp38-*64" - CIBW_SKIP: "cp27-* cp35-* cp36-* *-win32 pp* *-musllinux* *-manylinux_i686" - CIBW_BUILD_VERBOSITY: 3 - CIBW_ENVIRONMENT_LINUX: LD_LIBRARY_PATH='/project/build/bdist.linux-x86_64/wheel/sherpa_onnx/lib' - CIBW_REPAIR_WHEEL_COMMAND_MACOS: "" - - - name: Display wheels - shell: bash - run: | - ls -lh ./wheelhouse/*.whl - unzip -l ./wheelhouse/*.whl - - - uses: actions/upload-artifact@v3 - with: - name: ${{ matrix.os }}-wheels - path: ./wheelhouse/*.whl - - build-nuget-packages: - name: build-nuget-packages - runs-on: ubuntu-latest - needs: build-libs + os: [ubuntu-latest] steps: - uses: actions/checkout@v4 - - - name: Retrieve artifact from ubuntu-latest - uses: actions/download-artifact@v2 - with: - name: ubuntu-latest-wheels - path: ./linux - - - name: Retrieve artifact from macos-latest - uses: actions/download-artifact@v2 - with: - name: macos-latest-wheels - path: ./macos - - - name: Retrieve artifact from windows-latest - uses: actions/download-artifact@v2 - with: - name: windows-latest-wheels - path: ./windows - - - name: Display wheels - shell: bash - run: | - tree . - - - name: Unzip Ubuntu wheels - shell: bash - run: | - cd linux - unzip ./*.whl - tree . - - - name: Unzip macOS wheels - shell: bash - run: | - cd macos - unzip ./*.whl - tree . - - - name: Unzip Windows wheels - shell: bash - run: | - cd windows - unzip ./*.whl - tree . - - - name: Setup .NET Core 3.1 - uses: actions/setup-dotnet@v1 with: - dotnet-version: 3.1.x + fetch-depth: 0 - - name: Setup .NET 7.0 + - name: Setup .NET uses: actions/setup-dotnet@v1 with: - dotnet-version: 7.0.x + dotnet-version: | + 6.0.x + 7.0.x - name: Check dotnet run: dotnet --info - - name: build nuget packages + - name: Build shell: bash run: | cd scripts/dotnet ./run.sh - ls -lh packages - - uses: actions/upload-artifact@v3 - name: upload nuget packages - with: - name: nuget-packages - path: scripts/dotnet/packages/*.nupkg + ls -lh /tmp/packages - name: publish .Net packages to nuget.org if: github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa' && github.event_name == 'push' && contains(github.ref, 'refs/tags/') @@ -133,13 +48,5 @@ jobs: API_KEY: ${{ secrets.NUGET_API_KEY }} run: | # API_KEY is valid until 2024.05.02 - cd scripts/dotnet/packages + cd /tmp/packages dotnet nuget push ./org.k2fsa.sherpa.onnx.*.nupkg --skip-duplicate --api-key $API_KEY --source https://api.nuget.org/v3/index.json - - - name: Release nuget packages - 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: scripts/dotnet/packages/*.nupkg diff --git a/.github/workflows/test-dot-net-nuget.yaml b/.github/workflows/test-dot-net-nuget.yaml new file mode 100644 index 000000000..8ced1c751 --- /dev/null +++ b/.github/workflows/test-dot-net-nuget.yaml @@ -0,0 +1,57 @@ +name: test-dot-net-nuget + +on: + workflow_dispatch: + + schedule: + # minute (0-59) + # hour (0-23) + # day of the month (1-31) + # month (1-12) + # day of the week (0-6) + # nightly build at 23:50 UTC time every day + - cron: "50 23 * * *" + +concurrency: + group: test-dot-net-nuget + cancel-in-progress: true + +permissions: + contents: read + +jobs: + test-dot-net-nuget: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup .NET 6.0 + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 6.0.x + + - name: Check dotnet + run: dotnet --info + + - name: Decode a file + shell: bash + run: | + cd dotnet-examples/ + + cd online-decode-files + ./run-transducer.sh + ./run-paraformer.sh + + cd ../offline-decode-files + ./run-nemo-ctc.sh + ./run-paraformer.sh + ./run-zipformer.sh + ./run-whisper.sh + ./run-tdnn-yesno.sh diff --git a/.github/workflows/test-dot-net.yaml b/.github/workflows/test-dot-net.yaml index 98ecbc9ce..f5fabda45 100644 --- a/.github/workflows/test-dot-net.yaml +++ b/.github/workflows/test-dot-net.yaml @@ -4,28 +4,13 @@ on: push: branches: - master - paths: - - '.github/workflows/test-dot-net' - - 'dotnet-examples/**' pull_request: branches: - master - paths: - - '.github/workflows/test-dot-net' - - 'dotnet-examples/**' workflow_dispatch: - schedule: - # minute (0-59) - # hour (0-23) - # day of the month (1-31) - # month (1-12) - # day of the week (0-6) - # nightly build at 23:50 UTC time every day - - cron: "50 23 * * *" - concurrency: group: test-dot-net cancel-in-progress: true @@ -34,31 +19,124 @@ permissions: contents: read jobs: - test-dot-net: + build-libs: + name: ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] + python-version: ["3.8"] steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - - name: Setup .NET Core 3.1 - uses: actions/setup-dotnet@v1 + - name: ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ${{ matrix.os }}-release-shared + + - name: Build sherpa-onnx + 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 + cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=./install -DCMAKE_BUILD_TYPE=Release .. + cmake --build . --target install --config Release + + - uses: actions/upload-artifact@v3 + with: + name: ${{ matrix.os }} + path: ./build/install/lib/ + + test-dot-net: + runs-on: ${{ matrix.os }} + needs: [build-libs] + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + python-version: ["3.8"] + + steps: + - uses: actions/checkout@v4 with: - dotnet-version: 3.1.x + fetch-depth: 0 - - name: Setup .NET 6.0 - uses: actions/setup-dotnet@v1 + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 with: - dotnet-version: 6.0.x + python-version: ${{ matrix.python-version }} + + - name: Install Python dependencies + shell: bash + run: | + python3 -m pip install --upgrade pip Jinja2 + + - name: Retrieve artifact from ubuntu-latest + uses: actions/download-artifact@v2 + with: + name: ubuntu-latest + path: /tmp/linux + + - name: Retrieve artifact from macos-latest + uses: actions/download-artifact@v2 + with: + name: macos-latest + path: /tmp/macos + + - name: Retrieve artifact from windows-latest + uses: actions/download-artifact@v2 + with: + name: windows-latest + path: /tmp/windows + + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: | + 6.0.x + 7.0.x - name: Check dotnet run: dotnet --info + - name: Display files + shell: bash + run: | + echo "----------/tmp/----------" + ls -lh /tmp/ + + echo "----------/tmp/linux----------" + ls -lh /tmp/linux + + echo "----------/tmp/macos----------" + ls -lh /tmp/macos + + echo "----------/tmp/windows----------" + ls -lh /tmp/windows + + - name: Build + shell: bash + run: | + cd scripts/dotnet + ./run.sh + + - name: Copy files + shell: bash + run: | + cp -v scripts/dotnet/examples/offline-decode-files.csproj dotnet-examples/offline-decode-files/ + cp -v scripts/dotnet/examples/online-decode-files.csproj dotnet-examples/online-decode-files/ + cp -v scripts/dotnet/examples/speech-recognition-from-microphone.csproj dotnet-examples/speech-recognition-from-microphone/ + + ls -lh /tmp + - name: Decode a file shell: bash run: | diff --git a/scripts/dotnet/examples/README.md b/scripts/dotnet/examples/README.md new file mode 100644 index 000000000..253488e50 --- /dev/null +++ b/scripts/dotnet/examples/README.md @@ -0,0 +1,3 @@ +# Introduction + +Files in this directory are used exclusively by CI. diff --git a/scripts/dotnet/examples/offline-decode-files.csproj b/scripts/dotnet/examples/offline-decode-files.csproj new file mode 100644 index 000000000..6ae4b5a06 --- /dev/null +++ b/scripts/dotnet/examples/offline-decode-files.csproj @@ -0,0 +1,20 @@ + + + + Exe + net6.0 + offline_decode_files + enable + enable + + + + /tmp/packages;$(RestoreSources);https://api.nuget.org/v3/index.json + + + + + + + + diff --git a/scripts/dotnet/examples/online-decode-files.csproj b/scripts/dotnet/examples/online-decode-files.csproj new file mode 100644 index 000000000..740e6dda1 --- /dev/null +++ b/scripts/dotnet/examples/online-decode-files.csproj @@ -0,0 +1,21 @@ + + + + Exe + net6.0 + online_decode_files + enable + enable + + + + + /tmp/packages;$(RestoreSources);https://api.nuget.org/v3/index.json + + + + + + + + diff --git a/scripts/dotnet/examples/speech-recognition-from-microphone.csproj b/scripts/dotnet/examples/speech-recognition-from-microphone.csproj new file mode 100644 index 000000000..12d25a841 --- /dev/null +++ b/scripts/dotnet/examples/speech-recognition-from-microphone.csproj @@ -0,0 +1,21 @@ + + + + Exe + net6.0 + speech_recognition_from_microphone + enable + enable + + + + /tmp/packages;$(RestoreSources);https://api.nuget.org/v3/index.json + + + + + + + + + diff --git a/scripts/dotnet/generate.py b/scripts/dotnet/generate.py index 72f4b2bcb..bbd44d3be 100755 --- a/scripts/dotnet/generate.py +++ b/scripts/dotnet/generate.py @@ -33,12 +33,18 @@ def get_dict(): def process_linux(s): libs = [ + "libespeak-ng.so", + "libkaldi-decoder-core.so", "libkaldi-native-fbank-core.so", - "libonnxruntime.so.1.16.2", + "libonnxruntime.so.1.16.3", + "libpiper_phonemize.so.1", "libsherpa-onnx-c-api.so", "libsherpa-onnx-core.so", + "libsherpa-onnx-fst.so.6", + "libsherpa-onnx-kaldifst-core.so", + "libucd.so", ] - prefix = f"{SHERPA_ONNX_DIR}/linux/sherpa_onnx/lib/" + prefix = "/tmp/linux/" libs = [prefix + lib for lib in libs] libs = "\n ;".join(libs) @@ -55,12 +61,18 @@ def process_linux(s): def process_macos(s): libs = [ + "libespeak-ng.dylib", + "libkaldi-decoder-core.dylib", "libkaldi-native-fbank-core.dylib", - "libonnxruntime.1.16.2.dylib", + "libonnxruntime.1.16.3.dylib", + "libpiper_phonemize.1.dylib", "libsherpa-onnx-c-api.dylib", "libsherpa-onnx-core.dylib", + "libsherpa-onnx-fst.6.dylib", + "libsherpa-onnx-kaldifst-core.dylib", + "libucd.dylib", ] - prefix = f"{SHERPA_ONNX_DIR}/macos/sherpa_onnx/lib/" + prefix = f"/tmp/macos/" libs = [prefix + lib for lib in libs] libs = "\n ;".join(libs) @@ -77,30 +89,23 @@ def process_macos(s): def process_windows(s): libs = [ + "espeak-ng.dll", + "kaldi-decoder-core.dll", "kaldi-native-fbank-core.dll", "onnxruntime.dll", + "piper_phonemize.dll", "sherpa-onnx-c-api.dll", "sherpa-onnx-core.dll", + "sherpa-onnx-fst.lib", + "sherpa-onnx-kaldifst-core.lib", + "ucd.dll", ] version = get_version() - prefix1 = f"{SHERPA_ONNX_DIR}/windows/sherpa_onnx/lib/" - prefix2 = f"{SHERPA_ONNX_DIR}/windows/sherpa_onnx/" - prefix3 = f"{SHERPA_ONNX_DIR}/windows/" - prefix4 = f"{SHERPA_ONNX_DIR}/windows/sherpa_onnx-{version}.data/data/bin/" - print(prefix1, prefix2, prefix3, prefix4) - - lib_list = [] - for lib in libs: - for prefix in [prefix1, prefix2, prefix3, prefix4]: - f = Path(prefix) / lib - if f.is_file(): - lib_list.append(str(f)) - break - - print("lib_list", lib_list) - libs = "\n ;".join(lib_list) + prefix = "/tmp/windows/" + libs = [prefix + lib for lib in libs] + libs = "\n ;".join(libs) d = get_dict() d["dotnet_rid"] = "win-x64" diff --git a/scripts/dotnet/offline.cs b/scripts/dotnet/offline.cs index 3d8014398..2c040cbac 100644 --- a/scripts/dotnet/offline.cs +++ b/scripts/dotnet/offline.cs @@ -138,6 +138,8 @@ public OfflineRecognizerConfig() DecodingMethod = "greedy_search"; MaxActivePaths = 4; + HotwordsFile = ""; + HotwordsScore = 1.5F; } public FeatureConfig FeatConfig; @@ -148,6 +150,11 @@ public OfflineRecognizerConfig() public string DecodingMethod; public int MaxActivePaths; + + [MarshalAs(UnmanagedType.LPStr)] + public string HotwordsFile; + + public float HotwordsScore; } public class OfflineRecognizerResult diff --git a/scripts/dotnet/run.sh b/scripts/dotnet/run.sh index 03fd7990a..f56b2471b 100755 --- a/scripts/dotnet/run.sh +++ b/scripts/dotnet/run.sh @@ -3,6 +3,75 @@ set -ex +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +SHERPA_ONNX_DIR=$(cd $SCRIPT_DIR/../.. && pwd) +echo "SCRIPT_DIR: $SCRIPT_DIR" +echo "SHERPA_ONNX_DIR: $SHERPA_ONNX_DIR" + +SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" $SHERPA_ONNX_DIR/CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) + +mkdir -p /tmp/ +pushd /tmp + +mkdir -p linux macos windows + +if [ ! -f /tmp/linux/libsherpa-onnx-core.so ]; then + echo "---linux x86_64---" + cd linux + mkdir wheel + cd wheel + curl -OL https://huggingface.co/csukuangfj/sherpa-onnx-wheels/resolve/main/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl + unzip sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl + cp -v sherpa_onnx/lib/*.so* ../ + cd .. + rm -v libpiper_phonemize.so libpiper_phonemize.so.1.2.0 + rm -v libsherpa-onnx-fst.so + rm -v libonnxruntime.so + rm -v libcargs.so + rm -rf wheel + ls -lh + cd .. +fi + +if [ ! -f /tmp/macos/libsherpa-onnx-core.dylib ]; then + echo "---macOS x86_64---" + cd macos + mkdir wheel + cd wheel + curl -OL https://huggingface.co/csukuangfj/sherpa-onnx-wheels/resolve/main/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-macosx_10_14_x86_64.whl + unzip sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-macosx_10_14_x86_64.whl + cp -v sherpa_onnx/lib/*.dylib ../ + + cd .. + + rm -v libcargs.dylib + rm -v libonnxruntime.dylib + rm -v libpiper_phonemize.1.2.0.dylib libpiper_phonemize.dylib + rm -v libsherpa-onnx-fst.dylib + rm -rf wheel + ls -lh + cd .. +fi + + +if [ ! -f /tmp/windows/libsherpa-onnx-core.dll ]; then + echo "---windows x64---" + cd windows + mkdir wheel + cd wheel + curl -OL https://huggingface.co/csukuangfj/sherpa-onnx-wheels/resolve/main/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-win_amd64.whl + unzip sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-win_amd64.whl + cp -v sherpa_onnx-${SHERPA_ONNX_VERSION}.data/data/bin/*.dll ../ + cp -v sherpa_onnx-${SHERPA_ONNX_VERSION}.data/data/bin/*.lib ../ + cd .. + + rm -rf wheel + ls -lh + cd .. +fi + +popd + mkdir -p macos linux windows all cp ./online.cs all @@ -31,3 +100,6 @@ dotnet pack -c Release -o ../packages popd ls -lh packages + +mkdir /tmp/packages +cp -v packages/*.nupkg /tmp/packages