

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





    def __init__(      self						,   a						,   a=13						,   a=7						,   a=True						,   a=True						,   a=True						,   a=True						,   a=True						,   a=False						,   a=False						,   a=False						,   a=2						,   a=99						,   a=0						,   a=32						,   a=5						,   a=4						,   a=0.1						,   a=0.1						,   a=512						,   a=2						,   a=0.02						,   a=2						,   a=4						,   a="last"						,   a=True						,   a=None						,   a=0						,   ):
         lowercase__    :   int      =       parent
         lowercase__    :   Optional[Any]      =       batch_size
         lowercase__    :   str      =       seq_length
         lowercase__    :   str      =       is_training
         lowercase__    :   Tuple      =       use_input_lengths
         lowercase__    :   List[Any]      =       use_token_type_ids
         lowercase__    :   int      =       use_labels
         lowercase__    :   Union[str, Any]      =       gelu_activation
         lowercase__    :   str      =       sinusoidal_embeddings
         lowercase__    :   Tuple      =       causal
         lowercase__    :   int      =       asm
         lowercase__    :   Optional[Any]      =       n_langs
         lowercase__    :   Union[str, Any]      =       vocab_size
         lowercase__    :   Any      =       n_special
         lowercase__    :   Any      =       hidden_size
         lowercase__    :   Dict      =       num_hidden_layers
         lowercase__    :   Any      =       num_attention_heads
         lowercase__    :   Union[str, Any]      =       hidden_dropout_prob
         lowercase__    :   List[str]      =       attention_probs_dropout_prob
         lowercase__    :   Union[str, Any]      =       max_position_embeddings
         lowercase__    :   Any      =       type_sequence_label_size
         lowercase__    :   str      =       initializer_range
         lowercase__    :   int      =       num_labels
         lowercase__    :   Optional[int]      =       num_choices
         lowercase__    :   str      =       summary_type
         lowercase__    :   List[str]      =       use_proj
         lowercase__    :   List[str]      =       scope
         lowercase__    :   Any      =       bos_token_id





    def       snake_case_				(      self):
         lowercase__    :   List[str]      =       ids_tensor([self.batch_size, self.seq_length]						,   self.vocab_size)
         lowercase__    :   Union[str, Any]      =       random_attention_mask([self.batch_size, self.seq_length])

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

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

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

         lowercase__    :   Optional[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       snake_case_				(      self):
         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       snake_case_				(      self						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   ):
         lowercase__    :   Tuple      =       XLMModel(config=_lowerCAmelCase)
         model.to(_lowerCAmelCase)
         model.eval()
         lowercase__    :   int      =       model(_lowerCAmelCase						,   lengths=_lowerCAmelCase						,   langs=_lowerCAmelCase)
         lowercase__    :   int      =       model(_lowerCAmelCase						,   langs=_lowerCAmelCase)
         lowercase__    :   Optional[Any]      =       model(_lowerCAmelCase)
         self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, self.seq_length, self.hidden_size))





    def       snake_case_				(      self						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   ):
         lowercase__    :   Optional[int]      =       XLMWithLMHeadModel(_lowerCAmelCase)
         model.to(_lowerCAmelCase)
         model.eval()

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





    def       snake_case_				(      self						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   ):
         lowercase__    :   Optional[int]      =       XLMForQuestionAnsweringSimple(_lowerCAmelCase)
         model.to(_lowerCAmelCase)
         model.eval()

         lowercase__    :   Optional[int]      =       model(_lowerCAmelCase)

         lowercase__    :   Dict      =       model(_lowerCAmelCase						,   start_positions=_lowerCAmelCase						,   end_positions=_lowerCAmelCase)
         lowercase__    :   Optional[int]      =       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       snake_case_				(      self						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   ):
         lowercase__    :   Any      =       XLMForQuestionAnswering(_lowerCAmelCase)
         model.to(_lowerCAmelCase)
         model.eval()

         lowercase__    :   Optional[int]      =       model(_lowerCAmelCase)

         lowercase__    :   Optional[int]      =       model(
             _lowerCAmelCase						,   start_positions=_lowerCAmelCase						,   end_positions=_lowerCAmelCase						,   cls_index=_lowerCAmelCase						,   is_impossible=_lowerCAmelCase						,   p_mask=_lowerCAmelCase						,   )

         lowercase__    :   Any      =       model(
             _lowerCAmelCase						,   start_positions=_lowerCAmelCase						,   end_positions=_lowerCAmelCase						,   cls_index=_lowerCAmelCase						,   is_impossible=_lowerCAmelCase						,   )

         (lowercase__			)    :   int      =       result_with_labels.to_tuple()

         lowercase__    :   Union[str, Any]      =       model(_lowerCAmelCase						,   start_positions=_lowerCAmelCase						,   end_positions=_lowerCAmelCase)

         (lowercase__			)    :   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       snake_case_				(      self						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   ):
         lowercase__    :   Optional[int]      =       XLMForSequenceClassification(_lowerCAmelCase)
         model.to(_lowerCAmelCase)
         model.eval()

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





    def       snake_case_				(      self						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   ):
         lowercase__    :   Dict      =       self.num_labels
         lowercase__    :   Any      =       XLMForTokenClassification(_lowerCAmelCase)
         model.to(_lowerCAmelCase)
         model.eval()

         lowercase__    :   List[str]      =       model(_lowerCAmelCase						,   attention_mask=_lowerCAmelCase						,   labels=_lowerCAmelCase)
         self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.seq_length, self.num_labels))





    def       snake_case_				(      self						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   a						,   ):
         lowercase__    :   Optional[Any]      =       self.num_choices
         lowercase__    :   List[str]      =       XLMForMultipleChoice(config=_lowerCAmelCase)
         model.to(_lowerCAmelCase)
         model.eval()
         lowercase__    :   Tuple      =       input_ids.unsqueeze(1).expand(-1						,   self.num_choices						,   -1).contiguous()
         lowercase__    :   List[str]      =       token_type_ids.unsqueeze(1).expand(-1						,   self.num_choices						,   -1).contiguous()
         lowercase__    :   int      =       input_mask.unsqueeze(1).expand(-1						,   self.num_choices						,   -1).contiguous()
         lowercase__    :   str      =       model(
             _lowerCAmelCase						,   attention_mask=_lowerCAmelCase						,   token_type_ids=_lowerCAmelCase						,   labels=_lowerCAmelCase						,   )
         self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.num_choices))





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



@require_torch
class       SCREAMING_SNAKE_CASE__					(__snake_case		,       __snake_case		,       __snake_case		,       unittest.TestCase     ):
    __lowerCamelCase						:       List[str]														=     (
        (
            XLMModel,
            XLMWithLMHeadModel,
            XLMForQuestionAnswering,
            XLMForSequenceClassification,
            XLMForQuestionAnsweringSimple,
            XLMForTokenClassification,
            XLMForMultipleChoice,
        )
        if is_torch_available()
        else ()
    )
    __lowerCamelCase						:       Tuple														=     (
        (XLMWithLMHeadModel,) if is_torch_available() else ()
    )  # TODO (PVP): Check other models whether language generation is also applicable
    __lowerCamelCase						:       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       snake_case_				(      self						,   a						,   a						,   a						,   a						,   a):
         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						,   a						,   a						,   a=False):
         lowercase__    :   Tuple      =       super()._prepare_for_class(_lowerCAmelCase						,   _lowerCAmelCase						,   return_labels=_lowerCAmelCase)

         if return_labels:
              if model_class.__name__ == "XLMForQuestionAnswering":
                   lowercase__    :   Optional[Any]      =       torch.zeros(
                       self.model_tester.batch_size						,   dtype=torch.long						,   device=_lowerCAmelCase)
                   lowercase__    :   Dict      =       torch.zeros(
                       self.model_tester.batch_size						,   dtype=torch.long						,   device=_lowerCAmelCase)

         return inputs_dict





    def       snake_case_				(      self):
         lowercase__    :   str      =       XLMModelTester(self)
         lowercase__    :   Tuple      =       ConfigTester(self						,   config_class=_lowerCAmelCase						,   emb_dim=37)





    def       snake_case_				(      self):
         self.config_tester.run_common_tests()





    def       snake_case_				(      self):
         lowercase__    :   Any      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_model(*_lowerCAmelCase)





    def       snake_case_				(      self):
         lowercase__    :   Any      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_lm_head(*_lowerCAmelCase)





    def       snake_case_				(      self):
         lowercase__    :   Union[str, Any]      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_simple_qa(*_lowerCAmelCase)





    def       snake_case_				(      self):
         lowercase__    :   Tuple      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_qa(*_lowerCAmelCase)





    def       snake_case_				(      self):
         lowercase__    :   List[str]      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_sequence_classif(*_lowerCAmelCase)





    def       snake_case_				(      self):
         lowercase__    :   str      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_token_classif(*_lowerCAmelCase)





    def       snake_case_				(      self):
         lowercase__    :   int      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_for_multiple_choice(*_lowerCAmelCase)





    def       snake_case_				(      self						,   a						,   a						,   a						,   a						,   a						,   a=False						,   a=1):
         self.assertIsInstance(_lowerCAmelCase						,   _lowerCAmelCase)
         self.assertListEqual(
             [isinstance(_lowerCAmelCase						,   _lowerCAmelCase) for iter_attentions in attentions]						,   [True] * len(_lowerCAmelCase))
         self.assertEqual(len(_lowerCAmelCase)						,   (max_length - min_length) * num_beam_groups)

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

              lowercase__    :   Union[str, Any]      =       (
                  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(_lowerCAmelCase))





    def       snake_case_				(      self						,   a						,   a						,   a						,   a						,   a						,   a=False						,   a=1):
         self.assertIsInstance(_lowerCAmelCase						,   _lowerCAmelCase)
         self.assertListEqual(
             [isinstance(_lowerCAmelCase						,   _lowerCAmelCase) for iter_hidden_states in hidden_states]						,   [True] * len(_lowerCAmelCase)						,   )
         self.assertEqual(len(_lowerCAmelCase)						,   (max_length - min_length) * num_beam_groups)

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





    @slow
    def       snake_case_				(      self):
         for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              lowercase__    :   Union[str, Any]      =       XLMModel.from_pretrained(_lowerCAmelCase)
              self.assertIsNotNone(_lowerCAmelCase)



@require_torch
class       SCREAMING_SNAKE_CASE__					(unittest.TestCase     ):





    @slow
    def       snake_case_				(      self):
         lowercase__    :   Any      =       XLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048')
         model.to(_lowerCAmelCase)
         lowercase__    :   Optional[Any]      =       torch.tensor([[14, 447]]						,   dtype=torch.long						,   device=_lowerCAmelCase)  # the president
         lowercase__    :   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
         lowercase__    :   List[Any]      =       model.generate(_lowerCAmelCase						,   do_sample=_lowerCAmelCase)
         self.assertListEqual(output_ids[0].cpu().numpy().tolist()						,   _lowerCAmelCase)







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


_UpperCAmelCase		:  int	=   {"configuration_swin": ["SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP", "SwinConfig", "SwinOnnxConfig"]}


try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCAmelCase		:  List[str]	=   [
         "SWIN_PRETRAINED_MODEL_ARCHIVE_LIST",
         "SwinForImageClassification",
         "SwinForMaskedImageModeling",
         "SwinModel",
         "SwinPreTrainedModel",
         "SwinBackbone",
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCAmelCase		:  int	=   [
         "TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST",
         "TFSwinForImageClassification",
         "TFSwinForMaskedImageModeling",
         "TFSwinModel",
         "TFSwinPreTrainedModel",
     ]

if TYPE_CHECKING:
     from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_swin import (
              SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
              SwinBackbone,
              SwinForImageClassification,
              SwinForMaskedImageModeling,
              SwinModel,
              SwinPreTrainedModel,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_swin import (
              TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFSwinForImageClassification,
              TFSwinForMaskedImageModeling,
              TFSwinModel,
              TFSwinPreTrainedModel,
          )

else:
     import sys

     _UpperCAmelCase		:  int	=   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



