




'''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 			_a  :
       def __init__(	self						:	str   ,				lowercase						:	Optional[int]   ,				lowercase						:	List[str]=13   ,				lowercase						:	List[str]=7   ,				lowercase						:	Dict=True   ,				lowercase						:	Optional[Any]=True   ,				lowercase						:	List[str]=True   ,				lowercase						:	Union[str, Any]=True   ,				lowercase						:	List[str]=99   ,				lowercase						:	Optional[int]=16   ,				lowercase						:	Optional[int]=36   ,				lowercase						:	Union[str, Any]=6   ,				lowercase						:	Dict=6   ,				lowercase						:	Dict=6   ,				lowercase						:	Any=37   ,				lowercase						:	Tuple="gelu"   ,				lowercase						:	Tuple=0.1   ,				lowercase						:	Tuple=0.1   ,				lowercase						:	Any=512   ,				lowercase						:	Dict=16   ,				lowercase						:	Union[str, Any]=2   ,				lowercase						:	List[str]=0.02   ,				lowercase						:	Any=3   ,				lowercase						:	Dict=4   ,				lowercase						:	Tuple=None   ,				):



           '''simple docstring'''

           UpperCAmelCase			       =		parent
           UpperCAmelCase			       =		batch_size
           UpperCAmelCase			       =		seq_length
           UpperCAmelCase			       =		is_training
           UpperCAmelCase			       =		use_input_mask
           UpperCAmelCase			       =		use_token_type_ids
           UpperCAmelCase			       =		use_labels
           UpperCAmelCase			       =		vocab_size
           UpperCAmelCase			       =		embedding_size
           UpperCAmelCase			       =		hidden_size
           UpperCAmelCase			       =		num_hidden_layers
           UpperCAmelCase			       =		num_hidden_groups
           UpperCAmelCase			       =		num_attention_heads
           UpperCAmelCase			       =		intermediate_size
           UpperCAmelCase			       =		hidden_act
           UpperCAmelCase			       =		hidden_dropout_prob
           UpperCAmelCase			       =		attention_probs_dropout_prob
           UpperCAmelCase			       =		max_position_embeddings
           UpperCAmelCase			       =		type_vocab_size
           UpperCAmelCase			       =		type_sequence_label_size
           UpperCAmelCase			       =		initializer_range
           UpperCAmelCase			       =		num_labels
           UpperCAmelCase			       =		num_choices
           UpperCAmelCase			       =		scope
       def     A      (	self						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size				)

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

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

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

           UpperCAmelCase			       =		self.get_config()

           return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
       def     A      (	self						:	List[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     A      (	self						:	List[str]   ,				lowercase						:	str   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Any   ,				lowercase						:	List[str]   ,				lowercase						:	List[str]   ,				lowercase						:	Optional[Any]   ,				lowercase						:	str				):



           '''simple docstring'''

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



           '''simple docstring'''

           UpperCAmelCase			       =		AlbertForPreTraining(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		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     A      (	self						:	str   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Dict   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	List[Any]   ,				lowercase						:	int   ,				lowercase						:	Dict   ,				lowercase						:	Tuple				):



           '''simple docstring'''

           UpperCAmelCase			       =		AlbertForMaskedLM(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		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     A      (	self						:	Tuple   ,				lowercase						:	Any   ,				lowercase						:	List[Any]   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Tuple   ,				lowercase						:	int   ,				lowercase						:	List[str]   ,				lowercase						:	Tuple				):



           '''simple docstring'''

           UpperCAmelCase			       =		AlbertForQuestionAnswering(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		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     A      (	self						:	str   ,				lowercase						:	Optional[Any]   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Dict   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	int   ,				lowercase						:	List[Any]   ,				lowercase						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.num_labels
           UpperCAmelCase			       =		AlbertForSequenceClassification(lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		model(lowercase   ,				attention_mask=lowercase   ,				token_type_ids=lowercase   ,				labels=lowercase				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_labels)				)
       def     A      (	self						:	str   ,				lowercase						:	List[Any]   ,				lowercase						:	str   ,				lowercase						:	int   ,				lowercase						:	List[Any]   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Dict   ,				lowercase						:	Union[str, Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.num_labels
           UpperCAmelCase			       =		AlbertForTokenClassification(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		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     A      (	self						:	Optional[Any]   ,				lowercase						:	Optional[Any]   ,				lowercase						:	Optional[int]   ,				lowercase						:	List[Any]   ,				lowercase						:	List[str]   ,				lowercase						:	List[Any]   ,				lowercase						:	List[Any]   ,				lowercase						:	Optional[Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.num_choices
           UpperCAmelCase			       =		AlbertForMultipleChoice(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		input_ids.unsqueeze(1				).expand(-1   ,				self.num_choices   ,				-1				).contiguous()
           UpperCAmelCase			       =		token_type_ids.unsqueeze(1				).expand(-1   ,				self.num_choices   ,				-1				).contiguous()
           UpperCAmelCase			       =		input_mask.unsqueeze(1				).expand(-1   ,				self.num_choices   ,				-1				).contiguous()
           UpperCAmelCase			       =		model(
               lowercase   ,				attention_mask=lowercase   ,				token_type_ids=lowercase   ,				labels=lowercase   ,				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_choices)				)







       def     A      (	self						:	Union[str, Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.prepare_config_and_inputs()
           (
               (
               UpperCAmelCase
           )       ,     (
               UpperCAmelCase
           )       ,     (
               UpperCAmelCase
           )       ,     (
               UpperCAmelCase
           )       ,     (
               UpperCAmelCase
           )       ,     (
               UpperCAmelCase
           )       ,     (
               UpperCAmelCase
           )       ,     
           )			       =		config_and_inputs
           UpperCAmelCase			       =		{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
           return config, inputs_dict






@require_torch
class 			_a  (	lowercase_							,							lowercase_							,							unittest.TestCase ):
       __a				:     Union[str, Any]   				=					(
           (
               AlbertModel,
               AlbertForPreTraining,
               AlbertForMaskedLM,
               AlbertForMultipleChoice,
               AlbertForSequenceClassification,
               AlbertForTokenClassification,
               AlbertForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       __a				:     Dict   				=					(
           {
               """feature-extraction""": AlbertModel,
               """fill-mask""": AlbertForMaskedLM,
               """question-answering""": AlbertForQuestionAnswering,
               """text-classification""": AlbertForSequenceClassification,
               """token-classification""": AlbertForTokenClassification,
               """zero-shot""": AlbertForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       __a				:     int   				=					True
       def     A      (	self						:	Union[str, Any]   ,				lowercase						:	Dict   ,				lowercase						:	Any   ,				lowercase						:	Any=False				):



           '''simple docstring'''

           UpperCAmelCase			       =		super()._prepare_for_class(lowercase   ,				lowercase   ,				return_labels=lowercase				)

           if return_labels:
               if model_class in get_values(lowercase				):
                   UpperCAmelCase			       =		torch.zeros(
                       (self.model_tester.batch_size, self.model_tester.seq_length)   ,				dtype=torch.long   ,				device=lowercase				)
                   UpperCAmelCase			       =		torch.zeros(
                       self.model_tester.batch_size   ,				dtype=torch.long   ,				device=lowercase				)
           return inputs_dict
       def     A      (	self						:	int				):



           '''simple docstring'''

           UpperCAmelCase			       =		AlbertModelTester(self				)
           UpperCAmelCase			       =		ConfigTester(self   ,				config_class=lowercase   ,				hidden_size=37				)
       def     A      (	self						:	List[Any]				):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       def     A      (	self						:	Tuple				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase				)
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_pretraining(*lowercase				)
       def     A      (	self						:	Union[str, Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*lowercase				)
       def     A      (	self						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*lowercase				)
       def     A      (	self						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*lowercase				)
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*lowercase				)
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
               UpperCAmelCase			       =		type
               self.model_tester.create_and_check_model(*lowercase				)







       @slow
       def     A      (	self						:	Tuple				):



           '''simple docstring'''

           for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase			       =		AlbertModel.from_pretrained(lowercase				)
               self.assertIsNotNone(lowercase				)






@require_torch
class 			_a  (	unittest.TestCase ):
       @slow
       def     A      (	self						:	List[Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		AlbertModel.from_pretrained('''albert-base-v2'''				)
           UpperCAmelCase			       =		torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]				)
           UpperCAmelCase			       =		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
           with torch.no_grad():
               UpperCAmelCase			       =		model(lowercase   ,				attention_mask=lowercase				)[0]
           UpperCAmelCase			       =		torch.Size((1, 11, 768)				)
           self.assertEqual(output.shape   ,				lowercase				)
           UpperCAmelCase			       =		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				)				)








'''simple docstring'''

class   __lowerCAmelCase						:  # Public class to implement a graph





      '''simple docstring'''






      def __init__(self    :    int   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    list[list[bool]] ):

             '''simple docstring'''







             lowercase__      			= row
             lowercase__      			= col
             lowercase__      			= graph



      def       UpperCamelCase__		(self    :    Optional[int]   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    list[list[bool]] ):

             '''simple docstring'''







             return (
                 0 <= i < self.ROW
                 and 0 <= j < self.COL
                 and not visited[i][j]
                 and self.graph[i][j]
             )



      def       UpperCamelCase__		(self    :    int   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    list[list[bool]] ):

             '''simple docstring'''







             lowercase__      			= [-1, -1, -1, 0, 0, 1, 1, 1]  # Coordinate order
             lowercase__      			= [-1, 0, 1, -1, 1, -1, 0, 1]
             lowercase__      			= True  # Make those cells visited
             for k in range(8 ):
                    if self.is_safe(i + row_nbr[k]   ,						j + col_nbr[k]   ,						UpperCamelCase ):
                           self.diffs(i + row_nbr[k]   ,						j + col_nbr[k]   ,						UpperCamelCase )






      def       UpperCamelCase__		(self    :    Dict ):  # And finally, count all islands.

             '''simple docstring'''







             lowercase__      			= [[False for j in range(self.COL )] for i in range(self.ROW )]
             lowercase__      			= 0
             for i in range(self.ROW ):
                    for j in range(self.COL ):
                           if visited[i][j] is False and self.graph[i][j] == 1:
                                  self.diffs(UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase )
                                  count += 1
             return count




