Skip to content

Commit

Permalink
Add C++ API for streaming ASR.
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Oct 22, 2024
1 parent 1af8ad8 commit 8d71188
Show file tree
Hide file tree
Showing 25 changed files with 672 additions and 22 deletions.
21 changes: 21 additions & 0 deletions .github/scripts/test-cxx-api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -ex

log() {
# This function is from espnet
local fname=${BASH_SOURCE[1]##*/}
echo -e "$(date '+%Y-%m-%d %H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*"
}

echo "CXX_STREAMING_ZIPFORMER_EXE is $CXX_STREAMING_ZIPFORMER_EXE"
echo "PATH: $PATH"

log "------------------------------------------------------------"
log "Test streaming zipformer CXX API"
log "------------------------------------------------------------"
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
$CXX_STREAMING_ZIPFORMER_EXE
rm -rf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20
2 changes: 0 additions & 2 deletions .github/workflows/c-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
push:
branches:
- master
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'
paths:
- '.github/workflows/c-api.yaml'
- 'CMakeLists.txt'
Expand Down
117 changes: 117 additions & 0 deletions .github/workflows/cxx-api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
name: cxx-api

on:
push:
branches:
- master
paths:
- '.github/workflows/cxx-api.yaml'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
- 'sherpa-onnx/c-api/*'
- 'cxx-api-examples/**'
pull_request:
branches:
- master
paths:
- '.github/workflows/cxx-api.yaml'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
- 'sherpa-onnx/c-api/*'
- 'cxx-api-examples/**'

workflow_dispatch:

concurrency:
group: cxx-api-${{ github.ref }}
cancel-in-progress: true

jobs:
cxx_api:
name: ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]

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

- name: ccache
uses: hendrikmuhs/[email protected]
with:
key: ${{ matrix.os }}-cxx-api-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 \
-D CMAKE_BUILD_TYPE=Release \
-D BUILD_SHARED_LIBS=ON \
-D CMAKE_INSTALL_PREFIX=./install \
-D SHERPA_ONNX_ENABLE_BINARY=OFF \
..
make -j2 install
ls -lh install/lib
ls -lh install/include
if [[ ${{ matrix.os }} == ubuntu-latest ]]; then
ldd ./install/lib/libsherpa-onnx-c-api.so
ldd ./install/lib/libsherpa-onnx-cxx-api.so
echo "---"
readelf -d ./install/lib/libsherpa-onnx-c-api.so
readelf -d ./install/lib/libsherpa-onnx-cxx-api.so
fi
if [[ ${{ matrix.os }} == macos-latest ]]; then
otool -L ./install/lib/libsherpa-onnx-c-api.dylib
otool -L ./install/lib/libsherpa-onnx-cxx-api.dylib
fi
- name: Test streaming zipformer
shell: bash
run: |
g++ -std=c++17 -o streaming-zipformer-cxx-api ./cxx-api-examples/streaming-zipformer-cxx-api.cc \
-I ./build/install/include \
-L ./build/install/lib/ \
-l sherpa-onnx-cxx-api \
-l sherpa-onnx-c-api \
-l onnxruntime
ls -lh streaming-zipformer-cxx-api
if [[ ${{ matrix.os }} == ubuntu-latest ]]; then
ldd ./streaming-zipformer-cxx-api
echo "----"
readelf -d ./streaming-zipformer-cxx-api
fi
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
ls -lh sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20
echo "---"
ls -lh sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/test_wavs
export LD_LIBRARY_PATH=$PWD/build/install/lib:$LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=$PWD/build/install/lib:$DYLD_LIBRARY_PATH
./streaming-zipformer-cxx-api
rm -rf sherpa-onnx-streaming-zipformer-*
rm ./streaming-zipformer-cxx-api
14 changes: 14 additions & 0 deletions .github/workflows/linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ on:
- '.github/scripts/test-offline-punctuation.sh'
- '.github/scripts/test-online-punctuation.sh'
- '.github/scripts/test-speaker-diarization.sh'
- '.github/scripts/test-c-api.sh'
- '.github/scripts/test-cxx-api.sh'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
Expand All @@ -40,6 +42,8 @@ on:
- '.github/scripts/test-offline-punctuation.sh'
- '.github/scripts/test-online-punctuation.sh'
- '.github/scripts/test-speaker-diarization.sh'
- '.github/scripts/test-c-api.sh'
- '.github/scripts/test-cxx-api.sh'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
Expand Down Expand Up @@ -145,6 +149,16 @@ jobs:
name: release-${{ matrix.build_type }}-with-shared-lib-${{ matrix.shared_lib }}-with-tts-${{ matrix.with_tts }}
path: install/*

- name: Test C++ API
shell: bash
run: |
du -h -d1 .
export PATH=$PWD/build/bin:$PATH
export CXX_STREAMING_ZIPFORMER_EXE=streaming-zipformer-cxx-api
.github/scripts/test-cxx-api.sh
du -h -d1 .
- name: Test offline speaker diarization
shell: bash
run: |
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ on:
- '.github/scripts/test-offline-punctuation.sh'
- '.github/scripts/test-online-punctuation.sh'
- '.github/scripts/test-speaker-diarization.sh'
- '.github/scripts/test-c-api.sh'
- '.github/scripts/test-cxx-api.sh'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
Expand All @@ -39,6 +41,8 @@ on:
- '.github/scripts/test-offline-punctuation.sh'
- '.github/scripts/test-online-punctuation.sh'
- '.github/scripts/test-speaker-diarization.sh'
- '.github/scripts/test-c-api.sh'
- '.github/scripts/test-cxx-api.sh'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
Expand Down Expand Up @@ -117,6 +121,16 @@ jobs:
otool -L build/bin/sherpa-onnx
otool -l build/bin/sherpa-onnx
- name: Test C++ API
shell: bash
run: |
du -h -d1 .
export PATH=$PWD/build/bin:$PATH
export CXX_STREAMING_ZIPFORMER_EXE=streaming-zipformer-cxx-api
.github/scripts/test-cxx-api.sh
du -h -d1 .
- name: Test offline speaker diarization
shell: bash
run: |
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/sanitizer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ jobs:
otool -L build/bin/sherpa-onnx
otool -l build/bin/sherpa-onnx
- name: Test C++ API
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export CXX_STREAMING_ZIPFORMER_EXE=streaming-zipformer-cxx-api
.github/scripts/test-cxx-api.sh
- name: Test online punctuation
shell: bash
run: |
Expand Down Expand Up @@ -109,7 +117,6 @@ jobs:
.github/scripts/test-online-ctc.sh
- name: Test C API
shell: bash
run: |
Expand Down
13 changes: 12 additions & 1 deletion .github/workflows/windows-x64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ on:
- '.github/scripts/test-offline-punctuation.sh'
- '.github/scripts/test-online-punctuation.sh'
- '.github/scripts/test-speaker-diarization.sh'
- '.github/scripts/test-c-api.sh'
- '.github/scripts/test-cxx-api.sh'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
Expand All @@ -36,6 +38,8 @@ on:
- '.github/scripts/test-offline-punctuation.sh'
- '.github/scripts/test-online-punctuation.sh'
- '.github/scripts/test-speaker-diarization.sh'
- '.github/scripts/test-c-api.sh'
- '.github/scripts/test-cxx-api.sh'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
Expand Down Expand Up @@ -89,10 +93,17 @@ jobs:
name: release-windows-x64-${{ matrix.shared_lib }}-${{ matrix.with_tts }}
path: build/install/*

- name: Test C++ API
shell: bash
run: |
export PATH=$PWD/build/bin/Release:$PATH
export CXX_STREAMING_ZIPFORMER_EXE=streaming-zipformer-cxx-api.exe
.github/scripts/test-cxx-api.sh
- name: Test offline speaker diarization
shell: bash
run: |
du -h -d1 .
export PATH=$PWD/build/bin/Release:$PATH
export EXE=sherpa-onnx-offline-speaker-diarization.exe
Expand Down
13 changes: 12 additions & 1 deletion .github/workflows/windows-x86.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ on:
- '.github/scripts/test-offline-punctuation.sh'
- '.github/scripts/test-online-punctuation.sh'
- '.github/scripts/test-speaker-diarization.sh'
- '.github/scripts/test-c-api.sh'
- '.github/scripts/test-cxx-api.sh'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
Expand All @@ -36,6 +38,8 @@ on:
- '.github/scripts/test-offline-punctuation.sh'
- '.github/scripts/test-online-punctuation.sh'
- '.github/scripts/test-speaker-diarization.sh'
- '.github/scripts/test-c-api.sh'
- '.github/scripts/test-cxx-api.sh'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
Expand Down Expand Up @@ -89,10 +93,17 @@ jobs:
name: release-windows-x86-${{ matrix.shared_lib }}-${{ matrix.with_tts }}
path: build/install/*

- name: Test C++ API
shell: bash
run: |
export PATH=$PWD/build/bin/Release:$PATH
export CXX_STREAMING_ZIPFORMER_EXE=streaming-zipformer-cxx-api.exe
.github/scripts/test-cxx-api.sh
- name: Test offline speaker diarization
shell: bash
run: |
du -h -d1 .
export PATH=$PWD/build/bin/Release:$PATH
export EXE=sherpa-onnx-offline-speaker-diarization.exe
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ add_subdirectory(sherpa-onnx)
if(SHERPA_ONNX_ENABLE_C_API AND SHERPA_ONNX_ENABLE_BINARY AND SHERPA_ONNX_BUILD_C_API_EXAMPLES)
set(SHERPA_ONNX_PKG_WITH_CARGS "-lcargs")
add_subdirectory(c-api-examples)
add_subdirectory(cxx-api-examples)
endif()

if(SHERPA_ONNX_ENABLE_WASM)
Expand Down
2 changes: 1 addition & 1 deletion c-api-examples/streaming-ctc-buffered-tokens-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int32_t main() {
recognizer_config.decoding_method = "greedy_search";
recognizer_config.model_config = online_model_config;

SherpaOnnxOnlineRecognizer *recognizer =
const SherpaOnnxOnlineRecognizer *recognizer =
SherpaOnnxCreateOnlineRecognizer(&recognizer_config);

free((void *)tokens_buf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ int32_t main() {
recognizer_config.decoding_method = "greedy_search";
recognizer_config.model_config = online_model_config;

SherpaOnnxOnlineRecognizer *recognizer =
const SherpaOnnxOnlineRecognizer *recognizer =
SherpaOnnxCreateOnlineRecognizer(&recognizer_config);

free((void *)tokens_buf);
Expand Down
2 changes: 1 addition & 1 deletion c-api-examples/streaming-paraformer-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int32_t main() {
recognizer_config.decoding_method = "greedy_search";
recognizer_config.model_config = online_model_config;

SherpaOnnxOnlineRecognizer *recognizer =
const SherpaOnnxOnlineRecognizer *recognizer =
SherpaOnnxCreateOnlineRecognizer(&recognizer_config);

if (recognizer == NULL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int32_t main() {
recognizer_config.hotwords_buf = hotwords_buf;
recognizer_config.hotwords_buf_size = hotwords_buf_size;

SherpaOnnxOnlineRecognizer *recognizer =
const SherpaOnnxOnlineRecognizer *recognizer =
SherpaOnnxCreateOnlineRecognizer(&recognizer_config);

free((void *)tokens_buf);
Expand Down
2 changes: 1 addition & 1 deletion c-api-examples/streaming-zipformer-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int32_t main() {
recognizer_config.decoding_method = "greedy_search";
recognizer_config.model_config = online_model_config;

SherpaOnnxOnlineRecognizer *recognizer =
const SherpaOnnxOnlineRecognizer *recognizer =
SherpaOnnxCreateOnlineRecognizer(&recognizer_config);

if (recognizer == NULL) {
Expand Down
1 change: 1 addition & 0 deletions cmake/cmake_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def get_binaries():
binaries += [
"onnxruntime.dll",
"sherpa-onnx-c-api.dll",
"sherpa-onnx-cxx-api.dll",
]

return binaries
Expand Down
4 changes: 4 additions & 0 deletions cxx-api-examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include_directories(${CMAKE_SOURCE_DIR})

add_executable(streaming-zipformer-cxx-api ./streaming-zipformer-cxx-api.cc)
target_link_libraries(streaming-zipformer-cxx-api sherpa-onnx-cxx-api)
Loading

0 comments on commit 8d71188

Please sign in to comment.