
"""simple docstring"""



import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, 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 (
	    FlaubertForMultipleChoice,
	    FlaubertForQuestionAnswering,
	    FlaubertForQuestionAnsweringSimple,
	    FlaubertForSequenceClassification,
	    FlaubertForTokenClassification,
	    FlaubertModel,
	    FlaubertWithLMHeadModel,
	)
	from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST
class        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase							):






				'''simple docstring'''



				def __init__(					self	:			Any				,     UpperCAmelCase_	:			List[str]				,     UpperCAmelCase_	:			Union[str, Any]=13				,     UpperCAmelCase_	:			Optional[Any]=7				,     UpperCAmelCase_	:			Optional[int]=True				,     UpperCAmelCase_	:			List[str]=True				,     UpperCAmelCase_	:			List[str]=True				,     UpperCAmelCase_	:			List[str]=True				,     UpperCAmelCase_	:			int=True				,     UpperCAmelCase_	:			Any=False				,     UpperCAmelCase_	:			List[str]=False				,     UpperCAmelCase_	:			List[Any]=False				,     UpperCAmelCase_	:			Optional[Any]=2				,     UpperCAmelCase_	:			Tuple=99				,     UpperCAmelCase_	:			Optional[int]=0				,     UpperCAmelCase_	:			str=32				,     UpperCAmelCase_	:			List[str]=5				,     UpperCAmelCase_	:			Union[str, Any]=4				,     UpperCAmelCase_	:			Optional[Any]=0.1				,     UpperCAmelCase_	:			List[str]=0.1				,     UpperCAmelCase_	:			Any=512				,     UpperCAmelCase_	:			Any=12				,     UpperCAmelCase_	:			Tuple=2				,     UpperCAmelCase_	:			str=0.02				,     UpperCAmelCase_	:			Any=3				,     UpperCAmelCase_	:			Union[str, Any]=4				,     UpperCAmelCase_	:			Optional[Any]="last"				,     UpperCAmelCase_	:			int=None				,     UpperCAmelCase_	:			Optional[Any]=None				,     )		->  int:







											"""simple docstring"""




											_lowerCAmelCase  	=			parent
											_lowerCAmelCase  	=			batch_size
											_lowerCAmelCase  	=			seq_length
											_lowerCAmelCase  	=			is_training
											_lowerCAmelCase  	=			use_input_lengths
											_lowerCAmelCase  	=			use_token_type_ids
											_lowerCAmelCase  	=			use_labels
											_lowerCAmelCase  	=			gelu_activation
											_lowerCAmelCase  	=			sinusoidal_embeddings
											_lowerCAmelCase  	=			causal
											_lowerCAmelCase  	=			asm
											_lowerCAmelCase  	=			n_langs
											_lowerCAmelCase  	=			vocab_size
											_lowerCAmelCase  	=			n_special
											_lowerCAmelCase  	=			hidden_size
											_lowerCAmelCase  	=			num_hidden_layers
											_lowerCAmelCase  	=			num_attention_heads
											_lowerCAmelCase  	=			hidden_dropout_prob
											_lowerCAmelCase  	=			attention_probs_dropout_prob
											_lowerCAmelCase  	=			max_position_embeddings
											_lowerCAmelCase  	=			type_vocab_size
											_lowerCAmelCase  	=			type_sequence_label_size
											_lowerCAmelCase  	=			initializer_range
											_lowerCAmelCase  	=			num_labels
											_lowerCAmelCase  	=			num_choices
											_lowerCAmelCase  	=			summary_type
											_lowerCAmelCase  	=			use_proj
											_lowerCAmelCase  	=			scope


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







											"""simple docstring"""




											_lowerCAmelCase  	=			ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size  )
											_lowerCAmelCase  	=			random_attention_mask([self.batch_size, self.seq_length]  )

											_lowerCAmelCase  	=			None
											if self.use_input_lengths:
																		_lowerCAmelCase  	=			(
																		    ids_tensor([self.batch_size]				,     vocab_size=2  ) + self.seq_length - 2
																		)  # small variation of seq_length

											_lowerCAmelCase  	=			None
											if self.use_token_type_ids:
																		_lowerCAmelCase  	=			ids_tensor([self.batch_size, self.seq_length]				,     self.n_langs  )

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

											_lowerCAmelCase  	=			self.get_config()

											return (
											    config,
											    input_ids,
											    token_type_ids,
											    input_lengths,
											    sequence_labels,
											    token_labels,
											    is_impossible_labels,
											    choice_labels,
											    input_mask,
											)


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







											"""simple docstring"""




											return FlaubertConfig(
											    vocab_size=self.vocab_size				,     n_special=self.n_special				,     emb_dim=self.hidden_size				,     n_layers=self.num_hidden_layers				,     n_heads=self.num_attention_heads				,     dropout=self.hidden_dropout_prob				,     attention_dropout=self.attention_probs_dropout_prob				,     gelu_activation=self.gelu_activation				,     sinusoidal_embeddings=self.sinusoidal_embeddings				,     asm=self.asm				,     causal=self.causal				,     n_langs=self.n_langs				,     max_position_embeddings=self.max_position_embeddings				,     initializer_range=self.initializer_range				,     summary_type=self.summary_type				,     use_proj=self.use_proj				,     )


				def 				__lowerCamelCase	(					self	:			List[Any]				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			int				,     )		->  Dict:







											"""simple docstring"""




											_lowerCAmelCase  	=			FlaubertModel(config=__UpperCamelCase  )
											model.to(__UpperCamelCase  )
											model.eval()
											_lowerCAmelCase  	=			model(__UpperCamelCase				,     lengths=__UpperCamelCase				,     langs=__UpperCamelCase  )
											_lowerCAmelCase  	=			model(__UpperCamelCase				,     langs=__UpperCamelCase  )
											_lowerCAmelCase  	=			model(__UpperCamelCase  )
											self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)  )


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







											"""simple docstring"""




											_lowerCAmelCase  	=			FlaubertWithLMHeadModel(__UpperCamelCase  )
											model.to(__UpperCamelCase  )
											model.eval()

											_lowerCAmelCase  	=			model(__UpperCamelCase				,     token_type_ids=__UpperCamelCase				,     labels=__UpperCamelCase  )
											self.parent.assertEqual(result.loss.shape				,     ()  )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.vocab_size)  )


				def 				__lowerCamelCase	(					self	:			List[str]				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			List[str]				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			int				,     )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase  	=			FlaubertForQuestionAnsweringSimple(__UpperCamelCase  )
											model.to(__UpperCamelCase  )
											model.eval()

											_lowerCAmelCase  	=			model(__UpperCamelCase  )

											_lowerCAmelCase  	=			model(__UpperCamelCase				,     start_positions=__UpperCamelCase				,     end_positions=__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)  )


				def 				__lowerCamelCase	(					self	:			Optional[Any]				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			int				,     UpperCAmelCase_	:			List[str]				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			str				,     )		->  int:







											"""simple docstring"""




											_lowerCAmelCase  	=			FlaubertForQuestionAnswering(__UpperCamelCase  )
											model.to(__UpperCamelCase  )
											model.eval()

											_lowerCAmelCase  	=			model(__UpperCamelCase  )

											_lowerCAmelCase  	=			model(
											    __UpperCamelCase				,     start_positions=__UpperCamelCase				,     end_positions=__UpperCamelCase				,     cls_index=__UpperCamelCase				,     is_impossible=__UpperCamelCase				,     p_mask=__UpperCamelCase				,     )

											_lowerCAmelCase  	=			model(
											    __UpperCamelCase				,     start_positions=__UpperCamelCase				,     end_positions=__UpperCamelCase				,     cls_index=__UpperCamelCase				,     is_impossible=__UpperCamelCase				,     )

											((_lowerCAmelCase   )	,		   )  	=			result_with_labels.to_tuple()

											_lowerCAmelCase  	=			model(__UpperCamelCase				,     start_positions=__UpperCamelCase				,     end_positions=__UpperCamelCase  )

											((_lowerCAmelCase   )	,		   )  	=			result_with_labels.to_tuple()

											self.parent.assertEqual(result_with_labels.loss.shape				,     ()  )
											self.parent.assertEqual(result.start_top_log_probs.shape				,     (self.batch_size, model.config.start_n_top)  )
											self.parent.assertEqual(result.start_top_index.shape				,     (self.batch_size, model.config.start_n_top)  )
											self.parent.assertEqual(
											    result.end_top_log_probs.shape				,     (self.batch_size, model.config.start_n_top * model.config.end_n_top)  )
											self.parent.assertEqual(
											    result.end_top_index.shape				,     (self.batch_size, model.config.start_n_top * model.config.end_n_top)  )
											self.parent.assertEqual(result.cls_logits.shape				,     (self.batch_size,)  )


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







											"""simple docstring"""




											_lowerCAmelCase  	=			FlaubertForSequenceClassification(__UpperCamelCase  )
											model.to(__UpperCamelCase  )
											model.eval()

											_lowerCAmelCase  	=			model(__UpperCamelCase  )
											_lowerCAmelCase  	=			model(__UpperCamelCase				,     labels=__UpperCamelCase  )

											self.parent.assertEqual(result.loss.shape				,     ()  )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.type_sequence_label_size)  )


				def 				__lowerCamelCase	(					self	:			Optional[int]				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			List[str]				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			List[str]				,     UpperCAmelCase_	:			Optional[Any]				,     )		->  Optional[int]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.num_labels
											_lowerCAmelCase  	=			FlaubertForTokenClassification(__UpperCamelCase  )
											model.to(__UpperCamelCase  )
											model.eval()

											_lowerCAmelCase  	=			model(__UpperCamelCase				,     attention_mask=__UpperCamelCase				,     labels=__UpperCamelCase  )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels)  )


				def 				__lowerCamelCase	(					self	:			str				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			int				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Union[str, Any]				,     )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.num_choices
											_lowerCAmelCase  	=			FlaubertForMultipleChoice(config=__UpperCamelCase  )
											model.to(__UpperCamelCase  )
											model.eval()
											_lowerCAmelCase  	=			input_ids.unsqueeze(1  ).expand(-1				,     self.num_choices				,     -1  ).contiguous()
											_lowerCAmelCase  	=			token_type_ids.unsqueeze(1  ).expand(-1				,     self.num_choices				,     -1  ).contiguous()
											_lowerCAmelCase  	=			input_mask.unsqueeze(1  ).expand(-1				,     self.num_choices				,     -1  ).contiguous()
											_lowerCAmelCase  	=			model(
											    __UpperCamelCase				,     attention_mask=__UpperCamelCase				,     token_type_ids=__UpperCamelCase				,     labels=__UpperCamelCase				,     )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices)  )







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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.prepare_config_and_inputs()
											(
											    (
											    _lowerCAmelCase   
											)	,		(
											    _lowerCAmelCase   
											)	,		(
											    _lowerCAmelCase   
											)	,		(
											    _lowerCAmelCase   
											)	,		(
											    _lowerCAmelCase   
											)	,		(
											    _lowerCAmelCase   
											)	,		(
											    _lowerCAmelCase   
											)	,		(
											    _lowerCAmelCase   
											)	,		(
											    _lowerCAmelCase   
											)	,		   
											)  	=			config_and_inputs
											_lowerCAmelCase  	=			{
											    'input_ids': input_ids,
											    'token_type_ids': token_type_ids,
											    'lengths': input_lengths,
											    'attention_mask': input_mask,
											}
											return config, inputs_dict






@require_torch
class        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase      ,				UpperCAmelCase      ,				unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Optional[Any]							=					(
				    (
				        FlaubertModel,
				        FlaubertWithLMHeadModel,
				        FlaubertForQuestionAnswering,
				        FlaubertForQuestionAnsweringSimple,
				        FlaubertForSequenceClassification,
				        FlaubertForTokenClassification,
				        FlaubertForMultipleChoice,
				    )
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE_:							Optional[Any]							=					(
				    {
				        "feature-extraction": FlaubertModel,
				        "fill-mask": FlaubertWithLMHeadModel,
				        "question-answering": FlaubertForQuestionAnsweringSimple,
				        "text-classification": FlaubertForSequenceClassification,
				        "token-classification": FlaubertForTokenClassification,
				        "zero-shot": FlaubertForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)


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







											"""simple docstring"""




											if (
											    pipeline_test_casse_name == "QAPipelineTests"
											    and tokenizer_name is not None
											    and not tokenizer_name.endswith('Fast'  )
											):
																		# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
																		# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
																		# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
																		return True

											return False


				def 				__lowerCamelCase	(					self	:			Optional[int]				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			Any=False  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			super()._prepare_for_class(__UpperCamelCase				,     __UpperCamelCase				,     return_labels=__UpperCamelCase  )

											if return_labels:
																		if model_class.__name__ == "FlaubertForQuestionAnswering":
																									_lowerCAmelCase  	=			torch.zeros(
																									    self.model_tester.batch_size				,     dtype=torch.long				,     device=__UpperCamelCase  )
																									_lowerCAmelCase  	=			torch.zeros(
																									    self.model_tester.batch_size				,     dtype=torch.long				,     device=__UpperCamelCase  )

											return inputs_dict


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







											"""simple docstring"""




											_lowerCAmelCase  	=			FlaubertModelTester(self  )
											_lowerCAmelCase  	=			ConfigTester(self				,     config_class=__UpperCamelCase				,     emb_dim=37  )


				def 				__lowerCamelCase	(					self	:			int  )		->  List[str]:







											"""simple docstring"""




											self.config_tester.run_common_tests()


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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_model(*__UpperCamelCase  )


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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_lm_head(*__UpperCamelCase  )


				def 				__lowerCamelCase	(					self	:			int  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_simple_qa(*__UpperCamelCase  )


				def 				__lowerCamelCase	(					self	:			Dict  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_qa(*__UpperCamelCase  )


				def 				__lowerCamelCase	(					self	:			Tuple  )		->  Optional[int]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_sequence_classif(*__UpperCamelCase  )


				def 				__lowerCamelCase	(					self	:			Dict  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_token_classif(*__UpperCamelCase  )


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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_multiple_choice(*__UpperCamelCase  )


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







											"""simple docstring"""




											for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		_lowerCAmelCase  	=			FlaubertModel.from_pretrained(__UpperCamelCase  )
																		self.assertIsNotNone(__UpperCamelCase  )







				@slow
				@require_torch_gpu
				def 				__lowerCamelCase	(					self	:			Tuple  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs_for_common()
											for model_class in self.all_model_classes:
																		# FlauBertForMultipleChoice behaves incorrectly in JIT environments.
																		if model_class == FlaubertForMultipleChoice:
																									return

																		_lowerCAmelCase  	=			True
																		_lowerCAmelCase  	=			model_class(config=__UpperCamelCase  )

																		_lowerCAmelCase  	=			self._prepare_for_class(__UpperCamelCase				,     __UpperCamelCase  )
																		_lowerCAmelCase  	=			torch.jit.trace(
																		    __UpperCamelCase				,     (inputs_dict['input_ids'].to('cpu'  ), inputs_dict['attention_mask'].to('cpu'  ))  )

																		with tempfile.TemporaryDirectory() as tmp:
																									torch.jit.save(__UpperCamelCase				,     os.path.join(__UpperCamelCase				,     'traced_model.pt'  )  )
																									_lowerCAmelCase  	=			torch.jit.load(os.path.join(__UpperCamelCase				,     'traced_model.pt'  )				,     map_location=__UpperCamelCase  )
																									loaded(inputs_dict['input_ids'].to(__UpperCamelCase  )				,     inputs_dict['attention_mask'].to(__UpperCamelCase  )  )






@require_torch
class        _SCREAMING_SNAKE_CASE  (  unittest.TestCase							):






				'''simple docstring'''



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







											"""simple docstring"""




											_lowerCAmelCase  	=			FlaubertModel.from_pretrained('flaubert/flaubert_base_cased'  )
											_lowerCAmelCase  	=			torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]  )
											with torch.no_grad():
																		_lowerCAmelCase  	=			model(__UpperCamelCase  )[0]
											_lowerCAmelCase  	=			torch.Size((1, 11, 768)  )
											self.assertEqual(output.shape				,     __UpperCamelCase  )
											_lowerCAmelCase  	=			torch.tensor(
											    [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]]  )

											self.assertTrue(torch.allclose(output[:, :3, :3]				,     __UpperCamelCase				,     atol=1E-4  )  )


"""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        _SCREAMING_SNAKE_CASE  :






				'''simple docstring'''



				def __init__(					self	:			Tuple				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			str=13				,     UpperCAmelCase_	:			Dict=7				,     UpperCAmelCase_	:			Tuple=True				,     UpperCAmelCase_	:			Any=True				,     UpperCAmelCase_	:			Optional[int]=False				,     UpperCAmelCase_	:			Optional[Any]=True				,     UpperCAmelCase_	:			Optional[Any]=99				,     UpperCAmelCase_	:			int=64				,     UpperCAmelCase_	:			Union[str, Any]=5				,     UpperCAmelCase_	:			Union[str, Any]=4				,     UpperCAmelCase_	:			List[Any]=64				,     UpperCAmelCase_	:			List[str]="gelu"				,     UpperCAmelCase_	:			Optional[Any]=0.1				,     UpperCAmelCase_	:			Dict=0.1				,     UpperCAmelCase_	:			Tuple=512				,     UpperCAmelCase_	:			Optional[Any]=16				,     UpperCAmelCase_	:			str=2				,     UpperCAmelCase_	:			int=0.02				,     UpperCAmelCase_	:			List[str]=3				,     UpperCAmelCase_	:			int=4				,     UpperCAmelCase_	:			Tuple=None				,     )		->  Optional[int]:







											"""simple docstring"""




											_lowerCAmelCase  	=			parent
											_lowerCAmelCase  	=			batch_size
											_lowerCAmelCase  	=			seq_length
											_lowerCAmelCase  	=			is_training
											_lowerCAmelCase  	=			use_input_mask
											_lowerCAmelCase  	=			use_token_type_ids
											_lowerCAmelCase  	=			use_labels
											_lowerCAmelCase  	=			vocab_size
											_lowerCAmelCase  	=			hidden_size
											_lowerCAmelCase  	=			num_hidden_layers
											_lowerCAmelCase  	=			num_attention_heads
											_lowerCAmelCase  	=			intermediate_size
											_lowerCAmelCase  	=			hidden_act
											_lowerCAmelCase  	=			hidden_dropout_prob
											_lowerCAmelCase  	=			attention_probs_dropout_prob
											_lowerCAmelCase  	=			max_position_embeddings
											_lowerCAmelCase  	=			type_vocab_size
											_lowerCAmelCase  	=			type_sequence_label_size
											_lowerCAmelCase  	=			initializer_range
											_lowerCAmelCase  	=			num_labels
											_lowerCAmelCase  	=			num_choices
											_lowerCAmelCase  	=			scope


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







											"""simple docstring"""




											return MPNetConfig.from_pretrained('microsoft/mpnet-base'  )


				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  Tuple:







											"""simple docstring"""




											_lowerCAmelCase  	=			ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size  )

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

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

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


				def 				__lowerCamelCase	(					self	:			Any  )		->  Tuple:







											"""simple docstring"""




											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				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			Optional[int]  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			MPNetModel(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(UpperCAmelCase_				,     UpperCAmelCase_  )
											_lowerCAmelCase  	=			model(UpperCAmelCase_  )
											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	:			List[str]				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			int  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			MPNetForQuestionAnswering(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(
											    UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     start_positions=UpperCAmelCase_				,     end_positions=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)  )


				def 				__lowerCamelCase	(					self	:			Dict				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			Union[str, Any]  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.num_labels
											_lowerCAmelCase  	=			MPNetForSequenceClassification(UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     labels=UpperCAmelCase_  )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels)  )


				def 				__lowerCamelCase	(					self	:			int				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			List[Any]  )		->  Dict:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.num_choices
											_lowerCAmelCase  	=			MPNetForMultipleChoice(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			input_ids.unsqueeze(1  ).expand(-1				,     self.num_choices				,     -1  ).contiguous()
											_lowerCAmelCase  	=			input_mask.unsqueeze(1  ).expand(-1				,     self.num_choices				,     -1  ).contiguous()
											_lowerCAmelCase  	=			model(
											    UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     labels=UpperCAmelCase_				,     )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices)  )


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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.num_labels
											_lowerCAmelCase  	=			MPNetForTokenClassification(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     labels=UpperCAmelCase_  )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels)  )







				def 				__lowerCamelCase	(					self	:			str  )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.prepare_config_and_inputs()
											((_lowerCAmelCase)	,		(_lowerCAmelCase)	,		(_lowerCAmelCase)	,		(_lowerCAmelCase)	,		(_lowerCAmelCase)	,		(_lowerCAmelCase))  	=			config_and_inputs
											_lowerCAmelCase  	=			{'input_ids': input_ids, 'attention_mask': input_mask}
											return config, inputs_dict






@require_torch
class        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase      ,				UpperCAmelCase      ,				unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Union[str, Any]							=					(
				    (
				        MPNetForMaskedLM,
				        MPNetForMultipleChoice,
				        MPNetForQuestionAnswering,
				        MPNetForSequenceClassification,
				        MPNetForTokenClassification,
				        MPNetModel,
				    )
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE_:							int							=					(
				    {
				        "feature-extraction": MPNetModel,
				        "fill-mask": MPNetForMaskedLM,
				        "question-answering": MPNetForQuestionAnswering,
				        "text-classification": MPNetForSequenceClassification,
				        "token-classification": MPNetForTokenClassification,
				        "zero-shot": MPNetForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				SCREAMING_SNAKE_CASE_:							Optional[Any]							=					False
				SCREAMING_SNAKE_CASE_:							Dict							=					True


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







											"""simple docstring"""




											_lowerCAmelCase  	=			MPNetModelTester(self  )
											_lowerCAmelCase  	=			ConfigTester(self				,     config_class=UpperCAmelCase_				,     hidden_size=37  )


				def 				__lowerCamelCase	(					self	:			Dict  )		->  Dict:







											"""simple docstring"""




											self.config_tester.run_common_tests()


				def 				__lowerCamelCase	(					self	:			Tuple  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_model(*UpperCAmelCase_  )


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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_sequence_classification(*UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			Optional[int]  )		->  int:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_multiple_choice(*UpperCAmelCase_  )


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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_token_classification(*UpperCAmelCase_  )







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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_question_answering(*UpperCAmelCase_  )






@require_torch
class        _SCREAMING_SNAKE_CASE  (  unittest.TestCase							):






				'''simple docstring'''



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







											"""simple docstring"""




											_lowerCAmelCase  	=			MPNetModel.from_pretrained('microsoft/mpnet-base'  )
											_lowerCAmelCase  	=			torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]  )
											_lowerCAmelCase  	=			model(UpperCAmelCase_  )[0]
											_lowerCAmelCase  	=			torch.Size((1, 11, 768)  )
											self.assertEqual(output.shape				,     UpperCAmelCase_  )
											_lowerCAmelCase  	=			torch.tensor(
											    [[[-0.0550, 0.1943, -0.0740], [-0.0562, 0.2211, -0.0579], [-0.0437, 0.3337, -0.0641]]]  )
											# compare the actual values for a slice.
											self.assertTrue(torch.allclose(output[:, :3, :3]				,     UpperCAmelCase_				,     atol=1E-4  )  )






