
'''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  UpperCAmelCase_					:





 def __init__(     self			,							lowercase_			,							lowercase_=13			,							lowercase_=7			,							lowercase_=False			,							lowercase_=True			,							lowercase_=True			,							lowercase_=True			,							lowercase_=99			,							lowercase_=32			,							lowercase_=5			,							lowercase_=4			,							lowercase_=37			,							lowercase_="gelu"			,							lowercase_=0.1			,							lowercase_=0.1			,							lowercase_=5_12			,							lowercase_=16			,							lowercase_=2			,							lowercase_=0.02			,							lowercase_=3			,							lowercase_=4			,							lowercase_=None			,							lowercase_=0			,							):
     snake_case_			:     Dict        =    parent
     snake_case_			:     Tuple        =    batch_size
     snake_case_			:     Optional[int]        =    seq_length
     snake_case_			:     List[Any]        =    is_training
     snake_case_			:     str        =    use_input_mask
     snake_case_			:     Optional[int]        =    use_token_type_ids
     snake_case_			:     Optional[int]        =    use_labels
     snake_case_			:     Dict        =    vocab_size
     snake_case_			:     Optional[Any]        =    hidden_size
     snake_case_			:     str        =    num_hidden_layers
     snake_case_			:     Any        =    num_attention_heads
     snake_case_			:     List[str]        =    intermediate_size
     snake_case_			:     Union[str, Any]        =    hidden_act
     snake_case_			:     str        =    hidden_dropout_prob
     snake_case_			:     Union[str, Any]        =    attention_probs_dropout_prob
     snake_case_			:     Tuple        =    max_position_embeddings
     snake_case_			:     Union[str, Any]        =    type_vocab_size
     snake_case_			:     Tuple        =    type_sequence_label_size
     snake_case_			:     Optional[int]        =    initializer_range
     snake_case_			:     Dict        =    num_labels
     snake_case_			:     List[Any]        =    num_choices
     snake_case_			:     Dict        =    scope
     snake_case_			:     Dict        =    projection_dim





 def 							snake_case__		(     self):
     snake_case_			:     Optional[int]        =    ids_tensor([self.batch_size, self.seq_length]			,							self.vocab_size)

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

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

     snake_case_			:     str        =    None
     snake_case_			:     List[Any]        =    None
     snake_case_			:     Union[str, Any]        =    None
     if self.use_labels:
         snake_case_			:     Optional[Any]        =    ids_tensor([self.batch_size]			,							self.type_sequence_label_size)
         snake_case_			:     str        =    ids_tensor([self.batch_size, self.seq_length]			,							self.num_labels)
         snake_case_			:     List[Any]        =    ids_tensor([self.batch_size]			,							self.num_choices)

     snake_case_			:     int        =    self.get_config()

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





 def 							snake_case__		(     self):
     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 							snake_case__		(     self			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_):
     snake_case_			:     List[str]        =    DPRContextEncoder(config=lowercase_)
     model.to(lowercase_)
     model.eval()
     snake_case_			:     Dict        =    model(lowercase_			,							attention_mask=lowercase_			,							token_type_ids=lowercase_)
     snake_case_			:     Union[str, Any]        =    model(lowercase_			,							token_type_ids=lowercase_)
     snake_case_			:     Dict        =    model(lowercase_)
     self.parent.assertEqual(result.pooler_output.shape			,							(self.batch_size, self.projection_dim or self.hidden_size))





 def 							snake_case__		(     self			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_):
     snake_case_			:     Union[str, Any]        =    DPRQuestionEncoder(config=lowercase_)
     model.to(lowercase_)
     model.eval()
     snake_case_			:     Optional[int]        =    model(lowercase_			,							attention_mask=lowercase_			,							token_type_ids=lowercase_)
     snake_case_			:     Dict        =    model(lowercase_			,							token_type_ids=lowercase_)
     snake_case_			:     Optional[int]        =    model(lowercase_)
     self.parent.assertEqual(result.pooler_output.shape			,							(self.batch_size, self.projection_dim or self.hidden_size))





 def 							snake_case__		(     self			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_):
     snake_case_			:     Optional[int]        =    DPRReader(config=lowercase_)
     model.to(lowercase_)
     model.eval()
     snake_case_			:     List[Any]        =    model(
         lowercase_			,							attention_mask=lowercase_			,							)

     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 							snake_case__		(     self):
     snake_case_			:     List[str]        =    self.prepare_config_and_inputs()
     (
         (
         snake_case_
     )      ,			(
         snake_case_
     )      ,			(
         snake_case_
     )      ,			(
         snake_case_
     )      ,			(
         snake_case_
     )      ,			(
         snake_case_
     )      ,			(
         snake_case_
     )      ,			
     )			:     Optional[int]        =    config_and_inputs
     snake_case_			:     Optional[int]        =    {"input_ids": input_ids}
     return config, inputs_dict






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

 UpperCAmelCase_		     =    False
 UpperCAmelCase_		     =    False  # why?
 UpperCAmelCase_		     =    False
 UpperCAmelCase_		     =    False





 def 							snake_case__		(     self):
     snake_case_			:     int        =    DPRModelTester(self)
     snake_case_			:     str        =    ConfigTester(self			,							config_class=lowercase_			,							hidden_size=37)





 def 							snake_case__		(     self):
     self.config_tester.run_common_tests()





 def 							snake_case__		(     self):
     snake_case_			:     Optional[Any]        =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_context_encoder(*lowercase_)





 def 							snake_case__		(     self):
     snake_case_			:     Any        =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_question_encoder(*lowercase_)





 def 							snake_case__		(     self):
     snake_case_			:     List[str]        =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_reader(*lowercase_)





 def 							snake_case__		(     self):
     snake_case_			:     Optional[Any]        =    self.model_tester.prepare_config_and_inputs()[0]

     snake_case_			:     Dict        =    DPRQuestionEncoder(config=lowercase_)
     model.to(lowercase_)
     model.eval()

     with tempfile.TemporaryDirectory() as tmp_dirname:
         model.save_pretrained(lowercase_)
         snake_case_			:     Optional[int]        =    DPRQuestionEncoder.from_pretrained(lowercase_			,							projection_dim=5_12)

     self.assertIsNotNone(lowercase_)





 @slow
 def 							snake_case__		(     self):
     for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case_			:     List[Any]        =    DPRContextEncoder.from_pretrained(lowercase_)
         self.assertIsNotNone(lowercase_)

     for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case_			:     int        =    DPRContextEncoder.from_pretrained(lowercase_)
         self.assertIsNotNone(lowercase_)

     for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case_			:     Optional[int]        =    DPRQuestionEncoder.from_pretrained(lowercase_)
         self.assertIsNotNone(lowercase_)

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






@require_torch
class  UpperCAmelCase_					(						unittest.TestCase       ):





 @slow
 def 							snake_case__		(     self):
     snake_case_			:     int        =    DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"			,							return_dict=lowercase_)
     model.to(lowercase_)

     snake_case_			:     Tuple        =    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=lowercase_)  # [CLS] hello, is my dog cute? [SEP]
     snake_case_			:     Dict        =    model(lowercase_)[0]  # embedding shape = (1, 768)
     # compare the actual values for a slice.
     snake_case_			:     List[str]        =    torch.tensor(
         [
             [
                 0.03_236_253,
                 0.12_753_335,
                 0.16_818_509,
                 0.00_279_786,
                 0.3_896_933,
                 0.24_264_945,
                 0.2_178_971,
                 -0.02_335_227,
                 -0.08_481_959,
                 -0.14_324_117,
             ]
         ]			,							dtype=torch.float			,							device=lowercase_			,							)
     self.assertTrue(torch.allclose(output[:, :10]			,							lowercase_			,							atol=1E-4))





 @slow
 def 							snake_case__		(     self):
     snake_case_			:     str        =    DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base")
     snake_case_			:     int        =    DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base")
     model.to(lowercase_)

     snake_case_			:     List[str]        =    tokenizer(
         questions="What is love ?"			,							titles="Haddaway"			,							texts="What Is Love is a song recorded by the artist Haddaway"			,							padding=lowercase_			,							return_tensors="pt"			,							)
     encoded_inputs.to(lowercase_)

     snake_case_			:     Optional[int]        =    model(**lowercase_)

     # compare the actual values for a slice.
     snake_case_			:     Tuple        =    torch.tensor(
         [[-10.3_005, -10.7_765, -11.4_872, -11.6_841, -11.9_312, -10.3_002, -9.8_544, -11.7_378, -12.0_821, -10.2_975]]			,							dtype=torch.float			,							device=lowercase_			,							)

     snake_case_			:     Any        =    torch.tensor(
         [[-11.0_684, -11.7_041, -11.5_397, -10.3_465, -10.8_791, -6.8_443, -11.9_959, -11.0_364, -10.0_096, -6.8_405]]			,							dtype=torch.float			,							device=lowercase_			,							)
     self.assertTrue(torch.allclose(outputs.start_logits[:, :10]			,							lowercase_			,							atol=1E-4))
     self.assertTrue(torch.allclose(outputs.end_logits[:, :10]			,							lowercase_			,							atol=1E-4))






'''simple docstring'''
def     UpperCamelCase_  (						__SCREAMING_SNAKE_CASE							):
    """simple docstring"""






    if number != int(__SCREAMING_SNAKE_CASE							):
        raise ValueError("factorial() only accepts integral values"							)
    if number < 0:
        raise ValueError("factorial() not defined for negative values"							)
    snake_case_			:     int        =    1
    for i in range(1,		number + 1							):
        value *= i
    return value

def     UpperCamelCase_  (						__SCREAMING_SNAKE_CASE							):
    """simple docstring"""






    if not isinstance(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							):
        raise ValueError("factorial() only accepts integral values"							)
    if n < 0:
        raise ValueError("factorial() not defined for negative values"							)
    return 1 if n == 0 or n == 1 else n * factorial(n - 1							)


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  a_           =    int(input("Enter a positive integer: ").strip() or 0)
  print(f"""factorial{n} is {factorial(n)}""")




