


'''simple docstring'''


from __future__ import annotations

import copy
import os
import tempfile
from importlib import import_module
from math import isnan

from transformers import is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import _tf_gpu_memory_limit, require_tf, slow

from ..test_modeling_tf_common import ids_tensor


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

				from transformers import (
				    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
				    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
				    TF_MODEL_FOR_MASKED_LM_MAPPING,
				    TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
				    TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING,
				    TF_MODEL_FOR_PRETRAINING_MAPPING,
				    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
				    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
				    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
				    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
				    TFSharedEmbeddings,
				)

				if _tf_gpu_memory_limit is not None:
								UpperCAmelCase_	:     str							     =	tf.config.list_physical_devices('GPU')
								for gpu in gpus:
												# Restrict TensorFlow to only allocate x GB of memory on the GPUs
												try:
																tf.config.set_logical_device_configuration(
																    gpu, [tf.config.LogicalDeviceConfiguration(memory_limit=_tf_gpu_memory_limit)]
																)
																UpperCAmelCase_	:     Any							     =	tf.config.list_logical_devices('GPU')
																print('Logical GPUs', logical_gpus)
												except RuntimeError as e:
																# Virtual devices must be set before GPUs have been initialized
																print(e)





@require_tf
class 					lowercase__						:





							'''simple docstring'''


							A_							:   int   			=						None
							A_							:   Any   			=						()
							A_							:   Tuple   			=						()
							A_							:   Union[str, Any]   			=						True
							A_							:   str   			=						True
							A_							:   Any   			=						True
							A_							:   List[Any]   			=						False






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case=False						):
											_SCREAMING_SNAKE_CASE				: int  	=   copy.deepcopy(__snake_case						)

											if model_class in get_values(__snake_case						):
															_SCREAMING_SNAKE_CASE				: Dict  	=   {
															    k: tf.tile(tf.expand_dims(__snake_case					,      1						)					,      (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)						)
															    if isinstance(__snake_case					,      tf.Tensor						) and v.ndim > 0
															    else v
															    for k, v in inputs_dict.items()
															}

											if return_labels:
															if model_class in get_values(__snake_case						):
																			_SCREAMING_SNAKE_CASE				: Optional[int]  	=   tf.ones(self.model_tester.batch_size					,      dtype=tf.intaa						)
															elif model_class in get_values(__snake_case						):
																			_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   tf.zeros(self.model_tester.batch_size					,      dtype=tf.intaa						)
																			_SCREAMING_SNAKE_CASE				: List[Any]  	=   tf.zeros(self.model_tester.batch_size					,      dtype=tf.intaa						)
															elif model_class in [
															    *get_values(__snake_case						),
															    *get_values(__snake_case						),
															]:
																			_SCREAMING_SNAKE_CASE				: Optional[int]  	=   tf.zeros(self.model_tester.batch_size					,      dtype=tf.intaa						)
															elif model_class in get_values(__snake_case						):
																			_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   tf.zeros(self.model_tester.batch_size					,      dtype=tf.intaa						)
															elif model_class in [
															    *get_values(__snake_case						),
															    *get_values(__snake_case						),
															    *get_values(__snake_case						),
															    *get_values(__snake_case						),
															    *get_values(__snake_case						),
															]:
																			_SCREAMING_SNAKE_CASE				: Optional[int]  	=   tf.zeros(
																			    (self.model_tester.batch_size, self.model_tester.seq_length)					,      dtype=tf.intaa						)
											return inputs_dict






							@slow
							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: str  	=   self.model_tester.prepare_config_and_inputs_for_common()
											for model_class in self.all_model_classes[:2]:
															_SCREAMING_SNAKE_CASE				: List[str]  	=   self._prepare_for_class(__snake_case					,      __snake_case						)
															_SCREAMING_SNAKE_CASE				: Any  	=   model_class(__snake_case						)

															@tf.function
															def run_in_graph_mode():
																			return model(__snake_case						)

															_SCREAMING_SNAKE_CASE				: int  	=   run_in_graph_mode()
															self.assertIsNotNone(__snake_case						)






							@slow
							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: List[Any]  	=   self.model_tester.prepare_config_and_inputs_for_common()
											for model_class in self.all_model_classes[:2]:
															_SCREAMING_SNAKE_CASE				: Dict  	=   self._prepare_for_class(__snake_case					,      __snake_case						)
															_SCREAMING_SNAKE_CASE				: Optional[int]  	=   model_class(__snake_case						)

															@tf.function(experimental_compile=__snake_case						)
															def run_in_graph_mode():
																			return model(__snake_case						)

															_SCREAMING_SNAKE_CASE				: Optional[int]  	=   run_in_graph_mode()
															self.assertIsNotNone(__snake_case						)






							@slow
							def UpperCAmelCase_			(					self						):
											# This is a copy of the test_keras_fit method, but we use XLA compilation instead of eager
											_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: int  	=   self.model_tester.prepare_config_and_inputs_for_common()
											for model_class in self.all_model_classes[:2]:
															_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   model_class(__snake_case						)
															if getattr(__snake_case					,      """hf_compute_loss"""					,      __snake_case						):
																			# Test that model correctly compute the loss with kwargs
																			_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   self._prepare_for_class(inputs_dict.copy()					,      __snake_case					,      return_labels=__snake_case						)
																			# Is there a better way to remove these decoder inputs?
																			_SCREAMING_SNAKE_CASE				: Tuple  	=   {
																			    key: val
																			    for key, val in prepared_for_class.items()
																			    if key not in ("""head_mask""", """decoder_head_mask""", """cross_attn_head_mask""", """decoder_input_ids""")
																			}

																			_SCREAMING_SNAKE_CASE				: Tuple  	=   {
																			    """labels""",
																			    """label""",
																			    """label_ids""",
																			    """start_positions""",
																			    """start_position""",
																			    """end_positions""",
																			    """end_position""",
																			    """next_sentence_label""",
																			}
																			_SCREAMING_SNAKE_CASE				: Optional[int]  	=   possible_label_cols.intersection(set(__snake_case						)						)
																			self.assertGreater(len(__snake_case						)					,      0					,      msg="""No matching label names found!"""						)
																			_SCREAMING_SNAKE_CASE				: int  	=   {key: val for key, val in prepared_for_class.items() if key in label_names}
																			_SCREAMING_SNAKE_CASE				: Tuple  	=   {key: val for key, val in prepared_for_class.items() if key not in label_names}
																			self.assertGreater(len(__snake_case						)					,      0						)

																			# Make sure it works with XLA!
																			model.compile(optimizer=tf.keras.optimizers.SGD(0.0						)					,      jit_compile=__snake_case						)
																			# Make sure the model fits without crashing regardless of where we pass the labels
																			_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   model.fit(
																			    __snake_case					,      validation_data=__snake_case					,      steps_per_epoch=1					,      validation_steps=1					,      shuffle=__snake_case					,      verbose=0					,      )
																			_SCREAMING_SNAKE_CASE				: int  	=   history.history["""loss"""][0]
																			self.assertTrue(not isnan(__snake_case						)						)
																			_SCREAMING_SNAKE_CASE				: List[Any]  	=   history.history["""val_loss"""][0]
																			self.assertTrue(not isnan(__snake_case						)						)

																			# Now test it with separate labels, to make sure that path works in XLA too.
																			_SCREAMING_SNAKE_CASE				: Optional[int]  	=   model_class(__snake_case						)
																			model.compile(optimizer=tf.keras.optimizers.SGD(0.0						)					,      jit_compile=__snake_case						)
																			_SCREAMING_SNAKE_CASE				: List[Any]  	=   model.fit(
																			    __snake_case					,      __snake_case					,      validation_data=(inputs_minus_labels, labels)					,      steps_per_epoch=1					,      validation_steps=1					,      shuffle=__snake_case					,      verbose=0					,      )

																			_SCREAMING_SNAKE_CASE				: Optional[int]  	=   history.history["""loss"""][0]
																			self.assertTrue(not isnan(__snake_case						)						)
																			_SCREAMING_SNAKE_CASE				: Optional[int]  	=   history.history["""val_loss"""][0]
																			self.assertTrue(not isnan(__snake_case						)						)






							@slow
							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: str  	=   self.model_tester.prepare_config_and_inputs_for_common()
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   True
											_SCREAMING_SNAKE_CASE				: Any  	=   True

											if hasattr(__snake_case					,      """use_cache"""						):
															_SCREAMING_SNAKE_CASE				: List[Any]  	=   True

											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   getattr(self.model_tester					,      """encoder_seq_length"""					,      self.model_tester.seq_length						)
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   getattr(self.model_tester					,      """key_length"""					,      __snake_case						)

											for model_class in self.all_model_classes[:2]:
															_SCREAMING_SNAKE_CASE				: Any  	=   self._prepare_for_class(__snake_case					,      __snake_case						)
															_SCREAMING_SNAKE_CASE				: Tuple  	=   model_class(__snake_case						)
															model.build()
															_SCREAMING_SNAKE_CASE				: Tuple  	=   len(model(__snake_case						)						)

															for key in list(class_inputs_dict.keys()						):
																			# Remove keys not in the serving signature, as the SavedModel will not be compiled to deal with them
																			if key not in model.input_signature:
																							del class_inputs_dict[key]
																			# Check it's a tensor, in case the inputs dict has some bools in it too
																			elif isinstance(class_inputs_dict[key]					,      tf.Tensor						) and class_inputs_dict[key].dtype.is_integer:
																							_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   tf.cast(class_inputs_dict[key]					,      tf.intaa						)

															if set(class_inputs_dict.keys()						) != set(model.input_signature.keys()						):
																			continue  # Some models have inputs that the preparation functions don't create, we skip those

															with tempfile.TemporaryDirectory() as tmpdirname:
																			model.save_pretrained(__snake_case					,      saved_model=__snake_case						)
																			_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   os.path.join(__snake_case					,      """saved_model"""					,      """1"""						)
																			_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   tf.keras.models.load_model(__snake_case						)
																			_SCREAMING_SNAKE_CASE				: str  	=   model(__snake_case						)

																			if self.is_encoder_decoder:
																							_SCREAMING_SNAKE_CASE				: List[Any]  	=   outputs["""encoder_hidden_states"""]
																							_SCREAMING_SNAKE_CASE				: List[Any]  	=   outputs["""encoder_attentions"""]
																			else:
																							_SCREAMING_SNAKE_CASE				: Tuple  	=   outputs["""hidden_states"""]
																							_SCREAMING_SNAKE_CASE				: str  	=   outputs["""attentions"""]

																			self.assertEqual(len(__snake_case						)					,      __snake_case						)

																			_SCREAMING_SNAKE_CASE				: Tuple  	=   getattr(
																			    self.model_tester					,      """expected_num_hidden_layers"""					,      self.model_tester.num_hidden_layers + 1						)

																			self.assertEqual(len(__snake_case						)					,      __snake_case						)
																			self.assertListEqual(
																			    list(output_hidden_states[0].shape[-2:]						)					,      [self.model_tester.seq_length, self.model_tester.hidden_size]					,      )

																			self.assertEqual(len(__snake_case						)					,      self.model_tester.num_hidden_layers						)
																			self.assertListEqual(
																			    list(output_attentions[0].shape[-3:]						)					,      [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]					,      )






							@slow
							def UpperCAmelCase_			(					self						):
											tf.keras.mixed_precision.set_global_policy("""mixed_float16"""						)

											# try/finally block to ensure subsequent tests run in float32
											try:
															_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: Dict  	=   self.model_tester.prepare_config_and_inputs_for_common()
															for model_class in self.all_model_classes[:2]:
																			_SCREAMING_SNAKE_CASE				: Dict  	=   self._prepare_for_class(__snake_case					,      __snake_case						)
																			_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   model_class(__snake_case						)
																			_SCREAMING_SNAKE_CASE				: Any  	=   model(__snake_case						)

																			self.assertIsNotNone(__snake_case						)
											finally:
															tf.keras.mixed_precision.set_global_policy("""float32"""						)






							@slow
							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: Dict  	=   self.model_tester.prepare_config_and_inputs_for_common()
											# head_mask and decoder_head_mask has different shapes than other input args
											if "head_mask" in inputs_dict:
															del inputs_dict["head_mask"]
											if "decoder_head_mask" in inputs_dict:
															del inputs_dict["decoder_head_mask"]
											if "cross_attn_head_mask" in inputs_dict:
															del inputs_dict["cross_attn_head_mask"]
											_SCREAMING_SNAKE_CASE				: Dict  	=   {
											    module_member
											    for model_class in self.all_model_classes
											    for module in (import_module(model_class.__module__						),)
											    for module_member_name in dir(__snake_case						)
											    if module_member_name.endswith("""MainLayer"""						)
											    for module_member in (getattr(__snake_case					,      __snake_case						),)
											    if isinstance(__snake_case					,      __snake_case						)
											    and tf.keras.layers.Layer in module_member.__bases__
											    and getattr(__snake_case					,      """_keras_serializable"""					,      __snake_case						)
											}

											for main_layer_class in tf_main_layer_classes:
															# T5MainLayer needs an embed_tokens parameter when called without the inputs_embeds parameter
															if "T5" in main_layer_class.__name__:
																			# Take the same values than in TFT5ModelTester for this shared layer
																			_SCREAMING_SNAKE_CASE				: int  	=   TFSharedEmbeddings(self.model_tester.vocab_size					,      self.model_tester.hidden_size					,      name="""shared"""						)
																			_SCREAMING_SNAKE_CASE				: Optional[int]  	=   False
																			_SCREAMING_SNAKE_CASE				: int  	=   main_layer_class(__snake_case					,      embed_tokens=__snake_case						)
															else:
																			_SCREAMING_SNAKE_CASE				: Any  	=   main_layer_class(__snake_case						)

															_SCREAMING_SNAKE_CASE				: List[str]  	=   {
															    name: tf.keras.Input(tensor.shape[1:]					,      dtype=tensor.dtype						) for name, tensor in inputs_dict.items()
															}

															if hasattr(self.model_tester					,      """num_labels"""						):
																			_SCREAMING_SNAKE_CASE				: Tuple  	=   self.model_tester.num_labels
															else:
																			_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   2

															_SCREAMING_SNAKE_CASE				: List[str]  	=   tf.data.Dataset.from_tensor_slices(
															    (inputs_dict, np.ones((self.model_tester.batch_size, self.model_tester.seq_length, num_labels, 1)						))						).batch(1						)

															_SCREAMING_SNAKE_CASE				: Tuple  	=   main_layer(__snake_case						)[0]
															_SCREAMING_SNAKE_CASE				: List[Any]  	=   tf.keras.layers.Dense(__snake_case					,      activation="""softmax"""					,      name="""outputs"""						)(__snake_case						)
															_SCREAMING_SNAKE_CASE				: str  	=   tf.keras.models.Model(inputs=__snake_case					,      outputs=[outputs]						)

															model.compile(loss="""binary_crossentropy"""					,      optimizer="""adam"""					,      metrics=["""binary_accuracy"""]						)
															model.fit(__snake_case					,      epochs=1						)

															with tempfile.TemporaryDirectory() as tmpdirname:
																			_SCREAMING_SNAKE_CASE				: List[Any]  	=   os.path.join(__snake_case					,      """keras_model.h5"""						)
																			model.save(__snake_case						)
																			if "T5" in main_layer_class.__name__:
																							_SCREAMING_SNAKE_CASE				: Tuple  	=   tf.keras.models.load_model(
																							    __snake_case					,      custom_objects={
																							        main_layer_class.__name__: main_layer_class,
																							        """TFSharedEmbeddings""": TFSharedEmbeddings,
																							    }					,      )
																			else:
																							_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   tf.keras.models.load_model(
																							    __snake_case					,      custom_objects={main_layer_class.__name__: main_layer_class}						)
																			assert isinstance(__snake_case					,      tf.keras.Model						)
																			model(__snake_case						)






							@slow
							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE	, _SCREAMING_SNAKE_CASE				: Any  	=   self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes[:2]:
															_SCREAMING_SNAKE_CASE				: Dict  	=   model_class(__snake_case						)

															_SCREAMING_SNAKE_CASE				: int  	=   copy.deepcopy(__snake_case						)

															if not self.is_encoder_decoder:
																			_SCREAMING_SNAKE_CASE				: Tuple  	=   inputs["""input_ids"""]
																			del inputs["input_ids"]
															else:
																			_SCREAMING_SNAKE_CASE				: List[str]  	=   inputs["""input_ids"""]
																			_SCREAMING_SNAKE_CASE				: str  	=   inputs.get("""decoder_input_ids"""					,      __snake_case						)
																			del inputs["input_ids"]
																			inputs.pop("""decoder_input_ids"""					,      __snake_case						)

															if not self.is_encoder_decoder:
																			_SCREAMING_SNAKE_CASE				: Any  	=   model.get_input_embeddings()(__snake_case						)
															else:
																			_SCREAMING_SNAKE_CASE				: Any  	=   model.get_input_embeddings()(__snake_case						)
																			_SCREAMING_SNAKE_CASE				: Tuple  	=   model.get_input_embeddings()(__snake_case						)

															_SCREAMING_SNAKE_CASE				: List[Any]  	=   self._prepare_for_class(__snake_case					,      __snake_case						)

															@tf.function
															def run_in_graph_mode():
																			return model(__snake_case						)

															_SCREAMING_SNAKE_CASE				: Optional[int]  	=   run_in_graph_mode()
															self.assertIsNotNone(__snake_case						)






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case						):
											# special tokens cannot be bad tokens
											_SCREAMING_SNAKE_CASE				: Tuple  	=   []
											if model.config.bos_token_id is not None:
															special_tokens.append(model.config.bos_token_id						)
											if model.config.pad_token_id is not None:
															special_tokens.append(model.config.pad_token_id						)
											if model.config.eos_token_id is not None:
															special_tokens.append(model.config.eos_token_id						)

											# create random bad tokens that are not special tokens
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   []
											while len(__snake_case						) < num_bad_tokens:
															_SCREAMING_SNAKE_CASE				: Optional[int]  	=   tf.squeeze(ids_tensor((1, 1)					,      self.model_tester.vocab_size						)					,      0						).numpy()[0]
															if token not in special_tokens:
																			bad_tokens.append(__snake_case						)
											return bad_tokens






							def UpperCAmelCase_			(					self					,      __snake_case						):
											for token_id in output_ids[0].numpy().tolist():
															self.assertGreaterEqual(__snake_case					,      0						)
															self.assertLess(__snake_case					,      self.model_tester.vocab_size						)






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case						):
											# for all bad word tokens
											for bad_word_ids in bad_words_ids:
															# for all slices in batch
															for generated_ids_slice in generated_ids:
																			# for all word idx
																			for i in range(len(__snake_case						)					,      len(__snake_case						)						):
																							# if tokens match
																							if generated_ids_slice[i - len(__snake_case						) : i] == bad_word_ids:
																											return True
											return False








'''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 					lowercase__						:





							'''simple docstring'''


							def __init__(					self					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Dict  	=   parent
											_SCREAMING_SNAKE_CASE				: Any  	=   13
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   7
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   True
											_SCREAMING_SNAKE_CASE				: str  	=   True
											_SCREAMING_SNAKE_CASE				: Dict  	=   True
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   True
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   True
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   False
											_SCREAMING_SNAKE_CASE				: Tuple  	=   False
											_SCREAMING_SNAKE_CASE				: Any  	=   False
											_SCREAMING_SNAKE_CASE				: Dict  	=   2
											_SCREAMING_SNAKE_CASE				: str  	=   99
											_SCREAMING_SNAKE_CASE				: Any  	=   0
											_SCREAMING_SNAKE_CASE				: List[str]  	=   32
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   2
											_SCREAMING_SNAKE_CASE				: List[str]  	=   4
											_SCREAMING_SNAKE_CASE				: List[str]  	=   0.1
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   0.1
											_SCREAMING_SNAKE_CASE				: int  	=   512
											_SCREAMING_SNAKE_CASE				: List[str]  	=   16
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   2
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   0.02
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   3
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   4
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   """last"""
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   True
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   None
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   0






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size						)
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   random_attention_mask([self.batch_size, self.seq_length]					,      dtype=tf.floataa						)

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

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

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

											_SCREAMING_SNAKE_CASE				: Dict  	=   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					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   TFXLMModel(config=__snake_case						)
											_SCREAMING_SNAKE_CASE				: int  	=   {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
											_SCREAMING_SNAKE_CASE				: int  	=   model(__snake_case						)

											_SCREAMING_SNAKE_CASE				: str  	=   [input_ids, input_mask]
											_SCREAMING_SNAKE_CASE				: str  	=   model(__snake_case						)
											self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)						)






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   TFXLMWithLMHeadModel(__snake_case						)

											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   model(__snake_case						)

											_SCREAMING_SNAKE_CASE				: str  	=   outputs

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






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: List[str]  	=   TFXLMForQuestionAnsweringSimple(__snake_case						)

											_SCREAMING_SNAKE_CASE				: Any  	=   {"""input_ids""": input_ids, """lengths""": input_lengths}

											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   model(__snake_case						)

											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					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Dict  	=   TFXLMForSequenceClassification(__snake_case						)

											_SCREAMING_SNAKE_CASE				: int  	=   {"""input_ids""": input_ids, """lengths""": input_lengths}

											_SCREAMING_SNAKE_CASE				: List[str]  	=   model(__snake_case						)

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






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      ):
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   self.num_labels
											_SCREAMING_SNAKE_CASE				: Dict  	=   TFXLMForTokenClassification(config=__snake_case						)
											_SCREAMING_SNAKE_CASE				: Any  	=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
											_SCREAMING_SNAKE_CASE				: Dict  	=   model(__snake_case						)
											self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.num_labels)						)






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






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   self.prepare_config_and_inputs()
											(
											    (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, (
											    _SCREAMING_SNAKE_CASE
											)	, 
											)				: Tuple  	=   config_and_inputs
											_SCREAMING_SNAKE_CASE				: Tuple  	=   {
											    """input_ids""": input_ids,
											    """token_type_ids""": token_type_ids,
											    """langs""": token_type_ids,
											    """lengths""": input_lengths,
											}
											return config, inputs_dict





@require_tf
class 					lowercase__						(		_snake_case  ,  _snake_case  ,  unittest.TestCase      ):





							'''simple docstring'''


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






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case						):
											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						):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   TFXLMModelTester(self						)
											_SCREAMING_SNAKE_CASE				: List[str]  	=   ConfigTester(self					,      config_class=__snake_case					,      emb_dim=37						)






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






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: int  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_model(*__snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Dict  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_lm_head(*__snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_qa(*__snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_sequence_classif(*__snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_for_token_classification(*__snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Dict  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_for_multiple_choice(*__snake_case						)






							@slow
							def UpperCAmelCase_			(					self						):
											for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_SCREAMING_SNAKE_CASE				: int  	=   TFXLMModel.from_pretrained(__snake_case						)
															self.assertIsNotNone(__snake_case						)





@require_tf
class 					lowercase__						(		unittest.TestCase      ):





							'''simple docstring'''


							@slow
							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   TFXLMWithLMHeadModel.from_pretrained("""xlm-mlm-en-2048"""						)
											_SCREAMING_SNAKE_CASE				: Any  	=   tf.convert_to_tensor([[14, 447]]					,      dtype=tf.intaa						)  # the president
											_SCREAMING_SNAKE_CASE				: Union[str, 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
											_SCREAMING_SNAKE_CASE				: Dict  	=   model.generate(__snake_case					,      do_sample=__snake_case						)
											self.assertListEqual(output_ids[0].numpy().tolist()					,      __snake_case						)




