





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

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__=13,		__magic_name__=7,		__magic_name__=True,		__magic_name__=True,		__magic_name__=True,		__magic_name__=True,		__magic_name__=True,		__magic_name__=False,		__magic_name__=False,		__magic_name__=False,		__magic_name__=2,		__magic_name__=99,		__magic_name__=0,		__magic_name__=32,		__magic_name__=5,		__magic_name__=4,		__magic_name__=0.1,		__magic_name__=0.1,		__magic_name__=512,		__magic_name__=2,		__magic_name__=0.02,		__magic_name__=2,		__magic_name__=4,		__magic_name__="last",		__magic_name__=True,		__magic_name__=None,		__magic_name__=0,		) ->      str:






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




   def       UpperCamelCase__  (    self							) ->      Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:				int          =      ids_tensor([self.batch_size, self.seq_length],		self.vocab_size							)
       UpperCamelCase__							:				int          =      random_attention_mask([self.batch_size, self.seq_length]							)

       UpperCamelCase__							:				Optional[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__							:				Union[str, Any]          =      None
       if self.use_token_type_ids:
           UpperCamelCase__							:				str          =      ids_tensor([self.batch_size, self.seq_length],		self.n_langs							)

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

       UpperCamelCase__							:				Any          =      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							) ->      int:






       """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,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		) ->      List[str]:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      XLMModel(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:				List[str]          =      model(__magic_name__,		lengths=__magic_name__,		langs=__magic_name__							)
       UpperCamelCase__							:				Tuple          =      model(__magic_name__,		langs=__magic_name__							)
       UpperCamelCase__							:				Union[str, Any]          =      model(__magic_name__							)
       self.parent.assertEqual(result.last_hidden_state.shape,		(self.batch_size, self.seq_length, self.hidden_size)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		) ->      List[Any]:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      XLMWithLMHeadModel(__magic_name__							)
       model.to(__magic_name__							)
       model.eval()

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




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		) ->      Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:				Tuple          =      XLMForQuestionAnsweringSimple(__magic_name__							)
       model.to(__magic_name__							)
       model.eval()

       UpperCamelCase__							:				List[str]          =      model(__magic_name__							)

       UpperCamelCase__							:				int          =      model(__magic_name__,		start_positions=__magic_name__,		end_positions=__magic_name__							)
       UpperCamelCase__							:				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       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				List[str]          =      XLMForQuestionAnswering(__magic_name__							)
       model.to(__magic_name__							)
       model.eval()

       UpperCamelCase__							:				Any          =      model(__magic_name__							)

       UpperCamelCase__							:				Tuple          =      model(
           __magic_name__,		start_positions=__magic_name__,		end_positions=__magic_name__,		cls_index=__magic_name__,		is_impossible=__magic_name__,		p_mask=__magic_name__,		)

       UpperCamelCase__							:				List[Any]          =      model(
           __magic_name__,		start_positions=__magic_name__,		end_positions=__magic_name__,		cls_index=__magic_name__,		is_impossible=__magic_name__,		)

       ((UpperCamelCase__)   ,)							:				Tuple          =      result_with_labels.to_tuple()

       UpperCamelCase__							:				Any          =      model(__magic_name__,		start_positions=__magic_name__,		end_positions=__magic_name__							)

       ((UpperCamelCase__)   ,)							:				int          =      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,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				Dict          =      XLMForSequenceClassification(__magic_name__							)
       model.to(__magic_name__							)
       model.eval()

       UpperCamelCase__							:				Dict          =      model(__magic_name__							)
       UpperCamelCase__							:				Union[str, Any]          =      model(__magic_name__,		labels=__magic_name__							)
       self.parent.assertEqual(result.loss.shape,		()							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.type_sequence_label_size)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      self.num_labels
       UpperCamelCase__							:				Optional[int]          =      XLMForTokenClassification(__magic_name__							)
       model.to(__magic_name__							)
       model.eval()

       UpperCamelCase__							:				List[Any]          =      model(__magic_name__,		attention_mask=__magic_name__,		labels=__magic_name__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.num_labels)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				Optional[int]          =      self.num_choices
       UpperCamelCase__							:				Optional[Any]          =      XLMForMultipleChoice(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:				Optional[Any]          =      input_ids.unsqueeze(1							).expand(-1,		self.num_choices,		-1							).contiguous()
       UpperCamelCase__							:				Tuple          =      token_type_ids.unsqueeze(1							).expand(-1,		self.num_choices,		-1							).contiguous()
       UpperCamelCase__							:				int          =      input_mask.unsqueeze(1							).expand(-1,		self.num_choices,		-1							).contiguous()
       UpperCamelCase__							:				Tuple          =      model(
           __magic_name__,		attention_mask=__magic_name__,		token_type_ids=__magic_name__,		labels=__magic_name__,		)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_choices)							)




   def       UpperCamelCase__  (    self							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				List[str]          =      self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,(
           UpperCamelCase__
       )   ,
       )							:				Optional[int]          =      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__       (	__lowerCamelCase			,				__lowerCamelCase			,				__lowerCamelCase			,				unittest.TestCase				):

   '''simple docstring'''


   a						:							List[Any]   		=		(
       (
           XLMModel,
           XLMWithLMHeadModel,
           XLMForQuestionAnswering,
           XLMForSequenceClassification,
           XLMForQuestionAnsweringSimple,
           XLMForTokenClassification,
           XLMForMultipleChoice,
       )
       if is_torch_available()
       else ()
   )
   a						:							Dict   		=		(
       (XLMWithLMHeadModel,) if is_torch_available() else ()
   )  # TODO (PVP): Check other models whether language generation is also applicable
   a						:							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       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Tuple:






       """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,		__magic_name__,		__magic_name__,		__magic_name__=False							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      super()._prepare_for_class(__magic_name__,		__magic_name__,		return_labels=__magic_name__							)

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

       return inputs_dict




   def       UpperCamelCase__  (    self							) ->      int:






       """simple docstring"""
       UpperCamelCase__							:				int          =      XLMModelTester(self							)
       UpperCamelCase__							:				Optional[int]          =      ConfigTester(self,		config_class=__magic_name__,		emb_dim=37							)




   def       UpperCamelCase__  (    self							) ->      Tuple:






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




   def       UpperCamelCase__  (    self							) ->      Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_model(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_lm_head(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Union[str, Any]:






       """simple docstring"""
       UpperCamelCase__							:				Tuple          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_simple_qa(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      str:






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_qa(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_sequence_classif(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      int:






       """simple docstring"""
       UpperCamelCase__							:				int          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_token_classif(*__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:				Dict          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_for_multiple_choice(*__magic_name__							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__=False,		__magic_name__=1							) ->      List[Any]:






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

       for idx, iter_attentions in enumerate(__magic_name__							):
           # adds PAD dummy token
           UpperCamelCase__							:				Optional[Any]          =      min_length + idx + 1
           UpperCamelCase__							:				str          =      min_length + idx + 1

           UpperCamelCase__							:				Optional[int]          =      (
               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(__magic_name__							)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__=False,		__magic_name__=1							) ->      Dict:






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

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




   @slow
   def       UpperCamelCase__  (    self							) ->      List[str]:






       """simple docstring"""
       for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__							:				Dict          =      XLMModel.from_pretrained(__magic_name__							)
           self.assertIsNotNone(__magic_name__							)






@require_torch
class     lowercase__       (	unittest.TestCase				):

   '''simple docstring'''


   @slow
   def       UpperCamelCase__  (    self							) ->      Union[str, Any]:






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








import re
from pathlib import Path
from unittest import TestCase

import pytest






@pytest.mark.integration
class     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Union[str, Any]:






       """simple docstring"""

       with open(__magic_name__,		encoding='''utf-8'''							) as input_file:
           UpperCamelCase__							:				Tuple          =      re.compile(R'''(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)'''							)
           UpperCamelCase__							:				str          =      input_file.read()
           UpperCamelCase__							:				List[Any]          =      regexp.search(__magic_name__							)

       return match




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Any:






       """simple docstring"""

       with open(__magic_name__,		encoding='''utf-8'''							) as input_file:
           UpperCamelCase__							:				Dict          =      re.compile(R'''#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()''',		re.DOTALL							)
           UpperCamelCase__							:				Any          =      input_file.read()
           # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search`
           UpperCamelCase__							:				Tuple          =      regexp.finditer(__magic_name__							)

       UpperCamelCase__							:				Dict          =      [match for match in matches if match is not None and match.group(1							) is not None]
       return matches[0] if matches else None




   def       UpperCamelCase__  (    self							) ->      List[Any]:






       """simple docstring"""
       UpperCamelCase__							:				int          =      Path('''./datasets'''							)
       UpperCamelCase__							:				Any          =      list(dataset_paths.absolute().glob('''**/*.py'''							)							)

       for dataset in dataset_files:
           if self._no_encoding_on_file_open(str(__magic_name__							)							):
               raise AssertionError(f"open(...) must use utf-8 encoding in {dataset}"							)




   def       UpperCamelCase__  (    self							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				Optional[int]          =      Path('''./datasets'''							)
       UpperCamelCase__							:				Optional[Any]          =      list(dataset_paths.absolute().glob('''**/*.py'''							)							)

       for dataset in dataset_files:
           if self._no_print_statements(str(__magic_name__							)							):
               raise AssertionError(f"print statement found in {dataset}. Use datasets.logger/logging instead."							)

