K2FSA: Icefall Installation

Nadira Povey
2 min readJul 12, 2023

--

I wanted to run the code below that uses K2FSA: Icefall model to decode speech and as you can see the model ends with cpu_jit-torch-1.10.0.pt. That means we need PyTorch 1.10 or above. I have Linux machine Ubuntu 20.04.4 LTS.

  ./long_file_recog/recognize.py \
--world-size $world_size \
--num-workers 2 \
--nn-model-filename ./icefall-asr-librispeech-pruned-transducer-stateless7-2022-11-11/exp/cpu_jit-torch-1.10.0.pt \
--bpe-model data/lang_bpe_500/bpe.model \
--max-duration 2400 \
--decoding-method greedy_search
--master 12345

Step 1 Install Cuda 11.3

You can use the following commands to install CUDA 11.3. We install it into /ceph-sh1/fangjun/software/cuda-11.3.1. You can replace it if needed.

wget https://developer.download.nvidia.com/compute/cuda/11.3.1/local_installers/cuda_11.3.1_465.19.01_linux.run

chmod +x cuda_11.3.1_465.19.01_linux.run

./cuda_11.3.1_465.19.01_linux.run \
--silent \
--toolkit \
--installpath=/ceph-sh1/fangjun/software/cuda-11.3.1 \
--no-opengl-libs \
--no-drm \
--no-man-page

Install cuDNN for CUDA 11.3

Now, install cuDNN for CUDA 11.3.

wget https://huggingface.co/csukuangfj/cudnn/resolve/main/cudnn-11.3-linux-x64-v8.2.0.53.tgz
tar xvf cudnn-11.3-linux-x64-v8.2.0.53.tgz --strip-components=1 -C /ceph-sh1/fangjun/software/cuda-11.3.1

Set environment variables for CUDA 11.3

Note that we have to set the following environment variables after installing CUDA 11.3. You can save the following code to activate-cuda-11.3.sh and use source activate-cuda-11.3.sh if you want to activate CUDA 11.3.

export CUDA_HOME=/ceph-sh1/fangjun/software/cuda-11.3.1
export PATH=$CUDA_HOME/bin:$PATH
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$CUDA_HOME/lib:$LD_LIBRARY_PATH

export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME
export CUDA_TOOLKIT_ROOT=$CUDA_HOME
export CUDA_BIN_PATH=$CUDA_HOME
export CUDA_PATH=$CUDA_HOME
export CUDA_INC_PATH=$CUDA_HOME/targets/x86_64-linux
export CFLAGS=-I$CUDA_HOME/targets/x86_64-linux/include:$CFLAGS

Step 2: Create python environment

np@np-INTEL:~/anna$ python -m venv venv
np@np-INTEL:~/anna$ source venv/bin/activate

Step 3: Install torch, torchvision and torchaudio

Installation code is taken from https://pytorch.org/get-started/previous-versions/ by looking at previous versions of torch installations.

(venv) np@np-INTEL:~/anna$ pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113

Step 4: Install k2

I couldn’t install K2 from source so I actually picked k2 version that we can install using pip and did reverse engineering. Looking at k2 version I knew I needed cuda11.3 and torch 1.12.1. To find out all available k2 versions check the list at the end of this blog.

old way that doesn’t work:

wrong --> pip install k2==1.21.dev20221008+cuda11.3.torch1.12.1 -f https://k2-fsa.org/nightly/

If you are getting “AttributeError: module ‘k2’ has no attribute ‘swoosh_r’” you might need pip install again. In my case the pip install below corrected an error.

AttributeError: module 'k2' has no attribute 'swoosh_r'

new way that works https://github.com/k2-fsa/k2/issues/1232

pip install k2==1.24.3.dev20230725+cuda11.3.torch1.12.1 -f https://k2-fsa.github.io/k2/cuda.html

Step 5: Install lhotse

(venv) np@np-INTEL:~/anna$ pip install -q kaldialign sentencepiece>=0.1.96 lhotse

Step 6: Install Icefall

cd /tmp
git clone https://github.com/k2-fsa/icefall
cd icefall
pip install -r requirements.txt
export PYTHONPATH=/tmp/icefall:$PYTHONPATH

Please follow the testing code in this link to make sure that your installation was a success.

Step7: Install kaldifeat

pip install kaldifeat==1.25.0.dev20230726+cuda11.3.torch1.12.1 -f https://csukuangfj.github.io/kaldifeat/cuda.html

All available K2 installations are below or check https://k2-fsa.github.io/k2/cuda.html

--

--