





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

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




              """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 lowercase (		self: Union[str, Any]		)       -> str:




              """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 lowercase (		self: List[Any]		)       -> 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 lowercase (		self: Any					,   _SCREAMING_SNAKE_CASE: Optional[int]					,   _SCREAMING_SNAKE_CASE: Union[str, Any]					,   _SCREAMING_SNAKE_CASE: List[str]					,   _SCREAMING_SNAKE_CASE: Dict					,   _SCREAMING_SNAKE_CASE: Union[str, Any]					,   _SCREAMING_SNAKE_CASE: List[Any]					,   _SCREAMING_SNAKE_CASE: Dict		)       -> str:




              """simple docstring"""


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




              """simple docstring"""


              UpperCamelCase_          =  AlbertForPreTraining(config=_SCREAMING_SNAKE_CASE		)
              model.to(_SCREAMING_SNAKE_CASE		)
              model.eval()
              UpperCamelCase_          =  model(
                  _SCREAMING_SNAKE_CASE					,   attention_mask=_SCREAMING_SNAKE_CASE					,   token_type_ids=_SCREAMING_SNAKE_CASE					,   labels=_SCREAMING_SNAKE_CASE					,   sentence_order_label=_SCREAMING_SNAKE_CASE					,   )
              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 lowercase (		self: Tuple					,   _SCREAMING_SNAKE_CASE: Dict					,   _SCREAMING_SNAKE_CASE: Union[str, Any]					,   _SCREAMING_SNAKE_CASE: List[Any]					,   _SCREAMING_SNAKE_CASE: Optional[Any]					,   _SCREAMING_SNAKE_CASE: Dict					,   _SCREAMING_SNAKE_CASE: Union[str, Any]					,   _SCREAMING_SNAKE_CASE: Dict		)       -> Union[str, Any]:




              """simple docstring"""


              UpperCamelCase_          =  AlbertForMaskedLM(config=_SCREAMING_SNAKE_CASE		)
              model.to(_SCREAMING_SNAKE_CASE		)
              model.eval()
              UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE					,   attention_mask=_SCREAMING_SNAKE_CASE					,   token_type_ids=_SCREAMING_SNAKE_CASE					,   labels=_SCREAMING_SNAKE_CASE		)
              self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.vocab_size)		)

       def lowercase (		self: int					,   _SCREAMING_SNAKE_CASE: Optional[Any]					,   _SCREAMING_SNAKE_CASE: List[str]					,   _SCREAMING_SNAKE_CASE: Any					,   _SCREAMING_SNAKE_CASE: List[str]					,   _SCREAMING_SNAKE_CASE: Union[str, Any]					,   _SCREAMING_SNAKE_CASE: List[Any]					,   _SCREAMING_SNAKE_CASE: List[Any]		)       -> Optional[Any]:




              """simple docstring"""


              UpperCamelCase_          =  AlbertForQuestionAnswering(config=_SCREAMING_SNAKE_CASE		)
              model.to(_SCREAMING_SNAKE_CASE		)
              model.eval()
              UpperCamelCase_          =  model(
                  _SCREAMING_SNAKE_CASE					,   attention_mask=_SCREAMING_SNAKE_CASE					,   token_type_ids=_SCREAMING_SNAKE_CASE					,   start_positions=_SCREAMING_SNAKE_CASE					,   end_positions=_SCREAMING_SNAKE_CASE					,   )
              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 lowercase (		self: List[str]					,   _SCREAMING_SNAKE_CASE: Union[str, Any]					,   _SCREAMING_SNAKE_CASE: str					,   _SCREAMING_SNAKE_CASE: Any					,   _SCREAMING_SNAKE_CASE: List[str]					,   _SCREAMING_SNAKE_CASE: List[str]					,   _SCREAMING_SNAKE_CASE: List[str]					,   _SCREAMING_SNAKE_CASE: Any		)       -> Tuple:




              """simple docstring"""


              UpperCamelCase_          =  self.num_labels
              UpperCamelCase_          =  AlbertForSequenceClassification(_SCREAMING_SNAKE_CASE		)
              model.to(_SCREAMING_SNAKE_CASE		)
              model.eval()
              UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE					,   attention_mask=_SCREAMING_SNAKE_CASE					,   token_type_ids=_SCREAMING_SNAKE_CASE					,   labels=_SCREAMING_SNAKE_CASE		)
              self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.num_labels)		)

       def lowercase (		self: int					,   _SCREAMING_SNAKE_CASE: Optional[Any]					,   _SCREAMING_SNAKE_CASE: int					,   _SCREAMING_SNAKE_CASE: List[Any]					,   _SCREAMING_SNAKE_CASE: Union[str, Any]					,   _SCREAMING_SNAKE_CASE: Optional[Any]					,   _SCREAMING_SNAKE_CASE: int					,   _SCREAMING_SNAKE_CASE: Tuple		)       -> Dict:




              """simple docstring"""


              UpperCamelCase_          =  self.num_labels
              UpperCamelCase_          =  AlbertForTokenClassification(config=_SCREAMING_SNAKE_CASE		)
              model.to(_SCREAMING_SNAKE_CASE		)
              model.eval()
              UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE					,   attention_mask=_SCREAMING_SNAKE_CASE					,   token_type_ids=_SCREAMING_SNAKE_CASE					,   labels=_SCREAMING_SNAKE_CASE		)
              self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.num_labels)		)

       def lowercase (		self: Optional[int]					,   _SCREAMING_SNAKE_CASE: Tuple					,   _SCREAMING_SNAKE_CASE: Tuple					,   _SCREAMING_SNAKE_CASE: Optional[Any]					,   _SCREAMING_SNAKE_CASE: Union[str, Any]					,   _SCREAMING_SNAKE_CASE: Optional[Any]					,   _SCREAMING_SNAKE_CASE: Dict					,   _SCREAMING_SNAKE_CASE: Optional[Any]		)       -> Any:




              """simple docstring"""


              UpperCamelCase_          =  self.num_choices
              UpperCamelCase_          =  AlbertForMultipleChoice(config=_SCREAMING_SNAKE_CASE		)
              model.to(_SCREAMING_SNAKE_CASE		)
              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(
                  _SCREAMING_SNAKE_CASE					,   attention_mask=_SCREAMING_SNAKE_CASE					,   token_type_ids=_SCREAMING_SNAKE_CASE					,   labels=_SCREAMING_SNAKE_CASE					,   )
              self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.num_choices)		)



       def lowercase (		self: Optional[Any]		)       -> str:




              """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 							_UpperCamelCase					(	lowerCAmelCase_  ,						lowerCAmelCase_  ,						unittest.TestCase     ):
       _UpperCamelCase				:					str         =   (
           (
               AlbertModel,
               AlbertForPreTraining,
               AlbertForMaskedLM,
               AlbertForMultipleChoice,
               AlbertForSequenceClassification,
               AlbertForTokenClassification,
               AlbertForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       _UpperCamelCase				:					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				:					Optional[int]         =   True

       def lowercase (		self: str					,   _SCREAMING_SNAKE_CASE: Tuple					,   _SCREAMING_SNAKE_CASE: Any					,   _SCREAMING_SNAKE_CASE: Dict=False		)       -> Optional[int]:




              """simple docstring"""


              UpperCamelCase_          =  super()._prepare_for_class(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   return_labels=_SCREAMING_SNAKE_CASE		)

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

       def lowercase (		self: Dict		)       -> Any:




              """simple docstring"""


              UpperCamelCase_          =  AlbertModelTester(self		)
              UpperCamelCase_          =  ConfigTester(self					,   config_class=_SCREAMING_SNAKE_CASE					,   hidden_size=37		)

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




              """simple docstring"""


              self.config_tester.run_common_tests()

       def lowercase (		self: List[str]		)       -> Dict:




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)

       def lowercase (		self: Dict		)       -> Tuple:




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE		)

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




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE		)

       def lowercase (		self: Any		)       -> Tuple:




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE		)

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




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE		)

       def lowercase (		self: Dict		)       -> Optional[int]:




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE		)

       def lowercase (		self: str		)       -> Union[str, 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(*_SCREAMING_SNAKE_CASE		)



       @slow
       def lowercase (		self: str		)       -> Optional[int]:




              """simple docstring"""


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



@require_torch
class 							_UpperCamelCase					(	unittest.TestCase     ):

       @slow
       def lowercase (		self: Any		)       -> Any:




              """simple docstring"""


              UpperCamelCase_          =  AlbertModel.from_pretrained("albert-base-v2"		)
              UpperCamelCase_          =  torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]		)
              UpperCamelCase_          =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
              with torch.no_grad():
                     UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE					,   attention_mask=_SCREAMING_SNAKE_CASE		)[0]
              UpperCamelCase_          =  torch.Size((1, 11, 768)		)
              self.assertEqual(output.shape					,   _SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  torch.tensor(
                  [[[-0.65_13, 1.50_35, -0.27_66], [-0.65_15, 1.50_46, -0.27_80], [-0.65_12, 1.50_49, -0.27_84]]]		)

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








_UpperCAmelCase        =				{
    'Pillow': 'Pillow',
    'accelerate': 'accelerate>=0.11.0',
    'compel': 'compel==0.1.8',
    'black': 'black~=23.1',
    'datasets': 'datasets',
    'filelock': 'filelock',
    'flax': 'flax>=0.4.1',
    'hf-doc-builder': 'hf-doc-builder>=0.3.0',
    'huggingface-hub': 'huggingface-hub>=0.13.2',
    'requests-mock': 'requests-mock==1.10.0',
    'importlib_metadata': 'importlib_metadata',
    'invisible-watermark': 'invisible-watermark',
    'isort': 'isort>=5.5.4',
    'jax': 'jax>=0.2.8,!=0.3.2',
    'jaxlib': 'jaxlib>=0.1.65',
    'Jinja2': 'Jinja2',
    'k-diffusion': 'k-diffusion>=0.0.12',
    'torchsde': 'torchsde',
    'note_seq': 'note_seq',
    'librosa': 'librosa',
    'numpy': 'numpy',
    'omegaconf': 'omegaconf',
    'parameterized': 'parameterized',
    'protobuf': 'protobuf>=3.20.3,<4',
    'pytest': 'pytest',
    'pytest-timeout': 'pytest-timeout',
    'pytest-xdist': 'pytest-xdist',
    'ruff': 'ruff>=0.0.241',
    'safetensors': 'safetensors',
    'sentencepiece': 'sentencepiece>=0.1.91,!=0.1.92',
    'scipy': 'scipy',
    'onnx': 'onnx',
    'regex': 'regex!=2019.12.17',
    'requests': 'requests',
    'tensorboard': 'tensorboard',
    'torch': 'torch>=1.4',
    'torchvision': 'torchvision',
    'transformers': 'transformers>=4.25.1',
    'urllib3': 'urllib3<=2.0.0',
}

