

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







  """simple docstring"""




  def __init__(			self    : Any				,				UpperCAmelCase__    : List[Any]				,				UpperCAmelCase__    : Optional[Any]=1_3				,				UpperCAmelCase__    : Any=7				,				UpperCAmelCase__    : Optional[Any]=True				,				UpperCAmelCase__    : List[Any]=True				,				UpperCAmelCase__    : str=True				,				UpperCAmelCase__    : str=True				,				UpperCAmelCase__    : Optional[int]=True				,				UpperCAmelCase__    : Dict=False				,				UpperCAmelCase__    : List[Any]=False				,				UpperCAmelCase__    : List[Any]=False				,				UpperCAmelCase__    : Optional[Any]=2				,				UpperCAmelCase__    : List[str]=9_9				,				UpperCAmelCase__    : Optional[int]=0				,				UpperCAmelCase__    : str=3_2				,				UpperCAmelCase__    : str=5				,				UpperCAmelCase__    : Optional[int]=4				,				UpperCAmelCase__    : str=0.1				,				UpperCAmelCase__    : List[Any]=0.1				,				UpperCAmelCase__    : Dict=5_1_2				,				UpperCAmelCase__    : int=2				,				UpperCAmelCase__    : Dict=0.02				,				UpperCAmelCase__    : Union[str, Any]=2				,				UpperCAmelCase__    : Union[str, Any]=4				,				UpperCAmelCase__    : Union[str, Any]="last"				,				UpperCAmelCase__    : Tuple=True				,				UpperCAmelCase__    : Any=None				,				UpperCAmelCase__    : List[Any]=0				,				)					->List[str]:
        """simple docstring"""







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


  def  _lowercase      (			self    : str		)					->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Dict       =   ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size		)
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   random_attention_mask([self.batch_size, self.seq_length]		)

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

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

        SCREAMING_SNAKE_CASE :      Any       =   None
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   None
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   None
        if self.use_labels:
              SCREAMING_SNAKE_CASE :      str       =   ids_tensor([self.batch_size]				,				self.type_sequence_label_size		)
              SCREAMING_SNAKE_CASE :      Union[str, Any]       =   ids_tensor([self.batch_size, self.seq_length]				,				self.num_labels		)
              SCREAMING_SNAKE_CASE :      Any       =   ids_tensor([self.batch_size]				,				2		).float()
              SCREAMING_SNAKE_CASE :      str       =   ids_tensor([self.batch_size]				,				self.num_choices		)

        SCREAMING_SNAKE_CASE :      Optional[int]       =   self.get_config()

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


  def  _lowercase      (			self    : Optional[Any]		)					->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    : List[str]				,				UpperCAmelCase__    : Any				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : int				,				)					->Union[str, Any]:
        """simple docstring"""







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


  def  _lowercase      (			self    : Dict				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : List[str]				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : Dict				,				)					->Dict:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[str]       =   XLMWithLMHeadModel(UpperCAmelCase__		)
        model.to(UpperCAmelCase__		)
        model.eval()

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


  def  _lowercase      (			self    : Tuple				,				UpperCAmelCase__    : Tuple				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : List[Any]				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Any				,				UpperCAmelCase__    : Dict				,				)					->int:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[Any]       =   XLMForQuestionAnsweringSimple(UpperCAmelCase__		)
        model.to(UpperCAmelCase__		)
        model.eval()

        SCREAMING_SNAKE_CASE :      Tuple       =   model(UpperCAmelCase__		)

        SCREAMING_SNAKE_CASE :      str       =   model(UpperCAmelCase__				,				start_positions=UpperCAmelCase__				,				end_positions=UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      List[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  _lowercase      (			self    : List[str]				,				UpperCAmelCase__    : Any				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : Any				,				UpperCAmelCase__    : Any				,				UpperCAmelCase__    : List[Any]				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : List[Any]				,				UpperCAmelCase__    : Dict				,				)					->str:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[int]       =   XLMForQuestionAnswering(UpperCAmelCase__		)
        model.to(UpperCAmelCase__		)
        model.eval()

        SCREAMING_SNAKE_CASE :      Dict       =   model(UpperCAmelCase__		)

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

        SCREAMING_SNAKE_CASE :      Any       =   model(
            UpperCAmelCase__				,				start_positions=UpperCAmelCase__				,				end_positions=UpperCAmelCase__				,				cls_index=UpperCAmelCase__				,				is_impossible=UpperCAmelCase__				,				)

        ((SCREAMING_SNAKE_CASE)     ,	) :      Union[str, Any]       =   result_with_labels.to_tuple()

        SCREAMING_SNAKE_CASE :      Optional[Any]       =   model(UpperCAmelCase__				,				start_positions=UpperCAmelCase__				,				end_positions=UpperCAmelCase__		)

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







        SCREAMING_SNAKE_CASE :      Optional[int]       =   XLMForSequenceClassification(UpperCAmelCase__		)
        model.to(UpperCAmelCase__		)
        model.eval()

        SCREAMING_SNAKE_CASE :      Optional[Any]       =   model(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      int       =   model(UpperCAmelCase__				,				labels=UpperCAmelCase__		)
        self.parent.assertEqual(result.loss.shape				,				()		)
        self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.type_sequence_label_size)		)


  def  _lowercase      (			self    : List[Any]				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : List[str]				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : Dict				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : List[str]				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : int				,				)					->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[str]       =   self.num_labels
        SCREAMING_SNAKE_CASE :      List[Any]       =   XLMForTokenClassification(UpperCAmelCase__		)
        model.to(UpperCAmelCase__		)
        model.eval()

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


  def  _lowercase      (			self    : Optional[int]				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : Tuple				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : Any				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : List[str]				,				UpperCAmelCase__    : List[Any]				,				)					->List[Any]:
        """simple docstring"""







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




  def  _lowercase      (			self    : str		)					->List[Any]:
        """simple docstring"""







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







@require_torch
class        a__       (   UpperCAmelCase	,			UpperCAmelCase	,			UpperCAmelCase	,			unittest.TestCase       ):







  """simple docstring"""




  UpperCAmelCase__      : List[Any]			       =(
      (
          XLMModel,
          XLMWithLMHeadModel,
          XLMForQuestionAnswering,
          XLMForSequenceClassification,
          XLMForQuestionAnsweringSimple,
          XLMForTokenClassification,
          XLMForMultipleChoice,
      )
      if is_torch_available()
      else ()
  )
  UpperCAmelCase__      : Union[str, Any]			       =(
      (XLMWithLMHeadModel,) if is_torch_available() else ()
  )  # TODO (PVP): Check other models whether language generation is also applicable
  UpperCAmelCase__      : 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  _lowercase      (			self    : Optional[Any]				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : Any		)					->Dict:
        """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]				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : List[Any]=False		)					->str:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[Any]       =   super()._prepare_for_class(UpperCAmelCase__				,				UpperCAmelCase__				,				return_labels=UpperCAmelCase__		)

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

        return inputs_dict


  def  _lowercase      (			self    : List[str]		)					->Dict:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[Any]       =   XLMModelTester(self		)
        SCREAMING_SNAKE_CASE :      Optional[int]       =   ConfigTester(self				,				config_class=UpperCAmelCase__				,				emb_dim=3_7		)


  def  _lowercase      (			self    : Optional[Any]		)					->Any:
        """simple docstring"""







        self.config_tester.run_common_tests()


  def  _lowercase      (			self    : List[Any]		)					->Union[str, Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      str       =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_model(*UpperCAmelCase__		)


  def  _lowercase      (			self    : Any		)					->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[str]       =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_lm_head(*UpperCAmelCase__		)


  def  _lowercase      (			self    : Any		)					->int:
        """simple docstring"""







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


  def  _lowercase      (			self    : Union[str, Any]		)					->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Dict       =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_qa(*UpperCAmelCase__		)


  def  _lowercase      (			self    : Optional[Any]		)					->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Dict       =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_sequence_classif(*UpperCAmelCase__		)


  def  _lowercase      (			self    : List[Any]		)					->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[Any]       =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_token_classif(*UpperCAmelCase__		)


  def  _lowercase      (			self    : List[Any]		)					->Tuple:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[int]       =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCAmelCase__		)


  def  _lowercase      (			self    : List[Any]				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : Any				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Tuple=False				,				UpperCAmelCase__    : Union[str, Any]=1		)					->Union[str, Any]:
        """simple docstring"""







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

        for idx, iter_attentions in enumerate(UpperCAmelCase__		):
              # adds PAD dummy token
              SCREAMING_SNAKE_CASE :      Dict       =   min_length + idx + 1
              SCREAMING_SNAKE_CASE :      int       =   min_length + idx + 1

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


  def  _lowercase      (			self    : Union[str, Any]				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Any				,				UpperCAmelCase__    : Tuple				,				UpperCAmelCase__    : Union[str, Any]=False				,				UpperCAmelCase__    : Optional[int]=1		)					->int:
        """simple docstring"""







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

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




  @slow
  def  _lowercase      (			self    : Union[str, Any]		)					->Tuple:
        """simple docstring"""







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







@require_torch
class        a__       (   unittest.TestCase       ):







  """simple docstring"""




  @slow
  def  _lowercase      (			self    : Optional[Any]		)					->Any:
        """simple docstring"""







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







def 					__lowercase      (     _A			)     ->						list[int]:
      SCREAMING_SNAKE_CASE :      int       =   [0 for i in range(len(_A			)			)]

      # initialize interval's left pointer and right pointer
      SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Dict       =   0, 0

      for i in range(1						,    len(_A			)			):
            # case when current index is inside the interval
            if i <= right_pointer:
                  SCREAMING_SNAKE_CASE :      Any       =   min(right_pointer - i + 1						,    z_result[i - left_pointer]			)
                  SCREAMING_SNAKE_CASE :      Optional[Any]       =   min_edge

            while go_next(_A						,    _A						,    _A			):
                  z_result[i] += 1

            # if new index's result gives us more right interval,
            # we've to update left_pointer and right_pointer
            if i + z_result[i] - 1 > right_pointer:
                  SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      List[Any]       =   i, i + z_result[i] - 1

      return z_result






def 					__lowercase      (     _A						,    _A						,    _A			)     ->						bool:
      return i + z_result[i] < len(_A			) and s[z_result[i]] == s[i + z_result[i]]






def 					__lowercase      (     _A						,    _A			)     ->						int:
      SCREAMING_SNAKE_CASE :      Any       =   0
      # concatenate 'pattern' and 'input_str' and call z_function
      # with concatenated string
      SCREAMING_SNAKE_CASE :      List[Any]       =   z_function(pattern + input_str			)

      for val in z_result:
            # if value is greater then length of the pattern string
            # that means this index is starting position of substring
            # which is equal to pattern string
            if val >= len(_A			):
                  answer += 1

      return answer


if __name__ == "__main__":
  import doctest

  doctest.testmod()




