

"""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    _lowerCAmelCase    :







 """simple docstring"""







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


 def    _lowercase					(     self				:     int      ):
    __lowercase								=		ids_tensor([self.batch_size, self.seq_length],   self.vocab_size      )

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

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

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

    __lowercase								=		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				:     str,   UpperCAmelCase__				:     Tuple,   UpperCAmelCase__				:     List[Any],   UpperCAmelCase__				:     int,   UpperCAmelCase__				:     Any,   UpperCAmelCase__				:     Optional[int],   UpperCAmelCase__				:     Optional[int],   UpperCAmelCase__				:     List[Any]      ):
    __lowercase								=		TFAlbertModel(config=__SCREAMING_SNAKE_CASE      )
    # inputs = {'input_ids': input_ids,
    #           'attention_mask': input_mask,
    #           'token_type_ids': token_type_ids}
    # sequence_output, pooled_output = model(**inputs)
    __lowercase								=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
    __lowercase								=		model(__SCREAMING_SNAKE_CASE      )

    __lowercase								=		[input_ids, input_mask]
    __lowercase								=		model(__SCREAMING_SNAKE_CASE      )

    __lowercase								=		model(__SCREAMING_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    _lowercase					(     self				:     Optional[Any],   UpperCAmelCase__				:     int,   UpperCAmelCase__				:     int,   UpperCAmelCase__				:     str,   UpperCAmelCase__				:     int,   UpperCAmelCase__				:     Tuple,   UpperCAmelCase__				:     List[str],   UpperCAmelCase__				:     List[Any]      ):
    __lowercase								=		self.num_labels
    __lowercase								=		TFAlbertForPreTraining(config=__SCREAMING_SNAKE_CASE      )
    __lowercase								=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
    __lowercase								=		model(__SCREAMING_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    _lowercase					(     self				:     List[str],   UpperCAmelCase__				:     int,   UpperCAmelCase__				:     List[str],   UpperCAmelCase__				:     Union[str, Any],   UpperCAmelCase__				:     str,   UpperCAmelCase__				:     List[Any],   UpperCAmelCase__				:     Union[str, Any],   UpperCAmelCase__				:     List[Any]      ):
    __lowercase								=		TFAlbertForMaskedLM(config=__SCREAMING_SNAKE_CASE      )
    __lowercase								=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
    __lowercase								=		model(__SCREAMING_SNAKE_CASE      )
    self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.seq_length, self.vocab_size)      )


 def    _lowercase					(     self				:     Union[str, Any],   UpperCAmelCase__				:     Optional[Any],   UpperCAmelCase__				:     Optional[Any],   UpperCAmelCase__				:     str,   UpperCAmelCase__				:     List[str],   UpperCAmelCase__				:     int,   UpperCAmelCase__				:     Dict,   UpperCAmelCase__				:     Any      ):
    __lowercase								=		self.num_labels
    __lowercase								=		TFAlbertForSequenceClassification(config=__SCREAMING_SNAKE_CASE      )
    __lowercase								=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
    __lowercase								=		model(__SCREAMING_SNAKE_CASE      )
    self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.num_labels)      )


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


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



 def    _lowercase					(     self				:     Optional[int]      ):
    __lowercase								=		self.prepare_config_and_inputs()
    (
        __lowercase			
    )								=		config_and_inputs
    __lowercase								=		{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
    return config, inputs_dict







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







 """simple docstring"""







 __UpperCAmelCase      :   Tuple  						=      (
     (
         TFAlbertModel,
         TFAlbertForPreTraining,
         TFAlbertForMaskedLM,
         TFAlbertForSequenceClassification,
         TFAlbertForQuestionAnswering,
         TFAlbertForTokenClassification,
         TFAlbertForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 __UpperCAmelCase      :   Union[str, Any]  						=      (
     {
         "feature-extraction": TFAlbertModel,
         "fill-mask": TFAlbertForMaskedLM,
         "question-answering": TFAlbertForQuestionAnswering,
         "text-classification": TFAlbertForSequenceClassification,
         "token-classification": TFAlbertForTokenClassification,
         "zero-shot": TFAlbertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 __UpperCAmelCase      :   str  						=      False
 __UpperCAmelCase      :   List[Any]  						=      False


 def    _lowercase					(     self				:     Dict,   UpperCAmelCase__				:     str,   UpperCAmelCase__				:     Dict,   UpperCAmelCase__				:     Dict=False      ):
    __lowercase								=		super()._prepare_for_class(__SCREAMING_SNAKE_CASE,   __SCREAMING_SNAKE_CASE,   return_labels=__SCREAMING_SNAKE_CASE      )

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

    return inputs_dict


 def    _lowercase					(     self				:     Any      ):
    __lowercase								=		TFAlbertModelTester(self      )
    __lowercase								=		ConfigTester(self,   config_class=__SCREAMING_SNAKE_CASE,   hidden_size=3_7      )


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


 def    _lowercase					(     self				:     Optional[int]      ):
    __lowercase								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_albert_model(*__SCREAMING_SNAKE_CASE      )


 def    _lowercase					(     self				:     List[str]      ):
    __lowercase								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_albert_for_pretraining(*__SCREAMING_SNAKE_CASE      )


 def    _lowercase					(     self				:     Union[str, Any]      ):
    __lowercase								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_albert_for_masked_lm(*__SCREAMING_SNAKE_CASE      )


 def    _lowercase					(     self				:     List[Any]      ):
    __lowercase								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_albert_for_multiple_choice(*__SCREAMING_SNAKE_CASE      )


 def    _lowercase					(     self				:     int      ):
    __lowercase								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_albert_for_sequence_classification(*__SCREAMING_SNAKE_CASE      )


 def    _lowercase					(     self				:     Any      ):
    __lowercase								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_albert_for_question_answering(*__SCREAMING_SNAKE_CASE      )



 @slow
 def    _lowercase					(     self				:     int      ):




    for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowercase								=		TFAlbertModel.from_pretrained(__SCREAMING_SNAKE_CASE      )
       self.assertIsNotNone(__SCREAMING_SNAKE_CASE      )







@require_tf
class    _lowerCAmelCase    (      unittest.TestCase  ):







 """simple docstring"""







 @slow
 def    _lowercase					(     self				:     Optional[int]      ):
    __lowercase								=		TFAlbertForPreTraining.from_pretrained("albert-base-v2"      )
    __lowercase								=		tf.constant([[0, 1, 2, 3, 4, 5]]      )
    __lowercase								=		model(__SCREAMING_SNAKE_CASE      )[0]

    __lowercase								=		[1, 6, 3_0_0_0_0]
    self.assertEqual(output.shape,   __SCREAMING_SNAKE_CASE      )

    __lowercase								=		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],   __SCREAMING_SNAKE_CASE,   atol=1E-4      )








from typing import TYPE_CHECKING

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


__UpperCAmelCase			:			int			      =					{"configuration_vilt": ["VILT_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViltConfig"]}

try:
  if not is_vision_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCAmelCase			:			Optional[Any]			      =					["ViltFeatureExtractor"]
  __UpperCAmelCase			:			Union[str, Any]			      =					["ViltImageProcessor"]
  __UpperCAmelCase			:			Dict			      =					["ViltProcessor"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCAmelCase			:			List[Any]			      =					[
      "VILT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "ViltForImageAndTextRetrieval",
      "ViltForImagesAndTextClassification",
      "ViltForTokenClassification",
      "ViltForMaskedLM",
      "ViltForQuestionAnswering",
      "ViltLayer",
      "ViltModel",
      "ViltPreTrainedModel",
  ]


if TYPE_CHECKING:
  from .configuration_vilt import VILT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViltConfig

  try:
    if not is_vision_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .feature_extraction_vilt import ViltFeatureExtractor
    from .image_processing_vilt import ViltImageProcessor
    from .processing_vilt import ViltProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_vilt import (
        VILT_PRETRAINED_MODEL_ARCHIVE_LIST,
        ViltForImageAndTextRetrieval,
        ViltForImagesAndTextClassification,
        ViltForMaskedLM,
        ViltForQuestionAnswering,
        ViltForTokenClassification,
        ViltLayer,
        ViltModel,
        ViltPreTrainedModel,
    )


else:
  import sys

  __UpperCAmelCase			:			Any			      =					_LazyModule(__name__, globals()["__file__"], _import_structure)

