
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):




	'''simple docstring'''




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







	def   _lowerCamelCase							(   self						:Union[str, Any]		)						->					List[Any]:
								__UpperCamelCase      :  int       =   ids_tensor([self.batch_size, self.seq_length]	,  self.vocab_size		)
								__UpperCamelCase      :  Union[str, Any]       =   random_attention_mask([self.batch_size, self.seq_length]		)

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

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

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

								__UpperCamelCase      :  List[str]       =   self.get_config()

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







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







	def   _lowerCamelCase							(   self						:str	,  a						:Optional[int]	,  a						:Dict	,  a						:Dict	,  a						:str	,  a						:Optional[int]	,  a						:Any	,  a						:int	,  a						:str	,  a						:Optional[int]	,  )						->					Tuple:
								__UpperCamelCase      :  Optional[int]       =   FlaubertWithLMHeadModel(a		)
								model.to(a		)
								model.eval()

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

								__UpperCamelCase      :  str       =   model(a		)

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

								__UpperCamelCase      :  Union[str, Any]       =   model(a		)

								__UpperCamelCase      :  Any       =   model(
								    a	,  start_positions=a	,  end_positions=a	,  cls_index=a	,  is_impossible=a	,  p_mask=a	,  )

								__UpperCamelCase      :  Optional[Any]       =   model(
								    a	,  start_positions=a	,  end_positions=a	,  cls_index=a	,  is_impossible=a	,  )

								((__UpperCamelCase)  ,		)      :  Tuple       =   result_with_labels.to_tuple()

								__UpperCamelCase      :  List[Any]       =   model(a	,  start_positions=a	,  end_positions=a		)

								((__UpperCamelCase)  ,		)      :  List[str]       =   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   _lowerCamelCase							(   self						:Union[str, Any]	,  a						:Optional[int]	,  a						:Dict	,  a						:Optional[Any]	,  a						:Any	,  a						:Tuple	,  a						:Optional[Any]	,  a						:Dict	,  a						:List[Any]	,  a						:Dict	,  )						->					Optional[Any]:
								__UpperCamelCase      :  str       =   FlaubertForSequenceClassification(a		)
								model.to(a		)
								model.eval()

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

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







	def   _lowerCamelCase							(   self						:Optional[Any]	,  a						:Union[str, Any]	,  a						:str	,  a						:Tuple	,  a						:Dict	,  a						:Tuple	,  a						:List[Any]	,  a						:Dict	,  a						:str	,  a						:Union[str, Any]	,  )						->					Union[str, Any]:
								__UpperCamelCase      :  List[Any]       =   self.num_choices
								__UpperCamelCase      :  str       =   FlaubertForMultipleChoice(config=a		)
								model.to(a		)
								model.eval()
								__UpperCamelCase      :  List[Any]       =   input_ids.unsqueeze(1		).expand(-1	,  self.num_choices	,  -1		).contiguous()
								__UpperCamelCase      :  Tuple       =   token_type_ids.unsqueeze(1		).expand(-1	,  self.num_choices	,  -1		).contiguous()
								__UpperCamelCase      :  Optional[int]       =   input_mask.unsqueeze(1		).expand(-1	,  self.num_choices	,  -1		).contiguous()
								__UpperCamelCase      :  str       =   model(
								    a	,  attention_mask=a	,  token_type_ids=a	,  labels=a	,  )
								self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.num_choices)		)







	def   _lowerCamelCase							(   self						:Any		)						->					Optional[int]:
								__UpperCamelCase      :  int       =   self.prepare_config_and_inputs()
								(
								    (
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		(
								    __UpperCamelCase
								)  ,		
								)      :  Any       =   config_and_inputs
								__UpperCamelCase      :  Optional[int]       =   {
								    "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):




	'''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   _lowerCamelCase							(   self						:Optional[int]	,  a						:Tuple	,  a						:Optional[Any]	,  a						:Dict	,  a						:Tuple	,  a						:Dict		)						->					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   _lowerCamelCase							(   self						:Dict	,  a						:Any	,  a						:int	,  a						:Optional[int]=False		)						->					Union[str, Any]:
								__UpperCamelCase      :  Tuple       =   super()._prepare_for_class(a	,  a	,  return_labels=a		)

								if return_labels:
															if model_class.__name__ == "FlaubertForQuestionAnswering":
																						__UpperCamelCase      :  Any       =   torch.zeros(
																						    self.model_tester.batch_size	,  dtype=torch.long	,  device=a		)
																						__UpperCamelCase      :  List[str]       =   torch.zeros(
																						    self.model_tester.batch_size	,  dtype=torch.long	,  device=a		)

								return inputs_dict







	def   _lowerCamelCase							(   self						:str		)						->					Optional[Any]:
								__UpperCamelCase      :  Optional[int]       =   FlaubertModelTester(self		)
								__UpperCamelCase      :  Optional[int]       =   ConfigTester(self	,  config_class=a	,  emb_dim=3_7		)







	def   _lowerCamelCase							(   self						:List[str]		)						->					Union[str, Any]:
								self.config_tester.run_common_tests()







	def   _lowerCamelCase							(   self						:Optional[Any]		)						->					Optional[Any]:
								__UpperCamelCase      :  Any       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_model(*a		)







	def   _lowerCamelCase							(   self						:Tuple		)						->					Dict:
								__UpperCamelCase      :  Union[str, Any]       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_lm_head(*a		)







	def   _lowerCamelCase							(   self						:Union[str, Any]		)						->					Any:
								__UpperCamelCase      :  Any       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_simple_qa(*a		)







	def   _lowerCamelCase							(   self						:List[Any]		)						->					str:
								__UpperCamelCase      :  Optional[int]       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_qa(*a		)







	def   _lowerCamelCase							(   self						:Dict		)						->					List[Any]:
								__UpperCamelCase      :  str       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_sequence_classif(*a		)







	def   _lowerCamelCase							(   self						:str		)						->					Tuple:
								__UpperCamelCase      :  List[str]       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_token_classif(*a		)







	def   _lowerCamelCase							(   self						:int		)						->					int:
								__UpperCamelCase      :  Tuple       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_multiple_choice(*a		)







	@slow
	def   _lowerCamelCase							(   self						:Dict		)						->					Optional[Any]:
								for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															__UpperCamelCase      :  List[Any]       =   FlaubertModel.from_pretrained(a		)
															self.assertIsNotNone(a		)







	@slow
	@require_torch_gpu
	def   _lowerCamelCase							(   self						:int		)						->					Tuple:
								__UpperCamelCase  ,		__UpperCamelCase      :  int       =   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      :  Dict       =   True
															__UpperCamelCase      :  Any       =   model_class(config=a		)

															__UpperCamelCase      :  List[str]       =   self._prepare_for_class(a	,  a		)
															__UpperCamelCase      :  Union[str, Any]       =   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"		)		)
																						__UpperCamelCase      :  Optional[int]       =   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 					lowerCamelCase__				(							unittest.TestCase):




	'''simple docstring'''




	@slow
	def   _lowerCamelCase							(   self						:Any		)						->					List[str]:
								__UpperCamelCase      :  Tuple       =   FlaubertModel.from_pretrained("flaubert/flaubert_base_cased"		)
								__UpperCamelCase      :  Tuple       =   torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]		)
								with torch.no_grad():
															__UpperCamelCase      :  Dict       =   model(a		)[0]
								__UpperCamelCase      :  str       =   torch.Size((1, 1_1, 7_6_8)		)
								self.assertEqual(output.shape	,  a		)
								__UpperCamelCase      :  Union[str, Any]       =   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]	,  a	,  atol=1E-4		)		)

from typing import List

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase    :   Union[str, Any]						=			logging.get_logger(__name__)

lowercase    :   Union[str, Any]						=			{
    'snap-research/efficientformer-l1-300': (
        'https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json'
    ),
}




class 					lowerCamelCase__				(							__lowercase):




	'''simple docstring'''




	_A								=	'efficientformer'







	def __init__(   self						:Union[str, Any]	,  a						:List[int] = [3, 2, 6, 4]	,  a						:List[int] = [4_8, 9_6, 2_2_4, 4_4_8]	,  a						:List[bool] = [True, True, True, True]	,  a						:int = 4_4_8	,  a						:int = 3_2	,  a						:int = 4	,  a						:int = 7	,  a						:int = 5	,  a						:int = 8	,  a						:int = 4	,  a						:float = 0.0	,  a						:int = 1_6	,  a						:int = 3	,  a						:int = 3	,  a						:int = 3	,  a						:int = 2	,  a						:int = 1	,  a						:float = 0.0	,  a						:int = 1	,  a						:bool = True	,  a						:bool = True	,  a						:float = 1E-5	,  a						:str = "gelu"	,  a						:float = 0.02	,  a						:float = 1E-1_2	,  a						:int = 2_2_4	,  a						:float = 1E-0_5	,  **a						:str	,  )						->					None:
								super().__init__(**a		)

								__UpperCamelCase      :  Optional[Any]       =   hidden_act
								__UpperCamelCase      :  Any       =   hidden_dropout_prob
								__UpperCamelCase      :  str       =   hidden_sizes
								__UpperCamelCase      :  Dict       =   num_hidden_layers
								__UpperCamelCase      :  Optional[Any]       =   num_attention_heads
								__UpperCamelCase      :  str       =   initializer_range
								__UpperCamelCase      :  List[str]       =   layer_norm_eps
								__UpperCamelCase      :  str       =   patch_size
								__UpperCamelCase      :  str       =   num_channels
								__UpperCamelCase      :  Any       =   depths
								__UpperCamelCase      :  Tuple       =   mlp_expansion_ratio
								__UpperCamelCase      :  List[str]       =   downsamples
								__UpperCamelCase      :  Optional[int]       =   dim
								__UpperCamelCase      :  Dict       =   key_dim
								__UpperCamelCase      :  Optional[Any]       =   attention_ratio
								__UpperCamelCase      :  Dict       =   resolution
								__UpperCamelCase      :  Union[str, Any]       =   pool_size
								__UpperCamelCase      :  Tuple       =   downsample_patch_size
								__UpperCamelCase      :  Optional[int]       =   downsample_stride
								__UpperCamelCase      :  Optional[int]       =   downsample_pad
								__UpperCamelCase      :  Union[str, Any]       =   drop_path_rate
								__UpperCamelCase      :  Union[str, Any]       =   num_metaad_blocks
								__UpperCamelCase      :  List[str]       =   distillation
								__UpperCamelCase      :  str       =   use_layer_scale
								__UpperCamelCase      :  Tuple       =   layer_scale_init_value
								__UpperCamelCase      :  str       =   image_size
								__UpperCamelCase      :  int       =   batch_norm_eps