

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 , lowercase , lowercase=13 , lowercase=7 , lowercase=True , lowercase=True , lowercase=True , lowercase=True , lowercase=99 , lowercase=16 , lowercase=32 , lowercase=2 , lowercase=4 , lowercase=37 , lowercase="gelu" , lowercase=0.1 , lowercase=0.1 , lowercase=512 , lowercase=16 , lowercase=2 , lowercase=0.02 , lowercase=3 , lowercase=4 , lowercase=None , )					->  int:
							'''simple docstring'''






							A__            =     parent
							A__            =     13
							A__            =     7
							A__            =     True
							A__            =     True
							A__            =     True
							A__            =     True
							A__            =     99
							A__            =     16
							A__            =     32
							A__            =     2
							A__            =     4
							A__            =     37
							A__            =     "gelu"
							A__            =     0.1
							A__            =     0.1
							A__            =     512
							A__            =     16
							A__            =     2
							A__            =     0.02
							A__            =     3
							A__            =     4
							A__            =     None


			def        UpperCamelCase						(      self    )					->  List[Any]:
							'''simple docstring'''






							A__            =     ids_tensor([self.batch_size, self.seq_length] , self.vocab_size    )

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

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

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

							A__            =     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 , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase    )					->  Any:
							'''simple docstring'''






							A__            =     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)
							A__            =     {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							A__            =     model(SCREAMING_SNAKE_CASE_    )

							A__            =     [input_ids, input_mask]
							A__            =     model(SCREAMING_SNAKE_CASE_    )

							A__            =     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        UpperCamelCase						(      self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase    )					->  str:
							'''simple docstring'''






							A__            =     self.num_labels
							A__            =     TFAlbertForPreTraining(config=SCREAMING_SNAKE_CASE_    )
							A__            =     {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							A__            =     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        UpperCamelCase						(      self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase    )					->  Union[str, Any]:
							'''simple docstring'''






							A__            =     TFAlbertForMaskedLM(config=SCREAMING_SNAKE_CASE_    )
							A__            =     {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							A__            =     model(SCREAMING_SNAKE_CASE_    )
							self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)    )


			def        UpperCamelCase						(      self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase    )					->  Optional[Any]:
							'''simple docstring'''






							A__            =     self.num_labels
							A__            =     TFAlbertForSequenceClassification(config=SCREAMING_SNAKE_CASE_    )
							A__            =     {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							A__            =     model(SCREAMING_SNAKE_CASE_    )
							self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)    )


			def        UpperCamelCase						(      self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase    )					->  int:
							'''simple docstring'''






							A__            =     TFAlbertForQuestionAnswering(config=SCREAMING_SNAKE_CASE_    )
							A__            =     {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							A__            =     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        UpperCamelCase						(      self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase    )					->  int:
							'''simple docstring'''






							A__            =     self.num_choices
							A__            =     TFAlbertForMultipleChoice(config=SCREAMING_SNAKE_CASE_    )
							A__            =     tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_ , 1    ) , (1, self.num_choices, 1)    )
							A__            =     tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_ , 1    ) , (1, self.num_choices, 1)    )
							A__            =     tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_ , 1    ) , (1, self.num_choices, 1)    )
							A__            =     {
							    "input_ids": multiple_choice_inputs_ids,
							    "attention_mask": multiple_choice_input_mask,
							    "token_type_ids": multiple_choice_token_type_ids,
							}
							A__            =     model(SCREAMING_SNAKE_CASE_    )
							self.parent.assertListEqual(list(result["logits"].shape    ) , [self.batch_size, self.num_choices]    )


			def        UpperCamelCase						(      self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase    )					->  Optional[int]:
							'''simple docstring'''






							A__            =     self.num_labels
							A__            =     TFAlbertForTokenClassification(config=SCREAMING_SNAKE_CASE_    )
							A__            =     {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}
							A__            =     model(SCREAMING_SNAKE_CASE_    )
							self.parent.assertListEqual(list(result["logits"].shape    ) , [self.batch_size, self.seq_length, self.num_labels]    )


			def        UpperCamelCase						(      self    )					->  List[Any]:
							'''simple docstring'''






							A__            =     self.prepare_config_and_inputs()
							(
							    (
							    A__							
							)      ,			(
							    A__							
							)      ,			(
							    A__							
							)      ,			(
							    A__							
							)      ,			(
							    A__							
							)      ,			(
							    A__							
							)      ,			(
							    A__							
							)      ,										
							)            =     config_and_inputs
							A__            =     {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
							return config, inputs_dict

@require_tf
class    a__  (   SCREAMING_SNAKE_CASE_							,		SCREAMING_SNAKE_CASE_							,		unittest.TestCase     ):
			"""simple docstring"""







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


			def        UpperCamelCase						(      self , lowercase , lowercase , lowercase=False    )					->  Optional[int]:
							'''simple docstring'''






							A__            =     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_    ):
															A__            =     tf.zeros(self.model_tester.batch_size , dtype=tf.intaa    )

							return inputs_dict


			def        UpperCamelCase						(      self    )					->  List[str]:
							'''simple docstring'''






							A__            =     TFAlbertModelTester(self    )
							A__            =     ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , hidden_size=37    )


			def        UpperCamelCase						(      self    )					->  Dict:
							'''simple docstring'''






							self.config_tester.run_common_tests()


			def        UpperCamelCase						(      self    )					->  Optional[int]:
							'''simple docstring'''






							A__            =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_model(*SCREAMING_SNAKE_CASE_    )


			def        UpperCamelCase						(      self    )					->  List[Any]:
							'''simple docstring'''






							A__            =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_pretraining(*SCREAMING_SNAKE_CASE_    )


			def        UpperCamelCase						(      self    )					->  Dict:
							'''simple docstring'''






							A__            =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_masked_lm(*SCREAMING_SNAKE_CASE_    )


			def        UpperCamelCase						(      self    )					->  Dict:
							'''simple docstring'''






							A__            =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_multiple_choice(*SCREAMING_SNAKE_CASE_    )


			def        UpperCamelCase						(      self    )					->  int:
							'''simple docstring'''






							A__            =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_sequence_classification(*SCREAMING_SNAKE_CASE_    )


			def        UpperCamelCase						(      self    )					->  int:
							'''simple docstring'''






							A__            =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_question_answering(*SCREAMING_SNAKE_CASE_    )


			@slow
			def        UpperCamelCase						(      self    )					->  List[Any]:
							'''simple docstring'''






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

@require_tf
class    a__  (   unittest.TestCase     ):
			"""simple docstring"""







			@slow
			def        UpperCamelCase						(      self    )					->  Tuple:
							'''simple docstring'''






							A__            =     TFAlbertForPreTraining.from_pretrained("albert-base-v2"    )
							A__            =     tf.constant([[0, 1, 2, 3, 4, 5]]    )
							A__            =     model(SCREAMING_SNAKE_CASE_    )[0]

							A__            =     [1, 6, 30000]
							self.assertEqual(output.shape , SCREAMING_SNAKE_CASE_    )

							A__            =     tf.constant(
							    [
							        [
							            [4.59_5668, 0.7446_2754, -1.81_8147],
							            [4.595_4347, 0.745_4184, -1.818_8258],
							            [4.595_4905, 0.744_8235, -1.818_2316],
							        ]
							    ]    )
							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


lowerCAmelCase__						=	{"""configuration_nat""": ["""NAT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """NatConfig"""]}


try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	lowerCAmelCase__						=	[
	    """NAT_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """NatForImageClassification""",
	    """NatModel""",
	    """NatPreTrainedModel""",
	    """NatBackbone""",
	]

if TYPE_CHECKING:
	from .configuration_nat import NAT_PRETRAINED_CONFIG_ARCHIVE_MAP, NatConfig

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_nat import (
		    NAT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    NatBackbone,
		    NatForImageClassification,
		    NatModel,
		    NatPreTrainedModel,
		)

else:
	import sys

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

