transformers safetensors marian text2text-generation translation mt pytorch sentence-piece multilingual allegro laniqo cs pl sk sl en license:cc-by-4.0 autotrain_compatible endpoints_compatible region:us
---
language:
- cs
- pl
- sk
- sl
- en
library_name: transformers
license: cc-by-4.0
tags:
- translation
- mt
- marian
- pytorch
- sentence-piece
- multilingual
- allegro
- laniqo
---

# MultiSlav BiDi Models


<p align="center">
  <a href="https://ml.allegro.tech/"><img src="allegro-title.svg" alt="MLR @ Allegro.com"></a>
</p>

##  Multilingual BiDi MT Models

___BiDi___ is a collection of Encoder-Decoder vanilla transformer models trained on sentence-level Machine Translation task. 
Each model is supporting Bi-Directional translation.

___BiDi___ models are part of the [___MultiSlav___ collection](https://huggingface.co/collections/allegro/multislav-6793d6b6419e5963e759a683). More information will be available soon in our upcoming MultiSlav paper.

Experiments were conducted under research project by [Machine Learning Research](https://ml.allegro.tech/) lab for [Allegro.com](https://ml.allegro.tech/).
Big thanks to [laniqo.com](laniqo.com) for cooperation in the research. 

<p align="center">
  <img src="bi-di.svg">
</p>

Graphic above provides an example of an BiDi model - [BiDi-ces-pol](https://huggingface.co/allegro/bidi-ces-pol) to translate from Polish to Czech language.
___BiDi-ces-pol___ is a bi-directional model supporting translation both __form Czech to Polish__ and __from Polish to Czech__ directions.


### Supported languages

To use a ___BiDi___ model, you must provide the target language for translation. 
Target language tokens are represented as 3-letter ISO 639-3 language codes embedded in a format >>xxx<<.
All accepted directions and their respective tokens are listed below.
Note that, for each model only two directions are available.
Each of them was added as a special token to Sentence-Piece tokenizer.

| **Target Language** | **First token** |
|---------------------|-----------------|
| Czech               | `>>ces<<`       |
| English             | `>>eng<<`       |
| Polish              | `>>pol<<`       |
| Slovak              | `>>slk<<`       |
| Slovene             | `>>slv<<`       |


### Bi-Di models available

We provided 10 ___BiDi___ models, allowing to translate between 20 languages.

| **Bi-Di model** | **Languages supported** | **HF repository**                                                   |
|-----------------|-------------------------|---------------------------------------------------------------------|
| BiDi-ces-eng    | Czech ↔ English         | [allegro/BiDi-ces-eng](https://huggingface.co/allegro/bidi-ces-eng) |
| BiDi-ces-pol    | Czech ↔ Polish          | [allegro/BiDi-ces-pol](https://huggingface.co/allegro/bidi-ces-pol) |
| BiDi-ces-slk    | Czech ↔ Slovak          | [allegro/BiDi-ces-slk](https://huggingface.co/allegro/bidi-ces-slk) |
| BiDi-ces-slv    | Czech ↔ Slovene         | [allegro/BiDi-ces-slv](https://huggingface.co/allegro/bidi-ces-slv) |
| BiDi-eng-pol    | English ↔ Polish        | [allegro/BiDi-eng-pol](https://huggingface.co/allegro/bidi-eng-pol) |
| BiDi-eng-slk    | English ↔ Slovak        | [allegro/BiDi-eng-slk](https://huggingface.co/allegro/bidi-eng-slk) |
| BiDi-eng-slv    | English ↔ Slovene       | [allegro/BiDi-eng-slv](https://huggingface.co/allegro/bidi-eng-slv) |
| BiDi-pol-slk    | Polish ↔ Slovak         | [allegro/BiDi-pol-slk](https://huggingface.co/allegro/bidi-pol-slk) |
| BiDi-pol-slv    | Polish ↔ Slovene        | [allegro/BiDi-pol-slv](https://huggingface.co/allegro/bidi-pol-slv) |
| BiDi-slk-slv    | Slovak ↔ Slovene        | [allegro/BiDi-slk-slv](https://huggingface.co/allegro/bidi-slk-slv) |

## Use case quickstart

Example code-snippet to use model. Due to bug the `MarianMTModel` must be used explicitly.
Remember to adjust source and target languages to your use-case.

```python
from transformers import AutoTokenizer, MarianMTModel

source_lang = "pol"
target_lang = "ces"
first_lang, second_lang = sorted([source_lang, target_lang])
model_name = f"Allegro/BiDi-{first_lang}-{second_lang}"

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)

text = f">>{target_lang}<<" + " " + "Allegro to internetowa platforma e-commerce, na której swoje produkty sprzedają średnie i małe firmy, jak również duże marki."

batch_to_translate = [text]
translations = model.generate(**tokenizer.batch_encode_plus(batch_to_translate, return_tensors="pt"))
decoded_translation = tokenizer.batch_decode(translations, skip_special_tokens=True, clean_up_tokenization_spaces=True)[0]

print(decoded_translation)
```

Generated Czech output:
> Allegro je online e-commerce platforma, na které své výrobky prodávají střední a malé firmy, stejně jako velké značky.


## Training

[SentencePiece](https://github.com/google/sentencepiece) tokenizer has a vocab size 32k in total (16k per language). Tokenizer was trained on randomly sampled part of the training corpus. 
During the training we used the [MarianNMT](https://marian-nmt.github.io/) framework.
Base marian configuration used: [transfromer-big](https://github.com/marian-nmt/marian-dev/blob/master/src/common/aliases.cpp#L113).
All training parameters are listed in table below.

### Training hyperparameters:

| **Hyperparameter**         | **Value**                                                                                                  |
|----------------------------|------------------------------------------------------------------------------------------------------------|
| Total Parameter Size       | 209M                                                                                                       |
| Vocab Size                 | 32k                                                                                                        |
| Base Parameters            | [Marian transfromer-big](https://github.com/marian-nmt/marian-dev/blob/master/src/common/aliases.cpp#L113) |
| Number of Encoding Layers  | 6                                                                                                          |
| Number of Decoding Layers  | 6                                                                                                          |
| Model Dimension            | 1024                                                                                                       |
| FF Dimension               | 4096                                                                                                       |
| Heads                      | 16                                                                                                         |
| Dropout                    | 0.1                                                                                                        |
| Batch Size                 | mini batch fit to VRAM                                                                                     |
| Training Accelerators      | 4x A100 40GB                                                                                               |
| Max Length                 | 100 tokens                                                                                                 |
| Optimizer                  | Adam                                                                                                       |
| Warmup steps               | 8000                                                                                                       |
| Context                    | Sentence-level MT                                                                                          |
| Languages Supported        | See [Bi-Di models available](#Bi-Di-models-available)                                                      |
| Precision                  | float16                                                                                                    |
| Validation Freq            | 3000 steps                                                                                                 |
| Stop Metric                | ChrF                                                                                                       |
| Stop Criterion             | 20 Validation steps                                                                                        |


## Training corpora

The main research question was: "How does adding additional, related languages impact the quality of the model?" - we explored it in the Slavic language family. 
___BiDi___ models are our baseline before expanding the data-regime by using higher-level multilinguality. 

Datasets were downloaded via [MT-Data](https://pypi.org/project/mtdata/0.2.10/) library. 
The number of total examples post filtering and deduplication varies, depending on languages supported, see the table below.

| **Language pair** | **Number of training examples** |
|-------------------|--------------------------------:|
| Czech ↔ Polish    |                             63M |
| Czech ↔ Slovak    |                             30M |
| Czech ↔ Slovene   |                             25M |
| Polish ↔ Slovak   |                             26M |
| Polish ↔ Slovene  |                             23M |
| Slovak ↔ Slovene  |                             18M |
| ----------------  | ------------------------------- |
| Czech ↔ English   |                            151M |
| English ↔ Polish  |                            150M |
| English ↔ Slovak  |                             52M |
| English ↔ Slovene |                             40M |

The datasets used (only applicable to specific directions):

| **Corpus**           |
|----------------------|
| paracrawl            |
| opensubtitles        |
| multiparacrawl       |
| dgt                  |
| elrc                 |
| xlent                |
| wikititles           |
| wmt                  |
| wikimatrix           |
| dcep                 |
| ELRC                 |
| tildemodel           |
| europarl             |
| eesc                 |
| eubookshop           |
| emea                 |
| jrc_acquis           |
| ema                  |
| qed                  |
| elitr_eca            |
| EU-dcep              |
| rapid                |
| ecb                  |
| kde4                 |
| news_commentary      |
| kde                  |
| bible_uedin          |
| europat              |
| elra                 |
| wikipedia            |
| wikimedia            |
| tatoeba              |
| globalvoices         |
| euconst              |
| ubuntu               |
| php                  |
| ecdc                 |
| eac                  |
| eac_reference        |
| gnome                |
| EU-eac               |
| books                |
| EU-ecdc              |
| newsdev              |
| khresmoi_summary     |
| czechtourism         |
| khresmoi_summary_dev |
| worldbank            |

## Evaluation

Evaluation of the models was performed on [Flores200](https://huggingface.co/datasets/facebook/flores) dataset. 
The table below compares performance of the open-source models and all applicable models from our collection.
Metric used: Unbabel/wmt22-comet-da.

| **Direction**                                      | **CES → ENG** | **CES → POL** | **CES → SLK** | **CES → SLV** | **ENG → CES** | **ENG → POL** | **ENG → SLK** | **ENG → SLV** | **POL → CES** | **POL → ENG** | **POL → SLK** | **POL → SLV** | **SLK → CES** | **SLK → ENG** | **SLK → POL** | **SLK → SLV** | **SLV → CES** | **SLV → ENG** | **SLV → POL** | **SLV → SLK** |
|----------------------------------------------------|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|--------------:|
| **M2M-100**                                        |          87.0 |          89.0 |          92.1 |          89.7 |          88.6 |          86.4 |          88.4 |          87.3 |          89.6 |          84.6 |          89.4 |          88.4 |          92.7 |          86.8 |          89.1 |          89.6 |          90.3 |          86.4 |          88.7 |          90.1 |
| **NLLB-200**                                       |          88.1 |          88.9 |          91.2 |          88.6 |          90.4 |      __88.5__ |          90.1 |          88.8 |          89.4 |      __85.8__ |          88.9 |          87.7 |          91.8 |          88.2 |          88.9 |          88.8 |          90.0 |      __87.5__ |          88.6 |          89.4 |
| **Seamless-M4T**                                   |          87.5 |          80.9 |          90.8 |          82.0 |      __90.7__ |      __88.5__ |      __90.6__ |      __89.6__ |          79.6 |          85.4 |          80.0 |          76.4 |          91.5 |          87.2 |          81.2 |          82.9 |          80.9 |          87.3 |          76.7 |          81.0 |
| **OPUS-MT Sla-Sla**                                |      __88.2__ |          82.8 |             - |          83.4 |          89.1 |          85.6 |             - |          84.5 |          82.9 |          82.2 |             - |          81.2 |             - |             - |             - |             - |          83.5 |          84.1 |          80.8 |             - |
| **OPUS-MT SK-EN**                                  |             - |             - |             - |             - |             - |             - |          89.5 |             - |             - |             - |             - |             - |             - |      __88.4__ |             - |             - |             - |             - |             - |             - |
| _Our contributions:_                               |               |               |               |               |               |               |               |               |               |               |               |               |               |               |               |               |               |               |               |               |
| **BiDi Models**<span style="color:green;">*</span> |          87.5 |          89.4 |          92.4 |          89.8 |          87.8 |          86.2 |          87.2 |          86.6 |          90.0 |          85.0 |          89.1 |          88.4 |          92.9 |          87.3 |          88.8 |          89.4 |          90.0 |          86.9 |          88.1 |          89.1 |
| **P4-pol**<span style="color:red;">◊</span>        |             - |          89.6 |          90.8 |          88.7 |             - |             - |             - |             - |          90.2 |             - |          89.8 |          88.7 |          91.0 |             - |          89.3 |          88.4 |          89.3 |             - |          88.7 |          88.5 |
| **P5-eng**<span style="color:red;">◊</span>        |          88.0 |          89.0 |          90.7 |          89.0 |          88.8 |          87.3 |          88.4 |          87.5 |          89.0 |          85.7 |          88.5 |          87.8 |          91.0 |          88.2 |          88.6 |          88.5 |          89.6 |          87.2 |          88.4 |          88.9 |
| **P5-ces**<span style="color:red;">◊</span>        |          87.9 |          89.6 |      __92.5__ |          89.9 |          88.4 |          85.0 |          87.9 |          85.9 |          90.3 |          84.5 |          89.5 |          88.0 |      __93.0__ |          87.8 |          89.4 |          89.8 |          90.3 |          85.7 |          87.9 |          89.8 |
| **MultiSlav-4slav**                                |             - |          89.7 |      __92.5__ |          90.0 |             - |             - |             - |             - |          90.2 |             - |          89.6 |          88.7 |          92.9 |             - |          89.4 |          90.1 |      __90.6__ |             - |          88.9 |      __90.2__ |
| **MultiSlav-5lang**                                |          87.8 |      __89.8__ |      __92.5__ |      __90.1__ |          88.9 |          86.9 |          88.0 |          87.3 |      __90.4__ |          85.4 |          89.8 |      __88.9__ |          92.9 |          87.8 |      __89.6__ |      __90.2__ |      __90.6__ |          87.0 |      __89.2__ |      __90.2__ |

<span style="color:red;">◊</span> system of 2 models *Many2XXX* and *XXX2Many*, see [P5-ces2many](https://huggingface.co/allegro/p5-ces2many)

<span style="color:green;">*</span> results combined for all bi-directional models; each values for applicable model

## Limitations and Biases

We did not evaluate inherent bias contained in training datasets. It is advised to validate bias of our models in perspective domain. This might be especially problematic in translation from English to Slavic languages, which require explicitly indicated gender and might hallucinate based on bias present in training data.

## License

The model is licensed under CC BY 4.0, which allows for commercial use.

## Citation
TO BE UPDATED SOON 🤗



## Contact Options

Authors:
- MLR @ Allegro: [Artur Kot](https://linkedin.com/in/arturkot), [Mikołaj Koszowski](https://linkedin.com/in/mkoszowski), [Wojciech Chojnowski](https://linkedin.com/in/wojciech-chojnowski-744702348), [Mieszko Rutkowski](https://linkedin.com/in/mieszko-rutkowski)
- Laniqo.com: [Artur Nowakowski](https://linkedin.com/in/artur-nowakowski-mt), [Kamil Guttmann](https://linkedin.com/in/kamil-guttmann), [Mikołaj Pokrywka](https://linkedin.com/in/mikolaj-pokrywka)

Please don't hesitate to contact authors if you have any questions or suggestions:
- e-mail: artur.kot@allegro.com or mikolaj.koszowski@allegro.com
- LinkedIn: [Artur Kot](https://linkedin.com/in/arturkot) or [Mikołaj Koszowski](https://linkedin.com/in/mkoszowski)
