
"""simple docstring"""





from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, 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 tensorflow as tf

		from transformers import (
		    TFConvBertForMaskedLM,
		    TFConvBertForMultipleChoice,
		    TFConvBertForQuestionAnswering,
		    TFConvBertForSequenceClassification,
		    TFConvBertForTokenClassification,
		    TFConvBertModel,
		)



class __lowerCAmelCase				:




			"""simple docstring"""







			def __init__(				self						:					List[str]       ,   _snake_case						:					List[str]       ,   _snake_case						:					Tuple=13       ,   _snake_case						:					List[Any]=7       ,   _snake_case						:					str=True       ,   _snake_case						:					List[str]=True       ,   _snake_case						:					str=True       ,   _snake_case						:					Any=True       ,   _snake_case						:					List[str]=99       ,   _snake_case						:					Tuple=32       ,   _snake_case						:					Optional[int]=2       ,   _snake_case						:					Union[str, Any]=4       ,   _snake_case						:					Any=37       ,   _snake_case						:					Any="gelu"       ,   _snake_case						:					List[Any]=0.1       ,   _snake_case						:					str=0.1       ,   _snake_case						:					List[Any]=512       ,   _snake_case						:					Optional[Any]=16       ,   _snake_case						:					List[str]=2       ,   _snake_case						:					Optional[Any]=0.0_2       ,   _snake_case						:					Optional[Any]=3       ,   _snake_case						:					str=4       ,   _snake_case						:					Dict=None       ,   )      -> int:







								"""simple docstring"""
								A_				 =						parent
								A_				 =						13
								A_				 =						7
								A_				 =						True
								A_				 =						True
								A_				 =						True
								A_				 =						True
								A_				 =						99
								A_				 =						384
								A_				 =						2
								A_				 =						4
								A_				 =						37
								A_				 =						"gelu"
								A_				 =						0.1
								A_				 =						0.1
								A_				 =						512
								A_				 =						16
								A_				 =						2
								A_				 =						0.0_2
								A_				 =						3
								A_				 =						4
								A_				 =						128
								A_				 =						2
								A_				 =						9
								A_				 =						1
								A_				 =						None




			def      lowerCamelCase__     (				self						:					str	)      -> Optional[int]:







								"""simple docstring"""
								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_				 =						ConvBertConfig(
								    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       ,   return_dict=_snake_case       ,   )

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




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







								"""simple docstring"""
								A_				 =						TFConvBertModel(config=_snake_case	)
								A_				 =						{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

								A_				 =						[input_ids, input_mask]
								A_				 =						model(_snake_case	)

								A_				 =						model(_snake_case	)

								self.parent.assertEqual(result.last_hidden_state.shape       ,   (self.batch_size, self.seq_length, self.hidden_size)	)




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







								"""simple docstring"""
								A_				 =						TFConvBertForMaskedLM(config=_snake_case	)
								A_				 =						{
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}
								A_				 =						model(_snake_case	)
								self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.vocab_size)	)




			def      lowerCamelCase__     (				self						:					Dict       ,   _snake_case						:					List[str]       ,   _snake_case						:					Optional[Any]       ,   _snake_case						:					Any       ,   _snake_case						:					Tuple       ,   _snake_case						:					List[Any]       ,   _snake_case						:					List[str]       ,   _snake_case						:					Optional[Any]	)      -> Tuple:







								"""simple docstring"""
								A_				 =						self.num_labels
								A_				 =						TFConvBertForSequenceClassification(config=_snake_case	)
								A_				 =						{
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}

								A_				 =						model(_snake_case	)
								self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_labels)	)




			def      lowerCamelCase__     (				self						:					Any       ,   _snake_case						:					Optional[Any]       ,   _snake_case						:					str       ,   _snake_case						:					int       ,   _snake_case						:					List[Any]       ,   _snake_case						:					int       ,   _snake_case						:					List[Any]       ,   _snake_case						:					Dict	)      -> List[str]:







								"""simple docstring"""
								A_				 =						self.num_choices
								A_				 =						TFConvBertForMultipleChoice(config=_snake_case	)
								A_				 =						tf.tile(tf.expand_dims(_snake_case       ,   1	)       ,   (1, self.num_choices, 1)	)
								A_				 =						tf.tile(tf.expand_dims(_snake_case       ,   1	)       ,   (1, self.num_choices, 1)	)
								A_				 =						tf.tile(tf.expand_dims(_snake_case       ,   1	)       ,   (1, self.num_choices, 1)	)
								A_				 =						{
								    "input_ids": multiple_choice_inputs_ids,
								    "attention_mask": multiple_choice_input_mask,
								    "token_type_ids": multiple_choice_token_type_ids,
								}
								A_				 =						model(_snake_case	)
								self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_choices)	)




			def      lowerCamelCase__     (				self						:					Tuple       ,   _snake_case						:					Optional[Any]       ,   _snake_case						:					Union[str, Any]       ,   _snake_case						:					Dict       ,   _snake_case						:					List[str]       ,   _snake_case						:					List[Any]       ,   _snake_case						:					Any       ,   _snake_case						:					str	)      -> str:







								"""simple docstring"""
								A_				 =						self.num_labels
								A_				 =						TFConvBertForTokenClassification(config=_snake_case	)
								A_				 =						{
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}
								A_				 =						model(_snake_case	)
								self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.num_labels)	)




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







								"""simple docstring"""
								A_				 =						TFConvBertForQuestionAnswering(config=_snake_case	)
								A_				 =						{
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}

								A_				 =						model(_snake_case	)
								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)	)






			def      lowerCamelCase__     (				self						:					Any	)      -> Dict:







								"""simple docstring"""
								A_				 =						self.prepare_config_and_inputs()
								(
								    (
								    A_
								) ,    (
								    A_
								) ,    (
								    A_
								) ,    (
								    A_
								) ,    (
								    A_
								) ,    (
								    A_
								) ,    (
								    A_
								) ,    
								)				 =						config_and_inputs
								A_				 =						{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
								return config, inputs_dict





@require_tf
class __lowerCAmelCase				(					_lowercase     ,				_lowercase     ,				unittest.TestCase      ):




			"""simple docstring"""







			snake_case	      =				(
			    (
			        TFConvBertModel,
			        TFConvBertForMaskedLM,
			        TFConvBertForQuestionAnswering,
			        TFConvBertForSequenceClassification,
			        TFConvBertForTokenClassification,
			        TFConvBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			snake_case	      =				(
			    {
			        "feature-extraction": TFConvBertModel,
			        "fill-mask": TFConvBertForMaskedLM,
			        "question-answering": TFConvBertForQuestionAnswering,
			        "text-classification": TFConvBertForSequenceClassification,
			        "token-classification": TFConvBertForTokenClassification,
			        "zero-shot": TFConvBertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			snake_case	      =				False
			snake_case	      =				False
			snake_case	      =				False




			def      lowerCamelCase__     (				self						:					Dict	)      -> int:







								"""simple docstring"""
								A_				 =						TFConvBertModelTester(self	)
								A_				 =						ConfigTester(self       ,   config_class=_snake_case       ,   hidden_size=37	)




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







								"""simple docstring"""
								self.config_tester.run_common_tests()




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







								"""simple docstring"""
								A_				 =						self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_snake_case	)




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







								"""simple docstring"""
								A_				 =						self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*_snake_case	)




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







								"""simple docstring"""
								A_				 =						self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*_snake_case	)




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







								"""simple docstring"""
								A_				 =						self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*_snake_case	)




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







								"""simple docstring"""
								A_				 =						self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*_snake_case	)




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







								"""simple docstring"""
								A_				 =						self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*_snake_case	)




			@slow
			def      lowerCamelCase__     (				self						:					str	)      -> List[Any]:







								"""simple docstring"""
								A_ ,    A_				 =						self.model_tester.prepare_config_and_inputs_for_common()
								A_				 =						True
								A_				 =						True

								if hasattr(_snake_case       ,   "use_cache"	):
													A_				 =						True

								A_				 =						getattr(self.model_tester       ,   "encoder_seq_length"       ,   self.model_tester.seq_length	)
								A_				 =						getattr(self.model_tester       ,   "key_length"       ,   _snake_case	)

								for model_class in self.all_model_classes:
													A_				 =						self._prepare_for_class(_snake_case       ,   _snake_case	)
													A_				 =						model_class(_snake_case	)
													A_				 =						len(model(_snake_case	)	)

													with tempfile.TemporaryDirectory() as tmpdirname:
																		model.save_pretrained(_snake_case       ,   saved_model=_snake_case	)
																		A_				 =						os.path.join(_snake_case       ,   "saved_model"       ,   "1"	)
																		A_				 =						tf.keras.models.load_model(_snake_case	)
																		A_				 =						model(_snake_case	)

																		if self.is_encoder_decoder:
																							A_				 =						outputs["encoder_hidden_states"]
																							A_				 =						outputs["encoder_attentions"]
																		else:
																							A_				 =						outputs["hidden_states"]
																							A_				 =						outputs["attentions"]

																		self.assertEqual(len(_snake_case	)       ,   _snake_case	)

																		A_				 =						getattr(
																		    self.model_tester       ,   "expected_num_hidden_layers"       ,   self.model_tester.num_hidden_layers + 1	)

																		self.assertEqual(len(_snake_case	)       ,   _snake_case	)
																		self.assertListEqual(
																		    list(output_hidden_states[0].shape[-2:]	)       ,   [self.model_tester.seq_length, self.model_tester.hidden_size]       ,   )

																		self.assertEqual(len(_snake_case	)       ,   self.model_tester.num_hidden_layers	)
																		self.assertListEqual(
																		    list(output_attentions[0].shape[-3:]	)       ,   [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]       ,   )




			@slow
			def      lowerCamelCase__     (				self						:					Any	)      -> List[Any]:







								"""simple docstring"""
								A_				 =						TFConvBertModel.from_pretrained("YituTech/conv-bert-base"	)
								self.assertIsNotNone(_snake_case	)






			def      lowerCamelCase__     (				self						:					str	)      -> int:







								"""simple docstring"""
								A_ ,    A_				 =						self.model_tester.prepare_config_and_inputs_for_common()
								A_				 =						True
								A_				 =						getattr(self.model_tester       ,   "decoder_seq_length"       ,   self.model_tester.seq_length	)
								A_				 =						getattr(self.model_tester       ,   "encoder_seq_length"       ,   self.model_tester.seq_length	)
								A_				 =						getattr(self.model_tester       ,   "key_length"       ,   _snake_case	)
								A_				 =						getattr(self.model_tester       ,   "key_length"       ,   _snake_case	)

								def check_decoder_attentions_output(_snake_case						:					int	):
													A_				 =						len(_snake_case	)
													self.assertEqual(out_len % 2       ,   0	)
													A_				 =						outputs.decoder_attentions
													self.assertEqual(len(_snake_case	)       ,   self.model_tester.num_hidden_layers	)
													self.assertListEqual(
													    list(decoder_attentions[0].shape[-3:]	)       ,   [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]       ,   )

								def check_encoder_attentions_output(_snake_case						:					Union[str, Any]	):
													A_				 =						[
													    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
													]
													self.assertEqual(len(_snake_case	)       ,   self.model_tester.num_hidden_layers	)
													self.assertListEqual(
													    list(attentions[0].shape[-3:]	)       ,   [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]       ,   )

								for model_class in self.all_model_classes:
													A_				 =						True
													A_				 =						False
													A_				 =						model_class(_snake_case	)
													A_				 =						model(self._prepare_for_class(_snake_case       ,   _snake_case	)	)
													A_				 =						len(_snake_case	)
													self.assertEqual(config.output_hidden_states       ,   _snake_case	)
													check_encoder_attentions_output(_snake_case	)

													if self.is_encoder_decoder:
																		A_				 =						model_class(_snake_case	)
																		A_				 =						model(self._prepare_for_class(_snake_case       ,   _snake_case	)	)
																		self.assertEqual(config.output_hidden_states       ,   _snake_case	)
																		check_decoder_attentions_output(_snake_case	)

													# Check that output attentions can also be changed via the config
													del inputs_dict["output_attentions"]
													A_				 =						True
													A_				 =						model_class(_snake_case	)
													A_				 =						model(self._prepare_for_class(_snake_case       ,   _snake_case	)	)
													self.assertEqual(config.output_hidden_states       ,   _snake_case	)
													check_encoder_attentions_output(_snake_case	)

													# Check attention is always last and order is fine
													A_				 =						True
													A_				 =						True
													A_				 =						model_class(_snake_case	)
													A_				 =						model(self._prepare_for_class(_snake_case       ,   _snake_case	)	)

													self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)       ,   len(_snake_case	)	)
													self.assertEqual(model.config.output_hidden_states       ,   _snake_case	)
													check_encoder_attentions_output(_snake_case	)





@require_tf
class __lowerCAmelCase				(					unittest.TestCase      ):




			"""simple docstring"""







			@slow
			def      lowerCamelCase__     (				self						:					List[str]	)      -> Dict:







								"""simple docstring"""
								A_				 =						TFConvBertModel.from_pretrained("YituTech/conv-bert-base"	)
								A_				 =						tf.constant([[0, 1, 2, 3, 4, 5]]	)
								A_				 =						model(_snake_case	)[0]

								A_				 =						[1, 6, 768]
								self.assertEqual(output.shape       ,   _snake_case	)

								A_				 =						tf.constant(
								    [
								        [
								            [-0.0_3_4_7_5_4_9_3, -0.4_6_8_6_0_3_4, -0.3_0_6_3_8_8_3_2],
								            [0.2_2_6_3_7_2_4_8, -0.2_6_9_8_8_6_4_6, -0.7_4_2_3_4_2_4],
								            [0.1_0_3_2_4_8_6_8, -0.4_5_0_1_3_5_0_8, -0.5_8_2_8_0_7_8_4],
								        ]
								    ]	)
								tf.debugging.assert_near(output[:, :3, :3]       ,   _snake_case       ,   atol=1e-4	)






"""simple docstring"""





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


UpperCamelCase_					:						Tuple				= logging.get_logger(__name__)

UpperCamelCase_					:						Optional[int]				= {
    '''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 __lowerCAmelCase				(					_lowercase      ):




			"""simple docstring"""







			snake_case	      =				"markuplm"




			def __init__(				self						:					Optional[Any]       ,   _snake_case						:					Dict=30_522       ,   _snake_case						:					str=768       ,   _snake_case						:					Tuple=12       ,   _snake_case						:					Union[str, Any]=12       ,   _snake_case						:					Union[str, Any]=3_072       ,   _snake_case						:					List[str]="gelu"       ,   _snake_case						:					List[str]=0.1       ,   _snake_case						:					Optional[Any]=0.1       ,   _snake_case						:					Dict=512       ,   _snake_case						:					Union[str, Any]=2       ,   _snake_case						:					Tuple=0.0_2       ,   _snake_case						:					Tuple=1e-12       ,   _snake_case						:					Tuple=0       ,   _snake_case						:					Tuple=0       ,   _snake_case						:					Optional[Any]=2       ,   _snake_case						:					int=256       ,   _snake_case						:					Dict=1_024       ,   _snake_case						:					List[str]=216       ,   _snake_case						:					Optional[int]=1_001       ,   _snake_case						:					Union[str, Any]=32       ,   _snake_case						:					List[str]=50       ,   _snake_case						:					str="absolute"       ,   _snake_case						:					Optional[Any]=True       ,   _snake_case						:					str=None       ,   **_snake_case						:					Optional[Any]       ,   )      -> Optional[Any]:







								"""simple docstring"""
								super().__init__(
								    pad_token_id=_snake_case       ,   bos_token_id=_snake_case       ,   eos_token_id=_snake_case       ,   **_snake_case       ,   )
								A_				 =						vocab_size
								A_				 =						hidden_size
								A_				 =						num_hidden_layers
								A_				 =						num_attention_heads
								A_				 =						hidden_act
								A_				 =						intermediate_size
								A_				 =						hidden_dropout_prob
								A_				 =						attention_probs_dropout_prob
								A_				 =						max_position_embeddings
								A_				 =						type_vocab_size
								A_				 =						initializer_range
								A_				 =						layer_norm_eps
								A_				 =						position_embedding_type
								A_				 =						use_cache
								A_				 =						classifier_dropout
								# additional properties
								A_				 =						max_depth
								A_				 =						max_xpath_tag_unit_embeddings
								A_				 =						max_xpath_subs_unit_embeddings
								A_				 =						tag_pad_id
								A_				 =						subs_pad_id
								A_				 =						xpath_unit_hidden_size




