






"""simple docstring"""



from __future__ import annotations

import unittest

from transformers import AlbertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
						import tensorflow as tf

						from transformers import TF_MODEL_FOR_PRETRAINING_MAPPING
						from transformers.models.albert.modeling_tf_albert import (
						    TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFAlbertForMaskedLM,
						    TFAlbertForMultipleChoice,
						    TFAlbertForPreTraining,
						    TFAlbertForQuestionAnswering,
						    TFAlbertForSequenceClassification,
						    TFAlbertForTokenClassification,
						    TFAlbertModel,
						)





class  _UpperCAmelCase	:




					def __init__(    self					:						List[str]   ,						A					:						int   ,						A					:						Tuple=13   ,						A					:						Union[str, Any]=7   ,						A					:						str=True   ,						A					:						Any=True   ,						A					:						Any=True   ,						A					:						Tuple=True   ,						A					:						int=99   ,						A					:						Any=16   ,						A					:						Optional[Any]=32   ,						A					:						str=2   ,						A					:						str=4   ,						A					:						List[str]=37   ,						A					:						Optional[Any]="gelu"   ,						A					:						Optional[int]=0.1   ,						A					:						Dict=0.1   ,						A					:						Union[str, Any]=5_12   ,						A					:						Optional[Any]=16   ,						A					:						Optional[Any]=2   ,						A					:						Optional[int]=0.02   ,						A					:						List[str]=3   ,						A					:						Tuple=4   ,						A					:						Dict=None   ,						)  ->  Tuple:
							lowercase_			:			Any								=       parent
							lowercase_			:			List[str]								=       13
							lowercase_			:			Any								=       7
							lowercase_			:			Optional[int]								=       True
							lowercase_			:			Tuple								=       True
							lowercase_			:			List[str]								=       True
							lowercase_			:			Dict								=       True
							lowercase_			:			Union[str, Any]								=       99
							lowercase_			:			int								=       16
							lowercase_			:			List[str]								=       32
							lowercase_			:			Optional[int]								=       2
							lowercase_			:			Any								=       4
							lowercase_			:			List[Any]								=       37
							lowercase_			:			str								=       '''gelu'''
							lowercase_			:			Tuple								=       0.1
							lowercase_			:			str								=       0.1
							lowercase_			:			Tuple								=       5_12
							lowercase_			:			Union[str, Any]								=       16
							lowercase_			:			Tuple								=       2
							lowercase_			:			int								=       0.02
							lowercase_			:			Any								=       3
							lowercase_			:			Optional[Any]								=       4
							lowercase_			:			Optional[int]								=       None




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

							lowercase_			:			List[Any]								=       None
							if self.use_input_mask:
									lowercase_			:			Tuple								=       random_attention_mask([self.batch_size, self.seq_length]       )

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

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

							lowercase_			:			str								=       AlbertConfig(
							    vocab_size=self.vocab_size   ,						hidden_size=self.hidden_size   ,						embedding_size=self.embedding_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   ,						)

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




					def   A  (    self					:						List[str]   ,						A					:						Union[str, Any]   ,						A					:						Dict   ,						A					:						Any   ,						A					:						Union[str, Any]   ,						A					:						Optional[Any]   ,						A					:						Dict   ,						A					:						int       )  ->  Optional[Any]:
							lowercase_			:			Union[str, Any]								=       TFAlbertModel(config=A       )
							# inputs = {'input_ids': input_ids,
							#           'attention_mask': input_mask,
							#           'token_type_ids': token_type_ids}
							# sequence_output, pooled_output = model(**inputs)
							lowercase_			:			Optional[Any]								=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
							lowercase_			:			str								=       model(A       )

							lowercase_			:			Optional[int]								=       [input_ids, input_mask]
							lowercase_			:			Optional[Any]								=       model(A       )

							lowercase_			:			Any								=       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					:						int   ,						A					:						Union[str, Any]   ,						A					:						Any   ,						A					:						Dict   ,						A					:						List[Any]   ,						A					:						Optional[Any]   ,						A					:						List[str]   ,						A					:						Dict       )  ->  Optional[Any]:
							lowercase_			:			int								=       self.num_labels
							lowercase_			:			Optional[int]								=       TFAlbertForPreTraining(config=A       )
							lowercase_			:			Any								=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
							lowercase_			:			Union[str, Any]								=       model(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, self.num_labels)       )




					def   A  (    self					:						Tuple   ,						A					:						Any   ,						A					:						Any   ,						A					:						int   ,						A					:						Dict   ,						A					:						Any   ,						A					:						str   ,						A					:						int       )  ->  List[str]:
							lowercase_			:			str								=       TFAlbertForMaskedLM(config=A       )
							lowercase_			:			Optional[Any]								=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
							lowercase_			:			str								=       model(A       )
							self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.seq_length, self.vocab_size)       )




					def   A  (    self					:						List[Any]   ,						A					:						Union[str, Any]   ,						A					:						str   ,						A					:						Dict   ,						A					:						List[Any]   ,						A					:						List[Any]   ,						A					:						Tuple   ,						A					:						Tuple       )  ->  List[str]:
							lowercase_			:			Optional[int]								=       self.num_labels
							lowercase_			:			Union[str, Any]								=       TFAlbertForSequenceClassification(config=A       )
							lowercase_			:			List[Any]								=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
							lowercase_			:			Tuple								=       model(A       )
							self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.num_labels)       )




					def   A  (    self					:						Optional[Any]   ,						A					:						Any   ,						A					:						Union[str, Any]   ,						A					:						Tuple   ,						A					:						Any   ,						A					:						Any   ,						A					:						List[Any]   ,						A					:						str       )  ->  Optional[Any]:
							lowercase_			:			int								=       TFAlbertForQuestionAnswering(config=A       )
							lowercase_			:			List[Any]								=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
							lowercase_			:			Tuple								=       model(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					:						List[str]   ,						A					:						Any   ,						A					:						int   ,						A					:						Optional[int]   ,						A					:						Optional[Any]   ,						A					:						str   ,						A					:						List[str]   ,						A					:						Tuple       )  ->  List[Any]:
							lowercase_			:			List[str]								=       self.num_choices
							lowercase_			:			Union[str, Any]								=       TFAlbertForMultipleChoice(config=A       )
							lowercase_			:			Tuple								=       tf.tile(tf.expand_dims(A   ,						1       )   ,						(1, self.num_choices, 1)       )
							lowercase_			:			str								=       tf.tile(tf.expand_dims(A   ,						1       )   ,						(1, self.num_choices, 1)       )
							lowercase_			:			List[Any]								=       tf.tile(tf.expand_dims(A   ,						1       )   ,						(1, self.num_choices, 1)       )
							lowercase_			:			int								=       {
							    '''input_ids''': multiple_choice_inputs_ids,
							    '''attention_mask''': multiple_choice_input_mask,
							    '''token_type_ids''': multiple_choice_token_type_ids,
							}
							lowercase_			:			Tuple								=       model(A       )
							self.parent.assertListEqual(list(result['''logits'''].shape       )   ,						[self.batch_size, self.num_choices]       )




					def   A  (    self					:						int   ,						A					:						Any   ,						A					:						Optional[Any]   ,						A					:						Union[str, Any]   ,						A					:						Optional[int]   ,						A					:						Union[str, Any]   ,						A					:						str   ,						A					:						Any       )  ->  str:
							lowercase_			:			Dict								=       self.num_labels
							lowercase_			:			str								=       TFAlbertForTokenClassification(config=A       )
							lowercase_			:			Tuple								=       {
							    '''input_ids''': input_ids,
							    '''attention_mask''': input_mask,
							    '''token_type_ids''': token_type_ids,
							}
							lowercase_			:			str								=       model(A       )
							self.parent.assertListEqual(list(result['''logits'''].shape       )   ,						[self.batch_size, self.seq_length, self.num_labels]       )






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





@require_tf
class  _UpperCAmelCase	(			_A       ,						_A       ,						unittest.TestCase							):
					SCREAMING_SNAKE_CASE_		:    Union[str, Any]							   =       (
					    (
					        TFAlbertModel,
					        TFAlbertForPreTraining,
					        TFAlbertForMaskedLM,
					        TFAlbertForSequenceClassification,
					        TFAlbertForQuestionAnswering,
					        TFAlbertForTokenClassification,
					        TFAlbertForMultipleChoice,
					    )
					    if is_tf_available()
					    else ()
					)
					SCREAMING_SNAKE_CASE_		:    Optional[int]							   =       (
					    {
					        "feature-extraction": TFAlbertModel,
					        "fill-mask": TFAlbertForMaskedLM,
					        "question-answering": TFAlbertForQuestionAnswering,
					        "text-classification": TFAlbertForSequenceClassification,
					        "token-classification": TFAlbertForTokenClassification,
					        "zero-shot": TFAlbertForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)
					SCREAMING_SNAKE_CASE_		:    Any							   =       False
					SCREAMING_SNAKE_CASE_		:    int							   =       False




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

							if return_labels:
									if model_class in get_values(A       ):
											lowercase_			:			Optional[Any]								=       tf.zeros(self.model_tester.batch_size   ,						dtype=tf.intaa       )

							return inputs_dict




					def   A  (    self					:						str       )  ->  List[str]:
							lowercase_			:			int								=       TFAlbertModelTester(self       )
							lowercase_			:			Dict								=       ConfigTester(self   ,						config_class=A   ,						hidden_size=37       )




					def   A  (    self					:						Optional[Any]       )  ->  Any:
							self.config_tester.run_common_tests()




					def   A  (    self					:						Tuple       )  ->  Optional[Any]:
							lowercase_			:			Optional[Any]								=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_model(*A       )




					def   A  (    self					:						Union[str, Any]       )  ->  str:
							lowercase_			:			List[str]								=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_pretraining(*A       )




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




					def   A  (    self					:						Dict       )  ->  Dict:
							lowercase_			:			List[Any]								=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_albert_for_multiple_choice(*A       )




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




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






					@slow
					def   A  (    self					:						Tuple       )  ->  List[str]:
							for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowercase_			:			Dict								=       TFAlbertModel.from_pretrained(A       )
									self.assertIsNotNone(A       )





@require_tf
class  _UpperCAmelCase	(			unittest.TestCase							):




					@slow
					def   A  (    self					:						Any       )  ->  Union[str, Any]:
							lowercase_			:			Union[str, Any]								=       TFAlbertForPreTraining.from_pretrained('''albert-base-v2'''       )
							lowercase_			:			Dict								=       tf.constant([[0, 1, 2, 3, 4, 5]]       )
							lowercase_			:			Union[str, Any]								=       model(A       )[0]

							lowercase_			:			Tuple								=       [1, 6, 3_00_00]
							self.assertEqual(output.shape   ,						A       )

							lowercase_			:			List[Any]								=       tf.constant(
							    [
							        [
							            [4.595668, 0.74462754, -1.818147],
							            [4.5954347, 0.7454184, -1.8188258],
							            [4.5954905, 0.7448235, -1.8182316],
							        ]
							    ]       )
							tf.debugging.assert_near(output[:, :3, :3]   ,						A   ,						atol=1e-4       )











"""simple docstring"""



import argparse
import copy
import json
import multiprocessing
import os
import time

from create_dummy_models import COMPOSITE_MODELS, create_tiny_models
from huggingface_hub import ModelFilter, hf_api

import transformers
from transformers import AutoFeatureExtractor, AutoImageProcessor, AutoTokenizer
from transformers.image_processing_utils import BaseImageProcessor



def 						lowercase   (    ):
		lowercase_			:			Tuple								=       set()
		# Each auto modeling files contains multiple mappings. Let's get them in a dynamic way.
		for module_name in ["modeling_auto", "modeling_tf_auto", "modeling_flax_auto"]:
				lowercase_			:			str								=       getattr(transformers.models.auto		, __snake_case		, __snake_case     )
				if module is None:
						continue
				# all mappings in a single auto modeling file
				lowercase_			:			Tuple								=       [
				    x
				    for x in dir(__snake_case     )
				    if x.endswith('''_MAPPING_NAMES'''     )
				    and (x.startswith('''MODEL_'''     ) or x.startswith('''TF_MODEL_'''     ) or x.startswith('''FLAX_MODEL_'''     ))
				]
				for name in mapping_names:
						lowercase_			:			Tuple								=       getattr(__snake_case		, __snake_case     )
						if mapping is not None:
								for v in mapping.values():
										if isinstance(__snake_case		, (list, tuple)     ):
												model_names.update(__snake_case     )
										elif isinstance(__snake_case		, __snake_case     ):
												model_names.add(__snake_case     )

		return sorted(__snake_case     )



def 						lowercase   (    ):
		# All model names defined in auto mappings
		lowercase_			:			Tuple								=       set(get_all_model_names()     )

		with open('''tests/utils/tiny_model_summary.json'''     ) as fp:
				lowercase_			:			Tuple								=       json.load(__snake_case     )
		lowercase_			:			Union[str, Any]								=       set()
		for model_base_name in tiny_model_info:
				tiny_models_names.update(tiny_model_info[model_base_name]['''model_classes''']     )

		# Remove a tiny model name if one of its framework implementation hasn't yet a tiny version on the Hub.
		lowercase_			:			Union[str, Any]								=       model_names.difference(__snake_case     )
		for model_name in copy.copy(__snake_case     ):
				if not model_name.startswith('''TF'''     ) and F'''TF{model_name}''' in not_on_hub:
						tiny_models_names.remove(__snake_case     )
				elif model_name.startswith('''TF'''     ) and model_name[2:] in not_on_hub:
						tiny_models_names.remove(__snake_case     )

		return sorted(__snake_case     )



def 						lowercase   (    __snake_case    :			List[Any]     ):
		lowercase_			:			List[str]								=       COMPOSITE_MODELS.values()

		# All tiny model base names on Hub
		lowercase_			:			List[Any]								=       get_all_model_names()
		lowercase_			:			Tuple								=       hf_api.list_models(
		    filter=ModelFilter(
		        author='''hf-internal-testing'''		, )     )
		lowercase_			:			Tuple								=       set()
		for x in models:
				lowercase_			:			Tuple								=       x.modelId
				lowercase_	, lowercase_			:			Tuple								=       model.split('''/'''     )
				if not model.startswith('''tiny-random-'''     ):
						continue
				lowercase_			:			List[str]								=       model.replace('''tiny-random-'''		, ''''''     )
				if not model[0].isupper():
						continue
				if model not in model_names and model not in special_models:
						continue
				_models.add(__snake_case     )

		lowercase_			:			Tuple								=       sorted(_models     )
		# All tiny model names on Hub
		lowercase_			:			int								=       {}
		for model in models:
				lowercase_			:			str								=       F'''hf-internal-testing/tiny-random-{model}'''
				lowercase_			:			str								=       model.split('''-'''     )[0]
				try:
						lowercase_			:			Any								=       hf_api.repo_info(__snake_case     )
						lowercase_			:			str								=       {
						    '''tokenizer_classes''': set(),
						    '''processor_classes''': set(),
						    '''model_classes''': set(),
						    '''sha''': repo_info.sha,
						}
				except Exception:
						continue
				try:
						time.sleep(1     )
						lowercase_			:			Any								=       AutoTokenizer.from_pretrained(__snake_case     )
						content["tokenizer_classes"].add(tokenizer_fast.__class__.__name__     )
				except Exception:
						pass
				try:
						time.sleep(1     )
						lowercase_			:			List[str]								=       AutoTokenizer.from_pretrained(__snake_case		, use_fast=__snake_case     )
						content["tokenizer_classes"].add(tokenizer_slow.__class__.__name__     )
				except Exception:
						pass
				try:
						time.sleep(1     )
						lowercase_			:			Union[str, Any]								=       AutoImageProcessor.from_pretrained(__snake_case     )
						content["processor_classes"].add(img_p.__class__.__name__     )
				except Exception:
						pass
				try:
						time.sleep(1     )
						lowercase_			:			Optional[Any]								=       AutoFeatureExtractor.from_pretrained(__snake_case     )
						if not isinstance(__snake_case		, __snake_case     ):
								content["processor_classes"].add(feat_p.__class__.__name__     )
				except Exception:
						pass
				try:
						time.sleep(1     )
						lowercase_			:			Union[str, Any]								=       getattr(__snake_case		, __snake_case     )
						lowercase_			:			Dict								=       model_class.from_pretrained(__snake_case     )
						content["model_classes"].add(m.__class__.__name__     )
				except Exception:
						pass
				try:
						time.sleep(1     )
						lowercase_			:			List[str]								=       getattr(__snake_case		, F'''TF{model}'''     )
						lowercase_			:			List[str]								=       model_class.from_pretrained(__snake_case     )
						content["model_classes"].add(m.__class__.__name__     )
				except Exception:
						pass

				lowercase_			:			Dict								=       sorted(content['''tokenizer_classes''']     )
				lowercase_			:			Any								=       sorted(content['''processor_classes''']     )
				lowercase_			:			Union[str, Any]								=       sorted(content['''model_classes''']     )

				lowercase_			:			str								=       content
				with open(os.path.join(__snake_case		, '''hub_tiny_model_summary.json'''     )		, '''w'''     ) as fp:
						json.dump(__snake_case		, __snake_case		, ensure_ascii=__snake_case		, indent=4     )


if __name__ == "__main__":
						__A		:	Optional[int]      =		argparse.ArgumentParser()
						parser.add_argument('''--num_workers''', default=1, type=int, help='''The number of workers to run.''')
						__A		:	Optional[int]      =		parser.parse_args()

						# This has to be `spawn` to avoid hanging forever!
						multiprocessing.set_start_method('''spawn''')

						__A		:	List[str]      =		'''tiny_models'''
						__A		:	List[str]      =		True
						__A		:	int      =		None
						__A		:	Dict      =		get_tiny_model_names_from_repo()
						__A		:	Union[str, Any]      =		True
						__A		:	str      =		True
						__A		:	Optional[Any]      =		'''hf-internal-testing'''

						create_tiny_models(
						    output_path,
						    all,
						    model_types,
						    models_to_skip,
						    no_check,
						    upload,
						    organization,
						    token=os.environ.get('''TOKEN''', None),
						    num_workers=args.num_workers,
						)



