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

# MultiSlav P4-pol2many

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

##  Multilingual Polish-to-Many MT Model

___P4-pol2many___ is an Encoder-Decoder vanilla transformer model trained on sentence-level Machine Translation task. 
Model is supporting translation from Polish to 3 languages: Czech, Slovak, and Slovene.
This model is 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="p4-pol.svg">
</p>

___P4-pol2many___ - Polish-to-Many model translating from Polish to Slavic languages. 
This model and [_P4-many2pol_](https://huggingface.co/allegro/P4-many2pol) combine into ___P4-pol___ pivot system translating between _4_ Slavic languages. 
_P4-pol_ translates all supported Slavic languages using Many2One model to Polish bridge sentence 
and next using this One2Many model from Polish bridge sentence to target Slavic language.  

### Model description

* **Model name:** P4-pol2many
* **Source Language:** Polish
* **Target Languages:** Czech, Slovak, Slovene
* **Model Collection:** [MultiSlav](https://huggingface.co/collections/allegro/multislav-6793d6b6419e5963e759a683)
* **Model type:** MarianMTModel Encoder-Decoder
* **License:** CC BY 4.0 (commercial use allowed)
* **Developed by:** [MLR @ Allegro](https://ml.allegro.tech/) & [Laniqo.com](https://laniqo.com/)

### Supported languages

Using model you must specify target language for translation. 
To use the 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. 
Each of them was added as a special token to Sentence-Piece tokenizer.

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


## Use case quickstart

Example code-snippet to use model. Due to bug the `MarianMTModel` must be used explicitly.

```python
from transformers import AutoTokenizer, MarianMTModel

o2m_model_name = "Allegro/P4-pol2many"

o2m_tokenizer = AutoTokenizer.from_pretrained(o2m_model_name)
o2m_model = MarianMTModel.from_pretrained(o2m_model_name)

text = "Allegro to internetowa platforma e-commerce, na której swoje produkty sprzedają średnie i małe firmy, jak również duże marki."
target_languages = ["ces", "slk", "slv"]
batch_to_translate = [
    f">>{lang}<<" + " " + text for lang in target_languages
]
translations = o2m_model.generate(**o2m_tokenizer.batch_encode_plus(batch_to_translate, return_tensors="pt"))
bridge_translations = o2m_tokenizer.batch_decode(translations, skip_special_tokens=True, clean_up_tokenization_spaces=True)
for trans in bridge_translations:
    print(trans)
```

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

Generated Slovak output:
> Allegro je online platforma elektronického obchodu, na ktorej svoje produkty predávajú stredné a malé podniky, ako aj veľké značky.

Generated Slovene output:
> Allegro je spletna platforma za e-poslovanje, kjer svoje izdelke prodajajo srednje velika in mala podjetja ter velike blagovne znamke.

To pivot-translate to other languages via _bridge_ Polish sentence, we need One2Many model.
Many2One model requires explicit source language token as well.
Example for translating from Czech to Slovak:

```python
from transformers import AutoTokenizer, MarianMTModel

m2o_model_name = "Allegro/P4-many2pol"
o2m_model_name = "Allegro/P4-pol2many"

m2o_tokenizer = AutoTokenizer.from_pretrained(m2o_model_name)
m2o_model = MarianMTModel.from_pretrained(m2o_model_name)

o2m_tokenizer = AutoTokenizer.from_pretrained(o2m_model_name)
o2m_model = MarianMTModel.from_pretrained(o2m_model_name)

text = ">>ces<<" + " " + "Allegro je on-line e-commerce platforma, na které své produkty prodávají střední a malé firmy, stejně jako velké značky."

translation = m2o_model.generate(**m2o_tokenizer.batch_encode_plus([text], return_tensors="pt"))
bridge_translations = m2o_tokenizer.batch_decode(translation, skip_special_tokens=True, clean_up_tokenization_spaces=True)

post_edited_bridge = ">>slk<<" + " " + bridge_translations[0]
translation = o2m_model.generate(**o2m_tokenizer.batch_encode_plus([post_edited_bridge], return_tensors="pt"))
decoded_translations = o2m_tokenizer.batch_decode(translation, skip_special_tokens=True, clean_up_tokenization_spaces=True)

print(decoded_translations[0])
```

Generated Czech to Slovak pivot translation via Polish:
> Allegro je online platforma elektronického obchodu, na ktorej svoje produkty predávajú stredné a malé podniky, ako aj veľké značky.

## Training

[SentencePiece](https://github.com/google/sentencepiece) tokenizer has a vocab size 64k 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       | 242M                                                                                                       |
| Training Examples          | 112M                                                                                                       |
| Vocab Size                 | 64k                                                                                                        |
| 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                                                                                          |
| Source Language Supported  | Polish                                                                                                     |
| Target Languages Supported | Czech, Slovak, Slovene                                                                                     |
| Precision                  | float16                                                                                                    |
| Validation Freq            | 3000 steps                                                                                                 |
| Stop Metric                | ChrF                                                                                                       |
| Stop Criterion             | 20 Validation steps                                                                                        |


## Training corpora

<p align="center">
  <img src="pivot-data-pol2many.svg">
</p>

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. 
In this model we experimented with expanding data-regime by using data from multiple target languages. 
We found that additional target data clearly improved performance compared to the bi-directional baseline models.
For example in translation from Polish to Czech, this allowed us to expand training data-size from 63M to 112M examples, and from 23M to 112M for Polish to Slovene translation.
We only used explicitly open-source data to ensure open-source license of our model.

Datasets were downloaded via [MT-Data](https://pypi.org/project/mtdata/0.2.10/) library. Number of total examples post filtering and deduplication: __112M__.

The datasets used:

| **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.
Metrics BLEU, ChrF2, and Unbabel/wmt22-comet-da.

Translation results on translation from Polish to Czech (Slavic direction with the __highest__ data-regime):

| **Model**                                       | **Comet22** | **BLEU** | **ChrF** | **Model Size** |
|-------------------------------------------------|:-----------:|:--------:|:--------:|---------------:|
| M2M−100                                         |    89.6     |   19.8   |   47.7   |           1.2B |
| NLLB−200                                        |    89.4     |   19.2   |   46.7   |           1.3B |
| Opus Sla-Sla                                    |    82.9     |   14.6   |   42.6   |            64M |
| BiDi-ces-pol (baseline)                         |    90.0     |   20.3   |   48.5   |           209M |
| P4-pol2many <span style="color:green;">*</span> |    90.2     |   20.2   |   48.5   |           242M |
| P5-eng <span style="color:red;">◊</span>        |    89.0     |   19.9   |   48.3   |        2x 258M |
| P5-ces <span style="color:red;">◊</span>        |    90.3     |   20.2   |   48.6   |        2x 258M |
| MultiSlav-4slav                                 |    90.2     |   20.6   |   48.7   |           242M |
| ___MultiSlav-5lang___                           |  __90.4__   | __20.7__ | __48.9__ |           258M |

Translation results on translation from Polish to Slovene (direction to Polish with the __lowest__ data-regime):

| **Model**                                       | **Comet22** | **BLEU** | **ChrF** | **Model Size** |
|-------------------------------------------------|:-----------:|:--------:|:--------:|---------------:|
| M2M−100                                         |    89.6     |   26.6   |   55.0   |           1.2B |
| NLLB−200                                        |    88.8     |   23.3   |   42.0   |           1.3B |
| BiDi-pol-slv (baseline)                         |    89.4     |   26.6   |   55.4   |           209M |
| P4-pol2many <span style="color:green;">*</span> |    88.4     |   24.8   |   53.2   |           242M |
| P5-eng <span style="color:red;">◊</span>        |    88.5     |   25.6   |   54.6   |        2x 258M |
| P5-ces <span style="color:red;">◊</span>        |    89.8     |   26.6   |   55.3   |        2x 258M |
| MultiSlav-4slav                                 |    90.1     | __27.1__ | __55.7__ |           242M |
| ___MultiSlav-5lang___                           |  __90.2__   | __27.1__ | __55.7__ |           258M |


<span style="color:green;">*</span> this model

<span style="color:red;">◊</span> system of 2 models *Many2XXX* and *XXX2Many*

## 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)