





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	:






			def __init__(self ,							__magic_name__ ,							__magic_name__=13 ,							__magic_name__=7 ,							__magic_name__=True ,							__magic_name__=True ,							__magic_name__=True ,							__magic_name__=True ,							__magic_name__=99 ,							__magic_name__=16 ,							__magic_name__=36 ,							__magic_name__=6 ,							__magic_name__=6 ,							__magic_name__=6 ,							__magic_name__=37 ,							__magic_name__="gelu" ,							__magic_name__=0.1 ,							__magic_name__=0.1 ,							__magic_name__=512 ,							__magic_name__=16 ,							__magic_name__=2 ,							__magic_name__=0.02 ,							__magic_name__=3 ,							__magic_name__=4 ,							__magic_name__=None ,							)					->       Optional[int]:


							'''simple docstring'''



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






			def       lowerCamelCase  (self   )					->       str:


							'''simple docstring'''



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

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

							snake_case_						:   Dict           =  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_						:   str           =  None
							snake_case_						:   List[str]           =  None
							snake_case_						:   Tuple           =  None
							if self.use_labels:
											snake_case_						:   Dict           =  ids_tensor([self.batch_size] ,							self.type_sequence_label_size   )
											snake_case_						:   List[str]           =  ids_tensor([self.batch_size, self.seq_length] ,							self.num_labels   )
											snake_case_						:   Optional[int]           =  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       lowerCamelCase  (self   )					->       Optional[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       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       Tuple:


							'''simple docstring'''



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


							'''simple docstring'''



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






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       Optional[int]:


							'''simple docstring'''



							snake_case_						:   Any           =  AlbertForMaskedLM(config=_UpperCAmelCase   )
							model.to(_UpperCAmelCase   )
							model.eval()
							snake_case_						:   Dict           =  model(_UpperCAmelCase ,							attention_mask=_UpperCAmelCase ,							token_type_ids=_UpperCAmelCase ,							labels=_UpperCAmelCase   )
							self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.seq_length, self.vocab_size)   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       Dict:


							'''simple docstring'''



							snake_case_						:   int           =  AlbertForQuestionAnswering(config=_UpperCAmelCase   )
							model.to(_UpperCAmelCase   )
							model.eval()
							snake_case_						:   Optional[Any]           =  model(
							    _UpperCAmelCase ,							attention_mask=_UpperCAmelCase ,							token_type_ids=_UpperCAmelCase ,							start_positions=_UpperCAmelCase ,							end_positions=_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       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       List[str]:


							'''simple docstring'''



							snake_case_						:   Union[str, Any]           =  self.num_labels
							snake_case_						:   Any           =  AlbertForSequenceClassification(_UpperCAmelCase   )
							model.to(_UpperCAmelCase   )
							model.eval()
							snake_case_						:   Any           =  model(_UpperCAmelCase ,							attention_mask=_UpperCAmelCase ,							token_type_ids=_UpperCAmelCase ,							labels=_UpperCAmelCase   )
							self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.num_labels)   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       int:


							'''simple docstring'''



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






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       List[Any]:


							'''simple docstring'''



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






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


							'''simple docstring'''



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




@require_torch
class  __lowerCAmelCase	(      _UpperCAmelCase,					_UpperCAmelCase,					unittest.TestCase					):
			lowerCamelCase_					:					Optional[Any]	      =				(
			    (
			        AlbertModel,
			        AlbertForPreTraining,
			        AlbertForMaskedLM,
			        AlbertForMultipleChoice,
			        AlbertForSequenceClassification,
			        AlbertForTokenClassification,
			        AlbertForQuestionAnswering,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase_					:					str	      =				(
			    {
			        "feature-extraction": AlbertModel,
			        "fill-mask": AlbertForMaskedLM,
			        "question-answering": AlbertForQuestionAnswering,
			        "text-classification": AlbertForSequenceClassification,
			        "token-classification": AlbertForTokenClassification,
			        "zero-shot": AlbertForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			lowerCamelCase_					:					Any	      =				True






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__=False   )					->       str:


							'''simple docstring'''



							snake_case_						:   Optional[Any]           =  super()._prepare_for_class(_UpperCAmelCase ,							_UpperCAmelCase ,							return_labels=_UpperCAmelCase   )

							if return_labels:
											if model_class in get_values(_UpperCAmelCase   ):
															snake_case_						:   Dict           =  torch.zeros(
															    (self.model_tester.batch_size, self.model_tester.seq_length) ,							dtype=torch.long ,							device=_UpperCAmelCase   )
															snake_case_						:   Optional[int]           =  torch.zeros(
															    self.model_tester.batch_size ,							dtype=torch.long ,							device=_UpperCAmelCase   )
							return inputs_dict






			def       lowerCamelCase  (self   )					->       str:


							'''simple docstring'''



							snake_case_						:   Optional[int]           =  AlbertModelTester(self   )
							snake_case_						:   Optional[int]           =  ConfigTester(self ,							config_class=_UpperCAmelCase ,							hidden_size=37   )






			def       lowerCamelCase  (self   )					->       Dict:


							'''simple docstring'''



							self.config_tester.run_common_tests()






			def       lowerCamelCase  (self   )					->       Tuple:


							'''simple docstring'''



							snake_case_						:   List[Any]           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_UpperCAmelCase   )






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


							'''simple docstring'''



							snake_case_						:   Optional[Any]           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*_UpperCAmelCase   )






			def       lowerCamelCase  (self   )					->       Tuple:


							'''simple docstring'''



							snake_case_						:   Any           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase   )






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


							'''simple docstring'''



							snake_case_						:   Tuple           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase   )






			def       lowerCamelCase  (self   )					->       Any:


							'''simple docstring'''



							snake_case_						:   Optional[int]           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase   )






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


							'''simple docstring'''



							snake_case_						:   Optional[int]           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase   )






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


							'''simple docstring'''



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






			@slow
			def       lowerCamelCase  (self   )					->       Optional[Any]:


							'''simple docstring'''



							for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											snake_case_						:   str           =  AlbertModel.from_pretrained(_UpperCAmelCase   )
											self.assertIsNotNone(_UpperCAmelCase   )




@require_torch
class  __lowerCAmelCase	(      unittest.TestCase					):






			@slow
			def       lowerCamelCase  (self   )					->       Union[str, Any]:


							'''simple docstring'''



							snake_case_						:   Union[str, Any]           =  AlbertModel.from_pretrained('''albert-base-v2'''   )
							snake_case_						:   Dict           =  torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]   )
							snake_case_						:   Optional[Any]           =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
							with torch.no_grad():
											snake_case_						:   List[str]           =  model(_UpperCAmelCase ,							attention_mask=_UpperCAmelCase   )[0]
							snake_case_						:   Any           =  torch.Size((1, 11, 768)   )
							self.assertEqual(output.shape ,							_UpperCAmelCase   )
							snake_case_						:   Optional[Any]           =  torch.tensor(
							    [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]]   )

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







def 					lowerCamelCase_						(    _UpperCamelCase			)		->						str:






				"""simple docstring"""


				if number > 0:
								raise ValueError('''input must be a negative integer'''			)
				snake_case_						:   List[str]           =  len(bin(_UpperCamelCase			)[3:]			)
				snake_case_						:   str           =  bin(abs(_UpperCamelCase			) - (1 << binary_number_length)			)[3:]
				snake_case_						:   Dict           =  (
				    (
				        '''1'''
				        + '''0''' * (binary_number_length - len(_UpperCamelCase			))
				        + twos_complement_number
				    )
				    if number < 0
				    else '''0'''
				)
				return "0b" + twos_complement_number


if __name__ == "__main__":
	import doctest

	doctest.testmod()
