






from __future__ import annotations

import inspect
import unittest

from transformers import MobileViTConfig
from transformers.file_utils import is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
			import numpy as np
			import tensorflow as tf

			from transformers import TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel
			from transformers.models.mobilevit.modeling_tf_mobilevit import TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import MobileViTImageProcessor




class        snake_case_					(						UpperCamelCase_			):

						"""simple docstring"""



						def 					UpperCAmelCase__				(self:				Dict      )						->    str:




											'''simple docstring'''



											__a				:	List[str]			=				self.config_class(**self.inputs_dict      )
											self.parent.assertTrue(hasattr(__UpperCamelCase   ,				"hidden_sizes"      )      )
											self.parent.assertTrue(hasattr(__UpperCamelCase   ,				"neck_hidden_sizes"      )      )
											self.parent.assertTrue(hasattr(__UpperCamelCase   ,				"num_attention_heads"      )      )




class        snake_case_					:

						"""simple docstring"""



						def __init__(self:				Tuple   ,				__UpperCAmelCase:				Union[str, Any]   ,				__UpperCAmelCase:				str=13   ,				__UpperCAmelCase:				Union[str, Any]=32   ,				__UpperCAmelCase:				Optional[int]=2   ,				__UpperCAmelCase:				List[Any]=3   ,				__UpperCAmelCase:				Optional[int]=640   ,				__UpperCAmelCase:				Any=4   ,				__UpperCAmelCase:				Dict="silu"   ,				__UpperCAmelCase:				Optional[Any]=3   ,				__UpperCAmelCase:				int=32   ,				__UpperCAmelCase:				str=0.1   ,				__UpperCAmelCase:				Dict=0.1   ,				__UpperCAmelCase:				Optional[int]=0.1   ,				__UpperCAmelCase:				List[str]=0.02   ,				__UpperCAmelCase:				int=True   ,				__UpperCAmelCase:				Optional[Any]=True   ,				__UpperCAmelCase:				Any=10   ,				__UpperCAmelCase:				Tuple=None   ,				)						->    Optional[Any]:




											'''simple docstring'''



											__a				:	Tuple			=				parent
											__a				:	Dict			=				batch_size
											__a				:	Any			=				image_size
											__a				:	str			=				patch_size
											__a				:	Union[str, Any]			=				num_channels
											__a				:	Optional[int]			=				last_hidden_size
											__a				:	Optional[Any]			=				num_attention_heads
											__a				:	Any			=				hidden_act
											__a				:	List[str]			=				conv_kernel_size
											__a				:	str			=				output_stride
											__a				:	List[Any]			=				hidden_dropout_prob
											__a				:	Any			=				attention_probs_dropout_prob
											__a				:	List[Any]			=				classifier_dropout_prob
											__a				:	int			=				use_labels
											__a				:	Optional[Any]			=				is_training
											__a				:	Union[str, Any]			=				num_labels
											__a				:	Tuple			=				initializer_range
											__a				:	Optional[Any]			=				scope



						def 					UpperCAmelCase__				(self:				str      )						->    Optional[Any]:




											'''simple docstring'''



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

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

											__a				:	Optional[int]			=				self.get_config()

											return config, pixel_values, labels, pixel_labels



						def 					UpperCAmelCase__				(self:				List[str]      )						->    Dict:




											'''simple docstring'''



											return MobileViTConfig(
											    image_size=self.image_size   ,				patch_size=self.patch_size   ,				num_channels=self.num_channels   ,				num_attention_heads=self.num_attention_heads   ,				hidden_act=self.hidden_act   ,				conv_kernel_size=self.conv_kernel_size   ,				output_stride=self.output_stride   ,				hidden_dropout_prob=self.hidden_dropout_prob   ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,				classifier_dropout_prob=self.classifier_dropout_prob   ,				initializer_range=self.initializer_range   ,				)



						def 					UpperCAmelCase__				(self:				Any   ,				__UpperCAmelCase:				str   ,				__UpperCAmelCase:				Union[str, Any]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Optional[Any]      )						->    Union[str, Any]:




											'''simple docstring'''



											__a				:	str			=				TFMobileViTModel(config=__UpperCamelCase      )
											__a				:	List[str]			=				model(__UpperCamelCase   ,				training=__UpperCamelCase      )
											__a				:	Tuple			=				self.image_size // self.output_stride
											self.parent.assertEqual(
											    result.last_hidden_state.shape   ,				(self.batch_size, self.last_hidden_size, expected_height, expected_width)      )



						def 					UpperCAmelCase__				(self:				Tuple   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				List[Any]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				int      )						->    Any:




											'''simple docstring'''



											__a				:	List[str]			=				self.num_labels
											__a				:	List[Any]			=				TFMobileViTForImageClassification(__UpperCamelCase      )
											__a				:	Optional[Any]			=				model(__UpperCamelCase   ,				labels=__UpperCamelCase   ,				training=__UpperCamelCase      )
											self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_labels)      )



						def 					UpperCAmelCase__				(self:				Tuple   ,				__UpperCAmelCase:				str   ,				__UpperCAmelCase:				int   ,				__UpperCAmelCase:				int   ,				__UpperCAmelCase:				str      )						->    Optional[Any]:




											'''simple docstring'''



											__a				:	Tuple			=				self.num_labels
											__a				:	Tuple			=				TFMobileViTForSemanticSegmentation(__UpperCamelCase      )
											__a				:	List[Any]			=				self.image_size // self.output_stride

											__a				:	Any			=				model(__UpperCamelCase   ,				training=__UpperCamelCase      )
											self.parent.assertEqual(
											    result.logits.shape   ,				(self.batch_size, self.num_labels, expected_height, expected_width)      )

											__a				:	Dict			=				model(__UpperCamelCase   ,				labels=__UpperCamelCase   ,				training=__UpperCamelCase      )
											self.parent.assertEqual(
											    result.logits.shape   ,				(self.batch_size, self.num_labels, expected_height, expected_width)      )



						def 					UpperCAmelCase__				(self:				List[Any]      )						->    Dict:




											'''simple docstring'''



											__a				:	str			=				self.prepare_config_and_inputs()
											__a				:	Dict			=				config_and_inputs
											__a				:	List[str]			=				{'''pixel_values''': pixel_values}
											return config, inputs_dict




@require_tf
class        snake_case_					(						UpperCamelCase_		,       UpperCamelCase_		,       unittest.TestCase			):

						"""simple docstring"""



						snake_case__   					=			(
						    (TFMobileViTModel, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation)
						    if is_tf_available()
						    else ()
						)
						snake_case__   					=			(
						    {"""feature-extraction""": TFMobileViTModel, """image-classification""": TFMobileViTForImageClassification}
						    if is_tf_available()
						    else {}
						)

						snake_case__   					=			False
						snake_case__   					=			False
						snake_case__   					=			False
						snake_case__   					=			False
						snake_case__   					=			False



						def 					UpperCAmelCase__				(self:				Optional[int]      )						->    str:




											'''simple docstring'''



											__a				:	Optional[int]			=				TFMobileViTModelTester(self      )
											__a				:	Union[str, Any]			=				TFMobileViTConfigTester(self   ,				config_class=__UpperCamelCase   ,				has_text_modality=__UpperCamelCase      )



						def 					UpperCAmelCase__				(self:				int      )						->    List[str]:




											'''simple docstring'''



											self.config_tester.run_common_tests()



						@unittest.skip(reason="MobileViT does not use inputs_embeds"      )
						def 					UpperCAmelCase__				(self:				Union[str, Any]      )						->    List[str]:




											'''simple docstring'''



											pass



						@unittest.skip(reason="MobileViT does not support input and output embeddings"      )
						def 					UpperCAmelCase__				(self:				int      )						->    Optional[Any]:




											'''simple docstring'''



											pass



						@unittest.skip(reason="MobileViT does not output attentions"      )
						def 					UpperCAmelCase__				(self:				Optional[int]      )						->    Tuple:




											'''simple docstring'''



											pass



						def 					UpperCAmelCase__				(self:				Union[str, Any]      )						->    Any:




											'''simple docstring'''



											__a				:	Tuple			=				self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																__a				:	Union[str, Any]			=				model_class(__UpperCamelCase      )
																__a				:	List[Any]			=				inspect.signature(model.call      )
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																__a				:	Optional[int]			=				[*signature.parameters.keys()]

																__a				:	Union[str, Any]			=				['''pixel_values''']
																self.assertListEqual(arg_names[:1]   ,				__UpperCamelCase      )



						def 					UpperCAmelCase__				(self:				List[Any]      )						->    Union[str, Any]:




											'''simple docstring'''



											__a				:	str			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCamelCase      )



						def 					UpperCAmelCase__				(self:				Dict      )						->    Optional[Any]:




											'''simple docstring'''



											def check_hidden_states_output(__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				int      ):
																__a				:	Optional[Any]			=				model_class(__UpperCamelCase      )

																__a				:	Union[str, Any]			=				model(**self._prepare_for_class(__UpperCamelCase   ,				__UpperCamelCase      )      )

																__a				:	Tuple			=				outputs.hidden_states

																__a				:	Optional[Any]			=				5
																self.assertEqual(len(__UpperCamelCase      )   ,				__UpperCamelCase      )

																# MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
																# with the width and height being successively divided by 2.
																__a				:	Tuple			=				2
																for i in range(len(__UpperCamelCase      )      ):
																					self.assertListEqual(
																					    list(hidden_states[i].shape[-2:]      )   ,				[self.model_tester.image_size // divisor, self.model_tester.image_size // divisor]   ,				)
																					divisor *= 2

																self.assertEqual(self.model_tester.output_stride   ,				divisor // 2      )

											__a				:	List[Any]			=				self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																__a				:	List[Any]			=				True
																check_hidden_states_output(__UpperCamelCase   ,				__UpperCamelCase   ,				__UpperCamelCase      )

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																__a				:	List[str]			=				True

																check_hidden_states_output(__UpperCamelCase   ,				__UpperCamelCase   ,				__UpperCamelCase      )



						def 					UpperCAmelCase__				(self:				str      )						->    Union[str, Any]:




											'''simple docstring'''



											__a				:	Any			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase      )



						def 					UpperCAmelCase__				(self:				Optional[Any]      )						->    List[Any]:




											'''simple docstring'''



											__a				:	List[Any]			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_semantic_segmentation(*__UpperCamelCase      )



						@unittest.skipIf(
						    not is_tf_available() or len(tf.config.list_physical_devices("GPU"      )      ) == 0   ,				reason="TF does not support backprop for grouped convolutions on CPU."   ,				)
						def 					UpperCAmelCase__				(self:				Optional[Any]      )						->    List[str]:




											'''simple docstring'''



											super().test_dataset_conversion()



						def 					UpperCAmelCase__				(self:				Any   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Any   ,				__UpperCAmelCase:				Union[str, Any]=2E-1   ,				__UpperCAmelCase:				Optional[Any]=2E-1      )						->    List[str]:




											'''simple docstring'''



											self.assertTrue(np.allclose(__UpperCamelCase   ,				__UpperCamelCase   ,				atol=__UpperCamelCase   ,				rtol=__UpperCamelCase      )      )



						@unittest.skipIf(
						    not is_tf_available() or len(tf.config.list_physical_devices("GPU"      )      ) == 0   ,				reason="TF does not support backprop for grouped convolutions on CPU."   ,				)
						@slow
						def 					UpperCAmelCase__				(self:				Tuple      )						->    Tuple:




											'''simple docstring'''



											__a				:	str			=				self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																# Since `TFMobileViTModel` cannot operate with the default `fit()` method.
																if model_class.__name__ != "TFMobileViTModel":
																					__a				:	Optional[Any]			=				model_class(__UpperCamelCase      )
																					if getattr(__UpperCamelCase   ,				"hf_compute_loss"   ,				__UpperCamelCase      ):
																										super().test_keras_fit()



						def 					UpperCAmelCase__				(self:				Dict      )						->    Optional[int]:




											'''simple docstring'''



											__a				:	str			=				self.model_tester.prepare_config_and_inputs_for_common()
											for model_class in self.all_model_classes:
																# set an ignore index to correctly test the masked loss used in
																# `TFMobileViTForSemanticSegmentation`.
																if model_class.__name__ != "TFMobileViTForSemanticSegmentation":
																					__a				:	List[str]			=				5

																__a				:	Tuple			=				model_class(__UpperCamelCase      )
																if getattr(__UpperCamelCase   ,				"hf_compute_loss"   ,				__UpperCamelCase      ):
																					# The number of elements in the loss should be the same as the number of elements in the label
																					__a				:	Any			=				self._prepare_for_class(inputs_dict.copy()   ,				__UpperCamelCase   ,				return_labels=__UpperCamelCase      )
																					__a				:	Optional[Any]			=				prepared_for_class[
																					    sorted(prepared_for_class.keys() - inputs_dict.keys()   ,				reverse=__UpperCamelCase      )[0]
																					]
																					__a				:	Union[str, Any]			=				added_label.shape.as_list()[:1]

																					# Test that model correctly compute the loss with kwargs
																					__a				:	Dict			=				self._prepare_for_class(inputs_dict.copy()   ,				__UpperCamelCase   ,				return_labels=__UpperCamelCase      )
																					__a				:	str			=				{'''input_ids''', '''pixel_values''', '''input_features'''}
																					__a				:	str			=				possible_input_names.intersection(set(__UpperCamelCase      )      ).pop()
																					__a				:	Union[str, Any]			=				prepared_for_class.pop(__UpperCamelCase      )

																					__a				:	Any			=				model(__UpperCamelCase   ,				**__UpperCamelCase      )[0]
																					self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )

																					# Test that model correctly compute the loss when we mask some positions
																					__a				:	Tuple			=				self._prepare_for_class(inputs_dict.copy()   ,				__UpperCamelCase   ,				return_labels=__UpperCamelCase      )
																					__a				:	int			=				{'''input_ids''', '''pixel_values''', '''input_features'''}
																					__a				:	Tuple			=				possible_input_names.intersection(set(__UpperCamelCase      )      ).pop()
																					__a				:	Optional[int]			=				prepared_for_class.pop(__UpperCamelCase      )
																					if "labels" in prepared_for_class:
																										__a				:	Dict			=				prepared_for_class['''labels'''].numpy()
																										if len(labels.shape      ) > 1 and labels.shape[1] != 1:
																															# labels[0] = -100
																															__a				:	Any			=				tf.convert_to_tensor(__UpperCamelCase      )
																															__a				:	Optional[Any]			=				model(__UpperCamelCase   ,				**__UpperCamelCase      )[0]
																															self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )
																															self.assertTrue(not np.any(np.isnan(loss.numpy()      )      )      )

                # Test that model correctly compute the loss with a dict
																					__a				:	List[Any]			=				self._prepare_for_class(inputs_dict.copy()   ,				__UpperCamelCase   ,				return_labels=__UpperCamelCase      )
																					__a				:	List[str]			=				model(__UpperCamelCase      )[0]
																					self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )

																					# Test that model correctly compute the loss with a tuple
																					__a				:	str			=				self._prepare_for_class(inputs_dict.copy()   ,				__UpperCamelCase   ,				return_labels=__UpperCamelCase      )

																					# Get keys that were added with the _prepare_for_class function
																					__a				:	Optional[Any]			=				prepared_for_class.keys() - inputs_dict.keys()
																					__a				:	Union[str, Any]			=				inspect.signature(model.call      ).parameters
																					__a				:	Dict			=				list(signature.keys()      )

																					# Create a dictionary holding the location of the tensors in the tuple
																					__a				:	Optional[int]			=				{0: input_name}
																					for label_key in label_keys:
																										__a				:	Optional[int]			=				signature_names.index(__UpperCamelCase      )
																										__a				:	Optional[Any]			=				label_key
																					__a				:	int			=				sorted(tuple_index_mapping.items()      )
																					# Initialize a list with their default values, update the values and convert to a tuple
																					__a				:	Any			=				[]

																					for name in signature_names:
																										if name != "kwargs":
																															list_input.append(signature[name].default      )

																					for index, value in sorted_tuple_index_mapping:
																										__a				:	Tuple			=				prepared_for_class[value]

																					__a				:	List[str]			=				tuple(__UpperCamelCase      )

																					# Send to model
																					__a				:	Optional[int]			=				model(tuple_input[:-1]      )[0]

																					self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )



						@slow
						def 					UpperCAmelCase__				(self:				Optional[Any]      )						->    Any:




											'''simple docstring'''



											for model_name in TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__a				:	List[str]			=				TFMobileViTModel.from_pretrained(__UpperCamelCase      )
																self.assertIsNotNone(__UpperCamelCase      )







def 						a_      ()							->	Optional[int]:



					"""simple docstring"""
					__a				:	Dict			=				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
					return image




@require_tf
class        snake_case_					(						unittest.TestCase			):

						"""simple docstring"""



						@slow
						def 					UpperCAmelCase__				(self:				int      )						->    List[Any]:




											'''simple docstring'''



											__a				:	Union[str, Any]			=				TFMobileViTForImageClassification.from_pretrained("apple/mobilevit-xx-small"      )

											__a				:	Dict			=				MobileViTImageProcessor.from_pretrained("apple/mobilevit-xx-small"      )
											__a				:	List[Any]			=				prepare_img()
											__a				:	List[str]			=				image_processor(images=__UpperCamelCase   ,				return_tensors="tf"      )

											# forward pass
											__a				:	Any			=				model(**__UpperCamelCase   ,				training=__UpperCamelCase      )

											# verify the logits
											__a				:	Dict			=				tf.TensorShape((1, 1000)      )
											self.assertEqual(outputs.logits.shape   ,				__UpperCamelCase      )

											__a				:	List[str]			=				tf.constant([-1.93_64, -1.23_27, -0.46_53]      )

											tf.debugging.assert_near(outputs.logits[0, :3]   ,				__UpperCamelCase   ,				atol=1E-4   ,				rtol=1E-04      )



						@slow
						def 					UpperCAmelCase__				(self:				Any      )						->    int:




											'''simple docstring'''



											__a				:	Tuple			=				TFMobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"      )

											__a				:	Union[str, Any]			=				MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"      )

											__a				:	List[str]			=				prepare_img()
											__a				:	str			=				image_processor(images=__UpperCamelCase   ,				return_tensors="tf"      )

											# forward pass
											__a				:	Any			=				model(inputs.pixel_values   ,				training=__UpperCamelCase      )
											__a				:	Optional[int]			=				outputs.logits

											# verify the logits
											__a				:	List[Any]			=				tf.TensorShape((1, 21, 32, 32)      )
											self.assertEqual(logits.shape   ,				__UpperCamelCase      )

											__a				:	Tuple			=				tf.constant(
											    [
											        [[6.97_13, 6.97_86, 7.24_22], [7.28_93, 7.28_25, 7.44_46], [7.65_80, 7.87_97, 7.94_20]],
											        [[-10.68_69, -10.32_50, -10.34_71], [-10.42_28, -9.98_68, -9.71_32], [-11.04_05, -11.02_21, -10.73_18]],
											        [[-3.30_89, -2.85_39, -2.67_40], [-3.27_06, -2.56_21, -2.51_08], [-3.25_34, -2.66_15, -2.66_51]],
											    ]      )

											tf.debugging.assert_near(logits[0, :3, :3, :3]   ,				__UpperCamelCase   ,				rtol=1E-4   ,				atol=1E-4      )













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:				List[Any]   ,				__UpperCAmelCase:				str   ,				)						->    int:




											'''simple docstring'''



											__a				:	str			=				parent
											__a				:	Dict			=				13
											__a				:	Tuple			=				7
											__a				:	Union[str, Any]			=				True
											__a				:	Dict			=				True
											__a				:	Optional[Any]			=				True
											__a				:	Tuple			=				True
											__a				:	int			=				True
											__a				:	Tuple			=				False
											__a				:	Optional[int]			=				False
											__a				:	str			=				False
											__a				:	Optional[Any]			=				2
											__a				:	Optional[Any]			=				99
											__a				:	List[str]			=				0
											__a				:	int			=				32
											__a				:	List[Any]			=				2
											__a				:	Union[str, Any]			=				4
											__a				:	List[str]			=				0.1
											__a				:	Tuple			=				0.1
											__a				:	str			=				512
											__a				:	Any			=				16
											__a				:	Optional[Any]			=				2
											__a				:	str			=				0.02
											__a				:	Union[str, Any]			=				3
											__a				:	Union[str, Any]			=				4
											__a				:	Optional[int]			=				"last"
											__a				:	List[str]			=				True
											__a				:	List[Any]			=				None
											__a				:	Any			=				0



						def 					UpperCAmelCase__				(self:				int      )						->    str:




											'''simple docstring'''



											__a				:	Optional[Any]			=				ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size      )
											__a				:	Union[str, Any]			=				random_attention_mask([self.batch_size, self.seq_length]   ,				dtype=tf.floataa      )

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

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

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

											__a				:	str			=				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 					UpperCAmelCase__				(self:				int   ,				__UpperCAmelCase:				Union[str, Any]   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				List[Any]   ,				__UpperCAmelCase:				int   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Any   ,				)						->    List[Any]:




											'''simple docstring'''



											__a				:	Union[str, Any]			=				TFXLMModel(config=__UpperCAmelCase      )
											__a				:	Tuple			=				{"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
											__a				:	List[str]			=				model(__UpperCAmelCase      )

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



						def 					UpperCAmelCase__				(self:				Tuple   ,				__UpperCAmelCase:				Optional[Any]   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				int   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				Optional[Any]   ,				__UpperCAmelCase:				List[Any]   ,				__UpperCAmelCase:				List[str]   ,				)						->    Union[str, Any]:




											'''simple docstring'''



											__a				:	int			=				TFXLMWithLMHeadModel(__UpperCAmelCase      )

											__a				:	List[Any]			=				{"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
											__a				:	int			=				model(__UpperCAmelCase      )

											__a				:	int			=				outputs

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



						def 					UpperCAmelCase__				(self:				Optional[int]   ,				__UpperCAmelCase:				Any   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Optional[Any]   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Union[str, Any]   ,				__UpperCAmelCase:				Optional[Any]   ,				__UpperCAmelCase:				List[Any]   ,				)						->    List[str]:




											'''simple docstring'''



											__a				:	Tuple			=				TFXLMForQuestionAnsweringSimple(__UpperCAmelCase      )

											__a				:	Union[str, Any]			=				{"input_ids": input_ids, "lengths": input_lengths}

											__a				:	int			=				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 					UpperCAmelCase__				(self:				List[Any]   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Any   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Any   ,				__UpperCAmelCase:				str   ,				__UpperCAmelCase:				int   ,				__UpperCAmelCase:				Any   ,				__UpperCAmelCase:				Dict   ,				)						->    str:




											'''simple docstring'''



											__a				:	Dict			=				TFXLMForSequenceClassification(__UpperCAmelCase      )

											__a				:	Union[str, Any]			=				{"input_ids": input_ids, "lengths": input_lengths}

											__a				:	Any			=				model(__UpperCAmelCase      )

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



						def 					UpperCAmelCase__				(self:				Dict   ,				__UpperCAmelCase:				List[str]   ,				__UpperCAmelCase:				Optional[Any]   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				str   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				int   ,				)						->    int:




											'''simple docstring'''



											__a				:	Optional[int]			=				self.num_labels
											__a				:	Any			=				TFXLMForTokenClassification(config=__UpperCAmelCase      )
											__a				:	Dict			=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
											__a				:	List[Any]			=				model(__UpperCAmelCase      )
											self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.num_labels)      )



						def 					UpperCAmelCase__				(self:				Optional[int]   ,				__UpperCAmelCase:				Dict   ,				__UpperCAmelCase:				List[Any]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				List[Any]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Optional[int]   ,				__UpperCAmelCase:				Optional[int]   ,				)						->    List[str]:




											'''simple docstring'''



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



						def 					UpperCAmelCase__				(self:				Tuple      )						->    Optional[Any]:




											'''simple docstring'''



											__a				:	Tuple			=				self.prepare_config_and_inputs()
											(
											    (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, (
											    __a
											)			, 
											)				:	List[Any]			=				config_and_inputs
											__a				:	Optional[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_					(						__UpperCamelCase		,       __UpperCamelCase		,       unittest.TestCase			):

						"""simple docstring"""



						snake_case__   					=			(
						    (
						        TFXLMModel,
						        TFXLMWithLMHeadModel,
						        TFXLMForSequenceClassification,
						        TFXLMForQuestionAnsweringSimple,
						        TFXLMForTokenClassification,
						        TFXLMForMultipleChoice,
						    )
						    if is_tf_available()
						    else ()
						)
						snake_case__   					=			(
						    (TFXLMWithLMHeadModel,) if is_tf_available() else ()
						)  # TODO (PVP): Check other models whether language generation is also applicable
						snake_case__   					=			(
						    {
						        """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 {}
						)
						snake_case__   					=			False
						snake_case__   					=			False



						def 					UpperCAmelCase__				(self:				Union[str, Any]   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				Tuple   ,				__UpperCAmelCase:				List[Any]   ,				__UpperCAmelCase:				Union[str, Any]   ,				__UpperCAmelCase:				Tuple      )						->    Tuple:




											'''simple docstring'''



											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 					UpperCAmelCase__				(self:				Optional[Any]      )						->    int:




											'''simple docstring'''



											__a				:	List[Any]			=				TFXLMModelTester(self      )
											__a				:	int			=				ConfigTester(self   ,				config_class=__UpperCAmelCase   ,				emb_dim=37      )



						def 					UpperCAmelCase__				(self:				Tuple      )						->    Optional[Any]:




											'''simple docstring'''



											self.config_tester.run_common_tests()



						def 					UpperCAmelCase__				(self:				Any      )						->    int:




											'''simple docstring'''



											__a				:	Optional[int]			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_model(*__UpperCAmelCase      )



						def 					UpperCAmelCase__				(self:				Dict      )						->    Dict:




											'''simple docstring'''



											__a				:	str			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_lm_head(*__UpperCAmelCase      )



						def 					UpperCAmelCase__				(self:				Optional[int]      )						->    Optional[int]:




											'''simple docstring'''



											__a				:	List[str]			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_qa(*__UpperCAmelCase      )



						def 					UpperCAmelCase__				(self:				Optional[Any]      )						->    Any:




											'''simple docstring'''



											__a				:	List[Any]			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_sequence_classif(*__UpperCAmelCase      )



						def 					UpperCAmelCase__				(self:				Optional[int]      )						->    Optional[Any]:




											'''simple docstring'''



											__a				:	Any			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_for_token_classification(*__UpperCAmelCase      )



						def 					UpperCAmelCase__				(self:				Any      )						->    List[Any]:




											'''simple docstring'''



											__a				:	List[str]			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_for_multiple_choice(*__UpperCAmelCase      )



						@slow
						def 					UpperCAmelCase__				(self:				List[str]      )						->    Optional[int]:




											'''simple docstring'''



											for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__a				:	Optional[int]			=				TFXLMModel.from_pretrained(__UpperCAmelCase      )
																self.assertIsNotNone(__UpperCAmelCase      )




@require_tf
class        snake_case_					(						unittest.TestCase			):

						"""simple docstring"""



						@slow
						def 					UpperCAmelCase__				(self:				Optional[int]      )						->    str:




											'''simple docstring'''



											__a				:	Any			=				TFXLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048"      )
											__a				:	List[Any]			=				tf.convert_to_tensor([[14, 447]]   ,				dtype=tf.intaa      )  # the president
											__a				:	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
											__a				:	Tuple			=				model.generate(__UpperCAmelCase   ,				do_sample=__UpperCAmelCase      )
											self.assertListEqual(output_ids[0].numpy().tolist()   ,				__UpperCAmelCase      )





