






'''simple docstring'''





from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import numpy
   import tensorflow as tf

   from transformers import (
       TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
       TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
       TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
       BertConfig,
       DPRConfig,
       TFDPRContextEncoder,
       TFDPRQuestionEncoder,
       TFDPRReader,
   )


class  SCREAMING_SNAKE_CASE			:
     '''simple docstring'''


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


     def 				_A       (				self       :	int      ):
       SCREAMING_SNAKE_CASE       :      str							=    ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size      )

       SCREAMING_SNAKE_CASE       :      Optional[int]							=    None
       if self.use_input_mask:
         # follow test_modeling_tf_ctrl.py
         SCREAMING_SNAKE_CASE       :      List[str]							=    random_attention_mask([self.batch_size, self.seq_length]      )

       SCREAMING_SNAKE_CASE       :      List[Any]							=    None
       if self.use_token_type_ids:
         SCREAMING_SNAKE_CASE       :      Tuple							=    ids_tensor([self.batch_size, self.seq_length]					, self.type_vocab_size      )

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

       SCREAMING_SNAKE_CASE       :      Dict							=    BertConfig(
           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					, is_decoder=UpperCAmelCase_					, initializer_range=self.initializer_range					, )
       SCREAMING_SNAKE_CASE       :      Tuple							=    DPRConfig(projection_dim=self.projection_dim					, **config.to_dict()      )

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


     def 				_A       (				self       :	Optional[int]					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Optional[Any]      ):
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    TFDPRContextEncoder(config=UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      Tuple							=    model(UpperCAmelCase_					, attention_mask=UpperCAmelCase_					, token_type_ids=UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      Any							=    model(UpperCAmelCase_					, token_type_ids=UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    model(UpperCAmelCase_      )
       self.parent.assertEqual(result.pooler_output.shape					, (self.batch_size, self.projection_dim or self.hidden_size)      )


     def 				_A       (				self       :	Optional[Any]					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Union[str, Any]					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Dict      ):
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    TFDPRQuestionEncoder(config=UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    model(UpperCAmelCase_					, attention_mask=UpperCAmelCase_					, token_type_ids=UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    model(UpperCAmelCase_					, token_type_ids=UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      str							=    model(UpperCAmelCase_      )
       self.parent.assertEqual(result.pooler_output.shape					, (self.batch_size, self.projection_dim or self.hidden_size)      )


     def 				_A       (				self       :	Any					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	List[str]					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	List[str]      ):
       SCREAMING_SNAKE_CASE       :      Dict							=    TFDPRReader(config=UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      Tuple							=    model(UpperCAmelCase_					, attention_mask=UpperCAmelCase_      )

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





@require_tf
class  SCREAMING_SNAKE_CASE			(			A_      ,		A_      ,		unittest.TestCase					):
     '''simple docstring'''


     UpperCamelCase_							:		Union[str, Any]			=	(
         (
             TFDPRContextEncoder,
             TFDPRQuestionEncoder,
             TFDPRReader,
         )
         if is_tf_available()
         else ()
     )
     UpperCamelCase_							:		Optional[int]			=	{'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

     UpperCamelCase_							:		int			=	False
     UpperCamelCase_							:		Dict			=	False
     UpperCamelCase_							:		Tuple			=	False
     UpperCamelCase_							:		Optional[int]			=	False
     UpperCamelCase_							:		str			=	False


     def 				_A       (				self       :	int      ):
       SCREAMING_SNAKE_CASE       :      List[Any]							=    TFDPRModelTester(self      )
       SCREAMING_SNAKE_CASE       :      List[Any]							=    ConfigTester(self					, config_class=UpperCAmelCase_					, hidden_size=37      )


     def 				_A       (				self       :	Any      ):
       self.config_tester.run_common_tests()


     def 				_A       (				self       :	List[str]      ):
       SCREAMING_SNAKE_CASE       :      List[str]							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_context_encoder(*UpperCAmelCase_      )


     def 				_A       (				self       :	str      ):
       SCREAMING_SNAKE_CASE       :      List[str]							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_question_encoder(*UpperCAmelCase_      )


     def 				_A       (				self       :	Optional[Any]      ):
       SCREAMING_SNAKE_CASE       :      Tuple							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_reader(*UpperCAmelCase_      )






     @slow
     def 				_A       (				self       :	Tuple      ):
       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         SCREAMING_SNAKE_CASE       :      List[str]							=    TFDPRContextEncoder.from_pretrained(UpperCAmelCase_      )
         self.assertIsNotNone(UpperCAmelCase_      )

       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         SCREAMING_SNAKE_CASE       :      Dict							=    TFDPRContextEncoder.from_pretrained(UpperCAmelCase_      )
         self.assertIsNotNone(UpperCAmelCase_      )

       for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         SCREAMING_SNAKE_CASE       :      int							=    TFDPRQuestionEncoder.from_pretrained(UpperCAmelCase_      )
         self.assertIsNotNone(UpperCAmelCase_      )

       for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         SCREAMING_SNAKE_CASE       :      List[Any]							=    TFDPRReader.from_pretrained(UpperCAmelCase_      )
         self.assertIsNotNone(UpperCAmelCase_      )





@require_tf
class  SCREAMING_SNAKE_CASE			(			unittest.TestCase					):
     '''simple docstring'''


     @slow
     def 				_A       (				self       :	Union[str, Any]      ):
       SCREAMING_SNAKE_CASE       :      Any							=    TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"      )

       SCREAMING_SNAKE_CASE       :      List[Any]							=    tf.constant(
           [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]      )  # [CLS] hello, is my dog cute? [SEP]
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    model(UpperCAmelCase_      )[0]  # embedding shape = (1, 768)
       # compare the actual values for a slice.
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    tf.constant(
           [
               [
                   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,
               ]
           ]      )
       self.assertTrue(numpy.allclose(output[:, :10].numpy()					, expected_slice.numpy()					, atol=1E-4      )      )














from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case    				=		logging.get_logger(__name__)

snake_case    				=		{
    """microsoft/markuplm-base""": """https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json""",
    """microsoft/markuplm-large""": """https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json""",
}


class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     UpperCamelCase_							:		Optional[int]			=	'''markuplm'''


     def __init__(				self       :	int					, UpperCAmelCase_       :	List[str]=3_0522					, UpperCAmelCase_       :	Optional[Any]=768					, UpperCAmelCase_       :	Any=12					, UpperCAmelCase_       :	Tuple=12					, UpperCAmelCase_       :	Union[str, Any]=3072					, UpperCAmelCase_       :	Dict="gelu"					, UpperCAmelCase_       :	Optional[int]=0.1					, UpperCAmelCase_       :	Tuple=0.1					, UpperCAmelCase_       :	Dict=512					, UpperCAmelCase_       :	Optional[int]=2					, UpperCAmelCase_       :	Any=0.02					, UpperCAmelCase_       :	Dict=1E-12					, UpperCAmelCase_       :	int=0					, UpperCAmelCase_       :	Optional[Any]=0					, UpperCAmelCase_       :	Optional[int]=2					, UpperCAmelCase_       :	str=256					, UpperCAmelCase_       :	str=1024					, UpperCAmelCase_       :	List[str]=216					, UpperCAmelCase_       :	List[Any]=1001					, UpperCAmelCase_       :	Union[str, Any]=32					, UpperCAmelCase_       :	Dict=50					, UpperCAmelCase_       :	Optional[int]="absolute"					, UpperCAmelCase_       :	Optional[int]=True					, UpperCAmelCase_       :	Any=None					, **UpperCAmelCase_       :	Any					, ):
       super().__init__(
           pad_token_id=UpperCAmelCase_					, bos_token_id=UpperCAmelCase_					, eos_token_id=UpperCAmelCase_					, **UpperCAmelCase_					, )
       SCREAMING_SNAKE_CASE       :      Any							=    vocab_size
       SCREAMING_SNAKE_CASE       :      List[Any]							=    hidden_size
       SCREAMING_SNAKE_CASE       :      List[Any]							=    num_hidden_layers
       SCREAMING_SNAKE_CASE       :      Tuple							=    num_attention_heads
       SCREAMING_SNAKE_CASE       :      List[str]							=    hidden_act
       SCREAMING_SNAKE_CASE       :      Tuple							=    intermediate_size
       SCREAMING_SNAKE_CASE       :      Any							=    hidden_dropout_prob
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE       :      int							=    max_position_embeddings
       SCREAMING_SNAKE_CASE       :      Tuple							=    type_vocab_size
       SCREAMING_SNAKE_CASE       :      Any							=    initializer_range
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    layer_norm_eps
       SCREAMING_SNAKE_CASE       :      int							=    position_embedding_type
       SCREAMING_SNAKE_CASE       :      List[str]							=    use_cache
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    classifier_dropout
       # additional properties
       SCREAMING_SNAKE_CASE       :      Any							=    max_depth
       SCREAMING_SNAKE_CASE       :      int							=    max_xpath_tag_unit_embeddings
       SCREAMING_SNAKE_CASE       :      List[str]							=    max_xpath_subs_unit_embeddings
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    tag_pad_id
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    subs_pad_id
       SCREAMING_SNAKE_CASE       :      List[Any]							=    xpath_unit_hidden_size






