

'''simple docstring'''
import tempfile
import unittest

from transformers import DPRConfig, is_torch_available
from transformers.testing_utils import require_torch, 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 DPRContextEncoder, DPRQuestionEncoder, DPRReader, DPRReaderTokenizer
     from transformers.models.dpr.modeling_dpr import (
         DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
         DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
         DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
     )







class 							A__					:





  def __init__(	self   :			Any							,      _a   :			List[str]							,      _a   :			str=13							,      _a   :			int=7							,      _a   :			Any=False							,      _a   :			int=True							,      _a   :			List[Any]=True							,      _a   :			Optional[Any]=True							,      _a   :			List[Any]=99							,      _a   :			int=32							,      _a   :			List[str]=5							,      _a   :			int=4							,      _a   :			Optional[Any]=37							,      _a   :			Any="gelu"							,      _a   :			Tuple=0.1							,      _a   :			List[Any]=0.1							,      _a   :			str=512							,      _a   :			List[Any]=16							,      _a   :			str=2							,      _a   :			List[str]=0.02							,      _a   :			Dict=3							,      _a   :			List[Any]=4							,      _a   :			int=None							,      _a   :			Tuple=0							,      )			->					Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=parent
       _SCREAMING_SNAKE_CASE												=batch_size
       _SCREAMING_SNAKE_CASE												=seq_length
       _SCREAMING_SNAKE_CASE												=is_training
       _SCREAMING_SNAKE_CASE												=use_input_mask
       _SCREAMING_SNAKE_CASE												=use_token_type_ids
       _SCREAMING_SNAKE_CASE												=use_labels
       _SCREAMING_SNAKE_CASE												=vocab_size
       _SCREAMING_SNAKE_CASE												=hidden_size
       _SCREAMING_SNAKE_CASE												=num_hidden_layers
       _SCREAMING_SNAKE_CASE												=num_attention_heads
       _SCREAMING_SNAKE_CASE												=intermediate_size
       _SCREAMING_SNAKE_CASE												=hidden_act
       _SCREAMING_SNAKE_CASE												=hidden_dropout_prob
       _SCREAMING_SNAKE_CASE												=attention_probs_dropout_prob
       _SCREAMING_SNAKE_CASE												=max_position_embeddings
       _SCREAMING_SNAKE_CASE												=type_vocab_size
       _SCREAMING_SNAKE_CASE												=type_sequence_label_size
       _SCREAMING_SNAKE_CASE												=initializer_range
       _SCREAMING_SNAKE_CASE												=num_labels
       _SCREAMING_SNAKE_CASE												=num_choices
       _SCREAMING_SNAKE_CASE												=scope
       _SCREAMING_SNAKE_CASE												=projection_dim





  def 		A						(	self   :			Union[str, Any]				)			->					Optional[Any]:





       '''simple docstring'''






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

       _SCREAMING_SNAKE_CASE												=None
       if self.use_input_mask:
            _SCREAMING_SNAKE_CASE												=random_attention_mask([self.batch_size, self.seq_length]				)

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

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

       _SCREAMING_SNAKE_CASE												=self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





  def 		A						(	self   :			Union[str, Any]				)			->					Any:





       '''simple docstring'''






       return DPRConfig(
           projection_dim=self.projection_dim							,      vocab_size=self.vocab_size							,      hidden_size=self.hidden_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							,      )





  def 		A						(	self   :			Tuple							,      _a   :			int							,      _a   :			Any							,      _a   :			str							,      _a   :			List[str]							,      _a   :			List[str]							,      _a   :			int							,      _a   :			Union[str, Any]				)			->					Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=DPRContextEncoder(config=_a				)
       model.to(_a				)
       model.eval()
       _SCREAMING_SNAKE_CASE												=model(_a							,      attention_mask=_a							,      token_type_ids=_a				)
       _SCREAMING_SNAKE_CASE												=model(_a							,      token_type_ids=_a				)
       _SCREAMING_SNAKE_CASE												=model(_a				)
       self.parent.assertEqual(result.pooler_output.shape							,      (self.batch_size, self.projection_dim or self.hidden_size)				)





  def 		A						(	self   :			int							,      _a   :			Optional[int]							,      _a   :			Optional[Any]							,      _a   :			Optional[Any]							,      _a   :			Optional[Any]							,      _a   :			List[Any]							,      _a   :			Optional[int]							,      _a   :			str				)			->					int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=DPRQuestionEncoder(config=_a				)
       model.to(_a				)
       model.eval()
       _SCREAMING_SNAKE_CASE												=model(_a							,      attention_mask=_a							,      token_type_ids=_a				)
       _SCREAMING_SNAKE_CASE												=model(_a							,      token_type_ids=_a				)
       _SCREAMING_SNAKE_CASE												=model(_a				)
       self.parent.assertEqual(result.pooler_output.shape							,      (self.batch_size, self.projection_dim or self.hidden_size)				)





  def 		A						(	self   :			Optional[int]							,      _a   :			Optional[int]							,      _a   :			Dict							,      _a   :			Any							,      _a   :			List[str]							,      _a   :			Any							,      _a   :			Tuple							,      _a   :			Union[str, Any]				)			->					Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=DPRReader(config=_a				)
       model.to(_a				)
       model.eval()
       _SCREAMING_SNAKE_CASE												=model(
           _a							,      attention_mask=_a							,      )

       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)				)
       self.parent.assertEqual(result.relevance_logits.shape							,      (self.batch_size,)				)





  def 		A						(	self   :			str				)			->					Any:





       '''simple docstring'''






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







@require_torch
class 							A__					( A__  ,     A__  ,     unittest.TestCase						):
  A__					=       (
      (
          DPRContextEncoder,
          DPRQuestionEncoder,
          DPRReader,
      )
      if is_torch_available()
      else ()
  )
  A__					=       {'feature-extraction': DPRQuestionEncoder} if is_torch_available() else {}

  A__					=       False
  A__					=       False  # why?
  A__					=       False
  A__					=       False





  def 		A						(	self   :			List[str]				)			->					List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=DPRModelTester(self				)
       _SCREAMING_SNAKE_CASE												=ConfigTester(self							,      config_class=_a							,      hidden_size=37				)





  def 		A						(	self   :			Optional[Any]				)			->					int:





       '''simple docstring'''






       self.config_tester.run_common_tests()





  def 		A						(	self   :			Dict				)			->					Union[str, Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_context_encoder(*_a				)





  def 		A						(	self   :			List[Any]				)			->					int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_question_encoder(*_a				)





  def 		A						(	self   :			int				)			->					Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_reader(*_a				)





  def 		A						(	self   :			Dict				)			->					Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.model_tester.prepare_config_and_inputs()[0]

       _SCREAMING_SNAKE_CASE												=DPRQuestionEncoder(config=_a				)
       model.to(_a				)
       model.eval()

       with tempfile.TemporaryDirectory() as tmp_dirname:
            model.save_pretrained(_a				)
            _SCREAMING_SNAKE_CASE												=DPRQuestionEncoder.from_pretrained(_a							,      projection_dim=512				)

       self.assertIsNotNone(_a				)





  @slow
  def 		A						(	self   :			str				)			->					Tuple:





       '''simple docstring'''






       for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            _SCREAMING_SNAKE_CASE												=DPRContextEncoder.from_pretrained(_a				)
            self.assertIsNotNone(_a				)

       for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            _SCREAMING_SNAKE_CASE												=DPRContextEncoder.from_pretrained(_a				)
            self.assertIsNotNone(_a				)

       for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            _SCREAMING_SNAKE_CASE												=DPRQuestionEncoder.from_pretrained(_a				)
            self.assertIsNotNone(_a				)

       for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            _SCREAMING_SNAKE_CASE												=DPRReader.from_pretrained(_a				)
            self.assertIsNotNone(_a				)







@require_torch
class 							A__					( unittest.TestCase						):





  @slow
  def 		A						(	self   :			Optional[int]				)			->					Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=DPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'							,      return_dict=_a				)
       model.to(_a				)

       _SCREAMING_SNAKE_CASE												=torch.tensor(
           [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]							,      dtype=torch.long							,      device=_a				)  # [CLS] hello, is my dog cute? [SEP]
       _SCREAMING_SNAKE_CASE												=model(_a				)[0]  # embedding shape = (1, 768)
       # compare the actual values for a slice.
       _SCREAMING_SNAKE_CASE												=torch.tensor(
           [
               [
                   0.03_23_62_53,
                   0.12_75_33_35,
                   0.16_81_85_09,
                   0.00_27_97_86,
                   0.3_89_69_33,
                   0.24_26_49_45,
                   0.2_17_89_71,
                   -0.02_33_52_27,
                   -0.08_48_19_59,
                   -0.14_32_41_17,
               ]
           ]							,      dtype=torch.float							,      device=_a							,      )
       self.assertTrue(torch.allclose(output[:, :10]							,      _a							,      atol=1e-4				)				)





  @slow
  def 		A						(	self   :			Any				)			->					Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base'				)
       _SCREAMING_SNAKE_CASE												=DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base'				)
       model.to(_a				)

       _SCREAMING_SNAKE_CASE												=tokenizer(
           questions='What is love ?'							,      titles='Haddaway'							,      texts='What Is Love is a song recorded by the artist Haddaway'							,      padding=_a							,      return_tensors='pt'							,      )
       encoded_inputs.to(_a				)

       _SCREAMING_SNAKE_CASE												=model(**_a				)

       # compare the actual values for a slice.
       _SCREAMING_SNAKE_CASE												=torch.tensor(
           [[-10.30_05, -10.77_65, -11.48_72, -11.68_41, -11.93_12, -10.30_02, -9.85_44, -11.73_78, -12.08_21, -10.29_75]]							,      dtype=torch.float							,      device=_a							,      )

       _SCREAMING_SNAKE_CASE												=torch.tensor(
           [[-11.06_84, -11.70_41, -11.53_97, -10.34_65, -10.87_91, -6.84_43, -11.99_59, -11.03_64, -10.00_96, -6.84_05]]							,      dtype=torch.float							,      device=_a							,      )
       self.assertTrue(torch.allclose(outputs.start_logits[:, :10]							,      _a							,      atol=1e-4				)				)
       self.assertTrue(torch.allclose(outputs.end_logits[:, :10]							,      _a							,      atol=1e-4				)				)



'''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 							A__					( unittest.TestCase						):
  A__					=       MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
  A__					=       TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING





  def 		A						(	self   :			Optional[Any]							,      _a   :			Tuple							,      _a   :			Union[str, Any]							,      _a   :			str				)			->					List[Any]:





       '''simple docstring'''






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





  def 		A						(	self   :			Union[str, Any]							,      _a   :			List[Any]							,      _a   :			Optional[Any]				)			->					List[Any]:





       '''simple docstring'''






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

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

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





  @require_torch
  def 		A						(	self   :			Dict				)			->					Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=pipeline('translation_en_to_ro'							,      model='patrickvonplaten/t5-tiny-random'							,      framework='pt'				)
       _SCREAMING_SNAKE_CASE												=translator('This is a test string'							,      max_length=20				)
       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 		A						(	self   :			Tuple				)			->					Union[str, Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=pipeline('translation_en_to_ro'							,      model='patrickvonplaten/t5-tiny-random'							,      framework='tf'				)
       _SCREAMING_SNAKE_CASE												=translator('This is a test string'							,      max_length=20				)
       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 		A						(	self   :			Any				)			->					Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=pipeline('translation_en_to_de'							,      model='patrickvonplaten/t5-tiny-random'							,      framework='pt'				)
       _SCREAMING_SNAKE_CASE												=translator('This is a test string'							,      max_length=20				)
       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 		A						(	self   :			Dict				)			->					Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=pipeline('translation_en_to_de'							,      model='patrickvonplaten/t5-tiny-random'							,      framework='tf'				)
       _SCREAMING_SNAKE_CASE												=translator('This is a test string'							,      max_length=20				)
       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 							A__					( unittest.TestCase						):





  @require_torch
  @slow
  def 		A						(	self   :			int				)			->					Optional[Any]:





       '''simple docstring'''






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

       # but we do for this one
       _SCREAMING_SNAKE_CASE												=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 		A						(	self   :			Optional[Any]				)			->					List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=MBartForConditionalGeneration.from_pretrained('facebook/mbart-large-50-many-to-many-mmt'				)
       _SCREAMING_SNAKE_CASE												=MBartaaTokenizerFast.from_pretrained('facebook/mbart-large-50-many-to-many-mmt'				)

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

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

       _SCREAMING_SNAKE_CASE												=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
       _SCREAMING_SNAKE_CASE												=pipeline(task='translation'							,      model=_a							,      tokenizer=_a							,      src_lang='en_XX'							,      tgt_lang='ar_AR'				)
       _SCREAMING_SNAKE_CASE												=translator('This is a test'				)
       self.assertEqual(_a							,      [{'translation_text': 'هذا إختبار'}]				)





  @require_torch
  def 		A						(	self   :			int				)			->					int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												='patrickvonplaten/t5-tiny-random'
       _SCREAMING_SNAKE_CASE												=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 		A						(	self   :			str				)			->					List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												='patrickvonplaten/t5-tiny-random'
       with pytest.warns(_a							,      match=r'.*translation_en_to_de.*'				):
            _SCREAMING_SNAKE_CASE												=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 		A						(	self   :			Any				)			->					Dict:





       '''simple docstring'''






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