



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


   def       A__    (    self:     str							)	->      int:
       UpperCAmelCase_     :       Union[str, Any]  		=					ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size							)
       UpperCAmelCase_     :       List[Any]  		=					random_attention_mask([self.batch_size, self.seq_length]  ,dtype=tf.floataa							)

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

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

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

       UpperCAmelCase_     :       Union[str, Any]  		=					XLMConfig(
           vocab_size=self.vocab_size  ,n_special=self.n_special  ,emb_dim=self.hidden_size  ,n_layers=self.num_hidden_layers  ,n_heads=self.num_attention_heads  ,dropout=self.hidden_dropout_prob  ,attention_dropout=self.attention_probs_dropout_prob  ,gelu_activation=self.gelu_activation  ,sinusoidal_embeddings=self.sinusoidal_embeddings  ,asm=self.asm  ,causal=self.causal  ,n_langs=self.n_langs  ,max_position_embeddings=self.max_position_embeddings  ,initializer_range=self.initializer_range  ,summary_type=self.summary_type  ,use_proj=self.use_proj  ,bos_token_id=self.bos_token_id  ,)

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


   def       A__    (    self:     List[Any]  ,lowerCamelCase_:     Optional[int]  ,lowerCamelCase_:     Optional[Any]  ,lowerCamelCase_:     Optional[int]  ,lowerCamelCase_:     List[str]  ,lowerCamelCase_:     int  ,lowerCamelCase_:     int  ,lowerCamelCase_:     Tuple  ,lowerCamelCase_:     int  ,lowerCamelCase_:     str  ,)	->      List[str]:
       UpperCAmelCase_     :       Dict  		=					TFXLMModel(config=lowerCamelCase_							)
       UpperCAmelCase_     :       List[Any]  		=					{"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
       UpperCAmelCase_     :       Optional[Any]  		=					model(lowerCamelCase_							)

       UpperCAmelCase_     :       Any  		=					[input_ids, input_mask]
       UpperCAmelCase_     :       int  		=					model(lowerCamelCase_							)
       self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, self.seq_length, self.hidden_size)							)


   def       A__    (    self:     Optional[Any]  ,lowerCamelCase_:     List[str]  ,lowerCamelCase_:     List[Any]  ,lowerCamelCase_:     List[str]  ,lowerCamelCase_:     Any  ,lowerCamelCase_:     str  ,lowerCamelCase_:     Tuple  ,lowerCamelCase_:     List[Any]  ,lowerCamelCase_:     Dict  ,lowerCamelCase_:     str  ,)	->      Tuple:
       UpperCAmelCase_     :       Tuple  		=					TFXLMWithLMHeadModel(lowerCamelCase_							)

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

       UpperCAmelCase_     :       Any  		=					outputs

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


   def       A__    (    self:     Union[str, Any]  ,lowerCamelCase_:     str  ,lowerCamelCase_:     Dict  ,lowerCamelCase_:     Optional[int]  ,lowerCamelCase_:     int  ,lowerCamelCase_:     Optional[Any]  ,lowerCamelCase_:     str  ,lowerCamelCase_:     int  ,lowerCamelCase_:     Optional[int]  ,lowerCamelCase_:     Dict  ,)	->      List[Any]:
       UpperCAmelCase_     :       List[str]  		=					TFXLMForQuestionAnsweringSimple(lowerCamelCase_							)

       UpperCAmelCase_     :       str  		=					{"""input_ids""": input_ids, """lengths""": input_lengths}

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

       UpperCAmelCase_     :       List[str]  		=					{"""input_ids""": input_ids, """lengths""": input_lengths}

       UpperCAmelCase_     :       Tuple  		=					model(lowerCamelCase_							)

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


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


   def       A__    (    self:     int  ,lowerCamelCase_:     Optional[Any]  ,lowerCamelCase_:     List[str]  ,lowerCamelCase_:     Any  ,lowerCamelCase_:     Any  ,lowerCamelCase_:     Any  ,lowerCamelCase_:     str  ,lowerCamelCase_:     int  ,lowerCamelCase_:     Dict  ,lowerCamelCase_:     str  ,)	->      int:
       UpperCAmelCase_     :       str  		=					self.num_choices
       UpperCAmelCase_     :       Optional[int]  		=					TFXLMForMultipleChoice(config=lowerCamelCase_							)
       UpperCAmelCase_     :       Union[str, Any]  		=					tf.tile(tf.expand_dims(lowerCamelCase_  ,1							)  ,(1, self.num_choices, 1)							)
       UpperCAmelCase_     :       List[str]  		=					tf.tile(tf.expand_dims(lowerCamelCase_  ,1							)  ,(1, self.num_choices, 1)							)
       UpperCAmelCase_     :       Optional[Any]  		=					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       A__    (    self:     Optional[Any]							)	->      int:
       UpperCAmelCase_     :       Dict  		=					self.prepare_config_and_inputs()
       (
           (
           UpperCAmelCase_
       )		, (
           UpperCAmelCase_
       )		, (
           UpperCAmelCase_
       )		, (
           UpperCAmelCase_
       )		, (
           UpperCAmelCase_
       )		, (
           UpperCAmelCase_
       )		, (
           UpperCAmelCase_
       )		, (
           UpperCAmelCase_
       )		, (
           UpperCAmelCase_
       )		, 
       )     :       Tuple  		=					config_and_inputs
       UpperCAmelCase_     :       Dict  		=					{
           """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				(     __snake_case ,							__snake_case ,							unittest.TestCase ):






   '''simple docstring'''






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


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


   def       A__    (    self:     Union[str, Any]							)	->      Union[str, Any]:
       self.config_tester.run_common_tests()


   def       A__    (    self:     int							)	->      Dict:
       UpperCAmelCase_     :       Dict  		=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_model(*lowerCamelCase_							)


   def       A__    (    self:     int							)	->      Dict:
       UpperCAmelCase_     :       Tuple  		=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_lm_head(*lowerCamelCase_							)


   def       A__    (    self:     List[str]							)	->      Dict:
       UpperCAmelCase_     :       Optional[Any]  		=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_qa(*lowerCamelCase_							)


   def       A__    (    self:     List[str]							)	->      Any:
       UpperCAmelCase_     :       List[str]  		=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_sequence_classif(*lowerCamelCase_							)


   def       A__    (    self:     int							)	->      Optional[Any]:
       UpperCAmelCase_     :       Optional[Any]  		=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_for_token_classification(*lowerCamelCase_							)


   def       A__    (    self:     Union[str, Any]							)	->      Dict:
       UpperCAmelCase_     :       Union[str, Any]  		=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_for_multiple_choice(*lowerCamelCase_							)






   @slow
   def       A__    (    self:     Optional[int]							)	->      Optional[Any]:
       for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCAmelCase_     :       Tuple  		=					TFXLMModel.from_pretrained(lowerCamelCase_							)
           self.assertIsNotNone(lowerCamelCase_							)







@require_tf
class       _snake_case				(     unittest.TestCase ):






   '''simple docstring'''






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

"""simple docstring"""
def  lowerCAmelCase__     (      _UpperCamelCase							:					list       )       ->					list:





						"""simple docstring"""






						if len(_UpperCamelCase       ) <= 1:
												return data
						else:
												return [
												    *quick_sort([e for e in data[1:] if e <= data[0]]       ),
												    data[0],
												    *quick_sort([e for e in data[1:] if e > data[0]]       ),
												]


if __name__ == "__main__":
					import doctest

					doctest.testmod()


