Skip to content

Commit 72ea103

Browse files
authored
Add WebAssembly (#300)
1 parent 4c00ac7 commit 72ea103

13 files changed

+941
-6
lines changed
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
name: wasm-simd
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- '.github/workflows/build-wasm-simd.yaml'
9+
- 'CMakeLists.txt'
10+
- 'cmake/**'
11+
- 'sherpa-ncnn/csrc/*'
12+
- 'wasm/*'
13+
pull_request:
14+
branches:
15+
- master
16+
paths:
17+
- '.github/workflows/build-wasm-simd.yaml'
18+
- 'CMakeLists.txt'
19+
- 'cmake/**'
20+
- 'sherpa-ncnn/csrc/*'
21+
- 'wasm/*'
22+
23+
release:
24+
types:
25+
- published
26+
27+
workflow_dispatch:
28+
inputs:
29+
release:
30+
description: "Whether to release"
31+
type: boolean
32+
33+
env:
34+
RELEASE:
35+
|- # Release if there is a release tag name or a release flag in workflow_dispatch
36+
${{ github.event.release.tag_name != '' || github.event.inputs.release == 'true' }}
37+
38+
concurrency:
39+
group: linux-${{ github.ref }}
40+
cancel-in-progress: true
41+
42+
jobs:
43+
wasm-simd:
44+
runs-on: ${{ matrix.os }}
45+
strategy:
46+
fail-fast: false
47+
matrix:
48+
os: [ubuntu-latest]
49+
50+
steps:
51+
- uses: actions/checkout@v4
52+
with:
53+
fetch-depth: 0
54+
- name: Install emsdk
55+
uses: mymindstorm/setup-emsdk@v14
56+
57+
- name: View emsdk version
58+
shell: bash
59+
run: |
60+
emcc -v
61+
echo "--------------------"
62+
emcc --check
63+
64+
- name: Download model files
65+
shell: bash
66+
run: |
67+
cd wasm/assets
68+
ls -lh
69+
echo "----------"
70+
wget -q https://github.com/k2-fsa/sherpa-ncnn/releases/download/models/sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2
71+
tar xvf sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2
72+
mv -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13/*pnnx.ncnn.param .
73+
mv -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13/*pnnx.ncnn.bin .
74+
mv -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13/tokens.txt .
75+
76+
rm -rf sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13
77+
rm -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2
78+
79+
ls -lh
80+
81+
- name: Build sherpa-ncnn for WebAssembly
82+
shell: bash
83+
run: |
84+
./build-wasm-simd.sh
85+
86+
- name: collect files
87+
shell: bash
88+
run: |
89+
SHERPA_NCNN_VERSION=v$(grep "SHERPA_NCNN_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
90+
91+
mv build-wasm-simd/install/bin/wasm sherpa-ncnn-wasm-simd
92+
ls -lh sherpa-ncnn-wasm-simd
93+
tar cjfv sherpa-ncnn-wasm-simd-${SHERPA_NCNN_VERSION}.tar.bz2 ./sherpa-ncnn-wasm-simd
94+
95+
- name: Upload wasm files
96+
uses: actions/upload-artifact@v4
97+
with:
98+
name: sherpa-ncnn-wasm-simd
99+
path: ./sherpa-ncnn-wasm-simd-*.tar.bz2
100+
101+
- name: Release wasm files
102+
if: env.RELEASE == 'true'
103+
uses: svenstaro/upload-release-action@v2
104+
with:
105+
file_glob: true
106+
overwrite: true
107+
file: ./sherpa-ncnn-wasm-simd*.tar.bz2

.github/workflows/go.yaml

+10-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,18 @@ jobs:
4848
go env GOPATH
4949
go env GOARCH
5050
51-
- name: Set up MinGW
52-
if: matrix.os == 'windows-latest'
53-
uses: egor-tensin/setup-mingw@v2
51+
- name: Set up MinGW for x64
52+
if: matrix.os == 'windows-latest' && matrix.arch == 'x64'
53+
uses: csukuangfj/[email protected]
54+
with:
55+
platform: ${{ matrix.arch }}
56+
57+
- name: Set up MinGW for x86
58+
if: matrix.os == 'windows-latest' && matrix.arch == 'x86'
59+
uses: csukuangfj/[email protected]
5460
with:
5561
platform: ${{ matrix.arch }}
62+
version: '12.2.0'
5663

5764
- name: Show gcc
5865
if: matrix.os == 'windows-latest'

.github/workflows/test-pip-install.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
fail-fast: false
2828
matrix:
2929
os: [ubuntu-latest, macos-latest, windows-latest]
30-
python-version: ["3.7", "3.8", "3.9", "3.10"]
30+
python-version: ["3.8", "3.9", "3.10", "3.11"]
3131

3232
steps:
3333
- uses: actions/checkout@v2

CMakeLists.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
22
project(sherpa-ncnn)
33

4-
set(SHERPA_NCNN_VERSION "2.1.6")
4+
set(SHERPA_NCNN_VERSION "2.1.7")
55

66
# Disable warning about
77
#
@@ -40,6 +40,7 @@ option(SHERPA_NCNN_ENABLE_JNI "Whether to build JNI internface" OFF)
4040
option(SHERPA_NCNN_ENABLE_BINARY "Whether to build the binary sherpa-ncnn" ON)
4141
option(SHERPA_NCNN_ENABLE_TEST "Whether to build tests" OFF)
4242
option(SHERPA_NCNN_ENABLE_C_API "Whether to build C API" ON)
43+
option(SHERPA_NCNN_ENABLE_WASM "Whether to enable WASM" OFF)
4344
option(SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE "Whether to generate-int8-scale-table" ON)
4445
option(SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES "Whether to enable ffmpeg-examples" OFF)
4546

@@ -137,7 +138,6 @@ if(WIN32 AND MSVC)
137138
endforeach()
138139
endif()
139140

140-
141141
include(kaldi-native-fbank)
142142
include(ncnn)
143143

@@ -159,6 +159,9 @@ if(SHERPA_NCNN_ENABLE_C_API AND SHERPA_NCNN_ENABLE_BINARY)
159159
add_subdirectory(c-api-examples)
160160
endif()
161161

162+
if(SHERPA_NCNN_ENABLE_WASM)
163+
add_subdirectory(wasm)
164+
endif()
162165

163166
set(SHERPA_NCNN_PKG_CONFIG_EXTRA_LIBS)
164167

build-wasm-simd.sh

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) 2024 Xiaomi Corporation
3+
#
4+
# This script is to build sherpa-ncnn for WebAssembly
5+
#
6+
# See also
7+
# https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-webassembly
8+
#
9+
# Please refer to
10+
# https://k2-fsa.github.io/sherpa/ncnn/wasm/index.html
11+
# for more details.
12+
13+
set -ex
14+
15+
if [ x"$EMSCRIPTEN" == x"" ]; then
16+
if ! command -v emcc &> /dev/null; then
17+
echo "Please install emscripten first"
18+
echo ""
19+
echo "You can use the following commands to install it:"
20+
echo ""
21+
echo "git clone https://github.com/emscripten-core/emsdk.git"
22+
echo "cd emsdk"
23+
echo "git pull"
24+
echo "./emsdk install latest"
25+
echo "./emsdk activate latest"
26+
echo "source ./emsdk_env.sh"
27+
exit 1
28+
else
29+
EMSCRIPTEN=$(dirname $(realpath $(which emcc)))
30+
fi
31+
fi
32+
33+
export EMSCRIPTEN=$EMSCRIPTEN
34+
echo "EMSCRIPTEN: $EMSCRIPTEN"
35+
if [ ! -f $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake ]; then
36+
echo "Cannot find $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake"
37+
echo "Please make sure you have installed emsdk correctly"
38+
exit 1
39+
fi
40+
41+
mkdir -p build-wasm-simd
42+
pushd build-wasm-simd
43+
44+
export SHERPA_NCNN_IS_USING_BUILD_WASM_SH=ON
45+
46+
cmake \
47+
-DCMAKE_INSTALL_PREFIX=./install \
48+
-DCMAKE_BUILD_TYPE=Release \
49+
-DCMAKE_TOOLCHAIN_FILE=$EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake \
50+
-DNCNN_THREADS=OFF \
51+
-DNCNN_OPENMP=OFF \
52+
-DNCNN_SIMPLEOMP=OFF \
53+
-DNCNN_RUNTIME_CPU=OFF \
54+
-DNCNN_SSE2=ON \
55+
-DNCNN_AVX2=OFF \
56+
-DNCNN_AVX=OFF \
57+
-DNCNN_BUILD_TOOLS=OFF \
58+
-DNCNN_BUILD_EXAMPLES=OFF \
59+
-DNCNN_BUILD_BENCHMARK=OFF \
60+
\
61+
-DSHERPA_NCNN_ENABLE_WASM=ON \
62+
-DBUILD_SHARED_LIBS=OFF \
63+
-DSHERPA_NCNN_ENABLE_PYTHON=OFF \
64+
-DSHERPA_NCNN_ENABLE_PORTAUDIO=OFF \
65+
-DSHERPA_NCNN_ENABLE_JNI=OFF \
66+
-DSHERPA_NCNN_ENABLE_BINARY=OFF \
67+
-DSHERPA_NCNN_ENABLE_TEST=OFF \
68+
-DSHERPA_NCNN_ENABLE_C_API=ON \
69+
-DSHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE=OFF \
70+
-DSHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES=OFF \
71+
..
72+
73+
make -j2
74+
make install
75+
ls -lh install/bin/wasm

wasm/CMakeLists.txt

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
if(NOT $ENV{SHERPA_NCNN_IS_USING_BUILD_WASM_SH})
2+
message(FATAL_ERROR "Please use ./build-wasm.sh to build for wasm")
3+
endif()
4+
5+
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/assets/decoder_jit_trace-pnnx.ncnn.bin")
6+
message(WARNING "${CMAKE_CURRENT_SOURCE_DIR}/assets/decoder_jit_trace-pnnx.ncnn.bin does not exist")
7+
message(FATAL_ERROR "Please read ${CMAKE_CURRENT_SOURCE_DIR}/assets/README.md before you continue")
8+
endif()
9+
10+
set(exported_functions
11+
AcceptWaveform
12+
CreateRecognizer
13+
CreateStream
14+
Decode
15+
DestroyRecognizer
16+
DestroyResult
17+
DestroyStream
18+
GetResult
19+
InputFinished
20+
IsEndpoint
21+
IsReady
22+
Reset
23+
)
24+
set(mangled_exported_functions)
25+
foreach(x IN LISTS exported_functions)
26+
list(APPEND mangled_exported_functions "_${x}")
27+
endforeach()
28+
29+
list(JOIN mangled_exported_functions "," all_exported_functions)
30+
31+
include_directories(${CMAKE_SOURCE_DIR})
32+
set(MY_FLAGS " -s FORCE_FILESYSTEM=1 -s INITIAL_MEMORY=512MB ")
33+
string(APPEND MY_FLAGS " -sEXPORTED_FUNCTIONS=[_CopyHeap,_malloc,_free,${all_exported_functions}] ")
34+
string(APPEND MY_FLAGS "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/assets@. ")
35+
string(APPEND MY_FLAGS " -sEXPORTED_RUNTIME_METHODS=['ccall','stringToUTF8','setValue','getValue'] ")
36+
message(STATUS "MY_FLAGS: ${MY_FLAGS}")
37+
38+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MY_FLAGS}")
39+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MY_FLAGS}")
40+
set(CMAKE_EXECUTBLE_LINKER_FLAGS "${CMAKE_EXECUTBLE_LINKER_FLAGS} ${MY_FLAGS}")
41+
42+
add_executable(sherpa-ncnn-wasm-main sherpa-ncnn-wasm-main.cc)
43+
target_link_libraries(sherpa-ncnn-wasm-main sherpa-ncnn-core sherpa-ncnn-c-api)
44+
install(TARGETS sherpa-ncnn-wasm-main DESTINATION bin/wasm)
45+
46+
install(
47+
FILES
48+
"sherpa-ncnn.js"
49+
"app.js"
50+
"index.html"
51+
"$<TARGET_FILE_DIR:sherpa-ncnn-wasm-main>/sherpa-ncnn-wasm-main.js"
52+
"$<TARGET_FILE_DIR:sherpa-ncnn-wasm-main>/sherpa-ncnn-wasm-main.wasm"
53+
"$<TARGET_FILE_DIR:sherpa-ncnn-wasm-main>/sherpa-ncnn-wasm-main.data"
54+
DESTINATION
55+
bin/wasm
56+
)

0 commit comments

Comments
 (0)