# NLLB tokenizer multiple target/source languages within a training batch

**URL:** <https://discuss.huggingface.co/t/nllb-tokenizer-multiple-target-source-languages-within-a-training-batch/56307>\
**Category:** 🤗Tokenizers\
**Created:** [September 25, 2023, 1:48pm UTC](https://discuss.huggingface.co/t/nllb-tokenizer-multiple-target-source-languages-within-a-training-batch/56307 "2023-09-25T13:48:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitindub](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@nitindub](https://discuss.huggingface.co/u/nitindub)\
**Post date:** [September 25, 2023, 1:48pm UTC](https://discuss.huggingface.co/t/nllb-tokenizer-multiple-target-source-languages-within-a-training-batch/56307/1 "2023-09-25T13:48:49Z")

</div>

From the docs, one way to load the nllb tokenizer is:  
`tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-1.3B", use_auth_token=False, src_lang="eng_Latn", tgt_lang="hin_Deva")`

But, this way, tgt\_lang which gets assigned is `hin_Deva` for all labels. Is there a way to set the `tgt_lang` code when calling the tokenizer function, so that one can have multiple languages within a batch while training. e.g.

```auto
# loading tokenizer
tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-1.3B", use_auth_token=False, src_lang="eng_Latn")

```

```auto
model_inputs = tokenizer(inputs, text_target=targets, target_langs=target_langs, max_length=max_length, truncation=True)

# where target_langs = ['hin_Deva', 'tur_Latn', ....]

```

---

<div class="post-metadata">

**Author:** ![KhaiKit](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@KhaiKit](https://discuss.huggingface.co/u/KhaiKit)\
**Post date:** [October 20, 2023, 9:55am UTC](https://discuss.huggingface.co/t/nllb-tokenizer-multiple-target-source-languages-within-a-training-batch/56307/2 "2023-10-20T09:55:40Z")

</div>

Hi anyone has a solution to this?

To my understanding, one way to prepare the training data for finetuning if we feed the model the same sentence pair twice but flipped so that the model learns to translate in both direction.

Eg (LHS is the Input & RHS is the Target):

```auto
{"eng_Latn": "Hi", "zho_Hans": "你好"}
{"zho_Hans": "你好", "eng_Latn": "Hi"}

```

But I can’t seem to find a way to do this with the Trainer API.

---

<div class="post-metadata">

**Author:** ![nitindub](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@nitindub](https://discuss.huggingface.co/u/nitindub)\
**Post date:** [November 20, 2023, 3:49pm UTC](https://discuss.huggingface.co/t/nllb-tokenizer-multiple-target-source-languages-within-a-training-batch/56307/3 "2023-11-20T15:49:00Z")

</div>

Not sure, but found a workaround. Created a preprocess function that appends the `tokenizer.lang_code_to_id[ex["tgt_lang"]]` value as part of tokenization process.

```auto
def preprocess_function_helper(tokenizer, context_length=3):
    def preprocess_function(examples, max_length=128):
        ......
        model_inputs = tokenizer(
            inputs, text_target=targets, max_length=max_length, truncation=True
        )

        # Update first token id in each model_inputs['labels'] with lang_tokens
        for i in range(batch_size):
            model_inputs['labels'][i][0] = lang_tokens[i]
        .....

   return preprocess_function

dataset = dataset.map(
        preprocess_function_helper(tokenizer=tokenizer, context_length=context_length),
        batched=True,
        remove_columns=["translation"],
        load_from_cache_file=False,
    )

```

which gets passed into the models. Hope that helps.

---

<div class="post-metadata">

**Author:** ![KhaiKit](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@KhaiKit](https://discuss.huggingface.co/u/KhaiKit)\
**Post date:** [November 20, 2023, 4:26pm UTC](https://discuss.huggingface.co/t/nllb-tokenizer-multiple-target-source-languages-within-a-training-batch/56307/4 "2023-11-20T16:26:13Z")

</div>

Hey thanks for sharing!  
I think that helps a lot to ensure that the correct lang\_id is attached to the correct input sequence. However, when it comes to the training, how will the model know when to switch the output token id (In your case, between _eng\_Latn_ & _hin\_Deva_, or in my example, between _eng\_Latn_ & _zho\_Hans_)

The reason why I ask this is because I know **during inference** after the model has been trained, we need to **specify the _forced\_bos\_token\_id_ to generate output**.  
Thus I am **not sure if there is a need/way to specify the _forced\_bos\_token\_id_ while training**.

> Edit: I just realised that multi-lingual finetuning/training is not supported and is unlikely to be supported after reading a related [issue](https://github.com/huggingface/transformers/issues/15500) posted on huggingface’s Github

---

<div class="post-metadata">

**Author:** ![natekrasner](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@natekrasner](https://discuss.huggingface.co/u/natekrasner)\
**Post date:** [July 17, 2024, 11:31pm UTC](https://discuss.huggingface.co/t/nllb-tokenizer-multiple-target-source-languages-within-a-training-batch/56307/5 "2024-07-17T23:31:41Z")

</div>

I know this a late response, but I have been doing multilingual finetuning without issue (mostly). As long as the first token of the target sentence is the correct output lang\_id, the teacher forcing will include it after the first decoding step. The model does not have to correctly predict the output lang\_id, it only has to correctly predict the output sequence given the correct lang\_id.

As far as multiple languages in one batch goes, I’ll update here if my code works. I made a custom torch dataset which prepends the lang\_ids and tokenizes the sequences in the \_\_getitem\_\_ function. This way the sequences will be properly tokenized before the dataloader batches them. I am still working on my training loop code for this functionality, so I am unsure of what might go wrong.

---

<div class="post-metadata">

**Author:** ![natekrasner](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@natekrasner](https://discuss.huggingface.co/u/natekrasner)\
**Post date:** [January 10, 2025, 1:31pm UTC](https://discuss.huggingface.co/t/nllb-tokenizer-multiple-target-source-languages-within-a-training-batch/56307/6 "2025-01-10T13:31:04Z")

</div>

I was just reminded of this and I realized I never followed up to say that it worked. I wasn’t able to get it to converge while training on more than one language at a time, but that may have been due to other factors. The model allowed it with no issue, so it could definitely work for batch inference.
