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





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





		def       _A							(							self    :	List[Any]				):
							lowerCAmelCase		:					Tuple  	=     ids_tensor([self.batch_size, self.seq_length]						,  self.vocab_size				)

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

							lowerCAmelCase		:					Union[str, Any]  	=     None
							if self.use_token_type_ids:
												lowerCAmelCase		:					Union[str, Any]  	=     ids_tensor([self.batch_size, self.seq_length]						,  self.type_vocab_size				)

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

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

							lowerCAmelCase		:					int  	=     [input_ids, input_mask]
							lowerCAmelCase		:					Union[str, Any]  	=     model(lowerCamelCase_				)

							lowerCAmelCase		:					List[str]  	=     model(lowerCamelCase_				)

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





		def       _A							(							self    :	Tuple						,  lowerCamelCase__    :	Dict						,  lowerCamelCase__    :	Tuple						,  lowerCamelCase__    :	List[Any]						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	int						,  lowerCamelCase__    :	Optional[Any]						,  lowerCamelCase__    :	Union[str, Any]				):
							lowerCAmelCase		:					List[str]  	=     self.num_labels
							lowerCAmelCase		:					Dict  	=     TFAlbertForSequenceClassification(config=lowerCamelCase_				)
							lowerCAmelCase		:					List[Any]  	=     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
							lowerCAmelCase		:					Tuple  	=     model(lowerCamelCase_				)
							self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.num_labels)				)





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





		def       _A							(							self    :	Tuple						,  lowerCamelCase__    :	Union[str, Any]						,  lowerCamelCase__    :	Any						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	List[str]						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	Union[str, Any]						,  lowerCamelCase__    :	Any				):
							lowerCAmelCase		:					Tuple  	=     self.num_labels
							lowerCAmelCase		:					str  	=     TFAlbertForTokenClassification(config=lowerCamelCase_				)
							lowerCAmelCase		:					Optional[Any]  	=     {
							    '''input_ids''': input_ids,
							    '''attention_mask''': input_mask,
							    '''token_type_ids''': token_type_ids,
							}
							lowerCAmelCase		:					Optional[Any]  	=     model(lowerCamelCase_				)
							self.parent.assertListEqual(list(result['''logits'''].shape				)						,  [self.batch_size, self.seq_length, self.num_labels]				)





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



@require_tf
class 		__magic_name__   (						lowerCAmelCase_,				lowerCAmelCase_,				unittest.TestCase    ):
		_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       _A							(							self    :	int						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	List[str]						,  lowerCamelCase__    :	List[Any]=False				):
							lowerCAmelCase		:					Dict  	=     super()._prepare_for_class(lowerCamelCase_						,  lowerCamelCase_						,  return_labels=lowerCamelCase_				)

							if return_labels:
												if model_class in get_values(lowerCamelCase_				):
																	lowerCAmelCase		:					str  	=     tf.zeros(self.model_tester.batch_size						,  dtype=tf.intaa				)

							return inputs_dict





		def       _A							(							self    :	Any				):
							lowerCAmelCase		:					Optional[int]  	=     TFAlbertModelTester(self				)
							lowerCAmelCase		:					List[Any]  	=     ConfigTester(self						,  config_class=lowerCamelCase_						,  hidden_size=3_7				)





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





		def       _A							(							self    :	Any				):
							lowerCAmelCase		:					List[Any]  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_model(*lowerCamelCase_				)





		def       _A							(							self    :	int				):
							lowerCAmelCase		:					Union[str, Any]  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_pretraining(*lowerCamelCase_				)





		def       _A							(							self    :	Tuple				):
							lowerCAmelCase		:					Optional[int]  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_masked_lm(*lowerCamelCase_				)





		def       _A							(							self    :	List[str]				):
							lowerCAmelCase		:					List[Any]  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_multiple_choice(*lowerCamelCase_				)





		def       _A							(							self    :	Dict				):
							lowerCAmelCase		:					List[str]  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_sequence_classification(*lowerCamelCase_				)





		def       _A							(							self    :	List[str]				):
							lowerCAmelCase		:					List[Any]  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_question_answering(*lowerCamelCase_				)





		@slow
		def       _A							(							self    :	Dict				):

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



@require_tf
class 		__magic_name__   (						unittest.TestCase    ):





		@slow
		def       _A							(							self    :	Optional[int]				):
							lowerCAmelCase		:					Dict  	=     TFAlbertForPreTraining.from_pretrained('''albert-base-v2'''				)
							lowerCAmelCase		:					List[Any]  	=     tf.constant([[0, 1, 2, 3, 4, 5]]				)
							lowerCAmelCase		:					Tuple  	=     model(lowerCamelCase_				)[0]

							lowerCAmelCase		:					Optional[int]  	=     [1, 6, 3_0_0_0_0]
							self.assertEqual(output.shape						,  lowerCamelCase_				)

							lowerCAmelCase		:					str  	=     tf.constant(
							    [
							        [
							            [4.5_9_5_6_6_8, 0.7_4_4_6_2_7_5_4, -1.8_1_8_1_4_7],
							            [4.5_9_5_4_3_4_7, 0.7_4_5_4_1_8_4, -1.8_1_8_8_2_5_8],
							            [4.5_9_5_4_9_0_5, 0.7_4_4_8_2_3_5, -1.8_1_8_2_3_1_6],
							        ]
							    ]				)
							tf.debugging.assert_near(output[:, :3, :3]						,  lowerCamelCase_						,  atol=1E-4				)


def     UpperCAmelCase__							(       __magic_name__						:     Tuple							,  __magic_name__						:     int							,  __magic_name__						:     Dict							,  __magic_name__						:     str  ):


					'''simple docstring'''

					if start == end:
										return start if lst[start] > item else start + 1
					if start > end:
										return start

					lowerCAmelCase		:					Optional[Any]  	=     (start + end) // 2
					if lst[mid] < item:
										return binary_search(__magic_name__							,  __magic_name__							,  mid + 1							,  __magic_name__  )
					elif lst[mid] > item:
										return binary_search(__magic_name__							,  __magic_name__							,  __magic_name__							,  mid - 1  )
					else:
										return mid
def     UpperCAmelCase__							(       __magic_name__						:     List[Any]  ):


					'''simple docstring'''

					lowerCAmelCase		:					Union[str, Any]  	=     len(__magic_name__  )

					for index in range(1							,  __magic_name__  ):
										lowerCAmelCase		:					Optional[int]  	=     lst[index]
										lowerCAmelCase		:					Dict  	=     binary_search(__magic_name__							,  __magic_name__							,  0							,  index - 1  )
										lowerCAmelCase		:					Union[str, Any]  	=     lst[:pos] + [value] + lst[pos:index] + lst[index + 1 :]

					return lst
def     UpperCAmelCase__							(       __magic_name__						:     Optional[int]							,  __magic_name__						:     List[str]  ):


					'''simple docstring'''

					if not left:
										return right

					if not right:
										return left

					if left[0] < right[0]:
										return [left[0], *merge(left[1:]							,  __magic_name__  )]

					return [right[0], *merge(__magic_name__							,  right[1:]  )]
def     UpperCAmelCase__							(       __magic_name__						:     str  ):


					'''simple docstring'''

					lowerCAmelCase		:					str  	=     len(__magic_name__  )
					lowerCAmelCase	,							lowerCAmelCase		:					Union[str, Any]  	=     [], []
					lowerCAmelCase		:					Union[str, Any]  	=     [lst[0]]
					lowerCAmelCase		:					Optional[Any]  	=     []
					lowerCAmelCase		:					List[Any]  	=     1
					while i < length:
										if lst[i] < lst[i - 1]:
															runs.append(__magic_name__  )
															lowerCAmelCase		:					Any  	=     [lst[i]]
										else:
															new_run.append(lst[i]  )
										i += 1
					runs.append(__magic_name__  )

					for run in runs:
										sorted_runs.append(insertion_sort(__magic_name__  )  )
					for run in sorted_runs:
										lowerCAmelCase		:					int  	=     merge(__magic_name__							,  __magic_name__  )

					return sorted_array
def     UpperCAmelCase__							(       ):


					'''simple docstring'''

					lowerCAmelCase		:					Tuple  	=     [5, 9, 10, 3, -4, 5, 1_78, 92, 46, -18, 0, 7]
					lowerCAmelCase		:					Optional[Any]  	=     tim_sort(__magic_name__  )
					print(__magic_name__  )


if __name__ == "__main__":
		main()
