Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

New compilation guide for Kaldi to WASM

License

NotificationsYou must be signed in to change notification settings

msqr1/kaldi-wasm2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Overview

  • There isan older guide for this that compiled Kaldi withCLAPACK (not properly tested) andNetlib's Reference BLAS, which exists solely for implementers of the BLAS standard to verify the correctness of their own implementation, and thus, not optimized for performance.

  • This newer guide compile Kaldi usingOpenBLAS, a high-performance, optimized, actively maintained BLAS library officially supported by Kaldi. It also includes both BLAS and LAPACK. This was profiled to speed up Kaldi by around 20% compared to the build with Netlib's Reference BLAS and CLAPACK.

  • This guide compiles Kaldi's "online decoding" targetonline2 More info:https://kaldi-asr.org/doc/online_decoding.html

  • Guide version will be updated to follow Emscripten/OpenFST/OpenBLAS version change

Current versions

  • Emscripten: 3.1.69
  • OpenFST: 1.8.4
  • OpenBLAS: 0.3.28
  • Kaldi: ???

Step 1: Prepare

  • Execute all commands in one terminal
  • Make a directory and change into it before starting
  • Compilation optimization level is -O3 by default througout this guide
# Our build root, exported so that we can refer to it in this terminalexport ROOT="$PWD"# Clone this repositorygit clone --depth 1 https://github.com/msqr1/kaldi-wasm2 "$ROOT"# Enter that directorycd "$ROOT"
  • Optional: WASM-specific compilation flags that can boost performance, selected by me with careful condsideration on browser support:
    • Chrome ≥ 75 (2019)
    • Firefox ≥ 79 (2020)
    • Safari ≥ 15 (2021)
    • Edge ≥ 79 (2020)
  • Wasm features support table
  • Wasm flags list
export WAFLAGS="-mbulk-memory -mnontrapping-fptoint -mmutable-globals -msign-ext"

Step 2: Emscripten

  • As of writing, Emscripten 3.1.6x is known to work
  • Referencethis guide to setup Emscripten, the WASM compiler
# Get EMSDKgit clone https://github.com/emscripten-core/emsdk.git# Enter that directorycd emsdk# Install Emscripten./emsdk install 3.1.69# Activate Emcripten./emsdk activate 3.1.69# Add Emscripten tools to current terminalsource ./emsdk_env.sh

Step 3: OpenFST

# Go to build rootcd "$ROOT"# Get OpenFSTwget https://www.openfst.org/twiki/pub/FST/FstDownload/openfst-1.8.4.tar.gz -O openfst.tgz# Decompressmkdir openfsttar -xzf openfst.tgz -C openfst --strip-component 1# Enter that directorycd openfst# OpenFST bug fixes# Invalid assignmentpatch -i "$ROOT"/patches/openfst/fst.h.patch ./src/include/fst/fst.h# Undefined member variablepatch -i "$ROOT"/patches/openfst/bi-table.h.patch ./src/include/fst/bi-table.h# Configuring static OpenFST with Ngram fstsCXXFLAGS="$WAFLAGS -O3 -fno-rtti" emconfigure ./configure --prefix="$ROOT/openfst-build" --enable-static --disable-shared --enable-ngram-fsts --disable-bin# Compile and install into prefixemmake make -j$(nproc) install > /dev/null

Step 4: OpenBLAS

  • The most crucial step of the build, some hacks are required for this to work
# Go to build rootcd "$ROOT"# Get OpenBLASgit clone -b v0.3.28 --depth=1 https://github.com/OpenMathLib/OpenBLAS openblas# Enter that directorycd openblas# OpenBLAS hacks: remove march'es, fPIC, etc.patch -i "$ROOT"/patches/openblas/Makefile.riscv64.patch Makefile.riscv64patch -i "$ROOT"/patches/openblas/Makefile.prebuild.patch Makefile.prebuildpatch -i "$ROOT"/patches/openblas/Makefile.system.patch Makefile.system# Make single-threaded static OpenBLAS with just single and double precision# Change HOSTCC to the C compiler on your machine. Mine is gcc-12 from Debian 12CC=emcc HOSTCC=gcc-12 TARGET=RISCV64_GENERIC USE_THREAD=0 NO_SHARED=1 BINARY=32 BUILD_SINGLE=1 BUILD_DOUBLE=1 BUILD_BFLOAT16=0 BUILD_COMPLEX16=0 BUILD_COMPLEX=0 CFLAGS="$WAFLAGS -fno-exceptions -fno-rtti" make -j$(nproc) > /dev/null# Install into prefixPREFIX="$ROOT/openblas-build" NO_SHARED=1 make install

Step 5: Kaldi

# Go to build rootcd "$ROOT"# Get Kaldigit clone --depth 1 https://github.com/kaldi-asr/kaldi# Enter Kaldi sourcecd kaldi/src# Configuring static Kaldi with static installed dependenciesCXXFLAGS="$WAFLAGS -UHAVE_EXECINFO_H -g0 -O3 -msimd128" emconfigure ./configure --use-cuda=no --with-cudadecoder=no --static --static-math --static-fst --fst-root="$ROOT/openfst-build" --fst-version='1.8.4' --openblas-root="$ROOT/openblas-build" --host=WASM# Compile our targetmake -j$(nproc) online2 > /dev/null

Step 6: Clean up

# Go to build rootcd "$ROOT"# Clean uprm -rf openfst.tgz openfst openblas

Full command

export ROOT="$PWD"git clone --depth 1 https://github.com/msqr1/kaldi-wasm2 "$ROOT"cd "$ROOT"git clone --depth 1 https://github.com/emscripten-core/emsdk.gitcd emsdk./emsdk install 3.1.69./emsdk activate 3.1.69source emsdk_env.shcd "$ROOT"export WAFLAGS="-mbulk-memory -mnontrapping-fptoint -mmutable-globals -msign-ext"wget https://www.openfst.org/twiki/pub/FST/FstDownload/openfst-1.8.4.tar.gz -O openfst.tgzmkdir openfsttar -xzf openfst.tgz -C openfst --strip-component 1cd openfstCXXFLAGS="$WAFLAGS -O3 -fno-rtti" emconfigure ./configure --prefix="$ROOT/openfst-build" --enable-static --disable-shared --enable-ngram-fsts --disable-binemmake make -j$(nproc) install > /dev/nullcd "$ROOT"git clone -b v0.3.28 --depth=1 https://github.com/OpenMathLib/OpenBLAS openblascd openblaspatch -i "$ROOT"/patches/openblas/Makefile.riscv64.patch Makefile.riscv64patch -i "$ROOT"/patches/openblas/Makefile.prebuild.patch Makefile.prebuildpatch -i "$ROOT"/patches/openblas/Makefile.system.patch Makefile.systemCC=emcc HOSTCC=gcc-12 TARGET=RISCV64_GENERIC USE_THREAD=0 NO_SHARED=1 BINARY=32 BUILD_SINGLE=1 BUILD_DOUBLE=1 BUILD_BFLOAT16=0 BUILD_COMPLEX16=0 BUILD_COMPLEX=0 CFLAGS="$WAFLAGS -fno-exceptions -fno-rtti" make -j$(nproc) > /dev/nullPREFIX="$ROOT/openblas-build" NO_SHARED=1 make installcd "$ROOT"git clone --depth 1 https://github.com/kaldi-asr/kaldicd kaldi/srcCXXFLAGS="$WAFLAGS -UHAVE_EXECINFO_H -g0 -O3 -msimd128" emconfigure ./configure --use-cuda=no --with-cudadecoder=no --static --static-math --static-fst --fst-root="$ROOT/openfst-build" --fst-version='1.8.4' --openblas-root="$ROOT/openblas-build" --host=WASMmake -j$(nproc) online2 > /dev/nullcd "$ROOT"rm -rf openfst.tgz openfst openblas

Linking and running example

  • We're going to runnnet2/am-nnet-test.cc
# Compiler flagsexport CXXFLAGS="-DOPENFST_VER=10803-I$ROOT/kaldi/src-I$ROOT/openfst-build/include-I$ROOT/openblas-build/include"# Linker flagsexport LDFLAGS="-L$ROOT/kaldi/src-l:online2/kaldi-online2.a        -l:decoder/kaldi-decoder.a-l:ivector/kaldi-ivector.a        -l:gmm/kaldi-gmm.a-l:tree/kaldi-tree.a              -l:feat/kaldi-feat.a-l:cudamatrix/kaldi-cudamatrix.a  -l:lat/kaldi-lat.a-l:hmm/kaldi-hmm.a                -l:nnet3/kaldi-nnet3.a-l:transform/kaldi-transform.a    -l:matrix/kaldi-matrix.a-l:fstext/kaldi-fstext.a          -l:util/kaldi-util.a-l:base/kaldi-base.a              -l:nnet2/kaldi-nnet2.a-L$ROOT/openfst-build/lib         -l:libfst.a-L$ROOT/openblas-build/lib        -l:libopenblas.a"# Go to build rootcd "$ROOT"# Generate .html, .js, and .wasmem++ $WAFLAGS -O3 $CXXFLAGS $LDFLAGS kaldi/src/nnet2/am-nnet-test.cc -o index.html
  • Open theindex.html in a browser, check the console for logs from the test. Note that file URL will not work, use 127.0.0.1 or something else.

About

New compilation guide for Kaldi to WASM

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp