

from __future__ import annotations

import unittest

from transformers import AlbertConfig, is_tf_available
from transformers.models.auto import get_values
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_MODEL_FOR_PRETRAINING_MAPPING
      from transformers.models.albert.modeling_tf_albert import (
          TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFAlbertForMaskedLM,
          TFAlbertForMultipleChoice,
          TFAlbertForPreTraining,
          TFAlbertForQuestionAnswering,
          TFAlbertForSequenceClassification,
          TFAlbertForTokenClassification,
          TFAlbertModel,
      )






class     __magic_name__     :


 def __init__(    self		:			str      ,    lowerCAmelCase__		:			Union[str, Any]      ,    lowerCAmelCase__		:			Union[str, Any]=1_3      ,    lowerCAmelCase__		:			Any=7      ,    lowerCAmelCase__		:			List[Any]=True      ,    lowerCAmelCase__		:			Any=True      ,    lowerCAmelCase__		:			Any=True      ,    lowerCAmelCase__		:			Optional[int]=True      ,    lowerCAmelCase__		:			List[str]=9_9      ,    lowerCAmelCase__		:			Optional[int]=1_6      ,    lowerCAmelCase__		:			Optional[Any]=3_2      ,    lowerCAmelCase__		:			Dict=2      ,    lowerCAmelCase__		:			Union[str, Any]=4      ,    lowerCAmelCase__		:			Optional[int]=3_7      ,    lowerCAmelCase__		:			Union[str, Any]="gelu"      ,    lowerCAmelCase__		:			List[Any]=0.1      ,    lowerCAmelCase__		:			str=0.1      ,    lowerCAmelCase__		:			Tuple=5_1_2      ,    lowerCAmelCase__		:			Tuple=1_6      ,    lowerCAmelCase__		:			Union[str, Any]=2      ,    lowerCAmelCase__		:			Optional[Any]=0.02      ,    lowerCAmelCase__		:			List[str]=3      ,    lowerCAmelCase__		:			Tuple=4      ,    lowerCAmelCase__		:			Optional[int]=None      ,    )     ->   List[str]:
   UpperCAmelCase          =	parent
   UpperCAmelCase          =	1_3
   UpperCAmelCase          =	7
   UpperCAmelCase          =	True
   UpperCAmelCase          =	True
   UpperCAmelCase          =	True
   UpperCAmelCase          =	True
   UpperCAmelCase          =	9_9
   UpperCAmelCase          =	1_6
   UpperCAmelCase          =	3_2
   UpperCAmelCase          =	2
   UpperCAmelCase          =	4
   UpperCAmelCase          =	3_7
   UpperCAmelCase          =	"gelu"
   UpperCAmelCase          =	0.1
   UpperCAmelCase          =	0.1
   UpperCAmelCase          =	5_1_2
   UpperCAmelCase          =	1_6
   UpperCAmelCase          =	2
   UpperCAmelCase          =	0.02
   UpperCAmelCase          =	3
   UpperCAmelCase          =	4
   UpperCAmelCase          =	None


 def _UpperCamelCase						(    self		:			int     )     ->   int:
   UpperCAmelCase          =	ids_tensor([self.batch_size, self.seq_length]      ,    self.vocab_size     )

   UpperCAmelCase          =	None
   if self.use_input_mask:
     UpperCAmelCase          =	random_attention_mask([self.batch_size, self.seq_length]     )

   UpperCAmelCase          =	None
   if self.use_token_type_ids:
     UpperCAmelCase          =	ids_tensor([self.batch_size, self.seq_length]      ,    self.type_vocab_size     )

   UpperCAmelCase          =	None
   UpperCAmelCase          =	None
   UpperCAmelCase          =	None
   if self.use_labels:
     UpperCAmelCase          =	ids_tensor([self.batch_size]      ,    self.type_sequence_label_size     )
     UpperCAmelCase          =	ids_tensor([self.batch_size, self.seq_length]      ,    self.num_labels     )
     UpperCAmelCase          =	ids_tensor([self.batch_size]      ,    self.num_choices     )

   UpperCAmelCase          =	AlbertConfig(
       vocab_size=self.vocab_size      ,    hidden_size=self.hidden_size      ,    embedding_size=self.embedding_size      ,    num_hidden_layers=self.num_hidden_layers      ,    num_attention_heads=self.num_attention_heads      ,    intermediate_size=self.intermediate_size      ,    hidden_act=self.hidden_act      ,    hidden_dropout_prob=self.hidden_dropout_prob      ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,    max_position_embeddings=self.max_position_embeddings      ,    type_vocab_size=self.type_vocab_size      ,    initializer_range=self.initializer_range      ,    )

   return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


 def _UpperCamelCase						(    self		:			List[Any]      ,    lowerCAmelCase__		:			Dict      ,    lowerCAmelCase__		:			Optional[int]      ,    lowerCAmelCase__		:			Any      ,    lowerCAmelCase__		:			Union[str, Any]      ,    lowerCAmelCase__		:			Tuple      ,    lowerCAmelCase__		:			List[Any]      ,    lowerCAmelCase__		:			Optional[Any]     )     ->   Dict:
   UpperCAmelCase          =	TFAlbertModel(config=lowerCAmelCase__     )
   # inputs = {'input_ids': input_ids,
   #           'attention_mask': input_mask,
   #           'token_type_ids': token_type_ids}
   # sequence_output, pooled_output = model(**inputs)
   UpperCAmelCase          =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
   UpperCAmelCase          =	model(lowerCAmelCase__     )

   UpperCAmelCase          =	[input_ids, input_mask]
   UpperCAmelCase          =	model(lowerCAmelCase__     )

   UpperCAmelCase          =	model(lowerCAmelCase__     )

   self.parent.assertEqual(result.last_hidden_state.shape      ,    (self.batch_size, self.seq_length, self.hidden_size)     )
   self.parent.assertEqual(result.pooler_output.shape      ,    (self.batch_size, self.hidden_size)     )


 def _UpperCamelCase						(    self		:			Any      ,    lowerCAmelCase__		:			Tuple      ,    lowerCAmelCase__		:			int      ,    lowerCAmelCase__		:			Optional[int]      ,    lowerCAmelCase__		:			List[str]      ,    lowerCAmelCase__		:			Union[str, Any]      ,    lowerCAmelCase__		:			List[Any]      ,    lowerCAmelCase__		:			Dict     )     ->   int:
   UpperCAmelCase          =	self.num_labels
   UpperCAmelCase          =	TFAlbertForPreTraining(config=lowerCAmelCase__     )
   UpperCAmelCase          =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
   UpperCAmelCase          =	model(lowerCAmelCase__     )
   self.parent.assertEqual(result.prediction_logits.shape      ,    (self.batch_size, self.seq_length, self.vocab_size)     )
   self.parent.assertEqual(result.sop_logits.shape      ,    (self.batch_size, self.num_labels)     )


 def _UpperCamelCase						(    self		:			str      ,    lowerCAmelCase__		:			Optional[Any]      ,    lowerCAmelCase__		:			Optional[int]      ,    lowerCAmelCase__		:			Any      ,    lowerCAmelCase__		:			Union[str, Any]      ,    lowerCAmelCase__		:			List[str]      ,    lowerCAmelCase__		:			Tuple      ,    lowerCAmelCase__		:			Dict     )     ->   Any:
   UpperCAmelCase          =	TFAlbertForMaskedLM(config=lowerCAmelCase__     )
   UpperCAmelCase          =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
   UpperCAmelCase          =	model(lowerCAmelCase__     )
   self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.seq_length, self.vocab_size)     )


 def _UpperCamelCase						(    self		:			int      ,    lowerCAmelCase__		:			str      ,    lowerCAmelCase__		:			Dict      ,    lowerCAmelCase__		:			str      ,    lowerCAmelCase__		:			str      ,    lowerCAmelCase__		:			List[Any]      ,    lowerCAmelCase__		:			Dict      ,    lowerCAmelCase__		:			str     )     ->   Optional[Any]:
   UpperCAmelCase          =	self.num_labels
   UpperCAmelCase          =	TFAlbertForSequenceClassification(config=lowerCAmelCase__     )
   UpperCAmelCase          =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
   UpperCAmelCase          =	model(lowerCAmelCase__     )
   self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.num_labels)     )


 def _UpperCamelCase						(    self		:			List[Any]      ,    lowerCAmelCase__		:			Optional[Any]      ,    lowerCAmelCase__		:			Optional[int]      ,    lowerCAmelCase__		:			Optional[Any]      ,    lowerCAmelCase__		:			int      ,    lowerCAmelCase__		:			Dict      ,    lowerCAmelCase__		:			str      ,    lowerCAmelCase__		:			Optional[int]     )     ->   List[str]:
   UpperCAmelCase          =	TFAlbertForQuestionAnswering(config=lowerCAmelCase__     )
   UpperCAmelCase          =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
   UpperCAmelCase          =	model(lowerCAmelCase__     )
   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 _UpperCamelCase						(    self		:			List[Any]      ,    lowerCAmelCase__		:			int      ,    lowerCAmelCase__		:			Optional[int]      ,    lowerCAmelCase__		:			Union[str, Any]      ,    lowerCAmelCase__		:			Optional[Any]      ,    lowerCAmelCase__		:			Dict      ,    lowerCAmelCase__		:			Union[str, Any]      ,    lowerCAmelCase__		:			List[str]     )     ->   Optional[Any]:
   UpperCAmelCase          =	self.num_choices
   UpperCAmelCase          =	TFAlbertForMultipleChoice(config=lowerCAmelCase__     )
   UpperCAmelCase          =	tf.tile(tf.expand_dims(lowerCAmelCase__      ,    1     )      ,    (1, self.num_choices, 1)     )
   UpperCAmelCase          =	tf.tile(tf.expand_dims(lowerCAmelCase__      ,    1     )      ,    (1, self.num_choices, 1)     )
   UpperCAmelCase          =	tf.tile(tf.expand_dims(lowerCAmelCase__      ,    1     )      ,    (1, self.num_choices, 1)     )
   UpperCAmelCase          =	{
       "input_ids": multiple_choice_inputs_ids,
       "attention_mask": multiple_choice_input_mask,
       "token_type_ids": multiple_choice_token_type_ids,
   }
   UpperCAmelCase          =	model(lowerCAmelCase__     )
   self.parent.assertListEqual(list(result["logits"].shape     )      ,    [self.batch_size, self.num_choices]     )


 def _UpperCamelCase						(    self		:			Optional[int]      ,    lowerCAmelCase__		:			Union[str, Any]      ,    lowerCAmelCase__		:			Dict      ,    lowerCAmelCase__		:			str      ,    lowerCAmelCase__		:			Optional[Any]      ,    lowerCAmelCase__		:			str      ,    lowerCAmelCase__		:			int      ,    lowerCAmelCase__		:			Tuple     )     ->   List[str]:
   UpperCAmelCase          =	self.num_labels
   UpperCAmelCase          =	TFAlbertForTokenClassification(config=lowerCAmelCase__     )
   UpperCAmelCase          =	{
       "input_ids": input_ids,
       "attention_mask": input_mask,
       "token_type_ids": token_type_ids,
   }
   UpperCAmelCase          =	model(lowerCAmelCase__     )
   self.parent.assertListEqual(list(result["logits"].shape     )      ,    [self.batch_size, self.seq_length, self.num_labels]     )


 def _UpperCamelCase						(    self		:			int     )     ->   str:
   UpperCAmelCase          =	self.prepare_config_and_inputs()
   (
       (
       UpperCAmelCase
   )   ,						(
       UpperCAmelCase
   )   ,						(
       UpperCAmelCase
   )   ,						(
       UpperCAmelCase
   )   ,						(
       UpperCAmelCase
   )   ,						(
       UpperCAmelCase
   )   ,						(
       UpperCAmelCase
   )   ,						
   )          =	config_and_inputs
   UpperCAmelCase          =	{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
   return config, inputs_dict






@require_tf
class     __magic_name__     (							_snake_case						,    _snake_case						,    unittest.TestCase     ):
 UpperCAmelCase				     =    (
     (
         TFAlbertModel,
         TFAlbertForPreTraining,
         TFAlbertForMaskedLM,
         TFAlbertForSequenceClassification,
         TFAlbertForQuestionAnswering,
         TFAlbertForTokenClassification,
         TFAlbertForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 UpperCAmelCase				     =    (
     {
         """feature-extraction""": TFAlbertModel,
         """fill-mask""": TFAlbertForMaskedLM,
         """question-answering""": TFAlbertForQuestionAnswering,
         """text-classification""": TFAlbertForSequenceClassification,
         """token-classification""": TFAlbertForTokenClassification,
         """zero-shot""": TFAlbertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 UpperCAmelCase				     =    False
 UpperCAmelCase				     =    False


 def _UpperCamelCase						(    self		:			List[Any]      ,    lowerCAmelCase__		:			Optional[int]      ,    lowerCAmelCase__		:			Union[str, Any]      ,    lowerCAmelCase__		:			Union[str, Any]=False     )     ->   List[str]:
   UpperCAmelCase          =	super()._prepare_for_class(lowerCAmelCase__      ,    lowerCAmelCase__      ,    return_labels=lowerCAmelCase__     )

   if return_labels:
     if model_class in get_values(lowerCAmelCase__     ):
       UpperCAmelCase          =	tf.zeros(self.model_tester.batch_size      ,    dtype=tf.intaa     )

   return inputs_dict


 def _UpperCamelCase						(    self		:			Union[str, Any]     )     ->   Optional[int]:
   UpperCAmelCase          =	TFAlbertModelTester(self     )
   UpperCAmelCase          =	ConfigTester(self      ,    config_class=lowerCAmelCase__      ,    hidden_size=3_7     )


 def _UpperCamelCase						(    self		:			Any     )     ->   Optional[int]:
   self.config_tester.run_common_tests()


 def _UpperCamelCase						(    self		:			str     )     ->   str:
   UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_model(*lowerCAmelCase__     )


 def _UpperCamelCase						(    self		:			List[Any]     )     ->   Union[str, Any]:
   UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_for_pretraining(*lowerCAmelCase__     )


 def _UpperCamelCase						(    self		:			List[Any]     )     ->   Dict:
   UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_for_masked_lm(*lowerCAmelCase__     )


 def _UpperCamelCase						(    self		:			Union[str, Any]     )     ->   Union[str, Any]:
   UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_for_multiple_choice(*lowerCAmelCase__     )


 def _UpperCamelCase						(    self		:			Union[str, Any]     )     ->   Optional[Any]:
   UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_for_sequence_classification(*lowerCAmelCase__     )


 def _UpperCamelCase						(    self		:			Optional[Any]     )     ->   Dict:
   UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_for_question_answering(*lowerCAmelCase__     )


 @slow
 def _UpperCamelCase						(    self		:			List[str]     )     ->   Optional[Any]:
   for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     UpperCAmelCase          =	TFAlbertModel.from_pretrained(lowerCAmelCase__     )
     self.assertIsNotNone(lowerCAmelCase__     )






@require_tf
class     __magic_name__     (							unittest.TestCase     ):


 @slow
 def _UpperCamelCase						(    self		:			str     )     ->   List[Any]:
   UpperCAmelCase          =	TFAlbertForPreTraining.from_pretrained("albert-base-v2"     )
   UpperCAmelCase          =	tf.constant([[0, 1, 2, 3, 4, 5]]     )
   UpperCAmelCase          =	model(lowerCAmelCase__     )[0]

   UpperCAmelCase          =	[1, 6, 3_0_0_0_0]
   self.assertEqual(output.shape      ,    lowerCAmelCase__     )

   UpperCAmelCase          =	tf.constant(
       [
           [
               [4.595_668, 0.74_462_754, -1.818_147],
               [4.5_954_347, 0.7_454_184, -1.8_188_258],
               [4.5_954_905, 0.7_448_235, -1.8_182_316],
           ]
       ]     )
   tf.debugging.assert_near(output[:, :3, :3]      ,    lowerCAmelCase__      ,    atol=1e-4     )






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


lowerCAmelCase__							=			{
    "configuration_xlm_prophetnet": ["XLM_PROPHETNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMProphetNetConfig"],
}

try:
      if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowerCAmelCase__							=			["XLMProphetNetTokenizer"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowerCAmelCase__							=			[
          "XLM_PROPHETNET_PRETRAINED_MODEL_ARCHIVE_LIST",
          "XLMProphetNetDecoder",
          "XLMProphetNetEncoder",
          "XLMProphetNetForCausalLM",
          "XLMProphetNetForConditionalGeneration",
          "XLMProphetNetModel",
          "XLMProphetNetPreTrainedModel",
      ]


if TYPE_CHECKING:
      from .configuration_xlm_prophetnet import XLM_PROPHETNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMProphetNetConfig

      try:
            if not is_sentencepiece_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_xlm_prophetnet import XLMProphetNetTokenizer

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_xlm_prophetnet import (
                XLM_PROPHETNET_PRETRAINED_MODEL_ARCHIVE_LIST,
                XLMProphetNetDecoder,
                XLMProphetNetEncoder,
                XLMProphetNetForCausalLM,
                XLMProphetNetForConditionalGeneration,
                XLMProphetNetModel,
                XLMProphetNetPreTrainedModel,
            )

else:
      import sys

      lowerCAmelCase__							=			_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



