Meta Global AI Developer Hackathon. $1M in prizes.Get notified
CommunityMultilingualFine-tuning
Extend Llama to a New Language: A Technical GuideLlama is a powerful language model that has shown impressive results in various natural-language processing tasks.Read more

Introduction

Llama is a powerful language model that has shown impressive results in various natural-language processing tasks. However, its capabilities are limited to the languages it was trained on. To extend Llama to a new language, we need to adapt its tokenizer and fine-tune the model on a target task.

Merging the tokenizers

To merge the tokenizers, we need to combine the original Llama tokenizer with the new language tokenizer. This involves adding new tokens to the original tokenizer without disturbing its original vocabulary.

Step 1: Load the original Llama tokenizer

First, we load the original Llama tokenizer and its vocabulary.

python
from transformers import LlamaTokenizer
​
llama_tokenizer = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-7b-chat-hf')

Step 2: Load the new language tokenizer

Next, we load the new language tokenizer--in this case, Hindi--and its vocabulary.

python
new_tokenizer = LlamaTokenizer.from_pretrained('./hi_tokenizer')

Step 3: Merge the tokenizers

We then merge the two tokenizers by adding the new tokens to the original tokenizer.

python
python extend_tokenizer.py --new_tokenizer_path=./hi_tokenizer --extended_tokenizer_save_path=./extended_tokenizer

The extend_tokenizer.py script appends the new tokens to the original tokenizer's vocabulary and saves the extended tokenizer.

Step 4: Verify the merged tokenizer

We verify that the merged tokenizer works as expected by testing it on a text sample.

python
text = "मैं एक अच्छा हाथी हूँ"
our_tokenizer = LlamaTokenizer.from_pretrained('./extended_tokenizer')
print(our_tokenizer.tokenize(text))

Output:

python
['▁मैं', '▁एक', '▁अच', '्', 'छा', '▁हाथी', '▁हूँ']

RAFT: Retrieval Augmented Fine-Tuning

RAFT is a technique that combines fine-tuning with Retrieval Augmented Generation (RAG) to better utilize custom domain text data. RAFT teaches the model to differentiate between oracle documents (D*) that contain the answer to a question and distractor documents (Di) that do not.

RAFT architecture

Sequence mermaid diagram

Fine-tuning Llama for the new language

To fine-tune Llama for the new language, we need to prepare a dataset with questions, oracle documents, and distractor documents.

Step 1: Prepare the dataset

We prepare a dataset with the following format:

dataset

Step 2: Fine-tune Llama using RAFT

We fine-tune Llama using the RAFT technique, which involves training the model to differentiate between oracle documents and distractor documents.

Evaluating the fine-tuned model

To evaluate the fine-tuned model, we use the lm-evaluation-harness library, which provides a wide range of benchmarks for LLMs.

Step 1: Install lm-evaluation-harness

We install the lm-evaluation-harness library using pip.

bash
pip install lm-evaluation-harness

Step 2: Evaluate the Fine-Tuned Model

We evaluate the fine-tuned model using the lm-evaluation-harness library.

python
from lm_eval import evaluator
​
# Load the fine-tuned model
model = ...
​
# Evaluate the model
results = evaluator.simple_evaluate(
model=model,
tasks=["task1", "task2", ...],
num_fewshot=0,
batch_size=1,
)

Conclusion

By following the steps outlined in this guide, you can extend Llama to a new language and fine-tune it for specific tasks. The RAFT technique and lm-evaluation-harness library provide a powerful framework for adapting Llama to new languages and evaluating its performance.

Related Resources

Meta blue bg
Stay up-to-date

Our latest updates delivered to your inbox

Subscribe to our newsletter to keep up with the latest AI updates, releases and more.

Meta
Products
Models
Documentation
Resources
Community
Terms & policies