




import argparse
import collections
import os
import re
import tempfile

import pandas as pd
from datasets import Dataset
from huggingface_hub import hf_hub_download, upload_folder

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/update_metadata.py
_A     =			'''src/transformers'''


# This is to make sure the transformers module imported is the one in the repo.
_A     =			direct_transformers_import(TRANSFORMERS_PATH)


# Regexes that match TF/Flax/PT model names.
_A     =			re.compile(R'''TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''')
_A     =			re.compile(R'''Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''')
# Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes.
_A     =			re.compile(R'''(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''')


# Fill this with tuples (pipeline_tag, model_mapping, auto_model)
_A     =			[
    ('''pretraining''', '''MODEL_FOR_PRETRAINING_MAPPING_NAMES''', '''AutoModelForPreTraining'''),
    ('''feature-extraction''', '''MODEL_MAPPING_NAMES''', '''AutoModel'''),
    ('''audio-classification''', '''MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForAudioClassification'''),
    ('''text-generation''', '''MODEL_FOR_CAUSAL_LM_MAPPING_NAMES''', '''AutoModelForCausalLM'''),
    ('''automatic-speech-recognition''', '''MODEL_FOR_CTC_MAPPING_NAMES''', '''AutoModelForCTC'''),
    ('''image-classification''', '''MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForImageClassification'''),
    ('''image-segmentation''', '''MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES''', '''AutoModelForImageSegmentation'''),
    ('''fill-mask''', '''MODEL_FOR_MASKED_LM_MAPPING_NAMES''', '''AutoModelForMaskedLM'''),
    ('''object-detection''', '''MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES''', '''AutoModelForObjectDetection'''),
    (
        '''zero-shot-object-detection''',
        '''MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES''',
        '''AutoModelForZeroShotObjectDetection''',
    ),
    ('''question-answering''', '''MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES''', '''AutoModelForQuestionAnswering'''),
    ('''text2text-generation''', '''MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES''', '''AutoModelForSeq2SeqLM'''),
    ('''text-classification''', '''MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForSequenceClassification'''),
    ('''automatic-speech-recognition''', '''MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES''', '''AutoModelForSpeechSeq2Seq'''),
    (
        '''table-question-answering''',
        '''MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES''',
        '''AutoModelForTableQuestionAnswering''',
    ),
    ('''token-classification''', '''MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForTokenClassification'''),
    ('''multiple-choice''', '''MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES''', '''AutoModelForMultipleChoice'''),
    (
        '''next-sentence-prediction''',
        '''MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES''',
        '''AutoModelForNextSentencePrediction''',
    ),
    (
        '''audio-frame-classification''',
        '''MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES''',
        '''AutoModelForAudioFrameClassification''',
    ),
    ('''audio-xvector''', '''MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES''', '''AutoModelForAudioXVector'''),
    (
        '''document-question-answering''',
        '''MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES''',
        '''AutoModelForDocumentQuestionAnswering''',
    ),
    (
        '''visual-question-answering''',
        '''MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING_NAMES''',
        '''AutoModelForVisualQuestionAnswering''',
    ),
    ('''image-to-text''', '''MODEL_FOR_FOR_VISION_2_SEQ_MAPPING_NAMES''', '''AutoModelForVision2Seq'''),
    (
        '''zero-shot-image-classification''',
        '''MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMES''',
        '''AutoModelForZeroShotImageClassification''',
    ),
    ('''depth-estimation''', '''MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES''', '''AutoModelForDepthEstimation'''),
    ('''video-classification''', '''MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForVideoClassification'''),
    ('''mask-generation''', '''MODEL_FOR_MASK_GENERATION_MAPPING_NAMES''', '''AutoModelForMaskGeneration'''),
]





def __UpperCamelCase			(       _A  ):
						lowerCAmelCase_      			=  re.finditer('''.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)''' ,  _A  )
						return [m.group(0  ) for m in matches]





def __UpperCamelCase			(       ):
						lowerCAmelCase_      			=  transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
						lowerCAmelCase_      			=  {
						    config.replace('''Config''' ,  ''''''  ): model_type for model_type, config in config_maping_names.items()
						}

						# Dictionaries flagging if each model prefix has a backend in PT/TF/Flax.
						lowerCAmelCase_      			=  collections.defaultdict(_A  )
						lowerCAmelCase_      			=  collections.defaultdict(_A  )
						lowerCAmelCase_      			=  collections.defaultdict(_A  )

						# Let's lookup through all transformers object (once) and find if models are supported by a given backend.
						for attr_name in dir(_A  ):
												lowerCAmelCase_      			=  None
												if _re_tf_models.match(_A  ) is not None:
																		lowerCAmelCase_      			=  tf_models
																		lowerCAmelCase_      			=  _re_tf_models.match(_A  ).groups()[0]
												elif _re_flax_models.match(_A  ) is not None:
																		lowerCAmelCase_      			=  flax_models
																		lowerCAmelCase_      			=  _re_flax_models.match(_A  ).groups()[0]
												elif _re_pt_models.match(_A  ) is not None:
																		lowerCAmelCase_      			=  pt_models
																		lowerCAmelCase_      			=  _re_pt_models.match(_A  ).groups()[0]

												if lookup_dict is not None:
																		while len(_A  ) > 0:
																								if attr_name in model_prefix_to_model_type:
																														lowerCAmelCase_      			=  True
																														break
																								# Try again after removing the last word in the name
																								lowerCAmelCase_      			=  ''''''.join(camel_case_split(_A  )[:-1]  )

						lowerCAmelCase_      			=  set(list(pt_models.keys()  ) + list(tf_models.keys()  ) + list(flax_models.keys()  )  )
						lowerCAmelCase_      			=  list(_A  )
						all_models.sort()

						lowerCAmelCase_      			=  {'''model_type''': all_models}
						lowerCAmelCase_      			=  [pt_models[t] for t in all_models]
						lowerCAmelCase_      			=  [tf_models[t] for t in all_models]
						lowerCAmelCase_      			=  [flax_models[t] for t in all_models]

						# Now let's use the auto-mapping names to make sure
						lowerCAmelCase_      			=  {}
						for t in all_models:
												if t in transformers_module.models.auto.processing_auto.PROCESSOR_MAPPING_NAMES:
																		lowerCAmelCase_      			=  '''AutoProcessor'''
												elif t in transformers_module.models.auto.tokenization_auto.TOKENIZER_MAPPING_NAMES:
																		lowerCAmelCase_      			=  '''AutoTokenizer'''
												elif t in transformers_module.models.auto.feature_extraction_auto.FEATURE_EXTRACTOR_MAPPING_NAMES:
																		lowerCAmelCase_      			=  '''AutoFeatureExtractor'''
												else:
																		# Default to AutoTokenizer if a model has nothing, for backward compatibility.
																		lowerCAmelCase_      			=  '''AutoTokenizer'''

						lowerCAmelCase_      			=  [processors[t] for t in all_models]

						return pd.DataFrame(_A  )





def __UpperCamelCase			(       _A  ):
						lowerCAmelCase_      			=  [
						    transformers_module.models.auto.modeling_auto,
						    transformers_module.models.auto.modeling_tf_auto,
						    transformers_module.models.auto.modeling_flax_auto,
						]
						for pipeline_tag, model_mapping, auto_class in PIPELINE_TAGS_AND_AUTO_MODELS:
												lowerCAmelCase_      			=  [model_mapping, f"TF_{model_mapping}", f"FLAX_{model_mapping}"]
												lowerCAmelCase_      			=  [auto_class, f"TF_{auto_class}", f"Flax_{auto_class}"]
												# Loop through all three frameworks
												for module, cls, mapping in zip(_A ,  _A ,  _A  ):
																		# The type of pipeline may not exist in this framework
																		if not hasattr(_A ,  _A  ):
																								continue
																		# First extract all model_names
																		lowerCAmelCase_      			=  []
																		for name in getattr(_A ,  _A  ).values():
																								if isinstance(_A ,  _A  ):
																														model_names.append(_A  )
																								else:
																														model_names.extend(list(_A  )  )

            # Add pipeline tag and auto model class for those models
																		table.update({model_name: (pipeline_tag, cls) for model_name in model_names}  )

						return table





def __UpperCamelCase			(       _A ,  _A  ):
						lowerCAmelCase_      			=  get_frameworks_table()
						lowerCAmelCase_      			=  Dataset.from_pandas(_A  )

						lowerCAmelCase_      			=  hf_hub_download(
						    '''huggingface/transformers-metadata''' ,  '''pipeline_tags.json''' ,  repo_type='''dataset''' ,  token=_A  )
						lowerCAmelCase_      			=  Dataset.from_json(_A  )
						lowerCAmelCase_      			=  {
						    tags_dataset[i]['''model_class''']: (tags_dataset[i]['''pipeline_tag'''], tags_dataset[i]['''auto_class'''])
						    for i in range(len(_A  )  )
						}
						lowerCAmelCase_      			=  update_pipeline_and_auto_class_table(_A  )

						# Sort the model classes to avoid some nondeterministic updates to create false update commits.
						lowerCAmelCase_      			=  sorted(table.keys()  )
						lowerCAmelCase_      			=  pd.DataFrame(
						    {
						        '''model_class''': model_classes,
						        '''pipeline_tag''': [table[m][0] for m in model_classes],
						        '''auto_class''': [table[m][1] for m in model_classes],
						    }  )
						lowerCAmelCase_      			=  Dataset.from_pandas(_A  )

						with tempfile.TemporaryDirectory() as tmp_dir:
												frameworks_dataset.to_json(os.path.join(_A ,  '''frameworks.json'''  )  )
												tags_dataset.to_json(os.path.join(_A ,  '''pipeline_tags.json'''  )  )

												if commit_sha is not None:
																		lowerCAmelCase_      			=  (
																		    f"Update with commit {commit_sha}\n\nSee: "
																		    f"https://github.com/huggingface/transformers/commit/{commit_sha}"
																		)
												else:
																		lowerCAmelCase_      			=  '''Update'''

												upload_folder(
												    repo_id='''huggingface/transformers-metadata''' ,  folder_path=_A ,  repo_type='''dataset''' ,  token=_A ,  commit_message=_A ,  )





def __UpperCamelCase			(       ):
						lowerCAmelCase_      			=  {tag: cls for tag, _, cls in PIPELINE_TAGS_AND_AUTO_MODELS}
						lowerCAmelCase_      			=  transformers_module.pipelines.SUPPORTED_TASKS
						lowerCAmelCase_      			=  []
						for key in pipeline_tasks:
												if key not in in_table:
																		lowerCAmelCase_      			=  pipeline_tasks[key]['''pt''']
																		if isinstance(_A ,  (list, tuple)  ):
																								lowerCAmelCase_      			=  model[0]
																		lowerCAmelCase_      			=  model.__name__
																		if model not in in_table.values():
																								missing.append(_A  )

						if len(_A  ) > 0:
												lowerCAmelCase_      			=  ''', '''.join(_A  )
												raise ValueError(
												    '''The following pipeline tags are not present in the `PIPELINE_TAGS_AND_AUTO_MODELS` constant inside '''
												    f"`utils/update_metadata.py`: {msg}. Please add them!"  )


if __name__ == "__main__":
						_A     =			argparse.ArgumentParser()
						parser.add_argument('''--token''', type=str, help='''The token to use to push to the transformers-metadata dataset.''')
						parser.add_argument('''--commit_sha''', type=str, help='''The sha of the commit going with this update.''')
						parser.add_argument('''--check-only''', action='''store_true''', help='''Activate to just check all pipelines are present.''')
						_A     =			parser.parse_args()

						if args.check_only:
												check_pipeline_tags()
						else:
												update_metadata(args.token, args.commit_sha)










import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    SwiftFormerConfig,
    SwiftFormerForImageClassification,
    ViTImageProcessor,
)
from transformers.utils import logging


logging.set_verbosity_info()
_A     =			logging.get_logger(__name__)

_A     =			torch.device('''cpu''')





def __UpperCamelCase			(       ):
						lowerCAmelCase_      			=  '''http://images.cocodataset.org/val2017/000000039769.jpg'''
						lowerCAmelCase_      			=  Image.open(requests.get(_A ,  stream=_A  ).raw  )
						return im





def __UpperCamelCase			(       _A  ):
						if swiftformer_name == "swiftformer_xs":
												return torch.tensor([-2.1_7_0_3E0_0, 2.1_1_0_7E0_0, -2.0_8_1_1E0_0, 8.8_6_8_5E-0_1, 2.4_3_6_0E-0_1]  )

						elif swiftformer_name == "swiftformer_s":
												return torch.tensor([3.9_6_3_6E-0_1, 2.3_4_7_8E-0_1, -1.6_9_6_3E0_0, -1.7_3_8_1E0_0, -8.6_3_3_7E-0_1]  )

						elif swiftformer_name == "swiftformer_l1":
												return torch.tensor([-4.2_7_6_8E-0_1, -4.7_4_2_9E-0_1, -1.0_8_9_7E0_0, -1.0_2_4_8E0_0, 3.5_5_2_3E-0_2]  )

						elif swiftformer_name == "swiftformer_l3":
												return torch.tensor([-2.5_3_3_0E-0_1, 2.4_2_1_1E-0_1, -6.0_1_8_5E-0_1, -8.2_7_8_9E-0_1, -6.0_4_4_6E-0_2]  )





def __UpperCamelCase			(       _A ,  _A ,  _A  ):
						lowerCAmelCase_      			=  dct.pop(_A  )
						lowerCAmelCase_      			=  val





def __UpperCamelCase			(       _A  ):
						lowerCAmelCase_      			=  []
						for k in state_dict.keys():
												lowerCAmelCase_      			=  k
												if ".pwconv" in k:
																		lowerCAmelCase_      			=  k_new.replace('''.pwconv''' ,  '''.point_wise_conv'''  )
												if ".dwconv" in k:
																		lowerCAmelCase_      			=  k_new.replace('''.dwconv''' ,  '''.depth_wise_conv'''  )
												if ".Proj." in k:
																		lowerCAmelCase_      			=  k_new.replace('''.Proj.''' ,  '''.proj.'''  )
												if "patch_embed" in k_new:
																		lowerCAmelCase_      			=  k_new.replace('''patch_embed''' ,  '''swiftformer.patch_embed.patch_embedding'''  )
												if "network" in k_new:
																		lowerCAmelCase_      			=  k_new.split('''.'''  )
																		if ls[2].isdigit():
																								lowerCAmelCase_      			=  '''swiftformer.encoder.network.''' + ls[1] + '''.blocks.''' + ls[2] + '''.''' + '''.'''.join(ls[3:]  )
																		else:
																								lowerCAmelCase_      			=  k_new.replace('''network''' ,  '''swiftformer.encoder.network'''  )
												rename_keys.append((k, k_new)  )
						return rename_keys





@torch.no_grad()
def __UpperCamelCase			(       _A ,  _A ,  _A  ):
						lowerCAmelCase_      			=  SwiftFormerConfig()

						# dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size
						lowerCAmelCase_      			=  1000
						lowerCAmelCase_      			=  '''huggingface/label-files'''
						lowerCAmelCase_      			=  '''imagenet-1k-id2label.json'''
						lowerCAmelCase_      			=  json.load(open(hf_hub_download(_A ,  _A ,  repo_type='''dataset'''  ) ,  '''r'''  )  )
						lowerCAmelCase_      			=  {int(_A  ): v for k, v in idalabel.items()}
						lowerCAmelCase_      			=  idalabel
						lowerCAmelCase_      			=  {v: k for k, v in idalabel.items()}

						# size of the architecture
						if swiftformer_name == "swiftformer_xs":
												lowerCAmelCase_      			=  [3, 3, 6, 4]
												lowerCAmelCase_      			=  [48, 56, 112, 220]

						elif swiftformer_name == "swiftformer_s":
												lowerCAmelCase_      			=  [3, 3, 9, 6]
												lowerCAmelCase_      			=  [48, 64, 168, 224]

						elif swiftformer_name == "swiftformer_l1":
												lowerCAmelCase_      			=  [4, 3, 10, 5]
												lowerCAmelCase_      			=  [48, 96, 192, 384]

						elif swiftformer_name == "swiftformer_l3":
												lowerCAmelCase_      			=  [4, 4, 12, 6]
												lowerCAmelCase_      			=  [64, 128, 320, 512]

						# load state_dict of original model, remove and rename some keys
						if original_ckpt:
												if original_ckpt.startswith('''https'''  ):
																		lowerCAmelCase_      			=  torch.hub.load_state_dict_from_url(_A ,  map_location='''cpu''' ,  check_hash=_A  )
												else:
																		lowerCAmelCase_      			=  torch.load(_A ,  map_location='''cpu'''  )
						lowerCAmelCase_      			=  checkpoint

						lowerCAmelCase_      			=  create_rename_keys(_A  )
						for rename_key_src, rename_key_dest in rename_keys:
												rename_key(_A ,  _A ,  _A  )

						# load HuggingFace model
						lowerCAmelCase_      			=  SwiftFormerForImageClassification(_A  ).eval()
						hf_model.load_state_dict(_A  )

						# prepare test inputs
						lowerCAmelCase_      			=  prepare_img()
						lowerCAmelCase_      			=  ViTImageProcessor.from_pretrained('''preprocessor_config'''  )
						lowerCAmelCase_      			=  processor(images=_A ,  return_tensors='''pt'''  )

						# compare outputs from both models
						lowerCAmelCase_      			=  get_expected_output(_A  )
						lowerCAmelCase_      			=  hf_model(inputs['''pixel_values''']  ).logits

						assert hf_logits.shape == torch.Size([1, 1000]  )
						assert torch.allclose(hf_logits[0, 0:5] ,  _A ,  atol=1E-3  )

						Path(_A  ).mkdir(exist_ok=_A  )
						print(f"Saving model {swiftformer_name} to {pytorch_dump_folder_path}"  )
						hf_model.save_pretrained(_A  )


if __name__ == "__main__":
						_A     =			argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    '''--swiftformer_name''',
						    default='''swiftformer_xs''',
						    choices=['''swiftformer_xs''', '''swiftformer_s''', '''swiftformer_l1''', '''swiftformer_l3'''],
						    type=str,
						    help='''Name of the SwiftFormer model you\'d like to convert.''',
						)
						parser.add_argument(
						    '''--pytorch_dump_folder_path''',
						    default='''./converted_outputs/''',
						    type=str,
						    help='''Path to the output PyTorch model directory.''',
						)
						parser.add_argument('''--original_ckpt''', default=None, type=str, help='''Path to the original model checkpoint.''')

						_A     =			parser.parse_args()
						convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)




