Skip to content

Commit afcfb6b

Browse files
committed
[runtime] support raspberry pi
1 parent a1e801c commit afcfb6b

File tree

15 files changed

+76
-46
lines changed

15 files changed

+76
-46
lines changed

runtime/core/cmake/openfst.cmake

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,25 @@ if(NOT ANDROID)
55
set(WITH_GFLAGS OFF CACHE BOOL "whether build glog with gflags" FORCE)
66
include(glog)
77

8-
set(CONFIG_FLAGS "")
9-
if(NOT FST_HAVE_BIN)
10-
if(MSVC)
11-
set(HAVE_BIN OFF CACHE BOOL "Build the fst binaries" FORCE)
12-
else()
13-
set(CONFIG_FLAGS "--disable-bin")
14-
endif()
15-
endif()
16-
17-
if(MSVC)
18-
set(HAVE_SCRIPT OFF CACHE BOOL "Build the fstscript" FORCE)
19-
set(HAVE_COMPACT OFF CACHE BOOL "Build compact" FORCE)
20-
set(HAVE_CONST OFF CACHE BOOL "Build const" FORCE)
21-
set(HAVE_GRM OFF CACHE BOOL "Build grm" FORCE)
22-
set(HAVE_PDT OFF CACHE BOOL "Build pdt" FORCE)
23-
set(HAVE_MPDT OFF CACHE BOOL "Build mpdt" FORCE)
24-
set(HAVE_LINEAR OFF CACHE BOOL "Build linear" FORCE)
25-
set(HAVE_LOOKAHEAD OFF CACHE BOOL "Build lookahead" FORCE)
26-
set(HAVE_NGRAM OFF CACHE BOOL "Build ngram" FORCE)
27-
set(HAVE_SPECIAL OFF CACHE BOOL "Build special" FORCE)
8+
if(NOT GRAPH_TOOLS)
9+
set(HAVE_BIN OFF CACHE BOOL "Build the fst binaries" FORCE)
2810
endif()
11+
set(HAVE_SCRIPT OFF CACHE BOOL "Build the fstscript" FORCE)
12+
set(HAVE_COMPACT OFF CACHE BOOL "Build compact" FORCE)
13+
set(HAVE_CONST OFF CACHE BOOL "Build const" FORCE)
14+
set(HAVE_GRM OFF CACHE BOOL "Build grm" FORCE)
15+
set(HAVE_PDT OFF CACHE BOOL "Build pdt" FORCE)
16+
set(HAVE_MPDT OFF CACHE BOOL "Build mpdt" FORCE)
17+
set(HAVE_LINEAR OFF CACHE BOOL "Build linear" FORCE)
18+
set(HAVE_LOOKAHEAD OFF CACHE BOOL "Build lookahead" FORCE)
19+
set(HAVE_NGRAM OFF CACHE BOOL "Build ngram" FORCE)
20+
set(HAVE_SPECIAL OFF CACHE BOOL "Build special" FORCE)
2921

3022
# The original openfst uses GNU Build System to run configure and build.
3123
# So, we use "OpenFST port for Windows" to build openfst with cmake in Windows.
3224
# Openfst is compiled with glog/gflags to avoid log and flag conflicts with log and flags in wenet/libtorch.
3325
# To build openfst with gflags and glog, we comment out some vars of {flags, log}.h and flags.cc.
3426
set(openfst_SOURCE_DIR ${fc_base}/openfst-src CACHE PATH "OpenFST source directory")
35-
set(openfst_BINARY_DIR ${fc_base}/openfst-build CACHE PATH "OpenFST build directory")
36-
set(openfst_PREFIX_DIR ${fc_base}/openfst-subbuild/openfst-populate-prefix CACHE PATH "OpenFST prefix directory")
37-
if(NOT MSVC)
38-
ExternalProject_Add(openfst
39-
URL https://github.com/mjansche/openfst/archive/1.6.5.zip
40-
URL_HASH SHA256=b720357a464f42e181d7e33f60867b54044007f50baedc8f4458a3926f4a5a78
41-
PREFIX ${openfst_PREFIX_DIR}
42-
SOURCE_DIR ${openfst_SOURCE_DIR}
43-
BINARY_DIR ${openfst_BINARY_DIR}
44-
CONFIGURE_COMMAND ${openfst_SOURCE_DIR}/configure ${CONFIG_FLAGS} --prefix=${openfst_PREFIX_DIR}
45-
"CPPFLAGS=-I${gflags_BINARY_DIR}/include -I${glog_SOURCE_DIR}/src -I${glog_BINARY_DIR} ${TORCH_CXX_FLAGS}"
46-
"LDFLAGS=-L${gflags_BINARY_DIR} -L${glog_BINARY_DIR}"
47-
"LIBS=-lgflags_nothreads -lglog -lpthread"
48-
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/patch/openfst ${openfst_SOURCE_DIR}
49-
BUILD_COMMAND make -j$(nproc)
50-
)
51-
add_dependencies(openfst gflags glog)
52-
link_directories(${openfst_PREFIX_DIR}/lib)
53-
else()
54-
add_compile_options(/W0 /wd4244 /wd4267)
5527
FetchContent_Declare(openfst
5628
URL https://github.com/kkm000/openfst/archive/refs/tags/win/1.6.5.1.tar.gz
5729
URL_HASH SHA256=02c49b559c3976a536876063369efc0e41ab374be1035918036474343877046e
@@ -60,7 +32,6 @@ if(NOT ANDROID)
6032
FetchContent_MakeAvailable(openfst)
6133
add_dependencies(fst gflags glog)
6234
target_link_libraries(fst PUBLIC gflags_nothreads_static glog)
63-
endif()
6435
include_directories(${openfst_SOURCE_DIR}/src/include)
6536
else()
6637
set(openfst_BINARY_DIR ${build_DIR}/wenet-openfst-android-1.0.2.aar/jni)

runtime/libtorch/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
33
project(wenet VERSION 0.1)
44

55
option(CXX11_ABI "whether to use CXX11_ABI libtorch" OFF)
6-
option(FST_HAVE_BIN "whether to build fst binaries" OFF)
6+
option(GRAPH_TOOLS "whether to build TLG graph tools" OFF)
77
option(BUILD_TESTING "whether to build unit test" ON)
8-
option(GRAPH_TOOLS "whether to build TLG graph tools" ON)
98

109
option(GRPC "whether to build with gRPC" OFF)
1110
# TODO(Binbin Zhang): Change websocket to OFF since it depends on boost
@@ -48,9 +47,6 @@ include_directories(
4847

4948
# Build all libraries
5049
add_subdirectory(utils)
51-
if(NOT MSVC)
52-
add_dependencies(utils openfst)
53-
endif()
5450
add_subdirectory(frontend)
5551
add_subdirectory(post_processor)
5652
add_subdirectory(kaldi) # kaldi: wfst based decoder

runtime/raspberrypi/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../libtorch/CMakeLists.txt

runtime/raspberrypi/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# WeNet & Raspberry PI (Cross Compile)
2+
3+
* Step 1. Install cross compile tools in the PC.
4+
5+
``` sh
6+
sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
7+
```
8+
9+
Or download, and install the binaries from: https://releases.linaro.org/components/toolchain/binaries/latest-7
10+
11+
12+
* Step 2. Export your experiment model to ONNX by https://github.com/wenet-e2e/wenet/blob/main/wenet/bin/export_onnx_cpu.py
13+
14+
``` sh
15+
exp=exp # Change it to your experiment dir
16+
onnx_dir=onnx
17+
python -m wenet.bin.export_onnx_cpu \
18+
--config $exp/train.yaml \
19+
--checkpoint $exp/final.pt \
20+
--chunk_size 16 \
21+
--output_dir $onnx_dir \
22+
--num_decoding_left_chunks -1
23+
24+
# When it finishes, you can find `encoder.onnx(.quant)`, `ctc.onnx(.quant)`, and `decoder.onnx(.quant)` in the $onnx_dir respectively.
25+
# We use the quantified to speed up the inference, so rename it without the suffix `.quant`
26+
```
27+
28+
* Step 3. Build. The build requires cmake 3.14 or above. and Send the binary and libraries to Raspberry PI.
29+
30+
``` sh
31+
cmake -B build -DONNX=ON -DTORCH=OFF -DWEBSOCKET=OFF -DGRPC=OFF -DCMAKE_TOOLCHAIN_FILE=toolchains/aarch64-linux-gnu.toolchain.cmake
32+
cmake --build build
33+
scp build/bin/decoder_main pi@xxx.xxx.xxx:/path/to/wenet
34+
scp fc_base/onnxruntime-src/lib/libonnxruntime.so* pi@xxx.xxx.xxx:/path/to/wenet
35+
```
36+
37+
* Step 4. Testing, the RTF(real time factor) is shown in Raspberry PI's console.
38+
39+
``` sh
40+
cd /path/to/wenet
41+
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
42+
export GLOG_logtostderr=1
43+
export GLOG_v=2
44+
wav_path=your_test_wav_path
45+
onnx_dir=your_model_dir
46+
units=units.txt # Change it to your model units path
47+
./build/bin/decoder_main \
48+
--chunk_size 16 \
49+
--wav_path $wav_path \
50+
--onnx_dir $onnx_dir \
51+
--unit_path $units 2>&1 | tee log.txt
52+
```

runtime/raspberrypi/api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../core/api

runtime/raspberrypi/bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../core/bin

runtime/raspberrypi/cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../core/cmake

runtime/raspberrypi/decoder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../core/decoder

runtime/raspberrypi/frontend

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../core/frontend

runtime/raspberrypi/kaldi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../core/kaldi

0 commit comments

Comments
 (0)