

'''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    snake_case     (      lowercase_		):






				"""simple docstring"""



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



				def   a__					(    self       )     ->							Tuple:
											SCREAMING_SNAKE_CASE_				=				ids_tensor([self.batch_size, self.seq_length],     self.vocab_size       )
											SCREAMING_SNAKE_CASE_				=				random_attention_mask([self.batch_size, self.seq_length]       )

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

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

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

											SCREAMING_SNAKE_CASE_				=				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       )     ->							List[str]:
											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,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     )     ->							List[str]:
											SCREAMING_SNAKE_CASE_				=				FlaubertModel(config=_lowercase       )
											model.to(_lowercase       )
											model.eval()
											SCREAMING_SNAKE_CASE_				=				model(_lowercase,     lengths=_lowercase,     langs=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				model(_lowercase,     langs=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				model(_lowercase       )
											self.parent.assertEqual(result.last_hidden_state.shape,     (self.batch_size, self.seq_length, self.hidden_size)       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     )     ->							Any:
											SCREAMING_SNAKE_CASE_				=				FlaubertWithLMHeadModel(_lowercase       )
											model.to(_lowercase       )
											model.eval()

											SCREAMING_SNAKE_CASE_				=				model(_lowercase,     token_type_ids=_lowercase,     labels=_lowercase       )
											self.parent.assertEqual(result.loss.shape,     ()       )
											self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.seq_length, self.vocab_size)       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     )     ->							Optional[Any]:
											SCREAMING_SNAKE_CASE_				=				FlaubertForQuestionAnsweringSimple(_lowercase       )
											model.to(_lowercase       )
											model.eval()

											SCREAMING_SNAKE_CASE_				=				model(_lowercase       )

											SCREAMING_SNAKE_CASE_				=				model(_lowercase,     start_positions=_lowercase,     end_positions=_lowercase       )
											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,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     )     ->							Tuple:
											SCREAMING_SNAKE_CASE_				=				FlaubertForQuestionAnswering(_lowercase       )
											model.to(_lowercase       )
											model.eval()

											SCREAMING_SNAKE_CASE_				=				model(_lowercase       )

											SCREAMING_SNAKE_CASE_				=				model(
											    _lowercase,     start_positions=_lowercase,     end_positions=_lowercase,     cls_index=_lowercase,     is_impossible=_lowercase,     p_mask=_lowercase,     )

											SCREAMING_SNAKE_CASE_				=				model(
											    _lowercase,     start_positions=_lowercase,     end_positions=_lowercase,     cls_index=_lowercase,     is_impossible=_lowercase,     )

											((SCREAMING_SNAKE_CASE_)				,    )				=				result_with_labels.to_tuple()

											SCREAMING_SNAKE_CASE_				=				model(_lowercase,     start_positions=_lowercase,     end_positions=_lowercase       )

											((SCREAMING_SNAKE_CASE_)				,    )				=				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,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     )     ->							List[str]:
											SCREAMING_SNAKE_CASE_				=				FlaubertForSequenceClassification(_lowercase       )
											model.to(_lowercase       )
											model.eval()

											SCREAMING_SNAKE_CASE_				=				model(_lowercase       )
											SCREAMING_SNAKE_CASE_				=				model(_lowercase,     labels=_lowercase       )

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



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     )     ->							Any:
											SCREAMING_SNAKE_CASE_				=				self.num_labels
											SCREAMING_SNAKE_CASE_				=				FlaubertForTokenClassification(_lowercase       )
											model.to(_lowercase       )
											model.eval()

											SCREAMING_SNAKE_CASE_				=				model(_lowercase,     attention_mask=_lowercase,     labels=_lowercase       )
											self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.seq_length, self.num_labels)       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     )     ->							Any:
											SCREAMING_SNAKE_CASE_				=				self.num_choices
											SCREAMING_SNAKE_CASE_				=				FlaubertForMultipleChoice(config=_lowercase       )
											model.to(_lowercase       )
											model.eval()
											SCREAMING_SNAKE_CASE_				=				input_ids.unsqueeze(1       ).expand(-1,     self.num_choices,     -1       ).contiguous()
											SCREAMING_SNAKE_CASE_				=				token_type_ids.unsqueeze(1       ).expand(-1,     self.num_choices,     -1       ).contiguous()
											SCREAMING_SNAKE_CASE_				=				input_mask.unsqueeze(1       ).expand(-1,     self.num_choices,     -1       ).contiguous()
											SCREAMING_SNAKE_CASE_				=				model(
											    _lowercase,     attention_mask=_lowercase,     token_type_ids=_lowercase,     labels=_lowercase,     )
											self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.num_choices)       )





				def   a__					(    self       )     ->							List[Any]:
											SCREAMING_SNAKE_CASE_				=				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_
											)				,    
											)				=				config_and_inputs
											SCREAMING_SNAKE_CASE_				=				{
											    'input_ids': input_ids,
											    'token_type_ids': token_type_ids,
											    'lengths': input_lengths,
											    'attention_mask': input_mask,
											}
											return config, inputs_dict






@require_torch
class    snake_case     (      lowercase_,						lowercase_,						unittest.TestCase		):






				"""simple docstring"""
				_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,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     _lowercase       )     ->							List[str]:
											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,     _lowercase,     _lowercase,     _lowercase=False       )     ->							Tuple:
											SCREAMING_SNAKE_CASE_				=				super()._prepare_for_class(_lowercase,     _lowercase,     return_labels=_lowercase       )

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

											return inputs_dict



				def   a__					(    self       )     ->							Optional[Any]:
											SCREAMING_SNAKE_CASE_				=				FlaubertModelTester(self       )
											SCREAMING_SNAKE_CASE_				=				ConfigTester(self,     config_class=_lowercase,     emb_dim=37       )



				def   a__					(    self       )     ->							str:
											self.config_tester.run_common_tests()



				def   a__					(    self       )     ->							Dict:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_model(*_lowercase       )



				def   a__					(    self       )     ->							Union[str, Any]:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_lm_head(*_lowercase       )



				def   a__					(    self       )     ->							Tuple:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_simple_qa(*_lowercase       )



				def   a__					(    self       )     ->							List[str]:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_qa(*_lowercase       )



				def   a__					(    self       )     ->							Any:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_sequence_classif(*_lowercase       )



				def   a__					(    self       )     ->							Union[str, Any]:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_token_classif(*_lowercase       )



				def   a__					(    self       )     ->							str:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_multiple_choice(*_lowercase       )



				@slow
				def   a__					(    self       )     ->							Any:
											for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE_				=				FlaubertModel.from_pretrained(_lowercase       )
																		self.assertIsNotNone(_lowercase       )





				@slow
				@require_torch_gpu
				def   a__					(    self       )     ->							Dict:
											SCREAMING_SNAKE_CASE_				,    SCREAMING_SNAKE_CASE_				=				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_				=				True
																		SCREAMING_SNAKE_CASE_				=				model_class(config=_lowercase       )

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

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






@require_torch
class    snake_case     (      unittest.TestCase		):






				"""simple docstring"""



				@slow
				def   a__					(    self       )     ->							Dict:
											SCREAMING_SNAKE_CASE_				=				FlaubertModel.from_pretrained('flaubert/flaubert_base_cased'       )
											SCREAMING_SNAKE_CASE_				=				torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]       )
											with torch.no_grad():
																		SCREAMING_SNAKE_CASE_				=				model(_lowercase       )[0]
											SCREAMING_SNAKE_CASE_				=				torch.Size((1, 11, 768)       )
											self.assertEqual(output.shape,     _lowercase       )
											SCREAMING_SNAKE_CASE_				=				torch.tensor(
											    [[[-2.6_251, -1.4_298, -0.0_227], [-2.8_510, -1.6_387, 0.2_258], [-2.8_114, -1.1_832, -0.3_066]]]       )

											self.assertTrue(torch.allclose(output[:, :3, :3],     _lowercase,     atol=1E-4       )       )








'''simple docstring'''

import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class    snake_case     (      lowercase_		):






				"""simple docstring"""
				_a    	=     ["""image_processor""", """tokenizer"""]
				_a    	=     """CLIPImageProcessor"""
				_a    	=     ("""XLMRobertaTokenizer""", """XLMRobertaTokenizerFast""")



				def __init__(    self,     _lowercase=None,     _lowercase=None,     **_lowercase       )     ->							int:
											SCREAMING_SNAKE_CASE_				=				None
											if "feature_extractor" in kwargs:
																		warnings.warn(
																		    'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
																		    ' instead.',     _lowercase,     )
																		SCREAMING_SNAKE_CASE_				=				kwargs.pop('feature_extractor'       )

											SCREAMING_SNAKE_CASE_				=				image_processor if image_processor is not None else feature_extractor
											if image_processor is None:
																		raise ValueError('You need to specify an `image_processor`.'       )
											if tokenizer is None:
																		raise ValueError('You need to specify a `tokenizer`.'       )

											super().__init__(_lowercase,     _lowercase       )



				def __call__(    self,     _lowercase=None,     _lowercase=None,     _lowercase=None,     **_lowercase       )     ->							str:

											if text is None and images is None:
																		raise ValueError('You have to specify either text or images. Both cannot be none.'       )

											if text is not None:
																		SCREAMING_SNAKE_CASE_				=				self.tokenizer(_lowercase,     return_tensors=_lowercase,     **_lowercase       )

											if images is not None:
																		SCREAMING_SNAKE_CASE_				=				self.image_processor(_lowercase,     return_tensors=_lowercase,     **_lowercase       )

											if text is not None and images is not None:
																		SCREAMING_SNAKE_CASE_				=				image_features.pixel_values
																		return encoding
											elif text is not None:
																		return encoding
											else:
																		return BatchEncoding(data=dict(**_lowercase       ),     tensor_type=_lowercase       )



				def   a__					(    self,     *_lowercase,     **_lowercase       )     ->							Optional[int]:
											return self.tokenizer.batch_decode(*_lowercase,     **_lowercase       )



				def   a__					(    self,     *_lowercase,     **_lowercase       )     ->							Optional[Any]:
											return self.tokenizer.decode(*_lowercase,     **_lowercase       )





				@property
				def   a__					(    self       )     ->							Optional[int]:
											SCREAMING_SNAKE_CASE_				=				self.tokenizer.model_input_names
											SCREAMING_SNAKE_CASE_				=				self.image_processor.model_input_names
											return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names       )       )





