





'''simple docstring'''







import unittest

from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow


if is_flax_available():
					import jax

					from transformers.models.auto.modeling_flax_auto import FlaxAutoModel
					from transformers.models.bert.modeling_flax_bert import FlaxBertModel
					from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel
@require_flax
class 				__SCREAMING_SNAKE_CASE  (unittest.TestCase      ):






				"""simple docstring"""







				@slow
				def   UpperCamelCase__   (  self     :					Optional[int]					):
								for model_name in ["bert-base-cased", "bert-large-uncased"]:
												with self.subTest(__a					):
																_a								= AutoConfig.from_pretrained(__a					)
																self.assertIsNotNone(__a					)
																self.assertIsInstance(__a					,				__a					)

																_a								= FlaxAutoModel.from_pretrained(__a					)
																self.assertIsNotNone(__a					)
																self.assertIsInstance(__a					,				__a					)




				@slow
				def   UpperCamelCase__   (  self     :					int					):
								for model_name in ["roberta-base", "roberta-large"]:
												with self.subTest(__a					):
																_a								= AutoConfig.from_pretrained(__a					)
																self.assertIsNotNone(__a					)
																self.assertIsInstance(__a					,				__a					)

																_a								= FlaxAutoModel.from_pretrained(__a					)
																self.assertIsNotNone(__a					)
																self.assertIsInstance(__a					,				__a					)




				@slow
				def   UpperCamelCase__   (  self     :					Optional[int]					):
								for model_name in ["bert-base-cased", "bert-large-uncased"]:
												_a								= AutoTokenizer.from_pretrained(__a					)
												_a								= FlaxBertModel.from_pretrained(__a					)
												_a								= tokenizer("Do you support jax jitted function?"					,				return_tensors=TensorType.JAX					)

												@jax.jit
												def eval(**__a     :					Optional[Any]					):
																return model(**__a					)

												eval(**__a					).block_until_ready()




				@slow
				def   UpperCamelCase__   (  self     :					Dict					):
								for model_name in ["roberta-base", "roberta-large"]:
												_a								= AutoTokenizer.from_pretrained(__a					)
												_a								= FlaxRobertaModel.from_pretrained(__a					)
												_a								= tokenizer("Do you support jax jitted function?"					,				return_tensors=TensorType.JAX					)

												@jax.jit
												def eval(**__a     :					str					):
																return model(**__a					)

												eval(**__a					).block_until_ready()




				def   UpperCamelCase__   (  self     :					Any					):
								with self.assertRaisesRegex(
								    __a					,				"bert-base is not a local folder and is not a valid model identifier"					):
												_a								= FlaxAutoModel.from_pretrained("bert-base"					)




				def   UpperCamelCase__   (  self     :					int					):
								with self.assertRaisesRegex(
								    __a					,				r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"					):
												_a								= FlaxAutoModel.from_pretrained(__a					,				revision="aaaaaa"					)




				def   UpperCamelCase__   (  self     :					Dict					):
								with self.assertRaisesRegex(
								    __a					,				"hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack"					,				):
												_a								= FlaxAutoModel.from_pretrained("hf-internal-testing/config-no-model"					)




				def   UpperCamelCase__   (  self     :					str					):
								with self.assertRaisesRegex(__a					,				"Use `from_pt=True` to load this model"					):
												_a								= FlaxAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only"					)

"""simple docstring"""


import unittest

from transformers import MPNetConfig, 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 (
        MPNetForMaskedLM,
        MPNetForMultipleChoice,
        MPNetForQuestionAnswering,
        MPNetForSequenceClassification,
        MPNetForTokenClassification,
        MPNetModel,
    )


class 				a :


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


    def    lowerCAmelCase_     (    self		:     Union[str, Any]    ):
      return MPNetConfig.from_pretrained("""microsoft/mpnet-base"""    )


    def    lowerCAmelCase_     (    self		:     Union[str, Any]    ):
      _UpperCAmelCase         =					ids_tensor([self.batch_size, self.seq_length]      , self.vocab_size    )

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

      _UpperCAmelCase         =					None
      _UpperCAmelCase         =					None
      _UpperCAmelCase         =					None
      if self.use_labels:
        _UpperCAmelCase         =					ids_tensor([self.batch_size]      , self.type_sequence_label_size    )
        _UpperCAmelCase         =					ids_tensor([self.batch_size, self.seq_length]      , self.num_labels    )
        _UpperCAmelCase         =					ids_tensor([self.batch_size]      , self.num_choices    )

      _UpperCAmelCase         =					self.get_config()
      return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels


    def    lowerCAmelCase_     (    self		:     Optional[int]    ):
      return 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      , )


    def    lowerCAmelCase_     (    self		:     Dict      , __lowerCAmelCase		:     Union[str, Any]      , __lowerCAmelCase		:     Any      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     Tuple      , __lowerCAmelCase		:     Union[str, Any]      , __lowerCAmelCase		:     str    ):
      _UpperCAmelCase         =					MPNetModel(config=__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()
      _UpperCAmelCase         =					model(__lowerCAmelCase      , __lowerCAmelCase    )
      _UpperCAmelCase         =					model(__lowerCAmelCase    )
      self.parent.assertEqual(result.last_hidden_state.shape      , (self.batch_size, self.seq_length, self.hidden_size)    )
      self.parent.assertEqual(result.pooler_output.shape      , (self.batch_size, self.hidden_size)    )


    def    lowerCAmelCase_     (    self		:     Optional[Any]      , __lowerCAmelCase		:     Optional[int]      , __lowerCAmelCase		:     Any      , __lowerCAmelCase		:     Optional[int]      , __lowerCAmelCase		:     List[str]      , __lowerCAmelCase		:     Dict      , __lowerCAmelCase		:     List[str]    ):
      _UpperCAmelCase         =					MPNetForQuestionAnswering(config=__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()
      _UpperCAmelCase         =					model(
          __lowerCAmelCase      , attention_mask=__lowerCAmelCase      , start_positions=__lowerCAmelCase      , end_positions=__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    lowerCAmelCase_     (    self		:     List[Any]      , __lowerCAmelCase		:     Tuple      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     Optional[Any]      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     List[Any]      , __lowerCAmelCase		:     int    ):
      _UpperCAmelCase         =					self.num_labels
      _UpperCAmelCase         =					MPNetForSequenceClassification(__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()
      _UpperCAmelCase         =					model(__lowerCAmelCase      , attention_mask=__lowerCAmelCase      , labels=__lowerCAmelCase    )
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.num_labels)    )


    def    lowerCAmelCase_     (    self		:     int      , __lowerCAmelCase		:     List[Any]      , __lowerCAmelCase		:     Optional[int]      , __lowerCAmelCase		:     Dict      , __lowerCAmelCase		:     List[str]      , __lowerCAmelCase		:     List[str]      , __lowerCAmelCase		:     Union[str, Any]    ):
      _UpperCAmelCase         =					self.num_choices
      _UpperCAmelCase         =					MPNetForMultipleChoice(config=__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()
      _UpperCAmelCase         =					input_ids.unsqueeze(1    ).expand(-1      , self.num_choices      , -1    ).contiguous()
      _UpperCAmelCase         =					input_mask.unsqueeze(1    ).expand(-1      , self.num_choices      , -1    ).contiguous()
      _UpperCAmelCase         =					model(
          __lowerCAmelCase      , attention_mask=__lowerCAmelCase      , labels=__lowerCAmelCase      , )
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.num_choices)    )


    def    lowerCAmelCase_     (    self		:     Optional[Any]      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     Dict      , __lowerCAmelCase		:     Optional[int]      , __lowerCAmelCase		:     Any      , __lowerCAmelCase		:     Tuple      , __lowerCAmelCase		:     Any    ):
      _UpperCAmelCase         =					self.num_labels
      _UpperCAmelCase         =					MPNetForTokenClassification(config=__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()
      _UpperCAmelCase         =					model(__lowerCAmelCase      , attention_mask=__lowerCAmelCase      , labels=__lowerCAmelCase    )
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.seq_length, self.num_labels)    )


    def    lowerCAmelCase_     (    self		:     Dict    ):
      _UpperCAmelCase         =					self.prepare_config_and_inputs()
      ((_UpperCAmelCase)       , (_UpperCAmelCase)       , (_UpperCAmelCase)       , (_UpperCAmelCase)       , (_UpperCAmelCase)       , (_UpperCAmelCase))         =					config_and_inputs
      _UpperCAmelCase         =					{"""input_ids""": input_ids, """attention_mask""": input_mask}
      return config, inputs_dict



@require_torch
class 				a (						lowerCAmelCase_		,							lowerCAmelCase_		,							unittest.TestCase      ):
    _snake_case   :     List[Any]   							=							(
        (
            MPNetForMaskedLM,
            MPNetForMultipleChoice,
            MPNetForQuestionAnswering,
            MPNetForSequenceClassification,
            MPNetForTokenClassification,
            MPNetModel,
        )
        if is_torch_available()
        else ()
    )
    _snake_case   :     Union[str, Any]   							=							(
        {
            'feature-extraction': MPNetModel,
            'fill-mask': MPNetForMaskedLM,
            'question-answering': MPNetForQuestionAnswering,
            'text-classification': MPNetForSequenceClassification,
            'token-classification': MPNetForTokenClassification,
            'zero-shot': MPNetForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    _snake_case   :     int   							=							False
    _snake_case   :     List[Any]   							=							True


    def    lowerCAmelCase_     (    self		:     Dict    ):
      _UpperCAmelCase         =					MPNetModelTester(self    )
      _UpperCAmelCase         =					ConfigTester(self      , config_class=__lowerCAmelCase      , hidden_size=37    )


    def    lowerCAmelCase_     (    self		:     Dict    ):
      self.config_tester.run_common_tests()


    def    lowerCAmelCase_     (    self		:     Tuple    ):
      _UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_model(*__lowerCAmelCase    )


    def    lowerCAmelCase_     (    self		:     Optional[int]    ):
      _UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_sequence_classification(*__lowerCAmelCase    )


    def    lowerCAmelCase_     (    self		:     Tuple    ):
      _UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_multiple_choice(*__lowerCAmelCase    )


    def    lowerCAmelCase_     (    self		:     List[Any]    ):
      _UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_token_classification(*__lowerCAmelCase    )


    def    lowerCAmelCase_     (    self		:     str    ):
      _UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_question_answering(*__lowerCAmelCase    )



@require_torch
class 				a (						unittest.TestCase      ):


    @slow
    def    lowerCAmelCase_     (    self		:     List[str]    ):
      _UpperCAmelCase         =					MPNetModel.from_pretrained("""microsoft/mpnet-base"""    )
      _UpperCAmelCase         =					torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]    )
      _UpperCAmelCase         =					model(__lowerCAmelCase    )[0]
      _UpperCAmelCase         =					torch.Size((1, 11, 768)    )
      self.assertEqual(output.shape      , __lowerCAmelCase    )
      _UpperCAmelCase         =					torch.tensor(
          [[[-0.0_550, 0.1_943, -0.0_740], [-0.0_562, 0.2_211, -0.0_579], [-0.0_437, 0.3_337, -0.0_641]]]    )
      # compare the actual values for a slice.
      self.assertTrue(torch.allclose(output[:, :3, :3]      , __lowerCAmelCase      , atol=1e-4    )    )
