


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      :Dict      ,      lowerCAmelCase__      :int      ,      )		->      str:
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		parent
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		13
										__SCREAMING_SNAKE_CASE						:					Any		      =		7
										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		True
										__SCREAMING_SNAKE_CASE						:					Dict		      =		True
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		True
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		True
										__SCREAMING_SNAKE_CASE						:					Any		      =		True
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		False
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		False
										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		False
										__SCREAMING_SNAKE_CASE						:					str		      =		2
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		99
										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		0
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		32
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		2
										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		4
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		0.1
										__SCREAMING_SNAKE_CASE						:					str		      =		0.1
										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		512
										__SCREAMING_SNAKE_CASE						:					int		      =		16
										__SCREAMING_SNAKE_CASE						:					Dict		      =		2
										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		0.02
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		3
										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		4
										__SCREAMING_SNAKE_CASE						:					Any		      =		'''last'''
										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		True
										__SCREAMING_SNAKE_CASE						:					str		      =		None
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		0


				def 		__magic_name__(	self      :Union[str, Any]     )		->      Any:
										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		ids_tensor([self.batch_size, self.seq_length]      ,      self.vocab_size     )
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		random_attention_mask([self.batch_size, self.seq_length]      ,      dtype=tf.floataa     )

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

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

										__SCREAMING_SNAKE_CASE						:					List[str]		      =		None
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		None
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		None
										if self.use_labels:
																__SCREAMING_SNAKE_CASE						:					int		      =		ids_tensor([self.batch_size]      ,      self.type_sequence_label_size     )
																__SCREAMING_SNAKE_CASE						:					Tuple		      =		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						:					Dict		      =		ids_tensor([self.batch_size]      ,      self.num_choices     )

										__SCREAMING_SNAKE_CASE						:					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 		__magic_name__(	self      :Tuple      ,      lowerCAmelCase__      :Optional[int]      ,      lowerCAmelCase__      :Optional[int]      ,      lowerCAmelCase__      :Optional[int]      ,      lowerCAmelCase__      :List[Any]      ,      lowerCAmelCase__      :Optional[int]      ,      lowerCAmelCase__      :str      ,      lowerCAmelCase__      :Any      ,      lowerCAmelCase__      :Any      ,      lowerCAmelCase__      :str      ,      )		->      List[Any]:
										__SCREAMING_SNAKE_CASE						:					str		      =		TFXLMModel(config=lowerCAmelCase__     )
										__SCREAMING_SNAKE_CASE						:					List[str]		      =		{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		model(lowerCAmelCase__     )

										__SCREAMING_SNAKE_CASE						:					Dict		      =		[input_ids, input_mask]
										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		model(lowerCAmelCase__     )
										self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, self.seq_length, self.hidden_size)     )


				def 		__magic_name__(	self      :Optional[Any]      ,      lowerCAmelCase__      :Dict      ,      lowerCAmelCase__      :Union[str, Any]      ,      lowerCAmelCase__      :Any      ,      lowerCAmelCase__      :List[Any]      ,      lowerCAmelCase__      :Dict      ,      lowerCAmelCase__      :Optional[Any]      ,      lowerCAmelCase__      :Union[str, Any]      ,      lowerCAmelCase__      :Optional[Any]      ,      lowerCAmelCase__      :int      ,      )		->      Optional[Any]:
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		TFXLMWithLMHeadModel(lowerCAmelCase__     )

										__SCREAMING_SNAKE_CASE						:					str		      =		{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
										__SCREAMING_SNAKE_CASE						:					List[str]		      =		model(lowerCAmelCase__     )

										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		outputs

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


				def 		__magic_name__(	self      :Tuple      ,      lowerCAmelCase__      :str      ,      lowerCAmelCase__      :List[str]      ,      lowerCAmelCase__      :Any      ,      lowerCAmelCase__      :int      ,      lowerCAmelCase__      :Tuple      ,      lowerCAmelCase__      :Optional[int]      ,      lowerCAmelCase__      :List[Any]      ,      lowerCAmelCase__      :int      ,      lowerCAmelCase__      :Dict      ,      )		->      str:
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		TFXLMForQuestionAnsweringSimple(lowerCAmelCase__     )

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

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

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

										__SCREAMING_SNAKE_CASE						:					Any		      =		model(lowerCAmelCase__     )

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


				def 		__magic_name__(	self      :List[str]      ,      lowerCAmelCase__      :Tuple      ,      lowerCAmelCase__      :Optional[Any]      ,      lowerCAmelCase__      :Any      ,      lowerCAmelCase__      :Dict      ,      lowerCAmelCase__      :int      ,      lowerCAmelCase__      :int      ,      lowerCAmelCase__      :Dict      ,      lowerCAmelCase__      :Any      ,      lowerCAmelCase__      :Union[str, Any]      ,      )		->      int:
										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		self.num_labels
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		TFXLMForTokenClassification(config=lowerCAmelCase__     )
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										__SCREAMING_SNAKE_CASE						:					Dict		      =		model(lowerCAmelCase__     )
										self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.num_labels)     )


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



				def 		__magic_name__(	self      :int     )		->      Optional[int]:
										__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
										)      ,      
										)						:					Optional[int]		      =		config_and_inputs
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		{
										    '''input_ids''': input_ids,
										    '''token_type_ids''': token_type_ids,
										    '''langs''': token_type_ids,
										    '''lengths''': input_lengths,
										}
										return config, inputs_dict




@require_tf
class 	_lowercase				(      A__						,      A__						,      unittest.TestCase	):






				'''simple docstring'''



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


				def 		__magic_name__(	self      :Tuple      ,      lowerCAmelCase__      :Any      ,      lowerCAmelCase__      :Union[str, Any]      ,      lowerCAmelCase__      :List[str]      ,      lowerCAmelCase__      :Dict      ,      lowerCAmelCase__      :Union[str, Any]     )		->      List[Any]:
										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 		__magic_name__(	self      :int     )		->      Any:
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		TFXLMModelTester(self     )
										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		ConfigTester(self      ,      config_class=lowerCAmelCase__      ,      emb_dim=37     )


				def 		__magic_name__(	self      :Any     )		->      Any:
										self.config_tester.run_common_tests()


				def 		__magic_name__(	self      :List[Any]     )		->      int:
										__SCREAMING_SNAKE_CASE						:					int		      =		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_model(*lowerCAmelCase__     )


				def 		__magic_name__(	self      :str     )		->      int:
										__SCREAMING_SNAKE_CASE						:					int		      =		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_lm_head(*lowerCAmelCase__     )


				def 		__magic_name__(	self      :Any     )		->      Optional[int]:
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_qa(*lowerCAmelCase__     )


				def 		__magic_name__(	self      :int     )		->      Dict:
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_sequence_classif(*lowerCAmelCase__     )


				def 		__magic_name__(	self      :str     )		->      List[Any]:
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_for_token_classification(*lowerCAmelCase__     )


				def 		__magic_name__(	self      :List[str]     )		->      Optional[int]:
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_for_multiple_choice(*lowerCAmelCase__     )



				@slow
				def 		__magic_name__(	self      :List[str]     )		->      Dict:
										for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__SCREAMING_SNAKE_CASE						:					Tuple		      =		TFXLMModel.from_pretrained(lowerCAmelCase__     )
																self.assertIsNotNone(lowerCAmelCase__     )




@require_tf
class 	_lowercase				(      unittest.TestCase	):






				'''simple docstring'''



				@slow
				def 		__magic_name__(	self      :Tuple     )		->      Dict:
										__SCREAMING_SNAKE_CASE						:					Dict		      =		TFXLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048'''     )
										__SCREAMING_SNAKE_CASE						:					int		      =		tf.convert_to_tensor([[14, 447]]      ,      dtype=tf.intaa     )  # the president
										__SCREAMING_SNAKE_CASE						:					int		      =		[
										    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						:					Tuple		      =		model.generate(lowerCAmelCase__      ,      do_sample=lowerCAmelCase__     )
										self.assertListEqual(output_ids[0].numpy().tolist()      ,      lowerCAmelCase__     )




from .number_theory.prime_numbers import next_prime
class 	_lowercase				:






				'''simple docstring'''



				def __init__(	self      :Tuple      ,      lowerCAmelCase__      :int      ,      lowerCAmelCase__      :int | None = None      ,      lowerCAmelCase__      :float | None = None      ,      )		->      None:
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		size_table
										__SCREAMING_SNAKE_CASE						:					Dict		      =		[None] * self.size_table
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		0.75 if lim_charge is None else lim_charge
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		1 if charge_factor is None else charge_factor
										__SCREAMING_SNAKE_CASE						:					list		      =		[]
										__SCREAMING_SNAKE_CASE						:					dict		      =		{}


				def 		__magic_name__(	self      :List[Any]     )		->      Tuple:
										return self._keys


				def 		__magic_name__(	self      :Any     )		->      Dict:
										return sum(1 for slot in self.values if slot is not None     ) / (
										    self.size_table * self.charge_factor
										)


				def 		__magic_name__(	self      :Optional[Any]      ,      lowerCAmelCase__      :Union[str, Any]     )		->      Optional[Any]:
										return key % self.size_table


				def 		__magic_name__(	self      :int      ,      lowerCAmelCase__      :Union[str, Any]     )		->      List[Any]:
										print(f'''step {step_ord}'''     )
										print(list(range(len(self.values     )     )     )     )
										print(self.values     )


				def 		__magic_name__(	self      :List[str]      ,      lowerCAmelCase__      :Optional[Any]     )		->      Dict:
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		1
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		values
										for value in values:
																self.insert_data(lowerCAmelCase__     )
																self._step_by_step(lowerCAmelCase__     )
																i += 1


				def 		__magic_name__(	self      :Tuple      ,      lowerCAmelCase__      :List[Any]      ,      lowerCAmelCase__      :Union[str, Any]     )		->      str:
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		data
										__SCREAMING_SNAKE_CASE						:					Any		      =		data


				def 		__magic_name__(	self      :Optional[int]      ,      lowerCAmelCase__      :Any      ,      lowerCAmelCase__      :List[str]=None     )		->      int:
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		self.hash_function(key + 1     )

										while self.values[new_key] is not None and self.values[new_key] != key:
																if self.values.count(lowerCAmelCase__     ) > 0:
																						__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		self.hash_function(new_key + 1     )
																else:
																						__SCREAMING_SNAKE_CASE						:					int		      =		None
																						break

										return new_key


				def 		__magic_name__(	self      :Optional[int]     )		->      Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:					str		      =		[value for value in self.values if value is not None]
										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		next_prime(self.size_table      ,      factor=2     )
										self._keys.clear()
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		[None] * self.size_table  # hell's pointers D: don't DRY ;/
										for value in survivor_values:
																self.insert_data(lowerCAmelCase__     )



				def 		__magic_name__(	self      :Tuple      ,      lowerCAmelCase__      :str     )		->      Dict:
										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		self.hash_function(lowerCAmelCase__     )

										if self.values[key] is None:
																self._set_value(lowerCAmelCase__      ,      lowerCAmelCase__     )

										elif self.values[key] == data:
																pass

										else:
																__SCREAMING_SNAKE_CASE						:					Tuple		      =		self._collision_resolution(lowerCAmelCase__      ,      lowerCAmelCase__     )
																if collision_resolution is not None:
																						self._set_value(lowerCAmelCase__      ,      lowerCAmelCase__     )
																else:
																						self.rehashing()
																						self.insert_data(lowerCAmelCase__     )
