
"""simple docstring"""




import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    FlaubertForMultipleChoice,
					    FlaubertForQuestionAnswering,
					    FlaubertForQuestionAnsweringSimple,
					    FlaubertForSequenceClassification,
					    FlaubertForTokenClassification,
					    FlaubertModel,
					    FlaubertWithLMHeadModel,
					)
					from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST



class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				def __init__(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__=13     , SCREAMING_SNAKE_CASE__=7     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=99     , SCREAMING_SNAKE_CASE__=0     , SCREAMING_SNAKE_CASE__=32     , SCREAMING_SNAKE_CASE__=5     , SCREAMING_SNAKE_CASE__=4     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=5_12     , SCREAMING_SNAKE_CASE__=12     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.02     , SCREAMING_SNAKE_CASE__=3     , SCREAMING_SNAKE_CASE__=4     , SCREAMING_SNAKE_CASE__="last"     , SCREAMING_SNAKE_CASE__=None     , SCREAMING_SNAKE_CASE__=None     , )      ->	Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: str     =     parent
											SCREAMING_SNAKE_CASE__		: str     =     batch_size
											SCREAMING_SNAKE_CASE__		: Tuple     =     seq_length
											SCREAMING_SNAKE_CASE__		: List[str]     =     is_training
											SCREAMING_SNAKE_CASE__		: Any     =     use_input_lengths
											SCREAMING_SNAKE_CASE__		: int     =     use_token_type_ids
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     use_labels
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     gelu_activation
											SCREAMING_SNAKE_CASE__		: int     =     sinusoidal_embeddings
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     causal
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     asm
											SCREAMING_SNAKE_CASE__		: Any     =     n_langs
											SCREAMING_SNAKE_CASE__		: Dict     =     vocab_size
											SCREAMING_SNAKE_CASE__		: List[Any]     =     n_special
											SCREAMING_SNAKE_CASE__		: List[Any]     =     hidden_size
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     num_hidden_layers
											SCREAMING_SNAKE_CASE__		: Any     =     num_attention_heads
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     hidden_dropout_prob
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     attention_probs_dropout_prob
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     max_position_embeddings
											SCREAMING_SNAKE_CASE__		: int     =     type_vocab_size
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     type_sequence_label_size
											SCREAMING_SNAKE_CASE__		: Tuple     =     initializer_range
											SCREAMING_SNAKE_CASE__		: List[str]     =     num_labels
											SCREAMING_SNAKE_CASE__		: Tuple     =     num_choices
											SCREAMING_SNAKE_CASE__		: List[Any]     =     summary_type
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     use_proj
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     scope



				def 			__magic_name__					(self					)      ->	Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: str     =     ids_tensor([self.batch_size, self.seq_length]     , self.vocab_size					)
											SCREAMING_SNAKE_CASE__		: Dict     =     random_attention_mask([self.batch_size, self.seq_length]					)

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

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

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

											SCREAMING_SNAKE_CASE__		: 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 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , )      ->	Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     FlaubertModel(config=SCREAMING_SNAKE_CASE__					)
											model.to(SCREAMING_SNAKE_CASE__					)
											model.eval()
											SCREAMING_SNAKE_CASE__		: List[str]     =     model(SCREAMING_SNAKE_CASE__     , lengths=SCREAMING_SNAKE_CASE__     , langs=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     model(SCREAMING_SNAKE_CASE__     , langs=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     model(SCREAMING_SNAKE_CASE__					)
											self.parent.assertEqual(result.last_hidden_state.shape     , (self.batch_size, self.seq_length, self.hidden_size)					)



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , )      ->	Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[int]     =     FlaubertWithLMHeadModel(SCREAMING_SNAKE_CASE__					)
											model.to(SCREAMING_SNAKE_CASE__					)
											model.eval()

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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , )      ->	List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     FlaubertForQuestionAnsweringSimple(SCREAMING_SNAKE_CASE__					)
											model.to(SCREAMING_SNAKE_CASE__					)
											model.eval()

											SCREAMING_SNAKE_CASE__		: Dict     =     model(SCREAMING_SNAKE_CASE__					)

											SCREAMING_SNAKE_CASE__		: List[str]     =     model(SCREAMING_SNAKE_CASE__     , start_positions=SCREAMING_SNAKE_CASE__     , end_positions=SCREAMING_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 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , )      ->	Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Dict     =     FlaubertForQuestionAnswering(SCREAMING_SNAKE_CASE__					)
											model.to(SCREAMING_SNAKE_CASE__					)
											model.eval()

											SCREAMING_SNAKE_CASE__		: Dict     =     model(SCREAMING_SNAKE_CASE__					)

											SCREAMING_SNAKE_CASE__		: str     =     model(
											    SCREAMING_SNAKE_CASE__     , start_positions=SCREAMING_SNAKE_CASE__     , end_positions=SCREAMING_SNAKE_CASE__     , cls_index=SCREAMING_SNAKE_CASE__     , is_impossible=SCREAMING_SNAKE_CASE__     , p_mask=SCREAMING_SNAKE_CASE__     , )

											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     model(
											    SCREAMING_SNAKE_CASE__     , start_positions=SCREAMING_SNAKE_CASE__     , end_positions=SCREAMING_SNAKE_CASE__     , cls_index=SCREAMING_SNAKE_CASE__     , is_impossible=SCREAMING_SNAKE_CASE__     , )

											((SCREAMING_SNAKE_CASE__)   ,   )		: Optional[Any]     =     result_with_labels.to_tuple()

											SCREAMING_SNAKE_CASE__		: List[Any]     =     model(SCREAMING_SNAKE_CASE__     , start_positions=SCREAMING_SNAKE_CASE__     , end_positions=SCREAMING_SNAKE_CASE__					)

											((SCREAMING_SNAKE_CASE__)   ,   )		: 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 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , )      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     FlaubertForSequenceClassification(SCREAMING_SNAKE_CASE__					)
											model.to(SCREAMING_SNAKE_CASE__					)
											model.eval()

											SCREAMING_SNAKE_CASE__		: Any     =     model(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     model(SCREAMING_SNAKE_CASE__     , labels=SCREAMING_SNAKE_CASE__					)

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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , )      ->	Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     self.num_labels
											SCREAMING_SNAKE_CASE__		: List[str]     =     FlaubertForTokenClassification(SCREAMING_SNAKE_CASE__					)
											model.to(SCREAMING_SNAKE_CASE__					)
											model.eval()

											SCREAMING_SNAKE_CASE__		: int     =     model(SCREAMING_SNAKE_CASE__     , attention_mask=SCREAMING_SNAKE_CASE__     , labels=SCREAMING_SNAKE_CASE__					)
											self.parent.assertEqual(result.logits.shape     , (self.batch_size, self.seq_length, self.num_labels)					)



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , )      ->	Any:



											"""simple docstring"""




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



				def 			__magic_name__					(self					)      ->	Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     self.prepare_config_and_inputs()
											(
											    (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   
											)		: int     =     config_and_inputs
											SCREAMING_SNAKE_CASE__		: Dict     =     {
											    """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_		(a__				,      a__				,      unittest.TestCase						):


				"""simple docstring"""



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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)      ->	Optional[Any]:



											"""simple docstring"""




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

											return False



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__=False					)      ->	Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Tuple     =     super()._prepare_for_class(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , return_labels=SCREAMING_SNAKE_CASE__					)

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

											return inputs_dict



				def 			__magic_name__					(self					)      ->	str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     FlaubertModelTester(self					)
											SCREAMING_SNAKE_CASE__		: str     =     ConfigTester(self     , config_class=SCREAMING_SNAKE_CASE__     , emb_dim=37					)



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



											"""simple docstring"""




											self.config_tester.run_common_tests()



				def 			__magic_name__					(self					)      ->	List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_model(*SCREAMING_SNAKE_CASE__					)



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_lm_head(*SCREAMING_SNAKE_CASE__					)



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: int     =     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_simple_qa(*SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Tuple     =     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_qa(*SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_sequence_classif(*SCREAMING_SNAKE_CASE__					)



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_token_classif(*SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: str     =     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_multiple_choice(*SCREAMING_SNAKE_CASE__					)



				@slow
				def 			__magic_name__					(self					)      ->	Optional[Any]:



											"""simple docstring"""




											for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE__		: Optional[Any]     =     FlaubertModel.from_pretrained(SCREAMING_SNAKE_CASE__					)
																		self.assertIsNotNone(SCREAMING_SNAKE_CASE__					)



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__		: List[Any]     =     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

																		SCREAMING_SNAKE_CASE__		: List[Any]     =     True
																		SCREAMING_SNAKE_CASE__		: List[str]     =     model_class(config=SCREAMING_SNAKE_CASE__					)

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

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






@require_torch
class 			lowerCAmelCase_		(unittest.TestCase						):


				"""simple docstring"""



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: int     =     FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased"""					)
											SCREAMING_SNAKE_CASE__		: int     =     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():
																		SCREAMING_SNAKE_CASE__		: Tuple     =     model(SCREAMING_SNAKE_CASE__					)[0]
											SCREAMING_SNAKE_CASE__		: int     =     torch.Size((1, 11, 7_68)					)
											self.assertEqual(output.shape     , SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     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]     , SCREAMING_SNAKE_CASE__     , atol=1E-4					)					)






"""simple docstring"""




import copy
from collections import OrderedDict
from typing import Dict, Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


UpperCAmelCase__   :					Optional[Any]												=				logging.get_logger(__name__)

UpperCAmelCase__   :					str												=				{
    'facebook/detr-resnet-50': 'https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json',
    # See all DETR models at https://huggingface.co/models?filter=detr
}



class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		List[Any]       =     '''detr'''
				__UpperCamelCase   :		List[Any]       =     ['''past_key_values''']
				__UpperCamelCase   :		List[Any]       =     {
				    '''hidden_size''': '''d_model''',
				    '''num_attention_heads''': '''encoder_attention_heads''',
				}



				def __init__(self     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=None     , SCREAMING_SNAKE_CASE__=3     , SCREAMING_SNAKE_CASE__=1_00     , SCREAMING_SNAKE_CASE__=6     , SCREAMING_SNAKE_CASE__=20_48     , SCREAMING_SNAKE_CASE__=8     , SCREAMING_SNAKE_CASE__=6     , SCREAMING_SNAKE_CASE__=20_48     , SCREAMING_SNAKE_CASE__=8     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__="relu"     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.02     , SCREAMING_SNAKE_CASE__=1.0     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__="sine"     , SCREAMING_SNAKE_CASE__="resnet50"     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=5     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=5     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.1     , **SCREAMING_SNAKE_CASE__     , )      ->	Optional[Any]:



											"""simple docstring"""




											if backbone_config is not None and use_timm_backbone:
																		raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""					)

											if not use_timm_backbone:
																		if backbone_config is None:
																									logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""					)
																									SCREAMING_SNAKE_CASE__		: Any     =     CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]					)
																		elif isinstance(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					):
																									SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     backbone_config.get("""model_type"""					)
																									SCREAMING_SNAKE_CASE__		: int     =     CONFIG_MAPPING[backbone_model_type]
																									SCREAMING_SNAKE_CASE__		: str     =     config_class.from_dict(SCREAMING_SNAKE_CASE__					)
																		# set timm attributes to None
																		SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__		: List[str]     =     None, None, None

											SCREAMING_SNAKE_CASE__		: Optional[int]     =     use_timm_backbone
											SCREAMING_SNAKE_CASE__		: Tuple     =     backbone_config
											SCREAMING_SNAKE_CASE__		: List[Any]     =     num_channels
											SCREAMING_SNAKE_CASE__		: Tuple     =     num_queries
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     d_model
											SCREAMING_SNAKE_CASE__		: str     =     encoder_ffn_dim
											SCREAMING_SNAKE_CASE__		: str     =     encoder_layers
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     encoder_attention_heads
											SCREAMING_SNAKE_CASE__		: Any     =     decoder_ffn_dim
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     decoder_layers
											SCREAMING_SNAKE_CASE__		: Dict     =     decoder_attention_heads
											SCREAMING_SNAKE_CASE__		: List[str]     =     dropout
											SCREAMING_SNAKE_CASE__		: Tuple     =     attention_dropout
											SCREAMING_SNAKE_CASE__		: Tuple     =     activation_dropout
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     activation_function
											SCREAMING_SNAKE_CASE__		: Any     =     init_std
											SCREAMING_SNAKE_CASE__		: Dict     =     init_xavier_std
											SCREAMING_SNAKE_CASE__		: Any     =     encoder_layerdrop
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     decoder_layerdrop
											SCREAMING_SNAKE_CASE__		: Dict     =     encoder_layers
											SCREAMING_SNAKE_CASE__		: List[str]     =     auxiliary_loss
											SCREAMING_SNAKE_CASE__		: List[Any]     =     position_embedding_type
											SCREAMING_SNAKE_CASE__		: List[str]     =     backbone
											SCREAMING_SNAKE_CASE__		: Dict     =     use_pretrained_backbone
											SCREAMING_SNAKE_CASE__		: Any     =     dilation
											# Hungarian matcher
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     class_cost
											SCREAMING_SNAKE_CASE__		: Tuple     =     bbox_cost
											SCREAMING_SNAKE_CASE__		: List[Any]     =     giou_cost
											# Loss coefficients
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     mask_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     dice_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     bbox_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Any     =     giou_loss_coefficient
											SCREAMING_SNAKE_CASE__		: List[str]     =     eos_coefficient
											super().__init__(is_encoder_decoder=SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return self.encoder_attention_heads



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return self.d_model



				@classmethod
				def 			__magic_name__					(cls     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	Tuple:



											"""simple docstring"""




											return cls(backbone_config=SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	Dict[str, any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Dict     =     copy.deepcopy(self.__dict__					)
											if output["backbone_config"] is not None:
																		SCREAMING_SNAKE_CASE__		: Any     =     self.backbone_config.to_dict()
											SCREAMING_SNAKE_CASE__		: Dict     =     self.__class__.model_type
											return output






class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		Any       =     version.parse('''1.11'''						)



				@property
				def 			__magic_name__					(self					)      ->	Mapping[str, Mapping[int, str]]:



											"""simple docstring"""




											return OrderedDict(
											    [
											        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
											        ("""pixel_mask""", {0: """batch"""}),
											    ]					)



				@property
				def 			__magic_name__					(self					)      ->	float:



											"""simple docstring"""




											return 1E-5



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return 12




