

'''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_ :






     '''simple docstring'''



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


     def    UpperCAmelCase_       (		self :     Any       )						->	Dict:
      UpperCAmelCase       :	List[Any]											= ids_tensor([self.batch_size, self.seq_length]     ,			self.vocab_size       )

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

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

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

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

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


     def    UpperCAmelCase_       (		self :     List[Any]       )						->	Optional[Any]:
      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 :     Tuple     ,			lowercase_ :     Optional[Any]     ,			lowercase_ :     Optional[Any]     ,			lowercase_ :     List[str]     ,			lowercase_ :     str     ,			lowercase_ :     Union[str, Any]     ,			lowercase_ :     List[str]     ,			lowercase_ :     Optional[int]       )						->	Optional[int]:
      UpperCAmelCase       :	List[Any]											= AlbertModel(config=lowerCAmelCase_       )
      model.to(lowerCAmelCase_       )
      model.eval()
      UpperCAmelCase       :	List[str]											= model(lowerCAmelCase_     ,			attention_mask=lowerCAmelCase_     ,			token_type_ids=lowerCAmelCase_       )
      UpperCAmelCase       :	Optional[Any]											= model(lowerCAmelCase_     ,			token_type_ids=lowerCAmelCase_       )
      UpperCAmelCase       :	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    UpperCAmelCase_       (		self :     List[Any]     ,			lowercase_ :     Optional[int]     ,			lowercase_ :     Any     ,			lowercase_ :     List[Any]     ,			lowercase_ :     int     ,			lowercase_ :     Optional[Any]     ,			lowercase_ :     Union[str, Any]     ,			lowercase_ :     Tuple       )						->	Optional[Any]:
      UpperCAmelCase       :	Dict											= AlbertForPreTraining(config=lowerCAmelCase_       )
      model.to(lowerCAmelCase_       )
      model.eval()
      UpperCAmelCase       :	str											= model(
          lowerCAmelCase_     ,			attention_mask=lowerCAmelCase_     ,			token_type_ids=lowerCAmelCase_     ,			labels=lowerCAmelCase_     ,			sentence_order_label=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, config.num_labels)       )


     def    UpperCAmelCase_       (		self :     str     ,			lowercase_ :     str     ,			lowercase_ :     List[str]     ,			lowercase_ :     List[Any]     ,			lowercase_ :     str     ,			lowercase_ :     Dict     ,			lowercase_ :     Union[str, Any]     ,			lowercase_ :     int       )						->	Any:
      UpperCAmelCase       :	List[Any]											= AlbertForMaskedLM(config=lowerCAmelCase_       )
      model.to(lowerCAmelCase_       )
      model.eval()
      UpperCAmelCase       :	int											= model(lowerCAmelCase_     ,			attention_mask=lowerCAmelCase_     ,			token_type_ids=lowerCAmelCase_     ,			labels=lowerCAmelCase_       )
      self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.seq_length, self.vocab_size)       )


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


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


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




     def    UpperCAmelCase_       (		self :     Optional[int]       )						->	str:
      UpperCAmelCase       :	Union[str, Any]											= self.prepare_config_and_inputs()
      (
          (
          UpperCAmelCase	
      )					, (
          UpperCAmelCase	
      )					, (
          UpperCAmelCase	
      )					, (
          UpperCAmelCase	
      )					, (
          UpperCAmelCase	
      )					, (
          UpperCAmelCase	
      )					, (
          UpperCAmelCase	
      )					, 	
      )       :	Optional[int]											= config_and_inputs
      UpperCAmelCase       :	Dict											= {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict




@require_torch
class        A_ (				_UpperCamelCase       ,   _UpperCamelCase       ,   unittest.TestCase							):






     '''simple docstring'''



     UpperCAmelCase_						:	int  		=  (
         (
             AlbertModel,
             AlbertForPreTraining,
             AlbertForMaskedLM,
             AlbertForMultipleChoice,
             AlbertForSequenceClassification,
             AlbertForTokenClassification,
             AlbertForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     UpperCAmelCase_						:	List[Any]  		=  (
         {
             """feature-extraction""": AlbertModel,
             """fill-mask""": AlbertForMaskedLM,
             """question-answering""": AlbertForQuestionAnswering,
             """text-classification""": AlbertForSequenceClassification,
             """token-classification""": AlbertForTokenClassification,
             """zero-shot""": AlbertForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     UpperCAmelCase_						:	Dict  		=  True


     def    UpperCAmelCase_       (		self :     Optional[int]     ,			lowercase_ :     Optional[int]     ,			lowercase_ :     Optional[int]     ,			lowercase_ :     Dict=False       )						->	Dict:
      UpperCAmelCase       :	str											= super()._prepare_for_class(lowerCAmelCase_     ,			lowerCAmelCase_     ,			return_labels=lowerCAmelCase_       )

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


     def    UpperCAmelCase_       (		self :     Optional[int]       )						->	Any:
      UpperCAmelCase       :	List[Any]											= AlbertModelTester(self       )
      UpperCAmelCase       :	str											= ConfigTester(self     ,			config_class=lowerCAmelCase_     ,			hidden_size=37       )


     def    UpperCAmelCase_       (		self :     List[str]       )						->	Tuple:
      self.config_tester.run_common_tests()


     def    UpperCAmelCase_       (		self :     int       )						->	Tuple:
      UpperCAmelCase       :	Optional[Any]											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCAmelCase_       )


     def    UpperCAmelCase_       (		self :     int       )						->	str:
      UpperCAmelCase       :	Tuple											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase_       )


     def    UpperCAmelCase_       (		self :     Any       )						->	Optional[Any]:
      UpperCAmelCase       :	List[str]											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase_       )


     def    UpperCAmelCase_       (		self :     List[Any]       )						->	Optional[Any]:
      UpperCAmelCase       :	str											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase_       )


     def    UpperCAmelCase_       (		self :     Optional[int]       )						->	Union[str, Any]:
      UpperCAmelCase       :	Tuple											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase_       )


     def    UpperCAmelCase_       (		self :     Any       )						->	str:
      UpperCAmelCase       :	List[Any]											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase_       )


     def    UpperCAmelCase_       (		self :     Union[str, Any]       )						->	Optional[Any]:
      UpperCAmelCase       :	Tuple											= self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
       UpperCAmelCase       :	Any											= type
       self.model_tester.create_and_check_model(*lowerCAmelCase_       )




     @slow
     def    UpperCAmelCase_       (		self :     Dict       )						->	int:
      for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase       :	Tuple											= AlbertModel.from_pretrained(lowerCAmelCase_       )
       self.assertIsNotNone(lowerCAmelCase_       )




@require_torch
class        A_ (				unittest.TestCase							):






     '''simple docstring'''



     @slow
     def    UpperCAmelCase_       (		self :     Tuple       )						->	int:
      UpperCAmelCase       :	Optional[int]											= AlbertModel.from_pretrained('albert-base-v2'       )
      UpperCAmelCase       :	List[str]											= torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]       )
      UpperCAmelCase       :	Optional[int]											= torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
      with torch.no_grad():
       UpperCAmelCase       :	List[str]											= model(lowerCAmelCase_     ,			attention_mask=lowerCAmelCase_       )[0]
      UpperCAmelCase       :	Dict											= torch.Size((1, 11, 768)       )
      self.assertEqual(output.shape     ,			lowerCAmelCase_       )
      UpperCAmelCase       :	Union[str, 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]     ,			lowerCAmelCase_     ,			atol=1E-4       )       )











def   a_    ( lowerCAmelCase_      :			str,	lowerCAmelCase_      :			int	):
   return [sentence[i : i + ngram_size] for i in range(len(lowerCAmelCase_	) - ngram_size + 1	)]


if __name__ == "__main__":
     from doctest import testmod

     testmod()

