






import unittest

from transformers import XLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     XLMForMultipleChoice,
     XLMForQuestionAnswering,
     XLMForQuestionAnsweringSimple,
     XLMForSequenceClassification,
     XLMForTokenClassification,
     XLMModel,
     XLMWithLMHeadModel,
 )
 from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST





class       __a  :







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


      '''simple docstring'''






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







 def  __lowercase (		self :       int						):


      '''simple docstring'''






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

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

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

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


      '''simple docstring'''






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







 def  __lowercase (		self :       str							,       SCREAMING_SNAKE_CASE :       Tuple							,       SCREAMING_SNAKE_CASE :       str							,       SCREAMING_SNAKE_CASE :       int							,       SCREAMING_SNAKE_CASE :       List[str]							,       SCREAMING_SNAKE_CASE :       Union[str, Any]							,       SCREAMING_SNAKE_CASE :       Tuple							,       SCREAMING_SNAKE_CASE :       List[str]							,       SCREAMING_SNAKE_CASE :       Any							,       SCREAMING_SNAKE_CASE :       List[Any]							,       ):


      '''simple docstring'''






      UpperCamelCase__      :       int	    =  XLMWithLMHeadModel(SCREAMING_SNAKE_CASE						)
      model.to(SCREAMING_SNAKE_CASE						)
      model.eval()

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







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


      '''simple docstring'''






      UpperCamelCase__      :       Union[str, Any]	    =  XLMForQuestionAnsweringSimple(SCREAMING_SNAKE_CASE						)
      model.to(SCREAMING_SNAKE_CASE						)
      model.eval()

      UpperCamelCase__      :       str	    =  model(SCREAMING_SNAKE_CASE						)

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


      '''simple docstring'''






      UpperCamelCase__      :       Tuple	    =  XLMForQuestionAnswering(SCREAMING_SNAKE_CASE						)
      model.to(SCREAMING_SNAKE_CASE						)
      model.eval()

      UpperCamelCase__      :       List[Any]	    =  model(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__      :       str	    =  model(
          SCREAMING_SNAKE_CASE							,       start_positions=SCREAMING_SNAKE_CASE							,       end_positions=SCREAMING_SNAKE_CASE							,       cls_index=SCREAMING_SNAKE_CASE							,       is_impossible=SCREAMING_SNAKE_CASE							,       p_mask=SCREAMING_SNAKE_CASE							,       )

      UpperCamelCase__      :       Optional[int]	    =  model(
          SCREAMING_SNAKE_CASE							,       start_positions=SCREAMING_SNAKE_CASE							,       end_positions=SCREAMING_SNAKE_CASE							,       cls_index=SCREAMING_SNAKE_CASE							,       is_impossible=SCREAMING_SNAKE_CASE							,       )

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

      UpperCamelCase__      :       Optional[int]	    =  model(SCREAMING_SNAKE_CASE							,       start_positions=SCREAMING_SNAKE_CASE							,       end_positions=SCREAMING_SNAKE_CASE						)

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


      '''simple docstring'''






      UpperCamelCase__      :       List[str]	    =  XLMForSequenceClassification(SCREAMING_SNAKE_CASE						)
      model.to(SCREAMING_SNAKE_CASE						)
      model.eval()

      UpperCamelCase__      :       Dict	    =  model(SCREAMING_SNAKE_CASE						)
      UpperCamelCase__      :       Dict	    =  model(SCREAMING_SNAKE_CASE							,       labels=SCREAMING_SNAKE_CASE						)
      self.parent.assertEqual(result.loss.shape							,       ()						)
      self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.type_sequence_label_size)						)







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


      '''simple docstring'''






      UpperCamelCase__      :       str	    =  self.num_labels
      UpperCamelCase__      :       Any	    =  XLMForTokenClassification(SCREAMING_SNAKE_CASE						)
      model.to(SCREAMING_SNAKE_CASE						)
      model.eval()

      UpperCamelCase__      :       int	    =  model(SCREAMING_SNAKE_CASE							,       attention_mask=SCREAMING_SNAKE_CASE							,       labels=SCREAMING_SNAKE_CASE						)
      self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.seq_length, self.num_labels)						)







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


      '''simple docstring'''






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







 def  __lowercase (		self :       Optional[int]						):


      '''simple docstring'''






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







@require_torch
class       __a  (   A__	,       A__	,       A__	,       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	:	Dict				=			(
     {
         '''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 :       str							,       SCREAMING_SNAKE_CASE :       Tuple							,       SCREAMING_SNAKE_CASE :       Tuple							,       SCREAMING_SNAKE_CASE :       Tuple							,       SCREAMING_SNAKE_CASE :       List[Any]							,       SCREAMING_SNAKE_CASE :       Optional[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 :       str							,       SCREAMING_SNAKE_CASE :       Any							,       SCREAMING_SNAKE_CASE :       Dict							,       SCREAMING_SNAKE_CASE :       List[str]=False						):


      '''simple docstring'''






      UpperCamelCase__      :       Any	    =  super()._prepare_for_class(SCREAMING_SNAKE_CASE							,       SCREAMING_SNAKE_CASE							,       return_labels=SCREAMING_SNAKE_CASE						)

      if return_labels:
           if model_class.__name__ == "XLMForQuestionAnswering":
                UpperCamelCase__      :       Dict	    =  torch.zeros(
                    self.model_tester.batch_size							,       dtype=torch.long							,       device=SCREAMING_SNAKE_CASE						)
                UpperCamelCase__      :       int	    =  torch.zeros(
                    self.model_tester.batch_size							,       dtype=torch.long							,       device=SCREAMING_SNAKE_CASE						)

      return inputs_dict







 def  __lowercase (		self :       Optional[Any]						):


      '''simple docstring'''






      UpperCamelCase__      :       List[Any]	    =  XLMModelTester(self						)
      UpperCamelCase__      :       Optional[Any]	    =  ConfigTester(self							,       config_class=SCREAMING_SNAKE_CASE							,       emb_dim=37						)







 def  __lowercase (		self :       Optional[Any]						):


      '''simple docstring'''






      self.config_tester.run_common_tests()







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


      '''simple docstring'''






      UpperCamelCase__      :       int	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_model(*SCREAMING_SNAKE_CASE						)







 def  __lowercase (		self :       List[Any]						):


      '''simple docstring'''






      UpperCamelCase__      :       Any	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_lm_head(*SCREAMING_SNAKE_CASE						)







 def  __lowercase (		self :       str						):


      '''simple docstring'''






      UpperCamelCase__      :       str	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_simple_qa(*SCREAMING_SNAKE_CASE						)







 def  __lowercase (		self :       Optional[Any]						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[Any]	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_qa(*SCREAMING_SNAKE_CASE						)







 def  __lowercase (		self :       List[str]						):


      '''simple docstring'''






      UpperCamelCase__      :       List[str]	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_sequence_classif(*SCREAMING_SNAKE_CASE						)







 def  __lowercase (		self :       Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__      :       List[str]	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_token_classif(*SCREAMING_SNAKE_CASE						)







 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(*SCREAMING_SNAKE_CASE						)







 def  __lowercase (		self :       Optional[Any]							,       SCREAMING_SNAKE_CASE :       int							,       SCREAMING_SNAKE_CASE :       Optional[int]							,       SCREAMING_SNAKE_CASE :       Optional[Any]							,       SCREAMING_SNAKE_CASE :       Optional[int]							,       SCREAMING_SNAKE_CASE :       Any							,       SCREAMING_SNAKE_CASE :       int=False							,       SCREAMING_SNAKE_CASE :       Optional[int]=1						):


      '''simple docstring'''






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

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

           UpperCamelCase__      :       Dict	    =  (
               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(SCREAMING_SNAKE_CASE						)						)







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


      '''simple docstring'''






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

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







 @slow
 def  __lowercase (		self :       Any						):


      '''simple docstring'''






      for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__      :       List[Any]	    =  XLMModel.from_pretrained(SCREAMING_SNAKE_CASE						)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE						)







@require_torch
class       __a  (   unittest.TestCase	):







 @slow
 def  __lowercase (		self :       str						):


      '''simple docstring'''






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







from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase     :							Dict    =logging.get_logger(__name__)





class       __a  (   A__	):
 _lowerCAmelCase	:	Optional[int]				=			'''timm_backbone'''







 def __init__(		self :       Dict							,       SCREAMING_SNAKE_CASE :       Dict=None							,       SCREAMING_SNAKE_CASE :       List[Any]=3							,       SCREAMING_SNAKE_CASE :       List[Any]=True							,       SCREAMING_SNAKE_CASE :       Dict=True							,       SCREAMING_SNAKE_CASE :       Union[str, Any]=None							,       **SCREAMING_SNAKE_CASE :       int							,       ):


      '''simple docstring'''






      super().__init__(**SCREAMING_SNAKE_CASE						)
      UpperCamelCase__      :       Union[str, Any]	    =  backbone
      UpperCamelCase__      :       Dict	    =  num_channels
      UpperCamelCase__      :       str	    =  features_only
      UpperCamelCase__      :       Dict	    =  use_pretrained_backbone
      UpperCamelCase__      :       Tuple	    =  True
      UpperCamelCase__      :       List[Any]	    =  out_indices if out_indices is not None else (-1,)