


'''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   ,					__magic_name__   ,					__magic_name__=13   ,					__magic_name__=7   ,					__magic_name__=False   ,					__magic_name__=True   ,					__magic_name__=True   ,					__magic_name__=True   ,					__magic_name__=99   ,					__magic_name__=32   ,					__magic_name__=5   ,					__magic_name__=4   ,					__magic_name__=37   ,					__magic_name__="gelu"   ,					__magic_name__=0.1   ,					__magic_name__=0.1   ,					__magic_name__=5_12   ,					__magic_name__=16   ,					__magic_name__=2   ,					__magic_name__=0.0_2   ,					__magic_name__=3   ,					__magic_name__=4   ,					__magic_name__=None   ,					__magic_name__=0   ,					)       ->				Tuple:
      _a         =				parent
      _a         =				batch_size
      _a         =				seq_length
      _a         =				is_training
      _a         =				use_input_mask
      _a         =				use_token_type_ids
      _a         =				use_labels
      _a         =				vocab_size
      _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         =				type_sequence_label_size
      _a         =				initializer_range
      _a         =				num_labels
      _a         =				num_choices
      _a         =				scope
      _a         =				projection_dim
 def 				__UpperCAmelCase		(			self    )       ->				List[str]:
      _a         =				ids_tensor([self.batch_size, self.seq_length]   ,					self.vocab_size    )

      _a         =				None
      if self.use_input_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         =				None
      _a         =				None
      _a         =				None
      if self.use_labels:
           _a         =				ids_tensor([self.batch_size]   ,					self.type_sequence_label_size    )
           _a         =				ids_tensor([self.batch_size, self.seq_length]   ,					self.num_labels    )
           _a         =				ids_tensor([self.batch_size]   ,					self.num_choices    )

      _a         =				self.get_config()

      return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 				__UpperCAmelCase		(			self    )       ->				int:
      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 				__UpperCAmelCase		(			self   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__    )       ->				Any:
      _a         =				DPRContextEncoder(config=__magic_name__    )
      model.to(__magic_name__    )
      model.eval()
      _a         =				model(__magic_name__   ,					attention_mask=__magic_name__   ,					token_type_ids=__magic_name__    )
      _a         =				model(__magic_name__   ,					token_type_ids=__magic_name__    )
      _a         =				model(__magic_name__    )
      self.parent.assertEqual(result.pooler_output.shape   ,					(self.batch_size, self.projection_dim or self.hidden_size)    )
 def 				__UpperCAmelCase		(			self   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__    )       ->				int:
      _a         =				DPRQuestionEncoder(config=__magic_name__    )
      model.to(__magic_name__    )
      model.eval()
      _a         =				model(__magic_name__   ,					attention_mask=__magic_name__   ,					token_type_ids=__magic_name__    )
      _a         =				model(__magic_name__   ,					token_type_ids=__magic_name__    )
      _a         =				model(__magic_name__    )
      self.parent.assertEqual(result.pooler_output.shape   ,					(self.batch_size, self.projection_dim or self.hidden_size)    )
 def 				__UpperCAmelCase		(			self   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__    )       ->				Optional[int]:
      _a         =				DPRReader(config=__magic_name__    )
      model.to(__magic_name__    )
      model.eval()
      _a         =				model(
          __magic_name__   ,					attention_mask=__magic_name__   ,					)

      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 				__UpperCAmelCase		(			self    )       ->				List[Any]:
      _a         =				self.prepare_config_and_inputs()
      (
          _a	
      )         =				config_and_inputs
      _a         =				{'input_ids': input_ids}
      return config, inputs_dict





@require_torch
class 			a  (  _SCREAMING_SNAKE_CASE	,					_SCREAMING_SNAKE_CASE	,					unittest.TestCase							):
 _lowerCAmelCase			       =						(
     (
         DPRContextEncoder,
         DPRQuestionEncoder,
         DPRReader,
     )
     if is_torch_available()
     else ()
 )
 _lowerCAmelCase			       =						{"""feature-extraction""": DPRQuestionEncoder} if is_torch_available() else {}

 _lowerCAmelCase			       =						False
 _lowerCAmelCase			       =						False  # why?
 _lowerCAmelCase			       =						False
 _lowerCAmelCase			       =						False
 def 				__UpperCAmelCase		(			self    )       ->				Union[str, Any]:
      _a         =				DPRModelTester(self    )
      _a         =				ConfigTester(self   ,					config_class=__magic_name__   ,					hidden_size=37    )
 def 				__UpperCAmelCase		(			self    )       ->				Optional[int]:
      self.config_tester.run_common_tests()
 def 				__UpperCAmelCase		(			self    )       ->				str:
      _a         =				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_context_encoder(*__magic_name__    )
 def 				__UpperCAmelCase		(			self    )       ->				Optional[Any]:
      _a         =				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_question_encoder(*__magic_name__    )
 def 				__UpperCAmelCase		(			self    )       ->				str:
      _a         =				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_reader(*__magic_name__    )
 def 				__UpperCAmelCase		(			self    )       ->				str:
      _a         =				self.model_tester.prepare_config_and_inputs()[0]

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

      with tempfile.TemporaryDirectory() as tmp_dirname:
           model.save_pretrained(__magic_name__    )
           _a         =				DPRQuestionEncoder.from_pretrained(__magic_name__   ,					projection_dim=5_12    )

      self.assertIsNotNone(__magic_name__    )




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




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

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

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

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





@require_torch
class 			a  (  unittest.TestCase							):
 @slow
 def 				__UpperCAmelCase		(			self    )       ->				Any:
      _a         =				DPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'   ,					return_dict=__magic_name__    )
      model.to(__magic_name__    )

      _a         =				torch.tensor(
          [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]]   ,					dtype=torch.long   ,					device=__magic_name__    )  # [CLS] hello, is my dog cute? [SEP]
      _a         =				model(__magic_name__    )[0]  # embedding shape = (1, 768)
      # compare the actual values for a slice.
      _a         =				torch.tensor(
          [
              [
                  0.0_3_2_3_6_2_5_3,
                  0.1_2_7_5_3_3_3_5,
                  0.1_6_8_1_8_5_0_9,
                  0.0_0_2_7_9_7_8_6,
                  0.3_8_9_6_9_3_3,
                  0.2_4_2_6_4_9_4_5,
                  0.2_1_7_8_9_7_1,
                  -0.0_2_3_3_5_2_2_7,
                  -0.0_8_4_8_1_9_5_9,
                  -0.1_4_3_2_4_1_1_7,
              ]
          ]   ,					dtype=torch.float   ,					device=__magic_name__   ,					)
      self.assertTrue(torch.allclose(output[:, :10]   ,					__magic_name__   ,					atol=1e-4    )    )




 @slow
 def 				__UpperCAmelCase		(			self    )       ->				List[str]:
      _a         =				DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base'    )
      _a         =				DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base'    )
      model.to(__magic_name__    )

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

      _a         =				model(**__magic_name__    )

      # compare the actual values for a slice.
      _a         =				torch.tensor(
          [[-1_0.3_0_0_5, -1_0.7_7_6_5, -1_1.4_8_7_2, -1_1.6_8_4_1, -1_1.9_3_1_2, -1_0.3_0_0_2, -9.8_5_4_4, -1_1.7_3_7_8, -1_2.0_8_2_1, -1_0.2_9_7_5]]   ,					dtype=torch.float   ,					device=__magic_name__   ,					)

      _a         =				torch.tensor(
          [[-1_1.0_6_8_4, -1_1.7_0_4_1, -1_1.5_3_9_7, -1_0.3_4_6_5, -1_0.8_7_9_1, -6.8_4_4_3, -1_1.9_9_5_9, -1_1.0_3_6_4, -1_0.0_0_9_6, -6.8_4_0_5]]   ,					dtype=torch.float   ,					device=__magic_name__   ,					)
      self.assertTrue(torch.allclose(outputs.start_logits[:, :10]   ,					__magic_name__   ,					atol=1e-4    )    )
      self.assertTrue(torch.allclose(outputs.end_logits[:, :10]   ,					__magic_name__   ,					atol=1e-4    )    )









"""simple docstring"""


from __future__ import annotations




def      snake_case    (A_ :int ):







  '''simple docstring'''

  assert (
      isinstance(A_      ,     A_ ) and mask > 0
  ), f'''mask needs to be positive integer, your input {mask}'''
  a	:     Any							=  []
  a	:     Union[str, Any]							=  mask

  while submask:
    all_submasks.append(A_ )
    a	:     Any							=  (submask - 1) & mask

  return all_submasks


if __name__ == "__main__":
   import doctest

   doctest.testmod()




