
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.
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.
First, we load the original Llama tokenizer and its vocabulary.
pythonfrom transformers import LlamaTokenizerllama_tokenizer = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-7b-chat-hf')
Next, we load the new language tokenizer--in this case, Hindi--and its vocabulary.
pythonnew_tokenizer = LlamaTokenizer.from_pretrained('./hi_tokenizer')
We then merge the two tokenizers by adding the new tokens to the original tokenizer.
pythonpython 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.
We verify that the merged tokenizer works as expected by testing it on a text sample.
pythontext = "मैं एक अच्छा हाथी हूँ"our_tokenizer = LlamaTokenizer.from_pretrained('./extended_tokenizer')print(our_tokenizer.tokenize(text))
Output:
python['▁मैं', '▁एक', '▁अच', '्', 'छा', '▁हाथी', '▁हूँ']
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
To fine-tune Llama for the new language, we need to prepare a dataset with questions, oracle documents, and distractor documents.
We prepare a dataset with the following format:
We fine-tune Llama using the RAFT technique, which involves training the model to differentiate between oracle documents and distractor documents.
To evaluate the fine-tuned model, we use the lm-evaluation-harness library, which provides a wide range of benchmarks for LLMs.
lm-evaluation-harnessWe install the lm-evaluation-harness library using pip.
bashpip install lm-evaluation-harness
We evaluate the fine-tuned model using the lm-evaluation-harness library.
pythonfrom lm_eval import evaluator# Load the fine-tuned modelmodel = ...# Evaluate the modelresults = evaluator.simple_evaluate(model=model,tasks=["task1", "task2", ...],num_fewshot=0,batch_size=1,)
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.
Subscribe to our newsletter to keep up with the latest AI updates, releases and more.