


"""simple docstring"""






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()
UpperCAmelCase    :     Dict   						=		logging.get_logger(__name__)

UpperCAmelCase    :     Union[str, Any]   						=		torch.device('cpu')
def      lowerCamelCase       (     )		->       str:

							'''simple docstring'''



							__UpperCAmelCase   :						Optional[int]      							=					"""http://images.cocodataset.org/val2017/000000039769.jpg"""
							__UpperCAmelCase   :						Optional[Any]      							=					Image.open(requests.get(_UpperCamelCase       ,			stream=_UpperCamelCase							).raw							)
							return im
def      lowerCamelCase       (     _UpperCamelCase		:      Union[str, Any]							)		->       Union[str, Any]:

							'''simple docstring'''



							if swiftformer_name == "swiftformer_xs":
														return torch.tensor([-2.1_703E00, 2.1_107E00, -2.0_811E00, 8.8_685E-01, 2.4_360E-01]							)

							elif swiftformer_name == "swiftformer_s":
														return torch.tensor([3.9_636E-01, 2.3_478E-01, -1.6_963E00, -1.7_381E00, -8.6_337E-01]							)

							elif swiftformer_name == "swiftformer_l1":
														return torch.tensor([-4.2_768E-01, -4.7_429E-01, -1.0_897E00, -1.0_248E00, 3.5_523E-02]							)

							elif swiftformer_name == "swiftformer_l3":
														return torch.tensor([-2.5_330E-01, 2.4_211E-01, -6.0_185E-01, -8.2_789E-01, -6.0_446E-02]							)
def      lowerCamelCase       (     _UpperCamelCase		:      Union[str, Any]       ,			_UpperCamelCase		:      Optional[int]       ,			_UpperCamelCase		:      List[Any]							)		->       Union[str, Any]:

							'''simple docstring'''



							__UpperCAmelCase   :						Any      							=					dct.pop(_UpperCamelCase							)
							__UpperCAmelCase   :						List[str]      							=					val
def      lowerCamelCase       (     _UpperCamelCase		:      List[Any]							)		->       List[Any]:

							'''simple docstring'''



							__UpperCAmelCase   :						Tuple      							=					[]
							for k in state_dict.keys():
														__UpperCAmelCase   :						int      							=					k
														if ".pwconv" in k:
																					__UpperCAmelCase   :						Optional[int]      							=					k_new.replace(""".pwconv"""       ,			""".point_wise_conv"""							)
														if ".dwconv" in k:
																					__UpperCAmelCase   :						List[Any]      							=					k_new.replace(""".dwconv"""       ,			""".depth_wise_conv"""							)
														if ".Proj." in k:
																					__UpperCAmelCase   :						str      							=					k_new.replace(""".Proj."""       ,			""".proj."""							)
														if "patch_embed" in k_new:
																					__UpperCAmelCase   :						int      							=					k_new.replace("""patch_embed"""       ,			"""swiftformer.patch_embed.patch_embedding"""							)
														if "network" in k_new:
																					__UpperCAmelCase   :						str      							=					k_new.split("""."""							)
																					if ls[2].isdigit():
																												__UpperCAmelCase   :						Dict      							=					"""swiftformer.encoder.network.""" + ls[1] + """.blocks.""" + ls[2] + """.""" + """.""".join(ls[3:]							)
																					else:
																												__UpperCAmelCase   :						List[str]      							=					k_new.replace("""network"""       ,			"""swiftformer.encoder.network"""							)
														rename_keys.append((k, k_new)							)
							return rename_keys
@torch.no_grad()
def      lowerCamelCase       (     _UpperCamelCase		:      int       ,			_UpperCamelCase		:      List[Any]       ,			_UpperCamelCase		:      int							)		->       Union[str, Any]:

							'''simple docstring'''



							__UpperCAmelCase   :						Optional[int]      							=					SwiftFormerConfig()

							# dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size
							__UpperCAmelCase   :						Optional[int]      							=					1_0_0_0
							__UpperCAmelCase   :						Optional[int]      							=					"""huggingface/label-files"""
							__UpperCAmelCase   :						str      							=					"""imagenet-1k-id2label.json"""
							__UpperCAmelCase   :						List[Any]      							=					json.load(open(hf_hub_download(_UpperCamelCase       ,			_UpperCamelCase       ,			repo_type="""dataset"""							)       ,			"""r"""							)							)
							__UpperCAmelCase   :						Any      							=					{int(_UpperCamelCase							): v for k, v in idalabel.items()}
							__UpperCAmelCase   :						Optional[Any]      							=					idalabel
							__UpperCAmelCase   :						Optional[int]      							=					{v: k for k, v in idalabel.items()}

							# size of the architecture
							if swiftformer_name == "swiftformer_xs":
														__UpperCAmelCase   :						Tuple      							=					[3, 3, 6, 4]
														__UpperCAmelCase   :						List[Any]      							=					[4_8, 5_6, 1_1_2, 2_2_0]

							elif swiftformer_name == "swiftformer_s":
														__UpperCAmelCase   :						Optional[int]      							=					[3, 3, 9, 6]
														__UpperCAmelCase   :						Tuple      							=					[4_8, 6_4, 1_6_8, 2_2_4]

							elif swiftformer_name == "swiftformer_l1":
														__UpperCAmelCase   :						Dict      							=					[4, 3, 1_0, 5]
														__UpperCAmelCase   :						Optional[Any]      							=					[4_8, 9_6, 1_9_2, 3_8_4]

							elif swiftformer_name == "swiftformer_l3":
														__UpperCAmelCase   :						str      							=					[4, 4, 1_2, 6]
														__UpperCAmelCase   :						str      							=					[6_4, 1_2_8, 3_2_0, 5_1_2]

							# load state_dict of original model, remove and rename some keys
							if original_ckpt:
														if original_ckpt.startswith("""https"""							):
																					__UpperCAmelCase   :						str      							=					torch.hub.load_state_dict_from_url(_UpperCamelCase       ,			map_location="""cpu"""       ,			check_hash=_UpperCamelCase							)
														else:
																					__UpperCAmelCase   :						Dict      							=					torch.load(_UpperCamelCase       ,			map_location="""cpu"""							)
							__UpperCAmelCase   :						str      							=					checkpoint

							__UpperCAmelCase   :						Union[str, Any]      							=					create_rename_keys(_UpperCamelCase							)
							for rename_key_src, rename_key_dest in rename_keys:
														rename_key(_UpperCamelCase       ,			_UpperCamelCase       ,			_UpperCamelCase							)

							# load HuggingFace model
							__UpperCAmelCase   :						int      							=					SwiftFormerForImageClassification(_UpperCamelCase							).eval()
							hf_model.load_state_dict(_UpperCamelCase							)

							# prepare test inputs
							__UpperCAmelCase   :						List[Any]      							=					prepare_img()
							__UpperCAmelCase   :						int      							=					ViTImageProcessor.from_pretrained("""preprocessor_config"""							)
							__UpperCAmelCase   :						Optional[Any]      							=					processor(images=_UpperCamelCase       ,			return_tensors="""pt"""							)

							# compare outputs from both models
							__UpperCAmelCase   :						Tuple      							=					get_expected_output(_UpperCamelCase							)
							__UpperCAmelCase   :						Dict      							=					hf_model(inputs["""pixel_values"""]							).logits

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

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


if __name__ == "__main__":
							UpperCAmelCase    :     Optional[Any]   						=		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.')

							UpperCAmelCase    :     Tuple   						=		parser.parse_args()
							convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)




"""simple docstring"""






from importlib import import_module

from .logging import get_logger


UpperCAmelCase    :     Any   						=		get_logger(__name__)





class      lowerCamelCase__   :







					"""simple docstring"""


					def __init__(       self    : Optional[int] ,       UpperCamelCase    : List[Any] ,       UpperCamelCase    : int=None  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Tuple      							=					attrs or []
												if module is not None:
																			for key in module.__dict__:
																										if key in attrs or not key.startswith("""__"""  ):
																																	setattr(self ,       UpperCamelCase ,       getattr(UpperCamelCase ,       UpperCamelCase  )  )
												__UpperCAmelCase   :						Any      							=					module._original_module if isinstance(UpperCamelCase ,       _PatchedModuleObj  ) else module






class      lowerCamelCase__   :







					"""simple docstring"""


					__a	       =							[]






					def __init__(       self    : str ,       UpperCamelCase    : Tuple ,       UpperCamelCase    : str ,       UpperCamelCase    : List[str] ,       UpperCamelCase    : Optional[Any]=None  ):
												'''simple docstring'''






												__UpperCAmelCase   :						int      							=					obj
												__UpperCAmelCase   :						Union[str, Any]      							=					target
												__UpperCAmelCase   :						List[str]      							=					new
												__UpperCAmelCase   :						Optional[int]      							=					target.split("""."""  )[0]
												__UpperCAmelCase   :						Tuple      							=					{}
												__UpperCAmelCase   :						Union[str, Any]      							=					attrs or []






					def __enter__(       self    : Dict  ):
												'''simple docstring'''






												*__UpperCAmelCase							,__UpperCAmelCase   :						str      							=					self.target.split("""."""  )

												# Patch modules:
												# it's used to patch attributes of submodules like "os.path.join";
												# in this case we need to patch "os" and "os.path"

												for i in range(len(UpperCamelCase  )  ):
																			try:
																										__UpperCAmelCase   :						List[Any]      							=					import_module(""".""".join(submodules[: i + 1]  )  )
																			except ModuleNotFoundError:
																										continue
																			# We iterate over all the globals in self.obj in case we find "os" or "os.path"
																			for attr in self.obj.__dir__():
																										__UpperCAmelCase   :						List[Any]      							=					getattr(self.obj ,       UpperCamelCase  )
																										# We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
																										# This allows to patch renamed modules like "from os import path as ospath".
																										if obj_attr is submodule or (
																										    (isinstance(UpperCamelCase ,       _PatchedModuleObj  ) and obj_attr._original_module is submodule)
																										):
																																	__UpperCAmelCase   :						Tuple      							=					obj_attr
																																	# patch at top level
																																	setattr(self.obj ,       UpperCamelCase ,       _PatchedModuleObj(UpperCamelCase ,       attrs=self.attrs  )  )
																																	__UpperCAmelCase   :						int      							=					getattr(self.obj ,       UpperCamelCase  )
																																	# construct lower levels patches
																																	for key in submodules[i + 1 :]:
																																								setattr(UpperCamelCase ,       UpperCamelCase ,       _PatchedModuleObj(getattr(UpperCamelCase ,       UpperCamelCase ,       UpperCamelCase  ) ,       attrs=self.attrs  )  )
																																								__UpperCAmelCase   :						Optional[int]      							=					getattr(UpperCamelCase ,       UpperCamelCase  )
																																	# finally set the target attribute
																																	setattr(UpperCamelCase ,       UpperCamelCase ,       self.new  )

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

												if submodules:  # if it's an attribute of a submodule like "os.path.join"
																			try:
																										__UpperCAmelCase   :						int      							=					getattr(import_module(""".""".join(UpperCamelCase  )  ) ,       UpperCamelCase  )
																			except (AttributeError, ModuleNotFoundError):
																										return
																			# We iterate over all the globals in self.obj in case we find "os.path.join"
																			for attr in self.obj.__dir__():
																										# We don't check for the name of the global, but rather if its value *is* "os.path.join".
																										# This allows to patch renamed attributes like "from os.path import join as pjoin".
																										if getattr(self.obj ,       UpperCamelCase  ) is attr_value:
																																	__UpperCAmelCase   :						Union[str, Any]      							=					getattr(self.obj ,       UpperCamelCase  )
																																	setattr(self.obj ,       UpperCamelCase ,       self.new  )
												elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
																			__UpperCAmelCase   :						str      							=					globals()["""__builtins__"""][target_attr]
																			setattr(self.obj ,       UpperCamelCase ,       self.new  )
												else:
																			raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.'''  )






					def __exit__(       self    : str ,       *UpperCamelCase    : Optional[int]  ):
												'''simple docstring'''






												for attr in list(self.original  ):
																			setattr(self.obj ,       UpperCamelCase ,       self.original.pop(UpperCamelCase  )  )






					def lowerCamelCase__							(       self    : int  ):
												'''simple docstring'''






												self.__enter__()
												self._active_patches.append(self  )






					def lowerCamelCase__							(       self    : List[Any]  ):
												'''simple docstring'''






												try:
																			self._active_patches.remove(self  )
												except ValueError:
																			# If the patch hasn't been started this will fail
																			return None

												return self.__exit__()
