





import unittest

from transformers import XLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          XLMForMultipleChoice,
          XLMForQuestionAnswering,
          XLMForQuestionAnsweringSimple,
          XLMForSequenceClassification,
          XLMForTokenClassification,
          XLMModel,
          XLMWithLMHeadModel,
      )
      from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST





class UpperCAmelCase_				:


  def __init__(							self ,__snake_case ,__snake_case=1_3 ,__snake_case=7 ,__snake_case=True ,__snake_case=True ,__snake_case=True ,__snake_case=True ,__snake_case=True ,__snake_case=False ,__snake_case=False ,__snake_case=False ,__snake_case=2 ,__snake_case=9_9 ,__snake_case=0 ,__snake_case=3_2 ,__snake_case=5 ,__snake_case=4 ,__snake_case=0.1 ,__snake_case=0.1 ,__snake_case=5_1_2 ,__snake_case=2 ,__snake_case=0.02 ,__snake_case=2 ,__snake_case=4 ,__snake_case="last" ,__snake_case=True ,__snake_case=None ,__snake_case=0 ,):



       """simple docstring"""





       A_          =       parent
       A_          =       batch_size
       A_          =       seq_length
       A_          =       is_training
       A_          =       use_input_lengths
       A_          =       use_token_type_ids
       A_          =       use_labels
       A_          =       gelu_activation
       A_          =       sinusoidal_embeddings
       A_          =       causal
       A_          =       asm
       A_          =       n_langs
       A_          =       vocab_size
       A_          =       n_special
       A_          =       hidden_size
       A_          =       num_hidden_layers
       A_          =       num_attention_heads
       A_          =       hidden_dropout_prob
       A_          =       attention_probs_dropout_prob
       A_          =       max_position_embeddings
       A_          =       type_sequence_label_size
       A_          =       initializer_range
       A_          =       num_labels
       A_          =       num_choices
       A_          =       summary_type
       A_          =       use_proj
       A_          =       scope
       A_          =       bos_token_id


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size )
       A_          =       random_attention_mask([self.batch_size, self.seq_length] )

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

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

       A_          =       None
       A_          =       None
       A_          =       None
       if self.use_labels:
            A_          =       ids_tensor([self.batch_size] ,self.type_sequence_label_size )
            A_          =       ids_tensor([self.batch_size, self.seq_length] ,self.num_labels )
            A_          =       ids_tensor([self.batch_size] ,2 ).float()
            A_          =       ids_tensor([self.batch_size] ,self.num_choices )

       A_          =       self.get_config()

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


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       return 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 ,num_labels=self.num_labels ,bos_token_id=self.bos_token_id ,)


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,):



       """simple docstring"""





       A_          =       XLMModel(config=__snake_case )
       model.to(__snake_case )
       model.eval()
       A_          =       model(__snake_case ,lengths=__snake_case ,langs=__snake_case )
       A_          =       model(__snake_case ,langs=__snake_case )
       A_          =       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 ,):



       """simple docstring"""





       A_          =       XLMWithLMHeadModel(__snake_case )
       model.to(__snake_case )
       model.eval()

       A_          =       model(__snake_case ,token_type_ids=__snake_case ,labels=__snake_case )
       self.parent.assertEqual(result.loss.shape ,() )
       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 ,):



       """simple docstring"""





       A_          =       XLMForQuestionAnsweringSimple(__snake_case )
       model.to(__snake_case )
       model.eval()

       A_          =       model(__snake_case )

       A_          =       model(__snake_case ,start_positions=__snake_case ,end_positions=__snake_case )
       A_          =       outputs
       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 ,):



       """simple docstring"""





       A_          =       XLMForQuestionAnswering(__snake_case )
       model.to(__snake_case )
       model.eval()

       A_          =       model(__snake_case )

       A_          =       model(
           __snake_case ,start_positions=__snake_case ,end_positions=__snake_case ,cls_index=__snake_case ,is_impossible=__snake_case ,p_mask=__snake_case ,)

       A_          =       model(
           __snake_case ,start_positions=__snake_case ,end_positions=__snake_case ,cls_index=__snake_case ,is_impossible=__snake_case ,)

       ((A_)   ,    )          =       result_with_labels.to_tuple()

       A_          =       model(__snake_case ,start_positions=__snake_case ,end_positions=__snake_case )

       ((A_)   ,    )          =       result_with_labels.to_tuple()

       self.parent.assertEqual(result_with_labels.loss.shape ,() )
       self.parent.assertEqual(result.start_top_log_probs.shape ,(self.batch_size, model.config.start_n_top) )
       self.parent.assertEqual(result.start_top_index.shape ,(self.batch_size, model.config.start_n_top) )
       self.parent.assertEqual(
           result.end_top_log_probs.shape ,(self.batch_size, model.config.start_n_top * model.config.end_n_top) )
       self.parent.assertEqual(
           result.end_top_index.shape ,(self.batch_size, model.config.start_n_top * model.config.end_n_top) )
       self.parent.assertEqual(result.cls_logits.shape ,(self.batch_size,) )


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,):



       """simple docstring"""





       A_          =       XLMForSequenceClassification(__snake_case )
       model.to(__snake_case )
       model.eval()

       A_          =       model(__snake_case )
       A_          =       model(__snake_case ,labels=__snake_case )
       self.parent.assertEqual(result.loss.shape ,() )
       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 ,):



       """simple docstring"""





       A_          =       self.num_labels
       A_          =       XLMForTokenClassification(__snake_case )
       model.to(__snake_case )
       model.eval()

       A_          =       model(__snake_case ,attention_mask=__snake_case ,labels=__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 ,):



       """simple docstring"""





       A_          =       self.num_choices
       A_          =       XLMForMultipleChoice(config=__snake_case )
       model.to(__snake_case )
       model.eval()
       A_          =       input_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous()
       A_          =       token_type_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous()
       A_          =       input_mask.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous()
       A_          =       model(
           __snake_case ,attention_mask=__snake_case ,token_type_ids=__snake_case ,labels=__snake_case ,)
       self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) )




  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.prepare_config_and_inputs()
       (
           (
           A_
       )   ,    (
           A_
       )   ,    (
           A_
       )   ,    (
           A_
       )   ,    (
           A_
       )   ,    (
           A_
       )   ,    (
           A_
       )   ,    (
           A_
       )   ,    (
           A_
       )   ,    
       )          =       config_and_inputs
       A_          =       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths}
       return config, inputs_dict





@require_torch
class UpperCAmelCase_				( _SCREAMING_SNAKE_CASE     ,		_SCREAMING_SNAKE_CASE     ,		_SCREAMING_SNAKE_CASE     ,		unittest.TestCase ):
  __lowerCAmelCase   :						Any							=   (
      (
          XLMModel,
          XLMWithLMHeadModel,
          XLMForQuestionAnswering,
          XLMForSequenceClassification,
          XLMForQuestionAnsweringSimple,
          XLMForTokenClassification,
          XLMForMultipleChoice,
      )
      if is_torch_available()
      else ()
  )
  __lowerCAmelCase   :						Optional[int]							=   (
      (XLMWithLMHeadModel,) if is_torch_available() else ()
  )  # TODO (PVP): Check other models whether language generation is also applicable
  __lowerCAmelCase   :						int							=   (
      {
          """feature-extraction""": XLMModel,
          """fill-mask""": XLMWithLMHeadModel,
          """question-answering""": XLMForQuestionAnsweringSimple,
          """text-classification""": XLMForSequenceClassification,
          """text-generation""": XLMWithLMHeadModel,
          """token-classification""": XLMForTokenClassification,
          """zero-shot""": XLMForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ):



       """simple docstring"""





       if (
           pipeline_test_casse_name == "QAPipelineTests"
           and tokenizer_name is not None
           and not tokenizer_name.endswith('''Fast''' )
       ):
            # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
            # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
            # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
            return True

       return False


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case=False ):



       """simple docstring"""





       A_          =       super()._prepare_for_class(__snake_case ,__snake_case ,return_labels=__snake_case )

       if return_labels:
            if model_class.__name__ == "XLMForQuestionAnswering":
                 A_          =       torch.zeros(
                     self.model_tester.batch_size ,dtype=torch.long ,device=__snake_case )
                 A_          =       torch.zeros(
                     self.model_tester.batch_size ,dtype=torch.long ,device=__snake_case )

       return inputs_dict


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       XLMModelTester(self )
       A_          =       ConfigTester(self ,config_class=__snake_case ,emb_dim=3_7 )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       self.config_tester.run_common_tests()


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_model(*__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_lm_head(*__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_simple_qa(*__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_qa(*__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_sequence_classif(*__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_token_classif(*__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_for_multiple_choice(*__snake_case )


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case=False ,__snake_case=1 ):



       """simple docstring"""





       self.assertIsInstance(__snake_case ,__snake_case )
       self.assertListEqual(
           [isinstance(__snake_case ,__snake_case ) for iter_attentions in attentions] ,[True] * len(__snake_case ) )
       self.assertEqual(len(__snake_case ) ,(max_length - min_length) * num_beam_groups )

       for idx, iter_attentions in enumerate(__snake_case ):
            # adds PAD dummy token
            A_          =       min_length + idx + 1
            A_          =       min_length + idx + 1

            A_          =       (
                batch_size * num_beam_groups,
                config.num_attention_heads,
                tgt_len,
                src_len,
            )
            # check attn size
            self.assertListEqual(
                [layer_attention.shape for layer_attention in iter_attentions] ,[expected_shape] * len(__snake_case ) )


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case=False ,__snake_case=1 ):



       """simple docstring"""





       self.assertIsInstance(__snake_case ,__snake_case )
       self.assertListEqual(
           [isinstance(__snake_case ,__snake_case ) for iter_hidden_states in hidden_states] ,[True] * len(__snake_case ) ,)
       self.assertEqual(len(__snake_case ) ,(max_length - min_length) * num_beam_groups )

       for idx, iter_hidden_states in enumerate(__snake_case ):
            # adds PAD dummy token
            A_          =       min_length + idx + 1
            A_          =       (batch_size * num_beam_groups, seq_len, config.hidden_size)
            # check hidden size
            self.assertListEqual(
                [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states] ,[expected_shape] * len(__snake_case ) ,)
       pass




  @slow
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A_          =       XLMModel.from_pretrained(__snake_case )
            self.assertIsNotNone(__snake_case )





@require_torch
class UpperCAmelCase_				( unittest.TestCase ):


  @slow
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       XLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048''' )
       model.to(__snake_case )
       A_          =       torch.tensor([[1_4, 4_4_7]] ,dtype=torch.long ,device=__snake_case )  # the president
       A_          =       [
           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
       A_          =       model.generate(__snake_case ,do_sample=__snake_case )
       self.assertListEqual(output_ids[0].cpu().numpy().tolist() ,__snake_case )










def      UpperCAmelCase_    (    _UpperCAmelCase							:list				)	->				list:
     '''simple docstring'''


     if len(_UpperCAmelCase				) < 2:
          return collection

     def circle_sort_util(_UpperCAmelCase							:list  ,						_UpperCAmelCase							:int  ,						_UpperCAmelCase							:int				) -> bool:
          A_          =       False

          if low == high:
               return swapped

          A_          =       low
          A_          =       high

          while left < right:
               if collection[left] > collection[right]:
                    A_   ,    A_          =       (
                        collection[right],
                        collection[left],
                    )
                    A_          =       True

               left += 1
               right -= 1

          if left == right and collection[left] > collection[right + 1]:
               A_   ,    A_          =       (
                   collection[right + 1],
                   collection[left],
               )

               A_          =       True

          A_          =       low + int((high - low) / 2				)
          A_          =       circle_sort_util(_UpperCAmelCase  ,						_UpperCAmelCase  ,						_UpperCAmelCase				)
          A_          =       circle_sort_util(_UpperCAmelCase  ,						mid + 1  ,						_UpperCAmelCase				)

          return swapped or left_swap or right_swap

     A_          =       True

     while is_not_sorted is True:
          A_          =       circle_sort_util(_UpperCAmelCase  ,						0  ,						len(_UpperCAmelCase				) - 1				)

     return collection


if __name__ == "__main__":
      a__      :     Optional[Any]         =		input('Enter numbers separated by a comma:\n').strip()
      a__      :     List[str]         =		[int(item) for item in user_input.split(',')]
      print(circle_sort(unsorted))



