# How to train KenLM no AWS Sagemaker?

**URL:** <https://discuss.huggingface.co/t/how-to-train-kenlm-no-aws-sagemaker/18444>\
**Category:** Amazon SageMaker\
**Created:** [May 27, 2022, 8:40pm UTC](https://discuss.huggingface.co/t/how-to-train-kenlm-no-aws-sagemaker/18444 "2022-05-27T20:40:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierreguillou](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/pierreguillou/32/19626_2.png) [@pierreguillou](https://discuss.huggingface.co/u/pierreguillou)\
**Post date:** [May 27, 2022, 8:40pm UTC](https://discuss.huggingface.co/t/how-to-train-kenlm-no-aws-sagemaker/18444/1 "2022-05-27T20:40:16Z")

</div>

HI,

Within the blog post of @patrickvonplaten [Boosting Wav2Vec2 with n-grams in 🤗 Transformers](https://huggingface.co/blog/wav2vec2-with-ngram), there is the following installation code of the n-grams language model [KenLM](https://github.com/kpu/kenlm#kenlm):

```auto
#Let's start by installing the Ubuntu library prerequisites:
sudo apt install build-essential cmake libboost-system-dev libboost-thread-dev libboost-program-options-dev libboost-test-dev libeigen3-dev zlib1g-dev libbz2-dev liblzma-dev

# before downloading and unpacking the KenLM repo.
wget -O - https://kheafield.com/code/kenlm.tar.gz | tar xz

# KenLM is written in C++, so we'll make use of cmake to build the binaries.
mkdir kenlm/build && cd kenlm/build && cmake .. && make -j2
ls kenlm/build/bin

```

However, this code is for the Linux distribution Ubuntu. It does not run in the Amazon Linux distribution of AWS Sagemaker instances.

Anyone knows how to adapt this code?

And what about the KenLM code for inference in AWS Sagemaker to deploy a Wav2vec model with KenLM inside an inference script for [real-time inference](https://docs.aws.amazon.com/sagemaker/latest/dg/realtime-endpoints.html)?

_ **Note** : I read the thread [Serveless memory problem when deploy Wav2Vec2 with custom inference code](https://discuss.huggingface.co/t/serveless-memory-problem-when-deploy-wav2vec2-with-custom-inference-code/18023/4) and the [post](https://discuss.huggingface.co/t/serveless-memory-problem-when-deploy-wav2vec2-with-custom-inference-code/18023/19) of @philschmid about how to install KenLM no AWS Sagemaker but as said, these instructions are for an Ubuntu distribution, not for the Amazon Linux one. Then, they do not work an AWS Sagemaker._

---

<div class="post-metadata">

**Author:** ![pierreguillou](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/pierreguillou/32/19626_2.png) [@pierreguillou](https://discuss.huggingface.co/u/pierreguillou)\
**Post date:** [May 31, 2022, 6:47pm UTC](https://discuss.huggingface.co/t/how-to-train-kenlm-no-aws-sagemaker/18444/2 "2022-05-31T18:47:41Z")

</div>

Hello.

This [issue no github do KenLM](https://github.com/kpu/kenlm/issues/322) gives a link to the boost installation:

> Here are the instructions for installing Boost in a home directory [dependencies . kenlm . code . Kenneth Heafield](https://neural.mt/code/kenlm/dependencies/)

Then, just add `sudo` in a AWS Sagemaker terminal to the following commands and you will succeed in installing Boost:

```auto
wget https://dl.bintray.com/boostorg/release/1.72.0/source/boost_1_72_0.tar.bz2
tar xjf boost_1_72_0.tar.bz2

cd boost_1_72_0

sudo ./bootstrap.sh

#Note that this may fail. A common cause is incorrectly installed gzip 
#or bzip2. KenLM does not use the iostreams library, so it might work 
#anyway. But an incomplete install will break other packages like Moses.

sudo ./b2 --prefix=$PREFIX --libdir=$LIBDIR --layout=tagged link=static,shared threading=multi,single install -j4 || echo FAILURE
cd ...

```

After that, just follow the 2 other instructions:

```auto
# before downloading and unpacking the KenLM repo.
wget -O - https://kheafield.com/code/kenlm.tar.gz | tar xz

# KenLM is written in C++, so we'll make use of cmake to build the binaries.
mkdir kenlm/build && cd kenlm/build && cmake .. && make -j2
ls kenlm/build/bin

```

Et voilà! You can train a model KenLM in AWS Sagemaker. (thanks to Egberto Caetano Araujo da Silva who found this solution!)

---

<div class="post-metadata">

**Author:** ![philschmid](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/philschmid/32/38022_2.png) [@philschmid](https://discuss.huggingface.co/u/philschmid)\
**Post date:** [June 1, 2022, 8:02am UTC](https://discuss.huggingface.co/t/how-to-train-kenlm-no-aws-sagemaker/18444/3 "2022-06-01T08:02:29Z")

</div>

FYI the SageMaker DLCs, both Training and Inference are UBUNTU based. You can find the dockerfiles here: [deep-learning-containers/huggingface at master · aws/deep-learning-containers · GitHub](https://github.com/aws/deep-learning-containers/tree/master/huggingface)

---

<div class="post-metadata">

**Author:** ![sid-lekh](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@sid-lekh](https://discuss.huggingface.co/u/sid-lekh)\
**Post date:** [February 11, 2024, 12:13pm UTC](https://discuss.huggingface.co/t/how-to-train-kenlm-no-aws-sagemaker/18444/4 "2024-02-11T12:13:20Z")

</div>

The KenLM building requires cmake. cmake is not available. while trying to install cmake, libssl-dev is missing. Not able to progress so forth.
