



"""simple docstring"""





import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    MODEL_FOR_PRETRAINING_MAPPING,
					    AlbertForMaskedLM,
					    AlbertForMultipleChoice,
					    AlbertForPreTraining,
					    AlbertForQuestionAnswering,
					    AlbertForSequenceClassification,
					    AlbertForTokenClassification,
					    AlbertModel,
					)
					from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST



class  _lowerCAmelCase     :





		"""simple docstring"""






		def __init__(       self				,     _lowercase				,     _lowercase=1_3				,     _lowercase=7				,     _lowercase=True				,     _lowercase=True				,     _lowercase=True				,     _lowercase=True				,     _lowercase=9_9				,     _lowercase=1_6				,     _lowercase=3_6				,     _lowercase=6				,     _lowercase=6				,     _lowercase=6				,     _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				,     )			->    int:







								'''simple docstring'''

								snake_case_ :			Dict            =      parent
								snake_case_ :			Dict            =      batch_size
								snake_case_ :			str            =      seq_length
								snake_case_ :			List[str]            =      is_training
								snake_case_ :			Tuple            =      use_input_mask
								snake_case_ :			Dict            =      use_token_type_ids
								snake_case_ :			List[Any]            =      use_labels
								snake_case_ :			Optional[Any]            =      vocab_size
								snake_case_ :			List[Any]            =      embedding_size
								snake_case_ :			List[str]            =      hidden_size
								snake_case_ :			str            =      num_hidden_layers
								snake_case_ :			Any            =      num_hidden_groups
								snake_case_ :			Union[str, Any]            =      num_attention_heads
								snake_case_ :			Optional[int]            =      intermediate_size
								snake_case_ :			Tuple            =      hidden_act
								snake_case_ :			str            =      hidden_dropout_prob
								snake_case_ :			Optional[int]            =      attention_probs_dropout_prob
								snake_case_ :			Tuple            =      max_position_embeddings
								snake_case_ :			List[str]            =      type_vocab_size
								snake_case_ :			Tuple            =      type_sequence_label_size
								snake_case_ :			Dict            =      initializer_range
								snake_case_ :			Dict            =      num_labels
								snake_case_ :			Dict            =      num_choices
								snake_case_ :			Tuple            =      scope





		def  UpperCAmelCase__				(       self )			->    Optional[int]:







								'''simple docstring'''

								snake_case_ :			Dict            =      ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size )

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

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

								snake_case_ :			List[Any]            =      None
								snake_case_ :			Tuple            =      None
								snake_case_ :			Optional[Any]            =      None
								if self.use_labels:
														snake_case_ :			int            =      ids_tensor([self.batch_size]				,     self.type_sequence_label_size )
														snake_case_ :			Tuple            =      ids_tensor([self.batch_size, self.seq_length]				,     self.num_labels )
														snake_case_ :			Tuple            =      ids_tensor([self.batch_size]				,     self.num_choices )

								snake_case_ :			List[str]            =      self.get_config()

								return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





		def  UpperCAmelCase__				(       self )			->    Union[str, Any]:







								'''simple docstring'''

								return AlbertConfig(
								    vocab_size=self.vocab_size				,     hidden_size=self.hidden_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				,     num_hidden_groups=self.num_hidden_groups				,     )





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    List[str]:







								'''simple docstring'''

								snake_case_ :			Any            =      AlbertModel(config=_lowercase )
								model.to(_lowercase )
								model.eval()
								snake_case_ :			str            =      model(_lowercase				,     attention_mask=_lowercase				,     token_type_ids=_lowercase )
								snake_case_ :			List[str]            =      model(_lowercase				,     token_type_ids=_lowercase )
								snake_case_ :			Dict            =      model(_lowercase )
								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 )			->    List[Any]:







								'''simple docstring'''

								snake_case_ :			List[str]            =      AlbertForPreTraining(config=_lowercase )
								model.to(_lowercase )
								model.eval()
								snake_case_ :			Tuple            =      model(
								    _lowercase				,     attention_mask=_lowercase				,     token_type_ids=_lowercase				,     labels=_lowercase				,     sentence_order_label=_lowercase				,     )
								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, config.num_labels) )





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    int:







								'''simple docstring'''

								snake_case_ :			List[str]            =      AlbertForMaskedLM(config=_lowercase )
								model.to(_lowercase )
								model.eval()
								snake_case_ :			List[Any]            =      model(_lowercase				,     attention_mask=_lowercase				,     token_type_ids=_lowercase				,     labels=_lowercase )
								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 )			->    str:







								'''simple docstring'''

								snake_case_ :			Optional[int]            =      AlbertForQuestionAnswering(config=_lowercase )
								model.to(_lowercase )
								model.eval()
								snake_case_ :			Tuple            =      model(
								    _lowercase				,     attention_mask=_lowercase				,     token_type_ids=_lowercase				,     start_positions=_lowercase				,     end_positions=_lowercase				,     )
								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 )			->    Any:







								'''simple docstring'''

								snake_case_ :			int            =      self.num_labels
								snake_case_ :			Any            =      AlbertForSequenceClassification(_lowercase )
								model.to(_lowercase )
								model.eval()
								snake_case_ :			Dict            =      model(_lowercase				,     attention_mask=_lowercase				,     token_type_ids=_lowercase				,     labels=_lowercase )
								self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels) )





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    Any:







								'''simple docstring'''

								snake_case_ :			Any            =      self.num_labels
								snake_case_ :			List[str]            =      AlbertForTokenClassification(config=_lowercase )
								model.to(_lowercase )
								model.eval()
								snake_case_ :			Tuple            =      model(_lowercase				,     attention_mask=_lowercase				,     token_type_ids=_lowercase				,     labels=_lowercase )
								self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels) )





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    int:







								'''simple docstring'''

								snake_case_ :			str            =      self.num_choices
								snake_case_ :			Any            =      AlbertForMultipleChoice(config=_lowercase )
								model.to(_lowercase )
								model.eval()
								snake_case_ :			int            =      input_ids.unsqueeze(1 ).expand(-1				,     self.num_choices				,     -1 ).contiguous()
								snake_case_ :			List[str]            =      token_type_ids.unsqueeze(1 ).expand(-1				,     self.num_choices				,     -1 ).contiguous()
								snake_case_ :			List[str]            =      input_mask.unsqueeze(1 ).expand(-1				,     self.num_choices				,     -1 ).contiguous()
								snake_case_ :			Optional[int]            =      model(
								    _lowercase				,     attention_mask=_lowercase				,     token_type_ids=_lowercase				,     labels=_lowercase				,     )
								self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices) )





		def  UpperCAmelCase__				(       self )			->    str:







								'''simple docstring'''

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




@require_torch
class  _lowerCAmelCase     (					SCREAMING_SNAKE_CASE__	,					SCREAMING_SNAKE_CASE__	,					unittest.TestCase		):





		"""simple docstring"""






		_lowerCamelCase      	=							(
		    (
		        AlbertModel,
		        AlbertForPreTraining,
		        AlbertForMaskedLM,
		        AlbertForMultipleChoice,
		        AlbertForSequenceClassification,
		        AlbertForTokenClassification,
		        AlbertForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		_lowerCamelCase      	=							(
		    {
		        '''feature-extraction''': AlbertModel,
		        '''fill-mask''': AlbertForMaskedLM,
		        '''question-answering''': AlbertForQuestionAnswering,
		        '''text-classification''': AlbertForSequenceClassification,
		        '''token-classification''': AlbertForTokenClassification,
		        '''zero-shot''': AlbertForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		_lowerCamelCase      	=							True





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase=False )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			List[str]            =      super()._prepare_for_class(_lowercase				,     _lowercase				,     return_labels=_lowercase )

								if return_labels:
														if model_class in get_values(_lowercase ):
																				snake_case_ :			List[str]            =      torch.zeros(
																				    (self.model_tester.batch_size, self.model_tester.seq_length)				,     dtype=torch.long				,     device=_lowercase )
																				snake_case_ :			List[str]            =      torch.zeros(
																				    self.model_tester.batch_size				,     dtype=torch.long				,     device=_lowercase )
								return inputs_dict





		def  UpperCAmelCase__				(       self )			->    List[Any]:







								'''simple docstring'''

								snake_case_ :			Tuple            =      AlbertModelTester(self )
								snake_case_ :			str            =      ConfigTester(self				,     config_class=_lowercase				,     hidden_size=3_7 )





		def  UpperCAmelCase__				(       self )			->    str:







								'''simple docstring'''

								self.config_tester.run_common_tests()





		def  UpperCAmelCase__				(       self )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			Dict            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    int:







								'''simple docstring'''

								snake_case_ :			Optional[int]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_pretraining(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    Union[str, Any]:







								'''simple docstring'''

								snake_case_ :			Dict            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    Dict:







								'''simple docstring'''

								snake_case_ :			Any            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    List[str]:







								'''simple docstring'''

								snake_case_ :			Union[str, Any]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			Dict            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    Optional[int]:







								'''simple docstring'''

								snake_case_ :			Any            =      self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
														snake_case_ :			List[Any]            =      type
														self.model_tester.create_and_check_model(*_lowercase )





		@slow
		def  UpperCAmelCase__				(       self )			->    str:







								'''simple docstring'''

								for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														snake_case_ :			Union[str, Any]            =      AlbertModel.from_pretrained(_lowercase )
														self.assertIsNotNone(_lowercase )




@require_torch
class  _lowerCAmelCase     (					unittest.TestCase		):





		"""simple docstring"""






		@slow
		def  UpperCAmelCase__				(       self )			->    Any:







								'''simple docstring'''

								snake_case_ :			Any            =      AlbertModel.from_pretrained("""albert-base-v2""" )
								snake_case_ :			str            =      torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]] )
								snake_case_ :			Union[str, Any]            =      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
								with torch.no_grad():
														snake_case_ :			Optional[int]            =      model(_lowercase				,     attention_mask=_lowercase )[0]
								snake_case_ :			int            =      torch.Size((1, 1_1, 7_6_8) )
								self.assertEqual(output.shape				,     _lowercase )
								snake_case_ :			Any            =      torch.tensor(
								    [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] )

								self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,     _lowercase				,     atol=1E-4 ) )








class 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     Dict					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Optional[Any]=None					,      __lowerCamelCase							:     Optional[Any]=None   )       -> Dict:

			'''simple docstring'''




			__lowercase											=     data
			__lowercase											=     previous
			__lowercase											=     next_node




	def __str__(						self							:     int   )       -> str:

			'''simple docstring'''




			return F"{self.data}"




	def    UpperCAmelCase			(						self							:     int   )       -> int:

			'''simple docstring'''




			return self.data




	def    UpperCAmelCase			(						self							:     Tuple   )       -> Optional[int]:

			'''simple docstring'''




			return self.next






	def    UpperCAmelCase			(						self							:     Optional[int]   )       -> Any:

			'''simple docstring'''




			return self.previous





class 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     Tuple					,      __lowerCamelCase							:     Optional[Any]   )       -> str:

			'''simple docstring'''




			__lowercase											=     head




	def __iter__(						self							:     Union[str, Any]   )       -> List[Any]:

			'''simple docstring'''




			return self






	def    UpperCAmelCase			(						self							:     str   )       -> str:

			'''simple docstring'''




			if not self.current:
					raise StopIteration
			else:
					__lowercase											=     self.current.get_data()
					__lowercase											=     self.current.get_next()
					return value





class 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     List[str]   )       -> Any:

			'''simple docstring'''




			__lowercase											=     None  # First node in list
			__lowercase											=     None  # Last node in list




	def __str__(						self							:     int   )       -> Any:

			'''simple docstring'''




			__lowercase											=     self.head
			__lowercase											=     []
			while current is not None:
					nodes.append(current.get_data()   )
					__lowercase											=     current.get_next()
			return " ".join(str(__lowerCamelCase   ) for node in nodes   )




	def __contains__(						self							:     Tuple					,      __lowerCamelCase							:     int   )       -> List[Any]:

			'''simple docstring'''




			__lowercase											=     self.head
			while current:
					if current.get_data() == value:
							return True
					__lowercase											=     current.get_next()
			return False




	def __iter__(						self							:     Optional[int]   )       -> List[str]:

			'''simple docstring'''




			return LinkedListIterator(self.head   )




	def    UpperCAmelCase			(						self							:     Any   )       -> str:

			'''simple docstring'''




			if self.head:
					return self.head.get_data()
			return None




	def    UpperCAmelCase			(						self							:     str   )       -> Union[str, Any]:

			'''simple docstring'''




			if self.tail:
					return self.tail.get_data()
			return None




	def    UpperCAmelCase			(						self							:     str					,      __lowerCamelCase							:     Node   )       -> None:

			'''simple docstring'''




			if self.head is None:
					__lowercase											=     node
					__lowercase											=     node
			else:
					self.insert_before_node(self.head					,      __lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Dict					,      __lowerCamelCase							:     Node   )       -> None:

			'''simple docstring'''




			if self.head is None:
					self.set_head(__lowerCamelCase   )
			else:
					self.insert_after_node(self.tail					,      __lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     int   )       -> None:

			'''simple docstring'''




			__lowercase											=     Node(__lowerCamelCase   )
			if self.head is None:
					self.set_head(__lowerCamelCase   )
			else:
					self.set_tail(__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     int					,      __lowerCamelCase							:     Node					,      __lowerCamelCase							:     Node   )       -> None:

			'''simple docstring'''




			__lowercase											=     node
			__lowercase											=     node.previous

			if node.get_previous() is None:
					__lowercase											=     node_to_insert
			else:
					__lowercase											=     node_to_insert

			__lowercase											=     node_to_insert




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     Node					,      __lowerCamelCase							:     Node   )       -> None:

			'''simple docstring'''




			__lowercase											=     node
			__lowercase											=     node.next

			if node.get_next() is None:
					__lowercase											=     node_to_insert
			else:
					__lowercase											=     node_to_insert

			__lowercase											=     node_to_insert




	def    UpperCAmelCase			(						self							:     List[str]					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     int   )       -> None:

			'''simple docstring'''




			__lowercase											=     1
			__lowercase											=     Node(__lowerCamelCase   )
			__lowercase											=     self.head
			while node:
					if current_position == position:
							self.insert_before_node(__lowerCamelCase					,      __lowerCamelCase   )
							return
					current_position += 1
					__lowercase											=     node.next
			self.insert_after_node(self.tail					,      __lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     int   )       -> Node:

			'''simple docstring'''




			__lowercase											=     self.head
			while node:
					if node.get_data() == item:
							return node
					__lowercase											=     node.get_next()
			raise Exception('Node not found'   )




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     List[str]   )       -> Dict:

			'''simple docstring'''




			if (node := self.get_node(__lowerCamelCase   )) is not None:
					if node == self.head:
							__lowercase											=     self.head.get_next()

					if node == self.tail:
							__lowercase											=     self.tail.get_previous()

					self.remove_node_pointers(__lowerCamelCase   )




	@staticmethod
	def    UpperCAmelCase			(						__lowerCamelCase							:     Node   )       -> None:

			'''simple docstring'''




			if node.get_next():
					__lowercase											=     node.previous

			if node.get_previous():
					__lowercase											=     node.next

			__lowercase											=     None
			__lowercase											=     None






	def    UpperCAmelCase			(						self							:     str   )       -> List[str]:

			'''simple docstring'''




			return self.head is None





def  SCREAMING_SNAKE_CASE				(      )		->    None:
		pass


if __name__ == "__main__":
		import doctest

		doctest.testmod()




