





"""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      (       _lowercase ):


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


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

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

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

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

								UpperCamelCase   =	self.get_config()

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


						def     snake_case_   (self		) ->   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     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							) ->   Union[str, Any]:
								UpperCamelCase   =	FlaubertModel(config=__UpperCamelCase		)
								model.to(__UpperCamelCase		)
								model.eval()
								UpperCamelCase   =	model(__UpperCamelCase   ,							lengths=__UpperCamelCase   ,							langs=__UpperCamelCase		)
								UpperCamelCase   =	model(__UpperCamelCase   ,							langs=__UpperCamelCase		)
								UpperCamelCase   =	model(__UpperCamelCase		)
								self.parent.assertEqual(result.last_hidden_state.shape   ,							(self.batch_size, self.seq_length, self.hidden_size)		)


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							) ->   Union[str, Any]:
								UpperCamelCase   =	FlaubertWithLMHeadModel(__UpperCamelCase		)
								model.to(__UpperCamelCase		)
								model.eval()

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


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							) ->   Optional[int]:
								UpperCamelCase   =	FlaubertForQuestionAnsweringSimple(__UpperCamelCase		)
								model.to(__UpperCamelCase		)
								model.eval()

								UpperCamelCase   =	model(__UpperCamelCase		)

								UpperCamelCase   =	model(__UpperCamelCase   ,							start_positions=__UpperCamelCase   ,							end_positions=__UpperCamelCase		)
								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     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							) ->   Any:
								UpperCamelCase   =	FlaubertForQuestionAnswering(__UpperCamelCase		)
								model.to(__UpperCamelCase		)
								model.eval()

								UpperCamelCase   =	model(__UpperCamelCase		)

								UpperCamelCase   =	model(
								    __UpperCamelCase   ,							start_positions=__UpperCamelCase   ,							end_positions=__UpperCamelCase   ,							cls_index=__UpperCamelCase   ,							is_impossible=__UpperCamelCase   ,							p_mask=__UpperCamelCase   ,							)

								UpperCamelCase   =	model(
								    __UpperCamelCase   ,							start_positions=__UpperCamelCase   ,							end_positions=__UpperCamelCase   ,							cls_index=__UpperCamelCase   ,							is_impossible=__UpperCamelCase   ,							)

								((UpperCamelCase   )   ,				   )   =	result_with_labels.to_tuple()

								UpperCamelCase   =	model(__UpperCamelCase   ,							start_positions=__UpperCamelCase   ,							end_positions=__UpperCamelCase		)

								((UpperCamelCase   )   ,				   )   =	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     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							) ->   Dict:
								UpperCamelCase   =	FlaubertForSequenceClassification(__UpperCamelCase		)
								model.to(__UpperCamelCase		)
								model.eval()

								UpperCamelCase   =	model(__UpperCamelCase		)
								UpperCamelCase   =	model(__UpperCamelCase   ,							labels=__UpperCamelCase		)

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


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							) ->   Union[str, Any]:
								UpperCamelCase   =	self.num_labels
								UpperCamelCase   =	FlaubertForTokenClassification(__UpperCamelCase		)
								model.to(__UpperCamelCase		)
								model.eval()

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


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							) ->   Optional[Any]:
								UpperCamelCase   =	self.num_choices
								UpperCamelCase   =	FlaubertForMultipleChoice(config=__UpperCamelCase		)
								model.to(__UpperCamelCase		)
								model.eval()
								UpperCamelCase   =	input_ids.unsqueeze(1		).expand(-1   ,							self.num_choices   ,							-1		).contiguous()
								UpperCamelCase   =	token_type_ids.unsqueeze(1		).expand(-1   ,							self.num_choices   ,							-1		).contiguous()
								UpperCamelCase   =	input_mask.unsqueeze(1		).expand(-1   ,							self.num_choices   ,							-1		).contiguous()
								UpperCamelCase   =	model(
								    __UpperCamelCase   ,							attention_mask=__UpperCamelCase   ,							token_type_ids=__UpperCamelCase   ,							labels=__UpperCamelCase   ,							)
								self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.num_choices)		)




						def     snake_case_   (self		) ->   Any:
								UpperCamelCase   =	self.prepare_config_and_inputs()
								(
								    (
								    UpperCamelCase   
								)   ,				(
								    UpperCamelCase   
								)   ,				(
								    UpperCamelCase   
								)   ,				(
								    UpperCamelCase   
								)   ,				(
								    UpperCamelCase   
								)   ,				(
								    UpperCamelCase   
								)   ,				(
								    UpperCamelCase   
								)   ,				(
								    UpperCamelCase   
								)   ,				(
								    UpperCamelCase   
								)   ,				   
								)   =	config_and_inputs
								UpperCamelCase   =	{
								    "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      (       _lowercase  ,			_lowercase  ,			unittest.TestCase ):
						UpperCAmelCase_ =			(
						    (
						        FlaubertModel,
						        FlaubertWithLMHeadModel,
						        FlaubertForQuestionAnswering,
						        FlaubertForQuestionAnsweringSimple,
						        FlaubertForSequenceClassification,
						        FlaubertForTokenClassification,
						        FlaubertForMultipleChoice,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCAmelCase_ =			(
						    {
						        "feature-extraction": FlaubertModel,
						        "fill-mask": FlaubertWithLMHeadModel,
						        "question-answering": FlaubertForQuestionAnsweringSimple,
						        "text-classification": FlaubertForSequenceClassification,
						        "token-classification": FlaubertForTokenClassification,
						        "zero-shot": FlaubertForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a		) ->   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     snake_case_   (self   ,							__a   ,							__a   ,							__a=False		) ->   Optional[Any]:
								UpperCamelCase   =	super()._prepare_for_class(__UpperCamelCase   ,							__UpperCamelCase   ,							return_labels=__UpperCamelCase		)

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

								return inputs_dict


						def     snake_case_   (self		) ->   List[str]:
								UpperCamelCase   =	FlaubertModelTester(self		)
								UpperCamelCase   =	ConfigTester(self   ,							config_class=__UpperCamelCase   ,							emb_dim=37		)


						def     snake_case_   (self		) ->   List[Any]:
								self.config_tester.run_common_tests()


						def     snake_case_   (self		) ->   str:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_model(*__UpperCamelCase		)


						def     snake_case_   (self		) ->   Dict:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_lm_head(*__UpperCamelCase		)


						def     snake_case_   (self		) ->   int:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_simple_qa(*__UpperCamelCase		)


						def     snake_case_   (self		) ->   Any:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_qa(*__UpperCamelCase		)


						def     snake_case_   (self		) ->   str:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_sequence_classif(*__UpperCamelCase		)


						def     snake_case_   (self		) ->   List[Any]:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_token_classif(*__UpperCamelCase		)


						def     snake_case_   (self		) ->   Union[str, Any]:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_multiple_choice(*__UpperCamelCase		)


						@slow
						def     snake_case_   (self		) ->   Dict:
								for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										UpperCamelCase   =	FlaubertModel.from_pretrained(__UpperCamelCase		)
										self.assertIsNotNone(__UpperCamelCase		)




						@slow
						@require_torch_gpu
						def     snake_case_   (self		) ->   List[Any]:
								UpperCamelCase   ,				UpperCamelCase   =	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

										UpperCamelCase   =	True
										UpperCamelCase   =	model_class(config=__UpperCamelCase		)

										UpperCamelCase   =	self._prepare_for_class(__UpperCamelCase   ,							__UpperCamelCase		)
										UpperCamelCase   =	torch.jit.trace(
										    __UpperCamelCase   ,							(inputs_dict["input_ids"].to("cpu"		), inputs_dict["attention_mask"].to("cpu"		))		)

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





@require_torch
class        _lowerCamelCase      (       unittest.TestCase ):


						@slow
						def     snake_case_   (self		) ->   Union[str, Any]:
								UpperCamelCase   =	FlaubertModel.from_pretrained("flaubert/flaubert_base_cased"		)
								UpperCamelCase   =	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():
										UpperCamelCase   =	model(__UpperCamelCase		)[0]
								UpperCamelCase   =	torch.Size((1, 11, 7_68)		)
								self.assertEqual(output.shape   ,							__UpperCamelCase		)
								UpperCamelCase   =	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]   ,							__UpperCamelCase   ,							atol=1e-4		)		)













"""simple docstring"""





lowerCAmelCase__     			=			'''
# Transformers installation
! pip install transformers datasets
# To install from source instead of the last release, comment the command above and uncomment the following one.
# ! pip install git+https://github.com/huggingface/transformers.git
'''

lowerCAmelCase__     			=			[{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
lowerCAmelCase__     			=			{
    '''{processor_class}''': '''FakeProcessorClass''',
    '''{model_class}''': '''FakeModelClass''',
    '''{object_class}''': '''FakeObjectClass''',
}






