


import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

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


if is_torch_available():
   import torch

   from transformers import (
       FlaubertForMultipleChoice,
       FlaubertForQuestionAnswering,
       FlaubertForQuestionAnsweringSimple,
       FlaubertForSequenceClassification,
       FlaubertForTokenClassification,
       FlaubertModel,
       FlaubertWithLMHeadModel,
   )
   from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST
class 		__magic_name__						(A__				):





     '''simple docstring'''



     def __init__(					self:List[str]     ,		_a:Tuple     ,		_a:List[str]=13     ,		_a:int=7     ,		_a:Dict=True     ,		_a:Optional[int]=True     ,		_a:Any=True     ,		_a:int=True     ,		_a:Dict=True     ,		_a:int=False     ,		_a:List[Any]=False     ,		_a:Dict=False     ,		_a:Any=2     ,		_a:int=99     ,		_a:Tuple=0     ,		_a:str=32     ,		_a:Union[str, Any]=5     ,		_a:Optional[int]=4     ,		_a:Any=0.1     ,		_a:Tuple=0.1     ,		_a:Optional[Any]=5_12     ,		_a:int=12     ,		_a:Tuple=2     ,		_a:int=0.02     ,		_a:Tuple=3     ,		_a:Optional[int]=4     ,		_a:Tuple="last"     ,		_a:Dict=None     ,		_a:Union[str, Any]=None     ,		):
       snake_case__						= parent
       snake_case__						= batch_size
       snake_case__						= seq_length
       snake_case__						= is_training
       snake_case__						= use_input_lengths
       snake_case__						= use_token_type_ids
       snake_case__						= use_labels
       snake_case__						= gelu_activation
       snake_case__						= sinusoidal_embeddings
       snake_case__						= causal
       snake_case__						= asm
       snake_case__						= n_langs
       snake_case__						= vocab_size
       snake_case__						= n_special
       snake_case__						= hidden_size
       snake_case__						= num_hidden_layers
       snake_case__						= num_attention_heads
       snake_case__						= hidden_dropout_prob
       snake_case__						= attention_probs_dropout_prob
       snake_case__						= max_position_embeddings
       snake_case__						= type_vocab_size
       snake_case__						= type_sequence_label_size
       snake_case__						= initializer_range
       snake_case__						= num_labels
       snake_case__						= num_choices
       snake_case__						= summary_type
       snake_case__						= use_proj
       snake_case__						= scope

     def 	SCREAMING_SNAKE_CASE__						(					self:List[Any]   ):
       snake_case__						= ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size   )
       snake_case__						= random_attention_mask([self.batch_size, self.seq_length]   )

       snake_case__						= None
       if self.use_input_lengths:
         snake_case__						= (
             ids_tensor([self.batch_size]     ,		vocab_size=2   ) + self.seq_length - 2
         )  # small variation of seq_length

       snake_case__						= None
       if self.use_token_type_ids:
         snake_case__						= ids_tensor([self.batch_size, self.seq_length]     ,		self.n_langs   )

       snake_case__						= None
       snake_case__						= None
       snake_case__						= None
       if self.use_labels:
         snake_case__						= ids_tensor([self.batch_size]     ,		self.type_sequence_label_size   )
         snake_case__						= ids_tensor([self.batch_size, self.seq_length]     ,		self.num_labels   )
         snake_case__						= ids_tensor([self.batch_size]     ,		2   ).float()
         snake_case__						= ids_tensor([self.batch_size]     ,		self.num_choices   )

       snake_case__						= self.get_config()

       return (
           config,
           input_ids,
           token_type_ids,
           input_lengths,
           sequence_labels,
           token_labels,
           is_impossible_labels,
           choice_labels,
           input_mask,
       )

     def 	SCREAMING_SNAKE_CASE__						(					self:int   ):
       return FlaubertConfig(
           vocab_size=self.vocab_size     ,		n_special=self.n_special     ,		emb_dim=self.hidden_size     ,		n_layers=self.num_hidden_layers     ,		n_heads=self.num_attention_heads     ,		dropout=self.hidden_dropout_prob     ,		attention_dropout=self.attention_probs_dropout_prob     ,		gelu_activation=self.gelu_activation     ,		sinusoidal_embeddings=self.sinusoidal_embeddings     ,		asm=self.asm     ,		causal=self.causal     ,		n_langs=self.n_langs     ,		max_position_embeddings=self.max_position_embeddings     ,		initializer_range=self.initializer_range     ,		summary_type=self.summary_type     ,		use_proj=self.use_proj     ,		)

     def 	SCREAMING_SNAKE_CASE__						(					self:int     ,		_a:Optional[Any]     ,		_a:List[str]     ,		_a:Tuple     ,		_a:Optional[int]     ,		_a:Any     ,		_a:List[Any]     ,		_a:Optional[Any]     ,		_a:List[str]     ,		_a:Optional[Any]     ,		):
       snake_case__						= FlaubertModel(config=snake_case_   )
       model.to(snake_case_   )
       model.eval()
       snake_case__						= model(snake_case_     ,		lengths=snake_case_     ,		langs=snake_case_   )
       snake_case__						= model(snake_case_     ,		langs=snake_case_   )
       snake_case__						= model(snake_case_   )
       self.parent.assertEqual(result.last_hidden_state.shape     ,		(self.batch_size, self.seq_length, self.hidden_size)   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict     ,		_a:List[Any]     ,		_a:List[str]     ,		_a:List[str]     ,		_a:int     ,		_a:Union[str, Any]     ,		_a:Any     ,		_a:str     ,		_a:Dict     ,		_a:int     ,		):
       snake_case__						= FlaubertWithLMHeadModel(snake_case_   )
       model.to(snake_case_   )
       model.eval()

       snake_case__						= model(snake_case_     ,		token_type_ids=snake_case_     ,		labels=snake_case_   )
       self.parent.assertEqual(result.loss.shape     ,		()   )
       self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.vocab_size)   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple     ,		_a:Dict     ,		_a:str     ,		_a:Optional[Any]     ,		_a:List[Any]     ,		_a:Tuple     ,		_a:Optional[int]     ,		_a:Tuple     ,		_a:List[str]     ,		_a:List[str]     ,		):
       snake_case__						= FlaubertForQuestionAnsweringSimple(snake_case_   )
       model.to(snake_case_   )
       model.eval()

       snake_case__						= model(snake_case_   )

       snake_case__						= model(snake_case_     ,		start_positions=snake_case_     ,		end_positions=snake_case_   )
       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 	SCREAMING_SNAKE_CASE__						(					self:Dict     ,		_a:Dict     ,		_a:Tuple     ,		_a:List[str]     ,		_a:Any     ,		_a:Any     ,		_a:Any     ,		_a:Tuple     ,		_a:str     ,		_a:List[Any]     ,		):
       snake_case__						= FlaubertForQuestionAnswering(snake_case_   )
       model.to(snake_case_   )
       model.eval()

       snake_case__						= model(snake_case_   )

       snake_case__						= model(
           snake_case_     ,		start_positions=snake_case_     ,		end_positions=snake_case_     ,		cls_index=snake_case_     ,		is_impossible=snake_case_     ,		p_mask=snake_case_     ,		)

       snake_case__						= model(
           snake_case_     ,		start_positions=snake_case_     ,		end_positions=snake_case_     ,		cls_index=snake_case_     ,		is_impossible=snake_case_     ,		)

       ((snake_case__     )				,				     )						= result_with_labels.to_tuple()

       snake_case__						= model(snake_case_     ,		start_positions=snake_case_     ,		end_positions=snake_case_   )

       ((snake_case__     )				,				     )						= result_with_labels.to_tuple()

       self.parent.assertEqual(result_with_labels.loss.shape     ,		()   )
       self.parent.assertEqual(result.start_top_log_probs.shape     ,		(self.batch_size, model.config.start_n_top)   )
       self.parent.assertEqual(result.start_top_index.shape     ,		(self.batch_size, model.config.start_n_top)   )
       self.parent.assertEqual(
           result.end_top_log_probs.shape     ,		(self.batch_size, model.config.start_n_top * model.config.end_n_top)   )
       self.parent.assertEqual(
           result.end_top_index.shape     ,		(self.batch_size, model.config.start_n_top * model.config.end_n_top)   )
       self.parent.assertEqual(result.cls_logits.shape     ,		(self.batch_size,)   )

     def 	SCREAMING_SNAKE_CASE__						(					self:List[Any]     ,		_a:Tuple     ,		_a:Tuple     ,		_a:Union[str, Any]     ,		_a:str     ,		_a:List[str]     ,		_a:List[str]     ,		_a:Tuple     ,		_a:Dict     ,		_a:str     ,		):
       snake_case__						= FlaubertForSequenceClassification(snake_case_   )
       model.to(snake_case_   )
       model.eval()

       snake_case__						= model(snake_case_   )
       snake_case__						= model(snake_case_     ,		labels=snake_case_   )

       self.parent.assertEqual(result.loss.shape     ,		()   )
       self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.type_sequence_label_size)   )

     def 	SCREAMING_SNAKE_CASE__						(					self:str     ,		_a:str     ,		_a:Tuple     ,		_a:Union[str, Any]     ,		_a:Optional[int]     ,		_a:Any     ,		_a:Any     ,		_a:str     ,		_a:List[Any]     ,		_a:Optional[Any]     ,		):
       snake_case__						= self.num_labels
       snake_case__						= FlaubertForTokenClassification(snake_case_   )
       model.to(snake_case_   )
       model.eval()

       snake_case__						= model(snake_case_     ,		attention_mask=snake_case_     ,		labels=snake_case_   )
       self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.num_labels)   )

     def 	SCREAMING_SNAKE_CASE__						(					self:int     ,		_a:str     ,		_a:Optional[Any]     ,		_a:int     ,		_a:Any     ,		_a:Union[str, Any]     ,		_a:Optional[Any]     ,		_a:Optional[int]     ,		_a:str     ,		_a:Union[str, Any]     ,		):
       snake_case__						= self.num_choices
       snake_case__						= FlaubertForMultipleChoice(config=snake_case_   )
       model.to(snake_case_   )
       model.eval()
       snake_case__						= input_ids.unsqueeze(1   ).expand(-1     ,		self.num_choices     ,		-1   ).contiguous()
       snake_case__						= token_type_ids.unsqueeze(1   ).expand(-1     ,		self.num_choices     ,		-1   ).contiguous()
       snake_case__						= input_mask.unsqueeze(1   ).expand(-1     ,		self.num_choices     ,		-1   ).contiguous()
       snake_case__						= model(
           snake_case_     ,		attention_mask=snake_case_     ,		token_type_ids=snake_case_     ,		labels=snake_case_     ,		)
       self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_choices)   )







     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple   ):
       snake_case__						= self.prepare_config_and_inputs()
       (
           (
           snake_case__     
       )				,				(
           snake_case__     
       )				,				(
           snake_case__     
       )				,				(
           snake_case__     
       )				,				(
           snake_case__     
       )				,				(
           snake_case__     
       )				,				(
           snake_case__     
       )				,				(
           snake_case__     
       )				,				(
           snake_case__     
       )				,				     
       )						= config_and_inputs
       snake_case__						= {
           '''input_ids''': input_ids,
           '''token_type_ids''': token_type_ids,
           '''lengths''': input_lengths,
           '''attention_mask''': input_mask,
       }
       return config, inputs_dict




@require_torch
class 		__magic_name__						(A__   ,A__   ,unittest.TestCase				):





     '''simple docstring'''



     __lowercase				:							str	   =       (
         (
             FlaubertModel,
             FlaubertWithLMHeadModel,
             FlaubertForQuestionAnswering,
             FlaubertForQuestionAnsweringSimple,
             FlaubertForSequenceClassification,
             FlaubertForTokenClassification,
             FlaubertForMultipleChoice,
         )
         if is_torch_available()
         else ()
     )
     __lowercase				:							Union[str, Any]	   =       (
         {
             'feature-extraction': FlaubertModel,
             'fill-mask': FlaubertWithLMHeadModel,
             'question-answering': FlaubertForQuestionAnsweringSimple,
             'text-classification': FlaubertForSequenceClassification,
             'token-classification': FlaubertForTokenClassification,
             'zero-shot': FlaubertForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )

     def 	SCREAMING_SNAKE_CASE__						(					self:str     ,		_a:str     ,		_a:Union[str, Any]     ,		_a:List[Any]     ,		_a:List[Any]     ,		_a:Optional[int]   ):
       if (
           pipeline_test_casse_name == "QAPipelineTests"
           and tokenizer_name is not None
           and not tokenizer_name.endswith('''Fast'''   )
       ):
         # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
         # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
         # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
         return True

       return False

     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]     ,		_a:List[Any]     ,		_a:Tuple     ,		_a:str=False   ):
       snake_case__						= super()._prepare_for_class(snake_case_     ,		snake_case_     ,		return_labels=snake_case_   )

       if return_labels:
         if model_class.__name__ == "FlaubertForQuestionAnswering":
           snake_case__						= torch.zeros(
               self.model_tester.batch_size     ,		dtype=torch.long     ,		device=snake_case_   )
           snake_case__						= torch.zeros(
               self.model_tester.batch_size     ,		dtype=torch.long     ,		device=snake_case_   )

       return inputs_dict

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict   ):
       snake_case__						= FlaubertModelTester(self   )
       snake_case__						= ConfigTester(self     ,		config_class=snake_case_     ,		emb_dim=37   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]   ):
       self.config_tester.run_common_tests()

     def 	SCREAMING_SNAKE_CASE__						(					self:Any   ):
       snake_case__						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_model(*snake_case_   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Union[str, Any]   ):
       snake_case__						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_lm_head(*snake_case_   )

     def 	SCREAMING_SNAKE_CASE__						(					self:List[Any]   ):
       snake_case__						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_simple_qa(*snake_case_   )

     def 	SCREAMING_SNAKE_CASE__						(					self:str   ):
       snake_case__						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_qa(*snake_case_   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Union[str, Any]   ):
       snake_case__						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case_   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Any   ):
       snake_case__						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_token_classif(*snake_case_   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict   ):
       snake_case__						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_multiple_choice(*snake_case_   )

     @slow
     def 	SCREAMING_SNAKE_CASE__						(					self:Union[str, Any]   ):
       for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case__						= FlaubertModel.from_pretrained(snake_case_   )
         self.assertIsNotNone(snake_case_   )







     @slow
     @require_torch_gpu
     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple   ):
       snake_case__				,				snake_case__						= self.model_tester.prepare_config_and_inputs_for_common()
       for model_class in self.all_model_classes:
         # FlauBertForMultipleChoice behaves incorrectly in JIT environments.
         if model_class == FlaubertForMultipleChoice:
           return

         snake_case__						= True
         snake_case__						= model_class(config=snake_case_   )

         snake_case__						= self._prepare_for_class(snake_case_     ,		snake_case_   )
         snake_case__						= torch.jit.trace(
             snake_case_     ,		(inputs_dict['''input_ids'''].to('''cpu'''   ), inputs_dict['''attention_mask'''].to('''cpu'''   ))   )

         with tempfile.TemporaryDirectory() as tmp:
           torch.jit.save(snake_case_     ,		os.path.join(snake_case_     ,		'''traced_model.pt'''   )   )
           snake_case__						= torch.jit.load(os.path.join(snake_case_     ,		'''traced_model.pt'''   )     ,		map_location=snake_case_   )
           loaded(inputs_dict['''input_ids'''].to(snake_case_   )     ,		inputs_dict['''attention_mask'''].to(snake_case_   )   )




@require_torch
class 		__magic_name__						(unittest.TestCase				):





     '''simple docstring'''



     @slow
     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple   ):
       snake_case__						= FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''   )
       snake_case__						= torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]   )
       with torch.no_grad():
         snake_case__						= model(snake_case_   )[0]
       snake_case__						= torch.Size((1, 11, 7_68)   )
       self.assertEqual(output.shape     ,		snake_case_   )
       snake_case__						= torch.tensor(
           [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]]   )

       self.assertTrue(torch.allclose(output[:, :3, :3]     ,		snake_case_     ,		atol=1e-4   )   )



"""simple docstring"""
# Usage:
# ./gen-card-allenai-wmt16.py

import os
from pathlib import Path

def  lowercase     (_lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase						):
						__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=_lowerCAmelCase ,    exist_ok=_lowerCAmelCase						)
						__lowerCAmelCase     =  os.path.join(_lowerCAmelCase ,    """README.md"""						)
						print(f"""Generating {path}"""						)
						with open(_lowerCAmelCase ,    """w""" ,    encoding="""utf-8"""						) as f:
												f.write(_lowerCAmelCase						)


# make sure we are under the root of the project
SCREAMING_SNAKE_CASE_					     =	Path(__file__).resolve().parent.parent.parent
SCREAMING_SNAKE_CASE_					     =	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"]:
			SCREAMING_SNAKE_CASE_					     =	model_cards_dir / '''allenai''' / model_name
			write_model_card(model_card_dir, src_lang='''en''', tgt_lang='''de''', model_name=model_name)
