






"""simple docstring"""



import gc
import os
import tempfile
import unittest

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, StableDiffusionPipeline, UNetaDConditionModel
from diffusers.loaders import AttnProcsLayers, LoraLoaderMixin
from diffusers.models.attention_processor import (
    Attention,
    AttnProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRAAttnProcessor2_0,
    LoRAXFormersAttnProcessor,
    XFormersAttnProcessor,
)
from diffusers.utils import TEXT_ENCODER_ATTN_MODULE, floats_tensor, torch_device



def 						lowercase   (    __snake_case    :			nn.Module     ):
		lowercase_			:			List[Any]								=       {}
		for name in unet.attn_processors.keys():
				lowercase_			:			List[Any]								=       None if name.endswith('''attn1.processor'''     ) else unet.config.cross_attention_dim
				if name.startswith('''mid_block'''     ):
						lowercase_			:			int								=       unet.config.block_out_channels[-1]
				elif name.startswith('''up_blocks'''     ):
						lowercase_			:			List[str]								=       int(name[len('''up_blocks.'''     )]     )
						lowercase_			:			Tuple								=       list(reversed(unet.config.block_out_channels     )     )[block_id]
				elif name.startswith('''down_blocks'''     ):
						lowercase_			:			str								=       int(name[len('''down_blocks.'''     )]     )
						lowercase_			:			List[Any]								=       unet.config.block_out_channels[block_id]
				lowercase_			:			List[Any]								=       (
				    LoRAAttnProcessor2_0 if hasattr(__snake_case		, '''scaled_dot_product_attention'''     ) else LoRAAttnProcessor
				)
				lowercase_			:			str								=       lora_attn_processor_class(
				    hidden_size=__snake_case		, cross_attention_dim=__snake_case     )
		lowercase_			:			Optional[int]								=       AttnProcsLayers(__snake_case     )
		return lora_attn_procs, unet_lora_layers



def 						lowercase   (    __snake_case    :			nn.Module     ):
		lowercase_			:			Union[str, Any]								=       {}
		lowercase_			:			Optional[int]								=       (
		    LoRAAttnProcessor2_0 if hasattr(__snake_case		, '''scaled_dot_product_attention'''     ) else LoRAAttnProcessor
		)
		for name, module in text_encoder.named_modules():
				if name.endswith(__snake_case     ):
						lowercase_			:			Union[str, Any]								=       lora_attn_processor_class(
						    hidden_size=module.out_proj.out_features		, cross_attention_dim=__snake_case     )
		return text_lora_attn_procs



def 						lowercase   (    __snake_case    :			nn.Module     ):
		lowercase_			:			Tuple								=       create_text_encoder_lora_attn_procs(__snake_case     )
		lowercase_			:			str								=       AttnProcsLayers(__snake_case     )
		return text_encoder_lora_layers



def 						lowercase   (    __snake_case    :			Tuple		, __snake_case    :			Dict=False     ):
		for _, attn_proc in text_lora_attn_procs.items():
				# set up.weights
				for layer_name, layer_module in attn_proc.named_modules():
						if layer_name.endswith('''_lora'''     ):
								lowercase_			:			Tuple								=       (
								    torch.randn_like(layer_module.up.weight     )
								    if randn_weight
								    else torch.zeros_like(layer_module.up.weight     )
								)
								lowercase_			:			Dict								=       torch.nn.Parameter(__snake_case     )






class  _UpperCAmelCase	(			unittest.TestCase							):




					def   A  (    self					:						int       )  ->  Optional[int]:
							torch.manual_seed(0       )
							lowercase_			:			str								=       UNetaDConditionModel(
							    block_out_channels=(32, 64)   ,						layers_per_block=2   ,						sample_size=32   ,						in_channels=4   ,						out_channels=4   ,						down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')   ,						up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')   ,						cross_attention_dim=32   ,						)
							lowercase_			:			int								=       DDIMScheduler(
							    beta_start=0.00085   ,						beta_end=0.012   ,						beta_schedule='''scaled_linear'''   ,						clip_sample=A   ,						set_alpha_to_one=A   ,						steps_offset=1   ,						)
							torch.manual_seed(0       )
							lowercase_			:			Tuple								=       AutoencoderKL(
							    block_out_channels=[32, 64]   ,						in_channels=3   ,						out_channels=3   ,						down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']   ,						up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']   ,						latent_channels=4   ,						)
							lowercase_			:			Tuple								=       CLIPTextConfig(
							    bos_token_id=0   ,						eos_token_id=2   ,						hidden_size=32   ,						intermediate_size=37   ,						layer_norm_eps=1e-05   ,						num_attention_heads=4   ,						num_hidden_layers=5   ,						pad_token_id=1   ,						vocab_size=10_00   ,						)
							lowercase_			:			Tuple								=       CLIPTextModel(A       )
							lowercase_			:			int								=       CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''       )

							lowercase_	, lowercase_			:			Dict								=       create_unet_lora_layers(A       )
							lowercase_			:			Dict								=       create_text_encoder_lora_layers(A       )

							lowercase_			:			Any								=       {
							    '''unet''': unet,
							    '''scheduler''': scheduler,
							    '''vae''': vae,
							    '''text_encoder''': text_encoder,
							    '''tokenizer''': tokenizer,
							    '''safety_checker''': None,
							    '''feature_extractor''': None,
							}
							lowercase_			:			List[str]								=       {
							    '''unet_lora_layers''': unet_lora_layers,
							    '''text_encoder_lora_layers''': text_encoder_lora_layers,
							    '''unet_lora_attn_procs''': unet_lora_attn_procs,
							}
							return pipeline_components, lora_components




					def   A  (    self					:						Optional[Any]       )  ->  Dict:
							lowercase_			:			Any								=       1
							lowercase_			:			int								=       10
							lowercase_			:			Optional[Any]								=       4
							lowercase_			:			List[Any]								=       (32, 32)

							lowercase_			:			List[str]								=       torch.manual_seed(0       )
							lowercase_			:			Optional[int]								=       floats_tensor((batch_size, num_channels) + sizes       )
							lowercase_			:			List[Any]								=       torch.randint(1   ,						A   ,						size=(batch_size, sequence_length)   ,						generator=A       )

							lowercase_			:			Optional[int]								=       {
							    '''prompt''': '''A painting of a squirrel eating a burger''',
							    '''generator''': generator,
							    '''num_inference_steps''': 2,
							    '''guidance_scale''': 6.0,
							    '''output_type''': '''numpy''',
							}

							return noise, input_ids, pipeline_inputs

							# copied from: https://colab.research.google.com/gist/sayakpaul/df2ef6e1ae6d8c10a49d859883b10860/scratchpad.ipynb




					def   A  (    self					:						Any       )  ->  Optional[int]:
							lowercase_			:			Optional[Any]								=       77

							lowercase_			:			str								=       torch.randint(2   ,						56   ,						size=(1, max_seq_length)   ,						generator=torch.manual_seed(0       )       )

							lowercase_			:			Optional[int]								=       {}
							lowercase_			:			int								=       inputs
							return prepared_inputs




					def   A  (    self					:						Any   ,						A					:						Tuple       )  ->  Union[str, Any]:
							lowercase_	, lowercase_			:			Optional[Any]								=       self.get_dummy_components()
							LoraLoaderMixin.save_lora_weights(
							    save_directory=A   ,						unet_lora_layers=lora_components['''unet_lora_layers''']   ,						text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']   ,						)
							self.assertTrue(os.path.isfile(os.path.join(A   ,						'''pytorch_lora_weights.bin'''       )       )       )




					def   A  (    self					:						Tuple       )  ->  Dict:
							lowercase_	, lowercase_			:			List[str]								=       self.get_dummy_components()
							lowercase_			:			Optional[int]								=       StableDiffusionPipeline(**A       )
							lowercase_			:			Optional[int]								=       sd_pipe.to(A       )
							sd_pipe.set_progress_bar_config(disable=A       )

							lowercase_	, lowercase_	, lowercase_			:			Tuple								=       self.get_dummy_inputs()

							lowercase_			:			int								=       sd_pipe(**A       ).images
							lowercase_			:			Optional[int]								=       original_images[0, -3:, -3:, -1]

							with tempfile.TemporaryDirectory() as tmpdirname:
									LoraLoaderMixin.save_lora_weights(
									    save_directory=A   ,						unet_lora_layers=lora_components['''unet_lora_layers''']   ,						text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']   ,						)
									self.assertTrue(os.path.isfile(os.path.join(A   ,						'''pytorch_lora_weights.bin'''       )       )       )
									sd_pipe.load_lora_weights(A       )

							lowercase_			:			str								=       sd_pipe(**A       ).images
							lowercase_			:			str								=       lora_images[0, -3:, -3:, -1]

							# Outputs shouldn't match.
							self.assertFalse(torch.allclose(torch.from_numpy(A       )   ,						torch.from_numpy(A       )       )       )




					def   A  (    self					:						Any       )  ->  Optional[int]:
							lowercase_	, lowercase_			:			int								=       self.get_dummy_components()
							lowercase_			:			Dict								=       StableDiffusionPipeline(**A       )
							lowercase_			:			Tuple								=       sd_pipe.to(A       )
							sd_pipe.set_progress_bar_config(disable=A       )

							lowercase_	, lowercase_	, lowercase_			:			Union[str, Any]								=       self.get_dummy_inputs()

							lowercase_			:			Optional[Any]								=       sd_pipe(**A       ).images
							lowercase_			:			str								=       original_images[0, -3:, -3:, -1]

							with tempfile.TemporaryDirectory() as tmpdirname:
									LoraLoaderMixin.save_lora_weights(
									    save_directory=A   ,						unet_lora_layers=lora_components['''unet_lora_layers''']   ,						text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']   ,						safe_serialization=A   ,						)
									self.assertTrue(os.path.isfile(os.path.join(A   ,						'''pytorch_lora_weights.safetensors'''       )       )       )
									sd_pipe.load_lora_weights(A       )

							lowercase_			:			Optional[Any]								=       sd_pipe(**A       ).images
							lowercase_			:			str								=       lora_images[0, -3:, -3:, -1]

							# Outputs shouldn't match.
							self.assertFalse(torch.allclose(torch.from_numpy(A       )   ,						torch.from_numpy(A       )       )       )




					def   A  (    self					:						Optional[int]       )  ->  Optional[int]:
							lowercase_	, lowercase_			:			Optional[int]								=       self.get_dummy_components()
							lowercase_			:			Optional[int]								=       lora_components['''unet_lora_attn_procs''']
							lowercase_			:			Tuple								=       StableDiffusionPipeline(**A       )
							lowercase_			:			Optional[Any]								=       sd_pipe.to(A       )
							sd_pipe.set_progress_bar_config(disable=A       )

							lowercase_	, lowercase_	, lowercase_			:			Optional[Any]								=       self.get_dummy_inputs()

							lowercase_			:			List[str]								=       sd_pipe(**A       ).images
							lowercase_			:			Dict								=       original_images[0, -3:, -3:, -1]

							with tempfile.TemporaryDirectory() as tmpdirname:
									lowercase_			:			int								=       sd_pipe.unet
									unet.set_attn_processor(A       )
									unet.save_attn_procs(A       )
									self.assertTrue(os.path.isfile(os.path.join(A   ,						'''pytorch_lora_weights.bin'''       )       )       )
									sd_pipe.load_lora_weights(A       )

							lowercase_			:			Union[str, Any]								=       sd_pipe(**A       ).images
							lowercase_			:			List[Any]								=       lora_images[0, -3:, -3:, -1]

							# Outputs shouldn't match.
							self.assertFalse(torch.allclose(torch.from_numpy(A       )   ,						torch.from_numpy(A       )       )       )




					def   A  (    self					:						Dict       )  ->  List[Any]:
							lowercase_	, lowercase_			:			List[Any]								=       self.get_dummy_components()
							lowercase_			:			Any								=       StableDiffusionPipeline(**A       )

							lowercase_			:			Union[str, Any]								=       self.get_dummy_tokens()

							# inference without lora
							lowercase_			:			List[str]								=       pipe.text_encoder(**A       )[0]
							assert outputs_without_lora.shape == (1, 77, 32)

							# create lora_attn_procs with zeroed out up.weights
							lowercase_			:			Optional[int]								=       create_text_encoder_lora_attn_procs(pipe.text_encoder       )
							set_lora_up_weights(A   ,						randn_weight=A       )

							# monkey patch
							pipe._modify_text_encoder(A       )

							# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
							del text_attn_procs
							gc.collect()

							# inference with lora
							lowercase_			:			List[str]								=       pipe.text_encoder(**A       )[0]
							assert outputs_with_lora.shape == (1, 77, 32)

							assert torch.allclose(
							    A   ,						A       ), "lora_up_weight are all zero, so the lora outputs should be the same to without lora outputs"

							# create lora_attn_procs with randn up.weights
							lowercase_			:			int								=       create_text_encoder_lora_attn_procs(pipe.text_encoder       )
							set_lora_up_weights(A   ,						randn_weight=A       )

							# monkey patch
							pipe._modify_text_encoder(A       )

							# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
							del text_attn_procs
							gc.collect()

							# inference with lora
							lowercase_			:			Optional[int]								=       pipe.text_encoder(**A       )[0]
							assert outputs_with_lora.shape == (1, 77, 32)

							assert not torch.allclose(
							    A   ,						A       ), "lora_up_weight are not zero, so the lora outputs should be different to without lora outputs"




					def   A  (    self					:						Optional[Any]       )  ->  Any:
							lowercase_	, lowercase_			:			int								=       self.get_dummy_components()
							lowercase_			:			Union[str, Any]								=       StableDiffusionPipeline(**A       )

							lowercase_			:			Optional[Any]								=       self.get_dummy_tokens()

							# inference without lora
							lowercase_			:			Optional[Any]								=       pipe.text_encoder(**A       )[0]
							assert outputs_without_lora.shape == (1, 77, 32)

							# create lora_attn_procs with randn up.weights
							lowercase_			:			Optional[int]								=       create_text_encoder_lora_attn_procs(pipe.text_encoder       )
							set_lora_up_weights(A   ,						randn_weight=A       )

							# monkey patch
							pipe._modify_text_encoder(A       )

							# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
							del text_attn_procs
							gc.collect()

							# inference with lora
							lowercase_			:			Any								=       pipe.text_encoder(**A       )[0]
							assert outputs_with_lora.shape == (1, 77, 32)

							assert not torch.allclose(
							    A   ,						A       ), "lora outputs should be different to without lora outputs"

							# remove monkey patch
							pipe._remove_text_encoder_monkey_patch()

							# inference with removed lora
							lowercase_			:			List[str]								=       pipe.text_encoder(**A       )[0]
							assert outputs_without_lora_removed.shape == (1, 77, 32)

							assert torch.allclose(
							    A   ,						A       ), "remove lora monkey patch should restore the original outputs"




					def   A  (    self					:						Optional[int]       )  ->  Optional[Any]:
							lowercase_	, lowercase_			:			Dict								=       self.get_dummy_components()
							lowercase_			:			Optional[int]								=       StableDiffusionPipeline(**A       )
							lowercase_			:			Optional[int]								=       sd_pipe.to(A       )
							sd_pipe.set_progress_bar_config(disable=A       )

							lowercase_	, lowercase_	, lowercase_			:			List[str]								=       self.get_dummy_inputs()

							with tempfile.TemporaryDirectory() as tmpdirname:
									LoraLoaderMixin.save_lora_weights(
									    save_directory=A   ,						unet_lora_layers=lora_components['''unet_lora_layers''']   ,						text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']   ,						)
									self.assertTrue(os.path.isfile(os.path.join(A   ,						'''pytorch_lora_weights.bin'''       )       )       )
									sd_pipe.load_lora_weights(A       )

							lowercase_			:			List[str]								=       sd_pipe(**A       ).images
							lowercase_			:			int								=       lora_images[0, -3:, -3:, -1]

							lowercase_			:			int								=       sd_pipe(**A   ,						cross_attention_kwargs={'''scale''': 0.5}       ).images
							lowercase_			:			Dict								=       lora_images_with_scale[0, -3:, -3:, -1]

							# Outputs shouldn't match.
							self.assertFalse(
							    torch.allclose(torch.from_numpy(A       )   ,						torch.from_numpy(A       )       )       )




					def   A  (    self					:						str       )  ->  List[Any]:
							with tempfile.TemporaryDirectory() as tmpdirname:
									self.create_lora_weight_file(A       )

									lowercase_	, lowercase_			:			Optional[int]								=       self.get_dummy_components()
									lowercase_			:			int								=       StableDiffusionPipeline(**A       )
									lowercase_			:			Optional[int]								=       sd_pipe.to(A       )
									sd_pipe.set_progress_bar_config(disable=A       )

									# check if vanilla attention processors are used
									for _, module in sd_pipe.unet.named_modules():
											if isinstance(A   ,						A       ):
													self.assertIsInstance(module.processor   ,						(AttnProcessor, AttnProcessor2_0)       )

            # load LoRA weight file
									sd_pipe.load_lora_weights(A       )

									# check if lora attention processors are used
									for _, module in sd_pipe.unet.named_modules():
											if isinstance(A   ,						A       ):
													lowercase_			:			str								=       (
													    LoRAAttnProcessor2_0 if hasattr(A   ,						'''scaled_dot_product_attention'''       ) else LoRAAttnProcessor
													)
													self.assertIsInstance(module.processor   ,						A       )




					@unittest.skipIf(torch_device != '''cuda'''   ,						'''This test is supposed to run on GPU'''       )
					def   A  (    self					:						str       )  ->  Tuple:
							with tempfile.TemporaryDirectory() as tmpdirname:
									self.create_lora_weight_file(A       )

									lowercase_	, lowercase_			:			List[str]								=       self.get_dummy_components()
									lowercase_			:			List[str]								=       StableDiffusionPipeline(**A       )
									lowercase_			:			str								=       sd_pipe.to(A       )
									sd_pipe.set_progress_bar_config(disable=A       )

									# enable XFormers
									sd_pipe.enable_xformers_memory_efficient_attention()

									# check if xFormers attention processors are used
									for _, module in sd_pipe.unet.named_modules():
											if isinstance(A   ,						A       ):
													self.assertIsInstance(module.processor   ,						A       )

            # load LoRA weight file
									sd_pipe.load_lora_weights(A       )

									# check if lora attention processors are used
									for _, module in sd_pipe.unet.named_modules():
											if isinstance(A   ,						A       ):
													self.assertIsInstance(module.processor   ,						A       )






					@unittest.skipIf(torch_device != '''cuda'''   ,						'''This test is supposed to run on GPU'''       )
					def   A  (    self					:						List[Any]       )  ->  Any:
							lowercase_	, lowercase_			:			List[Any]								=       self.get_dummy_components()
							lowercase_			:			Tuple								=       StableDiffusionPipeline(**A       )
							lowercase_			:			Any								=       sd_pipe.to(A       )
							sd_pipe.set_progress_bar_config(disable=A       )

							lowercase_	, lowercase_	, lowercase_			:			List[str]								=       self.get_dummy_inputs()

							# enable XFormers
							sd_pipe.enable_xformers_memory_efficient_attention()

							lowercase_			:			Optional[Any]								=       sd_pipe(**A       ).images
							lowercase_			:			List[Any]								=       original_images[0, -3:, -3:, -1]

							with tempfile.TemporaryDirectory() as tmpdirname:
									LoraLoaderMixin.save_lora_weights(
									    save_directory=A   ,						unet_lora_layers=lora_components['''unet_lora_layers''']   ,						text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']   ,						)
									self.assertTrue(os.path.isfile(os.path.join(A   ,						'''pytorch_lora_weights.bin'''       )       )       )
									sd_pipe.load_lora_weights(A       )

							lowercase_			:			Optional[int]								=       sd_pipe(**A       ).images
							lowercase_			:			List[str]								=       lora_images[0, -3:, -3:, -1]

							# Outputs shouldn't match.
							self.assertFalse(torch.allclose(torch.from_numpy(A       )   ,						torch.from_numpy(A       )       )       )











"""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,
						)



