

'''simple docstring'''




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        lowercase_	(		unittest.TestCase ):

  '''simple docstring'''





  __lowerCAmelCase					: str      =   MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
  __lowerCAmelCase					: List[Any]      =   TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
  def 						snake_case_			(     self   , a_   , a_   , a_     )							->				Union[str, Any]:


    """simple docstring"""
    if isinstance(model.config   , a_     ):
      UpperCAmelCase							,      UpperCAmelCase													=	list(tokenizer.lang_code_to_id.keys()     )[:2]
      UpperCAmelCase													=	TranslationPipeline(model=a_   , tokenizer=a_   , src_lang=a_   , tgt_lang=a_     )
    else:
      UpperCAmelCase													=	TranslationPipeline(model=a_   , tokenizer=a_     )
    return translator, ["Some string", "Some other text"]
  def 						snake_case_			(     self   , a_   , a_     )							->				Any:


    """simple docstring"""
    UpperCAmelCase													=	translator('Some string'     )
    self.assertEqual(a_   , [{'translation_text': ANY(a_     )}]     )

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

    UpperCAmelCase													=	translator(['Some string', 'other string']     )
    self.assertEqual(a_   , [{'translation_text': ANY(a_     )}, {'translation_text': ANY(a_     )}]     )
  @require_torch
  def 						snake_case_			(     self     )							->				str:


    """simple docstring"""
    UpperCAmelCase													=	pipeline('translation_en_to_ro'   , model='patrickvonplaten/t5-tiny-random'   , framework='pt'     )
    UpperCAmelCase													=	translator('This is a test string'   , max_length=2_0     )
    self.assertEqual(
        a_   , [
            {
                'translation_text': (
                    'Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'
                    ' Beide Beide'
                )
            }
        ]   , )
  @require_tf
  def 						snake_case_			(     self     )							->				Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase													=	pipeline('translation_en_to_ro'   , model='patrickvonplaten/t5-tiny-random'   , framework='tf'     )
    UpperCAmelCase													=	translator('This is a test string'   , max_length=2_0     )
    self.assertEqual(
        a_   , [
            {
                'translation_text': (
                    'Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'
                    ' Beide Beide'
                )
            }
        ]   , )
  @require_torch
  def 						snake_case_			(     self     )							->				Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase													=	pipeline('translation_en_to_de'   , model='patrickvonplaten/t5-tiny-random'   , framework='pt'     )
    UpperCAmelCase													=	translator('This is a test string'   , max_length=2_0     )
    self.assertEqual(
        a_   , [
            {
                'translation_text': (
                    'monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'
                    ' urine urine urine urine urine urine urine'
                )
            }
        ]   , )





  @require_tf
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    UpperCAmelCase													=	pipeline('translation_en_to_de'   , model='patrickvonplaten/t5-tiny-random'   , framework='tf'     )
    UpperCAmelCase													=	translator('This is a test string'   , max_length=2_0     )
    self.assertEqual(
        a_   , [
            {
                'translation_text': (
                    'monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'
                    ' urine urine urine urine urine urine urine'
                )
            }
        ]   , )




class        lowercase_	(		unittest.TestCase ):

  '''simple docstring'''





  @require_torch
  @slow
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    # We don't provide a default for this pair
    with self.assertRaises(a_     ):
      pipeline(task='translation_cn_to_ar'     )

    # but we do for this one
    UpperCAmelCase													=	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 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    UpperCAmelCase													=	MBartForConditionalGeneration.from_pretrained('facebook/mbart-large-50-many-to-many-mmt'     )
    UpperCAmelCase													=	MBartaaTokenizerFast.from_pretrained('facebook/mbart-large-50-many-to-many-mmt'     )

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

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

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

    # src_lang, tgt_lang can be defined at pipeline call time
    UpperCAmelCase													=	pipeline(task='translation'   , model=a_   , tokenizer=a_   , src_lang='en_XX'   , tgt_lang='ar_AR'     )
    UpperCAmelCase													=	translator('This is a test'     )
    self.assertEqual(a_   , [{'translation_text': 'هذا إختبار'}]     )
  @require_torch
  def 						snake_case_			(     self     )							->				Optional[Any]:


    """simple docstring"""
    UpperCAmelCase													=	'patrickvonplaten/t5-tiny-random'
    UpperCAmelCase													=	pipeline(task='translation_cn_to_ar'   , model=a_     )
    self.assertEqual(translator._preprocess_params['src_lang']   , 'cn'     )
    self.assertEqual(translator._preprocess_params['tgt_lang']   , 'ar'     )
  @require_torch
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    UpperCAmelCase													=	'patrickvonplaten/t5-tiny-random'
    with pytest.warns(a_   , match=r'.*translation_en_to_de.*'     ):
      UpperCAmelCase													=	pipeline(task='translation'   , model=a_     )
    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 						snake_case_			(     self     )							->				Optional[Any]:


    """simple docstring"""
    with self.assertRaises(a_     ):
      pipeline(task='translation'     )





'''simple docstring'''




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_a				: Optional[int] 							=						logging.get_logger(__name__)

_a				: Dict 							=						{
    'mnaylor/mega-base-wikitext': 'https://huggingface.co/mnaylor/mega-base-wikitext/resolve/main/config.json',
}



class        lowercase_	(		a ):

  '''simple docstring'''





  __lowerCAmelCase					: List[str]      =   "mega"
  def __init__(     self   , a_=3_0_5_2_2   , a_=1_2_8   , a_=4   , a_=2_5_6   , a_=1_6   , a_=True   , a_=6_4   , a_=False   , a_=-1   , a_=None   , a_=True   , a_="scalenorm"   , a_=True   , a_="silu"   , a_="softmax"   , a_=0.1   , a_=0.1   , a_=0.1   , a_=False   , a_=True   , a_=2_5_6   , a_=True   , a_=0.1   , a_=2_0_4_8   , a_=False   , a_=2   , a_=0.02   , a_=0.2   , a_=0.02   , a_=1.0   , a_=1   , a_=0   , a_=2   , a_="rotary"   , a_=None   , a_=True   , a_=True   , **a_   , )							->				Any:


    """simple docstring"""
    super().__init__(pad_token_id=a_   , bos_token_id=a_   , eos_token_id=a_   , **a_     )

    UpperCAmelCase													=	vocab_size
    UpperCAmelCase													=	hidden_size
    UpperCAmelCase													=	num_hidden_layers
    UpperCAmelCase													=	activation
    UpperCAmelCase													=	attention_activation
    UpperCAmelCase													=	intermediate_size
    UpperCAmelCase													=	ema_projection_size
    UpperCAmelCase													=	bidirectional
    UpperCAmelCase													=	shared_representation_size
    UpperCAmelCase													=	use_chunking
    UpperCAmelCase													=	chunk_size
    UpperCAmelCase													=	truncation
    UpperCAmelCase													=	normalize_before_mega
    UpperCAmelCase													=	normalization_type
    UpperCAmelCase													=	norm_affine
    UpperCAmelCase													=	dropout_prob
    UpperCAmelCase													=	hidden_dropout_prob
    UpperCAmelCase													=	attention_probs_dropout_prob
    UpperCAmelCase													=	use_feature_dropout
    UpperCAmelCase													=	use_normalized_ffn
    UpperCAmelCase													=	nffn_hidden_size
    UpperCAmelCase													=	normalize_before_ffn
    UpperCAmelCase													=	nffn_activation_dropout_prob
    UpperCAmelCase													=	max_positions
    UpperCAmelCase													=	add_token_type_embeddings
    UpperCAmelCase													=	type_vocab_size
    UpperCAmelCase													=	initializer_range
    UpperCAmelCase													=	ema_delta_alpha_range
    UpperCAmelCase													=	ema_beta_range
    UpperCAmelCase													=	ema_gamma_omega_range
    UpperCAmelCase													=	relative_positional_bias
    UpperCAmelCase													=	use_cache
    UpperCAmelCase													=	classifier_dropout
    UpperCAmelCase													=	add_lm_hidden_dense_layer
    UpperCAmelCase													=	1  # not used but required by Hugging Face




class        lowercase_	(		a ):

  '''simple docstring'''





  @property
  def 						snake_case_			(     self     )							->				Mapping[str, Mapping[int, str]]:


    """simple docstring"""
    if self.task == "multiple-choice":
      UpperCAmelCase													=	{0: 'batch', 1: 'choice', 2: 'sequence'}
    else:
      UpperCAmelCase													=	{0: 'batch', 1: 'sequence'}
    return OrderedDict(
        [
            ('input_ids', dynamic_axis),
            ('attention_mask', dynamic_axis),
        ]     )


