




import unittest

import numpy as np

from transformers import is_flax_available
from transformers.testing_utils import require_flax

from ..test_modeling_flax_common import ids_tensor


if is_flax_available():
       import jax
       import jax.numpy as jnp

       from transformers.generation import (
           FlaxForcedBOSTokenLogitsProcessor,
           FlaxForcedEOSTokenLogitsProcessor,
           FlaxLogitsProcessorList,
           FlaxMinLengthLogitsProcessor,
           FlaxTemperatureLogitsWarper,
           FlaxTopKLogitsWarper,
           FlaxTopPLogitsWarper,
       )




@require_flax
class 		UpperCamelCase__	(unittest.TestCase     ):

    '''simple docstring'''



    def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__     )     ->      List[str]:
           lowerCamelCase   :      str			 =					jnp.ones((batch_size, length)     ) / length
           return scores







    def     _lowercase     (    self     )     ->      Tuple:
           lowerCamelCase   :      Optional[int]			 =					None
           lowerCamelCase   :      Optional[int]			 =					20

           lowerCamelCase   :      Dict			 =					self._get_uniform_logits(batch_size=2     ,  length=UpperCamelCase__     )

           # tweak scores to not be uniform anymore
           lowerCamelCase   :      Union[str, Any]			 =					scores.at[1, 5].set((1 / length) + 0.1     )  # peak, 1st batch
           lowerCamelCase   :      int			 =					scores.at[1, 10].set((1 / length) - 0.4     )  # valley, 1st batch

           # compute softmax
           lowerCamelCase   :      Any			 =					jax.nn.softmax(UpperCamelCase__     ,  axis=-1     )

           lowerCamelCase   :      Optional[Any]			 =					FlaxTemperatureLogitsWarper(temperature=0.5     )
           lowerCamelCase   :      List[Any]			 =					FlaxTemperatureLogitsWarper(temperature=1.3     )

           lowerCamelCase   :      Dict			 =					jax.nn.softmax(temp_dist_warper_sharper(UpperCamelCase__     ,  scores.copy()     ,  cur_len=UpperCamelCase__     )     ,  axis=-1     )
           lowerCamelCase   :      Any			 =					jax.nn.softmax(temp_dist_warper_smoother(UpperCamelCase__     ,  scores.copy()     ,  cur_len=UpperCamelCase__     )     ,  axis=-1     )

           # uniform distribution stays uniform
           self.assertTrue(jnp.allclose(probs[0, :]     ,  warped_prob_sharp[0, :]     ,  atol=1e-3     )     )
           self.assertTrue(jnp.allclose(probs[0, :]     ,  warped_prob_smooth[0, :]     ,  atol=1e-3     )     )

           # sharp peaks get higher, valleys get lower
           self.assertLess(probs[1, :].max()     ,  warped_prob_sharp[1, :].max()     )
           self.assertGreater(probs[1, :].min()     ,  warped_prob_sharp[1, :].min()     )

           # smooth peaks get lower, valleys get higher
           self.assertGreater(probs[1, :].max()     ,  warped_prob_smooth[1, :].max()     )
           self.assertLess(probs[1, :].min()     ,  warped_prob_smooth[1, :].min()     )







    def     _lowercase     (    self     )     ->      Any:
           lowerCamelCase   :      Dict			 =					None
           lowerCamelCase   :      Tuple			 =					10
           lowerCamelCase   :      Dict			 =					2

           # create ramp distribution
           lowerCamelCase   :      str			 =					np.broadcast_to(np.arange(UpperCamelCase__     )[None, :]     ,  (batch_size, vocab_size)     ).copy()
           lowerCamelCase   :      str			 =					ramp_logits[1:, : vocab_size // 2] + vocab_size

           lowerCamelCase   :      Dict			 =					FlaxTopKLogitsWarper(3     )

           lowerCamelCase   :      str			 =					top_k_warp(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )

           # check that correct tokens are filtered
           self.assertListEqual(jnp.isinf(scores[0]     ).tolist()     ,  7 * [True] + 3 * [False]     )
           self.assertListEqual(jnp.isinf(scores[1]     ).tolist()     ,  2 * [True] + 3 * [False] + 5 * [True]     )

           # check special case
           lowerCamelCase   :      int			 =					5
           lowerCamelCase   :      List[Any]			 =					FlaxTopKLogitsWarper(top_k=1     ,  filter_value=0.0     ,  min_tokens_to_keep=3     )

           lowerCamelCase   :      int			 =					np.broadcast_to(np.arange(UpperCamelCase__     )[None, :]     ,  (batch_size, length)     ).copy()
           lowerCamelCase   :      List[str]			 =					top_k_warp_safety_check(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )

           # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified
           self.assertListEqual((scores == 0.0).sum(axis=-1     ).tolist()     ,  [2, 2]     )







    def     _lowercase     (    self     )     ->      List[Any]:
           lowerCamelCase   :      Union[str, Any]			 =					None
           lowerCamelCase   :      int			 =					10
           lowerCamelCase   :      Optional[Any]			 =					2

           # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper)
           lowerCamelCase   :      Optional[Any]			 =					np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]]     )     )

           lowerCamelCase   :      Any			 =					FlaxTopPLogitsWarper(0.8     )
           lowerCamelCase   :      Optional[int]			 =					np.exp(top_p_warp(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )     )

           # dist should be filtered to keep min num values so that sum is >= top_p
           # exp (-inf) => 0
           lowerCamelCase   :      str			 =					np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]]     )
           self.assertTrue(np.allclose(UpperCamelCase__     ,  UpperCamelCase__     ,  atol=1e-3     )     )

           # check edge cases with negative and extreme logits
           lowerCamelCase   :      Optional[int]			 =					np.broadcast_to(np.arange(UpperCamelCase__     )[None, :]     ,  (batch_size, vocab_size)     ).copy() - (
               vocab_size // 2
           )

           # make ramp_logits more extreme
           lowerCamelCase   :      int			 =					ramp_logits[1] * 100.0

           # make sure at least 2 tokens are kept
           lowerCamelCase   :      Union[str, Any]			 =					FlaxTopPLogitsWarper(0.9     ,  min_tokens_to_keep=2     ,  filter_value=0.0     )
           lowerCamelCase   :      Optional[int]			 =					top_p_warp(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )

           # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2.
           self.assertListEqual((filtered_dist != 0.0).sum(axis=-1     ).tolist()     ,  [3, 2]     )







    def     _lowercase     (    self     )     ->      Union[str, Any]:
           lowerCamelCase   :      List[str]			 =					20
           lowerCamelCase   :      List[str]			 =					4
           lowerCamelCase   :      List[str]			 =					0

           lowerCamelCase   :      Dict			 =					FlaxMinLengthLogitsProcessor(min_length=10     ,  eos_token_id=UpperCamelCase__     )

           # check that min length is applied at length 5
           lowerCamelCase   :      Dict			 =					ids_tensor((batch_size, 20)     ,  vocab_size=20     )
           lowerCamelCase   :      Tuple			 =					5
           lowerCamelCase   :      Optional[Any]			 =					self._get_uniform_logits(UpperCamelCase__     ,  UpperCamelCase__     )
           lowerCamelCase   :      Optional[int]			 =					min_dist_processor(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
           self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist()     ,  4 * [-float("inf"     )]     )

           # check that min length is not applied anymore at length 15
           lowerCamelCase   :      Optional[Any]			 =					self._get_uniform_logits(UpperCamelCase__     ,  UpperCamelCase__     )
           lowerCamelCase   :      str			 =					15
           lowerCamelCase   :      List[Any]			 =					min_dist_processor(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
           self.assertFalse(jnp.isinf(UpperCamelCase__     ).any()     )







    def     _lowercase     (    self     )     ->      str:
           lowerCamelCase   :      List[str]			 =					20
           lowerCamelCase   :      List[Any]			 =					4
           lowerCamelCase   :      str			 =					0

           lowerCamelCase   :      List[str]			 =					FlaxForcedBOSTokenLogitsProcessor(bos_token_id=UpperCamelCase__     )

           # check that all scores are -inf except the bos_token_id score
           lowerCamelCase   :      Any			 =					ids_tensor((batch_size, 1)     ,  vocab_size=20     )
           lowerCamelCase   :      str			 =					1
           lowerCamelCase   :      Tuple			 =					self._get_uniform_logits(UpperCamelCase__     ,  UpperCamelCase__     )
           lowerCamelCase   :      int			 =					logits_processor(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
           self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :]     ).all()     )
           self.assertListEqual(scores[:, bos_token_id].tolist()     ,  4 * [0]     )  # score for bos_token_id shold be zero

           # check that bos_token_id is not forced if current length is greater than 1
           lowerCamelCase   :      List[str]			 =					3
           lowerCamelCase   :      Optional[Any]			 =					self._get_uniform_logits(UpperCamelCase__     ,  UpperCamelCase__     )
           lowerCamelCase   :      Optional[Any]			 =					logits_processor(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
           self.assertFalse(jnp.isinf(UpperCamelCase__     ).any()     )







    def     _lowercase     (    self     )     ->      List[str]:
           lowerCamelCase   :      Dict			 =					20
           lowerCamelCase   :      Optional[int]			 =					4
           lowerCamelCase   :      int			 =					0
           lowerCamelCase   :      Optional[int]			 =					5

           lowerCamelCase   :      int			 =					FlaxForcedEOSTokenLogitsProcessor(max_length=UpperCamelCase__     ,  eos_token_id=UpperCamelCase__     )

           # check that all scores are -inf except the eos_token_id when max_length is reached
           lowerCamelCase   :      Tuple			 =					ids_tensor((batch_size, 4)     ,  vocab_size=20     )
           lowerCamelCase   :      Optional[int]			 =					4
           lowerCamelCase   :      int			 =					self._get_uniform_logits(UpperCamelCase__     ,  UpperCamelCase__     )
           lowerCamelCase   :      Tuple			 =					logits_processor(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
           self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :]     ).all()     )
           self.assertListEqual(scores[:, eos_token_id].tolist()     ,  4 * [0]     )  # score for eos_token_id should be zero

           # check that eos_token_id is not forced if max_length is not reached
           lowerCamelCase   :      int			 =					3
           lowerCamelCase   :      List[str]			 =					self._get_uniform_logits(UpperCamelCase__     ,  UpperCamelCase__     )
           lowerCamelCase   :      Dict			 =					logits_processor(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
           self.assertFalse(jnp.isinf(UpperCamelCase__     ).any()     )







    def     _lowercase     (    self     )     ->      Any:
           lowerCamelCase   :      List[str]			 =					4
           lowerCamelCase   :      Union[str, Any]			 =					10
           lowerCamelCase   :      Dict			 =					15
           lowerCamelCase   :      int			 =					2
           lowerCamelCase   :      List[str]			 =					1
           lowerCamelCase   :      List[str]			 =					15

           # dummy input_ids and scores
           lowerCamelCase   :      Dict			 =					ids_tensor((batch_size, sequence_length)     ,  UpperCamelCase__     )
           lowerCamelCase   :      Tuple			 =					input_ids.copy()

           lowerCamelCase   :      Any			 =					self._get_uniform_logits(UpperCamelCase__     ,  UpperCamelCase__     )
           lowerCamelCase   :      str			 =					scores.copy()

           # instantiate all dist processors
           lowerCamelCase   :      Dict			 =					FlaxTemperatureLogitsWarper(temperature=0.5     )
           lowerCamelCase   :      Dict			 =					FlaxTopKLogitsWarper(3     )
           lowerCamelCase   :      Tuple			 =					FlaxTopPLogitsWarper(0.8     )

           # instantiate all logits processors
           lowerCamelCase   :      List[Any]			 =					FlaxMinLengthLogitsProcessor(min_length=10     ,  eos_token_id=UpperCamelCase__     )
           lowerCamelCase   :      Optional[Any]			 =					FlaxForcedBOSTokenLogitsProcessor(bos_token_id=UpperCamelCase__     )
           lowerCamelCase   :      Union[str, Any]			 =					FlaxForcedEOSTokenLogitsProcessor(max_length=UpperCamelCase__     ,  eos_token_id=UpperCamelCase__     )

           lowerCamelCase   :      Union[str, Any]			 =					10

           # no processor list
           lowerCamelCase   :      Any			 =					temp_dist_warp(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
           lowerCamelCase   :      Tuple			 =					top_k_warp(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
           lowerCamelCase   :      Dict			 =					top_p_warp(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
           lowerCamelCase   :      List[str]			 =					min_dist_proc(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
           lowerCamelCase   :      Optional[int]			 =					bos_dist_proc(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
           lowerCamelCase   :      Dict			 =					eos_dist_proc(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )

           # with processor list
           lowerCamelCase   :      List[Any]			 =					FlaxLogitsProcessorList(
               [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]     )
           lowerCamelCase   :      int			 =					processor(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )

           # scores should be equal
           self.assertTrue(jnp.allclose(UpperCamelCase__     ,  UpperCamelCase__     ,  atol=1e-3     )     )

           # input_ids should never be changed
           self.assertListEqual(input_ids.tolist()     ,  input_ids_comp.tolist()     )







    def     _lowercase     (    self     )     ->      Union[str, Any]:
           lowerCamelCase   :      List[str]			 =					4
           lowerCamelCase   :      int			 =					10
           lowerCamelCase   :      List[str]			 =					15
           lowerCamelCase   :      Optional[Any]			 =					2
           lowerCamelCase   :      Optional[int]			 =					1
           lowerCamelCase   :      Any			 =					15

           # dummy input_ids and scores
           lowerCamelCase   :      Dict			 =					ids_tensor((batch_size, sequence_length)     ,  UpperCamelCase__     )
           lowerCamelCase   :      Optional[Any]			 =					input_ids.copy()

           lowerCamelCase   :      List[Any]			 =					self._get_uniform_logits(UpperCamelCase__     ,  UpperCamelCase__     )
           lowerCamelCase   :      str			 =					scores.copy()

           # instantiate all dist processors
           lowerCamelCase   :      Optional[int]			 =					FlaxTemperatureLogitsWarper(temperature=0.5     )
           lowerCamelCase   :      Tuple			 =					FlaxTopKLogitsWarper(3     )
           lowerCamelCase   :      str			 =					FlaxTopPLogitsWarper(0.8     )

           # instantiate all logits processors
           lowerCamelCase   :      List[str]			 =					FlaxMinLengthLogitsProcessor(min_length=10     ,  eos_token_id=UpperCamelCase__     )
           lowerCamelCase   :      int			 =					FlaxForcedBOSTokenLogitsProcessor(bos_token_id=UpperCamelCase__     )
           lowerCamelCase   :      Optional[Any]			 =					FlaxForcedEOSTokenLogitsProcessor(max_length=UpperCamelCase__     ,  eos_token_id=UpperCamelCase__     )

           lowerCamelCase   :      Optional[Any]			 =					10

           # no processor list
           def run_no_processor_list(UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ):
                  lowerCamelCase   :      Tuple			 =					temp_dist_warp(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
                  lowerCamelCase   :      Union[str, Any]			 =					top_k_warp(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
                  lowerCamelCase   :      Optional[Any]			 =					top_p_warp(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
                  lowerCamelCase   :      Dict			 =					min_dist_proc(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
                  lowerCamelCase   :      List[str]			 =					bos_dist_proc(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
                  lowerCamelCase   :      Tuple			 =					eos_dist_proc(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
                  return scores

           # with processor list
           def run_processor_list(UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ):
                  lowerCamelCase   :      Optional[int]			 =					FlaxLogitsProcessorList(
                      [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]     )
                  lowerCamelCase   :      Optional[Any]			 =					processor(UpperCamelCase__     ,  UpperCamelCase__     ,  cur_len=UpperCamelCase__     )
                  return scores

           lowerCamelCase   :      List[Any]			 =					jax.jit(UpperCamelCase__     )
           lowerCamelCase   :      Optional[int]			 =					jax.jit(UpperCamelCase__     )

           lowerCamelCase   :      Optional[Any]			 =					jitted_run_no_processor_list(UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     )
           lowerCamelCase   :      Dict			 =					jitted_run_processor_list(UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     )

           # scores should be equal
           self.assertTrue(jnp.allclose(UpperCamelCase__     ,  UpperCamelCase__     ,  atol=1e-3     )     )

           # input_ids should never be changed
           self.assertListEqual(input_ids.tolist()     ,  input_ids_comp.tolist()     )











'''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()
a   :			List[Any]					  =    logging.get_logger(__name__)

a   :			Union[str, Any]					  =    torch.device("""cpu""")



def       __lowerCamelCase  (					)      ->			Any:
							UpperCAmelCase     :		Optional[Any]							     =		"""http://images.cocodataset.org/val2017/000000039769.jpg"""
							UpperCAmelCase     :		Dict							     =		Image.open(requests.get(_lowercase	,				stream=_lowercase		).raw		)
							return im



def       __lowerCamelCase  (					_lowercase		)      ->			Dict:
							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  (					_lowercase	,				_lowercase	,				_lowercase		)      ->			str:
							UpperCAmelCase     :		Union[str, Any]							     =		dct.pop(_lowercase		)
							UpperCAmelCase     :		str							     =		val



def       __lowerCamelCase  (					_lowercase		)      ->			List[str]:
							UpperCAmelCase     :		Tuple							     =		[]
							for k in state_dict.keys():
														UpperCAmelCase     :		Dict							     =		k
														if ".pwconv" in k:
																					UpperCAmelCase     :		Union[str, Any]							     =		k_new.replace(""".pwconv"""	,				""".point_wise_conv"""		)
														if ".dwconv" in k:
																					UpperCAmelCase     :		Dict							     =		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     :		Dict							     =		k_new.replace("""patch_embed"""	,				"""swiftformer.patch_embed.patch_embedding"""		)
														if "network" in k_new:
																					UpperCAmelCase     :		Optional[Any]							     =		k_new.split("""."""		)
																					if ls[2].isdigit():
																												UpperCAmelCase     :		Any							     =		"""swiftformer.encoder.network.""" + ls[1] + """.blocks.""" + ls[2] + """.""" + """.""".join(ls[3:]		)
																					else:
																												UpperCAmelCase     :		Any							     =		k_new.replace("""network"""	,				"""swiftformer.encoder.network"""		)
														rename_keys.append((k, k_new)		)
							return rename_keys



@torch.no_grad()
def       __lowerCamelCase  (					_lowercase	,				_lowercase	,				_lowercase		)      ->			Optional[int]:
							UpperCAmelCase     :		Optional[Any]							     =		SwiftFormerConfig()

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

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

							elif swiftformer_name == "swiftformer_s":
														UpperCAmelCase     :		str							     =		[3, 3, 9, 6]
														UpperCAmelCase     :		str							     =		[4_8, 6_4, 1_6_8, 2_2_4]

							elif swiftformer_name == "swiftformer_l1":
														UpperCAmelCase     :		List[Any]							     =		[4, 3, 1_0, 5]
														UpperCAmelCase     :		Union[str, Any]							     =		[4_8, 9_6, 1_9_2, 3_8_4]

							elif swiftformer_name == "swiftformer_l3":
														UpperCAmelCase     :		Any							     =		[4, 4, 1_2, 6]
														UpperCAmelCase     :		List[Any]							     =		[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     :		Dict							     =		torch.hub.load_state_dict_from_url(_lowercase	,				map_location="""cpu"""	,				check_hash=_lowercase		)
														else:
																					UpperCAmelCase     :		Dict							     =		torch.load(_lowercase	,				map_location="""cpu"""		)
							UpperCAmelCase     :		str							     =		checkpoint

							UpperCAmelCase     :		Tuple							     =		create_rename_keys(_lowercase		)
							for rename_key_src, rename_key_dest in rename_keys:
														rename_key(_lowercase	,				_lowercase	,				_lowercase		)

							# load HuggingFace model
							UpperCAmelCase     :		str							     =		SwiftFormerForImageClassification(_lowercase		).eval()
							hf_model.load_state_dict(_lowercase		)

							# prepare test inputs
							UpperCAmelCase     :		Any							     =		prepare_img()
							UpperCAmelCase     :		List[Any]							     =		ViTImageProcessor.from_pretrained("""preprocessor_config"""		)
							UpperCAmelCase     :		List[str]							     =		processor(images=_lowercase	,				return_tensors="""pt"""		)

							# compare outputs from both models
							UpperCAmelCase     :		List[str]							     =		get_expected_output(_lowercase		)
							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]	,				_lowercase	,				atol=1e-3		)

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


if __name__ == "__main__":
							a   :			Tuple					  =    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   :			Union[str, Any]					  =    parser.parse_args()
							convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)

