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





      '''simple docstring'''






      def __init__(       self  :					Tuple     ,	_UpperCamelCase  :					Tuple     ,	_UpperCamelCase  :					Union[str, Any]=13     ,	_UpperCamelCase  :					List[str]=7     ,	_UpperCamelCase  :					Union[str, Any]=True     ,	_UpperCamelCase  :					Union[str, Any]=True     ,	_UpperCamelCase  :					Union[str, Any]=True     ,	_UpperCamelCase  :					Dict=True     ,	_UpperCamelCase  :					int=99     ,	_UpperCamelCase  :					Tuple=16     ,	_UpperCamelCase  :					Optional[Any]=32     ,	_UpperCamelCase  :					Tuple=2     ,	_UpperCamelCase  :					Dict=4     ,	_UpperCamelCase  :					Any=37     ,	_UpperCamelCase  :					Union[str, Any]="gelu"     ,	_UpperCamelCase  :					Dict=0.1     ,	_UpperCamelCase  :					List[Any]=0.1     ,	_UpperCamelCase  :					Dict=512     ,	_UpperCamelCase  :					Dict=16     ,	_UpperCamelCase  :					List[str]=2     ,	_UpperCamelCase  :					Union[str, Any]=0.0_2     ,	_UpperCamelCase  :					Optional[int]=3     ,	_UpperCamelCase  :					Optional[Any]=4     ,	_UpperCamelCase  :					Optional[int]=None     ,	)					->Any:


          """simple docstring"""


          _lowerCamelCase  :		List[Any]			    =    parent
          _lowerCamelCase  :		Optional[int]			    =    13
          _lowerCamelCase  :		Dict			    =    7
          _lowerCamelCase  :		Union[str, Any]			    =    True
          _lowerCamelCase  :		Optional[int]			    =    True
          _lowerCamelCase  :		Any			    =    True
          _lowerCamelCase  :		Union[str, Any]			    =    True
          _lowerCamelCase  :		Union[str, Any]			    =    99
          _lowerCamelCase  :		Optional[Any]			    =    16
          _lowerCamelCase  :		int			    =    32
          _lowerCamelCase  :		Optional[int]			    =    2
          _lowerCamelCase  :		Dict			    =    4
          _lowerCamelCase  :		Optional[Any]			    =    37
          _lowerCamelCase  :		int			    =    """gelu"""
          _lowerCamelCase  :		List[str]			    =    0.1
          _lowerCamelCase  :		List[Any]			    =    0.1
          _lowerCamelCase  :		List[str]			    =    512
          _lowerCamelCase  :		str			    =    16
          _lowerCamelCase  :		Optional[Any]			    =    2
          _lowerCamelCase  :		List[Any]			    =    0.0_2
          _lowerCamelCase  :		Dict			    =    3
          _lowerCamelCase  :		Union[str, Any]			    =    4
          _lowerCamelCase  :		Union[str, Any]			    =    None



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Union[str, Any])					->Dict:


          """simple docstring"""


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

          _lowerCamelCase  :		Optional[int]			    =    None
          if self.use_input_mask:
              _lowerCamelCase  :		Union[str, Any]			    =    random_attention_mask([self.batch_size, self.seq_length])

          _lowerCamelCase  :		Optional[int]			    =    None
          if self.use_token_type_ids:
              _lowerCamelCase  :		Tuple			    =    ids_tensor([self.batch_size, self.seq_length]     ,	self.type_vocab_size)

          _lowerCamelCase  :		Optional[Any]			    =    None
          _lowerCamelCase  :		int			    =    None
          _lowerCamelCase  :		List[Any]			    =    None
          if self.use_labels:
              _lowerCamelCase  :		Union[str, Any]			    =    ids_tensor([self.batch_size]     ,	self.type_sequence_label_size)
              _lowerCamelCase  :		Dict			    =    ids_tensor([self.batch_size, self.seq_length]     ,	self.num_labels)
              _lowerCamelCase  :		Optional[Any]			    =    ids_tensor([self.batch_size]     ,	self.num_choices)

          _lowerCamelCase  :		List[Any]			    =    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 						_SCREAMING_SNAKE_CASE     (       self  :					Any     ,	_UpperCamelCase  :					Dict     ,	_UpperCamelCase  :					Union[str, Any]     ,	_UpperCamelCase  :					List[str]     ,	_UpperCamelCase  :					List[Any]     ,	_UpperCamelCase  :					Union[str, Any]     ,	_UpperCamelCase  :					List[str]     ,	_UpperCamelCase  :					Optional[int])					->List[Any]:


          """simple docstring"""


          _lowerCamelCase  :		List[Any]			    =    TFAlbertModel(config=_UpperCamelCase)
          # inputs = {'input_ids': input_ids,
          #           'attention_mask': input_mask,
          #           'token_type_ids': token_type_ids}
          # sequence_output, pooled_output = model(**inputs)
          _lowerCamelCase  :		List[str]			    =    {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          _lowerCamelCase  :		Dict			    =    model(_UpperCamelCase)

          _lowerCamelCase  :		List[str]			    =    [input_ids, input_mask]
          _lowerCamelCase  :		Optional[Any]			    =    model(_UpperCamelCase)

          _lowerCamelCase  :		List[str]			    =    model(_UpperCamelCase)

          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 						_SCREAMING_SNAKE_CASE     (       self  :					List[Any]     ,	_UpperCamelCase  :					Optional[int]     ,	_UpperCamelCase  :					str     ,	_UpperCamelCase  :					Union[str, Any]     ,	_UpperCamelCase  :					List[str]     ,	_UpperCamelCase  :					int     ,	_UpperCamelCase  :					str     ,	_UpperCamelCase  :					List[str])					->List[str]:


          """simple docstring"""


          _lowerCamelCase  :		Optional[Any]			    =    self.num_labels
          _lowerCamelCase  :		List[Any]			    =    TFAlbertForPreTraining(config=_UpperCamelCase)
          _lowerCamelCase  :		Optional[int]			    =    {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          _lowerCamelCase  :		List[Any]			    =    model(_UpperCamelCase)
          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 						_SCREAMING_SNAKE_CASE     (       self  :					List[str]     ,	_UpperCamelCase  :					int     ,	_UpperCamelCase  :					str     ,	_UpperCamelCase  :					Any     ,	_UpperCamelCase  :					Optional[int]     ,	_UpperCamelCase  :					Union[str, Any]     ,	_UpperCamelCase  :					Dict     ,	_UpperCamelCase  :					str)					->List[str]:


          """simple docstring"""


          _lowerCamelCase  :		Dict			    =    TFAlbertForMaskedLM(config=_UpperCamelCase)
          _lowerCamelCase  :		List[Any]			    =    {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          _lowerCamelCase  :		Dict			    =    model(_UpperCamelCase)
          self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.seq_length, self.vocab_size))



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Union[str, Any]     ,	_UpperCamelCase  :					Tuple     ,	_UpperCamelCase  :					Union[str, Any]     ,	_UpperCamelCase  :					Optional[Any]     ,	_UpperCamelCase  :					Any     ,	_UpperCamelCase  :					List[Any]     ,	_UpperCamelCase  :					List[str]     ,	_UpperCamelCase  :					Union[str, Any])					->Tuple:


          """simple docstring"""


          _lowerCamelCase  :		int			    =    self.num_labels
          _lowerCamelCase  :		Dict			    =    TFAlbertForSequenceClassification(config=_UpperCamelCase)
          _lowerCamelCase  :		str			    =    {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          _lowerCamelCase  :		str			    =    model(_UpperCamelCase)
          self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.num_labels))



      def 						_SCREAMING_SNAKE_CASE     (       self  :					int     ,	_UpperCamelCase  :					List[Any]     ,	_UpperCamelCase  :					Any     ,	_UpperCamelCase  :					Optional[Any]     ,	_UpperCamelCase  :					Union[str, Any]     ,	_UpperCamelCase  :					Optional[int]     ,	_UpperCamelCase  :					str     ,	_UpperCamelCase  :					Optional[Any])					->Tuple:


          """simple docstring"""


          _lowerCamelCase  :		Any			    =    TFAlbertForQuestionAnswering(config=_UpperCamelCase)
          _lowerCamelCase  :		Optional[Any]			    =    {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          _lowerCamelCase  :		Optional[Any]			    =    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 						_SCREAMING_SNAKE_CASE     (       self  :					str     ,	_UpperCamelCase  :					List[str]     ,	_UpperCamelCase  :					str     ,	_UpperCamelCase  :					List[Any]     ,	_UpperCamelCase  :					int     ,	_UpperCamelCase  :					Dict     ,	_UpperCamelCase  :					str     ,	_UpperCamelCase  :					Optional[int])					->List[Any]:


          """simple docstring"""


          _lowerCamelCase  :		int			    =    self.num_choices
          _lowerCamelCase  :		List[str]			    =    TFAlbertForMultipleChoice(config=_UpperCamelCase)
          _lowerCamelCase  :		List[Any]			    =    tf.tile(tf.expand_dims(_UpperCamelCase     ,	1)     ,	(1, self.num_choices, 1))
          _lowerCamelCase  :		Any			    =    tf.tile(tf.expand_dims(_UpperCamelCase     ,	1)     ,	(1, self.num_choices, 1))
          _lowerCamelCase  :		Any			    =    tf.tile(tf.expand_dims(_UpperCamelCase     ,	1)     ,	(1, self.num_choices, 1))
          _lowerCamelCase  :		int			    =    {
              """input_ids""": multiple_choice_inputs_ids,
              """attention_mask""": multiple_choice_input_mask,
              """token_type_ids""": multiple_choice_token_type_ids,
          }
          _lowerCamelCase  :		Optional[int]			    =    model(_UpperCamelCase)
          self.parent.assertListEqual(list(result["""logits"""].shape)     ,	[self.batch_size, self.num_choices])



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Optional[int]     ,	_UpperCamelCase  :					List[str]     ,	_UpperCamelCase  :					Union[str, Any]     ,	_UpperCamelCase  :					str     ,	_UpperCamelCase  :					Optional[int]     ,	_UpperCamelCase  :					int     ,	_UpperCamelCase  :					List[Any]     ,	_UpperCamelCase  :					List[str])					->Any:


          """simple docstring"""


          _lowerCamelCase  :		List[str]			    =    self.num_labels
          _lowerCamelCase  :		int			    =    TFAlbertForTokenClassification(config=_UpperCamelCase)
          _lowerCamelCase  :		str			    =    {
              """input_ids""": input_ids,
              """attention_mask""": input_mask,
              """token_type_ids""": token_type_ids,
          }
          _lowerCamelCase  :		Dict			    =    model(_UpperCamelCase)
          self.parent.assertListEqual(list(result["""logits"""].shape)     ,	[self.batch_size, self.seq_length, self.num_labels])




      def 						_SCREAMING_SNAKE_CASE     (       self  :					Optional[int])					->Any:


          """simple docstring"""


          _lowerCamelCase  :		str			    =    self.prepare_config_and_inputs()
          (
              (
              _lowerCamelCase  
          )    ,				(
              _lowerCamelCase  
          )    ,				(
              _lowerCamelCase  
          )    ,				(
              _lowerCamelCase  
          )    ,				(
              _lowerCamelCase  
          )    ,				(
              _lowerCamelCase  
          )    ,				(
              _lowerCamelCase  
          )    ,				  
          )  :		List[Any]			    =    config_and_inputs
          _lowerCamelCase  :		Tuple			    =    {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
          return config, inputs_dict





@require_tf
class 						__snake_case						(			SCREAMING_SNAKE_CASE__			,	SCREAMING_SNAKE_CASE__			,	unittest.TestCase		):





      '''simple docstring'''






      _snake_case     		=							(
          (
              TFAlbertModel,
              TFAlbertForPreTraining,
              TFAlbertForMaskedLM,
              TFAlbertForSequenceClassification,
              TFAlbertForQuestionAnswering,
              TFAlbertForTokenClassification,
              TFAlbertForMultipleChoice,
          )
          if is_tf_available()
          else ()
      )
      _snake_case     		=							(
          {
              'feature-extraction': TFAlbertModel,
              'fill-mask': TFAlbertForMaskedLM,
              'question-answering': TFAlbertForQuestionAnswering,
              'text-classification': TFAlbertForSequenceClassification,
              'token-classification': TFAlbertForTokenClassification,
              'zero-shot': TFAlbertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      _snake_case     		=							False
      _snake_case     		=							False



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Any     ,	_UpperCamelCase  :					Dict     ,	_UpperCamelCase  :					str     ,	_UpperCamelCase  :					Union[str, Any]=False)					->Optional[int]:


          """simple docstring"""


          _lowerCamelCase  :		List[str]			    =    super()._prepare_for_class(_UpperCamelCase     ,	_UpperCamelCase     ,	return_labels=_UpperCamelCase)

          if return_labels:
              if model_class in get_values(_UpperCamelCase):
                  _lowerCamelCase  :		Union[str, Any]			    =    tf.zeros(self.model_tester.batch_size     ,	dtype=tf.intaa)

          return inputs_dict



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Tuple)					->Optional[Any]:


          """simple docstring"""


          _lowerCamelCase  :		Optional[int]			    =    TFAlbertModelTester(self)
          _lowerCamelCase  :		int			    =    ConfigTester(self     ,	config_class=_UpperCamelCase     ,	hidden_size=37)



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Any)					->Tuple:


          """simple docstring"""


          self.config_tester.run_common_tests()



      def 						_SCREAMING_SNAKE_CASE     (       self  :					int)					->Union[str, Any]:


          """simple docstring"""


          _lowerCamelCase  :		Optional[int]			    =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_model(*_UpperCamelCase)



      def 						_SCREAMING_SNAKE_CASE     (       self  :					List[Any])					->Optional[int]:


          """simple docstring"""


          _lowerCamelCase  :		Tuple			    =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_for_pretraining(*_UpperCamelCase)



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Optional[Any])					->List[Any]:


          """simple docstring"""


          _lowerCamelCase  :		Optional[Any]			    =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_for_masked_lm(*_UpperCamelCase)



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Any)					->List[Any]:


          """simple docstring"""


          _lowerCamelCase  :		str			    =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_for_multiple_choice(*_UpperCamelCase)



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Union[str, Any])					->Optional[Any]:


          """simple docstring"""


          _lowerCamelCase  :		Optional[Any]			    =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_for_sequence_classification(*_UpperCamelCase)



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Union[str, Any])					->Tuple:


          """simple docstring"""


          _lowerCamelCase  :		Optional[int]			    =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_for_question_answering(*_UpperCamelCase)




      @slow
      def 						_SCREAMING_SNAKE_CASE     (       self  :					List[str])					->List[Any]:


          """simple docstring"""


          for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _lowerCamelCase  :		Optional[int]			    =    TFAlbertModel.from_pretrained(_UpperCamelCase)
              self.assertIsNotNone(_UpperCamelCase)





@require_tf
class 						__snake_case						(			unittest.TestCase		):





      '''simple docstring'''






      @slow
      def 						_SCREAMING_SNAKE_CASE     (       self  :					Optional[Any])					->Any:


          """simple docstring"""


          _lowerCamelCase  :		Optional[Any]			    =    TFAlbertForPreTraining.from_pretrained("""albert-base-v2""")
          _lowerCamelCase  :		Tuple			    =    tf.constant([[0, 1, 2, 3, 4, 5]])
          _lowerCamelCase  :		int			    =    model(_UpperCamelCase)[0]

          _lowerCamelCase  :		Dict			    =    [1, 6, 3_0000]
          self.assertEqual(output.shape     ,	_UpperCamelCase)

          _lowerCamelCase  :		Optional[int]			    =    tf.constant(
              [
                  [
                      [4.5_9_5_6_6_8, 0.7_4_4_6_2_7_5_4, -1.8_1_8_1_4_7],
                      [4.5_9_5_4_3_4_7, 0.7_4_5_4_1_8_4, -1.8_1_8_8_2_5_8],
                      [4.5_9_5_4_9_0_5, 0.7_4_4_8_2_3_5, -1.8_1_8_2_3_1_6],
                  ]
              ])
          tf.debugging.assert_near(output[:, :3, :3]     ,	_UpperCamelCase     ,	atol=1E-4)











from math import pi




def   _SCREAMING_SNAKE_CASE      (	snake_case_		:						float						):
     return degree / (180 / pi)


if __name__ == "__main__":
    from doctest import testmod

    testmod()