



'''simple docstring'''





def 						__snake_case(				_lowerCAmelCase = 1_000		)		-> int:
      return sum(e for e in range(3     ,   _lowerCAmelCase		) if e % 3 == 0 or e % 5 == 0		)


if __name__ == "__main__":
       print(F"{solution() = }")





'''simple docstring'''





import argparse
import json

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

from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification





def 						__snake_case(				_lowerCAmelCase		)		-> List[Any]:
      snake_case__  :    Dict    						=      SwinConfig()
      snake_case__  :    Optional[Any]    						=      swin_name.split("""_"""		)

      snake_case__  :    Any    						=      name_split[1]
      snake_case__  :    List[Any]    						=      int(name_split[4]		)
      snake_case__  :    int    						=      int(name_split[3][-1]		)

      if model_size == "tiny":
            snake_case__  :    List[Any]    						=      96
            snake_case__  :    int    						=      (2, 2, 6, 2)
            snake_case__  :    int    						=      (3, 6, 12, 24)
      elif model_size == "small":
            snake_case__  :    Union[str, Any]    						=      96
            snake_case__  :    Optional[Any]    						=      (2, 2, 18, 2)
            snake_case__  :    str    						=      (3, 6, 12, 24)
      elif model_size == "base":
            snake_case__  :    Dict    						=      128
            snake_case__  :    str    						=      (2, 2, 18, 2)
            snake_case__  :    Dict    						=      (4, 8, 16, 32)
      else:
            snake_case__  :    List[str]    						=      192
            snake_case__  :    str    						=      (2, 2, 18, 2)
            snake_case__  :    List[Any]    						=      (6, 12, 24, 48)

      if "in22k" in swin_name:
            snake_case__  :    str    						=      21_841
      else:
            snake_case__  :    List[str]    						=      1_000
            snake_case__  :    int    						=      """huggingface/label-files"""
            snake_case__  :    Any    						=      """imagenet-1k-id2label.json"""
            snake_case__  :    List[Any]    						=      json.load(open(hf_hub_download(_lowerCAmelCase     ,   _lowerCAmelCase     ,   repo_type="""dataset"""		)     ,   """r"""		)		)
            snake_case__  :    Dict    						=      {int(_lowerCAmelCase		): v for k, v in idalabel.items()}
            snake_case__  :    Optional[int]    						=      idalabel
            snake_case__  :    List[Any]    						=      {v: k for k, v in idalabel.items()}

      snake_case__  :    List[Any]    						=      img_size
      snake_case__  :    Dict    						=      num_classes
      snake_case__  :    Dict    						=      embed_dim
      snake_case__  :    Optional[int]    						=      depths
      snake_case__  :    int    						=      num_heads
      snake_case__  :    Optional[int]    						=      window_size

      return config





def 						__snake_case(				_lowerCAmelCase		)		-> Dict:
      if "patch_embed.proj" in name:
            snake_case__  :    List[str]    						=      name.replace("""patch_embed.proj"""     ,   """embeddings.patch_embeddings.projection"""		)
      if "patch_embed.norm" in name:
            snake_case__  :    int    						=      name.replace("""patch_embed.norm"""     ,   """embeddings.norm"""		)
      if "layers" in name:
            snake_case__  :    str    						=      """encoder.""" + name
      if "attn.proj" in name:
            snake_case__  :    List[str]    						=      name.replace("""attn.proj"""     ,   """attention.output.dense"""		)
      if "attn" in name:
            snake_case__  :    Tuple    						=      name.replace("""attn"""     ,   """attention.self"""		)
      if "norm1" in name:
            snake_case__  :    List[str]    						=      name.replace("""norm1"""     ,   """layernorm_before"""		)
      if "norm2" in name:
            snake_case__  :    Optional[Any]    						=      name.replace("""norm2"""     ,   """layernorm_after"""		)
      if "mlp.fc1" in name:
            snake_case__  :    Union[str, Any]    						=      name.replace("""mlp.fc1"""     ,   """intermediate.dense"""		)
      if "mlp.fc2" in name:
            snake_case__  :    Dict    						=      name.replace("""mlp.fc2"""     ,   """output.dense"""		)

      if name == "norm.weight":
            snake_case__  :    Tuple    						=      """layernorm.weight"""
      if name == "norm.bias":
            snake_case__  :    Union[str, Any]    						=      """layernorm.bias"""

      if "head" in name:
            snake_case__  :    Optional[int]    						=      name.replace("""head"""     ,   """classifier"""		)
      else:
            snake_case__  :    List[str]    						=      """swin.""" + name

      return name





def 						__snake_case(				_lowerCAmelCase     ,   _lowerCAmelCase		)		-> Optional[Any]:
      for key in orig_state_dict.copy().keys():
            snake_case__  :    Optional[int]    						=      orig_state_dict.pop(_lowerCAmelCase		)

            if "mask" in key:
                  continue
            elif "qkv" in key:
                  snake_case__  :    Dict    						=      key.split("""."""		)
                  snake_case__  :    Optional[int]    						=      int(key_split[1]		)
                  snake_case__  :    Union[str, Any]    						=      int(key_split[3]		)
                  snake_case__  :    List[Any]    						=      model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size

                  if "weight" in key:
                        snake_case__  :    Optional[Any]    						=      val[:dim, :]
                        snake_case__  :    Tuple    						=      val[
                            dim : dim * 2, :
                        ]
                        snake_case__  :    Dict    						=      val[-dim:, :]
                  else:
                        snake_case__  :    Tuple    						=      val[
                            :dim
                        ]
                        snake_case__  :    int    						=      val[
                            dim : dim * 2
                        ]
                        snake_case__  :    int    						=      val[
                            -dim:
                        ]
            else:
                  snake_case__  :    Union[str, Any]    						=      val

      return orig_state_dict





def 						__snake_case(				_lowerCAmelCase     ,   _lowerCAmelCase		)		-> int:
      snake_case__  :    Optional[int]    						=      timm.create_model(_lowerCAmelCase     ,   pretrained=_lowerCAmelCase		)
      timm_model.eval()

      snake_case__  :    Optional[int]    						=      get_swin_config(_lowerCAmelCase		)
      snake_case__  :    Optional[Any]    						=      SwinForImageClassification(_lowerCAmelCase		)
      model.eval()

      snake_case__  :    str    						=      convert_state_dict(timm_model.state_dict()     ,   _lowerCAmelCase		)
      model.load_state_dict(_lowerCAmelCase		)

      snake_case__  :    int    						=      """http://images.cocodataset.org/val2017/000000039769.jpg"""

      snake_case__  :    Dict    						=      AutoImageProcessor.from_pretrained("""microsoft/{}""".format(swin_name.replace("""_"""     ,   """-"""		)		)		)
      snake_case__  :    Dict    						=      Image.open(requests.get(_lowerCAmelCase     ,   stream=_lowerCAmelCase		).raw		)
      snake_case__  :    Optional[int]    						=      image_processor(images=_lowerCAmelCase     ,   return_tensors="""pt"""		)

      snake_case__  :    Optional[Any]    						=      timm_model(inputs["""pixel_values"""]		)
      snake_case__  :    str    						=      model(**_lowerCAmelCase		).logits

      assert torch.allclose(_lowerCAmelCase     ,   _lowerCAmelCase     ,   atol=1e-3		)

      print(f"Saving model {swin_name} to {pytorch_dump_folder_path}"		)
      model.save_pretrained(_lowerCAmelCase		)

      print(f"Saving image processor to {pytorch_dump_folder_path}"		)
      image_processor.save_pretrained(_lowerCAmelCase		)


if __name__ == "__main__":
       __a          =		argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           "--swin_name",
           default="swin_tiny_patch4_window7_224",
           type=str,
           help="Name of the Swin timm model you'd like to convert.",
       )
       parser.add_argument(
           "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory."
       )

       __a          =		parser.parse_args()
       convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
