Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support building Kaldi to WASM with OpenBLAS #4954

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ Platform specific notes
### Web Assembly

- Kaldi supports cross compiling for Web Assembly for in-browser execution
using [emscripten](https://emscripten.org/) and CLAPACK.
- See [this post](https://gitlab.inria.fr/kaldi.web/kaldi-wasm/-/wikis/build_details.md)
using [emscripten](https://emscripten.org) and OpenBLAS
- See [this repo](https://github.com/msqr1/kaldi-wasm2)
for a step-by-step description of the build process.
43 changes: 29 additions & 14 deletions src/configure
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,12 @@ if is_set $HOST; then
fi
else
TARGET_ARCH="$HOST"
if ! $static_fst || ! $static_math || $dynamic_kaldi; then
echo "WARNING: Dynamic libraries are not currently supported when compiling to WASM. Overriding --static, --static-math, and --static-fst."
fi
dynamic_kaldi=false
static_math=true
static_fst=true
fi

HOST_CXX="$HOST-c++"
Expand Down Expand Up @@ -1360,14 +1366,6 @@ or try another math library, e.g. --mathlib=OPENBLAS (Kaldi may be slower)."
** You can also use other matrix algebra libraries. For information, see:
** http://kaldi-asr.org/doc/matrixwrap.html"
fi
if [ -f $OPENBLASROOT/lib/libopenblas.so ]; then
OPENBLASLIBDIR=$OPENBLASROOT/lib
elif [ -f $OPENBLASROOT/lib64/libopenblas.so ]; then
# in REDHAT/CentOS package installs, the library is located here
OPENBLASLIBDIR=$OPENBLASROOT/lib64
else
failure "Expected to find the file $OPENBLASROOT/lib/libopenblas.so"
fi
if [ -f $OPENBLASROOT/include/cblas.h ] ; then
OPENBLASINCDIR=$OPENBLASROOT/include
elif [ -f $OPENBLASROOT/include/openblas/cblas.h ] ; then
Expand All @@ -1381,18 +1379,35 @@ or try another math library, e.g. --mathlib=OPENBLAS (Kaldi may be slower)."
echo "** if it is a package-based install)."
OPENBLASINCDIR="/usr/include"
fi
echo "Your math library seems to be OpenBLAS from $OPENBLASROOT. Configuring appropriately."
# TODO(kkm): Probably, OpenBLAS required libgfortran.so.3 at some point, but
# no longer does. *My* linker does not complain about a missing library, but
# is it safe to keep the reference if no longer required? Try to figure out
# how long ago the dependency was dropped.
if $static_math; then
if [ -f $OPENBLASROOT/lib/libopenblas.a ]; then
OPENBLASLIBDIR=$OPENBLASROOT/lib
else
failure "Expected to find the file $OPENBLASROOT/lib/libopenblas.a"
fi
echo "Configuring static OpenBlas since --static-math=yes"
OPENBLASLIBS="-L$OPENBLASLIBDIR -l:libopenblas.a -lgfortran"
OPENBLASLIBS="-L$OPENBLASLIBDIR -l:libopenblas.a"
# No Fortran for OpenBLAS
if [[ "$HOST" != WASM ]]; then
OPENBLASLIBS+="-lgfortran"
fi
else
if [ -f $OPENBLASROOT/lib/libopenblas.so ]; then
OPENBLASLIBDIR=$OPENBLASROOT/lib
elif [ -f $OPENBLASROOT/lib64/libopenblas.so ]; then
# in REDHAT/CentOS package installs, the library is located here
OPENBLASLIBDIR=$OPENBLASROOT/lib64
else
failure "Expected to find the file $OPENBLASROOT/lib/libopenblas.so"
fi
echo "Configuring dynamically loaded OpenBlas since --static-math=no (the default)"
OPENBLASLIBS="-L$OPENBLASLIBDIR -lopenblas -lgfortran -Wl,-rpath=$OPENBLASLIBDIR"
fi
echo "Your math library seems to be OpenBLAS from $OPENBLASROOT. Configuring appropriately."
# TODO(kkm): Probably, OpenBLAS required libgfortran.so.3 at some point, but
# no longer does. *My* linker does not complain about a missing library, but
# is it safe to keep the reference if no longer required? Try to figure out
# how long ago the dependency was dropped.
echo "OPENBLASINC = $OPENBLASINCDIR" >> kaldi.mk
echo "OPENBLASLIBS = $OPENBLASLIBS" >> kaldi.mk
echo >> kaldi.mk
Expand Down
3 changes: 2 additions & 1 deletion src/makefiles/linux_clapack.mk
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ ifeq ($(findstring clang,$(COMPILER)),clang)
CXXFLAGS += -Wno-mismatched-tags
endif

LDFLAGS = $(EXTRA_LDFLAGS) $(OPENFSTLDFLAGS) -rdynamic
LDFLAGS = $(EXTRA_LDFLAGS) $(OPENFSTLDFLAGS)
LDLIBS = $(EXTRA_LDLIBS) $(OPENFSTLIBS) $(CLAPACKLIBS) -lm -ldl

ifneq ($(ARCH), WASM)
LDFLAGS += -rdynamic
CXXFLAGS += -pthread
LDLIBS += -lpthread
endif
14 changes: 11 additions & 3 deletions src/makefiles/linux_openblas.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CXXFLAGS = -std=$(CXXLANGVERSION) -I.. -isystem $(OPENFSTINC) -O1 $(EXTRA_CXXFLA
-DOPENFST_VER=$(OPENFSTVER) \
-DKALDI_DOUBLEPRECISION=$(DOUBLE_PRECISION) \
-DHAVE_EXECINFO_H=1 -DHAVE_CXXABI_H -DHAVE_OPENBLAS -I$(OPENBLASINC) \
-msse -msse2 -pthread \
-msse -msse2 \
-g

ifeq ($(KALDI_FLAVOR), dynamic)
Expand All @@ -46,5 +46,13 @@ ifeq ($(findstring clang,$(COMPILER)),clang)
CXXFLAGS += -Wno-mismatched-tags
endif

LDFLAGS = $(EXTRA_LDFLAGS) $(OPENFSTLDFLAGS) -rdynamic
LDLIBS = $(EXTRA_LDLIBS) $(OPENFSTLIBS) $(OPENBLASLIBS) -lm -lpthread -ldl
LDFLAGS = $(EXTRA_LDFLAGS) $(OPENFSTLDFLAGS)
LDLIBS = $(EXTRA_LDLIBS) $(OPENFSTLIBS) $(OPENBLASLIBS) -lm -ldl

ifneq ($(ARCH), WASM)
CXXFLAGS += -pthread
LDLIBS += -lpthread
LDFLAGS += -rdynamic
else
CXXFLAGS += -DKALDI_WASM
endif
8 changes: 7 additions & 1 deletion src/util/kaldi-thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@
#include "util/kaldi-thread.h"

namespace kaldi {
int32 g_num_threads = 8; // Initialize this global variable.

int32 g_num_threads =
#ifdef KALDI_WASM
0 // Everything will run on the main thread for WASM
#else
8 // Initialize this global variable.
#endif
;
MultiThreadable::~MultiThreadable() {
// default implementation does nothing
}
Expand Down