
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 							UpperCAmelCase				(  __SCREAMING_SNAKE_CASE  ):




	'''simple docstring'''





	def __init__(	self :			Union[str, Any]							,A :			str							,A :			Optional[Any]=13							,A :			Dict=7							,A :			List[str]=True							,A :			Optional[int]=True							,A :			Union[str, Any]=True							,A :			Optional[int]=True							,A :			Any=True							,A :			str=False							,A :			Tuple=False							,A :			Union[str, Any]=False							,A :			Any=2							,A :			List[Any]=99							,A :			List[Any]=0							,A :			List[str]=32							,A :			Tuple=5							,A :			Optional[Any]=4							,A :			int=0.1							,A :			Dict=0.1							,A :			Union[str, Any]=5_12							,A :			Any=12							,A :			Tuple=2							,A :			str=0.02							,A :			List[str]=3							,A :			Optional[Any]=4							,A :			Optional[int]="last"							,A :			List[str]=None							,A :			Optional[int]=None							,):
					__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     UpperCamelCase_  (	self :			str			):
					__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     UpperCamelCase_  (	self :			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     UpperCamelCase_  (	self :			Union[str, Any]							,A :			Dict							,A :			Tuple							,A :			Dict							,A :			List[str]							,A :			Dict							,A :			Dict							,A :			int							,A :			Optional[Any]							,A :			Tuple							,):
					__A             =			FlaubertModel(config=A			)
					model.to(A			)
					model.eval()
					__A             =			model(A							,lengths=A							,langs=A			)
					__A             =			model(A							,langs=A			)
					__A             =			model(A			)
					self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size)			)






	def     UpperCamelCase_  (	self :			Dict							,A :			Any							,A :			Any							,A :			Union[str, Any]							,A :			Union[str, Any]							,A :			Dict							,A :			int							,A :			List[Any]							,A :			int							,A :			Any							,):
					__A             =			FlaubertWithLMHeadModel(A			)
					model.to(A			)
					model.eval()

					__A             =			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 :			Union[str, Any]							,A :			Dict							,A :			Any							,A :			Dict							,A :			Optional[int]							,A :			List[Any]							,A :			int							,A :			List[str]							,A :			Dict							,A :			Union[str, Any]							,):
					__A             =			FlaubertForQuestionAnsweringSimple(A			)
					model.to(A			)
					model.eval()

					__A             =			model(A			)

					__A             =			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 :			List[Any]							,A :			Optional[int]							,A :			List[str]							,A :			Dict							,A :			Any							,A :			Optional[int]							,A :			int							,A :			Dict							,A :			Union[str, Any]							,A :			Dict							,):
					__A             =			FlaubertForQuestionAnswering(A			)
					model.to(A			)
					model.eval()

					__A             =			model(A			)

					__A             =			model(
					    A							,start_positions=A							,end_positions=A							,cls_index=A							,is_impossible=A							,p_mask=A							,)

					__A             =			model(
					    A							,start_positions=A							,end_positions=A							,cls_index=A							,is_impossible=A							,)

					((__A)      ,							)             =			result_with_labels.to_tuple()

					__A             =			model(A							,start_positions=A							,end_positions=A			)

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

					__A             =			model(A			)
					__A             =			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 :			Any							,A :			List[Any]							,A :			Union[str, Any]							,A :			Any							,A :			List[Any]							,A :			str							,A :			Dict							,A :			Dict							,):
					__A             =			self.num_labels
					__A             =			FlaubertForTokenClassification(A			)
					model.to(A			)
					model.eval()

					__A             =			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 :			int							,A :			int							,A :			Optional[Any]							,A :			List[str]							,A :			Dict							,A :			Optional[Any]							,A :			Optional[int]							,A :			Optional[Any]							,A :			List[Any]							,A :			int							,):
					__A             =			self.num_choices
					__A             =			FlaubertForMultipleChoice(config=A			)
					model.to(A			)
					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(
					    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 :			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 							UpperCAmelCase				(  __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      unittest.TestCase  ):




	'''simple docstring'''





	snake_case_    =					(
	    (
	        FlaubertModel,
	        FlaubertWithLMHeadModel,
	        FlaubertForQuestionAnswering,
	        FlaubertForQuestionAnsweringSimple,
	        FlaubertForSequenceClassification,
	        FlaubertForTokenClassification,
	        FlaubertForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	snake_case_    =					(
	    {
	        "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 :			Union[str, Any]							,A :			Tuple							,A :			int							,A :			Union[str, Any]							,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 :			int							,A :			Union[str, Any]							,A :			List[Any]							,A :			int=False			):
					__A             =			super()._prepare_for_class(A							,A							,return_labels=A			)

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

					return inputs_dict






	def     UpperCamelCase_  (	self :			int			):
					__A             =			FlaubertModelTester(self			)
					__A             =			ConfigTester(self							,config_class=A							,emb_dim=37			)






	def     UpperCamelCase_  (	self :			str			):
					self.config_tester.run_common_tests()






	def     UpperCamelCase_  (	self :			Optional[int]			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_flaubert_model(*A			)






	def     UpperCamelCase_  (	self :			Optional[int]			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_flaubert_lm_head(*A			)






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






	def     UpperCamelCase_  (	self :			List[Any]			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_flaubert_qa(*A			)






	def     UpperCamelCase_  (	self :			int			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_flaubert_sequence_classif(*A			)






	def     UpperCamelCase_  (	self :			Optional[int]			):
					__A             =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_flaubert_token_classif(*A			)






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






	@slow
	def     UpperCamelCase_  (	self :			Optional[Any]			):
					for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A             =			FlaubertModel.from_pretrained(A			)
									self.assertIsNotNone(A			)







	@slow
	@require_torch_gpu
	def     UpperCamelCase_  (	self :			List[str]			):
					__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=A			)

									__A             =			self._prepare_for_class(A							,A			)
									__A             =			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"			)			)
													__A             =			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 							UpperCAmelCase				(  unittest.TestCase  ):




	'''simple docstring'''





	@slow
	def     UpperCamelCase_  (	self :			Tuple			):
					__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(A			)[0]
					__A             =			torch.Size((1, 11, 7_68)			)
					self.assertEqual(output.shape							,A			)
					__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]							,A							,atol=1E-4			)			)


from __future__ import annotations


def 						UpperCAmelCase      (     a_ = 4						)		->     list[list[int]]:







				"""simple docstring"""



				__A             =			abs(a_						) or 4
				return [[1 + x + y * row_size for x in range(a_						)] for y in range(a_						)]


def 						UpperCAmelCase      (     a_						)		->     list[list[int]]:







				"""simple docstring"""



				return reverse_row(transpose(a_						)						)
				# OR.. transpose(reverse_column(matrix))


def 						UpperCAmelCase      (     a_						)		->     list[list[int]]:







				"""simple docstring"""



				return reverse_row(reverse_column(a_						)						)
				# OR.. reverse_column(reverse_row(matrix))


def 						UpperCAmelCase      (     a_						)		->     list[list[int]]:







				"""simple docstring"""



				return reverse_column(transpose(a_						)						)
				# OR.. transpose(reverse_row(matrix))


def 						UpperCAmelCase      (     a_						)		->     list[list[int]]:







				"""simple docstring"""



				__A             =			[list(a_						) for x in zip(*a_						)]
				return matrix


def 						UpperCAmelCase      (     a_						)		->     list[list[int]]:







				"""simple docstring"""



				__A             =			matrix[::-1]
				return matrix


def 						UpperCAmelCase      (     a_						)		->     list[list[int]]:







				"""simple docstring"""



				__A             =			[x[::-1] for x in matrix]
				return matrix


def 						UpperCAmelCase      (     a_						)		->     None:







				"""simple docstring"""



				for i in matrix:
								print(*a_						)


if __name__ == "__main__":
					SCREAMING_SNAKE_CASE							:Union[str, Any]										=    make_matrix()
					print('\norigin:\n')
					print_matrix(matrix)
					print('\nrotate 90 counterclockwise:\n')
					print_matrix(rotate_aa(matrix))

					SCREAMING_SNAKE_CASE							:Tuple										=    make_matrix()
					print('\norigin:\n')
					print_matrix(matrix)
					print('\nrotate 180:\n')
					print_matrix(rotate_aaa(matrix))

					SCREAMING_SNAKE_CASE							:Union[str, Any]										=    make_matrix()
					print('\norigin:\n')
					print_matrix(matrix)
					print('\nrotate 270 counterclockwise:\n')
					print_matrix(rotate_aaa(matrix))
