

"""simple docstring"""

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 			__a					:





       """simple docstring"""


       def __init__(   self			,					snake_case			,					snake_case=13			,					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=99			,					snake_case=0			,					snake_case=32			,					snake_case=5			,					snake_case=4			,					snake_case=0.1			,					snake_case=0.1			,					snake_case=512			,					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"""

        lowerCAmelCase__   :				List[Any] 			=				parent
        lowerCAmelCase__   :				Union[str, Any] 			=				batch_size
        lowerCAmelCase__   :				List[str] 			=				seq_length
        lowerCAmelCase__   :				int 			=				is_training
        lowerCAmelCase__   :				Union[str, Any] 			=				use_input_lengths
        lowerCAmelCase__   :				List[str] 			=				use_token_type_ids
        lowerCAmelCase__   :				Union[str, Any] 			=				use_labels
        lowerCAmelCase__   :				Tuple 			=				gelu_activation
        lowerCAmelCase__   :				str 			=				sinusoidal_embeddings
        lowerCAmelCase__   :				str 			=				causal
        lowerCAmelCase__   :				Tuple 			=				asm
        lowerCAmelCase__   :				Dict 			=				n_langs
        lowerCAmelCase__   :				int 			=				vocab_size
        lowerCAmelCase__   :				Any 			=				n_special
        lowerCAmelCase__   :				List[Any] 			=				hidden_size
        lowerCAmelCase__   :				int 			=				num_hidden_layers
        lowerCAmelCase__   :				List[str] 			=				num_attention_heads
        lowerCAmelCase__   :				List[str] 			=				hidden_dropout_prob
        lowerCAmelCase__   :				Union[str, Any] 			=				attention_probs_dropout_prob
        lowerCAmelCase__   :				Optional[Any] 			=				max_position_embeddings
        lowerCAmelCase__   :				Tuple 			=				type_sequence_label_size
        lowerCAmelCase__   :				Optional[Any] 			=				initializer_range
        lowerCAmelCase__   :				List[Any] 			=				num_labels
        lowerCAmelCase__   :				List[str] 			=				num_choices
        lowerCAmelCase__   :				Tuple 			=				summary_type
        lowerCAmelCase__   :				Any 			=				use_proj
        lowerCAmelCase__   :				List[str] 			=				scope
        lowerCAmelCase__   :				Union[str, Any] 			=				bos_token_id






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				ids_tensor([self.batch_size, self.seq_length]			,					self.vocab_size   )
        lowerCAmelCase__   :				Any 			=				random_attention_mask([self.batch_size, self.seq_length]   )

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

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

        lowerCAmelCase__   :				List[Any] 			=				None
        lowerCAmelCase__   :				Any 			=				None
        lowerCAmelCase__   :				Optional[int] 			=				None
        if self.use_labels:
         lowerCAmelCase__   :				str 			=				ids_tensor([self.batch_size]			,					self.type_sequence_label_size   )
         lowerCAmelCase__   :				Optional[Any] 			=				ids_tensor([self.batch_size, self.seq_length]			,					self.num_labels   )
         lowerCAmelCase__   :				int 			=				ids_tensor([self.batch_size]			,					2   ).float()
         lowerCAmelCase__   :				Optional[Any] 			=				ids_tensor([self.batch_size]			,					self.num_choices   )

        lowerCAmelCase__   :				int 			=				self.get_config()

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






       def 			SCREAMING_SNAKE_CASE_	(   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 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					):





        """simple docstring"""

        lowerCAmelCase__   :				List[str] 			=				XLMModel(config=lowercase__   )
        model.to(lowercase__   )
        model.eval()
        lowerCAmelCase__   :				Any 			=				model(lowercase__			,					lengths=lowercase__			,					langs=lowercase__   )
        lowerCAmelCase__   :				int 			=				model(lowercase__			,					langs=lowercase__   )
        lowerCAmelCase__   :				Optional[Any] 			=				model(lowercase__   )
        self.parent.assertEqual(result.last_hidden_state.shape			,					(self.batch_size, self.seq_length, self.hidden_size)   )






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[Any] 			=				XLMWithLMHeadModel(lowercase__   )
        model.to(lowercase__   )
        model.eval()

        lowerCAmelCase__   :				Tuple 			=				model(lowercase__			,					token_type_ids=lowercase__			,					labels=lowercase__   )
        self.parent.assertEqual(result.loss.shape			,					()   )
        self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.seq_length, self.vocab_size)   )






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					):





        """simple docstring"""

        lowerCAmelCase__   :				Tuple 			=				XLMForQuestionAnsweringSimple(lowercase__   )
        model.to(lowercase__   )
        model.eval()

        lowerCAmelCase__   :				int 			=				model(lowercase__   )

        lowerCAmelCase__   :				Optional[int] 			=				model(lowercase__			,					start_positions=lowercase__			,					end_positions=lowercase__   )
        lowerCAmelCase__   :				str 			=				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 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					):





        """simple docstring"""

        lowerCAmelCase__   :				List[str] 			=				XLMForQuestionAnswering(lowercase__   )
        model.to(lowercase__   )
        model.eval()

        lowerCAmelCase__   :				Optional[Any] 			=				model(lowercase__   )

        lowerCAmelCase__   :				Optional[Any] 			=				model(
            lowercase__			,					start_positions=lowercase__			,					end_positions=lowercase__			,					cls_index=lowercase__			,					is_impossible=lowercase__			,					p_mask=lowercase__			,					)

        lowerCAmelCase__   :				Union[str, Any] 			=				model(
            lowercase__			,					start_positions=lowercase__			,					end_positions=lowercase__			,					cls_index=lowercase__			,					is_impossible=lowercase__			,					)

        (lowerCAmelCase__     )   :				Tuple 			=				result_with_labels.to_tuple()

        lowerCAmelCase__   :				Dict 			=				model(lowercase__			,					start_positions=lowercase__			,					end_positions=lowercase__   )

        (lowerCAmelCase__     )   :				Optional[Any] 			=				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 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					):





        """simple docstring"""

        lowerCAmelCase__   :				int 			=				XLMForSequenceClassification(lowercase__   )
        model.to(lowercase__   )
        model.eval()

        lowerCAmelCase__   :				Optional[int] 			=				model(lowercase__   )
        lowerCAmelCase__   :				Dict 			=				model(lowercase__			,					labels=lowercase__   )
        self.parent.assertEqual(result.loss.shape			,					()   )
        self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.type_sequence_label_size)   )






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					):





        """simple docstring"""

        lowerCAmelCase__   :				List[Any] 			=				self.num_labels
        lowerCAmelCase__   :				str 			=				XLMForTokenClassification(lowercase__   )
        model.to(lowercase__   )
        model.eval()

        lowerCAmelCase__   :				Dict 			=				model(lowercase__			,					attention_mask=lowercase__			,					labels=lowercase__   )
        self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.seq_length, self.num_labels)   )






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					):





        """simple docstring"""

        lowerCAmelCase__   :				Dict 			=				self.num_choices
        lowerCAmelCase__   :				Dict 			=				XLMForMultipleChoice(config=lowercase__   )
        model.to(lowercase__   )
        model.eval()
        lowerCAmelCase__   :				Tuple 			=				input_ids.unsqueeze(1   ).expand(-1			,					self.num_choices			,					-1   ).contiguous()
        lowerCAmelCase__   :				Dict 			=				token_type_ids.unsqueeze(1   ).expand(-1			,					self.num_choices			,					-1   ).contiguous()
        lowerCAmelCase__   :				Optional[Any] 			=				input_mask.unsqueeze(1   ).expand(-1			,					self.num_choices			,					-1   ).contiguous()
        lowerCAmelCase__   :				Any 			=				model(
            lowercase__			,					attention_mask=lowercase__			,					token_type_ids=lowercase__			,					labels=lowercase__			,					)
        self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.num_choices)   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				self.prepare_config_and_inputs()
        (
            lowerCAmelCase__     
        )   :				List[Any] 			=				config_and_inputs
        lowerCAmelCase__   :				Optional[Any] 			=				{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """lengths""": input_lengths}
        return config, inputs_dict






@require_torch
class 			__a					(						snake_case_       ,	snake_case_       ,	snake_case_       ,	unittest.TestCase       ):





       """simple docstring"""


       __UpperCamelCase					:      Union[str, Any]    =						(
           (
               XLMModel,
               XLMWithLMHeadModel,
               XLMForQuestionAnswering,
               XLMForSequenceClassification,
               XLMForQuestionAnsweringSimple,
               XLMForTokenClassification,
               XLMForMultipleChoice,
           )
           if is_torch_available()
           else ()
       )
       __UpperCamelCase					:      Optional[int]    =						(
           (XLMWithLMHeadModel,) if is_torch_available() else ()
       )  # TODO (PVP): Check other models whether language generation is also applicable
       __UpperCamelCase					:      Any    =						(
           {
               '''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 			SCREAMING_SNAKE_CASE_	(   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 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case=False   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				super()._prepare_for_class(lowercase__			,					lowercase__			,					return_labels=lowercase__   )

        if return_labels:
         if model_class.__name__ == "XLMForQuestionAnswering":
          lowerCAmelCase__   :				int 			=				torch.zeros(
              self.model_tester.batch_size			,					dtype=torch.long			,					device=lowercase__   )
          lowerCAmelCase__   :				Union[str, Any] 			=				torch.zeros(
              self.model_tester.batch_size			,					dtype=torch.long			,					device=lowercase__   )

        return inputs_dict






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[Any] 			=				XLMModelTester(self   )
        lowerCAmelCase__   :				Dict 			=				ConfigTester(self			,					config_class=lowercase__			,					emb_dim=37   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        self.config_tester.run_common_tests()






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_model(*lowercase__   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				List[Any] 			=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_lm_head(*lowercase__   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Dict 			=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_simple_qa(*lowercase__   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

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






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Dict 			=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_sequence_classif(*lowercase__   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Any 			=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_token_classif(*lowercase__   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Dict 			=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_for_multiple_choice(*lowercase__   )






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case=False			,					snake_case=1   ):





        """simple docstring"""

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

        for idx, iter_attentions in enumerate(lowercase__   ):
         # adds PAD dummy token
         lowerCAmelCase__   :				Tuple 			=				min_length + idx + 1
         lowerCAmelCase__   :				str 			=				min_length + idx + 1

         lowerCAmelCase__   :				str 			=				(
             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(lowercase__   )   )






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case			,					snake_case=False			,					snake_case=1   ):





        """simple docstring"""

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

        for idx, iter_hidden_states in enumerate(lowercase__   ):
         # adds PAD dummy token
         lowerCAmelCase__   :				Optional[int] 			=				min_length + idx + 1
         lowerCAmelCase__   :				List[Any] 			=				(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(lowercase__   )			,					)
        pass






       @slow
       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         lowerCAmelCase__   :				Dict 			=				XLMModel.from_pretrained(lowercase__   )
         self.assertIsNotNone(lowercase__   )






@require_torch
class 			__a					(						unittest.TestCase       ):





       """simple docstring"""


       @slow
       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				int 			=				XLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048"   )
        model.to(lowercase__   )
        lowerCAmelCase__   :				Dict 			=				torch.tensor([[14, 447]]			,					dtype=torch.long			,					device=lowercase__   )  # the president
        lowerCAmelCase__   :				Tuple 			=				[
            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
        lowerCAmelCase__   :				List[str] 			=				model.generate(lowercase__			,					do_sample=lowercase__   )
        self.assertListEqual(output_ids[0].cpu().numpy().tolist()			,					lowercase__   )












'''simple docstring'''


from ..utils import DummyObject, requires_backends







class      SCREAMING_SNAKE_CASE    (						metaclass=snake_case_				):
      __magic_name__  :       Dict   							=					['''transformers''', '''torch''', '''note_seq''']






      def __init__( self   :  List[str]						,						*lowercase__   :  Dict						,						**lowercase__   :  int       ):


           '''simple docstring'''

           requires_backends(self						,						["""transformers""", """torch""", """note_seq"""]       )






      @classmethod
      def 							lowercase_						( cls   :  Dict						,						*lowercase__   :  List[str]						,						**lowercase__   :  str       ):


           '''simple docstring'''

           requires_backends(cls						,						["""transformers""", """torch""", """note_seq"""]       )






      @classmethod
      def 							lowercase_						( cls   :  str						,						*lowercase__   :  Optional[int]						,						**lowercase__   :  Any       ):


           '''simple docstring'''

           requires_backends(cls						,						["""transformers""", """torch""", """note_seq"""]       )
