



'''simple docstring'''







import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VivitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor


if is_torch_available():
							import torch
							from torch import nn

							from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VivitForVideoClassification, VivitModel
							from transformers.models.vivit.modeling_vivit import VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from transformers import VivitImageProcessor




class 				__lowerCAmelCase:






			def __init__(   self  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							List[str]      ,       SCREAMING_SNAKE_CASE  :							Dict=2      ,       SCREAMING_SNAKE_CASE  :							int=True      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]=True      ,       SCREAMING_SNAKE_CASE  :							Tuple=10      ,       SCREAMING_SNAKE_CASE  :							int=10      ,       SCREAMING_SNAKE_CASE  :							Any=8      ,       SCREAMING_SNAKE_CASE  :							Any=[2, 4, 4]      ,       SCREAMING_SNAKE_CASE  :							List[Any]=3      ,       SCREAMING_SNAKE_CASE  :							str=768      ,       SCREAMING_SNAKE_CASE  :							List[Any]=5      ,       SCREAMING_SNAKE_CASE  :							Any=4      ,       SCREAMING_SNAKE_CASE  :							str=37      ,       SCREAMING_SNAKE_CASE  :							Any="gelu_fast"      ,       SCREAMING_SNAKE_CASE  :							Optional[int]=0.0      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]=0.0      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]=0.02      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]=1E-06      ,       SCREAMING_SNAKE_CASE  :							Any=True      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]=None      ,       ):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Dict										=      parent
					SCREAMING_SNAKE_CASE_						:str										=      batch_size
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      is_training
					SCREAMING_SNAKE_CASE_						:Any										=      use_labels
					SCREAMING_SNAKE_CASE_						:Any										=      num_labels
					SCREAMING_SNAKE_CASE_						:str										=      image_size
					SCREAMING_SNAKE_CASE_						:str										=      num_frames
					SCREAMING_SNAKE_CASE_						:List[Any]										=      tubelet_size
					SCREAMING_SNAKE_CASE_						:int										=      num_channels
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      hidden_size
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      num_hidden_layers
					SCREAMING_SNAKE_CASE_						:int										=      num_attention_heads
					SCREAMING_SNAKE_CASE_						:str										=      intermediate_size
					SCREAMING_SNAKE_CASE_						:int										=      hidden_act
					SCREAMING_SNAKE_CASE_						:Tuple										=      hidden_dropout_prob
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      attention_probs_dropout_prob
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      initializer_range
					SCREAMING_SNAKE_CASE_						:Dict										=      layer_norm_eps
					SCREAMING_SNAKE_CASE_						:str										=      qkv_bias
					SCREAMING_SNAKE_CASE_						:List[str]										=      scope

					SCREAMING_SNAKE_CASE_						:str										=      (
					    (self.image_size // self.tubelet_size[2])
					    * (self.image_size // self.tubelet_size[1])
					    * (self.num_frames // self.tubelet_size[0])
					) + 1  # CLS token






			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Dict										=      floats_tensor(
					    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]	)

					SCREAMING_SNAKE_CASE_						:Tuple										=      None
					if self.use_labels:
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      ids_tensor([self.batch_size]      ,       self.num_labels	)

					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      self.get_config()

					return config, pixel_values, labels






			def 				_lowercase	(   self  :							Dict	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      VivitConfig(
					    num_frames=self.num_frames      ,       image_size=self.image_size      ,       tubelet_size=self.tubelet_size      ,       num_channels=self.num_channels      ,       hidden_size=self.hidden_size      ,       num_hidden_layers=self.num_hidden_layers      ,       num_attention_heads=self.num_attention_heads      ,       intermediate_size=self.intermediate_size      ,       hidden_act=self.hidden_act      ,       hidden_dropout_prob=self.hidden_dropout_prob      ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,       initializer_range=self.initializer_range      ,       layer_norm_eps=self.layer_norm_eps      ,       qkv_bias=self.qkv_bias      ,       )
					SCREAMING_SNAKE_CASE_						:Any										=      self.num_labels
					return config






			def 				_lowercase	(   self  :							Any      ,       SCREAMING_SNAKE_CASE  :							Any      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							Tuple	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[int]										=      VivitModel(config=SCREAMING_SNAKE_CASE	)
					model.to(SCREAMING_SNAKE_CASE	)
					model.eval()
					SCREAMING_SNAKE_CASE_						:List[str]										=      model(SCREAMING_SNAKE_CASE	)
					self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)	)






			def 				_lowercase	(   self  :							int      ,       SCREAMING_SNAKE_CASE  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							int	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:List[Any]										=      VivitForVideoClassification(SCREAMING_SNAKE_CASE	)
					model.to(SCREAMING_SNAKE_CASE	)
					model.eval()

					SCREAMING_SNAKE_CASE_						:Tuple										=      model(SCREAMING_SNAKE_CASE	)

					# verify the logits shape
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      torch.Size((self.batch_size, self.num_labels)	)
					self.parent.assertEqual(result.logits.shape      ,       SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							Optional[Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:str										=      self.prepare_config_and_inputs()
					SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						:List[Any]										=      config_and_inputs
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      {'pixel_values': pixel_values}
					return config, inputs_dict




@require_torch
class 				__lowerCAmelCase(							lowerCAmelCase__		,			unittest.TestCase      ):
			__snake_case  :		Any		=      (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

			__snake_case  :		Dict		=      False
			__snake_case  :		List[Any]		=      False
			__snake_case  :		List[str]		=      False
			__snake_case  :		Dict		=      False






			def 				_lowercase	(   self  :							Tuple	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:str										=      VivitModelTester(self	)
					SCREAMING_SNAKE_CASE_						:Tuple										=      ConfigTester(self      ,       config_class=SCREAMING_SNAKE_CASE      ,       has_text_modality=SCREAMING_SNAKE_CASE      ,       hidden_size=37	)






			def 				_lowercase	(   self  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							str      ,       SCREAMING_SNAKE_CASE  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							Any=False	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[int]										=      copy.deepcopy(SCREAMING_SNAKE_CASE	)

					if return_labels:
							if model_class in get_values(SCREAMING_SNAKE_CASE	):
									SCREAMING_SNAKE_CASE_						:Dict										=      torch.zeros(
									    self.model_tester.batch_size      ,       dtype=torch.long      ,       device=SCREAMING_SNAKE_CASE	)

					return inputs_dict






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					self.config_tester.run_common_tests()






			@unittest.skip(reason='Vivit does not use inputs_embeds'	)
			def 				_lowercase	(   self  :							Dict	):




					"""simple docstring"""







					pass






			def 				_lowercase	(   self  :							List[str]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						:Tuple										=      self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							SCREAMING_SNAKE_CASE_						:Dict										=      model_class(SCREAMING_SNAKE_CASE	)
							self.assertIsInstance(model.get_input_embeddings()      ,       (nn.Module)	)
							SCREAMING_SNAKE_CASE_						:Any										=      model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE      ,       nn.Linear	)	)






			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      model_class(SCREAMING_SNAKE_CASE	)
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      inspect.signature(model.forward	)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							SCREAMING_SNAKE_CASE_						:str										=      [*signature.parameters.keys()]

							SCREAMING_SNAKE_CASE_						:Any										=      ['pixel_values', 'head_mask']
							self.assertListEqual(arg_names[:2]      ,       SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							Any	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							Tuple	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Any										=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_video_classification(*SCREAMING_SNAKE_CASE	)






			@slow
			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							SCREAMING_SNAKE_CASE_						:str										=      VivitModel.from_pretrained(SCREAMING_SNAKE_CASE	)
							self.assertIsNotNone(SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.model_tester.prepare_config_and_inputs_for_common()
					SCREAMING_SNAKE_CASE_						:Tuple										=      True

					for model_class in self.all_model_classes:
							SCREAMING_SNAKE_CASE_						:Tuple										=      self.model_tester.seq_length

							SCREAMING_SNAKE_CASE_						:List[str]										=      True
							SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      False
							SCREAMING_SNAKE_CASE_						:Optional[int]										=      True
							SCREAMING_SNAKE_CASE_						:Dict										=      model_class(SCREAMING_SNAKE_CASE	)
							model.to(SCREAMING_SNAKE_CASE	)
							model.eval()
							with torch.no_grad():
									SCREAMING_SNAKE_CASE_						:Any										=      model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,       SCREAMING_SNAKE_CASE	)	)
							SCREAMING_SNAKE_CASE_						:Any										=      outputs.attentions
							self.assertEqual(len(SCREAMING_SNAKE_CASE	)      ,       self.model_tester.num_hidden_layers	)

							# check that output_attentions also work using config
							del inputs_dict["output_attentions"]
							SCREAMING_SNAKE_CASE_						:int										=      True
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      model_class(SCREAMING_SNAKE_CASE	)
							model.to(SCREAMING_SNAKE_CASE	)
							model.eval()
							with torch.no_grad():
									SCREAMING_SNAKE_CASE_						:int										=      model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,       SCREAMING_SNAKE_CASE	)	)
							SCREAMING_SNAKE_CASE_						:int										=      outputs.attentions
							self.assertEqual(len(SCREAMING_SNAKE_CASE	)      ,       self.model_tester.num_hidden_layers	)

							self.assertListEqual(
							    list(attentions[0].shape[-3:]	)      ,       [self.model_tester.num_attention_heads, seq_len, seq_len]      ,       )
							SCREAMING_SNAKE_CASE_						:Any										=      len(SCREAMING_SNAKE_CASE	)

							# Check attention is always last and order is fine
							SCREAMING_SNAKE_CASE_						:str										=      True
							SCREAMING_SNAKE_CASE_						:Optional[int]										=      True
							SCREAMING_SNAKE_CASE_						:str										=      model_class(SCREAMING_SNAKE_CASE	)
							model.to(SCREAMING_SNAKE_CASE	)
							model.eval()
							with torch.no_grad():
									SCREAMING_SNAKE_CASE_						:Any										=      model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,       SCREAMING_SNAKE_CASE	)	)

							self.assertEqual(out_len + 1      ,       len(SCREAMING_SNAKE_CASE	)	)

							SCREAMING_SNAKE_CASE_						:Dict										=      outputs.attentions

							self.assertEqual(len(SCREAMING_SNAKE_CASE	)      ,       self.model_tester.num_hidden_layers	)

							self.assertListEqual(
							    list(self_attentions[0].shape[-3:]	)      ,       [self.model_tester.num_attention_heads, seq_len, seq_len]      ,       )






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					def check_hidden_states_output(SCREAMING_SNAKE_CASE  :							int      ,       SCREAMING_SNAKE_CASE  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							Tuple	):
							SCREAMING_SNAKE_CASE_						:List[Any]										=      model_class(SCREAMING_SNAKE_CASE	)
							model.to(SCREAMING_SNAKE_CASE	)
							model.eval()

							with torch.no_grad():
									SCREAMING_SNAKE_CASE_						:Optional[int]										=      model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,       SCREAMING_SNAKE_CASE	)	)

							SCREAMING_SNAKE_CASE_						:int										=      outputs.hidden_states
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.model_tester.num_hidden_layers + 1
							self.assertEqual(len(SCREAMING_SNAKE_CASE	)      ,       SCREAMING_SNAKE_CASE	)

							SCREAMING_SNAKE_CASE_						:Dict										=      self.model_tester.seq_length

							self.assertListEqual(
							    list(hidden_states[0].shape[-2:]	)      ,       [seq_length, self.model_tester.hidden_size]      ,       )

					SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						:List[str]										=      self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							SCREAMING_SNAKE_CASE_						:Dict										=      True
							check_hidden_states_output(SCREAMING_SNAKE_CASE      ,       SCREAMING_SNAKE_CASE      ,       SCREAMING_SNAKE_CASE	)

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      True

							check_hidden_states_output(SCREAMING_SNAKE_CASE      ,       SCREAMING_SNAKE_CASE      ,       SCREAMING_SNAKE_CASE	)






			@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'	)
			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					pass







def  SCREAMING_SNAKE_CASE__ (				):
		SCREAMING_SNAKE_CASE_						:Tuple										=      hf_hub_download(
		    repo_id='hf-internal-testing/spaghetti-video' ,		filename='eating_spaghetti_32_frames.npy' ,		repo_type='dataset'     )
		SCREAMING_SNAKE_CASE_						:Any										=      np.load(SCREAMING_SNAKE_CASE     )
		return list(SCREAMING_SNAKE_CASE     )





@require_torch
@require_vision
class 				__lowerCAmelCase(							unittest.TestCase      ):






			@cached_property
			def 				_lowercase	(   self  :							Tuple	):




					"""simple docstring"""







					return VivitImageProcessor() if is_vision_available() else None






			@slow
			def 				_lowercase	(   self  :							Dict	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Tuple										=      VivitForVideoClassification.from_pretrained('google/vivit-b-16x2-kinetics400'	).to(SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:Tuple										=      self.default_image_processor
					SCREAMING_SNAKE_CASE_						:str										=      prepare_video()
					SCREAMING_SNAKE_CASE_						:Any										=      image_processor(SCREAMING_SNAKE_CASE      ,       return_tensors='pt'	).to(SCREAMING_SNAKE_CASE	)

					# forward pass
					with torch.no_grad():
							SCREAMING_SNAKE_CASE_						:Any										=      model(**SCREAMING_SNAKE_CASE	)

					# verify the logits
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      torch.Size((1, 400)	)
					self.assertEqual(outputs.logits.shape      ,       SCREAMING_SNAKE_CASE	)

					# taken from original model
					SCREAMING_SNAKE_CASE_						:int										=      torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58]	).to(SCREAMING_SNAKE_CASE	)

					self.assertTrue(torch.allclose(outputs.logits[0, :5]      ,       SCREAMING_SNAKE_CASE      ,       atol=1E-4	)	)


'''simple docstring'''






from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers import (
							    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TFXLMForMultipleChoice,
							    TFXLMForQuestionAnsweringSimple,
							    TFXLMForSequenceClassification,
							    TFXLMForTokenClassification,
							    TFXLMModel,
							    TFXLMWithLMHeadModel,
							    XLMConfig,
							)

class 							_snake_case     :
	"""simple docstring"""





	def __init__(						self				: str					,						UpperCamelCase_				: Union[str, Any]					,						):
			lowerCAmelCase_   :       Optional[Any]    							=parent
			lowerCAmelCase_   :       Any    							=13
			lowerCAmelCase_   :       str    							=7
			lowerCAmelCase_   :       Optional[Any]    							=True
			lowerCAmelCase_   :       Optional[int]    							=True
			lowerCAmelCase_   :       List[str]    							=True
			lowerCAmelCase_   :       Optional[int]    							=True
			lowerCAmelCase_   :       Dict    							=True
			lowerCAmelCase_   :       Union[str, Any]    							=False
			lowerCAmelCase_   :       List[Any]    							=False
			lowerCAmelCase_   :       Optional[int]    							=False
			lowerCAmelCase_   :       Union[str, Any]    							=2
			lowerCAmelCase_   :       List[str]    							=99
			lowerCAmelCase_   :       Optional[int]    							=0
			lowerCAmelCase_   :       Optional[int]    							=32
			lowerCAmelCase_   :       Dict    							=2
			lowerCAmelCase_   :       Dict    							=4
			lowerCAmelCase_   :       Tuple    							=0.1
			lowerCAmelCase_   :       Tuple    							=0.1
			lowerCAmelCase_   :       Tuple    							=512
			lowerCAmelCase_   :       Dict    							=16
			lowerCAmelCase_   :       List[Any]    							=2
			lowerCAmelCase_   :       Optional[int]    							=0.0_2
			lowerCAmelCase_   :       int    							=3
			lowerCAmelCase_   :       int    							=4
			lowerCAmelCase_   :       Union[str, Any]    							='''last'''
			lowerCAmelCase_   :       Optional[int]    							=True
			lowerCAmelCase_   :       Any    							=None
			lowerCAmelCase_   :       int    							=0




	def 	__A      (						self				: Dict      ):
			lowerCAmelCase_   :       List[Any]    							=ids_tensor([self.batch_size, self.seq_length]					,						self.vocab_size      )
			lowerCAmelCase_   :       Union[str, Any]    							=random_attention_mask([self.batch_size, self.seq_length]					,						dtype=tf.floataa      )

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

			lowerCAmelCase_   :       str    							=None
			if self.use_token_type_ids:
					lowerCAmelCase_   :       Optional[Any]    							=ids_tensor([self.batch_size, self.seq_length]					,						self.n_langs      )

			lowerCAmelCase_   :       Optional[int]    							=None
			lowerCAmelCase_   :       str    							=None
			lowerCAmelCase_   :       Dict    							=None
			if self.use_labels:
					lowerCAmelCase_   :       int    							=ids_tensor([self.batch_size]					,						self.type_sequence_label_size      )
					lowerCAmelCase_   :       int    							=ids_tensor([self.batch_size, self.seq_length]					,						self.num_labels      )
					lowerCAmelCase_   :       int    							=ids_tensor([self.batch_size]					,						2					,						dtype=tf.floataa      )
					lowerCAmelCase_   :       Optional[Any]    							=ids_tensor([self.batch_size]					,						self.num_choices      )

			lowerCAmelCase_   :       Tuple    							=XLMConfig(
			    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					,						bos_token_id=self.bos_token_id					,						)

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




	def 	__A      (						self				: Any					,						UpperCamelCase_				: Dict					,						UpperCamelCase_				: List[str]					,						UpperCamelCase_				: Dict					,						UpperCamelCase_				: List[str]					,						UpperCamelCase_				: int					,						UpperCamelCase_				: int					,						UpperCamelCase_				: int					,						UpperCamelCase_				: int					,						UpperCamelCase_				: Tuple					,						):
			lowerCAmelCase_   :       int    							=TFXLMModel(config=UpperCamelCase_      )
			lowerCAmelCase_   :       Optional[int]    							={'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
			lowerCAmelCase_   :       Union[str, Any]    							=model(UpperCamelCase_      )

			lowerCAmelCase_   :       Union[str, Any]    							=[input_ids, input_mask]
			lowerCAmelCase_   :       List[Any]    							=model(UpperCamelCase_      )
			self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, self.seq_length, self.hidden_size)      )




	def 	__A      (						self				: List[Any]					,						UpperCamelCase_				: List[str]					,						UpperCamelCase_				: Optional[Any]					,						UpperCamelCase_				: Optional[Any]					,						UpperCamelCase_				: Union[str, Any]					,						UpperCamelCase_				: Tuple					,						UpperCamelCase_				: List[str]					,						UpperCamelCase_				: Optional[Any]					,						UpperCamelCase_				: int					,						UpperCamelCase_				: List[Any]					,						):
			lowerCAmelCase_   :       List[str]    							=TFXLMWithLMHeadModel(UpperCamelCase_      )

			lowerCAmelCase_   :       List[Any]    							={'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
			lowerCAmelCase_   :       Tuple    							=model(UpperCamelCase_      )

			lowerCAmelCase_   :       int    							=outputs

			self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.vocab_size)      )




	def 	__A      (						self				: int					,						UpperCamelCase_				: Union[str, Any]					,						UpperCamelCase_				: Tuple					,						UpperCamelCase_				: Dict					,						UpperCamelCase_				: Optional[int]					,						UpperCamelCase_				: Any					,						UpperCamelCase_				: Optional[Any]					,						UpperCamelCase_				: Dict					,						UpperCamelCase_				: str					,						UpperCamelCase_				: Optional[int]					,						):
			lowerCAmelCase_   :       Optional[Any]    							=TFXLMForQuestionAnsweringSimple(UpperCamelCase_      )

			lowerCAmelCase_   :       str    							={'''input_ids''': input_ids, '''lengths''': input_lengths}

			lowerCAmelCase_   :       Optional[Any]    							=model(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 	__A      (						self				: Tuple					,						UpperCamelCase_				: Any					,						UpperCamelCase_				: Optional[int]					,						UpperCamelCase_				: str					,						UpperCamelCase_				: Optional[Any]					,						UpperCamelCase_				: List[Any]					,						UpperCamelCase_				: Optional[Any]					,						UpperCamelCase_				: Optional[int]					,						UpperCamelCase_				: Tuple					,						UpperCamelCase_				: Optional[int]					,						):
			lowerCAmelCase_   :       List[Any]    							=TFXLMForSequenceClassification(UpperCamelCase_      )

			lowerCAmelCase_   :       List[Any]    							={'''input_ids''': input_ids, '''lengths''': input_lengths}

			lowerCAmelCase_   :       int    							=model(UpperCamelCase_      )

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




	def 	__A      (						self				: List[str]					,						UpperCamelCase_				: int					,						UpperCamelCase_				: int					,						UpperCamelCase_				: Any					,						UpperCamelCase_				: int					,						UpperCamelCase_				: int					,						UpperCamelCase_				: Optional[int]					,						UpperCamelCase_				: List[str]					,						UpperCamelCase_				: int					,						UpperCamelCase_				: Optional[int]					,						):
			lowerCAmelCase_   :       Optional[Any]    							=self.num_labels
			lowerCAmelCase_   :       int    							=TFXLMForTokenClassification(config=UpperCamelCase_      )
			lowerCAmelCase_   :       Optional[int]    							={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
			lowerCAmelCase_   :       str    							=model(UpperCamelCase_      )
			self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.num_labels)      )




	def 	__A      (						self				: int					,						UpperCamelCase_				: int					,						UpperCamelCase_				: Dict					,						UpperCamelCase_				: Dict					,						UpperCamelCase_				: str					,						UpperCamelCase_				: Tuple					,						UpperCamelCase_				: Union[str, Any]					,						UpperCamelCase_				: Optional[Any]					,						UpperCamelCase_				: Union[str, Any]					,						UpperCamelCase_				: Optional[int]					,						):
			lowerCAmelCase_   :       List[str]    							=self.num_choices
			lowerCAmelCase_   :       Tuple    							=TFXLMForMultipleChoice(config=UpperCamelCase_      )
			lowerCAmelCase_   :       str    							=tf.tile(tf.expand_dims(UpperCamelCase_					,						1      )					,						(1, self.num_choices, 1)      )
			lowerCAmelCase_   :       Union[str, Any]    							=tf.tile(tf.expand_dims(UpperCamelCase_					,						1      )					,						(1, self.num_choices, 1)      )
			lowerCAmelCase_   :       Any    							=tf.tile(tf.expand_dims(UpperCamelCase_					,						1      )					,						(1, self.num_choices, 1)      )
			lowerCAmelCase_   :       Optional[Any]    							={
			    '''input_ids''': multiple_choice_inputs_ids,
			    '''attention_mask''': multiple_choice_input_mask,
			    '''token_type_ids''': multiple_choice_token_type_ids,
			}
			lowerCAmelCase_   :       Any    							=model(UpperCamelCase_      )
			self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_choices)      )





	def 	__A      (						self				: Union[str, Any]      ):
			lowerCAmelCase_   :       List[Any]    							=self.prepare_config_and_inputs()
			(
			    (
			    lowerCAmelCase_
			)							,		(
			    lowerCAmelCase_
			)							,		(
			    lowerCAmelCase_
			)							,		(
			    lowerCAmelCase_
			)							,		(
			    lowerCAmelCase_
			)							,		(
			    lowerCAmelCase_
			)							,		(
			    lowerCAmelCase_
			)							,		(
			    lowerCAmelCase_
			)							,		(
			    lowerCAmelCase_
			)							,		
			)   :       List[str]    							=config_and_inputs
			lowerCAmelCase_   :       List[Any]    							={
			    '''input_ids''': input_ids,
			    '''token_type_ids''': token_type_ids,
			    '''langs''': token_type_ids,
			    '''lengths''': input_lengths,
			}
			return config, inputs_dict







@require_tf
class 							_snake_case     (  lowerCAmelCase_						,    lowerCAmelCase_						,    unittest.TestCase ):
	"""simple docstring"""





	_UpperCamelCase							:  Union[str, Any]								=		(
	    (
	        TFXLMModel,
	        TFXLMWithLMHeadModel,
	        TFXLMForSequenceClassification,
	        TFXLMForQuestionAnsweringSimple,
	        TFXLMForTokenClassification,
	        TFXLMForMultipleChoice,
	    )
	    if is_tf_available()
	    else ()
	)
	_UpperCamelCase							:  Union[str, Any]								=		(
	    (TFXLMWithLMHeadModel,) if is_tf_available() else ()
	)  # TODO (PVP): Check other models whether language generation is also applicable
	_UpperCamelCase							:  List[str]								=		(
	    {
	        '''feature-extraction''': TFXLMModel,
	        '''fill-mask''': TFXLMWithLMHeadModel,
	        '''question-answering''': TFXLMForQuestionAnsweringSimple,
	        '''text-classification''': TFXLMForSequenceClassification,
	        '''text-generation''': TFXLMWithLMHeadModel,
	        '''token-classification''': TFXLMForTokenClassification,
	        '''zero-shot''': TFXLMForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	_UpperCamelCase							:  Optional[int]								=		False
	_UpperCamelCase							:  int								=		False




	def 	__A      (						self				: Optional[Any]					,						UpperCamelCase_				: Dict					,						UpperCamelCase_				: Union[str, Any]					,						UpperCamelCase_				: int					,						UpperCamelCase_				: Optional[Any]					,						UpperCamelCase_				: 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				: List[Any]      ):
			lowerCAmelCase_   :       Union[str, Any]    							=TFXLMModelTester(self      )
			lowerCAmelCase_   :       Optional[Any]    							=ConfigTester(self					,						config_class=UpperCamelCase_					,						emb_dim=37      )




	def 	__A      (						self				: Union[str, Any]      ):
			self.config_tester.run_common_tests()




	def 	__A      (						self				: int      ):
			lowerCAmelCase_   :       List[str]    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_model(*UpperCamelCase_      )




	def 	__A      (						self				: Optional[int]      ):
			lowerCAmelCase_   :       Optional[int]    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_lm_head(*UpperCamelCase_      )




	def 	__A      (						self				: Optional[Any]      ):
			lowerCAmelCase_   :       int    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_qa(*UpperCamelCase_      )




	def 	__A      (						self				: int      ):
			lowerCAmelCase_   :       Optional[int]    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_sequence_classif(*UpperCamelCase_      )




	def 	__A      (						self				: Optional[int]      ):
			lowerCAmelCase_   :       Optional[int]    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_for_token_classification(*UpperCamelCase_      )




	def 	__A      (						self				: List[Any]      ):
			lowerCAmelCase_   :       List[str]    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCamelCase_      )





	@slow
	def 	__A      (						self				: List[Any]      ):
			for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCAmelCase_   :       List[str]    							=TFXLMModel.from_pretrained(UpperCamelCase_      )
					self.assertIsNotNone(UpperCamelCase_      )







@require_tf
class 							_snake_case     (  unittest.TestCase ):
	"""simple docstring"""





	@slow
	def 	__A      (						self				: Dict      ):
			lowerCAmelCase_   :       Any    							=TFXLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048'''      )
			lowerCAmelCase_   :       List[str]    							=tf.convert_to_tensor([[14, 447]]					,						dtype=tf.intaa      )  # the president
			lowerCAmelCase_   :       List[Any]    							=[
			    14,
			    447,
			    14,
			    447,
			    14,
			    447,
			    14,
			    447,
			    14,
			    447,
			    14,
			    447,
			    14,
			    447,
			    14,
			    447,
			    14,
			    447,
			    14,
			    447,
			]  # the president the president the president the president the president the president the president the president the president the president
			# TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
			lowerCAmelCase_   :       Optional[int]    							=model.generate(UpperCamelCase_					,						do_sample=UpperCamelCase_      )
			self.assertListEqual(output_ids[0].numpy().tolist()					,						UpperCamelCase_      )






