





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  __A       (  _lowerCAmelCase      ):


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


				def _snake_case    (						self							):
								lowerCamelCase			=ids_tensor([self.batch_size, self.seq_length]				,	self.vocab_size							)
								lowerCamelCase			=random_attention_mask([self.batch_size, self.seq_length]							)

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

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

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

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


				def _snake_case    (						self				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	):
								lowerCamelCase			=FlaubertWithLMHeadModel(_lowercase							)
								model.to(_lowercase							)
								model.eval()

								lowerCamelCase			=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 _snake_case    (						self				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	):
								lowerCamelCase			=FlaubertForQuestionAnsweringSimple(_lowercase							)
								model.to(_lowercase							)
								model.eval()

								lowerCamelCase			=model(_lowercase							)

								lowerCamelCase			=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 _snake_case    (						self				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	):
								lowerCamelCase			=FlaubertForQuestionAnswering(_lowercase							)
								model.to(_lowercase							)
								model.eval()

								lowerCamelCase			=model(_lowercase							)

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

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

								((lowerCamelCase    )	,					    )			=result_with_labels.to_tuple()

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

								((lowerCamelCase    )	,					    )			=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				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	):
								lowerCamelCase			=FlaubertForSequenceClassification(_lowercase							)
								model.to(_lowercase							)
								model.eval()

								lowerCamelCase			=model(_lowercase							)
								lowerCamelCase			=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 _snake_case    (						self				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	):
								lowerCamelCase			=self.num_labels
								lowerCamelCase			=FlaubertForTokenClassification(_lowercase							)
								model.to(_lowercase							)
								model.eval()

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


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


				def _snake_case    (						self							):
								lowerCamelCase			=self.prepare_config_and_inputs()
								(
								    (
								    lowerCamelCase    
								)	,					(
								    lowerCamelCase    
								)	,					(
								    lowerCamelCase    
								)	,					(
								    lowerCamelCase    
								)	,					(
								    lowerCamelCase    
								)	,					(
								    lowerCamelCase    
								)	,					(
								    lowerCamelCase    
								)	,					(
								    lowerCamelCase    
								)	,					(
								    lowerCamelCase    
								)	,					    
								)			=config_and_inputs
								lowerCamelCase			={
								    """input_ids""": input_ids,
								    """token_type_ids""": token_type_ids,
								    """lengths""": input_lengths,
								    """attention_mask""": input_mask,
								}
								return config, inputs_dict




@require_torch
class  __A       (  _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 _snake_case    (						self				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_							):
								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				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_=False							):
								lowerCamelCase			=super()._prepare_for_class(_lowercase				,	_lowercase				,	return_labels=_lowercase							)

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

								return inputs_dict


				def _snake_case    (						self							):
								lowerCamelCase			=FlaubertModelTester(self							)
								lowerCamelCase			=ConfigTester(self				,	config_class=_lowercase				,	emb_dim=37							)


				def _snake_case    (						self							):
								self.config_tester.run_common_tests()


				def _snake_case    (						self							):
								lowerCamelCase			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_model(*_lowercase							)


				def _snake_case    (						self							):
								lowerCamelCase			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_lm_head(*_lowercase							)


				def _snake_case    (						self							):
								lowerCamelCase			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_simple_qa(*_lowercase							)


				def _snake_case    (						self							):
								lowerCamelCase			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_qa(*_lowercase							)


				def _snake_case    (						self							):
								lowerCamelCase			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_sequence_classif(*_lowercase							)


				def _snake_case    (						self							):
								lowerCamelCase			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_token_classif(*_lowercase							)


				def _snake_case    (						self							):
								lowerCamelCase			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_multiple_choice(*_lowercase							)


				@slow
				def _snake_case    (						self							):
								for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowerCamelCase			=FlaubertModel.from_pretrained(_lowercase							)
												self.assertIsNotNone(_lowercase							)


				@slow
				@require_torch_gpu
				def _snake_case    (						self							):
								lowerCamelCase	,					lowerCamelCase			=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

												lowerCamelCase			=True
												lowerCamelCase			=model_class(config=_lowercase							)

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


				@slow
				def _snake_case    (						self							):
								lowerCamelCase			=FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased"""							)
								lowerCamelCase			=torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]							)
								with torch.no_grad():
												lowerCamelCase			=model(_lowercase							)[0]
								lowerCamelCase			=torch.Size((1, 11, 768)							)
								self.assertEqual(output.shape				,	_lowercase							)
								lowerCamelCase			=torch.tensor(
								    [[[-2.6_2_5_1, -1.4_2_9_8, -0.0_2_2_7], [-2.8_5_1_0, -1.6_3_8_7, 0.2_2_5_8], [-2.8_1_1_4, -1.1_8_3_2, -0.3_0_6_6]]]							)

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












# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.:
#   python ./utils/get_modified_files.py utils src tests examples
#
# it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered
# since the output of this script is fed into Makefile commands it doesn't print a newline after the results

import re
import subprocess
import sys


UpperCAmelCase__  :     Union[str, Any]   			=subprocess.check_output('''git merge-base main HEAD'''.split()).decode('''utf-8''')
UpperCAmelCase__  :     List[str]   			=(
    subprocess.check_output(F"git diff --diff-filter=d --name-only {fork_point_sha}".split()).decode('''utf-8''').split()
)

UpperCAmelCase__  :     Tuple   			='''|'''.join(sys.argv[1:])
UpperCAmelCase__  :     List[str]   			=re.compile(rF"^({joined_dirs}).*?\.py$")

UpperCAmelCase__  :     Any   			=[x for x in modified_files if regex.match(x)]
print(''' '''.join(relevant_modified_files), end='''''')





