





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


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


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

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

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

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

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


					def 	lowerCamelCase       (					self			:List[Any]     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:str     ,       ):
												A		=  FlaubertWithLMHeadModel(__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()

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

												A		=  model(__UpperCamelCase		)

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

												A		=  model(__UpperCamelCase		)

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

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

												((A),	)		=  result_with_labels.to_tuple()

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

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

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

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


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


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







@require_torch
class 					_UpperCAmelCase   (	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 	lowerCamelCase       (					self			:str     ,       __UpperCamelCase			:Union[str, Any]     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:int		):
												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			:Tuple     ,       __UpperCamelCase			:Union[str, Any]     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:List[str]=False		):
												A		=  super()._prepare_for_class(__UpperCamelCase     ,       __UpperCamelCase     ,       return_labels=__UpperCamelCase		)

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

												return inputs_dict


					def 	lowerCamelCase       (					self			:List[Any]		):
												A		=  FlaubertModelTester(self		)
												A		=  ConfigTester(self     ,       config_class=__UpperCamelCase     ,       emb_dim=37		)


					def 	lowerCamelCase       (					self			:Optional[int]		):
												self.config_tester.run_common_tests()


					def 	lowerCamelCase       (					self			:Optional[Any]		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_model(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Optional[Any]		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_lm_head(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Optional[Any]		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_simple_qa(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Tuple		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_qa(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Optional[int]		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_sequence_classif(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Union[str, Any]		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_token_classif(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Any		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_multiple_choice(*__UpperCamelCase		)


					@slow
					def 	lowerCamelCase       (					self			:List[Any]		):
												for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			A		=  FlaubertModel.from_pretrained(__UpperCamelCase		)
																			self.assertIsNotNone(__UpperCamelCase		)


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

																			A		=  True
																			A		=  model_class(config=__UpperCamelCase		)

																			A		=  self._prepare_for_class(__UpperCamelCase     ,       __UpperCamelCase		)
																			A		=  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"		)		)
																										A		=  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 					_UpperCAmelCase   (	unittest.TestCase ):


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









"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


_snake_case     :		Optional[int]       						= logging.get_logger(__name__)

_snake_case     :		Optional[int]       						= {
    'google/vivit-b-16x2-kinetics400': (
        'https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json'
    ),
    # See all Vivit models at https://huggingface.co/models?filter=vivit
}






class 					_UpperCAmelCase   (	lowercase_ ):
					UpperCamelCase      =							'''vivit'''


					def __init__(					self			:Optional[Any]     ,       __UpperCamelCase			:Dict=2_24     ,       __UpperCamelCase			:int=32     ,       __UpperCamelCase			:Union[str, Any]=[2, 16, 16]     ,       __UpperCamelCase			:Optional[Any]=3     ,       __UpperCamelCase			:Optional[Any]=7_68     ,       __UpperCamelCase			:Any=12     ,       __UpperCamelCase			:List[str]=12     ,       __UpperCamelCase			:List[str]=30_72     ,       __UpperCamelCase			:Any="gelu_fast"     ,       __UpperCamelCase			:List[Any]=0.0     ,       __UpperCamelCase			:str=0.0     ,       __UpperCamelCase			:Dict=0.02     ,       __UpperCamelCase			:Optional[Any]=1e-06     ,       __UpperCamelCase			:Dict=True     ,       **__UpperCamelCase			:Tuple     ,       ):
												A		=  hidden_size
												A		=  num_hidden_layers
												A		=  num_attention_heads
												A		=  intermediate_size
												A		=  hidden_act
												A		=  hidden_dropout_prob
												A		=  attention_probs_dropout_prob
												A		=  initializer_range
												A		=  layer_norm_eps

												A		=  image_size
												A		=  num_frames
												A		=  tubelet_size
												A		=  num_channels
												A		=  qkv_bias

												super().__init__(**__UpperCamelCase		)


