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






	def __init__(self				: Optional[int]      ,	snake_case				: List[str]      ,	snake_case				: List[Any]=13      ,	snake_case				: Any=7      ,	snake_case				: Optional[Any]=True      ,	snake_case				: Union[str, Any]=True      ,	snake_case				: Optional[Any]=True      ,	snake_case				: Union[str, Any]=True      ,	snake_case				: List[str]=True      ,	snake_case				: List[str]=False      ,	snake_case				: Optional[Any]=False      ,	snake_case				: str=False      ,	snake_case				: Tuple=2      ,	snake_case				: Tuple=99      ,	snake_case				: Union[str, Any]=0      ,	snake_case				: int=32      ,	snake_case				: List[str]=5      ,	snake_case				: List[Any]=4      ,	snake_case				: int=0.1      ,	snake_case				: Tuple=0.1      ,	snake_case				: Tuple=512      ,	snake_case				: str=12      ,	snake_case				: int=2      ,	snake_case				: str=0.02      ,	snake_case				: List[str]=3      ,	snake_case				: Dict=4      ,	snake_case				: Dict="last"      ,	snake_case				: Dict=None      ,	snake_case				: Optional[Any]=None      ,	) ->			int:
							_lowercase       :	Union[str, Any]           =   parent
							_lowercase       :	Any           =   batch_size
							_lowercase       :	Any           =   seq_length
							_lowercase       :	str           =   is_training
							_lowercase       :	Optional[int]           =   use_input_lengths
							_lowercase       :	Tuple           =   use_token_type_ids
							_lowercase       :	Tuple           =   use_labels
							_lowercase       :	Tuple           =   gelu_activation
							_lowercase       :	List[Any]           =   sinusoidal_embeddings
							_lowercase       :	Optional[Any]           =   causal
							_lowercase       :	Dict           =   asm
							_lowercase       :	List[Any]           =   n_langs
							_lowercase       :	Union[str, Any]           =   vocab_size
							_lowercase       :	int           =   n_special
							_lowercase       :	Any           =   hidden_size
							_lowercase       :	Optional[int]           =   num_hidden_layers
							_lowercase       :	Tuple           =   num_attention_heads
							_lowercase       :	Union[str, Any]           =   hidden_dropout_prob
							_lowercase       :	Optional[int]           =   attention_probs_dropout_prob
							_lowercase       :	Any           =   max_position_embeddings
							_lowercase       :	List[Any]           =   type_vocab_size
							_lowercase       :	Tuple           =   type_sequence_label_size
							_lowercase       :	Any           =   initializer_range
							_lowercase       :	Optional[Any]           =   num_labels
							_lowercase       :	Optional[Any]           =   num_choices
							_lowercase       :	Optional[Any]           =   summary_type
							_lowercase       :	Union[str, Any]           =   use_proj
							_lowercase       :	int           =   scope






	def       _a(self				: int    ) ->			int:
							_lowercase       :	Optional[Any]           =   ids_tensor([self.batch_size, self.seq_length]      ,	self.vocab_size    )
							_lowercase       :	Union[str, Any]           =   random_attention_mask([self.batch_size, self.seq_length]    )

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

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

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

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






	def       _a(self				: int      ,	snake_case				: Tuple      ,	snake_case				: Optional[Any]      ,	snake_case				: List[Any]      ,	snake_case				: int      ,	snake_case				: Optional[int]      ,	snake_case				: Optional[Any]      ,	snake_case				: List[Any]      ,	snake_case				: Any      ,	snake_case				: Optional[Any]      ,	) ->			Tuple:
							_lowercase       :	Optional[int]           =   FlaubertWithLMHeadModel(snake_case    )
							model.to(snake_case    )
							model.eval()

							_lowercase       :	Optional[Any]           =   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       _a(self				: Any      ,	snake_case				: Any      ,	snake_case				: Tuple      ,	snake_case				: Any      ,	snake_case				: Optional[Any]      ,	snake_case				: Union[str, Any]      ,	snake_case				: int      ,	snake_case				: List[str]      ,	snake_case				: str      ,	snake_case				: Union[str, Any]      ,	) ->			Any:
							_lowercase       :	Any           =   FlaubertForQuestionAnsweringSimple(snake_case    )
							model.to(snake_case    )
							model.eval()

							_lowercase       :	str           =   model(snake_case    )

							_lowercase       :	Optional[int]           =   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       _a(self				: int      ,	snake_case				: Tuple      ,	snake_case				: str      ,	snake_case				: Dict      ,	snake_case				: List[Any]      ,	snake_case				: List[str]      ,	snake_case				: Optional[Any]      ,	snake_case				: Tuple      ,	snake_case				: Union[str, Any]      ,	snake_case				: List[Any]      ,	) ->			Tuple:
							_lowercase       :	List[Any]           =   FlaubertForQuestionAnswering(snake_case    )
							model.to(snake_case    )
							model.eval()

							_lowercase       :	int           =   model(snake_case    )

							_lowercase       :	int           =   model(
							    snake_case      ,	start_positions=snake_case      ,	end_positions=snake_case      ,	cls_index=snake_case      ,	is_impossible=snake_case      ,	p_mask=snake_case      ,	)

							_lowercase       :	Tuple           =   model(
							    snake_case      ,	start_positions=snake_case      ,	end_positions=snake_case      ,	cls_index=snake_case      ,	is_impossible=snake_case      ,	)

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

							_lowercase       :	List[str]           =   model(snake_case      ,	start_positions=snake_case      ,	end_positions=snake_case    )

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

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

							_lowercase       :	Optional[int]           =   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       _a(self				: Optional[int]      ,	snake_case				: List[Any]      ,	snake_case				: List[str]      ,	snake_case				: Dict      ,	snake_case				: str      ,	snake_case				: Tuple      ,	snake_case				: str      ,	snake_case				: Union[str, Any]      ,	snake_case				: Any      ,	snake_case				: str      ,	) ->			str:
							_lowercase       :	Union[str, Any]           =   self.num_choices
							_lowercase       :	Optional[int]           =   FlaubertForMultipleChoice(config=snake_case    )
							model.to(snake_case    )
							model.eval()
							_lowercase       :	Optional[Any]           =   input_ids.unsqueeze(1    ).expand(-1      ,	self.num_choices      ,	-1    ).contiguous()
							_lowercase       :	str           =   token_type_ids.unsqueeze(1    ).expand(-1      ,	self.num_choices      ,	-1    ).contiguous()
							_lowercase       :	Any           =   input_mask.unsqueeze(1    ).expand(-1      ,	self.num_choices      ,	-1    ).contiguous()
							_lowercase       :	Dict           =   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       _a(self				: str    ) ->			List[Any]:
							_lowercase       :	List[Any]           =   self.prepare_config_and_inputs()
							(
							    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    (
							    _lowercase
							)      ,    
							)       :	Optional[int]           =   config_and_inputs
							_lowercase       :	Optional[Any]           =   {
							    "input_ids": input_ids,
							    "token_type_ids": token_type_ids,
							    "lengths": input_lengths,
							    "attention_mask": input_mask,
							}
							return config, inputs_dict




@require_torch
class   __lowercase						(						__snake_case   , __snake_case   , 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				: Optional[int]      ,	snake_case				: List[str]      ,	snake_case				: str      ,	snake_case				: Optional[int]      ,	snake_case				: List[str]      ,	snake_case				: Optional[Any]    ) ->			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       _a(self				: List[Any]      ,	snake_case				: Any      ,	snake_case				: List[Any]      ,	snake_case				: Any=False    ) ->			str:
							_lowercase       :	Any           =   super()._prepare_for_class(snake_case      ,	snake_case      ,	return_labels=snake_case    )

							if return_labels:
													if model_class.__name__ == "FlaubertForQuestionAnswering":
																			_lowercase       :	Tuple           =   torch.zeros(
																			    self.model_tester.batch_size      ,	dtype=torch.long      ,	device=snake_case    )
																			_lowercase       :	Dict           =   torch.zeros(
																			    self.model_tester.batch_size      ,	dtype=torch.long      ,	device=snake_case    )

							return inputs_dict






	def       _a(self				: Dict    ) ->			Dict:
							_lowercase       :	str           =   FlaubertModelTester(self    )
							_lowercase       :	Any           =   ConfigTester(self      ,	config_class=snake_case      ,	emb_dim=37    )






	def       _a(self				: Optional[Any]    ) ->			Tuple:
							self.config_tester.run_common_tests()






	def       _a(self				: int    ) ->			Tuple:
							_lowercase       :	Any           =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_model(*snake_case    )






	def       _a(self				: List[str]    ) ->			str:
							_lowercase       :	Any           =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_lm_head(*snake_case    )






	def       _a(self				: Tuple    ) ->			int:
							_lowercase       :	Optional[int]           =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_simple_qa(*snake_case    )






	def       _a(self				: int    ) ->			Optional[int]:
							_lowercase       :	Optional[Any]           =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_qa(*snake_case    )






	def       _a(self				: List[Any]    ) ->			Optional[Any]:
							_lowercase       :	int           =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case    )






	def       _a(self				: List[str]    ) ->			Dict:
							_lowercase       :	Optional[Any]           =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_token_classif(*snake_case    )






	def       _a(self				: int    ) ->			List[str]:
							_lowercase       :	str           =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_multiple_choice(*snake_case    )






	@slow
	def       _a(self				: List[str]    ) ->			List[Any]:
							for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_lowercase       :	Tuple           =   FlaubertModel.from_pretrained(snake_case    )
													self.assertIsNotNone(snake_case    )






	@slow
	@require_torch_gpu
	def       _a(self				: Union[str, Any]    ) ->			str:
							_lowercase      ,    _lowercase       :	List[str]           =   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

													_lowercase       :	List[str]           =   True
													_lowercase       :	Optional[Any]           =   model_class(config=snake_case    )

													_lowercase       :	Any           =   self._prepare_for_class(snake_case      ,	snake_case    )
													_lowercase       :	str           =   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"    )    )
																			_lowercase       :	int           =   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   __lowercase						(						unittest.TestCase       ):






	@slow
	def       _a(self				: Union[str, Any]    ) ->			Tuple:
							_lowercase       :	Optional[Any]           =   FlaubertModel.from_pretrained("flaubert/flaubert_base_cased"    )
							_lowercase       :	List[str]           =   torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]    )
							with torch.no_grad():
													_lowercase       :	int           =   model(snake_case    )[0]
							_lowercase       :	Optional[Any]           =   torch.Size((1, 11, 768)    )
							self.assertEqual(output.shape      ,	snake_case    )
							_lowercase       :	Optional[Any]           =   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    )    )






import warnings

from ...utils import logging
from .image_processing_flava import FlavaImageProcessor


UpperCamelCase_						:     List[str]	       =							logging.get_logger(__name__)




class   __lowercase						(						__snake_case       ):






	def __init__(self				: int      ,	*snake_case				: Optional[Any]      ,	**snake_case				: Optional[Any]    ) ->			None:
							warnings.warn(
							    "The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please"
							    " use FlavaImageProcessor instead."      ,	snake_case      ,	)
							super().__init__(*snake_case      ,	**snake_case    )




