

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				:       List[Any]							,     _UpperCamelCase				:       Any							,     _UpperCamelCase				:       str=1_3							,     _UpperCamelCase				:       List[Any]=7							,     _UpperCamelCase				:       Optional[int]=True							,     _UpperCamelCase				:       Dict=True							,     _UpperCamelCase				:       List[Any]=True							,     _UpperCamelCase				:       Dict=True							,     _UpperCamelCase				:       Optional[int]=9_9							,     _UpperCamelCase				:       List[str]=1_6							,     _UpperCamelCase				:       int=3_2							,     _UpperCamelCase				:       Tuple=2							,     _UpperCamelCase				:       List[str]=4							,     _UpperCamelCase				:       Tuple=3_7							,     _UpperCamelCase				:       Optional[Any]="gelu"							,     _UpperCamelCase				:       Optional[Any]=0.1							,     _UpperCamelCase				:       Optional[Any]=0.1							,     _UpperCamelCase				:       Optional[Any]=5_1_2							,     _UpperCamelCase				:       Tuple=1_6							,     _UpperCamelCase				:       Tuple=2							,     _UpperCamelCase				:       Union[str, Any]=0.02							,     _UpperCamelCase				:       Optional[Any]=3							,     _UpperCamelCase				:       Any=4							,     _UpperCamelCase				:       List[str]=None							,     )   ->Optional[int]:
									snake_case_     =			parent
									snake_case_     =			1_3
									snake_case_     =			7
									snake_case_     =			True
									snake_case_     =			True
									snake_case_     =			True
									snake_case_     =			True
									snake_case_     =			9_9
									snake_case_     =			1_6
									snake_case_     =			3_2
									snake_case_     =			2
									snake_case_     =			4
									snake_case_     =			3_7
									snake_case_     =			'''gelu'''
									snake_case_     =			0.1
									snake_case_     =			0.1
									snake_case_     =			5_1_2
									snake_case_     =			1_6
									snake_case_     =			2
									snake_case_     =			0.02
									snake_case_     =			3
									snake_case_     =			4
									snake_case_     =			None







			def        snake_case__(  self				:       Optional[int]							)   ->Dict:
									snake_case_     =			ids_tensor([self.batch_size, self.seq_length]							,     self.vocab_size							)

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

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

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

									snake_case_     =			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        snake_case__(  self				:       str							,     _UpperCamelCase				:       List[str]							,     _UpperCamelCase				:       Optional[int]							,     _UpperCamelCase				:       List[str]							,     _UpperCamelCase				:       int							,     _UpperCamelCase				:       Optional[int]							,     _UpperCamelCase				:       int							,     _UpperCamelCase				:       Tuple							)   ->Dict:
									snake_case_     =			TFAlbertModel(config=_UpperCamelCase							)
									# inputs = {'input_ids': input_ids,
									#           'attention_mask': input_mask,
									#           'token_type_ids': token_type_ids}
									# sequence_output, pooled_output = model(**inputs)
									snake_case_     =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									snake_case_     =			model(_UpperCamelCase							)

									snake_case_     =			[input_ids, input_mask]
									snake_case_     =			model(_UpperCamelCase							)

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







			def        snake_case__(  self				:       List[Any]							,     _UpperCamelCase				:       List[str]							,     _UpperCamelCase				:       str							,     _UpperCamelCase				:       Any							,     _UpperCamelCase				:       int							,     _UpperCamelCase				:       Any							,     _UpperCamelCase				:       int							,     _UpperCamelCase				:       Union[str, Any]							)   ->int:
									snake_case_     =			self.num_labels
									snake_case_     =			TFAlbertForSequenceClassification(config=_UpperCamelCase							)
									snake_case_     =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									snake_case_     =			model(_UpperCamelCase							)
									self.parent.assertEqual(result.logits.shape							,     (self.batch_size, self.num_labels)							)







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







			def        snake_case__(  self				:       List[Any]							,     _UpperCamelCase				:       List[Any]							,     _UpperCamelCase				:       Any							,     _UpperCamelCase				:       str							,     _UpperCamelCase				:       Union[str, Any]							,     _UpperCamelCase				:       Any							,     _UpperCamelCase				:       List[str]							,     _UpperCamelCase				:       Tuple							)   ->Any:
									snake_case_     =			self.num_labels
									snake_case_     =			TFAlbertForTokenClassification(config=_UpperCamelCase							)
									snake_case_     =			{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}
									snake_case_     =			model(_UpperCamelCase							)
									self.parent.assertListEqual(list(result['''logits'''].shape							)							,     [self.batch_size, self.seq_length, self.num_labels]							)







			def        snake_case__(  self				:       int							)   ->Union[str, Any]:
									snake_case_     =			self.prepare_config_and_inputs()
									(
									    (
									    snake_case_
									),					(
									    snake_case_
									),					(
									    snake_case_
									),					(
									    snake_case_
									),					(
									    snake_case_
									),					(
									    snake_case_
									),					(
									    snake_case_
									),					
									)     =			config_and_inputs
									snake_case_     =			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
									return config, inputs_dict






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







			'''simple docstring'''

			SCREAMING_SNAKE_CASE  :     Dict		    =       (
			    (
			        TFAlbertModel,
			        TFAlbertForPreTraining,
			        TFAlbertForMaskedLM,
			        TFAlbertForSequenceClassification,
			        TFAlbertForQuestionAnswering,
			        TFAlbertForTokenClassification,
			        TFAlbertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			SCREAMING_SNAKE_CASE  :     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 {}
			)
			SCREAMING_SNAKE_CASE  :     str		    =       False
			SCREAMING_SNAKE_CASE  :     Optional[Any]		    =       False







			def        snake_case__(  self				:       Any							,     _UpperCamelCase				:       Optional[int]							,     _UpperCamelCase				:       int							,     _UpperCamelCase				:       Union[str, Any]=False							)   ->Tuple:
									snake_case_     =			super()._prepare_for_class(_UpperCamelCase							,     _UpperCamelCase							,     return_labels=_UpperCamelCase							)

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

									return inputs_dict







			def        snake_case__(  self				:       List[Any]							)   ->List[str]:
									snake_case_     =			TFAlbertModelTester(self							)
									snake_case_     =			ConfigTester(self							,     config_class=_UpperCamelCase							,     hidden_size=3_7							)







			def        snake_case__(  self				:       Tuple							)   ->Dict:
									self.config_tester.run_common_tests()







			def        snake_case__(  self				:       Optional[int]							)   ->Any:
									snake_case_     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_model(*_UpperCamelCase							)







			def        snake_case__(  self				:       int							)   ->Union[str, Any]:
									snake_case_     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_for_pretraining(*_UpperCamelCase							)







			def        snake_case__(  self				:       List[str]							)   ->Any:
									snake_case_     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_for_masked_lm(*_UpperCamelCase							)







			def        snake_case__(  self				:       List[str]							)   ->List[str]:
									snake_case_     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_for_multiple_choice(*_UpperCamelCase							)







			def        snake_case__(  self				:       Optional[int]							)   ->Optional[Any]:
									snake_case_     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_for_sequence_classification(*_UpperCamelCase							)







			def        snake_case__(  self				:       int							)   ->Optional[int]:
									snake_case_     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_for_question_answering(*_UpperCamelCase							)







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







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






@require_tf
class 			snake_case_     (	unittest.TestCase      ):







			'''simple docstring'''

			@slow
			def        snake_case__(  self				:       List[str]							)   ->Optional[Any]:
									snake_case_     =			TFAlbertForPreTraining.from_pretrained('''albert-base-v2'''							)
									snake_case_     =			tf.constant([[0, 1, 2, 3, 4, 5]]							)
									snake_case_     =			model(_UpperCamelCase							)[0]

									snake_case_     =			[1, 6, 3_0_0_0_0]
									self.assertEqual(output.shape							,     _UpperCamelCase							)

									snake_case_     =			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]							,     _UpperCamelCase							,     atol=1e-4							)


lowerCAmelCase_ 			=			(1, 1_53, 3_70, 3_71, 16_34, 24_67_80_51, 1_15_13_22_19_01_87_63_99_25_65_09_55_97_97_39_71_52_24_01)
lowerCAmelCase_ 			=			(-1_53, -1, 0, 1.2, 2_00, "A", [], {}, None)


def 						__SCREAMING_SNAKE_CASE      (SCREAMING_SNAKE_CASE__ ):
						if not isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ ) or n < 1:
												return False

						# Initialization of sum and number of digits.
						snake_case_     =			0
						snake_case_     =			0
						snake_case_     =			n
						# Calculation of digits of the number
						while temp > 0:
												number_of_digits += 1
												temp //= 10
						# Dividing number into separate digits and find Armstrong number
						snake_case_     =			n
						while temp > 0:
												snake_case_     =			temp % 10
												total += rem**number_of_digits
												temp //= 10
						return n == total


def 						__SCREAMING_SNAKE_CASE      (SCREAMING_SNAKE_CASE__ ):
						if not isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ ) or n < 1:
												return False

						# Init a "histogram" of the digits
						snake_case_     =			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
						snake_case_     =			0
						snake_case_     =			0
						snake_case_     =			n
						while temp > 0:
												snake_case_,					snake_case_     =			divmod(SCREAMING_SNAKE_CASE__						,	10 )
												digit_histogram[rem] += 1
												digit_total += 1

						for cnt, i in zip(SCREAMING_SNAKE_CASE__						,	range(len(SCREAMING_SNAKE_CASE__ ) ) ):
												total += cnt * i**digit_total

						return n == total


def 						__SCREAMING_SNAKE_CASE      (SCREAMING_SNAKE_CASE__ ):
						if not isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ ) or n < 1:
												return False
						snake_case_     =			len(str(SCREAMING_SNAKE_CASE__ ) )  # the power that all digits will be raised to
						# check if sum of each digit multiplied expo times is equal to number
						return n == sum(int(SCREAMING_SNAKE_CASE__ ) ** expo for i in str(SCREAMING_SNAKE_CASE__ ) )


def 						__SCREAMING_SNAKE_CASE      ():
						snake_case_     =			int(input('''Enter an integer to see if it is an Armstrong number: ''' ).strip() )
						print(F'''{num} is {'' if armstrong_number(SCREAMING_SNAKE_CASE__ ) else 'not '}an Armstrong number.''' )
						print(F'''{num} is {'' if narcissistic_number(SCREAMING_SNAKE_CASE__ ) else 'not '}an Armstrong number.''' )
						print(F'''{num} is {'' if pluperfect_number(SCREAMING_SNAKE_CASE__ ) else 'not '}an Armstrong number.''' )


if __name__ == "__main__":
					import doctest

					doctest.testmod()
					main()