






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





							"""simple docstring"""

							def __init__(  self:   Dict		,     __A:   Union[str, Any]		,     __A:   Tuple=13		,     __A:   List[Any]=7		,     __A:   List[Any]=True		,     __A:   int=True		,     __A:   Any=True		,     __A:   Union[str, Any]=True		,     __A:   Dict=99		,     __A:   Union[str, Any]=16		,     __A:   Any=36		,     __A:   List[Any]=6		,     __A:   List[Any]=6		,     __A:   Tuple=6		,     __A:   List[str]=37		,     __A:   List[Any]="gelu"		,     __A:   List[str]=0.1		,     __A:   Tuple=0.1		,     __A:   Union[str, Any]=5_12		,     __A:   int=16		,     __A:   int=2		,     __A:   Optional[Any]=0.02		,     __A:   List[Any]=3		,     __A:   Dict=4		,     __A:   Dict=None		,     )    ->				Dict:
												_A								= parent
												_A								= batch_size
												_A								= seq_length
												_A								= is_training
												_A								= use_input_mask
												_A								= use_token_type_ids
												_A								= use_labels
												_A								= vocab_size
												_A								= embedding_size
												_A								= hidden_size
												_A								= num_hidden_layers
												_A								= num_hidden_groups
												_A								= num_attention_heads
												_A								= intermediate_size
												_A								= hidden_act
												_A								= hidden_dropout_prob
												_A								= attention_probs_dropout_prob
												_A								= max_position_embeddings
												_A								= type_vocab_size
												_A								= type_sequence_label_size
												_A								= initializer_range
												_A								= num_labels
												_A								= num_choices
												_A								= scope

							def       __A (  self:   int   )    ->				str:
												_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								= self.get_config()

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

							def       __A (  self:   Optional[int]   )    ->				int:
												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       __A (  self:   int		,     __A:   Optional[int]		,     __A:   str		,     __A:   int		,     __A:   Optional[int]		,     __A:   Dict		,     __A:   Optional[Any]		,     __A:   Optional[Any]   )    ->				Dict:
												_A								= AlbertModel(config=__A   )
												model.to(__A   )
												model.eval()
												_A								= model(__A		,     attention_mask=__A		,     token_type_ids=__A   )
												_A								= model(__A		,     token_type_ids=__A   )
												_A								= model(__A   )
												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:   Optional[Any]		,     __A:   str		,     __A:   Union[str, Any]		,     __A:   Union[str, Any]		,     __A:   str		,     __A:   Union[str, Any]		,     __A:   str		,     __A:   Dict   )    ->				str:
												_A								= AlbertForPreTraining(config=__A   )
												model.to(__A   )
												model.eval()
												_A								= model(
												    __A		,     attention_mask=__A		,     token_type_ids=__A		,     labels=__A		,     sentence_order_label=__A		,     )
												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       __A (  self:   Optional[Any]		,     __A:   Dict		,     __A:   List[Any]		,     __A:   List[str]		,     __A:   Dict		,     __A:   int		,     __A:   Optional[int]		,     __A:   Dict   )    ->				Union[str, Any]:
												_A								= AlbertForMaskedLM(config=__A   )
												model.to(__A   )
												model.eval()
												_A								= model(__A		,     attention_mask=__A		,     token_type_ids=__A		,     labels=__A   )
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)   )

							def       __A (  self:   str		,     __A:   int		,     __A:   Any		,     __A:   Optional[int]		,     __A:   Union[str, Any]		,     __A:   Dict		,     __A:   int		,     __A:   int   )    ->				Dict:
												_A								= AlbertForQuestionAnswering(config=__A   )
												model.to(__A   )
												model.eval()
												_A								= model(
												    __A		,     attention_mask=__A		,     token_type_ids=__A		,     start_positions=__A		,     end_positions=__A		,     )
												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:   Any		,     __A:   Any		,     __A:   int		,     __A:   Optional[Any]		,     __A:   List[str]		,     __A:   Optional[Any]		,     __A:   Optional[int]		,     __A:   List[Any]   )    ->				Union[str, Any]:
												_A								= self.num_labels
												_A								= AlbertForSequenceClassification(__A   )
												model.to(__A   )
												model.eval()
												_A								= model(__A		,     attention_mask=__A		,     token_type_ids=__A		,     labels=__A   )
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)   )

							def       __A (  self:   List[Any]		,     __A:   List[Any]		,     __A:   int		,     __A:   Union[str, Any]		,     __A:   List[str]		,     __A:   Tuple		,     __A:   List[str]		,     __A:   List[Any]   )    ->				Union[str, Any]:
												_A								= self.num_labels
												_A								= AlbertForTokenClassification(config=__A   )
												model.to(__A   )
												model.eval()
												_A								= model(__A		,     attention_mask=__A		,     token_type_ids=__A		,     labels=__A   )
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)   )

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



							def       __A (  self:   List[str]   )    ->				str:
												_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_torch
class 						SCREAMING_SNAKE_CASE				( snake_case  ,	snake_case  ,	unittest.TestCase					):





							"""simple docstring"""

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

							def       __A (  self:   str		,     __A:   Any		,     __A:   int		,     __A:   int=False   )    ->				int:
												_A								= super()._prepare_for_class(__A		,     __A		,     return_labels=__A   )

												if return_labels:
																	if model_class in get_values(__A   ):
																						_A								= torch.zeros(
																						    (self.model_tester.batch_size, self.model_tester.seq_length)		,     dtype=torch.long		,     device=__A   )
																						_A								= torch.zeros(
																						    self.model_tester.batch_size		,     dtype=torch.long		,     device=__A   )
												return inputs_dict

							def       __A (  self:   Any   )    ->				List[Any]:
												_A								= AlbertModelTester(self   )
												_A								= ConfigTester(self		,     config_class=__A		,     hidden_size=37   )

							def       __A (  self:   Tuple   )    ->				int:
												self.config_tester.run_common_tests()

							def       __A (  self:   List[Any]   )    ->				Any:
												_A								= self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__A   )

							def       __A (  self:   Union[str, Any]   )    ->				Any:
												_A								= self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_pretraining(*__A   )

							def       __A (  self:   Any   )    ->				Tuple:
												_A								= self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_lm(*__A   )

							def       __A (  self:   Dict   )    ->				Any:
												_A								= self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*__A   )

							def       __A (  self:   Optional[Any]   )    ->				Dict:
												_A								= self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*__A   )

							def       __A (  self:   List[Any]   )    ->				Tuple:
												_A								= self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*__A   )

							def       __A (  self:   Union[str, Any]   )    ->				str:
												_A								= self.model_tester.prepare_config_and_inputs()
												for type in ["absolute", "relative_key", "relative_key_query"]:
																	_A								= type
																	self.model_tester.create_and_check_model(*__A   )



							@slow
							def       __A (  self:   Optional[int]   )    ->				Union[str, Any]:
												for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_A								= AlbertModel.from_pretrained(__A   )
																	self.assertIsNotNone(__A   )





@require_torch
class 						SCREAMING_SNAKE_CASE				( unittest.TestCase					):





							"""simple docstring"""

							@slow
							def       __A (  self:   Union[str, Any]   )    ->				Optional[Any]:
												_A								= AlbertModel.from_pretrained('''albert-base-v2'''   )
												_A								= torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]   )
												_A								= torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
												with torch.no_grad():
																	_A								= model(__A		,     attention_mask=__A   )[0]
												_A								= torch.Size((1, 11, 7_68)   )
												self.assertEqual(output.shape		,     __A   )
												_A								= 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]		,     __A		,     atol=1e-4   )   )











def      __A					(     _lowercase					,			_lowercase					,			_lowercase					,			_lowercase ):

					'''simple docstring'''







					_A							,_A								= len(_lowercase ), len(grid[0] )
					if (
					    min(_lowercase					,			_lowercase ) < 0
					    or row == row_length
					    or col == col_length
					    or (row, col) in visit
					    or grid[row][col] == 1
					):
										return 0
					if row == row_length - 1 and col == col_length - 1:
										return 1

					visit.add((row, col) )

					_A								= 0
					count += depth_first_search(_lowercase					,			row + 1					,			_lowercase					,			_lowercase )
					count += depth_first_search(_lowercase					,			row - 1					,			_lowercase					,			_lowercase )
					count += depth_first_search(_lowercase					,			_lowercase					,			col + 1					,			_lowercase )
					count += depth_first_search(_lowercase					,			_lowercase					,			col - 1					,			_lowercase )

					visit.remove((row, col) )
					return count


if __name__ == "__main__":
				import doctest

				doctest.testmod()



