






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     (						a    ):


				def __init__(		self   , UpperCAmelCase_   , UpperCAmelCase_=13   , UpperCAmelCase_=7   , UpperCAmelCase_=True   , UpperCAmelCase_=True   , UpperCAmelCase_=True   , UpperCAmelCase_=True   , UpperCAmelCase_=True   , UpperCAmelCase_=False   , UpperCAmelCase_=False   , UpperCAmelCase_=False   , UpperCAmelCase_=2   , UpperCAmelCase_=99   , UpperCAmelCase_=0   , UpperCAmelCase_=32   , UpperCAmelCase_=5   , UpperCAmelCase_=4   , UpperCAmelCase_=0.1   , UpperCAmelCase_=0.1   , UpperCAmelCase_=512   , UpperCAmelCase_=12   , UpperCAmelCase_=2   , UpperCAmelCase_=0.0_2   , UpperCAmelCase_=3   , UpperCAmelCase_=4   , UpperCAmelCase_="last"   , UpperCAmelCase_=None   , UpperCAmelCase_=None   , ):
								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 _snake_case       (		self							):
								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 _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   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , ):
								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 _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , ):
								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 _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , ):
								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 _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , ):
								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 _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , ):
								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 _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , ):
								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 _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , ):
								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 _snake_case       (		self							):
								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       __A     (						a    ,					a    ,					unittest.TestCase    ):
				__A      =				(
				    (
				        FlaubertModel,
				        FlaubertWithLMHeadModel,
				        FlaubertForQuestionAnswering,
				        FlaubertForQuestionAnsweringSimple,
				        FlaubertForSequenceClassification,
				        FlaubertForTokenClassification,
				        FlaubertForMultipleChoice,
				    )
				    if is_torch_available()
				    else ()
				)
				__A      =				(
				    {
				        """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   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_							):
								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   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_=False							):
								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 _snake_case       (		self							):
								lowerCamelCase	 =FlaubertModelTester(self							)
								lowerCamelCase	 =ConfigTester(self   , config_class=UpperCAmelCase_   , emb_dim=37							)


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


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_model(*UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_lm_head(*UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_simple_qa(*UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_qa(*UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_token_classif(*UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_multiple_choice(*UpperCAmelCase_							)


				@slow
				def _snake_case       (		self							):
								for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowerCamelCase	 =FlaubertModel.from_pretrained(UpperCAmelCase_							)
												self.assertIsNotNone(UpperCAmelCase_							)


				@slow
				@require_torch_gpu
				def _snake_case       (		self							):
								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       __A     (						unittest.TestCase    ):


				@slow
				def _snake_case       (		self							):
								lowerCamelCase	 =FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased"""							)
								lowerCamelCase	 =torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]							)
								with torch.no_grad():
												lowerCamelCase	 =model(UpperCAmelCase_							)[0]
								lowerCamelCase	 =torch.Size((1, 11, 768)							)
								self.assertEqual(output.shape   , UpperCAmelCase_							)
								lowerCamelCase	 =torch.tensor(
								    [[[-2.6_2_5_1, -1.4_2_9_8, -0.0_2_2_7], [-2.8_5_1_0, -1.6_3_8_7, 0.2_2_5_8], [-2.8_1_1_4, -1.1_8_3_2, -0.3_0_6_6]]]							)

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












from typing import List

from .keymap import KEYMAP, get_character





def      _lowercase					(						_UpperCAmelCase				)						->  Tuple:

				def decorator(_UpperCAmelCase				):
								lowerCamelCase	 =getattr(_UpperCAmelCase			,      """handle_key"""			,      []				)
								handle += [key]
								setattr(_UpperCAmelCase			,      """handle_key"""			,      _UpperCAmelCase				)
								return func

				return decorator





def      _lowercase					(						*_UpperCAmelCase				)						->  Tuple:

				def decorator(_UpperCAmelCase				):
								lowerCamelCase	 =getattr(_UpperCAmelCase			,      """handle_key"""			,      []				)
								handle += keys
								setattr(_UpperCAmelCase			,      """handle_key"""			,      _UpperCAmelCase				)
								return func

				return decorator

class       __A     (						a    ):


				def __new__(		cls   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_							):
								lowerCamelCase	 =super().__new__(cls   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_							)
								if not hasattr(UpperCAmelCase_   , """key_handler"""							):
												setattr(UpperCAmelCase_   , """key_handler"""   , {}							)
								setattr(UpperCAmelCase_   , """handle_input"""   , KeyHandler.handle_input							)

								for value in attrs.values():
												lowerCamelCase	 =getattr(UpperCAmelCase_   , """handle_key"""   , []							)
												for key in handled_keys:
																lowerCamelCase	 =value
								return new_cls


				@staticmethod
				def _snake_case       (		cls							):
								lowerCamelCase	 =get_character()
								if char != KEYMAP["undefined"]:
												lowerCamelCase	 =ord(UpperCAmelCase_							)
								lowerCamelCase	 =cls.key_handler.get(UpperCAmelCase_							)
								if handler:
												lowerCamelCase	 =char
												return handler(cls							)
								else:
												return None





def      _lowercase					(						cls				)						->  List[str]:
				return KeyHandler(cls.__name__			,      cls.__bases__			,      cls.__dict__.copy()				)





