

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__     :



 """simple docstring"""







 def __init__(		self			:						List[str]  ,    A_			:						List[str]  ,    A_			:						Optional[Any]=1_3  ,    A_			:						Union[str, Any]=7  ,    A_			:						List[Any]=False  ,    A_			:						int=True  ,    A_			:						Optional[int]=True  ,    A_			:						List[Any]=True  ,    A_			:						str=9_9  ,    A_			:						List[Any]=3_2  ,    A_			:						Union[str, Any]=5  ,    A_			:						Dict=4  ,    A_			:						Union[str, Any]=3_7  ,    A_			:						int="gelu"  ,    A_			:						List[Any]=0.1  ,    A_			:						Optional[int]=0.1  ,    A_			:						List[Any]=5_1_2  ,    A_			:						List[Any]=1_6  ,    A_			:						str=2  ,    A_			:						Optional[int]=0.02  ,    A_			:						Union[str, Any]=3  ,    A_			:						Tuple=4  ,    A_			:						Any=None  ,    A_			:						Optional[int]=0  ,    ):






      '''simple docstring'''




      _lowerCAmelCase :       Tuple	   =     parent
      _lowerCAmelCase :       Optional[int]	   =     batch_size
      _lowerCAmelCase :       List[str]	   =     seq_length
      _lowerCAmelCase :       Any	   =     is_training
      _lowerCAmelCase :       Union[str, Any]	   =     use_input_mask
      _lowerCAmelCase :       List[Any]	   =     use_token_type_ids
      _lowerCAmelCase :       Any	   =     use_labels
      _lowerCAmelCase :       List[str]	   =     vocab_size
      _lowerCAmelCase :       str	   =     hidden_size
      _lowerCAmelCase :       Tuple	   =     num_hidden_layers
      _lowerCAmelCase :       Optional[Any]	   =     num_attention_heads
      _lowerCAmelCase :       Optional[int]	   =     intermediate_size
      _lowerCAmelCase :       Any	   =     hidden_act
      _lowerCAmelCase :       Any	   =     hidden_dropout_prob
      _lowerCAmelCase :       List[str]	   =     attention_probs_dropout_prob
      _lowerCAmelCase :       str	   =     max_position_embeddings
      _lowerCAmelCase :       Optional[Any]	   =     type_vocab_size
      _lowerCAmelCase :       Union[str, Any]	   =     type_sequence_label_size
      _lowerCAmelCase :       List[str]	   =     initializer_range
      _lowerCAmelCase :       List[str]	   =     num_labels
      _lowerCAmelCase :       str	   =     num_choices
      _lowerCAmelCase :       Optional[Any]	   =     scope
      _lowerCAmelCase :       Union[str, Any]	   =     projection_dim

 def  __magic_name__							(		self			:						Tuple							):






      '''simple docstring'''




      _lowerCAmelCase :       Union[str, Any]	   =     ids_tensor([self.batch_size, self.seq_length]  ,    self.vocab_size							)

      _lowerCAmelCase :       str	   =     None
      if self.use_input_mask:
           _lowerCAmelCase :       Optional[Any]	   =     random_attention_mask([self.batch_size, self.seq_length]							)

      _lowerCAmelCase :       str	   =     None
      if self.use_token_type_ids:
           _lowerCAmelCase :       Optional[Any]	   =     ids_tensor([self.batch_size, self.seq_length]  ,    self.type_vocab_size							)

      _lowerCAmelCase :       Dict	   =     None
      _lowerCAmelCase :       Dict	   =     None
      _lowerCAmelCase :       Optional[int]	   =     None
      if self.use_labels:
           _lowerCAmelCase :       str	   =     ids_tensor([self.batch_size]  ,    self.type_sequence_label_size							)
           _lowerCAmelCase :       List[str]	   =     ids_tensor([self.batch_size, self.seq_length]  ,    self.num_labels							)
           _lowerCAmelCase :       int	   =     ids_tensor([self.batch_size]  ,    self.num_choices							)

      _lowerCAmelCase :       List[Any]	   =     self.get_config()

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

 def  __magic_name__							(		self			:						Optional[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  __magic_name__							(		self			:						Optional[Any]  ,    A_			:						Any  ,    A_			:						Union[str, Any]  ,    A_			:						Tuple  ,    A_			:						Optional[int]  ,    A_			:						Dict  ,    A_			:						Tuple  ,    A_			:						Any							):






      '''simple docstring'''




      _lowerCAmelCase :       int	   =     DPRContextEncoder(config=A_							)
      model.to(A_							)
      model.eval()
      _lowerCAmelCase :       int	   =     model(A_  ,    attention_mask=A_  ,    token_type_ids=A_							)
      _lowerCAmelCase :       str	   =     model(A_  ,    token_type_ids=A_							)
      _lowerCAmelCase :       Union[str, Any]	   =     model(A_							)
      self.parent.assertEqual(result.pooler_output.shape  ,    (self.batch_size, self.projection_dim or self.hidden_size)							)

 def  __magic_name__							(		self			:						Optional[int]  ,    A_			:						List[str]  ,    A_			:						Optional[int]  ,    A_			:						List[str]  ,    A_			:						int  ,    A_			:						Dict  ,    A_			:						List[Any]  ,    A_			:						Dict							):






      '''simple docstring'''




      _lowerCAmelCase :       List[Any]	   =     DPRQuestionEncoder(config=A_							)
      model.to(A_							)
      model.eval()
      _lowerCAmelCase :       str	   =     model(A_  ,    attention_mask=A_  ,    token_type_ids=A_							)
      _lowerCAmelCase :       Any	   =     model(A_  ,    token_type_ids=A_							)
      _lowerCAmelCase :       Union[str, Any]	   =     model(A_							)
      self.parent.assertEqual(result.pooler_output.shape  ,    (self.batch_size, self.projection_dim or self.hidden_size)							)

 def  __magic_name__							(		self			:						List[Any]  ,    A_			:						Optional[int]  ,    A_			:						List[str]  ,    A_			:						Any  ,    A_			:						Union[str, Any]  ,    A_			:						str  ,    A_			:						str  ,    A_			:						Dict							):






      '''simple docstring'''




      _lowerCAmelCase :       List[Any]	   =     DPRReader(config=A_							)
      model.to(A_							)
      model.eval()
      _lowerCAmelCase :       Tuple	   =     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  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      _lowerCAmelCase :       Any	   =     self.prepare_config_and_inputs()
      (
          (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      
      ) :       str	   =     config_and_inputs
      _lowerCAmelCase :       int	   =     {"input_ids": input_ids}
      return config, inputs_dict





@require_torch
class A__     (			A	,		A	,		unittest.TestCase			):



 """simple docstring"""







 _lowercase  :       Optional[int]  			=    (
     (
         DPRContextEncoder,
         DPRQuestionEncoder,
         DPRReader,
     )
     if is_torch_available()
     else ()
 )
 _lowercase  :       Tuple  			=    {'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

 _lowercase  :       Optional[int]  			=    False
 _lowercase  :       List[str]  			=    False  # why?
 _lowercase  :       Dict  			=    False
 _lowercase  :       Union[str, Any]  			=    False

 def  __magic_name__							(		self			:						Tuple							):






      '''simple docstring'''




      _lowerCAmelCase :       Tuple	   =     DPRModelTester(self							)
      _lowerCAmelCase :       Dict	   =     ConfigTester(self  ,    config_class=A_  ,    hidden_size=3_7							)

 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      self.config_tester.run_common_tests()

 def  __magic_name__							(		self			:						Union[str, Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       int	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_context_encoder(*A_							)

 def  __magic_name__							(		self			:						Tuple							):






      '''simple docstring'''




      _lowerCAmelCase :       List[str]	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_question_encoder(*A_							)

 def  __magic_name__							(		self			:						List[str]							):






      '''simple docstring'''




      _lowerCAmelCase :       Tuple	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_reader(*A_							)

 def  __magic_name__							(		self			:						List[str]							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[Any]	   =     self.model_tester.prepare_config_and_inputs()[0]

      _lowerCAmelCase :       Optional[int]	   =     DPRQuestionEncoder(config=A_							)
      model.to(A_							)
      model.eval()

      with tempfile.TemporaryDirectory() as tmp_dirname:
           model.save_pretrained(A_							)
           _lowerCAmelCase :       List[Any]	   =     DPRQuestionEncoder.from_pretrained(A_  ,    projection_dim=5_1_2							)

      self.assertIsNotNone(A_							)


 @slow
 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowerCAmelCase :       Tuple	   =     DPRContextEncoder.from_pretrained(A_							)
           self.assertIsNotNone(A_							)

      for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowerCAmelCase :       Dict	   =     DPRContextEncoder.from_pretrained(A_							)
           self.assertIsNotNone(A_							)

      for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowerCAmelCase :       Dict	   =     DPRQuestionEncoder.from_pretrained(A_							)
           self.assertIsNotNone(A_							)

      for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowerCAmelCase :       Optional[Any]	   =     DPRReader.from_pretrained(A_							)
           self.assertIsNotNone(A_							)





@require_torch
class A__     (			unittest.TestCase			):



 """simple docstring"""







 @slow
 def  __magic_name__							(		self			:						int							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[Any]	   =     DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"  ,    return_dict=A_							)
      model.to(A_							)

      _lowerCAmelCase :       str	   =     torch.tensor(
          [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]]  ,    dtype=torch.long  ,    device=A_							)  # [CLS] hello, is my dog cute? [SEP]
      _lowerCAmelCase :       Optional[int]	   =     model(A_							)[0]  # embedding shape = (1, 768)
      # compare the actual values for a slice.
      _lowerCAmelCase :       List[str]	   =     torch.tensor(
          [
              [
                  0.03236253,
                  0.12753335,
                  0.16818509,
                  0.00279786,
                  0.3896933,
                  0.24264945,
                  0.2178971,
                  -0.02335227,
                  -0.08481959,
                  -0.14324117,
              ]
          ]  ,    dtype=torch.float  ,    device=A_  ,    )
      self.assertTrue(torch.allclose(output[:, :1_0]  ,    A_  ,    atol=1E-4							)							)


 @slow
 def  __magic_name__							(		self			:						Union[str, Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[Any]	   =     DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base"							)
      _lowerCAmelCase :       Union[str, Any]	   =     DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base"							)
      model.to(A_							)

      _lowerCAmelCase :       Dict	   =     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_							)

      _lowerCAmelCase :       Optional[int]	   =     model(**A_							)

      # compare the actual values for a slice.
      _lowerCAmelCase :       Any	   =     torch.tensor(
          [[-10.3005, -10.7765, -11.4872, -11.6841, -11.9312, -10.3002, -9.8544, -11.7378, -12.0821, -10.2975]]  ,    dtype=torch.float  ,    device=A_  ,    )

      _lowerCAmelCase :       Optional[Any]	   =     torch.tensor(
          [[-11.0684, -11.7041, -11.5397, -10.3465, -10.8791, -6.8443, -11.9959, -11.0364, -10.0096, -6.8405]]  ,    dtype=torch.float  ,    device=A_  ,    )
      self.assertTrue(torch.allclose(outputs.start_logits[:, :1_0]  ,    A_  ,    atol=1E-4							)							)
      self.assertTrue(torch.allclose(outputs.end_logits[:, :1_0]  ,    A_  ,    atol=1E-4							)							)









import unittest
from timeit import timeit







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   int:

     """simple docstring"""







     _lowerCAmelCase :       List[str]	   =     first_num if first_num >= second_num else second_num
     _lowerCAmelCase :       List[str]	   =     max_num
     while (common_mult % first_num > 0) or (common_mult % second_num > 0):
          common_mult += max_num
     return common_mult







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   int:

     """simple docstring"""







     return b if a == 0 else greatest_common_divisor(b % a		,				SCREAMING_SNAKE_CASE					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   int:

     """simple docstring"""







     return first_num // greatest_common_divisor(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					) * second_num







def 						_snake_case		(     )							->   str:

     """simple docstring"""







     _lowerCAmelCase :       List[Any]	   =     (
         "from __main__ import least_common_multiple_slow, least_common_multiple_fast"
     )
     print(
         "least_common_multiple_slow():"		,				timeit("least_common_multiple_slow(1000, 999)"		,				setup=SCREAMING_SNAKE_CASE					)		,				)
     print(
         "least_common_multiple_fast():"		,				timeit("least_common_multiple_fast(1000, 999)"		,				setup=SCREAMING_SNAKE_CASE					)		,				)





class A__     (			unittest.TestCase			):



 """simple docstring"""







 _lowercase  :       Union[str, Any]  			=    (
     (10, 20),
     (13, 15),
     (4, 31),
     (10, 42),
     (43, 34),
     (5, 12),
     (12, 25),
     (10, 25),
     (6, 9),
 )
 _lowercase  :       Dict  			=    (20, 195, 124, 210, 1_462, 60, 300, 50, 18)

 def  __magic_name__							(		self			:						int							):






      '''simple docstring'''




      for i, (first_num, second_num) in enumerate(self.test_inputs							):
           _lowerCAmelCase :       Optional[int]	   =     least_common_multiple_slow(A_  ,    A_							)
           _lowerCAmelCase :       Optional[int]	   =     least_common_multiple_fast(A_  ,    A_							)
           with self.subTest(i=A_							):
                self.assertEqual(A_  ,    self.expected_results[i]							)
                self.assertEqual(A_  ,    self.expected_results[i]							)


if __name__ == "__main__":
  benchmark()
  unittest.main()






