Skip to content

Commit

Permalink
Add WebAssembly (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Feb 6, 2024
1 parent 4c00ac7 commit 72ea103
Show file tree
Hide file tree
Showing 13 changed files with 941 additions and 6 deletions.
107 changes: 107 additions & 0 deletions .github/workflows/build-wasm-simd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
name: wasm-simd

on:
push:
branches:
- master
paths:
- '.github/workflows/build-wasm-simd.yaml'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-ncnn/csrc/*'
- 'wasm/*'
pull_request:
branches:
- master
paths:
- '.github/workflows/build-wasm-simd.yaml'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-ncnn/csrc/*'
- 'wasm/*'

release:
types:
- published

workflow_dispatch:
inputs:
release:
description: "Whether to release"
type: boolean

env:
RELEASE:
|- # Release if there is a release tag name or a release flag in workflow_dispatch
${{ github.event.release.tag_name != '' || github.event.inputs.release == 'true' }}

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

jobs:
wasm-simd:
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/assets
ls -lh
echo "----------"
wget -q https://github.com/k2-fsa/sherpa-ncnn/releases/download/models/sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2
tar xvf sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2
mv -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13/*pnnx.ncnn.param .
mv -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13/*pnnx.ncnn.bin .
mv -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13/tokens.txt .
rm -rf sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13
rm -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2
ls -lh
- name: Build sherpa-ncnn for WebAssembly
shell: bash
run: |
./build-wasm-simd.sh
- name: collect files
shell: bash
run: |
SHERPA_NCNN_VERSION=v$(grep "SHERPA_NCNN_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
mv build-wasm-simd/install/bin/wasm sherpa-ncnn-wasm-simd
ls -lh sherpa-ncnn-wasm-simd
tar cjfv sherpa-ncnn-wasm-simd-${SHERPA_NCNN_VERSION}.tar.bz2 ./sherpa-ncnn-wasm-simd
- name: Upload wasm files
uses: actions/upload-artifact@v4
with:
name: sherpa-ncnn-wasm-simd
path: ./sherpa-ncnn-wasm-simd-*.tar.bz2

- name: Release wasm files
if: env.RELEASE == 'true'
uses: svenstaro/upload-release-action@v2
with:
file_glob: true
overwrite: true
file: ./sherpa-ncnn-wasm-simd*.tar.bz2
13 changes: 10 additions & 3 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,18 @@ jobs:
go env GOPATH
go env GOARCH
- name: Set up MinGW
if: matrix.os == 'windows-latest'
uses: egor-tensin/setup-mingw@v2
- name: Set up MinGW for x64
if: matrix.os == 'windows-latest' && matrix.arch == 'x64'
uses: csukuangfj/[email protected]
with:
platform: ${{ matrix.arch }}

- name: Set up MinGW for x86
if: matrix.os == 'windows-latest' && matrix.arch == 'x86'
uses: csukuangfj/[email protected]
with:
platform: ${{ matrix.arch }}
version: '12.2.0'

- name: Show gcc
if: matrix.os == 'windows-latest'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-pip-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v2
Expand Down
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(sherpa-ncnn)

set(SHERPA_NCNN_VERSION "2.1.6")
set(SHERPA_NCNN_VERSION "2.1.7")

# Disable warning about
#
Expand Down Expand Up @@ -40,6 +40,7 @@ option(SHERPA_NCNN_ENABLE_JNI "Whether to build JNI internface" OFF)
option(SHERPA_NCNN_ENABLE_BINARY "Whether to build the binary sherpa-ncnn" ON)
option(SHERPA_NCNN_ENABLE_TEST "Whether to build tests" OFF)
option(SHERPA_NCNN_ENABLE_C_API "Whether to build C API" ON)
option(SHERPA_NCNN_ENABLE_WASM "Whether to enable WASM" OFF)
option(SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE "Whether to generate-int8-scale-table" ON)
option(SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES "Whether to enable ffmpeg-examples" OFF)

Expand Down Expand Up @@ -137,7 +138,6 @@ if(WIN32 AND MSVC)
endforeach()
endif()


include(kaldi-native-fbank)
include(ncnn)

Expand All @@ -159,6 +159,9 @@ if(SHERPA_NCNN_ENABLE_C_API AND SHERPA_NCNN_ENABLE_BINARY)
add_subdirectory(c-api-examples)
endif()

if(SHERPA_NCNN_ENABLE_WASM)
add_subdirectory(wasm)
endif()

set(SHERPA_NCNN_PKG_CONFIG_EXTRA_LIBS)

Expand Down
75 changes: 75 additions & 0 deletions build-wasm-simd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env bash
# Copyright (c) 2024 Xiaomi Corporation
#
# This script is to build sherpa-ncnn for WebAssembly
#
# See also
# https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-webassembly
#
# Please refer to
# https://k2-fsa.github.io/sherpa/ncnn/wasm/index.html
# for more details.

set -ex

if [ x"$EMSCRIPTEN" == x"" ]; then
if ! command -v emcc &> /dev/null; then
echo "Please install emscripten first"
echo ""
echo "You can use the following commands to install it:"
echo ""
echo "git clone https://github.com/emscripten-core/emsdk.git"
echo "cd emsdk"
echo "git pull"
echo "./emsdk install latest"
echo "./emsdk activate latest"
echo "source ./emsdk_env.sh"
exit 1
else
EMSCRIPTEN=$(dirname $(realpath $(which emcc)))
fi
fi

export EMSCRIPTEN=$EMSCRIPTEN
echo "EMSCRIPTEN: $EMSCRIPTEN"
if [ ! -f $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake ]; then
echo "Cannot find $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake"
echo "Please make sure you have installed emsdk correctly"
exit 1
fi

mkdir -p build-wasm-simd
pushd build-wasm-simd

export SHERPA_NCNN_IS_USING_BUILD_WASM_SH=ON

cmake \
-DCMAKE_INSTALL_PREFIX=./install \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=$EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake \
-DNCNN_THREADS=OFF \
-DNCNN_OPENMP=OFF \
-DNCNN_SIMPLEOMP=OFF \
-DNCNN_RUNTIME_CPU=OFF \
-DNCNN_SSE2=ON \
-DNCNN_AVX2=OFF \
-DNCNN_AVX=OFF \
-DNCNN_BUILD_TOOLS=OFF \
-DNCNN_BUILD_EXAMPLES=OFF \
-DNCNN_BUILD_BENCHMARK=OFF \
\
-DSHERPA_NCNN_ENABLE_WASM=ON \
-DBUILD_SHARED_LIBS=OFF \
-DSHERPA_NCNN_ENABLE_PYTHON=OFF \
-DSHERPA_NCNN_ENABLE_PORTAUDIO=OFF \
-DSHERPA_NCNN_ENABLE_JNI=OFF \
-DSHERPA_NCNN_ENABLE_BINARY=OFF \
-DSHERPA_NCNN_ENABLE_TEST=OFF \
-DSHERPA_NCNN_ENABLE_C_API=ON \
-DSHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE=OFF \
-DSHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES=OFF \
..

make -j2
make install
ls -lh install/bin/wasm
56 changes: 56 additions & 0 deletions wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
if(NOT $ENV{SHERPA_NCNN_IS_USING_BUILD_WASM_SH})
message(FATAL_ERROR "Please use ./build-wasm.sh to build for wasm")
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
AcceptWaveform
CreateRecognizer
CreateStream
Decode
DestroyRecognizer
DestroyResult
DestroyStream
GetResult
InputFinished
IsEndpoint
IsReady
Reset
)
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 ")
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'] ")
message(STATUS "MY_FLAGS: ${MY_FLAGS}")

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}")

add_executable(sherpa-ncnn-wasm-main sherpa-ncnn-wasm-main.cc)
target_link_libraries(sherpa-ncnn-wasm-main sherpa-ncnn-core sherpa-ncnn-c-api)
install(TARGETS sherpa-ncnn-wasm-main DESTINATION bin/wasm)

install(
FILES
"sherpa-ncnn.js"
"app.js"
"index.html"
"$<TARGET_FILE_DIR:sherpa-ncnn-wasm-main>/sherpa-ncnn-wasm-main.js"
"$<TARGET_FILE_DIR:sherpa-ncnn-wasm-main>/sherpa-ncnn-wasm-main.wasm"
"$<TARGET_FILE_DIR:sherpa-ncnn-wasm-main>/sherpa-ncnn-wasm-main.data"
DESTINATION
bin/wasm
)
Loading

0 comments on commit 72ea103

Please sign in to comment.