



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 					__A       (			__SCREAMING_SNAKE_CASE   ):





			def __init__(self				,     __magic_name__				,     __magic_name__=13				,     __magic_name__=7				,     __magic_name__=True				,     __magic_name__=True				,     __magic_name__=True				,     __magic_name__=True				,     __magic_name__=True				,     __magic_name__=False				,     __magic_name__=False				,     __magic_name__=False				,     __magic_name__=2				,     __magic_name__=99				,     __magic_name__=0				,     __magic_name__=32				,     __magic_name__=5				,     __magic_name__=4				,     __magic_name__=0.1				,     __magic_name__=0.1				,     __magic_name__=512				,     __magic_name__=12				,     __magic_name__=2				,     __magic_name__=0.02				,     __magic_name__=3				,     __magic_name__=4				,     __magic_name__="last"				,     __magic_name__=None				,     __magic_name__=None				,     ):
						lowerCamelCase__			:   Any										=		parent
						lowerCamelCase__			:   Tuple										=		batch_size
						lowerCamelCase__			:   Optional[int]										=		seq_length
						lowerCamelCase__			:   Optional[Any]										=		is_training
						lowerCamelCase__			:   int										=		use_input_lengths
						lowerCamelCase__			:   str										=		use_token_type_ids
						lowerCamelCase__			:   Tuple										=		use_labels
						lowerCamelCase__			:   List[Any]										=		gelu_activation
						lowerCamelCase__			:   str										=		sinusoidal_embeddings
						lowerCamelCase__			:   int										=		causal
						lowerCamelCase__			:   int										=		asm
						lowerCamelCase__			:   List[Any]										=		n_langs
						lowerCamelCase__			:   Union[str, Any]										=		vocab_size
						lowerCamelCase__			:   Any										=		n_special
						lowerCamelCase__			:   Dict										=		hidden_size
						lowerCamelCase__			:   Any										=		num_hidden_layers
						lowerCamelCase__			:   Optional[int]										=		num_attention_heads
						lowerCamelCase__			:   int										=		hidden_dropout_prob
						lowerCamelCase__			:   List[str]										=		attention_probs_dropout_prob
						lowerCamelCase__			:   int										=		max_position_embeddings
						lowerCamelCase__			:   Optional[int]										=		type_vocab_size
						lowerCamelCase__			:   Optional[Any]										=		type_sequence_label_size
						lowerCamelCase__			:   str										=		initializer_range
						lowerCamelCase__			:   int										=		num_labels
						lowerCamelCase__			:   Tuple										=		num_choices
						lowerCamelCase__			:   List[str]										=		summary_type
						lowerCamelCase__			:   Union[str, Any]										=		use_proj
						lowerCamelCase__			:   int										=		scope





			def 							_snake_case     (self					):
						lowerCamelCase__			:   Optional[int]										=		ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size					)
						lowerCamelCase__			:   List[str]										=		random_attention_mask([self.batch_size, self.seq_length]					)

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

						lowerCamelCase__			:   str										=		None
						if self.use_token_type_ids:
									lowerCamelCase__			:   Union[str, Any]										=		ids_tensor([self.batch_size, self.seq_length]				,     self.n_langs					)

						lowerCamelCase__			:   Tuple										=		None
						lowerCamelCase__			:   Tuple										=		None
						lowerCamelCase__			:   List[Any]										=		None
						if self.use_labels:
									lowerCamelCase__			:   Any										=		ids_tensor([self.batch_size]				,     self.type_sequence_label_size					)
									lowerCamelCase__			:   Tuple										=		ids_tensor([self.batch_size, self.seq_length]				,     self.num_labels					)
									lowerCamelCase__			:   List[Any]										=		ids_tensor([self.batch_size]				,     2					).float()
									lowerCamelCase__			:   Optional[Any]										=		ids_tensor([self.batch_size]				,     self.num_choices					)

						lowerCamelCase__			:   Any										=		self.get_config()

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





			def 							_snake_case     (self					):
						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 							_snake_case     (self				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     ):
						lowerCamelCase__			:   int										=		FlaubertModel(config=__lowerCamelCase					)
						model.to(__lowerCamelCase					)
						model.eval()
						lowerCamelCase__			:   str										=		model(__lowerCamelCase				,     lengths=__lowerCamelCase				,     langs=__lowerCamelCase					)
						lowerCamelCase__			:   str										=		model(__lowerCamelCase				,     langs=__lowerCamelCase					)
						lowerCamelCase__			:   int										=		model(__lowerCamelCase					)
						self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)					)





			def 							_snake_case     (self				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     ):
						lowerCamelCase__			:   List[str]										=		FlaubertWithLMHeadModel(__lowerCamelCase					)
						model.to(__lowerCamelCase					)
						model.eval()

						lowerCamelCase__			:   List[Any]										=		model(__lowerCamelCase				,     token_type_ids=__lowerCamelCase				,     labels=__lowerCamelCase					)
						self.parent.assertEqual(result.loss.shape				,     ()					)
						self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.vocab_size)					)





			def 							_snake_case     (self				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     ):
						lowerCamelCase__			:   Optional[Any]										=		FlaubertForQuestionAnsweringSimple(__lowerCamelCase					)
						model.to(__lowerCamelCase					)
						model.eval()

						lowerCamelCase__			:   Any										=		model(__lowerCamelCase					)

						lowerCamelCase__			:   int										=		model(__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 							_snake_case     (self				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     ):
						lowerCamelCase__			:   Optional[int]										=		FlaubertForQuestionAnswering(__lowerCamelCase					)
						model.to(__lowerCamelCase					)
						model.eval()

						lowerCamelCase__			:   Union[str, Any]										=		model(__lowerCamelCase					)

						lowerCamelCase__			:   Dict										=		model(
						    __lowerCamelCase				,     start_positions=__lowerCamelCase				,     end_positions=__lowerCamelCase				,     cls_index=__lowerCamelCase				,     is_impossible=__lowerCamelCase				,     p_mask=__lowerCamelCase				,     )

						lowerCamelCase__			:   Tuple										=		model(
						    __lowerCamelCase				,     start_positions=__lowerCamelCase				,     end_positions=__lowerCamelCase				,     cls_index=__lowerCamelCase				,     is_impossible=__lowerCamelCase				,     )

						((lowerCamelCase__     )				,     )			:   List[str]										=		result_with_labels.to_tuple()

						lowerCamelCase__			:   List[str]										=		model(__lowerCamelCase				,     start_positions=__lowerCamelCase				,     end_positions=__lowerCamelCase					)

						((lowerCamelCase__     )				,     )			:   Optional[Any]										=		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 							_snake_case     (self				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     ):
						lowerCamelCase__			:   Dict										=		FlaubertForSequenceClassification(__lowerCamelCase					)
						model.to(__lowerCamelCase					)
						model.eval()

						lowerCamelCase__			:   Tuple										=		model(__lowerCamelCase					)
						lowerCamelCase__			:   str										=		model(__lowerCamelCase				,     labels=__lowerCamelCase					)

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





			def 							_snake_case     (self				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     ):
						lowerCamelCase__			:   Optional[int]										=		self.num_labels
						lowerCamelCase__			:   Any										=		FlaubertForTokenClassification(__lowerCamelCase					)
						model.to(__lowerCamelCase					)
						model.eval()

						lowerCamelCase__			:   Any										=		model(__lowerCamelCase				,     attention_mask=__lowerCamelCase				,     labels=__lowerCamelCase					)
						self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels)					)





			def 							_snake_case     (self				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     ):
						lowerCamelCase__			:   List[Any]										=		self.num_choices
						lowerCamelCase__			:   Optional[Any]										=		FlaubertForMultipleChoice(config=__lowerCamelCase					)
						model.to(__lowerCamelCase					)
						model.eval()
						lowerCamelCase__			:   Tuple										=		input_ids.unsqueeze(1					).expand(-1				,     self.num_choices				,     -1					).contiguous()
						lowerCamelCase__			:   Union[str, Any]										=		token_type_ids.unsqueeze(1					).expand(-1				,     self.num_choices				,     -1					).contiguous()
						lowerCamelCase__			:   Tuple										=		input_mask.unsqueeze(1					).expand(-1				,     self.num_choices				,     -1					).contiguous()
						lowerCamelCase__			:   List[str]										=		model(
						    __lowerCamelCase				,     attention_mask=__lowerCamelCase				,     token_type_ids=__lowerCamelCase				,     labels=__lowerCamelCase				,     )
						self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices)					)





			def 							_snake_case     (self					):
						lowerCamelCase__			:   Dict										=		self.prepare_config_and_inputs()
						(
						    (
						    lowerCamelCase__     
						)				,(
						    lowerCamelCase__     
						)				,(
						    lowerCamelCase__     
						)				,(
						    lowerCamelCase__     
						)				,(
						    lowerCamelCase__     
						)				,(
						    lowerCamelCase__     
						)				,(
						    lowerCamelCase__     
						)				,(
						    lowerCamelCase__     
						)				,(
						    lowerCamelCase__     
						)				,     
						)			:   Optional[Any]										=		config_and_inputs
						lowerCamelCase__			:   Tuple										=		{
						    """input_ids""": input_ids,
						    """token_type_ids""": token_type_ids,
						    """lengths""": input_lengths,
						    """attention_mask""": input_mask,
						}
						return config, inputs_dict







@require_torch
class 					__A       (			__SCREAMING_SNAKE_CASE       ,			__SCREAMING_SNAKE_CASE       ,			unittest.TestCase   ):
			UpperCamelCase       :Optional[int]	 =      (
			    (
			        FlaubertModel,
			        FlaubertWithLMHeadModel,
			        FlaubertForQuestionAnswering,
			        FlaubertForQuestionAnsweringSimple,
			        FlaubertForSequenceClassification,
			        FlaubertForTokenClassification,
			        FlaubertForMultipleChoice,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase       :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 							_snake_case     (self				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__				,     __magic_name__					):
						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 							_snake_case     (self				,     __magic_name__				,     __magic_name__				,     __magic_name__=False					):
						lowerCamelCase__			:   List[str]										=		super()._prepare_for_class(__lowerCamelCase				,     __lowerCamelCase				,     return_labels=__lowerCamelCase					)

						if return_labels:
									if model_class.__name__ == "FlaubertForQuestionAnswering":
												lowerCamelCase__			:   Optional[int]										=		torch.zeros(
												    self.model_tester.batch_size				,     dtype=torch.long				,     device=__lowerCamelCase					)
												lowerCamelCase__			:   Optional[int]										=		torch.zeros(
												    self.model_tester.batch_size				,     dtype=torch.long				,     device=__lowerCamelCase					)

						return inputs_dict





			def 							_snake_case     (self					):
						lowerCamelCase__			:   Any										=		FlaubertModelTester(self					)
						lowerCamelCase__			:   List[str]										=		ConfigTester(self				,     config_class=__lowerCamelCase				,     emb_dim=37					)





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





			def 							_snake_case     (self					):
						lowerCamelCase__			:   Dict										=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_model(*__lowerCamelCase					)





			def 							_snake_case     (self					):
						lowerCamelCase__			:   List[Any]										=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_lm_head(*__lowerCamelCase					)





			def 							_snake_case     (self					):
						lowerCamelCase__			:   List[Any]										=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_simple_qa(*__lowerCamelCase					)





			def 							_snake_case     (self					):
						lowerCamelCase__			:   List[Any]										=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_qa(*__lowerCamelCase					)





			def 							_snake_case     (self					):
						lowerCamelCase__			:   Optional[int]										=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_sequence_classif(*__lowerCamelCase					)





			def 							_snake_case     (self					):
						lowerCamelCase__			:   Any										=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_token_classif(*__lowerCamelCase					)





			def 							_snake_case     (self					):
						lowerCamelCase__			:   Any										=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_multiple_choice(*__lowerCamelCase					)





			@slow
			def 							_snake_case     (self					):
						for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCamelCase__			:   List[str]										=		FlaubertModel.from_pretrained(__lowerCamelCase					)
									self.assertIsNotNone(__lowerCamelCase					)





			@slow
			@require_torch_gpu
			def 							_snake_case     (self					):
						lowerCamelCase__				,lowerCamelCase__			:   str										=		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__			:   Any										=		True
									lowerCamelCase__			:   Union[str, Any]										=		model_class(config=__lowerCamelCase					)

									lowerCamelCase__			:   Any										=		self._prepare_for_class(__lowerCamelCase				,     __lowerCamelCase					)
									lowerCamelCase__			:   Optional[int]										=		torch.jit.trace(
									    __lowerCamelCase				,     (inputs_dict["""input_ids"""].to("""cpu"""					), inputs_dict["""attention_mask"""].to("""cpu"""					))					)

									with tempfile.TemporaryDirectory() as tmp:
												torch.jit.save(__lowerCamelCase				,     os.path.join(__lowerCamelCase				,     """traced_model.pt"""					)					)
												lowerCamelCase__			:   List[Any]										=		torch.jit.load(os.path.join(__lowerCamelCase				,     """traced_model.pt"""					)				,     map_location=__lowerCamelCase					)
												loaded(inputs_dict["""input_ids"""].to(__lowerCamelCase					)				,     inputs_dict["""attention_mask"""].to(__lowerCamelCase					)					)







@require_torch
class 					__A       (			unittest.TestCase   ):





			@slow
			def 							_snake_case     (self					):
						lowerCamelCase__			:   int										=		FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased"""					)
						lowerCamelCase__			:   Optional[int]										=		torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]					)
						with torch.no_grad():
									lowerCamelCase__			:   int										=		model(__lowerCamelCase					)[0]
						lowerCamelCase__			:   Any										=		torch.Size((1, 11, 768)					)
						self.assertEqual(output.shape				,     __lowerCamelCase					)
						lowerCamelCase__			:   Dict										=		torch.tensor(
						    [[[-2.62_51, -1.42_98, -0.02_27], [-2.85_10, -1.63_87, 0.22_58], [-2.81_14, -1.18_32, -0.30_66]]]					)

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








"""simple docstring"""






from ...configuration_utils import PretrainedConfig



class     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE  ):
   A__		:	str           =  '''bert-generation'''



   def __init__(       self    :	Tuple							,       __lowerCamelCase    :	Optional[int]=5_0_3_5_8							,       __lowerCamelCase    :	List[str]=1_0_2_4							,       __lowerCamelCase    :	Optional[Any]=2_4							,       __lowerCamelCase    :	Any=1_6							,       __lowerCamelCase    :	Union[str, Any]=4_0_9_6							,       __lowerCamelCase    :	Optional[Any]="gelu"							,       __lowerCamelCase    :	str=0.1							,       __lowerCamelCase    :	List[str]=0.1							,       __lowerCamelCase    :	int=5_1_2							,       __lowerCamelCase    :	Dict=0.0_2							,       __lowerCamelCase    :	Tuple=1E-12							,       __lowerCamelCase    :	Any=0							,       __lowerCamelCase    :	Union[str, Any]=2							,       __lowerCamelCase    :	List[Any]=1							,       __lowerCamelCase    :	Optional[Any]="absolute"							,       __lowerCamelCase    :	str=True							,       **__lowerCamelCase    :	List[Any]							,       ):



     """simple docstring"""
     super().__init__(pad_token_id=__lowerCamelCase							,       bos_token_id=__lowerCamelCase							,       eos_token_id=__lowerCamelCase							,       **__lowerCamelCase				)

     _snake_case   	=      vocab_size
     _snake_case   	=      hidden_size
     _snake_case   	=      num_hidden_layers
     _snake_case   	=      num_attention_heads
     _snake_case   	=      hidden_act
     _snake_case   	=      intermediate_size
     _snake_case   	=      hidden_dropout_prob
     _snake_case   	=      attention_probs_dropout_prob
     _snake_case   	=      max_position_embeddings
     _snake_case   	=      initializer_range
     _snake_case   	=      layer_norm_eps
     _snake_case   	=      position_embedding_type
     _snake_case   	=      use_cache





