


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

     '''simple docstring'''
     A_							:						Union[str, Any]     =		MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
     A_							:						Optional[Any]     =		TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING


     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)							->				int:
          if isinstance(model.config     ,					__UpperCAmelCase				):
               _a						,   _a     						=					list(tokenizer.lang_code_to_id.keys()				)[:2]
               _a     						=					TranslationPipeline(model=__UpperCAmelCase     ,					tokenizer=__UpperCAmelCase     ,					src_lang=__UpperCAmelCase     ,					tgt_lang=__UpperCAmelCase				)
          else:
               _a     						=					TranslationPipeline(model=__UpperCAmelCase     ,					tokenizer=__UpperCAmelCase				)
          return translator, ["Some string", "Some other text"]


     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)							->				Optional[int]:
          _a     						=					translator('''Some string'''				)
          self.assertEqual(__UpperCAmelCase     ,					[{'''translation_text''': ANY(__UpperCAmelCase				)}]				)

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

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


     @require_torch
     def 				_UpperCAmelCase   (   self				)							->				Any:
          _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(
              __UpperCAmelCase     ,					[
                  {
                      '''translation_text''': (
                          '''Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'''
                          ''' Beide Beide'''
                      )
                  }
              ]     ,					)


     @require_tf
     def 				_UpperCAmelCase   (   self				)							->				Dict:
          _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(
              __UpperCAmelCase     ,					[
                  {
                      '''translation_text''': (
                          '''Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'''
                          ''' Beide Beide'''
                      )
                  }
              ]     ,					)


     @require_torch
     def 				_UpperCAmelCase   (   self				)							->				List[str]:
          _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(
              __UpperCAmelCase     ,					[
                  {
                      '''translation_text''': (
                          '''monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'''
                          ''' urine urine urine urine urine urine urine'''
                      )
                  }
              ]     ,					)





     @require_tf
     def 				_UpperCAmelCase   (   self				)							->				Optional[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(
              __UpperCAmelCase     ,					[
                  {
                      '''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 				_UpperCAmelCase   (   self				)							->				Union[str, Any]:
          # We don't provide a default for this pair
          with self.assertRaises(__UpperCAmelCase				):
               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 				_UpperCAmelCase   (   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=__UpperCAmelCase     ,					tokenizer=__UpperCAmelCase				)
          # Missing src_lang, tgt_lang
          with self.assertRaises(__UpperCAmelCase				):
               translator('''This is a test'''				)

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

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

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


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


     @require_torch
     def 				_UpperCAmelCase   (   self				)							->				Optional[Any]:
          _a     						=					'''patrickvonplaten/t5-tiny-random'''
          with pytest.warns(__UpperCAmelCase     ,					match=r'''.*translation_en_to_de.*'''				):
               _a     						=					pipeline(task='''translation'''     ,					model=__UpperCAmelCase				)
          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 				_UpperCAmelCase   (   self				)							->				Dict:
          with self.assertRaises(__UpperCAmelCase				):
               pipeline(task='''translation'''				)



"""simple docstring"""






from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

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


if TYPE_CHECKING:
   from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType


__snake_case												=							logging.get_logger(__name__)

__snake_case												=							{
    '''microsoft/deberta-base''': '''https://huggingface.co/microsoft/deberta-base/resolve/main/config.json''',
    '''microsoft/deberta-large''': '''https://huggingface.co/microsoft/deberta-large/resolve/main/config.json''',
    '''microsoft/deberta-xlarge''': '''https://huggingface.co/microsoft/deberta-xlarge/resolve/main/config.json''',
    '''microsoft/deberta-base-mnli''': '''https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/config.json''',
    '''microsoft/deberta-large-mnli''': '''https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/config.json''',
    '''microsoft/deberta-xlarge-mnli''': '''https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/config.json''',
}


class      __lowerCamelCase	(       a__					):

     '''simple docstring'''
     A_							:						List[Any]     =		'deberta'


     def __init__(   self     ,					__UpperCAmelCase=50265     ,					__UpperCAmelCase=768     ,					__UpperCAmelCase=12     ,					__UpperCAmelCase=12     ,					__UpperCAmelCase=3072     ,					__UpperCAmelCase="gelu"     ,					__UpperCAmelCase=0.1     ,					__UpperCAmelCase=0.1     ,					__UpperCAmelCase=512     ,					__UpperCAmelCase=0     ,					__UpperCAmelCase=0.02     ,					__UpperCAmelCase=1e-7     ,					__UpperCAmelCase=False     ,					__UpperCAmelCase=-1     ,					__UpperCAmelCase=0     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=None     ,					__UpperCAmelCase=0     ,					__UpperCAmelCase="gelu"     ,					**__UpperCAmelCase     ,					)							->				Any:
          super().__init__(**__UpperCAmelCase				)

          _a     						=					hidden_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     						=					initializer_range
          _a     						=					relative_attention
          _a     						=					max_relative_positions
          _a     						=					pad_token_id
          _a     						=					position_biased_input

          # Backwards compatibility
          if type(__UpperCAmelCase				) == str:
               _a     						=					[x.strip() for x in pos_att_type.lower().split('''|'''				)]

          _a     						=					pos_att_type
          _a     						=					vocab_size
          _a     						=					layer_norm_eps

          _a     						=					kwargs.get('''pooler_hidden_size'''     ,					__UpperCAmelCase				)
          _a     						=					pooler_dropout
          _a     						=					pooler_hidden_act







class      __lowerCamelCase	(       a__					):

     '''simple docstring'''


     @property
     def 				_UpperCAmelCase   (   self				)							->				Mapping[str, Mapping[int, str]]:
          if self.task == "multiple-choice":
               _a     						=					{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
          else:
               _a     						=					{0: '''batch''', 1: '''sequence'''}
          if self._config.type_vocab_size > 0:
               return OrderedDict(
                   [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)]				)
          else:
               return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)]				)


     @property
     def 				_UpperCAmelCase   (   self				)							->				int:
          return 12





     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase = -1     ,					__UpperCAmelCase = -1     ,					__UpperCAmelCase = -1     ,					__UpperCAmelCase = False     ,					__UpperCAmelCase = None     ,					__UpperCAmelCase = 3     ,					__UpperCAmelCase = 40     ,					__UpperCAmelCase = 40     ,					__UpperCAmelCase = None     ,					)							->				Mapping[str, Any]:
          _a     						=					super().generate_dummy_inputs(preprocessor=__UpperCAmelCase     ,					framework=__UpperCAmelCase				)
          if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs:
               del dummy_inputs["token_type_ids"]
          return dummy_inputs