
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['▁मैं', '▁एक', '▁अच', '्', 'छा', '▁हाथी', '▁हूँ']