






'''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			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE=13			,		_SCREAMING_SNAKE_CASE=7			,		_SCREAMING_SNAKE_CASE=True			,		_SCREAMING_SNAKE_CASE=True			,		_SCREAMING_SNAKE_CASE=True			,		_SCREAMING_SNAKE_CASE=True			,		_SCREAMING_SNAKE_CASE=99			,		_SCREAMING_SNAKE_CASE=16			,		_SCREAMING_SNAKE_CASE=36			,		_SCREAMING_SNAKE_CASE=6			,		_SCREAMING_SNAKE_CASE=6			,		_SCREAMING_SNAKE_CASE=6			,		_SCREAMING_SNAKE_CASE=37			,		_SCREAMING_SNAKE_CASE="gelu"			,		_SCREAMING_SNAKE_CASE=0.1			,		_SCREAMING_SNAKE_CASE=0.1			,		_SCREAMING_SNAKE_CASE=512			,		_SCREAMING_SNAKE_CASE=16			,		_SCREAMING_SNAKE_CASE=2			,		_SCREAMING_SNAKE_CASE=0.0_2			,		_SCREAMING_SNAKE_CASE=3			,		_SCREAMING_SNAKE_CASE=4			,		_SCREAMING_SNAKE_CASE=None			,		)  ->      Optional[int]:



        """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      )  ->      Tuple:



        """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      )  ->      str:



        """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			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE      )  ->      List[str]:



        """simple docstring"""






        UpperCamelCase				  =					AlbertModel(config=lowerCAmelCase__      )
        model.to(lowerCAmelCase__      )
        model.eval()
        UpperCamelCase				  =					model(lowerCAmelCase__			,		attention_mask=lowerCAmelCase__			,		token_type_ids=lowerCAmelCase__      )
        UpperCamelCase				  =					model(lowerCAmelCase__			,		token_type_ids=lowerCAmelCase__      )
        UpperCamelCase				  =					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   A__						(      self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE      )  ->      List[Any]:



        """simple docstring"""






        UpperCamelCase				  =					AlbertForPreTraining(config=lowerCAmelCase__      )
        model.to(lowerCAmelCase__      )
        model.eval()
        UpperCamelCase				  =					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   A__						(      self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE      )  ->      str:



        """simple docstring"""






        UpperCamelCase				  =					AlbertForMaskedLM(config=lowerCAmelCase__      )
        model.to(lowerCAmelCase__      )
        model.eval()
        UpperCamelCase				  =					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   A__						(      self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE      )  ->      Union[str, Any]:



        """simple docstring"""






        UpperCamelCase				  =					AlbertForQuestionAnswering(config=lowerCAmelCase__      )
        model.to(lowerCAmelCase__      )
        model.eval()
        UpperCamelCase				  =					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   A__						(      self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE      )  ->      List[Any]:



        """simple docstring"""






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

  def   A__						(      self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE      )  ->      Dict:



        """simple docstring"""






        UpperCamelCase				  =					self.num_labels
        UpperCamelCase				  =					AlbertForTokenClassification(config=lowerCAmelCase__      )
        model.to(lowerCAmelCase__      )
        model.eval()
        UpperCamelCase				  =					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   A__						(      self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE      )  ->      Union[str, Any]:



        """simple docstring"""






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


  def   A__						(      self      )  ->      List[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 				a_						(			__a	,       __a	,       unittest.TestCase				):
  lowercase     		=      (
      (
          AlbertModel,
          AlbertForPreTraining,
          AlbertForMaskedLM,
          AlbertForMultipleChoice,
          AlbertForSequenceClassification,
          AlbertForTokenClassification,
          AlbertForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  lowercase     		=      (
      {
          '''feature-extraction''': AlbertModel,
          '''fill-mask''': AlbertForMaskedLM,
          '''question-answering''': AlbertForQuestionAnswering,
          '''text-classification''': AlbertForSequenceClassification,
          '''token-classification''': AlbertForTokenClassification,
          '''zero-shot''': AlbertForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  lowercase     		=      True

  def   A__						(      self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE=False      )  ->      List[Any]:



        """simple docstring"""






        UpperCamelCase				  =					super()._prepare_for_class(lowerCAmelCase__			,		lowerCAmelCase__			,		return_labels=lowerCAmelCase__      )

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

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



        """simple docstring"""






        UpperCamelCase				  =					AlbertModelTester(self      )
        UpperCamelCase				  =					ConfigTester(self			,		config_class=lowerCAmelCase__			,		hidden_size=37      )

  def   A__						(      self      )  ->      List[str]:



        """simple docstring"""






        self.config_tester.run_common_tests()

  def   A__						(      self      )  ->      List[Any]:



        """simple docstring"""






        UpperCamelCase				  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase__      )

  def   A__						(      self      )  ->      Tuple:



        """simple docstring"""






        UpperCamelCase				  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase__      )

  def   A__						(      self      )  ->      int:



        """simple docstring"""






        UpperCamelCase				  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__      )

  def   A__						(      self      )  ->      List[Any]:



        """simple docstring"""






        UpperCamelCase				  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase__      )

  def   A__						(      self      )  ->      Tuple:



        """simple docstring"""






        UpperCamelCase				  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__      )

  def   A__						(      self      )  ->      Dict:



        """simple docstring"""






        UpperCamelCase				  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__      )

  def   A__						(      self      )  ->      Tuple:



        """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(*lowerCAmelCase__      )


  @slow
  def   A__						(      self      )  ->      Optional[Any]:



        """simple docstring"""






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






@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, 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(lowerCAmelCase__			,		attention_mask=lowerCAmelCase__      )[0]
        UpperCamelCase				  =					torch.Size((1, 11, 768)      )
        self.assertEqual(output.shape			,		lowerCAmelCase__      )
        UpperCamelCase				  =					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]			,		lowerCAmelCase__			,		atol=1e-4      )      )


"""simple docstring"""





from ..utils import DummyObject, requires_backends
class 						SCREAMING_SNAKE_CASE_ (						metaclass=__a ):




		"""simple docstring"""





		__lowercase							:  Tuple						   =		['''keras_nlp''']




		def __init__(     self							,  *lowerCAmelCase__							,  **lowerCAmelCase__):
				requires_backends(self							,  ["""keras_nlp"""])




