

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     :
  """simple docstring"""






  def __init__(      self			:   Optional[Any]	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   str=13	,							lowerCAmelCase			:   Optional[Any]=7	,							lowerCAmelCase			:   str=True	,							lowerCAmelCase			:   Optional[Any]=True	,							lowerCAmelCase			:   Optional[Any]=True	,							lowerCAmelCase			:   List[Any]=True	,							lowerCAmelCase			:   Union[str, Any]=True	,							lowerCAmelCase			:   Dict=False	,							lowerCAmelCase			:   Any=False	,							lowerCAmelCase			:   Union[str, Any]=False	,							lowerCAmelCase			:   Optional[Any]=2	,							lowerCAmelCase			:   Tuple=99	,							lowerCAmelCase			:   Dict=0	,							lowerCAmelCase			:   str=32	,							lowerCAmelCase			:   Tuple=5	,							lowerCAmelCase			:   Any=4	,							lowerCAmelCase			:   List[str]=0.1	,							lowerCAmelCase			:   str=0.1	,							lowerCAmelCase			:   str=5_12	,							lowerCAmelCase			:   Optional[Any]=2	,							lowerCAmelCase			:   Tuple=0.02	,							lowerCAmelCase			:   Optional[int]=2	,							lowerCAmelCase			:   List[str]=4	,							lowerCAmelCase			:   Tuple="last"	,							lowerCAmelCase			:   Tuple=True	,							lowerCAmelCase			:   List[str]=None	,							lowerCAmelCase			:   List[str]=0	,							)							->     Union[str, Any]:






     """simple docstring"""







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







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






     """simple docstring"""







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

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

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

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

     __lowerCAmelCase  :      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 	SCREAMING_SNAKE_CASE   (      self			:   Union[str, Any]   )							->     Optional[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 	SCREAMING_SNAKE_CASE   (      self			:   Any	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   int	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   int	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   Optional[int]	,							)							->     Tuple:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						XLMModel(config=lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()
     __lowerCAmelCase  :      Optional[Any]    =						model(lowerCAmelCase	,							lengths=lowerCAmelCase	,							langs=lowerCAmelCase   )
     __lowerCAmelCase  :      Tuple    =						model(lowerCAmelCase	,							langs=lowerCAmelCase   )
     __lowerCAmelCase  :      Any    =						model(lowerCAmelCase   )
     self.parent.assertEqual(result.last_hidden_state.shape	,							(self.batch_size, self.seq_length, self.hidden_size)   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Tuple	,							lowerCAmelCase			:   Optional[Any]	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Optional[Any]	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   int	,							lowerCAmelCase			:   Any	,							)							->     List[str]:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[Any]    =						XLMWithLMHeadModel(lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()

     __lowerCAmelCase  :      Tuple    =						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 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   Optional[int]	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   int	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   List[Any]	,							)							->     Tuple:






     """simple docstring"""







     __lowerCAmelCase  :      Tuple    =						XLMForQuestionAnsweringSimple(lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()

     __lowerCAmelCase  :      Union[str, Any]    =						model(lowerCAmelCase   )

     __lowerCAmelCase  :      Optional[int]    =						model(lowerCAmelCase	,							start_positions=lowerCAmelCase	,							end_positions=lowerCAmelCase   )
     __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			:   Optional[int]	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Optional[int]	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   Optional[Any]	,							)							->     Dict:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[int]    =						XLMForQuestionAnswering(lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()

     __lowerCAmelCase  :      Dict    =						model(lowerCAmelCase   )

     __lowerCAmelCase  :      List[str]    =						model(
         lowerCAmelCase	,							start_positions=lowerCAmelCase	,							end_positions=lowerCAmelCase	,							cls_index=lowerCAmelCase	,							is_impossible=lowerCAmelCase	,							p_mask=lowerCAmelCase	,							)

     __lowerCAmelCase  :      Optional[int]    =						model(
         lowerCAmelCase	,							start_positions=lowerCAmelCase	,							end_positions=lowerCAmelCase	,							cls_index=lowerCAmelCase	,							is_impossible=lowerCAmelCase	,							)

     ((__lowerCAmelCase)   ,)  :      List[str]    =						result_with_labels.to_tuple()

     __lowerCAmelCase  :      Tuple    =						model(lowerCAmelCase	,							start_positions=lowerCAmelCase	,							end_positions=lowerCAmelCase   )

     ((__lowerCAmelCase)   ,)  :      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 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]	,							lowerCAmelCase			:   Optional[int]	,							lowerCAmelCase			:   int	,							lowerCAmelCase			:   int	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   Optional[Any]	,							lowerCAmelCase			:   Optional[Any]	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   Optional[Any]	,							)							->     List[str]:






     """simple docstring"""







     __lowerCAmelCase  :      List[Any]    =						XLMForSequenceClassification(lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()

     __lowerCAmelCase  :      Dict    =						model(lowerCAmelCase   )
     __lowerCAmelCase  :      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 	SCREAMING_SNAKE_CASE   (      self			:   Union[str, Any]	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Optional[Any]	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   int	,							)							->     int:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[Any]    =						self.num_labels
     __lowerCAmelCase  :      Tuple    =						XLMForTokenClassification(lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()

     __lowerCAmelCase  :      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 	SCREAMING_SNAKE_CASE   (      self			:   List[str]	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   int	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   Optional[Any]	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   str	,							)							->     Tuple:






     """simple docstring"""







     __lowerCAmelCase  :      Dict    =						self.num_choices
     __lowerCAmelCase  :      Dict    =						XLMForMultipleChoice(config=lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()
     __lowerCAmelCase  :      List[Any]    =						input_ids.unsqueeze(1   ).expand(-1	,							self.num_choices	,							-1   ).contiguous()
     __lowerCAmelCase  :      Union[str, Any]    =						token_type_ids.unsqueeze(1   ).expand(-1	,							self.num_choices	,							-1   ).contiguous()
     __lowerCAmelCase  :      Union[str, Any]    =						input_mask.unsqueeze(1   ).expand(-1	,							self.num_choices	,							-1   ).contiguous()
     __lowerCAmelCase  :      int    =						model(
         lowerCAmelCase	,							attention_mask=lowerCAmelCase	,							token_type_ids=lowerCAmelCase	,							labels=lowerCAmelCase	,							)
     self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.num_choices)   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   str   )							->     List[str]:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						self.prepare_config_and_inputs()
     (
         (
         __lowerCAmelCase
     )   ,(
         __lowerCAmelCase
     )   ,(
         __lowerCAmelCase
     )   ,(
         __lowerCAmelCase
     )   ,(
         __lowerCAmelCase
     )   ,(
         __lowerCAmelCase
     )   ,(
         __lowerCAmelCase
     )   ,(
         __lowerCAmelCase
     )   ,(
         __lowerCAmelCase
     )   ,
     )  :      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 SCREAMING_SNAKE_CASE     (  a_       ,							a_       ,							a_       ,							unittest.TestCase			):
  """simple docstring"""






  lowerCamelCase   :      int										=(
      (
          XLMModel,
          XLMWithLMHeadModel,
          XLMForQuestionAnswering,
          XLMForSequenceClassification,
          XLMForQuestionAnsweringSimple,
          XLMForTokenClassification,
          XLMForMultipleChoice,
      )
      if is_torch_available()
      else ()
  )
  lowerCamelCase   :      str										=(
      (XLMWithLMHeadModel,) if is_torch_available() else ()
  )  # TODO (PVP): Check other models whether language generation is also applicable
  lowerCamelCase   :      Optional[int]										=(
      {
          "feature-extraction": XLMModel,
          "fill-mask": XLMWithLMHeadModel,
          "question-answering": XLMForQuestionAnsweringSimple,
          "text-classification": XLMForSequenceClassification,
          "text-generation": XLMWithLMHeadModel,
          "token-classification": XLMForTokenClassification,
          "zero-shot": XLMForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Dict	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   int	,							lowerCAmelCase			:   str   )							->     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 	SCREAMING_SNAKE_CASE   (      self			:   int	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Optional[int]=False   )							->     List[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[int]    =						super()._prepare_for_class(lowerCAmelCase	,							lowerCAmelCase	,							return_labels=lowerCAmelCase   )

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

     return inputs_dict







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






     """simple docstring"""







     __lowerCAmelCase  :      Any    =						XLMModelTester(self   )
     __lowerCAmelCase  :      Tuple    =						ConfigTester(self	,							config_class=lowerCAmelCase	,							emb_dim=37   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   int   )							->     Any:






     """simple docstring"""







     self.config_tester.run_common_tests()







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]   )							->     str:






     """simple docstring"""







     __lowerCAmelCase  :      Union[str, Any]    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_model(*lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Tuple   )							->     Any:






     """simple docstring"""







     __lowerCAmelCase  :      List[str]    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_lm_head(*lowerCAmelCase   )







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






     """simple docstring"""







     __lowerCAmelCase  :      Union[str, Any]    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_simple_qa(*lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   str   )							->     Optional[int]:






     """simple docstring"""







     __lowerCAmelCase  :      List[str]    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_qa(*lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]   )							->     Optional[int]:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[int]    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_sequence_classif(*lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]   )							->     List[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Any    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_token_classif(*lowerCAmelCase   )







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






     """simple docstring"""







     __lowerCAmelCase  :      Dict    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_for_multiple_choice(*lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Optional[Any]	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Union[str, Any]=False	,							lowerCAmelCase			:   List[Any]=1   )							->     List[str]:






     """simple docstring"""







     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
        __lowerCAmelCase  :      Optional[Any]    =						min_length + idx + 1
        __lowerCAmelCase  :      Dict    =						min_length + idx + 1

        __lowerCAmelCase  :      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(lowerCAmelCase   )   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[Any]	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   Optional[int]	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   int=False	,							lowerCAmelCase			:   Optional[int]=1   )							->     Dict:






     """simple docstring"""







     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
        __lowerCAmelCase  :      Dict    =						min_length + idx + 1
        __lowerCAmelCase  :      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(lowerCAmelCase   )	,							)
     pass







  @slow
  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]   )							->     Optional[int]:






     """simple docstring"""







     for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __lowerCAmelCase  :      Tuple    =						XLMModel.from_pretrained(lowerCAmelCase   )
        self.assertIsNotNone(lowerCAmelCase   )






@require_torch
class SCREAMING_SNAKE_CASE     (  unittest.TestCase			):
  """simple docstring"""






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






     """simple docstring"""







     __lowerCAmelCase  :      Union[str, Any]    =						XLMWithLMHeadModel.from_pretrained("""xlm-mlm-en-2048"""   )
     model.to(lowerCAmelCase   )
     __lowerCAmelCase  :      Dict    =						torch.tensor([[14, 4_47]]	,							dtype=torch.long	,							device=lowerCAmelCase   )  # the president
     __lowerCAmelCase  :      int    =						[
         14,
         4_47,
         14,
         4_47,
         14,
         4_47,
         14,
         4_47,
         14,
         4_47,
         14,
         4_47,
         14,
         4_47,
         14,
         4_47,
         14,
         4_47,
         14,
         4_47,
     ]  # 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  :      Optional[int]    =						model.generate(lowerCAmelCase	,							do_sample=lowerCAmelCase   )
     self.assertListEqual(output_ids[0].cpu().numpy().tolist()	,							lowerCAmelCase   )





import argparse
import json
import os
import time
import zipfile

from get_ci_error_statistics import download_artifact, get_artifacts_links

from transformers import logging


__UpperCAmelCase								=			logging.get_logger(__name__)





def      snake_case_				(__A					:						List[str] ,       __A					:						str					)      ->     int:
   __lowerCAmelCase  :      str    =						set()
   __lowerCAmelCase  :      int    =						[]

   def parse_line(__A					:						List[Any]					):
      for line in fp:
         if isinstance(__A ,       __A					):
            __lowerCAmelCase  :      str    =						line.decode("""UTF-8"""					)
         if "warnings summary (final)" in line:
            continue
         # This means we are outside the body of a warning
         elif not line.startswith(""" """					):
            # process a single warning and move it to `selected_warnings`.
            if len(__A					) > 0:
               __lowerCAmelCase  :      Tuple    =						"""\n""".join(__A					)
               # Only keep the warnings specified in `targets`
               if any(f''': {x}: ''' in warning for x in targets					):
                  selected_warnings.add(__A					)
               buffer.clear()
            continue
         else:
            __lowerCAmelCase  :      Optional[int]    =						line.strip()
            buffer.append(__A					)

   if from_gh:
      for filename in os.listdir(__A					):
         __lowerCAmelCase  :      Optional[Any]    =						os.path.join(__A ,       __A					)
         if not os.path.isdir(__A					):
            # read the file
            if filename != "warnings.txt":
               continue
            with open(__A					) as fp:
               parse_line(__A					)
   else:
      try:
         with zipfile.ZipFile(__A					) as z:
            for filename in z.namelist():
               if not os.path.isdir(__A					):
                  # read the file
                  if filename != "warnings.txt":
                     continue
                  with z.open(__A					) as fp:
                     parse_line(__A					)
      except Exception:
         logger.warning(
             f'''{artifact_path} is either an invalid zip file or something else wrong. This file is skipped.'''					)

   return selected_warnings





def      snake_case_				(__A					:						Dict ,       __A					:						Union[str, Any]					)      ->     Dict:
   __lowerCAmelCase  :      Any    =						set()

   __lowerCAmelCase  :      Optional[int]    =						[os.path.join(__A ,       __A					) for p in os.listdir(__A					) if (p.endswith(""".zip"""					) or from_gh)]
   for p in paths:
      selected_warnings.update(extract_warnings_from_single_artifact(__A ,       __A					)					)

   return selected_warnings


if __name__ == "__main__":





   def      snake_case_				(__A					:						int					)      ->     Tuple:
      return values.split(""","""					)

   __UpperCAmelCase								=			argparse.ArgumentParser()
   # Required parameters
   parser.add_argument("""--workflow_run_id""", type=str, required=True, help="""A GitHub Actions workflow run id.""")
   parser.add_argument(
       """--output_dir""",
       type=str,
       required=True,
       help="""Where to store the downloaded artifacts and other result files.""",
   )
   parser.add_argument("""--token""", default=None, type=str, help="""A token that has actions:read permission.""")
   # optional parameters
   parser.add_argument(
       """--targets""",
       default="""DeprecationWarning,UserWarning,FutureWarning""",
       type=list_str,
       help="""Comma-separated list of target warning(s) which we want to extract.""",
   )
   parser.add_argument(
       """--from_gh""",
       action="""store_true""",
       help="""If running from a GitHub action workflow and collecting warnings from its artifacts.""",
   )

   __UpperCAmelCase								=			parser.parse_args()

   __UpperCAmelCase								=			args.from_gh
   if from_gh:
      # The artifacts have to be downloaded using `actions/download-artifact@v3`
      pass
   else:
      os.makedirs(args.output_dir, exist_ok=True)

      # get download links
      __UpperCAmelCase								=			get_artifacts_links(args.workflow_run_id, token=args.token)
      with open(os.path.join(args.output_dir, """artifacts.json"""), """w""", encoding="""UTF-8""") as fp:
         json.dump(artifacts, fp, ensure_ascii=False, indent=4)

      # download artifacts
      for idx, (name, url) in enumerate(artifacts.items()):
         print(name)
         print(url)
         print("""=""" * 80)
         download_artifact(name, url, args.output_dir, args.token)
         # Be gentle to GitHub
         time.sleep(1)

    # extract warnings from artifacts
   __UpperCAmelCase								=			extract_warnings(args.output_dir, args.targets)
   __UpperCAmelCase								=			sorted(selected_warnings)
   with open(os.path.join(args.output_dir, """selected_warnings.json"""), """w""", encoding="""UTF-8""") as fp:
      json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)


