





import unittest

import pytest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    MBartaaTokenizerFast,
    MBartConfig,
    MBartForConditionalGeneration,
    TranslationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch, slow

from .test_pipelines_common import ANY







@is_pipeline_test
class 		_lowerCAmelCase(		unittest.TestCase):

  """simple docstring"""







  lowerCamelCase__     	=       MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
  lowerCamelCase__     	=       TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

  def 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase      )->   Dict:






     if isinstance(model.config    ,							lowercase_      ):
        __A   ,		__A    =  list(tokenizer.lang_code_to_id.keys()      )[:2]
        __A    =  TranslationPipeline(model=lowercase_    ,							tokenizer=lowercase_    ,							src_lang=lowercase_    ,							tgt_lang=lowercase_      )
     else:
        __A    =  TranslationPipeline(model=lowercase_    ,							tokenizer=lowercase_      )
     return translator, ["Some string", "Some other text"]

  def 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase      )->   Dict:
     __A    =  translator('''Some string'''      )
     self.assertEqual(lowercase_    ,							[{'''translation_text''': ANY(lowercase_      )}]      )

     __A    =  translator(['''Some string''']      )
     self.assertEqual(lowercase_    ,							[{'''translation_text''': ANY(lowercase_      )}]      )

     __A    =  translator(['''Some string''', '''other string''']      )
     self.assertEqual(lowercase_    ,							[{'''translation_text''': ANY(lowercase_      )}, {'''translation_text''': ANY(lowercase_      )}]      )

  @require_torch
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   List[str]:
     __A    =  pipeline('''translation_en_to_ro'''    ,							model='''patrickvonplaten/t5-tiny-random'''    ,							framework='''pt'''      )
     __A    =  translator('''This is a test string'''    ,							max_length=20      )
     self.assertEqual(
         lowercase_    ,							[
             {
                 '''translation_text''': (
                     '''Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'''
                     ''' Beide Beide'''
                 )
             }
         ]    ,							)

  @require_tf
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   int:
     __A    =  pipeline('''translation_en_to_ro'''    ,							model='''patrickvonplaten/t5-tiny-random'''    ,							framework='''tf'''      )
     __A    =  translator('''This is a test string'''    ,							max_length=20      )
     self.assertEqual(
         lowercase_    ,							[
             {
                 '''translation_text''': (
                     '''Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'''
                     ''' Beide Beide'''
                 )
             }
         ]    ,							)

  @require_torch
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   int:
     __A    =  pipeline('''translation_en_to_de'''    ,							model='''patrickvonplaten/t5-tiny-random'''    ,							framework='''pt'''      )
     __A    =  translator('''This is a test string'''    ,							max_length=20      )
     self.assertEqual(
         lowercase_    ,							[
             {
                 '''translation_text''': (
                     '''monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'''
                     ''' urine urine urine urine urine urine urine'''
                 )
             }
         ]    ,							)





  @require_tf
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   Union[str, Any]:
     __A    =  pipeline('''translation_en_to_de'''    ,							model='''patrickvonplaten/t5-tiny-random'''    ,							framework='''tf'''      )
     __A    =  translator('''This is a test string'''    ,							max_length=20      )
     self.assertEqual(
         lowercase_    ,							[
             {
                 '''translation_text''': (
                     '''monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'''
                     ''' urine urine urine urine urine urine urine'''
                 )
             }
         ]    ,							)







class 		_lowerCAmelCase(		unittest.TestCase):

  """simple docstring"""







  @require_torch
  @slow
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   Dict:






     with self.assertRaises(lowercase_      ):
        pipeline(task='''translation_cn_to_ar'''      )

     # but we do for this one
     __A    =  pipeline(task='''translation_en_to_de'''      )
     self.assertEqual(translator._preprocess_params['''src_lang''']    ,							'''en'''      )
     self.assertEqual(translator._preprocess_params['''tgt_lang''']    ,							'''de'''      )

  @require_torch
  @slow
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   List[str]:
     __A    =  MBartForConditionalGeneration.from_pretrained('''facebook/mbart-large-50-many-to-many-mmt'''      )
     __A    =  MBartaaTokenizerFast.from_pretrained('''facebook/mbart-large-50-many-to-many-mmt'''      )

     __A    =  pipeline(task='''translation'''    ,							model=lowercase_    ,							tokenizer=lowercase_      )
     # Missing src_lang, tgt_lang
     with self.assertRaises(lowercase_      ):
        translator('''This is a test'''      )

     __A    =  translator('''This is a test'''    ,							src_lang='''en_XX'''    ,							tgt_lang='''ar_AR'''      )
     self.assertEqual(lowercase_    ,							[{'''translation_text''': '''هذا إختبار'''}]      )

     __A    =  translator('''This is a test'''    ,							src_lang='''en_XX'''    ,							tgt_lang='''hi_IN'''      )
     self.assertEqual(lowercase_    ,							[{'''translation_text''': '''यह एक परीक्षण है'''}]      )

     # src_lang, tgt_lang can be defined at pipeline call time
     __A    =  pipeline(task='''translation'''    ,							model=lowercase_    ,							tokenizer=lowercase_    ,							src_lang='''en_XX'''    ,							tgt_lang='''ar_AR'''      )
     __A    =  translator('''This is a test'''      )
     self.assertEqual(lowercase_    ,							[{'''translation_text''': '''هذا إختبار'''}]      )

  @require_torch
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   Optional[Any]:
     __A    =  '''patrickvonplaten/t5-tiny-random'''
     __A    =  pipeline(task='''translation_cn_to_ar'''    ,							model=lowercase_      )
     self.assertEqual(translator._preprocess_params['''src_lang''']    ,							'''cn'''      )
     self.assertEqual(translator._preprocess_params['''tgt_lang''']    ,							'''ar'''      )

  @require_torch
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   Any:
     __A    =  '''patrickvonplaten/t5-tiny-random'''
     with pytest.warns(lowercase_    ,							match=r'''.*translation_en_to_de.*'''      ):
        __A    =  pipeline(task='''translation'''    ,							model=lowercase_      )
     self.assertEqual(translator.task    ,							'''translation_en_to_de'''      )
     self.assertEqual(translator._preprocess_params['''src_lang''']    ,							'''en'''      )
     self.assertEqual(translator._preprocess_params['''tgt_lang''']    ,							'''de'''      )





  @require_torch
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   str:






     with self.assertRaises(lowercase_      ):
        pipeline(task='''translation'''      )




import unittest

import numpy as np

from transformers import ElectraConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
       from transformers.models.electra.modeling_flax_electra import (
           FlaxElectraForCausalLM,
           FlaxElectraForMaskedLM,
           FlaxElectraForMultipleChoice,
           FlaxElectraForPreTraining,
           FlaxElectraForQuestionAnswering,
           FlaxElectraForSequenceClassification,
           FlaxElectraForTokenClassification,
           FlaxElectraModel,
       )







class 		__magic_name__     (				unittest.TestCase				):

   '''simple docstring'''




   def __init__(							self,       lowercase_,       lowercase_=13,       lowercase_=7,       lowercase_=True,       lowercase_=True,       lowercase_=True,       lowercase_=True,       lowercase_=99,       lowercase_=24,       lowercase_=32,       lowercase_=5,       lowercase_=4,       lowercase_=37,       lowercase_="gelu",       lowercase_=0.1,       lowercase_=0.1,       lowercase_=512,       lowercase_=16,       lowercase_=2,       lowercase_=0.02,       lowercase_=4,       )				->       int:






     """simple docstring"""






     a__    =parent
     a__    =batch_size
     a__    =seq_length
     a__    =is_training
     a__    =use_attention_mask
     a__    =use_token_type_ids
     a__    =use_labels
     a__    =vocab_size
     a__    =hidden_size
     a__    =embedding_size
     a__    =num_hidden_layers
     a__    =num_attention_heads
     a__    =intermediate_size
     a__    =hidden_act
     a__    =hidden_dropout_prob
     a__    =attention_probs_dropout_prob
     a__    =max_position_embeddings
     a__    =type_vocab_size
     a__    =type_sequence_label_size
     a__    =initializer_range
     a__    =num_choices



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






     """simple docstring"""






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

     a__    =None
     if self.use_attention_mask:
       a__    =random_attention_mask([self.batch_size, self.seq_length]			)

     a__    =None
     if self.use_token_type_ids:
       a__    =ids_tensor([self.batch_size, self.seq_length],       self.type_vocab_size			)

     a__    =ElectraConfig(
         vocab_size=self.vocab_size,       hidden_size=self.hidden_size,       embedding_size=self.embedding_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,       )

     return config, input_ids, token_type_ids, attention_mask



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






     """simple docstring"""






     a__    =self.prepare_config_and_inputs()
     a__, a__, a__, a__    =config_and_inputs
     a__    ={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
     return config, inputs_dict







@require_flax
class 		__magic_name__     (				lowerCamelCase__						,			unittest.TestCase				):

   '''simple docstring'''




   lowerCamelCase__					:      List[str]        =  True

   lowerCamelCase__					:      Tuple        =  (
       (
           FlaxElectraModel,
           FlaxElectraForCausalLM,
           FlaxElectraForMaskedLM,
           FlaxElectraForPreTraining,
           FlaxElectraForTokenClassification,
           FlaxElectraForQuestionAnswering,
           FlaxElectraForMultipleChoice,
           FlaxElectraForSequenceClassification,
       )
       if is_flax_available()
       else ()
   )



   def 					_UpperCAmelCase					(							self			)				->       str:






     """simple docstring"""






     a__    =FlaxElectraModelTester(self			)



   @slow
   def 					_UpperCAmelCase					(							self			)				->       str:






     """simple docstring"""






     for model_class_name in self.all_model_classes:
       if model_class_name == FlaxElectraForMaskedLM:
         a__    =model_class_name.from_pretrained('''google/electra-small-generator'''			)
       else:
         a__    =model_class_name.from_pretrained('''google/electra-small-discriminator'''			)
       a__    =model(np.ones((1, 1)			)			)
       self.assertIsNotNone(lowercase_			)
