

"""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      __lowercase			:

  def __init__(  self		:			Tuple					,A		:			Optional[Any]					,A		:			Union[str, Any]=13					,A		:			List[str]=7					,A		:			List[str]=True					,A		:			Optional[int]=True					,A		:			Any=True					,A		:			int=True					,A		:			List[str]=True					,A		:			Any=False					,A		:			int=False					,A		:			Tuple=False					,A		:			int=2					,A		:			Tuple=99					,A		:			Dict=0					,A		:			List[str]=32					,A		:			Dict=5					,A		:			Optional[int]=4					,A		:			Tuple=0.1					,A		:			int=0.1					,A		:			Union[str, Any]=512					,A		:			Any=2					,A		:			Optional[Any]=0.0_2					,A		:			Dict=2					,A		:			List[str]=4					,A		:			List[str]="last"					,A		:			Optional[Any]=True					,A		:			Dict=None					,A		:			Tuple=0					,):






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

  def    __lowercase       (  self		:			str    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Optional[Any]					   =					ids_tensor([self.batch_size, self.seq_length]					,self.vocab_size    )
   UpperCAmelCase__					:  List[Any]					   =					random_attention_mask([self.batch_size, self.seq_length]    )

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

   UpperCAmelCase__					:  List[Any]					   =					None
   if self.use_token_type_ids:
    UpperCAmelCase__					:  Tuple					   =					ids_tensor([self.batch_size, self.seq_length]					,self.n_langs    )

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

   UpperCAmelCase__					:  str					   =					self.get_config()

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

  def    __lowercase       (  self		:			List[str]    ):






   '''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    __lowercase       (  self		:			Tuple					,A		:			Optional[Any]					,A		:			Dict					,A		:			List[str]					,A		:			List[Any]					,A		:			Union[str, Any]					,A		:			Dict					,A		:			Dict					,A		:			Optional[Any]					,A		:			List[Any]					,):






   '''simple docstring'''
   UpperCAmelCase__					:  Any					   =					XLMModel(config=A    )
   model.to(A    )
   model.eval()
   UpperCAmelCase__					:  Union[str, Any]					   =					model(A					,lengths=A					,langs=A    )
   UpperCAmelCase__					:  Union[str, Any]					   =					model(A					,langs=A    )
   UpperCAmelCase__					:  Optional[int]					   =					model(A    )
   self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.hidden_size)    )

  def    __lowercase       (  self		:			List[str]					,A		:			Optional[Any]					,A		:			Any					,A		:			List[Any]					,A		:			Dict					,A		:			Union[str, Any]					,A		:			Any					,A		:			Tuple					,A		:			Union[str, Any]					,A		:			Any					,):






   '''simple docstring'''
   UpperCAmelCase__					:  Optional[Any]					   =					XLMWithLMHeadModel(A    )
   model.to(A    )
   model.eval()

   UpperCAmelCase__					:  str					   =					model(A					,token_type_ids=A					,labels=A    )
   self.parent.assertEqual(result.loss.shape					,()    )
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.vocab_size)    )

  def    __lowercase       (  self		:			int					,A		:			List[str]					,A		:			List[Any]					,A		:			Any					,A		:			Dict					,A		:			Union[str, Any]					,A		:			Union[str, Any]					,A		:			int					,A		:			List[Any]					,A		:			Optional[int]					,):






   '''simple docstring'''
   UpperCAmelCase__					:  Optional[int]					   =					XLMForQuestionAnsweringSimple(A    )
   model.to(A    )
   model.eval()

   UpperCAmelCase__					:  Any					   =					model(A    )

   UpperCAmelCase__					:  Dict					   =					model(A					,start_positions=A					,end_positions=A    )
   UpperCAmelCase__					:  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    __lowercase       (  self		:			Union[str, Any]					,A		:			Optional[int]					,A		:			List[str]					,A		:			Optional[int]					,A		:			Union[str, Any]					,A		:			Any					,A		:			Union[str, Any]					,A		:			Tuple					,A		:			int					,A		:			Optional[Any]					,):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					XLMForQuestionAnswering(A    )
   model.to(A    )
   model.eval()

   UpperCAmelCase__					:  str					   =					model(A    )

   UpperCAmelCase__					:  Optional[int]					   =					model(
       A					,start_positions=A					,end_positions=A					,cls_index=A					,is_impossible=A					,p_mask=A					,)

   UpperCAmelCase__					:  Tuple					   =					model(
       A					,start_positions=A					,end_positions=A					,cls_index=A					,is_impossible=A					,)

   (UpperCAmelCase__							)					:  int					   =					result_with_labels.to_tuple()

   UpperCAmelCase__					:  Tuple					   =					model(A					,start_positions=A					,end_positions=A    )

   (UpperCAmelCase__							)					:  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    __lowercase       (  self		:			int					,A		:			int					,A		:			Union[str, Any]					,A		:			Union[str, Any]					,A		:			Tuple					,A		:			Optional[Any]					,A		:			Tuple					,A		:			str					,A		:			Optional[Any]					,A		:			Optional[Any]					,):






   '''simple docstring'''
   UpperCAmelCase__					:  Dict					   =					XLMForSequenceClassification(A    )
   model.to(A    )
   model.eval()

   UpperCAmelCase__					:  Union[str, Any]					   =					model(A    )
   UpperCAmelCase__					:  Tuple					   =					model(A					,labels=A    )
   self.parent.assertEqual(result.loss.shape					,()    )
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.type_sequence_label_size)    )

  def    __lowercase       (  self		:			int					,A		:			Optional[int]					,A		:			Optional[Any]					,A		:			List[str]					,A		:			str					,A		:			Union[str, Any]					,A		:			Optional[Any]					,A		:			Optional[int]					,A		:			List[str]					,A		:			List[str]					,):






   '''simple docstring'''
   UpperCAmelCase__					:  int					   =					self.num_labels
   UpperCAmelCase__					:  List[Any]					   =					XLMForTokenClassification(A    )
   model.to(A    )
   model.eval()

   UpperCAmelCase__					:  Optional[Any]					   =					model(A					,attention_mask=A					,labels=A    )
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.num_labels)    )

  def    __lowercase       (  self		:			Optional[int]					,A		:			Dict					,A		:			int					,A		:			List[Any]					,A		:			Optional[Any]					,A		:			int					,A		:			Optional[int]					,A		:			Union[str, Any]					,A		:			List[Any]					,A		:			List[Any]					,):






   '''simple docstring'''
   UpperCAmelCase__					:  int					   =					self.num_choices
   UpperCAmelCase__					:  Dict					   =					XLMForMultipleChoice(config=A    )
   model.to(A    )
   model.eval()
   UpperCAmelCase__					:  str					   =					input_ids.unsqueeze(1    ).expand(-1					,self.num_choices					,-1    ).contiguous()
   UpperCAmelCase__					:  Any					   =					token_type_ids.unsqueeze(1    ).expand(-1					,self.num_choices					,-1    ).contiguous()
   UpperCAmelCase__					:  str					   =					input_mask.unsqueeze(1    ).expand(-1					,self.num_choices					,-1    ).contiguous()
   UpperCAmelCase__					:  Any					   =					model(
       A					,attention_mask=A					,token_type_ids=A					,labels=A					,)
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.num_choices)    )




  def    __lowercase       (  self		:			List[str]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Union[str, Any]					   =					self.prepare_config_and_inputs()
   (
       UpperCAmelCase__							
   )					:  str					   =					config_and_inputs
   UpperCAmelCase__					:  int					   =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths}
   return config, inputs_dict







@require_torch
class      __lowercase			(    lowercase__	,			lowercase__	,			lowercase__	,			unittest.TestCase					):
  snake_case_				       =       (
      (
          XLMModel,
          XLMWithLMHeadModel,
          XLMForQuestionAnswering,
          XLMForSequenceClassification,
          XLMForQuestionAnsweringSimple,
          XLMForTokenClassification,
          XLMForMultipleChoice,
      )
      if is_torch_available()
      else ()
  )
  snake_case_				       =       (
      (XLMWithLMHeadModel,) if is_torch_available() else ()
  )  # TODO (PVP): Check other models whether language generation is also applicable
  snake_case_				       =       (
      {
          """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    __lowercase       (  self		:			int					,A		:			List[str]					,A		:			Union[str, Any]					,A		:			str					,A		:			Optional[int]					,A		:			List[Any]    ):






   '''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    __lowercase       (  self		:			Optional[Any]					,A		:			Tuple					,A		:			Tuple					,A		:			List[str]=False    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					super()._prepare_for_class(A					,A					,return_labels=A    )

   if return_labels:
    if model_class.__name__ == "XLMForQuestionAnswering":
     UpperCAmelCase__					:  str					   =					torch.zeros(
         self.model_tester.batch_size					,dtype=torch.long					,device=A    )
     UpperCAmelCase__					:  Dict					   =					torch.zeros(
         self.model_tester.batch_size					,dtype=torch.long					,device=A    )

   return inputs_dict

  def    __lowercase       (  self		:			Optional[Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  str					   =					XLMModelTester(self    )
   UpperCAmelCase__					:  int					   =					ConfigTester(self					,config_class=A					,emb_dim=37    )

  def    __lowercase       (  self		:			Any    ):






   '''simple docstring'''
   self.config_tester.run_common_tests()

  def    __lowercase       (  self		:			Any    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Optional[int]					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_xlm_model(*A    )

  def    __lowercase       (  self		:			Any    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Any					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_xlm_lm_head(*A    )

  def    __lowercase       (  self		:			List[str]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Dict					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_xlm_simple_qa(*A    )

  def    __lowercase       (  self		:			Dict    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Union[str, Any]					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_xlm_qa(*A    )

  def    __lowercase       (  self		:			Union[str, Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Any					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_xlm_sequence_classif(*A    )

  def    __lowercase       (  self		:			int    ):






   '''simple docstring'''
   UpperCAmelCase__					:  int					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_xlm_token_classif(*A    )

  def    __lowercase       (  self		:			int    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Dict					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_xlm_for_multiple_choice(*A    )

  def    __lowercase       (  self		:			List[Any]					,A		:			Tuple					,A		:			List[Any]					,A		:			str					,A		:			str					,A		:			Union[str, Any]					,A		:			Optional[int]=False					,A		:			Dict=1    ):






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

   for idx, iter_attentions in enumerate(A    ):
    # adds PAD dummy token
    UpperCAmelCase__					:  Tuple					   =					min_length + idx + 1
    UpperCAmelCase__					:  Optional[Any]					   =					min_length + idx + 1

    UpperCAmelCase__					:  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(A    )    )

  def    __lowercase       (  self		:			Any					,A		:			str					,A		:			Tuple					,A		:			Any					,A		:			Any					,A		:			int					,A		:			Union[str, Any]=False					,A		:			str=1    ):






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

   for idx, iter_hidden_states in enumerate(A    ):
    # adds PAD dummy token
    UpperCAmelCase__					:  str					   =					min_length + idx + 1
    UpperCAmelCase__					:  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(A    )					,)
   pass




  @slow
  def    __lowercase       (  self		:			Optional[Any]    ):






   '''simple docstring'''
   for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    UpperCAmelCase__					:  Union[str, Any]					   =					XLMModel.from_pretrained(A    )
    self.assertIsNotNone(A    )







@require_torch
class      __lowercase			(    unittest.TestCase					):

  @slow
  def    __lowercase       (  self		:			List[Any]    ):






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








"""simple docstring"""




# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import sys
import warnings
from os.path import abspath, dirname, join


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
__UpperCAmelCase =				abspath(join(dirname(dirname(__file__)), 'src'))
sys.path.insert(1, git_repo_path)

# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action='ignore', category=FutureWarning)
def   lowerCAmelCase						(					__UpperCamelCase      ):
 '''simple docstring'''





 from diffusers.utils.testing_utils import pytest_addoption_shared

 pytest_addoption_shared(__UpperCamelCase      )




def   lowerCAmelCase						(					__UpperCamelCase      ):
 '''simple docstring'''





 from diffusers.utils.testing_utils import pytest_terminal_summary_main

 UpperCAmelCase__					:  Optional[Any]					   =					terminalreporter.config.getoption("""--make-reports"""      )
 if make_reports:
  pytest_terminal_summary_main(__UpperCamelCase							,    id=__UpperCamelCase      )





