From ee3e57ffb0ff82d352f7125ebecc96df6be92c18 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Wed, 21 Feb 2024 15:47:57 +0800 Subject: [PATCH] Refactor WebAssemlby build script. Make it easier to build WebAssemlby for ASR. --- .../workflows/wasm-simd-hf-space-de-tts.yaml | 106 ++++++++++++++++++ ...en.yaml => wasm-simd-hf-space-en-tts.yaml} | 23 ++-- CMakeLists.txt | 10 +- build-wasm-simd.sh => build-wasm-simd-tts.sh | 5 +- wasm/CMakeLists.txt | 60 +--------- wasm/tts/CMakeLists.txt | 58 ++++++++++ wasm/{ => tts}/app.js | 0 wasm/{ => tts}/assets/.gitignore | 0 wasm/{ => tts}/assets/README.md | 2 +- wasm/{ => tts}/index.html | 0 wasm/{ => tts}/sherpa-onnx-wasm-main.cc | 0 wasm/{ => tts}/sherpa-onnx.js | 0 12 files changed, 189 insertions(+), 75 deletions(-) create mode 100644 .github/workflows/wasm-simd-hf-space-de-tts.yaml rename .github/workflows/{wasm-simd-hf-space-en.yaml => wasm-simd-hf-space-en-tts.yaml} (87%) rename build-wasm-simd.sh => build-wasm-simd-tts.sh (94%) create mode 100644 wasm/tts/CMakeLists.txt rename wasm/{ => tts}/app.js (100%) rename wasm/{ => tts}/assets/.gitignore (100%) rename wasm/{ => tts}/assets/README.md (97%) rename wasm/{ => tts}/index.html (100%) rename wasm/{ => tts}/sherpa-onnx-wasm-main.cc (100%) rename wasm/{ => tts}/sherpa-onnx.js (100%) diff --git a/.github/workflows/wasm-simd-hf-space-de-tts.yaml b/.github/workflows/wasm-simd-hf-space-de-tts.yaml new file mode 100644 index 000000000..dc3702ab8 --- /dev/null +++ b/.github/workflows/wasm-simd-hf-space-de-tts.yaml @@ -0,0 +1,106 @@ +name: wasm-simd-hf-space-de-tts + +on: + release: + types: + - published + + workflow_dispatch: + +concurrency: + group: wasm-simd-hf-space-de-tts-${{ github.ref }} + cancel-in-progress: true + +jobs: + wasm-simd-hf-space-de-tts: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Install emsdk + uses: mymindstorm/setup-emsdk@v14 + + - name: View emsdk version + shell: bash + run: | + emcc -v + echo "--------------------" + emcc --check + + - name: Download model files + shell: bash + run: | + cd wasm/tts/assets + ls -lh + echo "----------" + wget -q https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-de_DE-thorsten_emotional-medium.tar.bz2 + tar xf vits-piper-de_DE-thorsten_emotional-medium.tar.bz2 + rm vits-piper-de_DE-thorsten_emotional-medium.tar.bz2 + + mv -v vits-piper-de_DE-thorsten_emotional-medium/de_DE-thorsten_emotional-medium.onnx ./model.onnx + mv -v vits-piper-de_DE-thorsten_emotional-medium/tokens.txt ./ + mv vits-piper-de_DE-thorsten_emotional-medium/espeak-ng-data ./ + + rm -rf vits-piper-de_DE-thorsten_emotional-medium + + ls -lh + + - name: Build sherpa-onnx for WebAssembly + shell: bash + run: | + ./build-wasm-simd-tts.sh + + - name: collect files + shell: bash + run: | + SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) + + mv build-wasm-simd-tts/install/bin/wasm sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-de-tts + ls -lh sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-de-tts + tar cjfv sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-de-tts.tar.bz2 ./sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-de-tts + + - name: Upload wasm files + uses: actions/upload-artifact@v4 + with: + name: sherpa-onnx-wasm-simd-de-tts + path: ./sherpa-onnx-wasm-simd-*.tar.bz2 + + - name: Publish to huggingface + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + uses: nick-fields/retry@v2 + with: + max_attempts: 20 + timeout_seconds: 200 + shell: bash + command: | + SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) + + git config --global user.email "csukuangfj@gmail.com" + git config --global user.name "Fangjun Kuang" + + rm -rf huggingface + export GIT_LFS_SKIP_SMUDGE=1 + + git clone https://huggingface.co/spaces/k2-fsa/web-assembly-tts-sherpa-onnx-de huggingface + cd huggingface + git fetch + git pull + git merge -m "merge remote" --ff origin main + + cp -v ../sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-de-tts/* . + + git status + git lfs track "*.data" + git lfs track "*.wasm" + ls -lh + + git add . + git commit -m "update model" + git push https://csukuangfj:$HF_TOKEN@huggingface.co/spaces/k2-fsa/web-assembly-tts-sherpa-onnx-de main diff --git a/.github/workflows/wasm-simd-hf-space-en.yaml b/.github/workflows/wasm-simd-hf-space-en-tts.yaml similarity index 87% rename from .github/workflows/wasm-simd-hf-space-en.yaml rename to .github/workflows/wasm-simd-hf-space-en-tts.yaml index d9093a7fa..55f002a89 100644 --- a/.github/workflows/wasm-simd-hf-space-en.yaml +++ b/.github/workflows/wasm-simd-hf-space-en-tts.yaml @@ -1,9 +1,6 @@ -name: wasm-simd-hf-space-en +name: wasm-simd-hf-space-en-tts on: - push: - branches: - - wasm-2 release: types: - published @@ -11,11 +8,11 @@ on: workflow_dispatch: concurrency: - group: wasm-simd-hf-space-en-${{ github.ref }} + group: wasm-simd-hf-space-en-tts-${{ github.ref }} cancel-in-progress: true jobs: - wasm-simd-hf-space-en: + wasm-simd-hf-space-en-tts: runs-on: ${{ matrix.os }} strategy: fail-fast: false @@ -39,7 +36,7 @@ jobs: - name: Download model files shell: bash run: | - cd wasm/assets + cd wasm/tts/assets ls -lh echo "----------" wget -q https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-en_US-libritts_r-medium.tar.bz2 @@ -55,21 +52,21 @@ jobs: - name: Build sherpa-onnx for WebAssembly shell: bash run: | - ./build-wasm-simd.sh + ./build-wasm-simd-tts.sh - name: collect files shell: bash run: | SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) - mv build-wasm-simd/install/bin/wasm sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-en - ls -lh sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-en - tar cjfv sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-en.tar.bz2 ./sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-en + mv build-wasm-simd-tts/install/bin/wasm sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-en-tts + ls -lh sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-en-tts + tar cjfv sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-en-tts.tar.bz2 ./sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-en-tts - name: Upload wasm files uses: actions/upload-artifact@v4 with: - name: sherpa-onnx-wasm-simd-en + name: sherpa-onnx-wasm-simd-en-tts path: ./sherpa-onnx-wasm-simd-*.tar.bz2 - name: Publish to huggingface @@ -95,7 +92,7 @@ jobs: git pull git merge -m "merge remote" --ff origin main - cp -v ../sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-en/* . + cp -v ../sherpa-onnx-wasm-simd-${SHERPA_ONNX_VERSION}-en-tts/* . git status git lfs track "*.data" diff --git a/CMakeLists.txt b/CMakeLists.txt index 1cc8af693..03dbe3f78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ option(SHERPA_ONNX_ENABLE_C_API "Whether to build C API" ON) option(SHERPA_ONNX_ENABLE_WEBSOCKET "Whether to build webscoket server/client" ON) option(SHERPA_ONNX_ENABLE_GPU "Enable ONNX Runtime GPU support" OFF) option(SHERPA_ONNX_ENABLE_WASM "Whether to enable WASM" OFF) +option(SHERPA_ONNX_ENABLE_WASM_TTS "Whether to enable WASM for TTS" OFF) option(SHERPA_ONNX_ENABLE_BINARY "Whether to build binaries" ON) option(SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY "True to link libstdc++ statically. Used only when BUILD_SHARED_LIBS is OFF on Linux" ON) @@ -104,6 +105,14 @@ message(STATUS "SHERPA_ONNX_ENABLE_C_API ${SHERPA_ONNX_ENABLE_C_API}") message(STATUS "SHERPA_ONNX_ENABLE_WEBSOCKET ${SHERPA_ONNX_ENABLE_WEBSOCKET}") message(STATUS "SHERPA_ONNX_ENABLE_GPU ${SHERPA_ONNX_ENABLE_GPU}") message(STATUS "SHERPA_ONNX_ENABLE_WASM ${SHERPA_ONNX_ENABLE_WASM}") +message(STATUS "SHERPA_ONNX_ENABLE_WASM_TTS ${SHERPA_ONNX_ENABLE_WASM_TTS}") + +if(SHERPA_ONNX_ENABLE_WASM_TTS) + if(NOT SHERPA_ONNX_ENABLE_WASM) + message(FATAL_ERROR "Please set SHERPA_ONNX_ENABLE_WASM to ON if you enable WASM for tts") + endif() +endif() + if(SHERPA_ONNX_ENABLE_WASM) add_definitions(-DSHERPA_ONNX_ENABLE_WASM=1) endif() @@ -114,7 +123,6 @@ endif() set(CMAKE_CXX_EXTENSIONS OFF) message(STATUS "C++ Standard version: ${CMAKE_CXX_STANDARD}") - include(CheckIncludeFileCXX) if(UNIX AND NOT APPLE AND NOT SHERPA_ONNX_ENABLE_WASM AND NOT CMAKE_SYSTEM_NAME STREQUAL Android) diff --git a/build-wasm-simd.sh b/build-wasm-simd-tts.sh similarity index 94% rename from build-wasm-simd.sh rename to build-wasm-simd-tts.sh index 6f8fcef68..f3483613c 100755 --- a/build-wasm-simd.sh +++ b/build-wasm-simd-tts.sh @@ -31,8 +31,8 @@ if [ ! -f $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake ]; then exit 1 fi -mkdir -p build-wasm-simd -pushd build-wasm-simd +mkdir -p build-wasm-simd-tts +pushd build-wasm-simd-tts export SHERPA_ONNX_IS_USING_BUILD_WASM_SH=ON @@ -51,6 +51,7 @@ cmake \ -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ -DSHERPA_ONNX_ENABLE_GPU=OFF \ -DSHERPA_ONNX_ENABLE_WASM=ON \ + -DSHERPA_ONNX_ENABLE_WASM_TTS=ON \ -DSHERPA_ONNX_ENABLE_BINARY=OFF \ -DSHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY=OFF \ .. diff --git a/wasm/CMakeLists.txt b/wasm/CMakeLists.txt index 22d90ed25..2db9a3aa0 100644 --- a/wasm/CMakeLists.txt +++ b/wasm/CMakeLists.txt @@ -1,59 +1,3 @@ -if(NOT $ENV{SHERPA_ONNX_IS_USING_BUILD_WASM_SH}) - message(FATAL_ERROR "Please use ./build-wasm.sh to build for wasm") +if(SHERPA_ONNX_ENABLE_WASM_TTS) + add_subdirectory(tts) endif() - -if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/assets/decoder_jit_trace-pnnx.ncnn.bin") - message(WARNING "${CMAKE_CURRENT_SOURCE_DIR}/assets/decoder_jit_trace-pnnx.ncnn.bin does not exist") - # message(FATAL_ERROR "Please read ${CMAKE_CURRENT_SOURCE_DIR}/assets/README.md before you continue") -endif() - -set(exported_functions - MyPrint - SherpaOnnxCreateOfflineTts - SherpaOnnxDestroyOfflineTts - SherpaOnnxDestroyOfflineTtsGeneratedAudio - SherpaOnnxOfflineTtsGenerate - SherpaOnnxOfflineTtsGenerateWithCallback - SherpaOnnxOfflineTtsNumSpeakers - SherpaOnnxOfflineTtsSampleRate - SherpaOnnxWriteWave -) -set(mangled_exported_functions) -foreach(x IN LISTS exported_functions) - list(APPEND mangled_exported_functions "_${x}") -endforeach() -list(JOIN mangled_exported_functions "," all_exported_functions) - - -include_directories(${CMAKE_SOURCE_DIR}) -set(MY_FLAGS " -s FORCE_FILESYSTEM=1 -s INITIAL_MEMORY=512MB -s ALLOW_MEMORY_GROWTH=1") -string(APPEND MY_FLAGS " -sSTACK_SIZE=10485760 ") # 10MB -string(APPEND MY_FLAGS " -sEXPORTED_FUNCTIONS=[_CopyHeap,_malloc,_free,${all_exported_functions}] ") -string(APPEND MY_FLAGS "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/assets@. ") -string(APPEND MY_FLAGS " -sEXPORTED_RUNTIME_METHODS=['ccall','stringToUTF8','setValue','getValue'] ") - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MY_FLAGS}") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MY_FLAGS}") -set(CMAKE_EXECUTBLE_LINKER_FLAGS "${CMAKE_EXECUTBLE_LINKER_FLAGS} ${MY_FLAGS}") - -if (NOT CMAKE_EXECUTABLE_SUFFIX STREQUAL ".js") - message(FATAL_ERROR "The default suffix for building executables should be .js!") -endif() -# set(CMAKE_EXECUTABLE_SUFFIX ".html") - -add_executable(sherpa-onnx-wasm-main sherpa-onnx-wasm-main.cc) -target_link_libraries(sherpa-onnx-wasm-main sherpa-onnx-c-api) -install(TARGETS sherpa-onnx-wasm-main DESTINATION bin/wasm) - -install( - FILES - "$/sherpa-onnx-wasm-main.js" - "index.html" - "sherpa-onnx.js" - "app.js" - "$/sherpa-onnx-wasm-main.wasm" - "$/sherpa-onnx-wasm-main.data" - # "$/sherpa-onnx-wasm-main.html" - DESTINATION - bin/wasm -) diff --git a/wasm/tts/CMakeLists.txt b/wasm/tts/CMakeLists.txt new file mode 100644 index 000000000..2b5cf41f5 --- /dev/null +++ b/wasm/tts/CMakeLists.txt @@ -0,0 +1,58 @@ +if(NOT $ENV{SHERPA_ONNX_IS_USING_BUILD_WASM_SH}) + message(FATAL_ERROR "Please use ./build-wasm-simd-tts.sh to build for wasm TTS") +endif() + +if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/assets/model.onnx") + message(FATAL_ERROR "Please read ${CMAKE_CURRENT_SOURCE_DIR}/assets/README.md before you continue") +endif() + +set(exported_functions + MyPrint + SherpaOnnxCreateOfflineTts + SherpaOnnxDestroyOfflineTts + SherpaOnnxDestroyOfflineTtsGeneratedAudio + SherpaOnnxOfflineTtsGenerate + SherpaOnnxOfflineTtsGenerateWithCallback + SherpaOnnxOfflineTtsNumSpeakers + SherpaOnnxOfflineTtsSampleRate + SherpaOnnxWriteWave +) +set(mangled_exported_functions) +foreach(x IN LISTS exported_functions) + list(APPEND mangled_exported_functions "_${x}") +endforeach() +list(JOIN mangled_exported_functions "," all_exported_functions) + + +include_directories(${CMAKE_SOURCE_DIR}) +set(MY_FLAGS " -s FORCE_FILESYSTEM=1 -s INITIAL_MEMORY=512MB -s ALLOW_MEMORY_GROWTH=1") +string(APPEND MY_FLAGS " -sSTACK_SIZE=10485760 ") # 10MB +string(APPEND MY_FLAGS " -sEXPORTED_FUNCTIONS=[_CopyHeap,_malloc,_free,${all_exported_functions}] ") +string(APPEND MY_FLAGS "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/assets@. ") +string(APPEND MY_FLAGS " -sEXPORTED_RUNTIME_METHODS=['ccall','stringToUTF8','setValue','getValue'] ") + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MY_FLAGS}") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MY_FLAGS}") +set(CMAKE_EXECUTBLE_LINKER_FLAGS "${CMAKE_EXECUTBLE_LINKER_FLAGS} ${MY_FLAGS}") + +if (NOT CMAKE_EXECUTABLE_SUFFIX STREQUAL ".js") + message(FATAL_ERROR "The default suffix for building executables should be .js!") +endif() +# set(CMAKE_EXECUTABLE_SUFFIX ".html") + +add_executable(sherpa-onnx-wasm-main sherpa-onnx-wasm-main.cc) +target_link_libraries(sherpa-onnx-wasm-main sherpa-onnx-c-api) +install(TARGETS sherpa-onnx-wasm-main DESTINATION bin/wasm) + +install( + FILES + "$/sherpa-onnx-wasm-main.js" + "index.html" + "sherpa-onnx.js" + "app.js" + "$/sherpa-onnx-wasm-main.wasm" + "$/sherpa-onnx-wasm-main.data" + # "$/sherpa-onnx-wasm-main.html" + DESTINATION + bin/wasm +) diff --git a/wasm/app.js b/wasm/tts/app.js similarity index 100% rename from wasm/app.js rename to wasm/tts/app.js diff --git a/wasm/assets/.gitignore b/wasm/tts/assets/.gitignore similarity index 100% rename from wasm/assets/.gitignore rename to wasm/tts/assets/.gitignore diff --git a/wasm/assets/README.md b/wasm/tts/assets/README.md similarity index 97% rename from wasm/assets/README.md rename to wasm/tts/assets/README.md index 3381cd05d..41d5eb822 100644 --- a/wasm/assets/README.md +++ b/wasm/tts/assets/README.md @@ -18,7 +18,7 @@ rm -rf vits-piper-en_US-libritts_r-medium ``` You should have the following files in `assets` before you can run -`build-wasm-simd.sh` +`build-wasm-simd-tts.sh` ``` assets fangjun$ tree -L 1 diff --git a/wasm/index.html b/wasm/tts/index.html similarity index 100% rename from wasm/index.html rename to wasm/tts/index.html diff --git a/wasm/sherpa-onnx-wasm-main.cc b/wasm/tts/sherpa-onnx-wasm-main.cc similarity index 100% rename from wasm/sherpa-onnx-wasm-main.cc rename to wasm/tts/sherpa-onnx-wasm-main.cc diff --git a/wasm/sherpa-onnx.js b/wasm/tts/sherpa-onnx.js similarity index 100% rename from wasm/sherpa-onnx.js rename to wasm/tts/sherpa-onnx.js