



'''simple docstring'''

import copy
import unittest

from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
	    MODEL_FOR_QUESTION_ANSWERING_MAPPING,
	    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
	    MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
	    LayoutLMvaConfig,
	    LayoutLMvaForQuestionAnswering,
	    LayoutLMvaForSequenceClassification,
	    LayoutLMvaForTokenClassification,
	    LayoutLMvaModel,
	)
	from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
	from PIL import Image

	from transformers import LayoutLMvaImageProcessor


class 						a__  :


	def __init__(self			:			Optional[Any],       __UpperCAmelCase			:			str,       __UpperCAmelCase			:			List[str]=2,       __UpperCAmelCase			:			Any=3,       __UpperCAmelCase			:			Optional[int]=4,       __UpperCAmelCase			:			int=2,       __UpperCAmelCase			:			int=7,       __UpperCAmelCase			:			Dict=True,       __UpperCAmelCase			:			str=True,       __UpperCAmelCase			:			List[Any]=True,       __UpperCAmelCase			:			str=True,       __UpperCAmelCase			:			List[str]=99,       __UpperCAmelCase			:			str=36,       __UpperCAmelCase			:			Union[str, Any]=3,       __UpperCAmelCase			:			str=4,       __UpperCAmelCase			:			List[str]=37,       __UpperCAmelCase			:			Union[str, Any]="gelu",       __UpperCAmelCase			:			Dict=0.1,       __UpperCAmelCase			:			Union[str, Any]=0.1,       __UpperCAmelCase			:			Tuple=512,       __UpperCAmelCase			:			List[str]=16,       __UpperCAmelCase			:			Optional[int]=2,       __UpperCAmelCase			:			List[str]=0.02,       __UpperCAmelCase			:			List[Any]=6,       __UpperCAmelCase			:			str=6,       __UpperCAmelCase			:			List[str]=3,       __UpperCAmelCase			:			int=4,       __UpperCAmelCase			:			str=None,       __UpperCAmelCase			:			Dict=1000,       )  ->      Optional[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Optional[Any]          =			parent
					SCREAMING_SNAKE_CASE	:    Tuple          =			batch_size
					SCREAMING_SNAKE_CASE	:    List[Any]          =			num_channels
					SCREAMING_SNAKE_CASE	:    int          =			image_size
					SCREAMING_SNAKE_CASE	:    Tuple          =			patch_size
					SCREAMING_SNAKE_CASE	:    List[Any]          =			text_seq_length
					SCREAMING_SNAKE_CASE	:    List[Any]          =			is_training
					SCREAMING_SNAKE_CASE	:    str          =			use_input_mask
					SCREAMING_SNAKE_CASE	:    List[Any]          =			use_token_type_ids
					SCREAMING_SNAKE_CASE	:    List[str]          =			use_labels
					SCREAMING_SNAKE_CASE	:    str          =			vocab_size
					SCREAMING_SNAKE_CASE	:    str          =			hidden_size
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			num_hidden_layers
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			num_attention_heads
					SCREAMING_SNAKE_CASE	:    Tuple          =			intermediate_size
					SCREAMING_SNAKE_CASE	:    str          =			hidden_act
					SCREAMING_SNAKE_CASE	:    Tuple          =			hidden_dropout_prob
					SCREAMING_SNAKE_CASE	:    Dict          =			attention_probs_dropout_prob
					SCREAMING_SNAKE_CASE	:    Any          =			max_position_embeddings
					SCREAMING_SNAKE_CASE	:    List[str]          =			type_vocab_size
					SCREAMING_SNAKE_CASE	:    Tuple          =			type_sequence_label_size
					SCREAMING_SNAKE_CASE	:    Optional[int]          =			initializer_range
					SCREAMING_SNAKE_CASE	:    Tuple          =			coordinate_size
					SCREAMING_SNAKE_CASE	:    int          =			shape_size
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			num_labels
					SCREAMING_SNAKE_CASE	:    List[str]          =			num_choices
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			scope
					SCREAMING_SNAKE_CASE	:    Any          =			range_bbox

					# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
					SCREAMING_SNAKE_CASE	:    List[str]          =			text_seq_length
					SCREAMING_SNAKE_CASE	:    List[Any]          =			(image_size // patch_size) ** 2 + 1
					SCREAMING_SNAKE_CASE	:    int          =			self.text_seq_length + self.image_seq_length


	def 					lowercase__	(self			:			Dict   )  ->      Dict:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Dict          =			ids_tensor([self.batch_size, self.text_seq_length],       self.vocab_size   )

					SCREAMING_SNAKE_CASE	:    Dict          =			ids_tensor([self.batch_size, self.text_seq_length, 4],       self.range_bbox   )
					# Ensure that bbox is legal
					for i in range(bbox.shape[0]   ):
									for j in range(bbox.shape[1]   ):
													if bbox[i, j, 3] < bbox[i, j, 1]:
																	SCREAMING_SNAKE_CASE	:    Dict          =			bbox[i, j, 3]
																	SCREAMING_SNAKE_CASE	:    Optional[Any]          =			bbox[i, j, 1]
																	SCREAMING_SNAKE_CASE	:    Tuple          =			t
													if bbox[i, j, 2] < bbox[i, j, 0]:
																	SCREAMING_SNAKE_CASE	:    Optional[int]          =			bbox[i, j, 2]
																	SCREAMING_SNAKE_CASE	:    List[Any]          =			bbox[i, j, 0]
																	SCREAMING_SNAKE_CASE	:    Optional[int]          =			t

					SCREAMING_SNAKE_CASE	:    List[str]          =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

					SCREAMING_SNAKE_CASE	:    Optional[int]          =			None
					if self.use_input_mask:
									SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			random_attention_mask([self.batch_size, self.text_seq_length]   )

					SCREAMING_SNAKE_CASE	:    Optional[Any]          =			None
					if self.use_token_type_ids:
									SCREAMING_SNAKE_CASE	:    List[str]          =			ids_tensor([self.batch_size, self.text_seq_length],       self.type_vocab_size   )

					SCREAMING_SNAKE_CASE	:    int          =			None
					SCREAMING_SNAKE_CASE	:    Optional[Any]          =			None
					if self.use_labels:
									SCREAMING_SNAKE_CASE	:    List[str]          =			ids_tensor([self.batch_size],       self.type_sequence_label_size   )
									SCREAMING_SNAKE_CASE	:    Dict          =			ids_tensor([self.batch_size, self.text_seq_length],       self.num_labels   )

					SCREAMING_SNAKE_CASE	:    int          =			LayoutLMvaConfig(
					    vocab_size=self.vocab_size,       hidden_size=self.hidden_size,       num_hidden_layers=self.num_hidden_layers,       num_attention_heads=self.num_attention_heads,       intermediate_size=self.intermediate_size,       hidden_act=self.hidden_act,       hidden_dropout_prob=self.hidden_dropout_prob,       attention_probs_dropout_prob=self.attention_probs_dropout_prob,       max_position_embeddings=self.max_position_embeddings,       type_vocab_size=self.type_vocab_size,       initializer_range=self.initializer_range,       coordinate_size=self.coordinate_size,       shape_size=self.shape_size,       input_size=self.image_size,       patch_size=self.patch_size,       )

					return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels


	def 					lowercase__	(self			:			Any,       __UpperCAmelCase			:			Union[str, Any],       __UpperCAmelCase			:			int,       __UpperCAmelCase			:			Dict,       __UpperCAmelCase			:			int,       __UpperCAmelCase			:			Tuple,       __UpperCAmelCase			:			str,       __UpperCAmelCase			:			Optional[int],       __UpperCAmelCase			:			List[str]   )  ->      List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    int          =			LayoutLMvaModel(config=lowerCamelCase__   )
					model.to(lowerCamelCase__   )
					model.eval()

					# text + image
					SCREAMING_SNAKE_CASE	:    str          =			model(lowerCamelCase__,       pixel_values=lowerCamelCase__   )
					SCREAMING_SNAKE_CASE	:    Dict          =			model(
					    lowerCamelCase__,       bbox=lowerCamelCase__,       pixel_values=lowerCamelCase__,       attention_mask=lowerCamelCase__,       token_type_ids=lowerCamelCase__   )
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			model(lowerCamelCase__,       bbox=lowerCamelCase__,       pixel_values=lowerCamelCase__,       token_type_ids=lowerCamelCase__   )
					SCREAMING_SNAKE_CASE	:    List[Any]          =			model(lowerCamelCase__,       bbox=lowerCamelCase__,       pixel_values=lowerCamelCase__   )

					self.parent.assertEqual(result.last_hidden_state.shape,       (self.batch_size, self.seq_length, self.hidden_size)   )

					# text only
					SCREAMING_SNAKE_CASE	:    Optional[Any]          =			model(lowerCamelCase__   )
					self.parent.assertEqual(
					    result.last_hidden_state.shape,       (self.batch_size, self.text_seq_length, self.hidden_size)   )

					# image only
					SCREAMING_SNAKE_CASE	:    int          =			model(pixel_values=lowerCamelCase__   )
					self.parent.assertEqual(
					    result.last_hidden_state.shape,       (self.batch_size, self.image_seq_length, self.hidden_size)   )


	def 					lowercase__	(self			:			Optional[int],       __UpperCAmelCase			:			Optional[Any],       __UpperCAmelCase			:			Union[str, Any],       __UpperCAmelCase			:			int,       __UpperCAmelCase			:			Any,       __UpperCAmelCase			:			List[Any],       __UpperCAmelCase			:			str,       __UpperCAmelCase			:			Optional[int],       __UpperCAmelCase			:			Any   )  ->      Optional[int]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    str          =			self.num_labels
					SCREAMING_SNAKE_CASE	:    Tuple          =			LayoutLMvaForSequenceClassification(lowerCamelCase__   )
					model.to(lowerCamelCase__   )
					model.eval()
					SCREAMING_SNAKE_CASE	:    Optional[int]          =			model(
					    lowerCamelCase__,       bbox=lowerCamelCase__,       pixel_values=lowerCamelCase__,       attention_mask=lowerCamelCase__,       token_type_ids=lowerCamelCase__,       labels=lowerCamelCase__,       )
					self.parent.assertEqual(result.logits.shape,       (self.batch_size, self.num_labels)   )


	def 					lowercase__	(self			:			Dict,       __UpperCAmelCase			:			Any,       __UpperCAmelCase			:			List[Any],       __UpperCAmelCase			:			str,       __UpperCAmelCase			:			str,       __UpperCAmelCase			:			Tuple,       __UpperCAmelCase			:			List[str],       __UpperCAmelCase			:			List[str],       __UpperCAmelCase			:			Optional[Any]   )  ->      str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			self.num_labels
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			LayoutLMvaForTokenClassification(config=lowerCamelCase__   )
					model.to(lowerCamelCase__   )
					model.eval()
					SCREAMING_SNAKE_CASE	:    List[Any]          =			model(
					    lowerCamelCase__,       bbox=lowerCamelCase__,       pixel_values=lowerCamelCase__,       attention_mask=lowerCamelCase__,       token_type_ids=lowerCamelCase__,       labels=lowerCamelCase__,       )
					self.parent.assertEqual(result.logits.shape,       (self.batch_size, self.text_seq_length, self.num_labels)   )


	def 					lowercase__	(self			:			int,       __UpperCAmelCase			:			Dict,       __UpperCAmelCase			:			Optional[int],       __UpperCAmelCase			:			List[str],       __UpperCAmelCase			:			str,       __UpperCAmelCase			:			List[str],       __UpperCAmelCase			:			List[Any],       __UpperCAmelCase			:			Optional[int],       __UpperCAmelCase			:			Any   )  ->      str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Tuple          =			LayoutLMvaForQuestionAnswering(config=lowerCamelCase__   )
					model.to(lowerCamelCase__   )
					model.eval()
					SCREAMING_SNAKE_CASE	:    int          =			model(
					    lowerCamelCase__,       bbox=lowerCamelCase__,       pixel_values=lowerCamelCase__,       attention_mask=lowerCamelCase__,       token_type_ids=lowerCamelCase__,       start_positions=lowerCamelCase__,       end_positions=lowerCamelCase__,       )
					self.parent.assertEqual(result.start_logits.shape,       (self.batch_size, self.seq_length)   )
					self.parent.assertEqual(result.end_logits.shape,       (self.batch_size, self.seq_length)   )






	def 					lowercase__	(self			:			Any   )  ->      Tuple:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Tuple          =			self.prepare_config_and_inputs()
					(
					    (
					    SCREAMING_SNAKE_CASE		
					)       ,       (
					    SCREAMING_SNAKE_CASE		
					)       ,       (
					    SCREAMING_SNAKE_CASE		
					)       ,       (
					    SCREAMING_SNAKE_CASE		
					)       ,       (
					    SCREAMING_SNAKE_CASE		
					)       ,       (
					    SCREAMING_SNAKE_CASE		
					)       ,       (
					    SCREAMING_SNAKE_CASE		
					)       ,       (
					    SCREAMING_SNAKE_CASE		
					)       ,       		
					)	:    str          =			config_and_inputs
					SCREAMING_SNAKE_CASE	:    int          =			{
					    '''input_ids''': input_ids,
					    '''bbox''': bbox,
					    '''pixel_values''': pixel_values,
					    '''token_type_ids''': token_type_ids,
					    '''attention_mask''': input_mask,
					}
					return config, inputs_dict



@require_torch
class 						a__  (					_lowercase,      _lowercase,      unittest.TestCase						):
	__magic_name__		:     str						     =  False
	__magic_name__		:     Union[str, Any]						     =  False
	__magic_name__		:     Tuple						     =  False

	__magic_name__		:     Optional[Any]						     =  (
	    (
	        LayoutLMvaModel,
	        LayoutLMvaForSequenceClassification,
	        LayoutLMvaForTokenClassification,
	        LayoutLMvaForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	__magic_name__		:     Optional[int]						     =  (
	    {"document-question-answering": LayoutLMvaForQuestionAnswering, "feature-extraction": LayoutLMvaModel}
	    if is_torch_available()
	    else {}
	)


	def 					lowercase__	(self			:			List[str],       __UpperCAmelCase			:			Optional[int],       __UpperCAmelCase			:			Union[str, Any],       __UpperCAmelCase			:			List[str],       __UpperCAmelCase			:			List[Any],       __UpperCAmelCase			:			List[str]   )  ->      Any:

					"""simple docstring"""

					return True


	def 					lowercase__	(self			:			Optional[int]   )  ->      str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    str          =			LayoutLMvaModelTester(self   )
					SCREAMING_SNAKE_CASE	:    Optional[Any]          =			ConfigTester(self,       config_class=lowerCamelCase__,       hidden_size=37   )


	def 					lowercase__	(self			:			Optional[Any],       __UpperCAmelCase			:			Tuple,       __UpperCAmelCase			:			Optional[Any],       __UpperCAmelCase			:			Optional[Any]=False   )  ->      Tuple:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    int          =			copy.deepcopy(lowerCamelCase__   )
					if model_class in get_values(lowerCamelCase__   ):
									SCREAMING_SNAKE_CASE	:    Optional[int]          =			{
									    k: v.unsqueeze(1   ).expand(-1,       self.model_tester.num_choices,       -1   ).contiguous()
									    if isinstance(lowerCamelCase__,       torch.Tensor   ) and v.ndim > 1
									    else v
									    for k, v in inputs_dict.items()
									}
					if return_labels:
									if model_class in get_values(lowerCamelCase__   ):
													SCREAMING_SNAKE_CASE	:    int          =			torch.ones(self.model_tester.batch_size,       dtype=torch.long,       device=lowerCamelCase__   )
									elif model_class in get_values(lowerCamelCase__   ):
													SCREAMING_SNAKE_CASE	:    Optional[Any]          =			torch.zeros(
													    self.model_tester.batch_size,       dtype=torch.long,       device=lowerCamelCase__   )
													SCREAMING_SNAKE_CASE	:    Optional[int]          =			torch.zeros(
													    self.model_tester.batch_size,       dtype=torch.long,       device=lowerCamelCase__   )
									elif model_class in [
									    *get_values(lowerCamelCase__   ),
									]:
													SCREAMING_SNAKE_CASE	:    str          =			torch.zeros(
													    self.model_tester.batch_size,       dtype=torch.long,       device=lowerCamelCase__   )
									elif model_class in [
									    *get_values(lowerCamelCase__   ),
									]:
													SCREAMING_SNAKE_CASE	:    List[Any]          =			torch.zeros(
													    (self.model_tester.batch_size, self.model_tester.text_seq_length),       dtype=torch.long,       device=lowerCamelCase__,       )

					return inputs_dict


	def 					lowercase__	(self			:			str   )  ->      Union[str, Any]:

					"""simple docstring"""

					self.config_tester.run_common_tests()


	def 					lowercase__	(self			:			int   )  ->      Optional[int]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    List[Any]          =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*lowerCamelCase__   )


	def 					lowercase__	(self			:			List[Any]   )  ->      List[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    int          =			self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
									SCREAMING_SNAKE_CASE	:    Optional[Any]          =			type
									self.model_tester.create_and_check_model(*lowerCamelCase__   )


	def 					lowercase__	(self			:			Dict   )  ->      List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Optional[int]          =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__   )


	def 					lowercase__	(self			:			Any   )  ->      Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    Any          =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__   )


	def 					lowercase__	(self			:			Optional[int]   )  ->      Optional[int]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    List[Any]          =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__   )






	@slow
	def 					lowercase__	(self			:			Dict   )  ->      Optional[int]:

					"""simple docstring"""

					for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE	:    Optional[int]          =			LayoutLMvaModel.from_pretrained(lowerCamelCase__   )
									self.assertIsNotNone(lowerCamelCase__   )






def    __lowercase	():
				SCREAMING_SNAKE_CASE	:    Optional[int]          =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
				return image




@require_torch
class 						a__  (					unittest.TestCase						):


	@cached_property
	def 					lowercase__	(self			:			List[str]   )  ->      Optional[int]:

					"""simple docstring"""

					return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase__   ) if is_vision_available() else None






	@slow
	def 					lowercase__	(self			:			List[Any]   )  ->      str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE	:    str          =			LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''   ).to(lowerCamelCase__   )

					SCREAMING_SNAKE_CASE	:    str          =			self.default_image_processor
					SCREAMING_SNAKE_CASE	:    Optional[int]          =			prepare_img()
					SCREAMING_SNAKE_CASE	:    Optional[int]          =			image_processor(images=lowerCamelCase__,       return_tensors='''pt'''   ).pixel_values.to(lowerCamelCase__   )

					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			torch.tensor([[1, 2]]   )
					SCREAMING_SNAKE_CASE	:    List[str]          =			torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]   ).unsqueeze(0   )

					# forward pass
					SCREAMING_SNAKE_CASE	:    Any          =			model(
					    input_ids=input_ids.to(lowerCamelCase__   ),       bbox=bbox.to(lowerCamelCase__   ),       pixel_values=pixel_values.to(lowerCamelCase__   ),       )

					# verify the logits
					SCREAMING_SNAKE_CASE	:    Union[str, Any]          =			torch.Size((1, 199, 768)   )
					self.assertEqual(outputs.last_hidden_state.shape,       lowerCamelCase__   )

					SCREAMING_SNAKE_CASE	:    str          =			torch.tensor(
					    [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]   ).to(lowerCamelCase__   )

					self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3],       lowerCamelCase__,       atol=1e-4   )   )










# Usage:
# ./gen-card-allenai-wmt16.py

import os
from pathlib import Path







def      lowerCamelCase_						(			UpperCamelCase__							: int					,	UpperCamelCase__							: Optional[int]					,	UpperCamelCase__							: int					,	UpperCamelCase__							: str )	->			List[str]:





				"""simple docstring"""


				__lowerCamelCase									=       {
				    'en': 'Machine learning is great, isn\'t it?',
				    'ru': 'Машинное обучение - это здорово, не так ли?',
				    'de': 'Maschinelles Lernen ist großartig, nicht wahr?',
				}

				# BLUE scores as follows:
				# "pair": [fairseq, transformers]
				__lowerCamelCase									=       {
				    'wmt16-en-de-dist-12-1': [28.3, 27.52],
				    'wmt16-en-de-dist-6-1': [27.4, 27.11],
				    'wmt16-en-de-12-1': [26.9, 25.75],
				}
				__lowerCamelCase									=       F"""{src_lang}-{tgt_lang}"""

				__lowerCamelCase									=       F"""
---
language:
- {src_lang}
- {tgt_lang}
thumbnail:
tags:
- translation
- wmt16
- allenai
license: apache-2.0
datasets:
- wmt16
metrics:
- bleu
---

# FSMT

## Model description

This is a ported version of fairseq-based [wmt16 transformer](https://github.com/jungokasai/deep-shallow/) for {src_lang}-{tgt_lang}.

For more details, please, see [Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation](https://arxiv.org/abs/2006.10369).

All 3 models are available:

* [wmt16-en-de-dist-12-1](https://huggingface.co/allenai/wmt16-en-de-dist-12-1)
* [wmt16-en-de-dist-6-1](https://huggingface.co/allenai/wmt16-en-de-dist-6-1)
* [wmt16-en-de-12-1](https://huggingface.co/allenai/wmt16-en-de-12-1)


## Intended uses & limitations

#### How to use

```python
from transformers import FSMTForConditionalGeneration, FSMTTokenizer
mname = \"allenai/{model_name}\"
tokenizer = FSMTTokenizer.from_pretrained(mname)
model = FSMTForConditionalGeneration.from_pretrained(mname)

input = \"{texts[src_lang]}\"
input_ids = tokenizer.encode(input, return_tensors=\"pt\")
outputs = model.generate(input_ids)
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(decoded) # {texts[tgt_lang]}

```

#### Limitations and bias


## Training data

Pretrained weights were left identical to the original model released by allenai. For more details, please, see the [paper](https://arxiv.org/abs/2006.10369).

## Eval results

Here are the BLEU scores:

model   | fairseq | transformers
-------|---------|----------
{model_name}  | {scores[model_name][0]} | {scores[model_name][1]}

The score is slightly below the score reported in the paper, as the researchers don't use `sacrebleu` and measure the score on tokenized outputs. `transformers` score was measured using `sacrebleu` on detokenized outputs.

The score was calculated using this code:

```bash
git clone https://github.com/huggingface/transformers
cd transformers
export PAIR={pair}
export DATA_DIR=data/$PAIR
export SAVE_DIR=data/$PAIR
export BS=8
export NUM_BEAMS=5
mkdir -p $DATA_DIR
sacrebleu -t wmt16 -l $PAIR --echo src > $DATA_DIR/val.source
sacrebleu -t wmt16 -l $PAIR --echo ref > $DATA_DIR/val.target
echo $PAIR
PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py allenai/{model_name} $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS
```

## Data Sources

- [training, etc.](http://www.statmt.org/wmt16/)
- [test set](http://matrix.statmt.org/test_sets/newstest2016.tgz?1504722372)


### BibTeX entry and citation info

```
@misc{{kasai2020deep,
    title={{Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation}},
    author={{Jungo Kasai and Nikolaos Pappas and Hao Peng and James Cross and Noah A. Smith}},
    year={{2020}},
    eprint={{2006.10369}},
    archivePrefix={{arXiv}},
    primaryClass={{cs.CL}}
}}
```

"""
				model_card_dir.mkdir(parents=UpperCamelCase__					,	exist_ok=UpperCamelCase__ )
				__lowerCamelCase									=       os.path.join(UpperCamelCase__					,	'README.md' )
				print(F"""Generating {path}""" )
				with open(UpperCamelCase__					,	'w'					,	encoding='utf-8' ) as f:
								f.write(UpperCamelCase__ )


# make sure we are under the root of the project
__A								=       Path(__file__).resolve().parent.parent.parent
__A								=       repo_dir / "model_cards"

for model_name in ["wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1"]:
					__A								=       model_cards_dir / "allenai" / model_name
					write_model_card(model_card_dir, src_lang="en", tgt_lang="de", model_name=model_name)


