

'''simple docstring'''

import warnings
from typing import Dict

import numpy as np

from ..utils import ExplicitEnum, add_end_docstrings, is_tf_available, is_torch_available
from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline


if is_tf_available():
 from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

if is_torch_available():
 from ..models.auto.modeling_auto import MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING




def       UpperCamelCase(     UpperCAmelCase_	):
 return 1.0 / (1.0 + np.exp(-_outputs	))




def       UpperCamelCase(     UpperCAmelCase_	):
 UpperCAmelCase       :	Any											= np.max(_outputs  ,       axis=-1  ,       keepdims=_UpperCamelCase	)
 UpperCAmelCase       :	Optional[int]											= np.exp(_outputs - maxes	)
 return shifted_exp / shifted_exp.sum(axis=-1  ,       keepdims=_UpperCamelCase	)




class        A_ (				_snake_case							):






     '''simple docstring'''



     UpperCAmelCase_						:	Union[str, Any]  		=  """sigmoid"""
     UpperCAmelCase_						:	Union[str, Any]  		=  """softmax"""
     UpperCAmelCase_						:	Tuple  		=  """none"""




@add_end_docstrings(
    _snake_case       ,   r"""
        return_all_scores (`bool`, *optional*, defaults to `False`):
            Whether to return all prediction scores or just the one of the predicted class.
        function_to_apply (`str`, *optional*, defaults to `\"default\"`):
            The function to apply to the model outputs in order to retrieve the scores. Accepts four different values:

            - `\"default\"`: if the model has a single label, will apply the sigmoid function on the output. If the model
              has several labels, will apply the softmax function on the output.
            - `\"sigmoid\"`: Applies the sigmoid function on the output.
            - `\"softmax\"`: Applies the softmax function on the output.
            - `\"none\"`: Does not apply any function on the output.
    """       ,   )
class        A_ (				_snake_case							):






     '''simple docstring'''



     UpperCAmelCase_						:	int  		=  False
     UpperCAmelCase_						:	int  		=  ClassificationFunction.NONE


     def __init__(		self :     Any     ,			**lowercase_ :     Union[str, Any]       )						->	Any:
      super().__init__(**lowercase_       )

      self.check_model_type(
          TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
          if self.framework == 'tf'
          else MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING       )


     def    UpperCAmelCase_       (		self :     Union[str, Any]     ,			lowercase_ :     Any=None     ,			lowercase_ :     int=None     ,			lowercase_ :     List[Any]=""     ,			**lowercase_ :     List[str]       )						->	Tuple:
      UpperCAmelCase       :	Dict											= tokenizer_kwargs

      UpperCAmelCase       :	Union[str, Any]											= {}
      if hasattr(self.model.config     ,			'return_all_scores'       ) and return_all_scores is None:
       UpperCAmelCase       :	Optional[Any]											= self.model.config.return_all_scores

      if isinstance(lowercase_     ,			lowercase_       ) or top_k is None:
       UpperCAmelCase       :	List[Any]											= top_k
       UpperCAmelCase       :	Optional[int]											= False
      elif return_all_scores is not None:
       warnings.warn(
           '`return_all_scores` is now deprecated,  if want a similar functionality use `top_k=None` instead of'
           ' `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`.'     ,			lowercase_     ,			)
       if return_all_scores:
        UpperCAmelCase       :	Optional[int]											= None
       else:
        UpperCAmelCase       :	Union[str, Any]											= 1

      if isinstance(lowercase_     ,			lowercase_       ):
       UpperCAmelCase       :	Optional[int]											= ClassificationFunction[function_to_apply.upper()]

      if function_to_apply is not None:
       UpperCAmelCase       :	Dict											= function_to_apply
      return preprocess_params, {}, postprocess_params


     def __call__(		self :     Optional[Any]     ,			*lowercase_ :     Union[str, Any]     ,			**lowercase_ :     Union[str, Any]       )						->	Optional[int]:
      UpperCAmelCase       :	Tuple											= super().__call__(*lowercase_     ,			**lowercase_       )
      # TODO try and retrieve it in a nicer way from _sanitize_parameters.
      UpperCAmelCase       :	List[str]											= """top_k""" not in kwargs
      if isinstance(args[0]     ,			lowercase_       ) and _legacy:
       # This pipeline is odd, and return a list when single item is run
       return [result]
      else:
       return result


     def    UpperCAmelCase_       (		self :     Tuple     ,			lowercase_ :     List[Any]     ,			**lowercase_ :     List[str]       )						->	Tuple:
      UpperCAmelCase       :	List[Any]											= self.framework
      if isinstance(lowercase_     ,			lowercase_       ):
       return self.tokenizer(**lowercase_     ,			return_tensors=lowercase_     ,			**lowercase_       )
      elif isinstance(lowercase_     ,			lowercase_       ) and len(lowercase_       ) == 1 and isinstance(inputs[0]     ,			lowercase_       ) and len(inputs[0]       ) == 2:
       # It used to be valid to use a list of list of list for text pairs, keeping this path for BC
       return self.tokenizer(
           text=inputs[0][0]     ,			text_pair=inputs[0][1]     ,			return_tensors=lowercase_     ,			**lowercase_       )
      elif isinstance(lowercase_     ,			lowercase_       ):
       # This is likely an invalid usage of the pipeline attempting to pass text pairs.
       raise ValueError(
           'The pipeline received invalid inputs, if you are trying to send text pairs, you can try to send a'
           ' dictionary `{\"text\": \"My text\", \"text_pair\": \"My pair\"}` in order to send a text pair.'       )
      return self.tokenizer(lowercase_     ,			return_tensors=lowercase_     ,			**lowercase_       )


     def    UpperCAmelCase_       (		self :     Any     ,			lowercase_ :     List[Any]       )						->	Any:
      return self.model(**lowercase_       )




     def    UpperCAmelCase_       (		self :     List[Any]     ,			lowercase_ :     Tuple     ,			lowercase_ :     Union[str, Any]=None     ,			lowercase_ :     str=1     ,			lowercase_ :     int=True       )						->	Union[str, Any]:






      if function_to_apply is None:
       if self.model.config.problem_type == "multi_label_classification" or self.model.config.num_labels == 1:
        UpperCAmelCase       :	Tuple											= ClassificationFunction.SIGMOID
       elif self.model.config.problem_type == "single_label_classification" or self.model.config.num_labels > 1:
        UpperCAmelCase       :	Optional[Any]											= ClassificationFunction.SOFTMAX
       elif hasattr(self.model.config     ,			'function_to_apply'       ) and function_to_apply is None:
        UpperCAmelCase       :	Tuple											= self.model.config.function_to_apply
       else:
        UpperCAmelCase       :	int											= ClassificationFunction.NONE

      UpperCAmelCase       :	Dict											= model_outputs["""logits"""][0]
      UpperCAmelCase       :	int											= outputs.numpy()

      if function_to_apply == ClassificationFunction.SIGMOID:
       UpperCAmelCase       :	str											= sigmoid(lowercase_       )
      elif function_to_apply == ClassificationFunction.SOFTMAX:
       UpperCAmelCase       :	Optional[Any]											= softmax(lowercase_       )
      elif function_to_apply == ClassificationFunction.NONE:
       UpperCAmelCase       :	Optional[int]											= outputs
      else:
       raise ValueError(f"""Unrecognized `function_to_apply` argument: {function_to_apply}"""       )

      if top_k == 1 and _legacy:
       return {"label": self.model.config.idalabel[scores.argmax().item()], "score": scores.max().item()}

      UpperCAmelCase       :	Dict											= [
          {"""label""": self.model.config.idalabel[i], """score""": score.item()} for i, score in enumerate(lowercase_       )
      ]
      if not _legacy:
       dict_scores.sort(key=lambda lowercase_       : x["score"]     ,			reverse=lowercase_       )
       if top_k is not None:
        UpperCAmelCase       :	Any											= dict_scores[:top_k]
      return dict_scores








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