





"""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 				_a		:






				def __init__(				self	:					Any,				lowerCAmelCase__	:					List[Any],				)			->   Optional[int]:




										'''simple docstring'''



										_UpperCamelCase    :      Any						     =					parent
										_UpperCamelCase    :      Optional[int]						     =					1_3
										_UpperCamelCase    :      List[str]						     =					7
										_UpperCamelCase    :      Optional[int]						     =					True
										_UpperCamelCase    :      Dict						     =					True
										_UpperCamelCase    :      Optional[Any]						     =					True
										_UpperCamelCase    :      Optional[int]						     =					True
										_UpperCamelCase    :      Tuple						     =					True
										_UpperCamelCase    :      str						     =					False
										_UpperCamelCase    :      List[Any]						     =					False
										_UpperCamelCase    :      Union[str, Any]						     =					False
										_UpperCamelCase    :      List[Any]						     =					2
										_UpperCamelCase    :      Any						     =					9_9
										_UpperCamelCase    :      Optional[int]						     =					0
										_UpperCamelCase    :      List[Any]						     =					3_2
										_UpperCamelCase    :      Dict						     =					2
										_UpperCamelCase    :      List[str]						     =					4
										_UpperCamelCase    :      Dict						     =					0.1
										_UpperCamelCase    :      Any						     =					0.1
										_UpperCamelCase    :      Union[str, Any]						     =					5_1_2
										_UpperCamelCase    :      int						     =					1_6
										_UpperCamelCase    :      Dict						     =					2
										_UpperCamelCase    :      Tuple						     =					0.02
										_UpperCamelCase    :      str						     =					3
										_UpperCamelCase    :      Dict						     =					4
										_UpperCamelCase    :      List[Any]						     =					'''last'''
										_UpperCamelCase    :      List[str]						     =					True
										_UpperCamelCase    :      str						     =					None
										_UpperCamelCase    :      Dict						     =					0






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




										'''simple docstring'''



										_UpperCamelCase    :      int						     =					ids_tensor([self.batch_size, self.seq_length],				self.vocab_size							)
										_UpperCamelCase    :      Any						     =					random_attention_mask([self.batch_size, self.seq_length],				dtype=tf.floataa							)

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

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

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

										_UpperCamelCase    :      Optional[int]						     =					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        snake_case			(				self	:					int,				lowerCAmelCase__	:					Any,				lowerCAmelCase__	:					List[str],				lowerCAmelCase__	:					Tuple,				lowerCAmelCase__	:					List[str],				lowerCAmelCase__	:					Optional[Any],				lowerCAmelCase__	:					Optional[Any],				lowerCAmelCase__	:					int,				lowerCAmelCase__	:					Optional[int],				lowerCAmelCase__	:					Optional[int],				)			->   Optional[int]:




										'''simple docstring'''



										_UpperCamelCase    :      List[str]						     =					TFXLMModel(config=lowerCAmelCase__							)
										_UpperCamelCase    :      Union[str, Any]						     =					{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
										_UpperCamelCase    :      Optional[Any]						     =					model(lowerCAmelCase__							)

										_UpperCamelCase    :      Optional[int]						     =					[input_ids, input_mask]
										_UpperCamelCase    :      Optional[int]						     =					model(lowerCAmelCase__							)
										self.parent.assertEqual(result.last_hidden_state.shape,				(self.batch_size, self.seq_length, self.hidden_size)							)






				def        snake_case			(				self	:					Union[str, Any],				lowerCAmelCase__	:					Tuple,				lowerCAmelCase__	:					int,				lowerCAmelCase__	:					int,				lowerCAmelCase__	:					Union[str, Any],				lowerCAmelCase__	:					str,				lowerCAmelCase__	:					Dict,				lowerCAmelCase__	:					Optional[Any],				lowerCAmelCase__	:					int,				lowerCAmelCase__	:					Optional[int],				)			->   Dict:




										'''simple docstring'''



										_UpperCamelCase    :      List[Any]						     =					TFXLMWithLMHeadModel(lowerCAmelCase__							)

										_UpperCamelCase    :      Dict						     =					{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
										_UpperCamelCase    :      Tuple						     =					model(lowerCAmelCase__							)

										_UpperCamelCase    :      Any						     =					outputs

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






				def        snake_case			(				self	:					Tuple,				lowerCAmelCase__	:					Union[str, Any],				lowerCAmelCase__	:					Optional[int],				lowerCAmelCase__	:					Union[str, Any],				lowerCAmelCase__	:					Tuple,				lowerCAmelCase__	:					Any,				lowerCAmelCase__	:					int,				lowerCAmelCase__	:					Dict,				lowerCAmelCase__	:					Union[str, Any],				lowerCAmelCase__	:					Tuple,				)			->   int:




										'''simple docstring'''



										_UpperCamelCase    :      str						     =					TFXLMForQuestionAnsweringSimple(lowerCAmelCase__							)

										_UpperCamelCase    :      Optional[Any]						     =					{'''input_ids''': input_ids, '''lengths''': input_lengths}

										_UpperCamelCase    :      Optional[Any]						     =					model(lowerCAmelCase__							)

										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        snake_case			(				self	:					Union[str, Any],				lowerCAmelCase__	:					List[str],				lowerCAmelCase__	:					int,				lowerCAmelCase__	:					Union[str, Any],				lowerCAmelCase__	:					int,				lowerCAmelCase__	:					List[Any],				lowerCAmelCase__	:					Optional[Any],				lowerCAmelCase__	:					Tuple,				lowerCAmelCase__	:					List[str],				lowerCAmelCase__	:					List[Any],				)			->   Dict:




										'''simple docstring'''



										_UpperCamelCase    :      Tuple						     =					TFXLMForSequenceClassification(lowerCAmelCase__							)

										_UpperCamelCase    :      Tuple						     =					{'''input_ids''': input_ids, '''lengths''': input_lengths}

										_UpperCamelCase    :      List[Any]						     =					model(lowerCAmelCase__							)

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






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




										'''simple docstring'''



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






				def        snake_case			(				self	:					Optional[Any],				lowerCAmelCase__	:					List[Any],				lowerCAmelCase__	:					Dict,				lowerCAmelCase__	:					Any,				lowerCAmelCase__	:					Any,				lowerCAmelCase__	:					Tuple,				lowerCAmelCase__	:					Union[str, Any],				lowerCAmelCase__	:					Union[str, Any],				lowerCAmelCase__	:					str,				lowerCAmelCase__	:					List[Any],				)			->   Dict:




										'''simple docstring'''



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






				def        snake_case			(				self	:					List[Any]							)			->   Tuple:




										'''simple docstring'''



										_UpperCamelCase    :      Optional[int]						     =					self.prepare_config_and_inputs()
										(
										    _UpperCamelCase							
										)    :      int						     =					config_and_inputs
										_UpperCamelCase    :      Optional[int]						     =					{
										    '''input_ids''': input_ids,
										    '''token_type_ids''': token_type_ids,
										    '''langs''': token_type_ids,
										    '''lengths''': input_lengths,
										}
										return config, inputs_dict




@require_tf
class 				_a		(   _lowerCAmelCase			,       _lowerCAmelCase			,       unittest.TestCase       ):
				UpperCamelCase			=					(
				    (
				        TFXLMModel,
				        TFXLMWithLMHeadModel,
				        TFXLMForSequenceClassification,
				        TFXLMForQuestionAnsweringSimple,
				        TFXLMForTokenClassification,
				        TFXLMForMultipleChoice,
				    )
				    if is_tf_available()
				    else ()
				)
				UpperCamelCase			=					(
				    (TFXLMWithLMHeadModel,) if is_tf_available() else ()
				)  # TODO (PVP): Check other models whether language generation is also applicable
				UpperCamelCase			=					(
				    {
				        '''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			=					False
				UpperCamelCase			=					False






				def        snake_case			(				self	:					Tuple,				lowerCAmelCase__	:					Tuple,				lowerCAmelCase__	:					Optional[Any],				lowerCAmelCase__	:					int,				lowerCAmelCase__	:					Dict,				lowerCAmelCase__	:					Any							)			->   str:




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




										'''simple docstring'''



										_UpperCamelCase    :      Optional[Any]						     =					TFXLMModelTester(self							)
										_UpperCamelCase    :      List[Any]						     =					ConfigTester(self,				config_class=lowerCAmelCase__,				emb_dim=3_7							)






				def        snake_case			(				self	:					List[Any]							)			->   Tuple:




										'''simple docstring'''



										self.config_tester.run_common_tests()






				def        snake_case			(				self	:					Dict							)			->   Tuple:




										'''simple docstring'''



										_UpperCamelCase    :      str						     =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_model(*lowerCAmelCase__							)






				def        snake_case			(				self	:					Dict							)			->   Tuple:




										'''simple docstring'''



										_UpperCamelCase    :      Dict						     =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_lm_head(*lowerCAmelCase__							)






				def        snake_case			(				self	:					str							)			->   List[Any]:




										'''simple docstring'''



										_UpperCamelCase    :      str						     =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_qa(*lowerCAmelCase__							)






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




										'''simple docstring'''



										_UpperCamelCase    :      str						     =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_sequence_classif(*lowerCAmelCase__							)






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




										'''simple docstring'''



										_UpperCamelCase    :      Optional[int]						     =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_for_token_classification(*lowerCAmelCase__							)






				def        snake_case			(				self	:					Any							)			->   Tuple:




										'''simple docstring'''



										_UpperCamelCase    :      Any						     =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_for_multiple_choice(*lowerCAmelCase__							)






				@slow
				def        snake_case			(				self	:					Any							)			->   Dict:




										'''simple docstring'''



										for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_UpperCamelCase    :      int						     =					TFXLMModel.from_pretrained(lowerCAmelCase__							)
																self.assertIsNotNone(lowerCAmelCase__							)




@require_tf
class 				_a		(   unittest.TestCase       ):






				@slow
				def        snake_case			(				self	:					List[str]							)			->   str:




										'''simple docstring'''



										_UpperCamelCase    :      Union[str, Any]						     =					TFXLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048'''							)
										_UpperCamelCase    :      int						     =					tf.convert_to_tensor([[1_4, 4_4_7]],				dtype=tf.intaa							)  # the president
										_UpperCamelCase    :      Dict						     =					[
										    1_4,
										    4_4_7,
										    1_4,
										    4_4_7,
										    1_4,
										    4_4_7,
										    1_4,
										    4_4_7,
										    1_4,
										    4_4_7,
										    1_4,
										    4_4_7,
										    1_4,
										    4_4_7,
										    1_4,
										    4_4_7,
										    1_4,
										    4_4_7,
										    1_4,
										    4_4_7,
										]  # 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
										_UpperCamelCase    :      Tuple						     =					model.generate(lowerCAmelCase__,				do_sample=lowerCAmelCase__							)
										self.assertListEqual(output_ids[0].numpy().tolist(),				lowerCAmelCase__							)










"""simple docstring"""




def  a_       (							_lowercase     ):
						if len(_lowercase     ) > 1:
												_UpperCamelCase    :      List[str]						     =					len(_lowercase     ) // 2  # Finds the middle of the array
												_UpperCamelCase    :      Dict						     =					arr[
												    :middle_length
												]  # Creates an array of the elements in the first half.
												_UpperCamelCase    :      Union[str, Any]						     =					arr[
												    middle_length:
												]  # Creates an array of the elements in the second half.
												_UpperCamelCase    :      str						     =					len(_lowercase     )
												_UpperCamelCase    :      List[str]						     =					len(_lowercase     )
												merge(_lowercase     )  # Starts sorting the left.
												merge(_lowercase     )  # Starts sorting the right
												_UpperCamelCase    :      Dict						     =					0  # Left Counter
												_UpperCamelCase    :      Union[str, Any]						     =					0  # Right Counter
												_UpperCamelCase    :      Optional[int]						     =					0  # Position Counter
												while (
												    left_index < left_size and right_index < right_size
												):  # Runs until the lowers size of the left and right are sorted.
																		if left_array[left_index] < right_array[right_index]:
																								_UpperCamelCase    :      Dict						     =					left_array[left_index]
																								left_index += 1
																		else:
																								_UpperCamelCase    :      Tuple						     =					right_array[right_index]
																								right_index += 1
																		index += 1
												while (
												    left_index < left_size
												):  # Adds the left over elements in the left half of the array
																		_UpperCamelCase    :      int						     =					left_array[left_index]
																		left_index += 1
																		index += 1
												while (
												    right_index < right_size
												):  # Adds the left over elements in the right half of the array
																		_UpperCamelCase    :      Any						     =					right_array[right_index]
																		right_index += 1
																		index += 1
						return arr


if __name__ == "__main__":
						import doctest

						doctest.testmod()



