






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_					(   lowerCamelCase			):







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







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:List[Any]   =   ids_tensor([self.batch_size, self.seq_length]							,       self.vocab_size  )
								__magic_name__		:Dict   =   random_attention_mask([self.batch_size, self.seq_length]  )

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

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

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

								__magic_name__		:int   =   self.get_config()

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







				def       A						(    self  ):
								"""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       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]   =   FlaubertModel(config=__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()
								__magic_name__		:str   =   model(__lowerCAmelCase							,       lengths=__lowerCAmelCase							,       langs=__lowerCAmelCase  )
								__magic_name__		:Union[str, Any]   =   model(__lowerCAmelCase							,       langs=__lowerCAmelCase  )
								__magic_name__		:List[Any]   =   model(__lowerCAmelCase  )
								self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, self.seq_length, self.hidden_size)  )







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       ):
								"""simple docstring"""
								__magic_name__		:str   =   FlaubertWithLMHeadModel(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()

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







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]   =   FlaubertForQuestionAnsweringSimple(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()

								__magic_name__		:int   =   model(__lowerCAmelCase  )

								__magic_name__		:Optional[int]   =   model(__lowerCAmelCase							,       start_positions=__lowerCAmelCase							,       end_positions=__lowerCAmelCase  )
								self.parent.assertEqual(result.start_logits.shape							,       (self.batch_size, self.seq_length)  )
								self.parent.assertEqual(result.end_logits.shape							,       (self.batch_size, self.seq_length)  )







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       ):
								"""simple docstring"""
								__magic_name__		:Tuple   =   FlaubertForQuestionAnswering(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()

								__magic_name__		:Optional[Any]   =   model(__lowerCAmelCase  )

								__magic_name__		:int   =   model(
								    __lowerCAmelCase							,       start_positions=__lowerCAmelCase							,       end_positions=__lowerCAmelCase							,       cls_index=__lowerCAmelCase							,       is_impossible=__lowerCAmelCase							,       p_mask=__lowerCAmelCase							,       )

								__magic_name__		:Optional[Any]   =   model(
								    __lowerCAmelCase							,       start_positions=__lowerCAmelCase							,       end_positions=__lowerCAmelCase							,       cls_index=__lowerCAmelCase							,       is_impossible=__lowerCAmelCase							,       )

								((__magic_name__)  ,					)		:Any   =   result_with_labels.to_tuple()

								__magic_name__		:str   =   model(__lowerCAmelCase							,       start_positions=__lowerCAmelCase							,       end_positions=__lowerCAmelCase  )

								((__magic_name__)  ,					)		:List[str]   =   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       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]   =   FlaubertForSequenceClassification(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()

								__magic_name__		:int   =   model(__lowerCAmelCase  )
								__magic_name__		:List[str]   =   model(__lowerCAmelCase							,       labels=__lowerCAmelCase  )

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







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       ):
								"""simple docstring"""
								__magic_name__		:List[str]   =   self.num_labels
								__magic_name__		:Any   =   FlaubertForTokenClassification(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()

								__magic_name__		:List[Any]   =   model(__lowerCAmelCase							,       attention_mask=__lowerCAmelCase							,       labels=__lowerCAmelCase  )
								self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.seq_length, self.num_labels)  )







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       ):
								"""simple docstring"""
								__magic_name__		:Dict   =   self.num_choices
								__magic_name__		:Any   =   FlaubertForMultipleChoice(config=__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()
								__magic_name__		:int   =   input_ids.unsqueeze(1  ).expand(-1							,       self.num_choices							,       -1  ).contiguous()
								__magic_name__		:Any   =   token_type_ids.unsqueeze(1  ).expand(-1							,       self.num_choices							,       -1  ).contiguous()
								__magic_name__		:List[str]   =   input_mask.unsqueeze(1  ).expand(-1							,       self.num_choices							,       -1  ).contiguous()
								__magic_name__		:Any   =   model(
								    __lowerCAmelCase							,       attention_mask=__lowerCAmelCase							,       token_type_ids=__lowerCAmelCase							,       labels=__lowerCAmelCase							,       )
								self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.num_choices)  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Dict   =   self.prepare_config_and_inputs()
								(
								    (
								    __magic_name__
								)  ,					(
								    __magic_name__
								)  ,					(
								    __magic_name__
								)  ,					(
								    __magic_name__
								)  ,					(
								    __magic_name__
								)  ,					(
								    __magic_name__
								)  ,					(
								    __magic_name__
								)  ,					(
								    __magic_name__
								)  ,					(
								    __magic_name__
								)  ,					
								)		:int   =   config_and_inputs
								__magic_name__		:str   =   {
								    '''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_					(   lowerCamelCase      ,					lowerCamelCase      ,					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       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase  ):
								"""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       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase=False  ):
								"""simple docstring"""
								__magic_name__		:int   =   super()._prepare_for_class(__lowerCAmelCase							,       __lowerCAmelCase							,       return_labels=__lowerCAmelCase  )

								if return_labels:
												if model_class.__name__ == "FlaubertForQuestionAnswering":
																__magic_name__		:Union[str, Any]   =   torch.zeros(
																    self.model_tester.batch_size							,       dtype=torch.long							,       device=__lowerCAmelCase  )
																__magic_name__		:Any   =   torch.zeros(
																    self.model_tester.batch_size							,       dtype=torch.long							,       device=__lowerCAmelCase  )

								return inputs_dict







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Optional[Any]   =   FlaubertModelTester(self  )
								__magic_name__		:List[str]   =   ConfigTester(self							,       config_class=__lowerCAmelCase							,       emb_dim=3_7  )







				def       A						(    self  ):
								"""simple docstring"""
								self.config_tester.run_common_tests()







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Optional[Any]   =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_model(*__lowerCAmelCase  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:str   =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_lm_head(*__lowerCAmelCase  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Any   =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_simple_qa(*__lowerCAmelCase  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Tuple   =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_qa(*__lowerCAmelCase  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:List[str]   =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_sequence_classif(*__lowerCAmelCase  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:int   =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_token_classif(*__lowerCAmelCase  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:int   =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_multiple_choice(*__lowerCAmelCase  )







				@slow
				def       A						(    self  ):
								"""simple docstring"""
								for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												__magic_name__		:List[str]   =   FlaubertModel.from_pretrained(__lowerCAmelCase  )
												self.assertIsNotNone(__lowerCAmelCase  )







				@slow
				@require_torch_gpu
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__  ,					__magic_name__		: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

												__magic_name__		:List[str]   =   True
												__magic_name__		:Union[str, Any]   =   model_class(config=__lowerCAmelCase  )

												__magic_name__		:Union[str, Any]   =   self._prepare_for_class(__lowerCAmelCase							,       __lowerCAmelCase  )
												__magic_name__		:Union[str, Any]   =   torch.jit.trace(
												    __lowerCAmelCase							,       (inputs_dict['''input_ids'''].to('''cpu'''  ), inputs_dict['''attention_mask'''].to('''cpu'''  ))  )

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







@require_torch
class     lowerCamelCase_					(   unittest.TestCase			):







				@slow
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:int   =   FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''  )
								__magic_name__		:int   =   torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]  )
								with torch.no_grad():
												__magic_name__		:List[str]   =   model(__lowerCAmelCase  )[0]
								__magic_name__		:List[Any]   =   torch.Size((1, 1_1, 7_6_8)  )
								self.assertEqual(output.shape							,       __lowerCAmelCase  )
								__magic_name__		:Optional[Any]   =   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]							,       __lowerCAmelCase							,       atol=1E-4  )  )













def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case						,   snake_case							)		->    bool:
		return not any(
		    neighbour == 1 and colored_vertices[i] == color
		    for i, neighbour in enumerate(snake_case							)							)



def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case						,   snake_case						,   snake_case							)		->    bool:

		# Base Case
		if index == len(snake_case							):
				return True

		# Recursive Step
		for i in range(snake_case							):
				if valid_coloring(graph[index]						,   snake_case						,   snake_case							):
						# Color current vertex
						__lowercase											=     i
						# Validate coloring
						if util_color(snake_case						,   snake_case						,   snake_case						,   index + 1							):
								return True
						# Backtrack
						__lowercase											=     -1
		return False



def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case							)		->    list[int]:
		__lowercase											=     [-1] * len(snake_case							)

		if util_color(snake_case						,   snake_case						,   snake_case						,   0							):
				return colored_vertices

		return []




