alphacep / vosk-api

Offline speech recognition API for Android, iOS, Raspberry Pi and servers with Python, Java, C# and Node
Apache License 2.0
8k stars 1.11k forks source link

Build Vosk with Kaldi using MKL #583

Closed joshuar9 closed 2 years ago

joshuar9 commented 3 years ago

I wanted to build Vosk using Kaldi binaries built with the MKL math library. I read that MKL has a more performant version of BLAS and Lapack on intel architectures, compared to OpenBLAS and clapack.

I built vosk in the src directory using

KALDI_MKL=$KALDI_MKL KALDI_ROOT=$KALDI_ROOT make -j $(nproc)

where $KALDI_MKL=1 and $KALDI_ROOT is the root directory for kaldi

Then I did pip install -e . in the python directory and it didn't throw any errors.

But when I tried to use the vosk python api in the vosk-server project https://github.com/alphacep/vosk-server by, for example, running the websocket server example, I got this error:

(vosk) [ec2-user@ip-172-31-39-167 websocket]$ ./asr_server.py
Traceback (most recent call last):
  File "/home/ec2-user/vosk-server/websocket/./asr_server.py", line 11, in <module>
    from vosk import Model, KaldiRecognizer
  File "/home/ec2-user/vosk-api/python/vosk/__init__.py", line 21, in <module>
    _c = open_dll()
  File "/home/ec2-user/vosk-api/python/vosk/__init__.py", line 15, in open_dll
    return _ffi.dlopen(os.path.join(dlldir, "libvosk.so"))
OSError: cannot load library '/home/ec2-user/vosk-api/python/vosk/libvosk.so': /home/ec2-user/vosk-api/python/vosk/libvosk.so: undefined symbol: vsPowx

I saw this issue https://github.com/alphacep/vosk-api/issues/188 where someone was trying to build the c api, and they were told to link to the MKL libraries.

I'm not sure how to do that with python. Searching some more, I also found commit https://github.com/alphacep/vosk-api/commit/31bb0557d9f2a98c8672bbe9013e419d05eee8dc

But I was a bit confused because the latest python/setup.py doesn't mention anything about MKL.

I wanted to know:

  1. Was MKL support removed?
  2. Are there issues using MKL that make it less preferable to OpenBLAS/Clapack?
  3. Assuming there aren't huge issues, how can I build the python api so I can use the Kaldi binaries with MKL?

I appreciate any insight you can offer.

nshmyrev commented 3 years ago

Was MKL support removed?

No, it is still working

Are there issues using MKL that make it less preferable to OpenBLAS/Clapack?

MKL is slow on AMD, it is not universal enough. Also, it needs LD_PRELOAD as below.

Assuming there aren't huge issues, how can I build the python api so I can use the Kaldi binaries with MKL?

You need something like LD_PRELOAD=$MKLROOT/lib/intel64/libmkl_core.so python when you run python, it is pretty widely covered on the web.

nshmyrev commented 2 years ago

This more or less works now, I made a changes to improve linking, you just have to specify HAVE_MKL=1 HAVE_OPENBLAS_CLAPACK=0 in makefile.