






"""simple docstring"""





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





   '''simple docstring'''






   def __init__(   self			,				UpperCAmelCase_			,				UpperCAmelCase_=13			,				UpperCAmelCase_=7			,				UpperCAmelCase_=True			,				UpperCAmelCase_=True			,				UpperCAmelCase_=True			,				UpperCAmelCase_=True			,				UpperCAmelCase_=99			,				UpperCAmelCase_=16			,				UpperCAmelCase_=32			,				UpperCAmelCase_=2			,				UpperCAmelCase_=4			,				UpperCAmelCase_=37			,				UpperCAmelCase_="gelu"			,				UpperCAmelCase_=0.1			,				UpperCAmelCase_=0.1			,				UpperCAmelCase_=512			,				UpperCAmelCase_=16			,				UpperCAmelCase_=2			,				UpperCAmelCase_=0.02			,				UpperCAmelCase_=3			,				UpperCAmelCase_=4			,				UpperCAmelCase_=None			,				)	-> Optional[int]:




          '''simple docstring'''




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





   def __lowercase    (   self)	-> List[str]:




          '''simple docstring'''




          lowercase__:    str        =      ids_tensor([self.batch_size, self.seq_length]			,				self.vocab_size)

          lowercase__:    Dict        =      None
          if self.use_input_mask:
                 lowercase__:    Tuple        =      random_attention_mask([self.batch_size, self.seq_length])

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

          lowercase__:    Dict        =      None
          lowercase__:    int        =      None
          lowercase__:    Optional[int]        =      None
          if self.use_labels:
                 lowercase__:    Optional[int]        =      ids_tensor([self.batch_size]			,				self.type_sequence_label_size)
                 lowercase__:    Optional[int]        =      ids_tensor([self.batch_size, self.seq_length]			,				self.num_labels)
                 lowercase__:    Dict        =      ids_tensor([self.batch_size]			,				self.num_choices)

          lowercase__:    str        =      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 __lowercase    (   self			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_)	-> int:




          '''simple docstring'''




          lowercase__:    Tuple        =      TFAlbertModel(config=UpperCAmelCase_)
          # inputs = {'input_ids': input_ids,
          #           'attention_mask': input_mask,
          #           'token_type_ids': token_type_ids}
          # sequence_output, pooled_output = model(**inputs)
          lowercase__:    Optional[int]        =      {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
          lowercase__:    List[str]        =      model(UpperCAmelCase_)

          lowercase__:    int        =      [input_ids, input_mask]
          lowercase__:    Tuple        =      model(UpperCAmelCase_)

          lowercase__:    Optional[int]        =      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 __lowercase    (   self			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_)	-> List[str]:




          '''simple docstring'''




          lowercase__:    Tuple        =      self.num_labels
          lowercase__:    str        =      TFAlbertForPreTraining(config=UpperCAmelCase_)
          lowercase__:    Tuple        =      {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
          lowercase__:    str        =      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 __lowercase    (   self			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_)	-> Union[str, Any]:




          '''simple docstring'''




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





   def __lowercase    (   self			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_)	-> Dict:




          '''simple docstring'''




          lowercase__:    Dict        =      self.num_labels
          lowercase__:    str        =      TFAlbertForSequenceClassification(config=UpperCAmelCase_)
          lowercase__:    Union[str, Any]        =      {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
          lowercase__:    Optional[Any]        =      model(UpperCAmelCase_)
          self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.num_labels))





   def __lowercase    (   self			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_)	-> Union[str, Any]:




          '''simple docstring'''




          lowercase__:    Dict        =      TFAlbertForQuestionAnswering(config=UpperCAmelCase_)
          lowercase__:    List[Any]        =      {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
          lowercase__:    List[str]        =      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 __lowercase    (   self			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_)	-> Tuple:




          '''simple docstring'''




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





   def __lowercase    (   self			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_)	-> str:




          '''simple docstring'''




          lowercase__:    Tuple        =      self.num_labels
          lowercase__:    int        =      TFAlbertForTokenClassification(config=UpperCAmelCase_)
          lowercase__:    Union[str, Any]        =      {
              "input_ids": input_ids,
              "attention_mask": input_mask,
              "token_type_ids": token_type_ids,
          }
          lowercase__:    List[str]        =      model(UpperCAmelCase_)
          self.parent.assertListEqual(list(result["logits"].shape)			,				[self.batch_size, self.seq_length, self.num_labels])





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




          '''simple docstring'''




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







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





   '''simple docstring'''






   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 __lowercase    (   self			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_=False)	-> Optional[int]:




          '''simple docstring'''




          lowercase__:    List[Any]        =      super()._prepare_for_class(UpperCAmelCase_			,				UpperCAmelCase_			,				return_labels=UpperCAmelCase_)

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

          return inputs_dict





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




          '''simple docstring'''




          lowercase__:    Union[str, Any]        =      TFAlbertModelTester(self)
          lowercase__:    str        =      ConfigTester(self			,				config_class=UpperCAmelCase_			,				hidden_size=37)





   def __lowercase    (   self)	-> int:




          '''simple docstring'''




          self.config_tester.run_common_tests()





   def __lowercase    (   self)	-> Tuple:




          '''simple docstring'''




          lowercase__:    Dict        =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_model(*UpperCAmelCase_)





   def __lowercase    (   self)	-> Dict:




          '''simple docstring'''




          lowercase__:    int        =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_for_pretraining(*UpperCAmelCase_)





   def __lowercase    (   self)	-> Optional[Any]:




          '''simple docstring'''




          lowercase__:    Union[str, Any]        =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_for_masked_lm(*UpperCAmelCase_)





   def __lowercase    (   self)	-> List[str]:




          '''simple docstring'''




          lowercase__:    Optional[int]        =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_for_multiple_choice(*UpperCAmelCase_)





   def __lowercase    (   self)	-> int:




          '''simple docstring'''




          lowercase__:    Dict        =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_for_sequence_classification(*UpperCAmelCase_)





   def __lowercase    (   self)	-> List[str]:




          '''simple docstring'''




          lowercase__:    List[Any]        =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_albert_for_question_answering(*UpperCAmelCase_)





   @slow
   def __lowercase    (   self)	-> Dict:




          '''simple docstring'''




          for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowercase__:    str        =      TFAlbertModel.from_pretrained(UpperCAmelCase_)
                 self.assertIsNotNone(UpperCAmelCase_)







@require_tf
class 		_a       (		unittest.TestCase   ):





   '''simple docstring'''






   @slow
   def __lowercase    (   self)	-> Dict:




          '''simple docstring'''




          lowercase__:    Optional[Any]        =      TFAlbertForPreTraining.from_pretrained("albert-base-v2")
          lowercase__:    Any        =      tf.constant([[0, 1, 2, 3, 4, 5]])
          lowercase__:    Union[str, Any]        =      model(UpperCAmelCase_)[0]

          lowercase__:    Optional[Any]        =      [1, 6, 30_000]
          self.assertEqual(output.shape			,				UpperCAmelCase_)

          lowercase__:    Union[str, Any]        =      tf.constant(
              [
                  [
                      [4.59_56_68, 0.74_46_27_54, -1.81_81_47],
                      [4.5_95_43_47, 0.7_45_41_84, -1.8_18_82_58],
                      [4.5_95_49_05, 0.7_44_82_35, -1.8_18_23_16],
                  ]
              ])
          tf.debugging.assert_near(output[:, :3, :3]			,				UpperCAmelCase_			,				atol=1E-4)














"""simple docstring"""





def     A(  snake_case_		):
       """simple docstring"""





       if not isinstance(snake_case_						,       snake_case_		):
              raise ValueError("Input series is not valid, valid series - [1, 2/3, 2]"		)
       if len(snake_case_		) == 0:
              raise ValueError("Input list must be a non empty list"		)
       if len(snake_case_		) == 1 and series[0] != 0:
              return True
       lowercase__:    str        =      []
       lowercase__:    Optional[int]        =      len(snake_case_		)
       for i in range(0						,       snake_case_		):
              if series[i] == 0:
                     raise ValueError("Input series cannot have 0 as an element"		)
              rec_series.append(1 / series[i]		)
       lowercase__:    Optional[int]        =      rec_series[1] - rec_series[0]
       for index in range(2						,       snake_case_		):
              if rec_series[index] - rec_series[index - 1] != common_diff:
                     return False
       return True






def     A(  snake_case_		):
       """simple docstring"""





       if not isinstance(snake_case_						,       snake_case_		):
              raise ValueError("Input series is not valid, valid series - [2, 4, 6]"		)
       if len(snake_case_		) == 0:
              raise ValueError("Input list must be a non empty list"		)
       lowercase__:    Optional[int]        =      0
       for val in series:
              answer += 1 / val
       return len(snake_case_		) / answer


if __name__ == "__main__":
  import doctest

  doctest.testmod()






