
'''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        UpperCAmelCase_       :

 '''simple docstring'''







 def __init__(				self		:     Tuple ,							snake_case__		:     Any ,							snake_case__		:     Tuple=13 ,							snake_case__		:     str=7 ,							snake_case__		:     List[Any]=True ,							snake_case__		:     Any=True ,							snake_case__		:     Union[str, Any]=True ,							snake_case__		:     Tuple=True ,							snake_case__		:     str=99 ,							snake_case__		:     Union[str, Any]=16 ,							snake_case__		:     Union[str, Any]=32 ,							snake_case__		:     List[str]=2 ,							snake_case__		:     Optional[Any]=4 ,							snake_case__		:     Any=37 ,							snake_case__		:     Dict="gelu" ,							snake_case__		:     Union[str, Any]=0.1 ,							snake_case__		:     Tuple=0.1 ,							snake_case__		:     List[Any]=5_12 ,							snake_case__		:     Optional[Any]=16 ,							snake_case__		:     str=2 ,							snake_case__		:     Optional[Any]=0.02 ,							snake_case__		:     int=3 ,							snake_case__		:     Any=4 ,							snake_case__		:     List[Any]=None ,							):



   '''simple docstring'''




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







 def        UpperCamelCase     (				self		:     Dict   ):



   '''simple docstring'''




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

   UpperCAmelCase__			:				Any												=       None
   if self.use_input_mask:
     UpperCAmelCase__			:				int												=       random_attention_mask([self.batch_size, self.seq_length]   )

   UpperCAmelCase__			:				Optional[int]												=       None
   if self.use_token_type_ids:
     UpperCAmelCase__			:				List[Any]												=       ids_tensor([self.batch_size, self.seq_length] ,							self.type_vocab_size   )

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

   UpperCAmelCase__			:				int												=       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] ,							snake_case__		:     Union[str, Any] ,							snake_case__		:     Dict ,							snake_case__		:     int ,							snake_case__		:     int ,							snake_case__		:     Dict ,							snake_case__		:     int ,							snake_case__		:     List[str]   ):



   '''simple docstring'''




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

   UpperCAmelCase__			:				Any												=       [input_ids, input_mask]
   UpperCAmelCase__			:				Optional[int]												=       model(snake_case__   )

   UpperCAmelCase__			:				Any												=       model(snake_case__   )

   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		:     List[str] ,							snake_case__		:     Union[str, Any] ,							snake_case__		:     str ,							snake_case__		:     Optional[int] ,							snake_case__		:     int ,							snake_case__		:     Optional[Any] ,							snake_case__		:     Union[str, Any] ,							snake_case__		:     int   ):



   '''simple docstring'''




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



   '''simple docstring'''




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







 def        UpperCamelCase     (				self		:     List[Any] ,							snake_case__		:     Optional[int] ,							snake_case__		:     int ,							snake_case__		:     List[Any] ,							snake_case__		:     int ,							snake_case__		:     Tuple ,							snake_case__		:     Tuple ,							snake_case__		:     Union[str, Any]   ):



   '''simple docstring'''




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







 def        UpperCamelCase     (				self		:     int ,							snake_case__		:     str ,							snake_case__		:     Tuple ,							snake_case__		:     Optional[Any] ,							snake_case__		:     Optional[Any] ,							snake_case__		:     Optional[Any] ,							snake_case__		:     int ,							snake_case__		:     Any   ):



   '''simple docstring'''




   UpperCAmelCase__			:				str												=       TFAlbertForQuestionAnswering(config=snake_case__   )
   UpperCAmelCase__			:				List[Any]												=       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
   UpperCAmelCase__			:				List[str]												=       model(snake_case__   )
   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		:     Tuple ,							snake_case__		:     Union[str, Any] ,							snake_case__		:     Optional[int] ,							snake_case__		:     Optional[int] ,							snake_case__		:     List[str] ,							snake_case__		:     Optional[Any] ,							snake_case__		:     Any ,							snake_case__		:     Dict   ):



   '''simple docstring'''




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







 def        UpperCamelCase     (				self		:     List[str] ,							snake_case__		:     Any ,							snake_case__		:     List[Any] ,							snake_case__		:     str ,							snake_case__		:     int ,							snake_case__		:     Dict ,							snake_case__		:     List[Any] ,							snake_case__		:     List[Any]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Tuple												=       self.num_labels
   UpperCAmelCase__			:				Optional[Any]												=       TFAlbertForTokenClassification(config=snake_case__   )
   UpperCAmelCase__			:				Optional[Any]												=       {
       "input_ids": input_ids,
       "attention_mask": input_mask,
       "token_type_ids": token_type_ids,
   }
   UpperCAmelCase__			:				List[Any]												=       model(snake_case__   )
   self.parent.assertListEqual(list(result["logits"].shape   ) ,							[self.batch_size, self.seq_length, self.num_labels]   )







 def        UpperCamelCase     (				self		:     Tuple   ):



   '''simple docstring'''




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






@require_tf
class        UpperCAmelCase_       (						lowerCamelCase_				,							lowerCamelCase_				,							unittest.TestCase							):

 '''simple docstring'''
 lowercase_				:						Optional[int]				=       (
     (
         TFAlbertModel,
         TFAlbertForPreTraining,
         TFAlbertForMaskedLM,
         TFAlbertForSequenceClassification,
         TFAlbertForQuestionAnswering,
         TFAlbertForTokenClassification,
         TFAlbertForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 lowercase_				:						Optional[int]				=       (
     {
         """feature-extraction""": TFAlbertModel,
         """fill-mask""": TFAlbertForMaskedLM,
         """question-answering""": TFAlbertForQuestionAnswering,
         """text-classification""": TFAlbertForSequenceClassification,
         """token-classification""": TFAlbertForTokenClassification,
         """zero-shot""": TFAlbertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 lowercase_				:						List[Any]				=       False
 lowercase_				:						int				=       False







 def        UpperCamelCase     (				self		:     Union[str, Any] ,							snake_case__		:     List[Any] ,							snake_case__		:     Union[str, Any] ,							snake_case__		:     Union[str, Any]=False   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Optional[int]												=       super()._prepare_for_class(snake_case__ ,							snake_case__ ,							return_labels=snake_case__   )

   if return_labels:
     if model_class in get_values(snake_case__   ):
       UpperCAmelCase__			:				Any												=       tf.zeros(self.model_tester.batch_size ,							dtype=tf.intaa   )

   return inputs_dict







 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Union[str, Any]												=       TFAlbertModelTester(self   )
   UpperCAmelCase__			:				List[str]												=       ConfigTester(self ,							config_class=snake_case__ ,							hidden_size=37   )







 def        UpperCamelCase     (				self		:     Optional[int]   ):



   '''simple docstring'''




   self.config_tester.run_common_tests()







 def        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Optional[int]												=       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_model(*snake_case__   )







 def        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				int												=       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_for_pretraining(*snake_case__   )







 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_for_masked_lm(*snake_case__   )







 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				int												=       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_for_multiple_choice(*snake_case__   )







 def        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				str												=       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_for_sequence_classification(*snake_case__   )







 def        UpperCamelCase     (				self		:     Optional[int]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Tuple												=       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_albert_for_question_answering(*snake_case__   )







 @slow
 def        UpperCamelCase     (				self		:     Tuple   ):



   '''simple docstring'''




   for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     UpperCAmelCase__			:				List[Any]												=       TFAlbertModel.from_pretrained(snake_case__   )
     self.assertIsNotNone(snake_case__   )






@require_tf
class        UpperCAmelCase_       (						unittest.TestCase							):

 '''simple docstring'''







 @slow
 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




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

   UpperCAmelCase__			:				Optional[Any]												=       [1, 6, 3_00_00]
   self.assertEqual(output.shape ,							snake_case__   )

   UpperCAmelCase__			:				Optional[Any]												=       tf.constant(
       [
           [
               [4.595668, 0.74462754, -1.818147],
               [4.5954347, 0.7454184, -1.8188258],
               [4.5954905, 0.7448235, -1.8182316],
           ]
       ]   )
   tf.debugging.assert_near(output[:, :3, :3] ,							snake_case__ ,							atol=1e-4   )








'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


SCREAMING_SNAKE_CASE    =					{"""configuration_visual_bert""": ["""VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """VisualBertConfig"""]}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE    =					[
          """VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """VisualBertForMultipleChoice""",
          """VisualBertForPreTraining""",
          """VisualBertForQuestionAnswering""",
          """VisualBertForRegionToPhraseAlignment""",
          """VisualBertForVisualReasoning""",
          """VisualBertLayer""",
          """VisualBertModel""",
          """VisualBertPreTrainedModel""",
      ]


if TYPE_CHECKING:
      from .configuration_visual_bert import VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, VisualBertConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_visual_bert import (
                VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                VisualBertForMultipleChoice,
                VisualBertForPreTraining,
                VisualBertForQuestionAnswering,
                VisualBertForRegionToPhraseAlignment,
                VisualBertForVisualReasoning,
                VisualBertLayer,
                VisualBertModel,
                VisualBertPreTrainedModel,
            )


else:
      import sys

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



