


"""simple docstring"""







from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import (
     TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
     TFXLMForMultipleChoice,
     TFXLMForQuestionAnsweringSimple,
     TFXLMForSequenceClassification,
     TFXLMForTokenClassification,
     TFXLMModel,
     TFXLMWithLMHeadModel,
     XLMConfig,
 )






class 		_A    :







    """simple docstring"""




    def __init__(   self							:       Any							,					__UpperCAmelCase							:       List[str]							,					):
      a		:						Optional[int] 				=     parent
      a		:						Optional[int] 				=     13
      a		:						str 				=     7
      a		:						Dict 				=     True
      a		:						Union[str, Any] 				=     True
      a		:						Tuple 				=     True
      a		:						Optional[int] 				=     True
      a		:						Any 				=     True
      a		:						Tuple 				=     False
      a		:						Tuple 				=     False
      a		:						str 				=     False
      a		:						List[str] 				=     2
      a		:						str 				=     99
      a		:						List[Any] 				=     0
      a		:						int 				=     32
      a		:						int 				=     2
      a		:						List[str] 				=     4
      a		:						str 				=     0.1
      a		:						str 				=     0.1
      a		:						Union[str, Any] 				=     512
      a		:						str 				=     16
      a		:						Dict 				=     2
      a		:						int 				=     0.02
      a		:						Any 				=     3
      a		:						str 				=     4
      a		:						Tuple 				=     "last"
      a		:						Dict 				=     True
      a		:						Union[str, Any] 				=     None
      a		:						Dict 				=     0






    def       __snake_case				(   self							:       str):
      a		:						Optional[int] 				=     ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size)
      a		:						Dict 				=     random_attention_mask([self.batch_size, self.seq_length]							,					dtype=tf.floataa)

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

      a		:						int 				=     None
      if self.use_token_type_ids:
        a		:						Union[str, Any] 				=     ids_tensor([self.batch_size, self.seq_length]							,					self.n_langs)

      a		:						Dict 				=     None
      a		:						str 				=     None
      a		:						Dict 				=     None
      if self.use_labels:
        a		:						Dict 				=     ids_tensor([self.batch_size]							,					self.type_sequence_label_size)
        a		:						Optional[Any] 				=     ids_tensor([self.batch_size, self.seq_length]							,					self.num_labels)
        a		:						str 				=     ids_tensor([self.batch_size]							,					2							,					dtype=tf.floataa)
        a		:						int 				=     ids_tensor([self.batch_size]							,					self.num_choices)

      a		:						str 				=     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							,					bos_token_id=self.bos_token_id							,					)

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






    def       __snake_case				(   self							:       Optional[int]							,					__UpperCAmelCase							:       Any							,					__UpperCAmelCase							:       str							,					__UpperCAmelCase							:       List[str]							,					__UpperCAmelCase							:       Tuple							,					__UpperCAmelCase							:       Tuple							,					__UpperCAmelCase							:       Dict							,					__UpperCAmelCase							:       Optional[Any]							,					__UpperCAmelCase							:       str							,					__UpperCAmelCase							:       str							,					):
      a		:						str 				=     TFXLMModel(config=__UpperCAmelCase)
      a		:						Optional[int] 				=     {"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
      a		:						Union[str, Any] 				=     model(__UpperCAmelCase)

      a		:						str 				=     [input_ids, input_mask]
      a		:						int 				=     model(__UpperCAmelCase)
      self.parent.assertEqual(result.last_hidden_state.shape							,					(self.batch_size, self.seq_length, self.hidden_size))






    def       __snake_case				(   self							:       Any							,					__UpperCAmelCase							:       Optional[Any]							,					__UpperCAmelCase							:       List[Any]							,					__UpperCAmelCase							:       Optional[Any]							,					__UpperCAmelCase							:       str							,					__UpperCAmelCase							:       Any							,					__UpperCAmelCase							:       str							,					__UpperCAmelCase							:       List[str]							,					__UpperCAmelCase							:       Dict							,					__UpperCAmelCase							:       Optional[Any]							,					):
      a		:						List[str] 				=     TFXLMWithLMHeadModel(__UpperCAmelCase)

      a		:						int 				=     {"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
      a		:						str 				=     model(__UpperCAmelCase)

      a		:						Optional[int] 				=     outputs

      self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.seq_length, self.vocab_size))






    def       __snake_case				(   self							:       List[Any]							,					__UpperCAmelCase							:       Dict							,					__UpperCAmelCase							:       Union[str, Any]							,					__UpperCAmelCase							:       Union[str, Any]							,					__UpperCAmelCase							:       Dict							,					__UpperCAmelCase							:       Dict							,					__UpperCAmelCase							:       int							,					__UpperCAmelCase							:       str							,					__UpperCAmelCase							:       Union[str, Any]							,					__UpperCAmelCase							:       int							,					):
      a		:						Optional[Any] 				=     TFXLMForQuestionAnsweringSimple(__UpperCAmelCase)

      a		:						Optional[int] 				=     {"input_ids": input_ids, "lengths": input_lengths}

      a		:						Optional[int] 				=     model(__UpperCAmelCase)

      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       __snake_case				(   self							:       Any							,					__UpperCAmelCase							:       Tuple							,					__UpperCAmelCase							:       List[Any]							,					__UpperCAmelCase							:       Optional[int]							,					__UpperCAmelCase							:       int							,					__UpperCAmelCase							:       Union[str, Any]							,					__UpperCAmelCase							:       List[Any]							,					__UpperCAmelCase							:       Tuple							,					__UpperCAmelCase							:       str							,					__UpperCAmelCase							:       int							,					):
      a		:						Optional[Any] 				=     TFXLMForSequenceClassification(__UpperCAmelCase)

      a		:						Dict 				=     {"input_ids": input_ids, "lengths": input_lengths}

      a		:						Optional[Any] 				=     model(__UpperCAmelCase)

      self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.type_sequence_label_size))






    def       __snake_case				(   self							:       List[str]							,					__UpperCAmelCase							:       Dict							,					__UpperCAmelCase							:       List[str]							,					__UpperCAmelCase							:       Any							,					__UpperCAmelCase							:       Tuple							,					__UpperCAmelCase							:       Dict							,					__UpperCAmelCase							:       Optional[Any]							,					__UpperCAmelCase							:       List[str]							,					__UpperCAmelCase							:       Tuple							,					__UpperCAmelCase							:       Union[str, Any]							,					):
      a		:						List[str] 				=     self.num_labels
      a		:						Dict 				=     TFXLMForTokenClassification(config=__UpperCAmelCase)
      a		:						Any 				=     {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
      a		:						List[str] 				=     model(__UpperCAmelCase)
      self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.seq_length, self.num_labels))






    def       __snake_case				(   self							:       Optional[Any]							,					__UpperCAmelCase							:       Union[str, Any]							,					__UpperCAmelCase							:       Union[str, Any]							,					__UpperCAmelCase							:       Optional[Any]							,					__UpperCAmelCase							:       int							,					__UpperCAmelCase							:       Any							,					__UpperCAmelCase							:       Dict							,					__UpperCAmelCase							:       Tuple							,					__UpperCAmelCase							:       Tuple							,					__UpperCAmelCase							:       Tuple							,					):
      a		:						Optional[int] 				=     self.num_choices
      a		:						Tuple 				=     TFXLMForMultipleChoice(config=__UpperCAmelCase)
      a		:						List[str] 				=     tf.tile(tf.expand_dims(__UpperCAmelCase							,					1)							,					(1, self.num_choices, 1))
      a		:						Tuple 				=     tf.tile(tf.expand_dims(__UpperCAmelCase							,					1)							,					(1, self.num_choices, 1))
      a		:						List[Any] 				=     tf.tile(tf.expand_dims(__UpperCAmelCase							,					1)							,					(1, self.num_choices, 1))
      a		:						Union[str, Any] 				=     {
          "input_ids": multiple_choice_inputs_ids,
          "attention_mask": multiple_choice_input_mask,
          "token_type_ids": multiple_choice_token_type_ids,
      }
      a		:						List[str] 				=     model(__UpperCAmelCase)
      self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.num_choices))






    def       __snake_case				(   self							:       Tuple):
      a		:						Optional[int] 				=     self.prepare_config_and_inputs()
      (
          (
          a
      )				,						(
          a
      )				,						(
          a
      )				,						(
          a
      )				,						(
          a
      )				,						(
          a
      )				,						(
          a
      )				,						(
          a
      )				,						(
          a
      )				,						
      )		:						Any 				=     config_and_inputs
      a		:						Union[str, Any] 				=     {
          "input_ids": input_ids,
          "token_type_ids": token_type_ids,
          "langs": token_type_ids,
          "lengths": input_lengths,
      }
      return config, inputs_dict






@require_tf
class 		_A    (    _a       ,_a       ,unittest.TestCase    ):







    """simple docstring"""




    UpperCAmelCase			:				List[str]      =			(
        (
            TFXLMModel,
            TFXLMWithLMHeadModel,
            TFXLMForSequenceClassification,
            TFXLMForQuestionAnsweringSimple,
            TFXLMForTokenClassification,
            TFXLMForMultipleChoice,
        )
        if is_tf_available()
        else ()
    )
    UpperCAmelCase			:				Any      =			(
        (TFXLMWithLMHeadModel,) if is_tf_available() else ()
    )  # TODO (PVP): Check other models whether language generation is also applicable
    UpperCAmelCase			:				Optional[Any]      =			(
        {
            """feature-extraction""": TFXLMModel,
            """fill-mask""": TFXLMWithLMHeadModel,
            """question-answering""": TFXLMForQuestionAnsweringSimple,
            """text-classification""": TFXLMForSequenceClassification,
            """text-generation""": TFXLMWithLMHeadModel,
            """token-classification""": TFXLMForTokenClassification,
            """zero-shot""": TFXLMForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    UpperCAmelCase			:				Optional[Any]      =			False
    UpperCAmelCase			:				Optional[int]      =			False






    def       __snake_case				(   self							:       Dict							,					__UpperCAmelCase							:       Optional[int]							,					__UpperCAmelCase							:       str							,					__UpperCAmelCase							:       Any							,					__UpperCAmelCase							:       List[str]							,					__UpperCAmelCase							:       Union[str, Any]):
      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       __snake_case				(   self							:       List[str]):
      a		:						Union[str, Any] 				=     TFXLMModelTester(self)
      a		:						Optional[int] 				=     ConfigTester(self							,					config_class=__UpperCAmelCase							,					emb_dim=37)






    def       __snake_case				(   self							:       List[str]):
      self.config_tester.run_common_tests()






    def       __snake_case				(   self							:       str):
      a		:						List[Any] 				=     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_model(*__UpperCAmelCase)






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






    def       __snake_case				(   self							:       Optional[int]):
      a		:						Optional[Any] 				=     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_qa(*__UpperCAmelCase)






    def       __snake_case				(   self							:       Optional[int]):
      a		:						Optional[int] 				=     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_sequence_classif(*__UpperCAmelCase)






    def       __snake_case				(   self							:       Union[str, Any]):
      a		:						Optional[int] 				=     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_for_token_classification(*__UpperCAmelCase)






    def       __snake_case				(   self							:       Any):
      a		:						Dict 				=     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_for_multiple_choice(*__UpperCAmelCase)






    @slow
    def       __snake_case				(   self							:       Optional[int]):
      for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a		:						Dict 				=     TFXLMModel.from_pretrained(__UpperCAmelCase)
        self.assertIsNotNone(__UpperCAmelCase)






@require_tf
class 		_A    (    unittest.TestCase    ):







    """simple docstring"""




    @slow
    def       __snake_case				(   self							:       Optional[int]):
      a		:						str 				=     TFXLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048")
      a		:						Dict 				=     tf.convert_to_tensor([[14, 447]]							,					dtype=tf.intaa)  # the president
      a		:						Tuple 				=     [
          14,
          447,
          14,
          447,
          14,
          447,
          14,
          447,
          14,
          447,
          14,
          447,
          14,
          447,
          14,
          447,
          14,
          447,
          14,
          447,
      ]  # the president the president the president the president the president the president the president the president the president the president
      # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
      a		:						str 				=     model.generate(__UpperCAmelCase							,					do_sample=__UpperCAmelCase)
      self.assertListEqual(output_ids[0].numpy().tolist()							,					__UpperCAmelCase)










"""simple docstring"""







from ..utils import DummyObject, requires_backends






class 		_A    (    metaclass=_a    ):







    """simple docstring"""




    UpperCAmelCase			:				int      =			["""torch""", """transformers""", """invisible_watermark"""]






    def __init__(   self							:       int							,					*__UpperCAmelCase							:       List[Any]							,					**__UpperCAmelCase							:       Optional[int]):
      requires_backends(self							,					["torch", "transformers", "invisible_watermark"])






    @classmethod
    def       __snake_case				(   cls							:       Optional[Any]							,					*__UpperCAmelCase							:       Any							,					**__UpperCAmelCase							:       int):
      requires_backends(cls							,					["torch", "transformers", "invisible_watermark"])






    @classmethod
    def       __snake_case				(   cls							:       List[Any]							,					*__UpperCAmelCase							:       Optional[Any]							,					**__UpperCAmelCase							:       Tuple):
      requires_backends(cls							,					["torch", "transformers", "invisible_watermark"])






class 		_A    (    metaclass=_a    ):







    """simple docstring"""




    UpperCAmelCase			:				Optional[int]      =			["""torch""", """transformers""", """invisible_watermark"""]






    def __init__(   self							:       int							,					*__UpperCAmelCase							:       Tuple							,					**__UpperCAmelCase							:       Tuple):
      requires_backends(self							,					["torch", "transformers", "invisible_watermark"])






    @classmethod
    def       __snake_case				(   cls							:       int							,					*__UpperCAmelCase							:       str							,					**__UpperCAmelCase							:       Optional[int]):
      requires_backends(cls							,					["torch", "transformers", "invisible_watermark"])






    @classmethod
    def       __snake_case				(   cls							:       Any							,					*__UpperCAmelCase							:       Optional[int]							,					**__UpperCAmelCase							:       Union[str, Any]):
      requires_backends(cls							,					["torch", "transformers", "invisible_watermark"])






