






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




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




					def   A  (				self     :						Any   )  ->  Optional[Any]:
							lowercase_ : Tuple							       =   ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size   )

							lowercase_ : str							       =   None
							if self.use_input_mask:
									lowercase_ : int							       =   random_attention_mask([self.batch_size, self.seq_length]   )

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

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

							lowercase_ : str							       =   self.get_config()

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




					def   A  (				self     :						Tuple   )  ->  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   A  (				self     :						Union[str, Any]    ,						A     :						List[Any]    ,						A     :						Optional[Any]    ,						A     :						Optional[Any]    ,						A     :						Any    ,						A     :						Dict    ,						A     :						str    ,						A     :						Optional[int]   )  ->  str:
							lowercase_ : Dict							       =   AlbertModel(config=A   )
							model.to(A   )
							model.eval()
							lowercase_ : Optional[Any]							       =   model(A    ,						attention_mask=A    ,						token_type_ids=A   )
							lowercase_ : Dict							       =   model(A    ,						token_type_ids=A   )
							lowercase_ : Optional[int]							       =   model(A   )
							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     :						Dict    ,						A     :						Tuple    ,						A     :						Optional[Any]    ,						A     :						Optional[int]    ,						A     :						Dict    ,						A     :						str    ,						A     :						Tuple    ,						A     :						List[Any]   )  ->  List[str]:
							lowercase_ : str							       =   AlbertForPreTraining(config=A   )
							model.to(A   )
							model.eval()
							lowercase_ : Any							       =   model(
							    A    ,						attention_mask=A    ,						token_type_ids=A    ,						labels=A    ,						sentence_order_label=A    ,						)
							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     :						Tuple    ,						A     :						List[str]    ,						A     :						List[str]    ,						A     :						Any    ,						A     :						List[Any]    ,						A     :						Optional[int]    ,						A     :						Tuple    ,						A     :						Optional[int]   )  ->  Any:
							lowercase_ : Union[str, Any]							       =   AlbertForMaskedLM(config=A   )
							model.to(A   )
							model.eval()
							lowercase_ : Any							       =   model(A    ,						attention_mask=A    ,						token_type_ids=A    ,						labels=A   )
							self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.vocab_size)   )




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




					def   A  (				self     :						Any    ,						A     :						Union[str, Any]    ,						A     :						List[Any]    ,						A     :						List[str]    ,						A     :						Union[str, Any]    ,						A     :						Optional[int]    ,						A     :						str    ,						A     :						Dict   )  ->  Dict:
							lowercase_ : Any							       =   self.num_labels
							lowercase_ : Optional[int]							       =   AlbertForTokenClassification(config=A   )
							model.to(A   )
							model.eval()
							lowercase_ : int							       =   model(A    ,						attention_mask=A    ,						token_type_ids=A    ,						labels=A   )
							self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.num_labels)   )




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






					def   A  (				self     :						str   )  ->  Any:
							lowercase_ : Union[str, Any]							       =   self.prepare_config_and_inputs()
							(
							    (
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					
							) : Any							       =   config_and_inputs
							lowercase_ : Dict							       =   {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
							return config, inputs_dict





@require_torch
class 					_UpperCAmelCase       ( _A   ,						_A   ,						unittest.TestCase							):
					SCREAMING_SNAKE_CASE_		:				Optional[Any]							    =   (
					    (
					        AlbertModel,
					        AlbertForPreTraining,
					        AlbertForMaskedLM,
					        AlbertForMultipleChoice,
					        AlbertForSequenceClassification,
					        AlbertForTokenClassification,
					        AlbertForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					SCREAMING_SNAKE_CASE_		:				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 {}
					)
					SCREAMING_SNAKE_CASE_		:				str							    =   True




					def   A  (				self     :						List[Any]    ,						A     :						Any    ,						A     :						Optional[Any]    ,						A     :						Tuple=False   )  ->  List[Any]:
							lowercase_ : List[Any]							       =   super()._prepare_for_class(A    ,						A    ,						return_labels=A   )

							if return_labels:
									if model_class in get_values(A   ):
											lowercase_ : Any							       =   torch.zeros(
											    (self.model_tester.batch_size, self.model_tester.seq_length)    ,						dtype=torch.long    ,						device=A   )
											lowercase_ : Union[str, Any]							       =   torch.zeros(
											    self.model_tester.batch_size    ,						dtype=torch.long    ,						device=A   )
							return inputs_dict




					def   A  (				self     :						Tuple   )  ->  List[Any]:
							lowercase_ : Union[str, Any]							       =   AlbertModelTester(self   )
							lowercase_ : int							       =   ConfigTester(self    ,						config_class=A    ,						hidden_size=37   )




					def   A  (				self     :						Tuple   )  ->  Union[str, Any]:
							self.config_tester.run_common_tests()




					def   A  (				self     :						List[Any]   )  ->  int:
							lowercase_ : Optional[int]							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*A   )




					def   A  (				self     :						Optional[int]   )  ->  Tuple:
							lowercase_ : Dict							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*A   )




					def   A  (				self     :						Optional[int]   )  ->  Dict:
							lowercase_ : Optional[Any]							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*A   )




					def   A  (				self     :						List[str]   )  ->  Optional[Any]:
							lowercase_ : List[str]							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*A   )




					def   A  (				self     :						Optional[int]   )  ->  Any:
							lowercase_ : str							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*A   )




					def   A  (				self     :						int   )  ->  Dict:
							lowercase_ : Optional[Any]							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*A   )




					def   A  (				self     :						Union[str, Any]   )  ->  int:
							lowercase_ : Tuple							       =   self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
									lowercase_ : Any							       =   type
									self.model_tester.create_and_check_model(*A   )






					@slow
					def   A  (				self     :						Optional[int]   )  ->  Optional[int]:
							for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowercase_ : str							       =   AlbertModel.from_pretrained(A   )
									self.assertIsNotNone(A   )





@require_torch
class 					_UpperCAmelCase       ( unittest.TestCase							):




					@slow
					def   A  (				self     :						Tuple   )  ->  Dict:
							lowercase_ : Union[str, Any]							       =   AlbertModel.from_pretrained('''albert-base-v2'''   )
							lowercase_ : str							       =   torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]   )
							lowercase_ : Optional[Any]							       =   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
							with torch.no_grad():
									lowercase_ : Dict							       =   model(A    ,						attention_mask=A   )[0]
							lowercase_ : int							       =   torch.Size((1, 11, 7_68)   )
							self.assertEqual(output.shape    ,						A   )
							lowercase_ : List[Any]							       =   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]    ,						A    ,						atol=1e-4   )   )











"""simple docstring"""



import argparse
import os
import re

import packaging.version


__A		:       List[str]  				=		'''examples/'''
__A		:       int  				=		{
    '''examples''': (re.compile(R'''^check_min_version\("[^"]+"\)\s*$''', re.MULTILINE), '''check_min_version("VERSION")\n'''),
    '''init''': (re.compile(R'''^__version__\s+=\s+"([^"]+)"\s*$''', re.MULTILINE), '''__version__ = "VERSION"\n'''),
    '''setup''': (re.compile(R'''^(\s*)version\s*=\s*"[^"]+",''', re.MULTILINE), R'''\1version="VERSION",'''),
    '''doc''': (re.compile(R'''^(\s*)release\s*=\s*"[^"]+"$''', re.MULTILINE), '''release = "VERSION"\n'''),
}
__A		:       Dict  				=		{
    '''init''': '''src/transformers/__init__.py''',
    '''setup''': '''setup.py''',
}
__A		:       Optional[int]  				=		'''README.md'''



def 						lowercase    (				__snake_case				: int		,					__snake_case				: Any		,					__snake_case				: int	):
		with open(__snake_case		,					'''r'''		,					encoding='''utf-8'''		,					newline='''\n'''	) as f:
				lowercase_ : int							       =   f.read()
		lowercase_       ,					lowercase_ : List[str]							       =   REPLACE_PATTERNS[pattern]
		lowercase_ : Union[str, Any]							       =   replace.replace('''VERSION'''		,					__snake_case	)
		lowercase_ : Optional[Any]							       =   re_pattern.sub(__snake_case		,					__snake_case	)
		with open(__snake_case		,					'''w'''		,					encoding='''utf-8'''		,					newline='''\n'''	) as f:
				f.write(__snake_case	)



def 						lowercase    (				__snake_case				: int	):
		for folder, directories, fnames in os.walk(__snake_case	):
				# Removing some of the folders with non-actively maintained examples from the walk
				if "research_projects" in directories:
						directories.remove('''research_projects'''	)
				if "legacy" in directories:
						directories.remove('''legacy'''	)
				for fname in fnames:
						if fname.endswith('''.py'''	):
								update_version_in_file(os.path.join(__snake_case		,					__snake_case	)		,					__snake_case		,					pattern='''examples'''	)



def 						lowercase    (				__snake_case				: Optional[Any]		,					__snake_case				: Optional[Any]=False	):
		for pattern, fname in REPLACE_FILES.items():
				update_version_in_file(__snake_case		,					__snake_case		,					__snake_case	)
		if not patch:
				update_version_in_examples(__snake_case	)



def 						lowercase    (				):
		lowercase_ : Union[str, Any]							       =   '''🤗 Transformers currently provides the following architectures'''
		lowercase_ : Union[str, Any]							       =   '''1. Want to contribute a new model?'''
		with open(__snake_case		,					'''r'''		,					encoding='''utf-8'''		,					newline='''\n'''	) as f:
				lowercase_ : List[str]							       =   f.readlines()

		# Find the start of the list.
		lowercase_ : Optional[Any]							       =   0
		while not lines[start_index].startswith(_start_prompt	):
				start_index += 1
		start_index += 1

		lowercase_ : str							       =   start_index
		# Update the lines in the model list.
		while not lines[index].startswith(_end_prompt	):
				if lines[index].startswith('''1.'''	):
						lowercase_ : str							       =   lines[index].replace(
						    '''https://huggingface.co/docs/transformers/main/model_doc'''		,					'''https://huggingface.co/docs/transformers/model_doc'''		,					)
				index += 1

		with open(__snake_case		,					'''w'''		,					encoding='''utf-8'''		,					newline='''\n'''	) as f:
				f.writelines(__snake_case	)



def 						lowercase    (				):
		with open(REPLACE_FILES['''init''']		,					'''r'''	) as f:
				lowercase_ : List[Any]							       =   f.read()
		lowercase_ : List[str]							       =   REPLACE_PATTERNS['''init'''][0].search(__snake_case	).groups()[0]
		return packaging.version.parse(__snake_case	)



def 						lowercase    (				__snake_case				: Optional[Any]=False	):
		lowercase_ : str							       =   get_version()
		if patch and default_version.is_devrelease:
				raise ValueError('''Can\'t create a patch version from the dev branch, checkout a released version!'''	)
		if default_version.is_devrelease:
				lowercase_ : Optional[Any]							       =   default_version.base_version
		elif patch:
				lowercase_ : Optional[int]							       =   F'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}'''
		else:
				lowercase_ : Optional[int]							       =   F'''{default_version.major}.{default_version.minor + 1}.0'''

		# Now let's ask nicely if that's the right one.
		lowercase_ : int							       =   input(F'''Which version are you releasing? [{default_version}]'''	)
		if len(__snake_case	) == 0:
				lowercase_ : Dict							       =   default_version

		print(F'''Updating version to {version}.'''	)
		global_version_update(__snake_case		,					patch=__snake_case	)
		if not patch:
				print('''Cleaning main README, don\'t forget to run `make fix-copies`.'''	)
				clean_main_ref_in_model_list()



def 						lowercase    (				):
		lowercase_ : List[Any]							       =   get_version()
		lowercase_ : List[str]							       =   F'''{current_version.major}.{current_version.minor + 1}.0.dev0'''
		lowercase_ : Any							       =   current_version.base_version

		# Check with the user we got that right.
		lowercase_ : Tuple							       =   input(F'''Which version are we developing now? [{dev_version}]'''	)
		if len(__snake_case	) == 0:
				lowercase_ : str							       =   dev_version

		print(F'''Updating version to {version}.'''	)
		global_version_update(__snake_case	)
		print('''Cleaning main README, don\'t forget to run `make fix-copies`.'''	)
		clean_main_ref_in_model_list()


if __name__ == "__main__":
						__A		:       int  				=		argparse.ArgumentParser()
						parser.add_argument('''--post_release''', action='''store_true''', help='''Whether this is pre or post release.''')
						parser.add_argument('''--patch''', action='''store_true''', help='''Whether or not this is a patch release.''')
						__A		:       Any  				=		parser.parse_args()
						if not args.post_release:
												pre_release_work(patch=args.patch)
						elif args.patch:
												print('''Nothing to do after a patch :-)''')
						else:
												post_release_work()



