

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







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







            """simple docstring"""







            __lowercase      		=parent
            __lowercase      		=batch_size
            __lowercase      		=seq_length
            __lowercase      		=is_training
            __lowercase      		=use_input_mask
            __lowercase      		=use_token_type_ids
            __lowercase      		=use_labels
            __lowercase      		=vocab_size
            __lowercase      		=embedding_size
            __lowercase      		=hidden_size
            __lowercase      		=num_hidden_layers
            __lowercase      		=num_hidden_groups
            __lowercase      		=num_attention_heads
            __lowercase      		=intermediate_size
            __lowercase      		=hidden_act
            __lowercase      		=hidden_dropout_prob
            __lowercase      		=attention_probs_dropout_prob
            __lowercase      		=max_position_embeddings
            __lowercase      		=type_vocab_size
            __lowercase      		=type_sequence_label_size
            __lowercase      		=initializer_range
            __lowercase      		=num_labels
            __lowercase      		=num_choices
            __lowercase      		=scope







       def       snake_case (				self      :			int   ):







            """simple docstring"""







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

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

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

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

            __lowercase      		=self.get_config()

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







       def       snake_case (				self      :			Tuple   ):







            """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       snake_case (				self      :			List[str]    ,   __lowercase      :			int    ,   __lowercase      :			Dict    ,   __lowercase      :			Any    ,   __lowercase      :			Optional[int]    ,   __lowercase      :			Optional[int]    ,   __lowercase      :			Tuple    ,   __lowercase      :			List[Any]   ):







            """simple docstring"""







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







            """simple docstring"""







            __lowercase      		=AlbertForPreTraining(config=UpperCamelCase__   )
            model.to(UpperCamelCase__   )
            model.eval()
            __lowercase      		=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       snake_case (				self      :			Optional[int]    ,   __lowercase      :			Optional[Any]    ,   __lowercase      :			Dict    ,   __lowercase      :			Union[str, Any]    ,   __lowercase      :			Union[str, Any]    ,   __lowercase      :			Union[str, Any]    ,   __lowercase      :			Union[str, Any]    ,   __lowercase      :			Any   ):







            """simple docstring"""







            __lowercase      		=AlbertForMaskedLM(config=UpperCamelCase__   )
            model.to(UpperCamelCase__   )
            model.eval()
            __lowercase      		=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       snake_case (				self      :			Any    ,   __lowercase      :			List[str]    ,   __lowercase      :			Dict    ,   __lowercase      :			List[str]    ,   __lowercase      :			Tuple    ,   __lowercase      :			Optional[int]    ,   __lowercase      :			Tuple    ,   __lowercase      :			str   ):







            """simple docstring"""







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







            """simple docstring"""







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







       def       snake_case (				self      :			str    ,   __lowercase      :			Dict    ,   __lowercase      :			List[Any]    ,   __lowercase      :			Optional[int]    ,   __lowercase      :			Optional[Any]    ,   __lowercase      :			str    ,   __lowercase      :			int    ,   __lowercase      :			List[Any]   ):







            """simple docstring"""







            __lowercase      		=self.num_labels
            __lowercase      		=AlbertForTokenClassification(config=UpperCamelCase__   )
            model.to(UpperCamelCase__   )
            model.eval()
            __lowercase      		=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       snake_case (				self      :			Union[str, Any]    ,   __lowercase      :			Union[str, Any]    ,   __lowercase      :			Any    ,   __lowercase      :			Optional[Any]    ,   __lowercase      :			str    ,   __lowercase      :			Dict    ,   __lowercase      :			List[str]    ,   __lowercase      :			Optional[Any]   ):







            """simple docstring"""







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







       def       snake_case (				self      :			Tuple   ):







            """simple docstring"""







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






@require_torch
class 	lowerCAmelCase  (   snake_case_ ,      snake_case_ ,      unittest.TestCase ):
       lowerCAmelCase_												=			(
           (
               AlbertModel,
               AlbertForPreTraining,
               AlbertForMaskedLM,
               AlbertForMultipleChoice,
               AlbertForSequenceClassification,
               AlbertForTokenClassification,
               AlbertForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       lowerCAmelCase_												=			(
           {
               '''feature-extraction''': AlbertModel,
               '''fill-mask''': AlbertForMaskedLM,
               '''question-answering''': AlbertForQuestionAnswering,
               '''text-classification''': AlbertForSequenceClassification,
               '''token-classification''': AlbertForTokenClassification,
               '''zero-shot''': AlbertForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       lowerCAmelCase_												=			True







       def       snake_case (				self      :			Union[str, Any]    ,   __lowercase      :			Dict    ,   __lowercase      :			List[Any]    ,   __lowercase      :			List[Any]=False   ):







            """simple docstring"""







            __lowercase      		=super()._prepare_for_class(UpperCamelCase__    ,   UpperCamelCase__    ,   return_labels=UpperCamelCase__   )

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







       def       snake_case (				self      :			int   ):







            """simple docstring"""







            __lowercase      		=AlbertModelTester(self   )
            __lowercase      		=ConfigTester(self    ,   config_class=UpperCamelCase__    ,   hidden_size=37   )







       def       snake_case (				self      :			List[str]   ):







            """simple docstring"""







            self.config_tester.run_common_tests()







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







            """simple docstring"""







            __lowercase      		=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*UpperCamelCase__   )







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







            """simple docstring"""







            __lowercase      		=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_pretraining(*UpperCamelCase__   )







       def       snake_case (				self      :			int   ):







            """simple docstring"""







            __lowercase      		=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__   )







       def       snake_case (				self      :			Dict   ):







            """simple docstring"""







            __lowercase      		=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__   )







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







            """simple docstring"""







            __lowercase      		=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__   )







       def       snake_case (				self      :			Optional[Any]   ):







            """simple docstring"""







            __lowercase      		=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__   )







       def       snake_case (				self      :			Optional[Any]   ):







            """simple docstring"""







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







       @slow
       def       snake_case (				self      :			List[str]   ):







            """simple docstring"""







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






@require_torch
class 	lowerCAmelCase  (   unittest.TestCase ):







       @slow
       def       snake_case (				self      :			Dict   ):







            """simple docstring"""







            __lowercase      		=AlbertModel.from_pretrained('albert-base-v2'   )
            __lowercase      		=torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]   )
            __lowercase      		=torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
            with torch.no_grad():
                 __lowercase      		=model(UpperCamelCase__    ,   attention_mask=UpperCamelCase__   )[0]
            __lowercase      		=torch.Size((1, 11, 768)   )
            self.assertEqual(output.shape    ,   UpperCamelCase__   )
            __lowercase      		=torch.tensor(
                [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]]   )

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






"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__snake_case       =       {
    """configuration_poolformer""": [
        """POOLFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """PoolFormerConfig""",
        """PoolFormerOnnxConfig""",
    ]
}

try:
  if not is_vision_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __snake_case       =       ["""PoolFormerFeatureExtractor"""]
  __snake_case       =       ["""PoolFormerImageProcessor"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __snake_case       =       [
      """POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """PoolFormerForImageClassification""",
      """PoolFormerModel""",
      """PoolFormerPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_poolformer import (
      POOLFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
      PoolFormerConfig,
      PoolFormerOnnxConfig,
  )

  try:
    if not is_vision_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .feature_extraction_poolformer import PoolFormerFeatureExtractor
    from .image_processing_poolformer import PoolFormerImageProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_poolformer import (
        POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
        PoolFormerForImageClassification,
        PoolFormerModel,
        PoolFormerPreTrainedModel,
    )


else:
  import sys

  __snake_case       =       _LazyModule(__name__, globals()["""__file__"""], _import_structure)





