




"""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 							lowercase_			:



 '''simple docstring'''

 def __init__(					self			:			str       ,				_UpperCAmelCase			:			List[str]       ,				_UpperCAmelCase			:			Optional[Any]=13       ,				_UpperCAmelCase			:			Union[str, Any]=7       ,				_UpperCAmelCase			:			Tuple=True       ,				_UpperCAmelCase			:			Tuple=True       ,				_UpperCAmelCase			:			List[str]=True       ,				_UpperCAmelCase			:			Optional[Any]=True       ,				_UpperCAmelCase			:			Any=99       ,				_UpperCAmelCase			:			List[str]=16       ,				_UpperCAmelCase			:			Dict=36       ,				_UpperCAmelCase			:			Tuple=6       ,				_UpperCAmelCase			:			List[Any]=6       ,				_UpperCAmelCase			:			Tuple=6       ,				_UpperCAmelCase			:			List[Any]=37       ,				_UpperCAmelCase			:			List[str]="gelu"       ,				_UpperCAmelCase			:			Optional[Any]=0.1       ,				_UpperCAmelCase			:			Dict=0.1       ,				_UpperCAmelCase			:			Union[str, Any]=512       ,				_UpperCAmelCase			:			Tuple=16       ,				_UpperCAmelCase			:			Dict=2       ,				_UpperCAmelCase			:			List[Any]=0.02       ,				_UpperCAmelCase			:			Optional[int]=3       ,				_UpperCAmelCase			:			Dict=4       ,				_UpperCAmelCase			:			Optional[int]=None       ,				):
       _A     =  parent
       _A     =  batch_size
       _A     =  seq_length
       _A     =  is_training
       _A     =  use_input_mask
       _A     =  use_token_type_ids
       _A     =  use_labels
       _A     =  vocab_size
       _A     =  embedding_size
       _A     =  hidden_size
       _A     =  num_hidden_layers
       _A     =  num_hidden_groups
       _A     =  num_attention_heads
       _A     =  intermediate_size
       _A     =  hidden_act
       _A     =  hidden_dropout_prob
       _A     =  attention_probs_dropout_prob
       _A     =  max_position_embeddings
       _A     =  type_vocab_size
       _A     =  type_sequence_label_size
       _A     =  initializer_range
       _A     =  num_labels
       _A     =  num_choices
       _A     =  scope

 def      lowerCAmelCase_				(					self			:			Dict						):
       _A     =  ids_tensor([self.batch_size, self.seq_length]       ,				self.vocab_size						)

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

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

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

       _A     =  self.get_config()

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

 def      lowerCAmelCase_				(					self			:			List[Any]						):
       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			:			Dict       ,				_UpperCAmelCase			:			Optional[Any]       ,				_UpperCAmelCase			:			str       ,				_UpperCAmelCase			:			List[str]       ,				_UpperCAmelCase			:			str       ,				_UpperCAmelCase			:			Optional[Any]       ,				_UpperCAmelCase			:			str       ,				_UpperCAmelCase			:			str						):
       _A     =  AlbertModel(config=_UpperCAmelCase						)
       model.to(_UpperCAmelCase						)
       model.eval()
       _A     =  model(_UpperCAmelCase       ,				attention_mask=_UpperCAmelCase       ,				token_type_ids=_UpperCAmelCase						)
       _A     =  model(_UpperCAmelCase       ,				token_type_ids=_UpperCAmelCase						)
       _A     =  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      lowerCAmelCase_				(					self			:			int       ,				_UpperCAmelCase			:			List[Any]       ,				_UpperCAmelCase			:			Optional[Any]       ,				_UpperCAmelCase			:			Any       ,				_UpperCAmelCase			:			Union[str, Any]       ,				_UpperCAmelCase			:			Any       ,				_UpperCAmelCase			:			int       ,				_UpperCAmelCase			:			Any						):
       _A     =  AlbertForPreTraining(config=_UpperCAmelCase						)
       model.to(_UpperCAmelCase						)
       model.eval()
       _A     =  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      lowerCAmelCase_				(					self			:			Tuple       ,				_UpperCAmelCase			:			Optional[int]       ,				_UpperCAmelCase			:			str       ,				_UpperCAmelCase			:			Union[str, Any]       ,				_UpperCAmelCase			:			str       ,				_UpperCAmelCase			:			Tuple       ,				_UpperCAmelCase			:			str       ,				_UpperCAmelCase			:			Optional[Any]						):
       _A     =  AlbertForMaskedLM(config=_UpperCAmelCase						)
       model.to(_UpperCAmelCase						)
       model.eval()
       _A     =  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      lowerCAmelCase_				(					self			:			int       ,				_UpperCAmelCase			:			Optional[int]       ,				_UpperCAmelCase			:			Optional[int]       ,				_UpperCAmelCase			:			Dict       ,				_UpperCAmelCase			:			str       ,				_UpperCAmelCase			:			Optional[int]       ,				_UpperCAmelCase			:			Optional[int]       ,				_UpperCAmelCase			:			Union[str, Any]						):
       _A     =  AlbertForQuestionAnswering(config=_UpperCAmelCase						)
       model.to(_UpperCAmelCase						)
       model.eval()
       _A     =  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      lowerCAmelCase_				(					self			:			List[Any]       ,				_UpperCAmelCase			:			Optional[int]       ,				_UpperCAmelCase			:			Union[str, Any]       ,				_UpperCAmelCase			:			Optional[Any]       ,				_UpperCAmelCase			:			Optional[Any]       ,				_UpperCAmelCase			:			List[Any]       ,				_UpperCAmelCase			:			List[str]       ,				_UpperCAmelCase			:			Tuple						):
       _A     =  self.num_labels
       _A     =  AlbertForSequenceClassification(_UpperCAmelCase						)
       model.to(_UpperCAmelCase						)
       model.eval()
       _A     =  model(_UpperCAmelCase       ,				attention_mask=_UpperCAmelCase       ,				token_type_ids=_UpperCAmelCase       ,				labels=_UpperCAmelCase						)
       self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_labels)						)

 def      lowerCAmelCase_				(					self			:			List[str]       ,				_UpperCAmelCase			:			Any       ,				_UpperCAmelCase			:			Dict       ,				_UpperCAmelCase			:			Union[str, Any]       ,				_UpperCAmelCase			:			List[str]       ,				_UpperCAmelCase			:			List[str]       ,				_UpperCAmelCase			:			List[str]       ,				_UpperCAmelCase			:			Any						):
       _A     =  self.num_labels
       _A     =  AlbertForTokenClassification(config=_UpperCAmelCase						)
       model.to(_UpperCAmelCase						)
       model.eval()
       _A     =  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      lowerCAmelCase_				(					self			:			Tuple       ,				_UpperCAmelCase			:			List[str]       ,				_UpperCAmelCase			:			Tuple       ,				_UpperCAmelCase			:			Union[str, Any]       ,				_UpperCAmelCase			:			str       ,				_UpperCAmelCase			:			List[Any]       ,				_UpperCAmelCase			:			Union[str, Any]       ,				_UpperCAmelCase			:			List[Any]						):
       _A     =  self.num_choices
       _A     =  AlbertForMultipleChoice(config=_UpperCAmelCase						)
       model.to(_UpperCAmelCase						)
       model.eval()
       _A     =  input_ids.unsqueeze(1						).expand(-1       ,				self.num_choices       ,				-1						).contiguous()
       _A     =  token_type_ids.unsqueeze(1						).expand(-1       ,				self.num_choices       ,				-1						).contiguous()
       _A     =  input_mask.unsqueeze(1						).expand(-1       ,				self.num_choices       ,				-1						).contiguous()
       _A     =  model(
           _UpperCAmelCase       ,				attention_mask=_UpperCAmelCase       ,				token_type_ids=_UpperCAmelCase       ,				labels=_UpperCAmelCase       ,				)
       self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_choices)						)



 def      lowerCAmelCase_				(					self			:			Optional[int]						):
       _A     =  self.prepare_config_and_inputs()
       (
           (
           _A
       )  ,       (
           _A
       )  ,       (
           _A
       )  ,       (
           _A
       )  ,       (
           _A
       )  ,       (
           _A
       )  ,       (
           _A
       )  ,       
       )     =  config_and_inputs
       _A     =  {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
       return config, inputs_dict





@require_torch
class 							lowercase_			(	__lowerCAmelCase     ,      __lowerCAmelCase     ,      unittest.TestCase				):



 '''simple docstring'''
 UpperCAmelCase       :	Any     =      (
     (
         AlbertModel,
         AlbertForPreTraining,
         AlbertForMaskedLM,
         AlbertForMultipleChoice,
         AlbertForSequenceClassification,
         AlbertForTokenClassification,
         AlbertForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 UpperCAmelCase       :	List[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       :	int     =      True

 def      lowerCAmelCase_				(					self			:			Any       ,				_UpperCAmelCase			:			Dict       ,				_UpperCAmelCase			:			Optional[int]       ,				_UpperCAmelCase			:			int=False						):
       _A     =  super()._prepare_for_class(_UpperCAmelCase       ,				_UpperCAmelCase       ,				return_labels=_UpperCAmelCase						)

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

 def      lowerCAmelCase_				(					self			:			Any						):
       _A     =  AlbertModelTester(self						)
       _A     =  ConfigTester(self       ,				config_class=_UpperCAmelCase       ,				hidden_size=37						)

 def      lowerCAmelCase_				(					self			:			int						):
       self.config_tester.run_common_tests()

 def      lowerCAmelCase_				(					self			:			Optional[Any]						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			int						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_pretraining(*_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Optional[int]						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			int						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			int						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Union[str, Any]						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Optional[int]						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
             _A     =  type
             self.model_tester.create_and_check_model(*_UpperCAmelCase						)



 @slow
 def      lowerCAmelCase_				(					self			:			int						):
       for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _A     =  AlbertModel.from_pretrained(_UpperCAmelCase						)
             self.assertIsNotNone(_UpperCAmelCase						)





@require_torch
class 							lowercase_			(	unittest.TestCase				):



 '''simple docstring'''

 @slow
 def      lowerCAmelCase_				(					self			:			Optional[Any]						):
       _A     =  AlbertModel.from_pretrained('albert-base-v2'						)
       _A     =  torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]						)
       _A     =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
       with torch.no_grad():
             _A     =  model(_UpperCAmelCase       ,				attention_mask=_UpperCAmelCase						)[0]
       _A     =  torch.Size((1, 11, 768)						)
       self.assertEqual(output.shape       ,				_UpperCAmelCase						)
       _A     =  torch.tensor(
           [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]						)

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







'''simple docstring'''
from argparse import ArgumentParser

from .add_new_model import AddNewModelCommand
from .add_new_model_like import AddNewModelLikeCommand
from .convert import ConvertCommand
from .download import DownloadCommand
from .env import EnvironmentCommand
from .lfs import LfsCommands
from .pt_to_tf import PTtoTFCommand
from .run import RunCommand
from .serving import ServeCommand
from .user import UserCommands





def 			lowerCamelCase(							)		->		int:
			A_									= ArgumentParser('Transformers CLI tool'       ,usage='transformers-cli <command> [<args>]'     )
			A_									= parser.add_subparsers(help='transformers-cli command helpers'     )

			# Register commands
			ConvertCommand.register_subcommand(SCREAMING_SNAKE_CASE_     )
			DownloadCommand.register_subcommand(SCREAMING_SNAKE_CASE_     )
			EnvironmentCommand.register_subcommand(SCREAMING_SNAKE_CASE_     )
			RunCommand.register_subcommand(SCREAMING_SNAKE_CASE_     )
			ServeCommand.register_subcommand(SCREAMING_SNAKE_CASE_     )
			UserCommands.register_subcommand(SCREAMING_SNAKE_CASE_     )
			AddNewModelCommand.register_subcommand(SCREAMING_SNAKE_CASE_     )
			AddNewModelLikeCommand.register_subcommand(SCREAMING_SNAKE_CASE_     )
			LfsCommands.register_subcommand(SCREAMING_SNAKE_CASE_     )
			PTtoTFCommand.register_subcommand(SCREAMING_SNAKE_CASE_     )

			# Let's go
			A_									= parser.parse_args()

			if not hasattr(SCREAMING_SNAKE_CASE_       ,'func'     ):
						parser.print_help()
						exit(1     )

			# Run
			A_									= args.func(SCREAMING_SNAKE_CASE_     )
			service.run()


if __name__ == "__main__":
							main()




