


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


       """simple docstring"""




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


             '''simple docstring'''



             lowerCamelCase__		:				List[str]    =  parent
             lowerCamelCase__		:				str    =  batch_size
             lowerCamelCase__		:				str    =  seq_length
             lowerCamelCase__		:				Union[str, Any]    =  is_training
             lowerCamelCase__		:				Dict    =  use_input_mask
             lowerCamelCase__		:				Tuple    =  use_token_type_ids
             lowerCamelCase__		:				Optional[Any]    =  use_labels
             lowerCamelCase__		:				Optional[int]    =  vocab_size
             lowerCamelCase__		:				Any    =  embedding_size
             lowerCamelCase__		:				int    =  hidden_size
             lowerCamelCase__		:				List[Any]    =  num_hidden_layers
             lowerCamelCase__		:				Tuple    =  num_hidden_groups
             lowerCamelCase__		:				Tuple    =  num_attention_heads
             lowerCamelCase__		:				Dict    =  intermediate_size
             lowerCamelCase__		:				List[Any]    =  hidden_act
             lowerCamelCase__		:				List[Any]    =  hidden_dropout_prob
             lowerCamelCase__		:				List[Any]    =  attention_probs_dropout_prob
             lowerCamelCase__		:				Union[str, Any]    =  max_position_embeddings
             lowerCamelCase__		:				Optional[int]    =  type_vocab_size
             lowerCamelCase__		:				Optional[int]    =  type_sequence_label_size
             lowerCamelCase__		:				int    =  initializer_range
             lowerCamelCase__		:				Union[str, Any]    =  num_labels
             lowerCamelCase__		:				Optional[int]    =  num_choices
             lowerCamelCase__		:				Union[str, Any]    =  scope





       def 						lowerCAmelCase     (   self							:			List[str]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Tuple    =  ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size       )

             lowerCamelCase__		:				Optional[Any]    =  None
             if self.use_input_mask:
                   lowerCamelCase__		:				Optional[int]    =  random_attention_mask([self.batch_size, self.seq_length]       )

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

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

             lowerCamelCase__		:				Dict    =  self.get_config()

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





       def 						lowerCAmelCase     (   self							:			List[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 						lowerCAmelCase     (   self							:			List[Any]					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			Union[str, Any]					,      __lowerCamelCase							:			Tuple					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			List[str]					,      __lowerCamelCase							:			Optional[Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Any    =  AlbertModel(config=__lowerCamelCase       )
             model.to(__lowerCamelCase       )
             model.eval()
             lowerCamelCase__		:				int    =  model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase       )
             lowerCamelCase__		:				Optional[Any]    =  model(__lowerCamelCase					,      token_type_ids=__lowerCamelCase       )
             lowerCamelCase__		:				List[Any]    =  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 						lowerCAmelCase     (   self							:			int					,      __lowerCamelCase							:			Dict					,      __lowerCamelCase							:			Tuple					,      __lowerCamelCase							:			Tuple					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			Optional[int]					,      __lowerCamelCase							:			Optional[int]					,      __lowerCamelCase							:			Optional[int]       ):


             '''simple docstring'''



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


             '''simple docstring'''



             lowerCamelCase__		:				Optional[int]    =  AlbertForMaskedLM(config=__lowerCamelCase       )
             model.to(__lowerCamelCase       )
             model.eval()
             lowerCamelCase__		:				Dict    =  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 						lowerCAmelCase     (   self							:			Any					,      __lowerCamelCase							:			List[Any]					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			Dict					,      __lowerCamelCase							:			Union[str, Any]					,      __lowerCamelCase							:			Optional[int]					,      __lowerCamelCase							:			Optional[int]					,      __lowerCamelCase							:			Union[str, Any]       ):


             '''simple docstring'''



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


             '''simple docstring'''



             lowerCamelCase__		:				Optional[Any]    =  self.num_labels
             lowerCamelCase__		:				str    =  AlbertForSequenceClassification(__lowerCamelCase       )
             model.to(__lowerCamelCase       )
             model.eval()
             lowerCamelCase__		:				List[str]    =  model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase       )
             self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)       )





       def 						lowerCAmelCase     (   self							:			Tuple					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			List[str]					,      __lowerCamelCase							:			List[str]					,      __lowerCamelCase							:			Union[str, Any]					,      __lowerCamelCase							:			List[Any]					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			Any       ):


             '''simple docstring'''



             lowerCamelCase__		:				List[str]    =  self.num_labels
             lowerCamelCase__		:				Any    =  AlbertForTokenClassification(config=__lowerCamelCase       )
             model.to(__lowerCamelCase       )
             model.eval()
             lowerCamelCase__		:				Any    =  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 						lowerCAmelCase     (   self							:			Any					,      __lowerCamelCase							:			Optional[int]					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			Dict					,      __lowerCamelCase							:			Optional[int]					,      __lowerCamelCase							:			List[Any]					,      __lowerCamelCase							:			Tuple					,      __lowerCamelCase							:			Optional[Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Optional[Any]    =  self.num_choices
             lowerCamelCase__		:				Tuple    =  AlbertForMultipleChoice(config=__lowerCamelCase       )
             model.to(__lowerCamelCase       )
             model.eval()
             lowerCamelCase__		:				List[str]    =  input_ids.unsqueeze(1       ).expand(-1					,      self.num_choices					,      -1       ).contiguous()
             lowerCamelCase__		:				Tuple    =  token_type_ids.unsqueeze(1       ).expand(-1					,      self.num_choices					,      -1       ).contiguous()
             lowerCamelCase__		:				str    =  input_mask.unsqueeze(1       ).expand(-1					,      self.num_choices					,      -1       ).contiguous()
             lowerCamelCase__		:				Tuple    =  model(
                 __lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase					,      )
             self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_choices)       )





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


             '''simple docstring'''



             lowerCamelCase__		:				Tuple    =  self.prepare_config_and_inputs()
             (
                 (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   
             )		:				Tuple    =  config_and_inputs
             lowerCamelCase__		:				Optional[int]    =  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
             return config, inputs_dict







@require_torch
class 			_lowercase (							lowercase__							,  lowercase__							,  unittest.TestCase):


       """simple docstring"""




       A__											=			(
           (
               AlbertModel,
               AlbertForPreTraining,
               AlbertForMaskedLM,
               AlbertForMultipleChoice,
               AlbertForSequenceClassification,
               AlbertForTokenClassification,
               AlbertForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       A__											=			(
           {
               "feature-extraction": AlbertModel,
               "fill-mask": AlbertForMaskedLM,
               "question-answering": AlbertForQuestionAnswering,
               "text-classification": AlbertForSequenceClassification,
               "token-classification": AlbertForTokenClassification,
               "zero-shot": AlbertForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       A__											=			True





       def 						lowerCAmelCase     (   self							:			Optional[int]					,      __lowerCamelCase							:			List[Any]					,      __lowerCamelCase							:			Dict					,      __lowerCamelCase							:			str=False       ):


             '''simple docstring'''



             lowerCamelCase__		:				int    =  super()._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase					,      return_labels=__lowerCamelCase       )

             if return_labels:
                   if model_class in get_values(__lowerCamelCase       ):
                         lowerCamelCase__		:				Dict    =  torch.zeros(
                             (self.model_tester.batch_size, self.model_tester.seq_length)					,      dtype=torch.long					,      device=__lowerCamelCase       )
                         lowerCamelCase__		:				int    =  torch.zeros(
                             self.model_tester.batch_size					,      dtype=torch.long					,      device=__lowerCamelCase       )
             return inputs_dict





       def 						lowerCAmelCase     (   self							:			Any       ):


             '''simple docstring'''



             lowerCamelCase__		:				Dict    =  AlbertModelTester(self       )
             lowerCamelCase__		:				Optional[int]    =  ConfigTester(self					,      config_class=__lowerCamelCase					,      hidden_size=37       )





       def 						lowerCAmelCase     (   self							:			str       ):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       def 						lowerCAmelCase     (   self							:			int       ):


             '''simple docstring'''



             lowerCamelCase__		:				Any    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*__lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Dict       ):


             '''simple docstring'''



             lowerCamelCase__		:				int    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_pretraining(*__lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Optional[int]       ):


             '''simple docstring'''



             lowerCamelCase__		:				int    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_lm(*__lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Optional[int]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Any    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_multiple_choice(*__lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Tuple       ):


             '''simple docstring'''



             lowerCamelCase__		:				str    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Tuple       ):


             '''simple docstring'''



             lowerCamelCase__		:				List[Any]    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Any       ):


             '''simple docstring'''



             lowerCamelCase__		:				Optional[Any]    =  self.model_tester.prepare_config_and_inputs()
             for type in ["absolute", "relative_key", "relative_key_query"]:
                   lowerCamelCase__		:				Dict    =  type
                   self.model_tester.create_and_check_model(*__lowerCamelCase       )





       @slow
       def 						lowerCAmelCase     (   self							:			Optional[Any]       ):


             '''simple docstring'''



             for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   lowerCamelCase__		:				List[Any]    =  AlbertModel.from_pretrained(__lowerCamelCase       )
                   self.assertIsNotNone(__lowerCamelCase       )







@require_torch
class 			_lowercase (							unittest.TestCase):


       """simple docstring"""




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


             '''simple docstring'''



             lowerCamelCase__		:				Optional[int]    =  AlbertModel.from_pretrained("albert-base-v2"       )
             lowerCamelCase__		:				Union[str, Any]    =  torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]       )
             lowerCamelCase__		:				int    =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
             with torch.no_grad():
                   lowerCamelCase__		:				Tuple    =  model(__lowerCamelCase					,      attention_mask=__lowerCamelCase       )[0]
             lowerCamelCase__		:				int    =  torch.Size((1, 11, 768)       )
             self.assertEqual(output.shape					,      __lowerCamelCase       )
             lowerCamelCase__		:				Dict    =  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       )       )






def       lowercase_		(		_A			:    int    ,					_A			:    list						):

      """simple docstring"""




      _enforce_args(_A    ,					_A						)
      if n == 0:
            return 0
      lowerCamelCase__		:				Union[str, Any]    =  float("-inf"						)
      for i in range(1    ,					n + 1						):
            lowerCamelCase__		:				int    =  max(
                _A    ,					prices[i - 1] + naive_cut_rod_recursive(n - i    ,					_A						)						)

      return max_revue

def       lowercase_		(		_A			:    int    ,					_A			:    list						):

      """simple docstring"""




      _enforce_args(_A    ,					_A						)
      lowerCamelCase__		:				int    =  [float("-inf"						) for _ in range(n + 1						)]
      return _top_down_cut_rod_recursive(_A    ,					_A    ,					_A						)

def       lowercase_		(		_A			:    int    ,					_A			:    list    ,					_A			:    list						):

      """simple docstring"""




      if max_rev[n] >= 0:
            return max_rev[n]
      elif n == 0:
            return 0
      else:
            lowerCamelCase__		:				Dict    =  float("-inf"						)
            for i in range(1    ,					n + 1						):
                  lowerCamelCase__		:				int    =  max(
                      _A    ,					prices[i - 1] + _top_down_cut_rod_recursive(n - i    ,					_A    ,					_A						)    ,					)

            lowerCamelCase__		:				List[Any]    =  max_revenue

      return max_rev[n]

def       lowercase_		(		_A			:    int    ,					_A			:    list						):

      """simple docstring"""




      _enforce_args(_A    ,					_A						)

      # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of
      # length 0.
      lowerCamelCase__		:				int    =  [float("-inf"						) for _ in range(n + 1						)]
      lowerCamelCase__		:				Optional[int]    =  0

      for i in range(1    ,					n + 1						):
            lowerCamelCase__		:				Union[str, Any]    =  max_rev[i]
            for j in range(1    ,					i + 1						):
                  lowerCamelCase__		:				Any    =  max(_A    ,					prices[j - 1] + max_rev[i - j]						)

            lowerCamelCase__		:				Any    =  max_revenue_i

      return max_rev[n]

def       lowercase_		(		_A			:    int    ,					_A			:    list						):

      """simple docstring"""




      if n < 0:
            lowerCamelCase__		:				Optional[int]    =  F"n must be greater than or equal to 0. Got n = {n}"
            raise ValueError(_A						)

      if n > len(_A						):
            lowerCamelCase__		:				Optional[int]    =  (
                "Each integral piece of rod must have a corresponding price. "
                F"Got n = {n} but length of prices = {len(_A						)}"
            )
            raise ValueError(_A						)

def       lowercase_		(		):

      """simple docstring"""




      lowerCamelCase__		:				Optional[Any]    =  [6, 10, 12, 15, 20, 23]
      lowerCamelCase__		:				Dict    =  len(_A						)

      # the best revenue comes from cutting the rod into 6 pieces, each
      # of length 1 resulting in a revenue of 6 * 6 = 36.
      lowerCamelCase__		:				int    =  36

      lowerCamelCase__		:				List[Any]    =  top_down_cut_rod(_A    ,					_A						)
      lowerCamelCase__		:				List[Any]    =  bottom_up_cut_rod(_A    ,					_A						)
      lowerCamelCase__		:				List[Any]    =  naive_cut_rod_recursive(_A    ,					_A						)

      assert expected_max_revenue == max_rev_top_down
      assert max_rev_top_down == max_rev_bottom_up
      assert max_rev_bottom_up == max_rev_naive


if __name__ == "__main__":
    main()


