


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        snake_case_   (lowerCamelCase_      ):
					def __init__(      self   :Dict			,__snake_case   :int			,__snake_case   :List[Any]=13			,__snake_case   :Any=7			,__snake_case   :Union[str, Any]=True			,__snake_case   :Optional[int]=True			,__snake_case   :str=True			,__snake_case   :Any=True			,__snake_case   :List[Any]=True			,__snake_case   :Optional[int]=False			,__snake_case   :Union[str, Any]=False			,__snake_case   :Optional[int]=False			,__snake_case   :Optional[int]=2			,__snake_case   :Tuple=99			,__snake_case   :Optional[Any]=0			,__snake_case   :Union[str, Any]=32			,__snake_case   :int=5			,__snake_case   :Dict=4			,__snake_case   :Optional[Any]=0.1			,__snake_case   :Any=0.1			,__snake_case   :Dict=5_12			,__snake_case   :List[str]=12			,__snake_case   :Dict=2			,__snake_case   :List[str]=0.02			,__snake_case   :Any=3			,__snake_case   :Tuple=4			,__snake_case   :List[Any]="last"			,__snake_case   :List[Any]=None			,__snake_case   :Dict=None			,)					->    str:
												a__             =     parent
												a__             =     batch_size
												a__             =     seq_length
												a__             =     is_training
												a__             =     use_input_lengths
												a__             =     use_token_type_ids
												a__             =     use_labels
												a__             =     gelu_activation
												a__             =     sinusoidal_embeddings
												a__             =     causal
												a__             =     asm
												a__             =     n_langs
												a__             =     vocab_size
												a__             =     n_special
												a__             =     hidden_size
												a__             =     num_hidden_layers
												a__             =     num_attention_heads
												a__             =     hidden_dropout_prob
												a__             =     attention_probs_dropout_prob
												a__             =     max_position_embeddings
												a__             =     type_vocab_size
												a__             =     type_sequence_label_size
												a__             =     initializer_range
												a__             =     num_labels
												a__             =     num_choices
												a__             =     summary_type
												a__             =     use_proj
												a__             =     scope
					def  lowerCamelCase__(      self   :List[str]      )					->    List[Any]:
												a__             =     ids_tensor([self.batch_size, self.seq_length]			,self.vocab_size      )
												a__             =     random_attention_mask([self.batch_size, self.seq_length]      )

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

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

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

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

												a__             =     model(__snake_case			,token_type_ids=__snake_case			,labels=__snake_case      )
												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]			,__snake_case   :int			,__snake_case   :List[str]			,__snake_case   :str			,__snake_case   :Optional[int]			,__snake_case   :Any			,__snake_case   :Optional[Any]			,__snake_case   :List[Any]			,__snake_case   :Optional[int]			,__snake_case   :List[Any]			,)					->    str:
												a__             =     FlaubertForQuestionAnsweringSimple(__snake_case      )
												model.to(__snake_case      )
												model.eval()

												a__             =     model(__snake_case      )

												a__             =     model(__snake_case			,start_positions=__snake_case			,end_positions=__snake_case      )
												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			,__snake_case   :Tuple			,__snake_case   :List[Any]			,__snake_case   :List[Any]			,__snake_case   :Optional[Any]			,__snake_case   :Optional[int]			,__snake_case   :List[Any]			,__snake_case   :Dict			,__snake_case   :Tuple			,__snake_case   :str			,)					->    Optional[int]:
												a__             =     FlaubertForQuestionAnswering(__snake_case      )
												model.to(__snake_case      )
												model.eval()

												a__             =     model(__snake_case      )

												a__             =     model(
												    __snake_case			,start_positions=__snake_case			,end_positions=__snake_case			,cls_index=__snake_case			,is_impossible=__snake_case			,p_mask=__snake_case			,)

												a__             =     model(
												    __snake_case			,start_positions=__snake_case			,end_positions=__snake_case			,cls_index=__snake_case			,is_impossible=__snake_case			,)

												((a__)      ,					)             =     result_with_labels.to_tuple()

												a__             =     model(__snake_case			,start_positions=__snake_case			,end_positions=__snake_case      )

												((a__)      ,					)             =     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[Any]			,__snake_case   :Optional[int]			,__snake_case   :Dict			,__snake_case   :Optional[Any]			,__snake_case   :List[str]			,__snake_case   :Optional[int]			,__snake_case   :str			,__snake_case   :Any			,__snake_case   :str			,__snake_case   :List[Any]			,)					->    Dict:
												a__             =     FlaubertForSequenceClassification(__snake_case      )
												model.to(__snake_case      )
												model.eval()

												a__             =     model(__snake_case      )
												a__             =     model(__snake_case			,labels=__snake_case      )

												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]			,__snake_case   :int			,__snake_case   :List[str]			,__snake_case   :Union[str, Any]			,__snake_case   :List[str]			,__snake_case   :str			,__snake_case   :Optional[int]			,__snake_case   :List[str]			,__snake_case   :Union[str, Any]			,__snake_case   :Optional[Any]			,)					->    int:
												a__             =     self.num_labels
												a__             =     FlaubertForTokenClassification(__snake_case      )
												model.to(__snake_case      )
												model.eval()

												a__             =     model(__snake_case			,attention_mask=__snake_case			,labels=__snake_case      )
												self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.seq_length, self.num_labels)      )
					def  lowerCamelCase__(      self   :int			,__snake_case   :Tuple			,__snake_case   :Any			,__snake_case   :List[str]			,__snake_case   :Optional[Any]			,__snake_case   :Any			,__snake_case   :Tuple			,__snake_case   :Dict			,__snake_case   :Tuple			,__snake_case   :Union[str, Any]			,)					->    List[str]:
												a__             =     self.num_choices
												a__             =     FlaubertForMultipleChoice(config=__snake_case      )
												model.to(__snake_case      )
												model.eval()
												a__             =     input_ids.unsqueeze(1      ).expand(-1			,self.num_choices			,-1      ).contiguous()
												a__             =     token_type_ids.unsqueeze(1      ).expand(-1			,self.num_choices			,-1      ).contiguous()
												a__             =     input_mask.unsqueeze(1      ).expand(-1			,self.num_choices			,-1      ).contiguous()
												a__             =     model(
												    __snake_case			,attention_mask=__snake_case			,token_type_ids=__snake_case			,labels=__snake_case			,)
												self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.num_choices)      )



					def  lowerCamelCase__(      self   :Tuple      )					->    Optional[Any]:
												a__             =     self.prepare_config_and_inputs()
												(
												    (
												    a__
												)      ,					(
												    a__
												)      ,					(
												    a__
												)      ,					(
												    a__
												)      ,					(
												    a__
												)      ,					(
												    a__
												)      ,					(
												    a__
												)      ,					(
												    a__
												)      ,					(
												    a__
												)      ,					
												)             =     config_and_inputs
												a__             =     {
												    'input_ids': input_ids,
												    'token_type_ids': token_type_ids,
												    'lengths': input_lengths,
												    'attention_mask': input_mask,
												}
												return config, inputs_dict







@require_torch
class        snake_case_   (lowerCamelCase_	,						lowerCamelCase_	,						unittest.TestCase      ):
					UpperCAmelCase__					:    Dict          =			(
					    (
					        FlaubertModel,
					        FlaubertWithLMHeadModel,
					        FlaubertForQuestionAnswering,
					        FlaubertForQuestionAnsweringSimple,
					        FlaubertForSequenceClassification,
					        FlaubertForTokenClassification,
					        FlaubertForMultipleChoice,
					    )
					    if is_torch_available()
					    else ()
					)
					UpperCAmelCase__					:    Dict          =			(
					    {
					        '''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   :List[Any]			,__snake_case   :Optional[int]			,__snake_case   :str			,__snake_case   :Union[str, Any]			,__snake_case   :Dict			,__snake_case   :Union[str, Any]      )					->    Optional[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  lowerCamelCase__(      self   :Tuple			,__snake_case   :Tuple			,__snake_case   :Tuple			,__snake_case   :List[Any]=False      )					->    str:
												a__             =     super()._prepare_for_class(__snake_case			,__snake_case			,return_labels=__snake_case      )

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

												return inputs_dict
					def  lowerCamelCase__(      self   :str      )					->    List[Any]:
												a__             =     FlaubertModelTester(self      )
												a__             =     ConfigTester(self			,config_class=__snake_case			,emb_dim=37      )
					def  lowerCamelCase__(      self   :List[str]      )					->    str:
												self.config_tester.run_common_tests()
					def  lowerCamelCase__(      self   :Optional[Any]      )					->    Any:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_model(*__snake_case      )
					def  lowerCamelCase__(      self   :Optional[int]      )					->    Optional[Any]:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_lm_head(*__snake_case      )
					def  lowerCamelCase__(      self   :Dict      )					->    Union[str, Any]:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_simple_qa(*__snake_case      )
					def  lowerCamelCase__(      self   :str      )					->    str:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_qa(*__snake_case      )
					def  lowerCamelCase__(      self   :List[Any]      )					->    Dict:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_sequence_classif(*__snake_case      )
					def  lowerCamelCase__(      self   :Any      )					->    str:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_token_classif(*__snake_case      )
					def  lowerCamelCase__(      self   :List[str]      )					->    Union[str, Any]:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_multiple_choice(*__snake_case      )
					@slow
					def  lowerCamelCase__(      self   :List[Any]      )					->    Optional[Any]:
												for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			a__             =     FlaubertModel.from_pretrained(__snake_case      )
																			self.assertIsNotNone(__snake_case      )



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

																			a__             =     True
																			a__             =     model_class(config=__snake_case      )

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

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







@require_torch
class        snake_case_   (unittest.TestCase      ):
					@slow
					def  lowerCamelCase__(      self   :Optional[Any]      )					->    int:
												a__             =     FlaubertModel.from_pretrained('flaubert/flaubert_base_cased'      )
												a__             =     torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]      )
												with torch.no_grad():
																			a__             =     model(__snake_case      )[0]
												a__             =     torch.Size((1, 11, 7_68)      )
												self.assertEqual(output.shape			,__snake_case      )
												a__             =     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]			,__snake_case			,atol=1E-4      )      )








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


snake_case							:       str     			=      {
    '''configuration_rag''': ['''RagConfig'''],
    '''retrieval_rag''': ['''RagRetriever'''],
    '''tokenization_rag''': ['''RagTokenizer'''],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						snake_case							:       Any     			=      [
						    '''RagModel''',
						    '''RagPreTrainedModel''',
						    '''RagSequenceForGeneration''',
						    '''RagTokenForGeneration''',
						]

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						snake_case							:       Optional[int]     			=      [
						    '''TFRagModel''',
						    '''TFRagPreTrainedModel''',
						    '''TFRagSequenceForGeneration''',
						    '''TFRagTokenForGeneration''',
						]


if TYPE_CHECKING:
						from .configuration_rag import RagConfig
						from .retrieval_rag import RagRetriever
						from .tokenization_rag import RagTokenizer

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_rag import (
												    TFRagModel,
												    TFRagPreTrainedModel,
												    TFRagSequenceForGeneration,
												    TFRagTokenForGeneration,
												)

else:
						import sys

						snake_case							:       int     			=      _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




