



import logging
import os
from dataclasses import dataclass, field
from typing import Dict, Optional

import numpy as np
from utils_multiple_choice import MultipleChoiceDataset, Split, processors

import transformers
from transformers import (
    AutoConfig,
    AutoModelForMultipleChoice,
    AutoTokenizer,
    DataCollatorWithPadding,
    EvalPrediction,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.trainer_utils import is_main_process


_lowercase						: Optional[int]     						=logging.getLogger(__name__)

def       lowerCAmelCase_	(    _lowercase							:  Optional[int]		,  _lowercase							:  Union[str, Any])				->			str:







			"""simple docstring"""
			return (preds == labels).mean()

@dataclass
class   snake_case__							:







						"""simple docstring"""




						__lowerCAmelCase	:Any         =	field(
						    metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}						)
						__lowerCAmelCase	:Any         =	field(
						    default=__lowercase   ,      metadata={"help": "Pretrained config name or path if not the same as model_name"}						)
						__lowerCAmelCase	:int         =	field(
						    default=__lowercase   ,      metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}						)
						__lowerCAmelCase	:Any         =	field(
						    default=__lowercase   ,      metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}   ,      )




@dataclass
class   snake_case__							:







						"""simple docstring"""




						__lowerCAmelCase	:str         =	field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys()						)}						)
						__lowerCAmelCase	:str         =	field(metadata={"help": "Should contain the data files for the task."}						)
						__lowerCAmelCase	:List[Any]         =	field(
						    default=128   ,      metadata={
						        "help": (
						            "The maximum total input sequence length after tokenization. Sequences longer "
						            "than this will be truncated, sequences shorter will be padded."
						        )
						    }   ,      )
						__lowerCAmelCase	:List[Any]         =	field(
						    default=__lowercase   ,      metadata={"help": "Overwrite the cached training and evaluation sets"}						)




def       lowerCAmelCase_	(    )				->			Optional[int]:







			"""simple docstring"""
			# See all possible arguments in src/transformers/training_args.py
			# or by passing the --help flag to this script.
			# We now keep distinct sets of args, for a cleaner separation of concerns.

			a__  :		Any					=    HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments))
			a__			,		a__			,		a__  :		Optional[int]					=    parser.parse_args_into_dataclasses()

			if (
			    os.path.exists(training_args.output_dir)
			    and os.listdir(training_args.output_dir)
			    and training_args.do_train
			    and not training_args.overwrite_output_dir
			):
						raise ValueError(
						    F'''Output directory ({training_args.output_dir}) already exists and is not empty. Use'''
						    """ --overwrite_output_dir to overcome.""")

			# Setup logging
			logging.basicConfig(
			    format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s"""		,  datefmt="""%m/%d/%Y %H:%M:%S"""		,  level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN		,  )
			logger.warning(
			    """Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s"""		,  training_args.local_rank		,  training_args.device		,  training_args.n_gpu		,  bool(training_args.local_rank != -1)		,  training_args.fpaa		,  )
			# Set the verbosity to info of the Transformers logger (on main process only):
			if is_main_process(training_args.local_rank):
						transformers.utils.logging.set_verbosity_info()
						transformers.utils.logging.enable_default_handler()
						transformers.utils.logging.enable_explicit_format()
			logger.info("""Training/evaluation parameters %s"""		,  __snake_case)

			# Set seed
			set_seed(training_args.seed)

			try:
						a__  :		Any					=    processors[data_args.task_name]()
						a__  :		Dict					=    processor.get_labels()
						a__  :		Union[str, Any]					=    len(__snake_case)
			except KeyError:
						raise ValueError("""Task not found: %s""" % (data_args.task_name))

			# Load pretrained model and tokenizer
			#
			# Distributed training:
			# The .from_pretrained methods guarantee that only one local process can concurrently
			# download model & vocab.

			a__  :		Any					=    AutoConfig.from_pretrained(
			    model_args.config_name if model_args.config_name else model_args.model_name_or_path		,  num_labels=__snake_case		,  finetuning_task=data_args.task_name		,  cache_dir=model_args.cache_dir		,  )
			a__  :		Optional[Any]					=    AutoTokenizer.from_pretrained(
			    model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path		,  cache_dir=model_args.cache_dir		,  )
			a__  :		Union[str, Any]					=    AutoModelForMultipleChoice.from_pretrained(
			    model_args.model_name_or_path		,  from_tf=bool(""".ckpt""" in model_args.model_name_or_path)		,  config=__snake_case		,  cache_dir=model_args.cache_dir		,  )

			# Get datasets
			a__  :		int					=    (
			    MultipleChoiceDataset(
			        data_dir=data_args.data_dir		,  tokenizer=__snake_case		,  task=data_args.task_name		,  max_seq_length=data_args.max_seq_length		,  overwrite_cache=data_args.overwrite_cache		,  mode=Split.train		,  )
			    if training_args.do_train
			    else None
			)
			a__  :		int					=    (
			    MultipleChoiceDataset(
			        data_dir=data_args.data_dir		,  tokenizer=__snake_case		,  task=data_args.task_name		,  max_seq_length=data_args.max_seq_length		,  overwrite_cache=data_args.overwrite_cache		,  mode=Split.dev		,  )
			    if training_args.do_eval
			    else None
			)

			def compute_metrics(_lowercase							:  List[Any]) -> Dict:
						a__  :		Tuple					=    np.argmax(p.predictions		,  axis=1)
						return {"acc": simple_accuracy(__snake_case		,  p.label_ids)}

			# Data collator
			a__  :		str					=    DataCollatorWithPadding(__snake_case		,  pad_to_multiple_of=8) if training_args.fpaa else None

			# Initialize our Trainer
			a__  :		Optional[int]					=    Trainer(
			    model=__snake_case		,  args=__snake_case		,  train_dataset=__snake_case		,  eval_dataset=__snake_case		,  compute_metrics=__snake_case		,  data_collator=__snake_case		,  )

			# Training
			if training_args.do_train:
						trainer.train(
						    model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path) else None)
						trainer.save_model()
						# For convenience, we also re-save the tokenizer to the same directory,
						# so that you can share your model easily on huggingface.co/models =)
						if trainer.is_world_master():
									tokenizer.save_pretrained(training_args.output_dir)

    # Evaluation
			a__  :		Union[str, Any]					=    {}
			if training_args.do_eval:
						logger.info("""*** Evaluate ***""")

						a__  :		str					=    trainer.evaluate()

						a__  :		List[Any]					=    os.path.join(training_args.output_dir		,  """eval_results.txt""")
						if trainer.is_world_master():
									with open(__snake_case		,  """w""") as writer:
												logger.info("""***** Eval results *****""")
												for key, value in result.items():
															logger.info("""  %s = %s"""		,  __snake_case		,  __snake_case)
															writer.write("""%s = %s\n""" % (key, value))

												results.update(__snake_case)

			return results

def       lowerCAmelCase_	(    _lowercase							:  Optional[int])				->			Any:







			"""simple docstring"""
			# For xla_spawn (TPUs)
			main()


if __name__ == "__main__":
		main()





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     lowerCAmelCase_     (       __lowercase ):







							def __init__(   self      :     Union[str, Any] ,			_A      :     Optional[Any] ,			_A      :     Any=13 ,			_A      :     Union[str, Any]=7 ,			_A      :     List[str]=True ,			_A      :     List[str]=True ,			_A      :     List[str]=True ,			_A      :     List[str]=True ,			_A      :     List[Any]=True ,			_A      :     Optional[int]=False ,			_A      :     Any=False ,			_A      :     int=False ,			_A      :     Optional[Any]=2 ,			_A      :     Any=99 ,			_A      :     str=0 ,			_A      :     Union[str, Any]=32 ,			_A      :     List[Any]=5 ,			_A      :     Tuple=4 ,			_A      :     List[str]=0.1 ,			_A      :     Union[str, Any]=0.1 ,			_A      :     int=512 ,			_A      :     Union[str, Any]=12 ,			_A      :     List[str]=2 ,			_A      :     int=0.02 ,			_A      :     Optional[Any]=3 ,			_A      :     Any=4 ,			_A      :     Optional[int]="last" ,			_A      :     Any=None ,			_A      :     Dict=None ,			):
												_UpperCamelCase			     =     parent
												_UpperCamelCase			     =     batch_size
												_UpperCamelCase			     =     seq_length
												_UpperCamelCase			     =     is_training
												_UpperCamelCase			     =     use_input_lengths
												_UpperCamelCase			     =     use_token_type_ids
												_UpperCamelCase			     =     use_labels
												_UpperCamelCase			     =     gelu_activation
												_UpperCamelCase			     =     sinusoidal_embeddings
												_UpperCamelCase			     =     causal
												_UpperCamelCase			     =     asm
												_UpperCamelCase			     =     n_langs
												_UpperCamelCase			     =     vocab_size
												_UpperCamelCase			     =     n_special
												_UpperCamelCase			     =     hidden_size
												_UpperCamelCase			     =     num_hidden_layers
												_UpperCamelCase			     =     num_attention_heads
												_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			     =     summary_type
												_UpperCamelCase			     =     use_proj
												_UpperCamelCase			     =     scope







							def  UpperCamelCase_						(   self      :     List[str]		):
												_UpperCamelCase			     =     ids_tensor([self.batch_size, self.seq_length] ,			self.vocab_size		)
												_UpperCamelCase			     =     random_attention_mask([self.batch_size, self.seq_length]		)

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

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

												_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] ,			2		).float()
																	_UpperCamelCase			     =     ids_tensor([self.batch_size] ,			self.num_choices		)

												_UpperCamelCase			     =     self.get_config()

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







							def  UpperCamelCase_						(   self      :     str		):
												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  UpperCamelCase_						(   self      :     str ,			_A      :     Union[str, Any] ,			_A      :     Optional[Any] ,			_A      :     str ,			_A      :     Tuple ,			_A      :     List[str] ,			_A      :     List[Any] ,			_A      :     Any ,			_A      :     str ,			_A      :     Optional[int] ,			):
												_UpperCamelCase			     =     FlaubertModel(config=_A		)
												model.to(_A		)
												model.eval()
												_UpperCamelCase			     =     model(_A ,			lengths=_A ,			langs=_A		)
												_UpperCamelCase			     =     model(_A ,			langs=_A		)
												_UpperCamelCase			     =     model(_A		)
												self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, self.seq_length, self.hidden_size)		)







							def  UpperCamelCase_						(   self      :     Tuple ,			_A      :     List[Any] ,			_A      :     str ,			_A      :     Optional[int] ,			_A      :     Optional[Any] ,			_A      :     List[str] ,			_A      :     int ,			_A      :     str ,			_A      :     List[Any] ,			_A      :     Any ,			):
												_UpperCamelCase			     =     FlaubertWithLMHeadModel(_A		)
												model.to(_A		)
												model.eval()

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







							def  UpperCamelCase_						(   self      :     Tuple ,			_A      :     List[str] ,			_A      :     List[str] ,			_A      :     Optional[Any] ,			_A      :     Union[str, Any] ,			_A      :     str ,			_A      :     List[str] ,			_A      :     Tuple ,			_A      :     Optional[int] ,			_A      :     Dict ,			):
												_UpperCamelCase			     =     FlaubertForQuestionAnsweringSimple(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A		)

												_UpperCamelCase			     =     model(_A ,			start_positions=_A ,			end_positions=_A		)
												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  UpperCamelCase_						(   self      :     Tuple ,			_A      :     str ,			_A      :     Tuple ,			_A      :     Tuple ,			_A      :     Union[str, Any] ,			_A      :     List[str] ,			_A      :     int ,			_A      :     str ,			_A      :     Dict ,			_A      :     List[Any] ,			):
												_UpperCamelCase			     =     FlaubertForQuestionAnswering(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A		)

												_UpperCamelCase			     =     model(
												    _A ,			start_positions=_A ,			end_positions=_A ,			cls_index=_A ,			is_impossible=_A ,			p_mask=_A ,			)

												_UpperCamelCase			     =     model(
												    _A ,			start_positions=_A ,			end_positions=_A ,			cls_index=_A ,			is_impossible=_A ,			)

												((_UpperCamelCase)     ,    )			     =     result_with_labels.to_tuple()

												_UpperCamelCase			     =     model(_A ,			start_positions=_A ,			end_positions=_A		)

												((_UpperCamelCase)     ,    )			     =     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  UpperCamelCase_						(   self      :     List[Any] ,			_A      :     Union[str, Any] ,			_A      :     Tuple ,			_A      :     str ,			_A      :     int ,			_A      :     int ,			_A      :     Optional[int] ,			_A      :     Optional[int] ,			_A      :     int ,			_A      :     List[str] ,			):
												_UpperCamelCase			     =     FlaubertForSequenceClassification(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A		)
												_UpperCamelCase			     =     model(_A ,			labels=_A		)

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







							def  UpperCamelCase_						(   self      :     Optional[int] ,			_A      :     List[str] ,			_A      :     Optional[Any] ,			_A      :     str ,			_A      :     Union[str, Any] ,			_A      :     List[Any] ,			_A      :     int ,			_A      :     List[Any] ,			_A      :     str ,			_A      :     List[str] ,			):
												_UpperCamelCase			     =     self.num_labels
												_UpperCamelCase			     =     FlaubertForTokenClassification(_A		)
												model.to(_A		)
												model.eval()

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







							def  UpperCamelCase_						(   self      :     Tuple ,			_A      :     Dict ,			_A      :     str ,			_A      :     Optional[Any] ,			_A      :     List[str] ,			_A      :     Any ,			_A      :     Optional[int] ,			_A      :     Optional[Any] ,			_A      :     List[Any] ,			_A      :     List[str] ,			):
												_UpperCamelCase			     =     self.num_choices
												_UpperCamelCase			     =     FlaubertForMultipleChoice(config=_A		)
												model.to(_A		)
												model.eval()
												_UpperCamelCase			     =     input_ids.unsqueeze(1		).expand(-1 ,			self.num_choices ,			-1		).contiguous()
												_UpperCamelCase			     =     token_type_ids.unsqueeze(1		).expand(-1 ,			self.num_choices ,			-1		).contiguous()
												_UpperCamelCase			     =     input_mask.unsqueeze(1		).expand(-1 ,			self.num_choices ,			-1		).contiguous()
												_UpperCamelCase			     =     model(
												    _A ,			attention_mask=_A ,			token_type_ids=_A ,			labels=_A ,			)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_choices)		)







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




@require_torch
class     lowerCAmelCase_     (       __lowercase,  __lowercase,  unittest.TestCase ):
							UpperCAmelCase				   =     (
							    (
							        FlaubertModel,
							        FlaubertWithLMHeadModel,
							        FlaubertForQuestionAnswering,
							        FlaubertForQuestionAnsweringSimple,
							        FlaubertForSequenceClassification,
							        FlaubertForTokenClassification,
							        FlaubertForMultipleChoice,
							    )
							    if is_torch_available()
							    else ()
							)
							UpperCAmelCase				   =     (
							    {
							        "feature-extraction": FlaubertModel,
							        "fill-mask": FlaubertWithLMHeadModel,
							        "question-answering": FlaubertForQuestionAnsweringSimple,
							        "text-classification": FlaubertForSequenceClassification,
							        "token-classification": FlaubertForTokenClassification,
							        "zero-shot": FlaubertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)







							def  UpperCamelCase_						(   self      :     Union[str, Any] ,			_A      :     Dict ,			_A      :     Dict ,			_A      :     Tuple ,			_A      :     int ,			_A      :     Any		):
												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  UpperCamelCase_						(   self      :     str ,			_A      :     Any ,			_A      :     List[str] ,			_A      :     Optional[int]=False		):
												_UpperCamelCase			     =     super()._prepare_for_class(_A ,			_A ,			return_labels=_A		)

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

												return inputs_dict







							def  UpperCamelCase_						(   self      :     str		):
												_UpperCamelCase			     =     FlaubertModelTester(self		)
												_UpperCamelCase			     =     ConfigTester(self ,			config_class=_A ,			emb_dim=37		)







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												self.config_tester.run_common_tests()







							def  UpperCamelCase_						(   self      :     str		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_model(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_lm_head(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_simple_qa(*_A		)







							def  UpperCamelCase_						(   self      :     Union[str, Any]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_qa(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_sequence_classif(*_A		)







							def  UpperCamelCase_						(   self      :     Any		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_token_classif(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_multiple_choice(*_A		)







							@slow
							def  UpperCamelCase_						(   self      :     str		):
												for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_UpperCamelCase			     =     FlaubertModel.from_pretrained(_A		)
																	self.assertIsNotNone(_A		)







							@slow
							@require_torch_gpu
							def  UpperCamelCase_						(   self      :     List[Any]		):
												_UpperCamelCase     ,    _UpperCamelCase			     =     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

																	_UpperCamelCase			     =     True
																	_UpperCamelCase			     =     model_class(config=_A		)

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

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




@require_torch
class     lowerCAmelCase_     (       unittest.TestCase ):







							@slow
							def  UpperCamelCase_						(   self      :     int		):
												_UpperCamelCase			     =     FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''		)
												_UpperCamelCase			     =     torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]		)
												with torch.no_grad():
																	_UpperCamelCase			     =     model(_A		)[0]
												_UpperCamelCase			     =     torch.Size((1, 11, 768)		)
												self.assertEqual(output.shape ,			_A		)
												_UpperCamelCase			     =     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] ,			_A ,			atol=1e-4		)		)
