<!--Copyright 2020 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

-->
*This model was released on 2020-10-21 and added to Hugging Face Transformers on 2021-03-06 and contributed by [valhalla](https://huggingface.co/valhalla).*

# M2M100

[M2M100](https://huggingface.co/papers/2010.11125) addresses the limitations of existing multilingual machine translation models by focusing on true Many-to-Many translation between 100 languages, not just those involving English. The model is trained on a large, open-sourced dataset covering thousands of language pairs, mined through large-scale techniques. It employs a combination of dense scaling and language-specific sparse parameters to enhance model capacity. This approach results in BLEU score improvements of over 10 for non-English language pairs while maintaining competitive performance with leading WMT systems.

<hfoptions id="usage">
<hfoption id="Pipeline">

```py
import torch
from transformers import pipeline

pipeline = pipeline(task="translation_en_to_fr", model="facebook/m2m100_418M", dtype="auto")
pipeline("Plants create energy through a process known as photosynthesis.")
```

</hfoption>
<hfoption id="AutoModel">

```py
import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

model = AutoModelForSeq2SeqLM.from_pretrained("facebook/m2m100_418M", dtype="auto")
tokenizer = AutoTokenizer.from_pretrained("facebook/m2m100_418M")

tokenizer.src_lang = "fr"
encoded_fr = tokenizer("Les plantes créent de l’énergie à travers un processus connu sous le nom de photosynthèse.", return_tensors="pt")
generated_tokens = model.generate(**encoded_fr, forced_bos_token_id=tokenizer.get_lang_id("en"))
print(tokenizer.batch_decode(generated_tokens, skip_special_tokens=True))
```

</hfoption>
</hfoptions>

## Usage tips

- M2M100 is a multilingual encoder-decoder model primarily for translation tasks. It expects sequences in a specific format with special language ID tokens as prefixes.
- Source text format: `[lang_code] X [eos]`. `lang_code` is the source language ID for source text and target language ID for target text. `X` is the source or target text.
- [`M2M100Tokenizer`] depends on sentencepiece. Install it with `pip install sentencepiece` before running examples.
- M2M100 uses `eos_token_id` as the `decoder_start_token_id` for generation. The target language ID is forced as the first generated token. Pass the `forced_bos_token_id` parameter to the [`generate`] method to force the target language ID.

## M2M100Config

[[autodoc]] M2M100Config

## M2M100Tokenizer

[[autodoc]] M2M100Tokenizer
    - build_inputs_with_special_tokens
    - get_special_tokens_mask
    - create_token_type_ids_from_sequences
    - save_vocabulary

## M2M100Model

[[autodoc]] M2M100Model
    - forward

## M2M100ForConditionalGeneration

[[autodoc]] M2M100ForConditionalGeneration
    - forward
