code: 
  '''simple docstring'''
  
  from dataclasses import dataclass
  from typing import Any, Dict, Optional
  
  import torch
  import torch.nn.functional as F
  from torch import nn
  
  from ..configuration_utils import ConfigMixin, register_to_config
  from ..models.embeddings import ImagePositionalEmbeddings
  from ..utils import BaseOutput, deprecate
  from .attention import BasicTransformerBlock
  from .embeddings import PatchEmbed
  from .modeling_utils import ModelMixin
  
  @dataclass
  class 			UpperCAmelCase				(					a_ ):
  
  
  
  
  
  		"""simple docstring"""
  
  
  
  
  
  		A__  :   torch.FloatTensor
  
  
  
  
  
  
  
  class 			UpperCAmelCase				(					a_  ,		a_ ):
  
  
  
  
  
  		"""simple docstring"""
  
  
  
  
  
  		@register_to_config
  		def __init__(    self  ,					_snake_case = 16  ,					_snake_case = 88  ,					_snake_case = None  ,					_snake_case = None  ,					_snake_case = 1  ,					_snake_case = 0.0  ,					_snake_case = 32  ,					_snake_case = None  ,					_snake_case = False  ,					_snake_case = None  ,					_snake_case = None  ,					_snake_case = None  ,					_snake_case = "geglu"  ,					_snake_case = None  ,					_snake_case = False  ,					_snake_case = False  ,					_snake_case = False  ,					_snake_case = "layer_norm"  ,					_snake_case = True  ,					)				->    int:
  				super().__init__()
  				_UpperCamelCase       : Tuple    =      use_linear_projection
  				_UpperCamelCase       : Tuple    =      num_attention_heads
  				_UpperCamelCase       : List[Any]    =      attention_head_dim
  				_UpperCamelCase       : Union[str, Any]    =      num_attention_heads * attention_head_dim
  
  				# 1. Transformer2DModel can process both standard continuous images of shape `(batch_size, num_channels, width, height)` as well as quantized image embeddings of shape `(batch_size, num_image_vectors)`
  				# Define whether input is continuous or discrete depending on configuration
  				_UpperCamelCase       : int    =      (in_channels is not None) and (patch_size is None)
  				_UpperCamelCase       : Tuple    =      num_vector_embeds is not None
  				_UpperCamelCase       : Optional[Any]    =      in_channels is not None and patch_size is not None
  
  				if norm_type == "layer_norm" and num_embeds_ada_norm is not None:
  						_UpperCamelCase       : int    =      (
  						    F'''The configuration file of this model: {self.__class__} is outdated. `norm_type` is either not set or'''
  						    ''' incorrectly set to `\'layer_norm\'`.Make sure to set `norm_type` to `\'ada_norm\'` in the config.'''
  						    ''' Please make sure to update the config accordingly as leaving `norm_type` might led to incorrect'''
  						    ''' results in future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it'''
  						    ''' would be very nice if you could open a Pull request for the `transformer/config.json` file'''
  						)
  						deprecate('''norm_type!=num_embeds_ada_norm'''  ,					'''1.0.0'''  ,					_snake_case  ,					standard_warn=_snake_case     )
  						_UpperCamelCase       : List[str]    =      '''ada_norm'''
  
  				if self.is_input_continuous and self.is_input_vectorized:
  						raise ValueError(
  						    F'''Cannot define both `in_channels`: {in_channels} and `num_vector_embeds`: {num_vector_embeds}. Make'''
  						    ''' sure that either `in_channels` or `num_vector_embeds` is None.'''     )
  				elif self.is_input_vectorized and self.is_input_patches:
  						raise ValueError(
  						    F'''Cannot define both `num_vector_embeds`: {num_vector_embeds} and `patch_size`: {patch_size}. Make'''
  						    ''' sure that either `num_vector_embeds` or `num_patches` is None.'''     )
  				elif not self.is_input_continuous and not self.is_input_vectorized and not self.is_input_patches:
  						raise ValueError(
  						    F'''Has to define `in_channels`: {in_channels}, `num_vector_embeds`: {num_vector_embeds}, or patch_size:'''
  						    F''' {patch_size}. Make sure that `in_channels`, `num_vector_embeds` or `num_patches` is not None.'''     )
  
  				# 2. Define input layers
  				if self.is_input_continuous:
  						_UpperCamelCase       : List[str]    =      in_channels
  
  						_UpperCamelCase       : Dict    =      torch.nn.GroupNorm(num_groups=_snake_case  ,					num_channels=_snake_case  ,					eps=1E-6  ,					affine=_snake_case     )
  						if use_linear_projection:
  								_UpperCamelCase       : Dict    =      nn.Linear(_snake_case  ,					_snake_case     )
  						else:
  								_UpperCamelCase       : Tuple    =      nn.Convad(_snake_case  ,					_snake_case  ,					kernel_size=1  ,					stride=1  ,					padding=0     )
  				elif self.is_input_vectorized:
  						assert sample_size is not None, "Transformer2DModel over discrete input must provide sample_size"
  						assert num_vector_embeds is not None, "Transformer2DModel over discrete input must provide num_embed"
  
  						_UpperCamelCase       : Dict    =      sample_size
  						_UpperCamelCase       : Any    =      sample_size
  						_UpperCamelCase       : Optional[int]    =      num_vector_embeds
  						_UpperCamelCase       : Tuple    =      self.height * self.width
  
  						_UpperCamelCase       : Union[str, Any]    =      ImagePositionalEmbeddings(
  						    num_embed=_snake_case  ,					embed_dim=_snake_case  ,					height=self.height  ,					width=self.width     )
  				elif self.is_input_patches:
  						assert sample_size is not None, "Transformer2DModel over patched input must provide sample_size"
  
  						_UpperCamelCase       : List[Any]    =      sample_size
  						_UpperCamelCase       : Any    =      sample_size
  
  						_UpperCamelCase       : List[Any]    =      patch_size
  						_UpperCamelCase       : Any    =      PatchEmbed(
  						    height=_snake_case  ,					width=_snake_case  ,					patch_size=_snake_case  ,					in_channels=_snake_case  ,					embed_dim=_snake_case  ,					)
  
  				# 3. Define transformers blocks
  				_UpperCamelCase       : List[Any]    =      nn.ModuleList(
  				    [
  				        BasicTransformerBlock(
  				            _snake_case  ,					_snake_case  ,					_snake_case  ,					dropout=_snake_case  ,					cross_attention_dim=_snake_case  ,					activation_fn=_snake_case  ,					num_embeds_ada_norm=_snake_case  ,					attention_bias=_snake_case  ,					only_cross_attention=_snake_case  ,					upcast_attention=_snake_case  ,					norm_type=_snake_case  ,					norm_elementwise_affine=_snake_case  ,					)
  				        for d in range(_snake_case     )
  				    ]     )
  
  				# 4. Define output layers
  				_UpperCamelCase       : Optional[int]    =      in_channels if out_channels is None else out_channels
  				if self.is_input_continuous:
  						# TODO: should use out_channels for continuous projections
  						if use_linear_projection:
  								_UpperCamelCase       : Union[str, Any]    =      nn.Linear(_snake_case  ,					_snake_case     )
  						else:
  								_UpperCamelCase       : Dict    =      nn.Convad(_snake_case  ,					_snake_case  ,					kernel_size=1  ,					stride=1  ,					padding=0     )
  				elif self.is_input_vectorized:
  						_UpperCamelCase       : List[Any]    =      nn.LayerNorm(_snake_case     )
  						_UpperCamelCase       : List[str]    =      nn.Linear(_snake_case  ,					self.num_vector_embeds - 1     )
  				elif self.is_input_patches:
  						_UpperCamelCase       : Dict    =      nn.LayerNorm(_snake_case  ,					elementwise_affine=_snake_case  ,					eps=1E-6     )
  						_UpperCamelCase       : Union[str, Any]    =      nn.Linear(_snake_case  ,					2 * inner_dim     )
  						_UpperCamelCase       : Tuple    =      nn.Linear(_snake_case  ,					patch_size * patch_size * self.out_channels     )
  
  
  
  
  		def _lowercase  (    self  ,					_snake_case  ,					_snake_case = None  ,					_snake_case = None  ,					_snake_case = None  ,					_snake_case = None  ,					_snake_case = None  ,					_snake_case = None  ,					_snake_case = True  ,					)				->    Any:
  				# ensure attention_mask is a bias, and give it a singleton query_tokens dimension.
  				#   we may have done this conversion already, e.g. if we came here via UNet2DConditionModel#forward.
  				#   we can tell by counting dims; if ndim == 2: it's a mask rather than a bias.
  				# expects mask of shape:
  				#   [batch, key_tokens]
  				# adds singleton query_tokens dimension:
  				#   [batch,                    1, key_tokens]
  				# this helps to broadcast it as a bias over attention scores, which will be in one of the following shapes:
  				#   [batch,  heads, query_tokens, key_tokens] (e.g. torch sdp attn)
  				#   [batch * heads, query_tokens, key_tokens] (e.g. xformers or classic attn)
  				if attention_mask is not None and attention_mask.ndim == 2:
  						# assume that mask is expressed as:
  						#   (1 = keep,      0 = discard)
  						# convert mask into a bias that can be added to attention scores:
  						#       (keep = +0,     discard = -10000.0)
  						_UpperCamelCase       : Optional[Any]    =      (1 - attention_mask.to(hidden_states.dtype     )) * -10_000.0
  						_UpperCamelCase       : List[str]    =      attention_mask.unsqueeze(1     )
  
  				# convert encoder_attention_mask to a bias the same way we do for attention_mask
  				if encoder_attention_mask is not None and encoder_attention_mask.ndim == 2:
  						_UpperCamelCase       : Union[str, Any]    =      (1 - encoder_attention_mask.to(hidden_states.dtype     )) * -10_000.0
  						_UpperCamelCase       : Any    =      encoder_attention_mask.unsqueeze(1     )
  
  				# 1. Input
  				if self.is_input_continuous:
  						_UpperCamelCase,							_UpperCamelCase,							_UpperCamelCase,							_UpperCamelCase       : List[Any]    =      hidden_states.shape
  						_UpperCamelCase       : Dict    =      hidden_states
  
  						_UpperCamelCase       : Optional[Any]    =      self.norm(_snake_case     )
  						if not self.use_linear_projection:
  								_UpperCamelCase       : Optional[int]    =      self.proj_in(_snake_case     )
  								_UpperCamelCase       : Union[str, Any]    =      hidden_states.shape[1]
  								_UpperCamelCase       : Union[str, Any]    =      hidden_states.permute(0  ,					2  ,					3  ,					1     ).reshape(_snake_case  ,					height * width  ,					_snake_case     )
  						else:
  								_UpperCamelCase       : Optional[Any]    =      hidden_states.shape[1]
  								_UpperCamelCase       : Dict    =      hidden_states.permute(0  ,					2  ,					3  ,					1     ).reshape(_snake_case  ,					height * width  ,					_snake_case     )
  								_UpperCamelCase       : List[Any]    =      self.proj_in(_snake_case     )
  				elif self.is_input_vectorized:
  						_UpperCamelCase       : Optional[int]    =      self.latent_image_embedding(_snake_case     )
  				elif self.is_input_patches:
  						_UpperCamelCase       : Optional[int]    =      self.pos_embed(_snake_case     )
  
  				# 2. Blocks
  				for block in self.transformer_blocks:
  						_UpperCamelCase       : Union[str, Any]    =      block(
  						    _snake_case  ,					attention_mask=_snake_case  ,					encoder_hidden_states=_snake_case  ,					encoder_attention_mask=_snake_case  ,					timestep=_snake_case  ,					cross_attention_kwargs=_snake_case  ,					class_labels=_snake_case  ,					)
  
  				# 3. Output
  				if self.is_input_continuous:
  						if not self.use_linear_projection:
  								_UpperCamelCase       : Optional[Any]    =      hidden_states.reshape(_snake_case  ,					_snake_case  ,					_snake_case  ,					_snake_case     ).permute(0  ,					3  ,					1  ,					2     ).contiguous()
  								_UpperCamelCase       : Dict    =      self.proj_out(_snake_case     )
  						else:
  								_UpperCamelCase       : List[Any]    =      self.proj_out(_snake_case     )
  								_UpperCamelCase       : List[str]    =      hidden_states.reshape(_snake_case  ,					_snake_case  ,					_snake_case  ,					_snake_case     ).permute(0  ,					3  ,					1  ,					2     ).contiguous()
  
  						_UpperCamelCase       : str    =      hidden_states + residual
  				elif self.is_input_vectorized:
  						_UpperCamelCase       : Optional[Any]    =      self.norm_out(_snake_case     )
  						_UpperCamelCase       : Any    =      self.out(_snake_case     )
  						# (batch, self.num_vector_embeds - 1, self.num_latent_pixels)
  						_UpperCamelCase       : Optional[Any]    =      logits.permute(0  ,					2  ,					1     )
  
  						# log(p(x_0))
  						_UpperCamelCase       : List[str]    =      F.log_softmax(logits.double()  ,					dim=1     ).float()
  				elif self.is_input_patches:
  						# TODO: cleanup!
  						_UpperCamelCase       : Union[str, Any]    =      self.transformer_blocks[0].norma.emb(
  						    _snake_case  ,					_snake_case  ,					hidden_dtype=hidden_states.dtype     )
  						_UpperCamelCase,							_UpperCamelCase       : List[str]    =      self.proj_out_a(F.silu(_snake_case     )     ).chunk(2  ,					dim=1     )
  						_UpperCamelCase       : List[str]    =      self.norm_out(_snake_case     ) * (1 + scale[:, None]) + shift[:, None]
  						_UpperCamelCase       : List[Any]    =      self.proj_out_a(_snake_case     )
  
  						# unpatchify
  						_UpperCamelCase       : Optional[int]    =      int(hidden_states.shape[1] ** 0.5     )
  						_UpperCamelCase       : Dict    =      hidden_states.reshape(
  						    shape=(-1, height, width, self.patch_size, self.patch_size, self.out_channels)     )
  						_UpperCamelCase       : str    =      torch.einsum('''nhwpqc->nchpwq'''  ,					_snake_case     )
  						_UpperCamelCase       : Union[str, Any]    =      hidden_states.reshape(
  						    shape=(-1, self.out_channels, height * self.patch_size, width * self.patch_size)     )
  
  				if not return_dict:
  						return (output,)
  
  				return TransformeraDModelOutput(sample=_snake_case     )
  
  
  
  
  
  
style_context: 
  '''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 						snake_case__	(      ) ->      List[Any]:
  		_UpperCamelCase       : Union[str, Any]    =      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"]:
  				_UpperCamelCase       : List[Any]    =      getattr(transformers.models.auto			,UpperCamelCase			,UpperCamelCase	)
  				if module is None:
  						continue
  				# all mappings in a single auto modeling file
  				_UpperCamelCase       : Optional[Any]    =      [
  				    x
  				    for x in dir(UpperCamelCase	)
  				    if x.endswith('''_MAPPING_NAMES'''	)
  				    and (x.startswith('''MODEL_'''	) or x.startswith('''TF_MODEL_'''	) or x.startswith('''FLAX_MODEL_'''	))
  				]
  				for name in mapping_names:
  						_UpperCamelCase       : List[str]    =      getattr(UpperCamelCase			,UpperCamelCase	)
  						if mapping is not None:
  								for v in mapping.values():
  										if isinstance(UpperCamelCase			,(list, tuple)	):
  												model_names.update(UpperCamelCase	)
  										elif isinstance(UpperCamelCase			,UpperCamelCase	):
  												model_names.add(UpperCamelCase	)
  
  		return sorted(UpperCamelCase	)
  
  
  
  
  
  
  
  def 						snake_case__	(      ) ->      Any:
  		# All model names defined in auto mappings
  		_UpperCamelCase       : Union[str, Any]    =      set(get_all_model_names()	)
  
  		with open('''tests/utils/tiny_model_summary.json'''	) as fp:
  				_UpperCamelCase       : Optional[Any]    =      json.load(UpperCamelCase	)
  		_UpperCamelCase       : Tuple    =      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.
  		_UpperCamelCase       : List[Any]    =      model_names.difference(UpperCamelCase	)
  		for model_name in copy.copy(UpperCamelCase	):
  				if not model_name.startswith('''TF'''	) and f'''TF{model_name}''' in not_on_hub:
  						tiny_models_names.remove(UpperCamelCase	)
  				elif model_name.startswith('''TF'''	) and model_name[2:] in not_on_hub:
  						tiny_models_names.remove(UpperCamelCase	)
  
  		return sorted(UpperCamelCase	)
  
  
  
  
  
  
  
  def 						snake_case__	(      UpperCamelCase	) ->      List[str]:
  		_UpperCamelCase       : Optional[Any]    =      COMPOSITE_MODELS.values()
  
  		# All tiny model base names on Hub
  		_UpperCamelCase       : Optional[Any]    =      get_all_model_names()
  		_UpperCamelCase       : Union[str, Any]    =      hf_api.list_models(
  		    filter=ModelFilter(
  		        author='''hf-internal-testing'''			,)	)
  		_UpperCamelCase       : Any    =      set()
  		for x in models:
  				_UpperCamelCase       : Optional[int]    =      x.modelId
  				_UpperCamelCase,							_UpperCamelCase       : str    =      model.split('''/'''	)
  				if not model.startswith('''tiny-random-'''	):
  						continue
  				_UpperCamelCase       : Optional[Any]    =      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(UpperCamelCase	)
  
  		_UpperCamelCase       : List[Any]    =      sorted(_models	)
  		# All tiny model names on Hub
  		_UpperCamelCase       : int    =      {}
  		for model in models:
  				_UpperCamelCase       : Optional[Any]    =      f'''hf-internal-testing/tiny-random-{model}'''
  				_UpperCamelCase       : Any    =      model.split('''-'''	)[0]
  				try:
  						_UpperCamelCase       : List[str]    =      hf_api.repo_info(UpperCamelCase	)
  						_UpperCamelCase       : List[str]    =      {
  						    '''tokenizer_classes''': set(),
  						    '''processor_classes''': set(),
  						    '''model_classes''': set(),
  						    '''sha''': repo_info.sha,
  						}
  				except Exception:
  						continue
  				try:
  						time.sleep(1	)
  						_UpperCamelCase       : Union[str, Any]    =      AutoTokenizer.from_pretrained(UpperCamelCase	)
  						content["tokenizer_classes"].add(tokenizer_fast.__class__.__name__	)
  				except Exception:
  						pass
  				try:
  						time.sleep(1	)
  						_UpperCamelCase       : int    =      AutoTokenizer.from_pretrained(UpperCamelCase			,use_fast=UpperCamelCase	)
  						content["tokenizer_classes"].add(tokenizer_slow.__class__.__name__	)
  				except Exception:
  						pass
  				try:
  						time.sleep(1	)
  						_UpperCamelCase       : int    =      AutoImageProcessor.from_pretrained(UpperCamelCase	)
  						content["processor_classes"].add(img_p.__class__.__name__	)
  				except Exception:
  						pass
  				try:
  						time.sleep(1	)
  						_UpperCamelCase       : List[Any]    =      AutoFeatureExtractor.from_pretrained(UpperCamelCase	)
  						if not isinstance(UpperCamelCase			,UpperCamelCase	):
  								content["processor_classes"].add(feat_p.__class__.__name__	)
  				except Exception:
  						pass
  				try:
  						time.sleep(1	)
  						_UpperCamelCase       : Tuple    =      getattr(UpperCamelCase			,UpperCamelCase	)
  						_UpperCamelCase       : Tuple    =      model_class.from_pretrained(UpperCamelCase	)
  						content["model_classes"].add(m.__class__.__name__	)
  				except Exception:
  						pass
  				try:
  						time.sleep(1	)
  						_UpperCamelCase       : Dict    =      getattr(UpperCamelCase			,f'''TF{model}'''	)
  						_UpperCamelCase       : Optional[Any]    =      model_class.from_pretrained(UpperCamelCase	)
  						content["model_classes"].add(m.__class__.__name__	)
  				except Exception:
  						pass
  
  				_UpperCamelCase       : Dict    =      sorted(content['''tokenizer_classes''']	)
  				_UpperCamelCase       : Tuple    =      sorted(content['''processor_classes''']	)
  				_UpperCamelCase       : Union[str, Any]    =      sorted(content['''model_classes''']	)
  
  				_UpperCamelCase       : List[Any]    =      content
  				with open(os.path.join(UpperCamelCase			,'''hub_tiny_model_summary.json'''	)			,'''w'''	) as fp:
  						json.dump(UpperCamelCase			,UpperCamelCase			,ensure_ascii=UpperCamelCase			,indent=4	)
  
  
  if __name__ == "__main__":
  	_UpperCAmelCase   :      List[str] 						=  argparse.ArgumentParser()
  	parser.add_argument("""--num_workers""", default=1, type=int, help="""The number of workers to run.""")
  	_UpperCAmelCase   :      List[Any] 						=  parser.parse_args()
  
  	# This has to be `spawn` to avoid hanging forever!
  	multiprocessing.set_start_method("""spawn""")
  
  	_UpperCAmelCase   :      Optional[Any] 						=  """tiny_models"""
  	_UpperCAmelCase   :      Optional[Any] 						=  True
  	_UpperCAmelCase   :      str 						=  None
  	_UpperCAmelCase   :      int 						=  get_tiny_model_names_from_repo()
  	_UpperCAmelCase   :      Optional[Any] 						=  True
  	_UpperCAmelCase   :      List[str] 						=  True
  	_UpperCAmelCase   :      str 						=  """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,
  	)
  
  
  
  
  
  