


from __future__ import annotations

import unittest

from transformers import MPNetConfig, 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.models.mpnet.modeling_tf_mpnet import (
        TFMPNetForMaskedLM,
        TFMPNetForMultipleChoice,
        TFMPNetForQuestionAnswering,
        TFMPNetForSequenceClassification,
        TFMPNetForTokenClassification,
        TFMPNetModel,
    )







class 	_snake_case						:




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




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:List[str]											= ids_tensor([self.batch_size, self.seq_length]      ,						self.vocab_size					)

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

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

       a		:List[Any]											= MPNetConfig(
           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      ,						initializer_range=self.initializer_range      ,						)
       return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels




 def  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase					):
       a		:Union[str, Any]											= TFMPNetModel(config=_lowerCamelCase					)
       a		:Optional[int]											= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
       a		:int											= model(_lowerCamelCase					)
       a		:Dict											= [input_ids, input_mask]
       a		:List[str]											= model(_lowerCamelCase					)
       self.parent.assertEqual(result.last_hidden_state.shape      ,						(self.batch_size, self.seq_length, self.hidden_size)					)




 def  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase					):
       a		:int											= TFMPNetForMaskedLM(config=_lowerCamelCase					)
       a		:str											= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
       a		:List[Any]											= model(_lowerCamelCase					)
       self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.seq_length, self.vocab_size)					)




 def  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase					):
       a		:int											= TFMPNetForQuestionAnswering(config=_lowerCamelCase					)
       a		:Dict											= {
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
       }
       a		:Optional[int]											= model(_lowerCamelCase					)
       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  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase					):
       a		:Any											= self.num_labels
       a		:List[Any]											= TFMPNetForSequenceClassification(_lowerCamelCase					)
       a		:int											= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
       a		:Union[str, Any]											= model(_lowerCamelCase					)
       self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.num_labels)					)




 def  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase					):
       a		:Optional[Any]											= self.num_choices
       a		:str											= TFMPNetForMultipleChoice(_lowerCamelCase					)
       a		:Optional[int]											= tf.tile(tf.expand_dims(_lowerCamelCase      ,						1					)      ,						(1, self.num_choices, 1)					)
       a		:int											= tf.tile(tf.expand_dims(_lowerCamelCase      ,						1					)      ,						(1, self.num_choices, 1)					)
       a		:Tuple											= {
           '''input_ids''': multiple_choice_inputs_ids,
           '''attention_mask''': multiple_choice_input_mask,
       }
       a		:List[str]											= model(_lowerCamelCase					)
       self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.num_choices)					)




 def  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase					):
       a		:List[Any]											= self.num_labels
       a		:int											= TFMPNetForTokenClassification(_lowerCamelCase					)
       a		:str											= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
       a		:int											= model(_lowerCamelCase					)
       self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.seq_length, self.num_labels)					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Any											= self.prepare_config_and_inputs()
       (a)		:Dict											= config_and_inputs
       a		:Optional[Any]											= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
       return config, inputs_dict







@require_tf
class 	_snake_case						(   _snake_case		,					_snake_case		,					unittest.TestCase							):
 SCREAMING_SNAKE_CASE__   = (
     (
         TFMPNetForMaskedLM,
         TFMPNetForMultipleChoice,
         TFMPNetForQuestionAnswering,
         TFMPNetForSequenceClassification,
         TFMPNetForTokenClassification,
         TFMPNetModel,
     )
     if is_tf_available()
     else ()
 )
 SCREAMING_SNAKE_CASE__   = (
     {
         'feature-extraction': TFMPNetModel,
         'fill-mask': TFMPNetForMaskedLM,
         'question-answering': TFMPNetForQuestionAnswering,
         'text-classification': TFMPNetForSequenceClassification,
         'token-classification': TFMPNetForTokenClassification,
         'zero-shot': TFMPNetForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 SCREAMING_SNAKE_CASE__   = False
 SCREAMING_SNAKE_CASE__   = False




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Dict											= TFMPNetModelTester(self					)
       a		:Tuple											= ConfigTester(self      ,						config_class=_lowerCamelCase      ,						hidden_size=37					)




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




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Tuple											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_model(*_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Optional[int]											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_masked_lm(*_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:str											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_question_answering(*_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Optional[Any]											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_sequence_classification(*_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:List[str]											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_multiple_choice(*_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:str											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_token_classification(*_lowerCamelCase					)




 @slow
 def  SCREAMING_SNAKE_CASE__       (	self					):
       for model_name in ["microsoft/mpnet-base"]:
             a		:Optional[int]											= TFMPNetModel.from_pretrained(_lowerCamelCase					)
             self.assertIsNotNone(_lowerCamelCase					)







@require_tf
class 	_snake_case						(   unittest.TestCase							):




 @slow
 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:int											= TFMPNetModel.from_pretrained('''microsoft/mpnet-base'''					)
       a		:List[str]											= tf.constant([[0, 1, 2, 3, 4, 5]]					)
       a		:List[str]											= model(_lowerCamelCase					)[0]

       a		:int											= [1, 6, 768]
       self.assertEqual(output.shape      ,						_lowerCamelCase					)

       a		:List[str]											= tf.constant(
           [
               [
                   [-0.106_7172, 0.0821_6473, 0.002_4543],
                   [-0.0346_5879, 0.835_4118, -0.0325_2288],
                   [-0.0656_9476, -0.1242_4111, -0.049_4436],
               ]
           ]					)
       tf.debugging.assert_near(output[:, :3, :3]      ,						_lowerCamelCase      ,						atol=1e-4					)









from typing import Optional, Union

from ...file_utils import TensorType
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, TruncationStrategy







class 	_snake_case						(   _snake_case							):
 SCREAMING_SNAKE_CASE__   = 'MarkupLMFeatureExtractor'
 SCREAMING_SNAKE_CASE__   = ('MarkupLMTokenizer', 'MarkupLMTokenizerFast')
 SCREAMING_SNAKE_CASE__   = True




 def __call__(	self      ,						_lowerCamelCase=None      ,						_lowerCamelCase=None      ,						_lowerCamelCase=None      ,						_lowerCamelCase=None      ,						_lowerCamelCase=None      ,						_lowerCamelCase = True      ,						_lowerCamelCase = False      ,						_lowerCamelCase = None      ,						_lowerCamelCase = None      ,						_lowerCamelCase = 0      ,						_lowerCamelCase = None      ,						_lowerCamelCase = None      ,						_lowerCamelCase = None      ,						_lowerCamelCase = False      ,						_lowerCamelCase = False      ,						_lowerCamelCase = False      ,						_lowerCamelCase = False      ,						_lowerCamelCase = True      ,						_lowerCamelCase = None      ,						**_lowerCamelCase      ,						):
       # first, create nodes and xpaths
       if self.parse_html:
             if html_strings is None:
                   raise ValueError('''Make sure to pass HTML strings in case `parse_html` is set to `True`'''					)

             if nodes is not None or xpaths is not None or node_labels is not None:
                   raise ValueError(
                       '''Please don\'t pass nodes, xpaths nor node labels in case `parse_html` is set to `True`'''					)

             a		:int											= self.feature_extractor(_lowerCamelCase					)
             a		:Tuple											= features['''nodes''']
             a		:Dict											= features['''xpaths''']
       else:
             if html_strings is not None:
                   raise ValueError('''You have passed HTML strings but `parse_html` is set to `False`.'''					)
             if nodes is None or xpaths is None:
                   raise ValueError('''Make sure to pass nodes and xpaths in case `parse_html` is set to `False`'''					)

        # # second, apply the tokenizer
       if questions is not None and self.parse_html:
             if isinstance(_lowerCamelCase      ,						_lowerCamelCase					):
                   a		:Tuple											= [questions]  # add batch dimension (as the feature extractor always adds a batch dimension)

       a		:Optional[int]											= self.tokenizer(
           text=questions if questions is not None else nodes      ,						text_pair=nodes if questions is not None else None      ,						xpaths=_lowerCamelCase      ,						node_labels=_lowerCamelCase      ,						add_special_tokens=_lowerCamelCase      ,						padding=_lowerCamelCase      ,						truncation=_lowerCamelCase      ,						max_length=_lowerCamelCase      ,						stride=_lowerCamelCase      ,						pad_to_multiple_of=_lowerCamelCase      ,						return_token_type_ids=_lowerCamelCase      ,						return_attention_mask=_lowerCamelCase      ,						return_overflowing_tokens=_lowerCamelCase      ,						return_special_tokens_mask=_lowerCamelCase      ,						return_offsets_mapping=_lowerCamelCase      ,						return_length=_lowerCamelCase      ,						verbose=_lowerCamelCase      ,						return_tensors=_lowerCamelCase      ,						**_lowerCamelCase      ,						)

       return encoded_inputs




 def  SCREAMING_SNAKE_CASE__       (	self      ,						*_lowerCamelCase      ,						**_lowerCamelCase					):
       return self.tokenizer.batch_decode(*_lowerCamelCase      ,						**_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self      ,						*_lowerCamelCase      ,						**_lowerCamelCase					):
       return self.tokenizer.decode(*_lowerCamelCase      ,						**_lowerCamelCase					)




 @property
 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Any											= self.tokenizer.model_input_names
       return tokenizer_input_names





