





from __future__ import annotations

import random
import unittest

from transformers import TransfoXLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
         TFTransfoXLForSequenceClassification,
         TFTransfoXLLMHeadModel,
         TFTransfoXLModel,
     )

class      A       :



      def __init__(	self:						List[Any]		,  _lowerCAmelCase:						Any		,  )						->    Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_											=parent
         UpperCAmelCase_											=13
         UpperCAmelCase_											=7
         UpperCAmelCase_											=30
         UpperCAmelCase_											=self.seq_length + self.mem_len
         UpperCAmelCase_											=15
         UpperCAmelCase_											=True
         UpperCAmelCase_											=True
         UpperCAmelCase_											=99
         UpperCAmelCase_											=[10, 50, 80]
         UpperCAmelCase_											=32
         UpperCAmelCase_											=32
         UpperCAmelCase_											=4
         UpperCAmelCase_											=8
         UpperCAmelCase_											=128
         UpperCAmelCase_											=2
         UpperCAmelCase_											=2
         UpperCAmelCase_											=None
         UpperCAmelCase_											=1
         UpperCAmelCase_											=0
         UpperCAmelCase_											=3
         UpperCAmelCase_											=self.vocab_size - 1
         UpperCAmelCase_											=0.01



      def 	lowerCAmelCase__    (	self:						Any       )						->    Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_											=ids_tensor([self.batch_size, self.seq_length]		,  self.vocab_size       )
         UpperCAmelCase_											=ids_tensor([self.batch_size, self.seq_length]		,  self.vocab_size       )

         UpperCAmelCase_											=None
         if self.use_labels:
            UpperCAmelCase_											=ids_tensor([self.batch_size, self.seq_length]		,  self.vocab_size       )

         UpperCAmelCase_											=TransfoXLConfig(
             vocab_size=self.vocab_size		,  mem_len=self.mem_len		,  clamp_len=self.clamp_len		,  cutoffs=self.cutoffs		,  d_model=self.hidden_size		,  d_embed=self.d_embed		,  n_head=self.num_attention_heads		,  d_head=self.d_head		,  d_inner=self.d_inner		,  div_val=self.div_val		,  n_layer=self.num_hidden_layers		,  eos_token_id=self.eos_token_id		,  pad_token_id=self.vocab_size - 1		,  init_range=self.init_range		,  num_labels=self.num_labels		,  )

         return (config, input_ids_a, input_ids_a, lm_labels)



      def 	lowerCAmelCase__    (	self:						Tuple       )						->    str:
         '''simple docstring'''

         random.seed(self.seed       )
         tf.random.set_seed(self.seed       )



      def 	lowerCAmelCase__    (	self:						str		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						str		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Optional[int]       )						->    List[str]:
         '''simple docstring'''

         UpperCAmelCase_											=TFTransfoXLModel(_lowerCAmelCase       )

         UpperCAmelCase_						,       UpperCAmelCase_											=model(_lowerCAmelCase       ).to_tuple()

         UpperCAmelCase_											={"input_ids": input_ids_a, "mems": mems_a}

         UpperCAmelCase_						,       UpperCAmelCase_											=model(_lowerCAmelCase       ).to_tuple()

         self.parent.assertEqual(hidden_states_a.shape		,  (self.batch_size, self.seq_length, self.hidden_size)       )
         self.parent.assertEqual(hidden_states_a.shape		,  (self.batch_size, self.seq_length, self.hidden_size)       )
         self.parent.assertListEqual(
             [mem.shape for mem in mems_a]		,  [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers		,  )
         self.parent.assertListEqual(
             [mem.shape for mem in mems_a]		,  [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers		,  )



      def 	lowerCAmelCase__    (	self:						Dict		,  _lowerCAmelCase:						str		,  _lowerCAmelCase:						Optional[int]		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						List[Any]       )						->    List[str]:
         '''simple docstring'''

         UpperCAmelCase_											=TFTransfoXLLMHeadModel(_lowerCAmelCase       )

         UpperCAmelCase_						,       UpperCAmelCase_											=model(_lowerCAmelCase       ).to_tuple()

         UpperCAmelCase_											={"input_ids": input_ids_a, "labels": lm_labels}
         UpperCAmelCase_						,       UpperCAmelCase_											=model(_lowerCAmelCase       ).to_tuple()

         UpperCAmelCase_						,       UpperCAmelCase_											=model([input_ids_a, mems_a]       ).to_tuple()

         UpperCAmelCase_											={"input_ids": input_ids_a, "mems": mems_a, "labels": lm_labels}

         UpperCAmelCase_						,       UpperCAmelCase_											=model(_lowerCAmelCase       ).to_tuple()

         self.parent.assertEqual(lm_logits_a.shape		,  (self.batch_size, self.seq_length, self.vocab_size)       )
         self.parent.assertListEqual(
             [mem.shape for mem in mems_a]		,  [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers		,  )

         self.parent.assertEqual(lm_logits_a.shape		,  (self.batch_size, self.seq_length, self.vocab_size)       )
         self.parent.assertListEqual(
             [mem.shape for mem in mems_a]		,  [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers		,  )



      def 	lowerCAmelCase__    (	self:						str		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						Any       )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=TFTransfoXLForSequenceClassification(_lowerCAmelCase       )
         UpperCAmelCase_											=model(_lowerCAmelCase       )
         self.parent.assertEqual(result.logits.shape		,  (self.batch_size, self.num_labels)       )






      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_											=self.prepare_config_and_inputs()
         ((UpperCAmelCase_)						,       (UpperCAmelCase_)						,       (UpperCAmelCase_)						,       (UpperCAmelCase_))											=config_and_inputs
         UpperCAmelCase_											={"input_ids": input_ids_a}
         return config, inputs_dict

@require_tf
class      A       (	__lowercase		,      __lowercase		,      unittest.TestCase						):
      _snake_case							     =(
          (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else ()
      )
      _snake_case							     =() if is_tf_available() else ()
      _snake_case							     =(
          {
              '''feature-extraction''': TFTransfoXLModel,
              '''text-classification''': TFTransfoXLForSequenceClassification,
              '''text-generation''': TFTransfoXLLMHeadModel,
              '''zero-shot''': TFTransfoXLForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented
      _snake_case							     =False
      _snake_case							     =False
      _snake_case							     =False
      _snake_case							     =False



      def 	lowerCAmelCase__    (	self:						Any		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						List[Any]       )						->    str:
         '''simple docstring'''

         if pipeline_test_casse_name == "TextGenerationPipelineTests":
            # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`.
            # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple
            # tokenizer.
            return True

         return False



      def 	lowerCAmelCase__    (	self:						str       )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=TFTransfoXLModelTester(self       )
         UpperCAmelCase_											=ConfigTester(self		,  config_class=_lowerCAmelCase		,  d_embed=37       )



      def 	lowerCAmelCase__    (	self:						Dict       )						->    Optional[Any]:
         '''simple docstring'''

         self.config_tester.run_common_tests()



      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Optional[Any]:
         '''simple docstring'''

         self.model_tester.set_seed()
         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_transfo_xl_model(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Optional[int]:
         '''simple docstring'''

         self.model_tester.set_seed()
         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_transfo_xl_lm_head(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						List[str]       )						->    List[Any]:
         '''simple docstring'''

         UpperCAmelCase_						,       UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs_for_common()
         UpperCAmelCase_											=[TFTransfoXLForSequenceClassification]

         for model_class in self.all_model_classes:
            UpperCAmelCase_											=model_class(_lowerCAmelCase       )
            assert isinstance(model.get_input_embeddings()		,  tf.keras.layers.Layer       )
            if model_class in list_other_models_with_output_ebd:
               UpperCAmelCase_											=model.get_output_embeddings()
               assert isinstance(_lowerCAmelCase		,  tf.keras.layers.Layer       )
               UpperCAmelCase_											=model.get_bias()
               assert name is None
            else:
               UpperCAmelCase_											=model.get_output_embeddings()
               assert x is None
               UpperCAmelCase_											=model.get_bias()
               assert name is None



      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Any:
         '''simple docstring'''

         pass



      @slow
      def 	lowerCAmelCase__    (	self:						str       )						->    int:
         '''simple docstring'''

         for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCAmelCase_											=TFTransfoXLModel.from_pretrained(_lowerCAmelCase       )
            self.assertIsNotNone(_lowerCAmelCase       )






      @unittest.skip(reason="This model doesn't play well with fit() due to not returning a single loss."       )
      def 	lowerCAmelCase__    (	self:						str       )						->    Union[str, Any]:
         '''simple docstring'''

         pass

@require_tf
class      A       (	unittest.TestCase						):



      @unittest.skip("Skip test until #12651 is resolved."       )
      @slow
      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											=TFTransfoXLLMHeadModel.from_pretrained("transfo-xl-wt103"       )
         # fmt: off
         UpperCAmelCase_											=tf.convert_to_tensor([[33,1297,2,1,1009,4,1109,1_1739,4762,358,5,25,245,22,1706,17,2_0098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,4,1,329,3,6224,831,1_6002,2,8,603,7_8967,2_9546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,2_9546,54,8,3609,5,5_7211,49,4,1,277,18,8,1755,1_5691,3,341,25,416,693,4_2573,71,17,401,94,31,1_7919,2,2_9546,7873,18,1,435,23,1_1011,755,5,5167,3,7983,98,84,2,2_9546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,2_9546,824,1400,1868,2,19,160,2,311,8,5496,2,2_0920,17,25,1_5097,3,24,24,0]]		,  dtype=tf.intaa       )  # noqa: E231
         # fmt: on
         #  In 1991 , the remains of Russian Tsar Nicholas II and his family
         #  ( except for Alexei and Maria ) are discovered .
         #  The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the
         #  remainder of the story . 1883 Western Siberia ,
         #  a young Grigori Rasputin is asked by his father and a group of men to perform magic .
         #  Rasputin has a vision and denounces one of the men as a horse thief . Although his
         #  father initially slaps him for making such an accusation , Rasputin watches as the
         #  man is chased outside and beaten . Twenty years later , Rasputin sees a vision of
         #  the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous ,
         #  with people , even a bishop , begging for his blessing . <eod> </s> <eos>

         # fmt: off
         UpperCAmelCase_											=[33,1297,2,1,1009,4,1109,1_1739,4762,358,5,25,245,22,1706,17,2_0098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,4,1,329,3,6224,831,1_6002,2,8,603,7_8967,2_9546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,2_9546,54,8,3609,5,5_7211,49,4,1,277,18,8,1755,1_5691,3,341,25,416,693,4_2573,71,17,401,94,31,1_7919,2,2_9546,7873,18,1,435,23,1_1011,755,5,5167,3,7983,98,84,2,2_9546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,2_9546,824,1400,1868,2,19,160,2,311,8,5496,2,2_0920,17,25,1_5097,3,24,24,0,33,1,1857,2,1,1009,4,1109,1_1739,4762,358,5,25,245,28,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,4,1,329,3,0]  # noqa: E231
         # fmt: on
         #  In 1991, the remains of Russian Tsar Nicholas II and his family (
         #  except for Alexei and Maria ) are discovered. The voice of young son,
         #  Tsarevich Alexei Nikolaevich, narrates the remainder of the story.
         #  1883 Western Siberia, a young Grigori Rasputin is asked by his father
         #  and a group of men to perform magic. Rasputin has a vision and
         #  denounces one of the men as a horse thief. Although his father initially
         #  slaps him for making such an accusation, Rasputin watches as the man
         #  is chased outside and beaten. Twenty years later, Rasputin sees a vision
         #  of the Virgin Mary, prompting him to become a priest.
         #  Rasputin quickly becomes famous, with people, even a bishop, begging for
         #  his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar
         # Nicholas II and his family were discovered. The voice of <unk> young son,
         # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos>

         UpperCAmelCase_											=model.generate(_lowerCAmelCase		,  max_length=200		,  do_sample=_lowerCAmelCase       )
         self.assertListEqual(output_ids[0].numpy().tolist()		,  _lowerCAmelCase       )











# Usage:
# ./gen-card-facebook-wmt19.py

import os
from pathlib import Path




def     a__ (					lowercase__  ,    lowercase__  ,    lowercase__  ):






   '''simple docstring'''






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

   # BLUE scores as follows:
   # "pair": [fairseq, transformers]
   UpperCAmelCase_											={
       "ru-en": ["[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)", "39.20"],
       "en-ru": ["[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)", "33.47"],
       "en-de": ["[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)", "42.83"],
       "de-en": ["[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)", "41.35"],
   }
   UpperCAmelCase_											=F'{src_lang}-{tgt_lang}'

   UpperCAmelCase_											=F'\n---\nlanguage: \n- {src_lang}\n- {tgt_lang}\nthumbnail:\ntags:\n- translation\n- wmt19\n- facebook\nlicense: apache-2.0\ndatasets:\n- wmt19\nmetrics:\n- bleu\n---\n\n# FSMT\n\n## Model description\n\nThis is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}.\n\nFor more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616).\n\nThe abbreviation FSMT stands for FairSeqMachineTranslation\n\nAll four models are available:\n\n* [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru)\n* [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en)\n* [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de)\n* [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en)\n\n## Intended uses & limitations\n\n#### How to use\n\n```python\nfrom transformers import FSMTForConditionalGeneration, FSMTTokenizer\nmname = "facebook/wmt19-{src_lang}-{tgt_lang}"\ntokenizer = FSMTTokenizer.from_pretrained(mname)\nmodel = FSMTForConditionalGeneration.from_pretrained(mname)\n\ninput = "{texts[src_lang]}"\ninput_ids = tokenizer.encode(input, return_tensors="pt")\noutputs = model.generate(input_ids)\ndecoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\nprint(decoded) # {texts[tgt_lang]}\n\n```\n\n#### Limitations and bias\n\n- The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981)\n\n## Training data\n\nPretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616).\n\n## Eval results\n\npair   | fairseq | transformers\n-------|---------|----------\n{pair}  | {scores[pair][0]} | {scores[pair][1]}\n\nThe score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support:\n- model ensemble, therefore the best performing checkpoint was ported (``model4.pt``).\n- re-ranking\n\nThe score was calculated using this code:\n\n```bash\ngit clone https://github.com/huggingface/transformers\ncd transformers\nexport PAIR={pair}\nexport DATA_DIR=data/$PAIR\nexport SAVE_DIR=data/$PAIR\nexport BS=8\nexport NUM_BEAMS=15\nmkdir -p $DATA_DIR\nsacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source\nsacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target\necho $PAIR\nPYTHONPATH="src:examples/seq2seq" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $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\n```\nnote: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`.\n\n## Data Sources\n\n- [training, etc.](http://www.statmt.org/wmt19/)\n- [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561)\n\n\n### BibTeX entry and citation info\n\n```bibtex\n@inproceedings{{...,\n  year={{2020}},\n  title={{Facebook FAIR\'s WMT19 News Translation Task Submission}},\n  author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}},\n  booktitle={{Proc. of WMT}},\n}}\n```\n\n\n## TODO\n\n- port model ensemble (fairseq uses 4 model checkpoints)\n\n'
   os.makedirs(lowercase__  ,    exist_ok=lowercase__  )
   UpperCAmelCase_											=os.path.join(lowercase__  ,    "README.md"  )
   print(F'Generating {path}'  )
   with open(lowercase__  ,    "w"  ,    encoding="utf-8"  ) as f:
      f.write(lowercase__  )


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

for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]:
     __lowercase      ,			__lowercase      ,			__lowercase  :   Union[str, Any]						      =model_name.split("""-""")
     __lowercase  :   Optional[int]						      =model_cards_dir / """facebook""" / model_name
     write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)




