


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





      '''simple docstring'''






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

             '''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       UpperCamelCase__		(self    :    List[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       UpperCamelCase__		(self    :    List[str]   ,						UpperCamelCase    :    str   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    List[str]   ,						UpperCamelCase    :    List[str]   ,						UpperCamelCase    :    Optional[Any]   ,						UpperCamelCase    :    str ):

             '''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       UpperCamelCase__		(self    :    List[Any]   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    Optional[int]   ,						UpperCamelCase    :    List[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       UpperCamelCase__		(self    :    str   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Tuple ):

             '''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       UpperCamelCase__		(self    :    Tuple   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    Tuple   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    List[str]   ,						UpperCamelCase    :    Tuple ):

             '''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       UpperCamelCase__		(self    :    str   ,						UpperCamelCase    :    Optional[Any]   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    Dict ):

             '''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       UpperCamelCase__		(self    :    str   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    str   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Union[str, 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       UpperCamelCase__		(self    :    Optional[Any]   ,						UpperCamelCase    :    Optional[Any]   ,						UpperCamelCase    :    Optional[int]   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    List[str]   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    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       UpperCamelCase__		(self    :    Union[str, Any] ):

             '''simple docstring'''







             lowercase__      			= self.prepare_config_and_inputs()
             (
                 (
                 lowercase__
             )			,(
                 lowercase__
             )			,(
                 lowercase__
             )			,(
                 lowercase__
             )			,(
                 lowercase__
             )			,(
                 lowercase__
             )			,(
                 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						(lowercase_  ,     lowercase_  ,     unittest.TestCase     ):





      '''simple docstring'''






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



      def       UpperCamelCase__		(self    :    Union[str, Any]   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    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       UpperCamelCase__		(self    :    int ):

             '''simple docstring'''







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



      def       UpperCamelCase__		(self    :    List[Any] ):

             '''simple docstring'''







             self.config_tester.run_common_tests()



      def       UpperCamelCase__		(self    :    Tuple ):

             '''simple docstring'''







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



      def       UpperCamelCase__		(self    :    Optional[int] ):

             '''simple docstring'''







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



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

             '''simple docstring'''







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



      def       UpperCamelCase__		(self    :    Dict ):

             '''simple docstring'''







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



      def       UpperCamelCase__		(self    :    Dict ):

             '''simple docstring'''







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



      def       UpperCamelCase__		(self    :    Optional[int] ):

             '''simple docstring'''







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



      def       UpperCamelCase__		(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       UpperCamelCase__		(self    :    Tuple ):

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





      '''simple docstring'''






      @slow
      def       UpperCamelCase__		(self    :    List[Any] ):

             '''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.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]   ,						UpperCamelCase   ,						atol=1E-4 ) )








'''simple docstring'''

import warnings

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase							:   str  						=	logging.get_logger(__name__)

lowerCamelCase							:   int  						=	{
    'RUCAIBox/mvp': 'https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json',
}
class   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     Union[str, Any]  					=							"""mvp"""
      lowerCAmelCase__					:     Optional[Any]  					=							["""past_key_values"""]
      lowerCAmelCase__					:     List[str]  					=							{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}



      def __init__(self    :    Any   ,						UpperCamelCase    :    Optional[int]=50267   ,						UpperCamelCase    :    Tuple=1024   ,						UpperCamelCase    :    int=12   ,						UpperCamelCase    :    Tuple=4096   ,						UpperCamelCase    :    Dict=16   ,						UpperCamelCase    :    int=12   ,						UpperCamelCase    :    Optional[int]=4096   ,						UpperCamelCase    :    Optional[int]=16   ,						UpperCamelCase    :    Tuple=0.0   ,						UpperCamelCase    :    Tuple=0.0   ,						UpperCamelCase    :    List[Any]="gelu"   ,						UpperCamelCase    :    Union[str, Any]=1024   ,						UpperCamelCase    :    Optional[Any]=0.1   ,						UpperCamelCase    :    str=0.0   ,						UpperCamelCase    :    str=0.0   ,						UpperCamelCase    :    Optional[Any]=0.02   ,						UpperCamelCase    :    List[str]=0.0   ,						UpperCamelCase    :    List[str]=False   ,						UpperCamelCase    :    Optional[int]=True   ,						UpperCamelCase    :    Any=1   ,						UpperCamelCase    :    int=0   ,						UpperCamelCase    :    int=2   ,						UpperCamelCase    :    Any=True   ,						UpperCamelCase    :    Optional[Any]=2   ,						UpperCamelCase    :    Optional[Any]=2   ,						UpperCamelCase    :    Tuple=False   ,						UpperCamelCase    :    int=100   ,						UpperCamelCase    :    Optional[Any]=800   ,						**UpperCamelCase    :    str   ,						):

             '''simple docstring'''







             lowercase__      			= vocab_size
             lowercase__      			= max_position_embeddings
             lowercase__      			= d_model
             lowercase__      			= encoder_ffn_dim
             lowercase__      			= encoder_layers
             lowercase__      			= encoder_attention_heads
             lowercase__      			= decoder_ffn_dim
             lowercase__      			= decoder_layers
             lowercase__      			= decoder_attention_heads
             lowercase__      			= dropout
             lowercase__      			= attention_dropout
             lowercase__      			= activation_dropout
             lowercase__      			= activation_function
             lowercase__      			= init_std
             lowercase__      			= encoder_layerdrop
             lowercase__      			= decoder_layerdrop
             lowercase__      			= classifier_dropout
             lowercase__      			= use_cache
             lowercase__      			= encoder_layers
             lowercase__      			= scale_embedding  # scale factor will be sqrt(d_model) if True
             lowercase__      			= use_prompt
             lowercase__      			= prompt_length
             lowercase__      			= prompt_mid_dim

             super().__init__(
                 pad_token_id=UpperCamelCase   ,						bos_token_id=UpperCamelCase   ,						eos_token_id=UpperCamelCase   ,						is_encoder_decoder=UpperCamelCase   ,						decoder_start_token_id=UpperCamelCase   ,						forced_eos_token_id=UpperCamelCase   ,						**UpperCamelCase   ,						)

             if self.forced_bos_token_id is None and kwargs.get('''force_bos_token_to_be_generated'''   ,						UpperCamelCase ):
                    lowercase__      			= self.bos_token_id
                    warnings.warn(
                        f"Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. "
                        '''The config can simply be saved and uploaded again to be fixed.''' )




