



'''simple docstring'''






import unittest

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

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


if is_torch_available():
     import torch

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

class 				a__:







    def __init__( self					:    Optional[int]		,     __snake_case					:    Dict		,     __snake_case					:    int=13		,     __snake_case					:    int=7		,     __snake_case					:    Optional[Any]=True		,     __snake_case					:    Tuple=True		,     __snake_case					:    Optional[Any]=True		,     __snake_case					:    Tuple=True		,     __snake_case					:    Union[str, Any]=True		,     __snake_case					:    Dict=False		,     __snake_case					:    List[str]=False		,     __snake_case					:    Optional[int]=False		,     __snake_case					:    Dict=2		,     __snake_case					:    int=99		,     __snake_case					:    int=0		,     __snake_case					:    Union[str, Any]=32		,     __snake_case					:    int=5		,     __snake_case					:    int=4		,     __snake_case					:    Optional[Any]=0.1		,     __snake_case					:    Dict=0.1		,     __snake_case					:    Dict=5_12		,     __snake_case					:    int=2		,     __snake_case					:    str=0.02		,     __snake_case					:    Any=2		,     __snake_case					:    Optional[Any]=4		,     __snake_case					:    str="last"		,     __snake_case					:    str=True		,     __snake_case					:    List[Any]=None		,     __snake_case					:    Dict=0		,     ):
      a		:   Tuple	  =	parent
      a		:   List[Any]	  =	batch_size
      a		:   Optional[int]	  =	seq_length
      a		:   Tuple	  =	is_training
      a		:   Union[str, Any]	  =	use_input_lengths
      a		:   List[Any]	  =	use_token_type_ids
      a		:   Any	  =	use_labels
      a		:   Union[str, Any]	  =	gelu_activation
      a		:   Union[str, Any]	  =	sinusoidal_embeddings
      a		:   int	  =	causal
      a		:   int	  =	asm
      a		:   str	  =	n_langs
      a		:   Any	  =	vocab_size
      a		:   Union[str, Any]	  =	n_special
      a		:   List[Any]	  =	hidden_size
      a		:   Optional[Any]	  =	num_hidden_layers
      a		:   Dict	  =	num_attention_heads
      a		:   int	  =	hidden_dropout_prob
      a		:   Optional[Any]	  =	attention_probs_dropout_prob
      a		:   List[str]	  =	max_position_embeddings
      a		:   List[str]	  =	type_sequence_label_size
      a		:   List[Any]	  =	initializer_range
      a		:   Dict	  =	num_labels
      a		:   Dict	  =	num_choices
      a		:   int	  =	summary_type
      a		:   Dict	  =	use_proj
      a		:   Dict	  =	scope
      a		:   str	  =	bos_token_id







    def      lowercase_			( self					:    Dict      ):
      a		:   List[Any]	  =	ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size      )
      a		:   List[Any]	  =	random_attention_mask([self.batch_size, self.seq_length]      )

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

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

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

      a		:   Dict	  =	self.get_config()

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







    def      lowercase_			( self					:    List[Any]      ):
      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					:    Any		,     __snake_case					:    Tuple		,     __snake_case					:    Dict		,     __snake_case					:    Optional[int]		,     __snake_case					:    Dict		,     __snake_case					:    Tuple		,     __snake_case					:    Tuple		,     __snake_case					:    Optional[int]		,     __snake_case					:    Optional[int]		,     __snake_case					:    Dict		,     ):
      a		:   Tuple	  =	XLMModel(config=__snake_case      )
      model.to(__snake_case      )
      model.eval()
      a		:   Tuple	  =	model(__snake_case		,     lengths=__snake_case		,     langs=__snake_case      )
      a		:   Optional[Any]	  =	model(__snake_case		,     langs=__snake_case      )
      a		:   Union[str, Any]	  =	model(__snake_case      )
      self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)      )







    def      lowercase_			( self					:    List[Any]		,     __snake_case					:    int		,     __snake_case					:    str		,     __snake_case					:    int		,     __snake_case					:    Tuple		,     __snake_case					:    List[str]		,     __snake_case					:    Optional[Any]		,     __snake_case					:    Any		,     __snake_case					:    List[Any]		,     __snake_case					:    Optional[Any]		,     ):
      a		:   List[str]	  =	XLMWithLMHeadModel(__snake_case      )
      model.to(__snake_case      )
      model.eval()

      a		:   List[str]	  =	model(__snake_case		,     token_type_ids=__snake_case		,     labels=__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					:    int		,     __snake_case					:    Any		,     __snake_case					:    Any		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    Dict		,     __snake_case					:    Optional[Any]		,     __snake_case					:    List[Any]		,     __snake_case					:    Any		,     __snake_case					:    Tuple		,     __snake_case					:    Union[str, Any]		,     ):
      a		:   List[str]	  =	XLMForQuestionAnsweringSimple(__snake_case      )
      model.to(__snake_case      )
      model.eval()

      a		:   Tuple	  =	model(__snake_case      )

      a		:   int	  =	model(__snake_case		,     start_positions=__snake_case		,     end_positions=__snake_case      )
      a		:   Any	  =	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]		,     __snake_case					:    str		,     __snake_case					:    Optional[int]		,     __snake_case					:    str		,     __snake_case					:    List[Any]		,     __snake_case					:    Dict		,     __snake_case					:    Any		,     __snake_case					:    List[str]		,     __snake_case					:    Dict		,     __snake_case					:    Optional[int]		,     ):
      a		:   str	  =	XLMForQuestionAnswering(__snake_case      )
      model.to(__snake_case      )
      model.eval()

      a		:   Dict	  =	model(__snake_case      )

      a		:   Tuple	  =	model(
          __snake_case		,     start_positions=__snake_case		,     end_positions=__snake_case		,     cls_index=__snake_case		,     is_impossible=__snake_case		,     p_mask=__snake_case		,     )

      a		:   Optional[Any]	  =	model(
          __snake_case		,     start_positions=__snake_case		,     end_positions=__snake_case		,     cls_index=__snake_case		,     is_impossible=__snake_case		,     )

      ((a)  ,      )		:   str	  =	result_with_labels.to_tuple()

      a		:   Dict	  =	model(__snake_case		,     start_positions=__snake_case		,     end_positions=__snake_case      )

      ((a)  ,      )		:   Any	  =	result_with_labels.to_tuple()

      self.parent.assertEqual(result_with_labels.loss.shape		,     ()      )
      self.parent.assertEqual(result.start_top_log_probs.shape		,     (self.batch_size, model.config.start_n_top)      )
      self.parent.assertEqual(result.start_top_index.shape		,     (self.batch_size, model.config.start_n_top)      )
      self.parent.assertEqual(
          result.end_top_log_probs.shape		,     (self.batch_size, model.config.start_n_top * model.config.end_n_top)      )
      self.parent.assertEqual(
          result.end_top_index.shape		,     (self.batch_size, model.config.start_n_top * model.config.end_n_top)      )
      self.parent.assertEqual(result.cls_logits.shape		,     (self.batch_size,)      )







    def      lowercase_			( self					:    Any		,     __snake_case					:    Optional[int]		,     __snake_case					:    List[Any]		,     __snake_case					:    Any		,     __snake_case					:    int		,     __snake_case					:    str		,     __snake_case					:    Optional[int]		,     __snake_case					:    Tuple		,     __snake_case					:    str		,     __snake_case					:    Tuple		,     ):
      a		:   Dict	  =	XLMForSequenceClassification(__snake_case      )
      model.to(__snake_case      )
      model.eval()

      a		:   Union[str, Any]	  =	model(__snake_case      )
      a		:   Optional[int]	  =	model(__snake_case		,     labels=__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]		,     __snake_case					:    Optional[Any]		,     __snake_case					:    List[str]		,     __snake_case					:    Optional[int]		,     __snake_case					:    Any		,     __snake_case					:    Any		,     __snake_case					:    Optional[int]		,     __snake_case					:    Dict		,     __snake_case					:    Any		,     __snake_case					:    Dict		,     ):
      a		:   Optional[Any]	  =	self.num_labels
      a		:   Any	  =	XLMForTokenClassification(__snake_case      )
      model.to(__snake_case      )
      model.eval()

      a		:   Tuple	  =	model(__snake_case		,     attention_mask=__snake_case		,     labels=__snake_case      )
      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)      )







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







    def      lowercase_			( self					:    Tuple      ):
      a		:   Optional[Any]	  =	self.prepare_config_and_inputs()
      (
          (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      
      )		:   str	  =	config_and_inputs
      a		:   Tuple	  =	{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths}
      return config, inputs_dict

@require_torch
class 				a__(   lowerCamelCase__						,					lowerCamelCase__						,					lowerCamelCase__						,					unittest.TestCase      ):
    lowercase__					=       (
        (
            XLMModel,
            XLMWithLMHeadModel,
            XLMForQuestionAnswering,
            XLMForSequenceClassification,
            XLMForQuestionAnsweringSimple,
            XLMForTokenClassification,
            XLMForMultipleChoice,
        )
        if is_torch_available()
        else ()
    )
    lowercase__					=       (
        (XLMWithLMHeadModel,) if is_torch_available() else ()
    )  # TODO (PVP): Check other models whether language generation is also applicable
    lowercase__					=       (
        {
            """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		,     __snake_case					:    Optional[int]		,     __snake_case					:    Dict		,     __snake_case					:    Optional[Any]		,     __snake_case					:    str		,     __snake_case					:    Tuple      ):
      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					:    List[str]		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    List[str]=False      ):
      a		:   List[str]	  =	super()._prepare_for_class(__snake_case		,     __snake_case		,     return_labels=__snake_case      )

      if return_labels:
        if model_class.__name__ == "XLMForQuestionAnswering":
          a		:   Tuple	  =	torch.zeros(
              self.model_tester.batch_size		,     dtype=torch.long		,     device=__snake_case      )
          a		:   Any	  =	torch.zeros(
              self.model_tester.batch_size		,     dtype=torch.long		,     device=__snake_case      )

      return inputs_dict







    def      lowercase_			( self					:    Dict      ):
      a		:   List[Any]	  =	XLMModelTester(self      )
      a		:   Union[str, Any]	  =	ConfigTester(self		,     config_class=__snake_case		,     emb_dim=37      )







    def      lowercase_			( self					:    int      ):
      self.config_tester.run_common_tests()







    def      lowercase_			( self					:    Optional[int]      ):
      a		:   str	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_model(*__snake_case      )







    def      lowercase_			( self					:    List[str]      ):
      a		:   Optional[int]	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_lm_head(*__snake_case      )







    def      lowercase_			( self					:    Tuple      ):
      a		:   Optional[Any]	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_simple_qa(*__snake_case      )







    def      lowercase_			( self					:    str      ):
      a		:   List[str]	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_qa(*__snake_case      )







    def      lowercase_			( self					:    Tuple      ):
      a		:   str	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_sequence_classif(*__snake_case      )







    def      lowercase_			( self					:    str      ):
      a		:   str	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_token_classif(*__snake_case      )







    def      lowercase_			( self					:    int      ):
      a		:   Any	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_for_multiple_choice(*__snake_case      )







    def      lowercase_			( self					:    Dict		,     __snake_case					:    Any		,     __snake_case					:    Optional[Any]		,     __snake_case					:    str		,     __snake_case					:    Optional[Any]		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    Dict=False		,     __snake_case					:    List[Any]=1      ):
      self.assertIsInstance(__snake_case		,     __snake_case      )
      self.assertListEqual(
          [isinstance(__snake_case		,     __snake_case      ) for iter_attentions in attentions]		,     [True] * len(__snake_case      )      )
      self.assertEqual(len(__snake_case      )		,     (max_length - min_length) * num_beam_groups      )

      for idx, iter_attentions in enumerate(__snake_case      ):
        # adds PAD dummy token
        a		:   Any	  =	min_length + idx + 1
        a		:   str	  =	min_length + idx + 1

        a		:   str	  =	(
            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(__snake_case      )      )







    def      lowercase_			( self					:    int		,     __snake_case					:    List[str]		,     __snake_case					:    Any		,     __snake_case					:    List[Any]		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    Dict		,     __snake_case					:    Union[str, Any]=False		,     __snake_case					:    Union[str, Any]=1      ):
      self.assertIsInstance(__snake_case		,     __snake_case      )
      self.assertListEqual(
          [isinstance(__snake_case		,     __snake_case      ) for iter_hidden_states in hidden_states]		,     [True] * len(__snake_case      )		,     )
      self.assertEqual(len(__snake_case      )		,     (max_length - min_length) * num_beam_groups      )

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







    @slow
    def      lowercase_			( self					:    List[Any]      ):
      for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a		:   Tuple	  =	XLMModel.from_pretrained(__snake_case      )
        self.assertIsNotNone(__snake_case      )

@require_torch
class 				a__(   unittest.TestCase      ):







    @slow
    def      lowercase_			( self					:    Tuple      ):
      a		:   Any	  =	XLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048'      )
      model.to(__snake_case      )
      a		:   List[str]	  =	torch.tensor([[14, 4_47]]		,     dtype=torch.long		,     device=__snake_case      )  # the president
      a		:   Dict	  =	[
          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
      a		:   int	  =	model.generate(__snake_case		,     do_sample=__snake_case      )
      self.assertListEqual(output_ids[0].cpu().numpy().tolist()		,     __snake_case      )




'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


lowerCAmelCase:   List[str]					=		{
    'configuration_falcon': ['FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FalconConfig'],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     lowerCAmelCase:   str					=		[
         'FALCON_PRETRAINED_MODEL_ARCHIVE_LIST',
         'FalconForCausalLM',
         'FalconModel',
         'FalconPreTrainedModel',
         'FalconForSequenceClassification',
         'FalconForTokenClassification',
         'FalconForQuestionAnswering',
     ]


if TYPE_CHECKING:
     from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_falcon import (
              FALCON_PRETRAINED_MODEL_ARCHIVE_LIST,
              FalconForCausalLM,
              FalconForQuestionAnswering,
              FalconForSequenceClassification,
              FalconForTokenClassification,
              FalconModel,
              FalconPreTrainedModel,
          )


else:
     import sys

     lowerCAmelCase:   Optional[int]					=		_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)