

'''simple docstring'''



import argparse
import json

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

from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel
from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD


torch.set_grad_enabled(False)


def       snake_case   (	snake_case      : Union[str, Any]					,		snake_case      : Tuple=False			) ->  List[Any]:


    """simple docstring"""


    lowerCAmelCase 						=							[]
    for i in range(config.num_hidden_layers			):
        # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
        rename_keys.append((F'module.blocks.{i}.norm1.weight', F'vit.encoder.layer.{i}.layernorm_before.weight')			)
        rename_keys.append((F'module.blocks.{i}.norm1.bias', F'vit.encoder.layer.{i}.layernorm_before.bias')			)
        rename_keys.append(
            (F'module.blocks.{i}.attn.proj.weight', F'vit.encoder.layer.{i}.attention.output.dense.weight')			)
        rename_keys.append((F'module.blocks.{i}.attn.proj.bias', F'vit.encoder.layer.{i}.attention.output.dense.bias')			)
        rename_keys.append((F'module.blocks.{i}.norm2.weight', F'vit.encoder.layer.{i}.layernorm_after.weight')			)
        rename_keys.append((F'module.blocks.{i}.norm2.bias', F'vit.encoder.layer.{i}.layernorm_after.bias')			)
        rename_keys.append((F'module.blocks.{i}.mlp.fc1.weight', F'vit.encoder.layer.{i}.intermediate.dense.weight')			)
        rename_keys.append((F'module.blocks.{i}.mlp.fc1.bias', F'vit.encoder.layer.{i}.intermediate.dense.bias')			)
        rename_keys.append((F'module.blocks.{i}.mlp.fc2.weight', F'vit.encoder.layer.{i}.output.dense.weight')			)
        rename_keys.append((F'module.blocks.{i}.mlp.fc2.bias', F'vit.encoder.layer.{i}.output.dense.bias')			)

    # projection layer + position embeddings
    rename_keys.extend(
        [
            ('module.cls_token', 'vit.embeddings.cls_token'),
            ('module.patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight'),
            ('module.patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias'),
            ('module.pos_embed', 'vit.embeddings.position_embeddings'),
        ]			)

    if base_model:
        # layernorm + pooler
        rename_keys.extend(
            [
                ('module.norm.weight', 'layernorm.weight'),
                ('module.norm.bias', 'layernorm.bias'),
            ]			)

        # if just the base model, we should remove "vit" from all keys that start with "vit"
        lowerCAmelCase 						=							[(pair[0], pair[1][4:]) if pair[1].startswith('vit'			) else pair for pair in rename_keys]
    else:
        # layernorm + classification head
        rename_keys.extend(
            [
                ('norm.weight', 'vit.layernorm.weight'),
                ('norm.bias', 'vit.layernorm.bias'),
                ('head.weight', 'classifier.weight'),
                ('head.bias', 'classifier.bias'),
            ]			)

    return rename_keys


def       snake_case   (	snake_case      : str					,		snake_case      : Optional[Any]					,		snake_case      : Union[str, Any]=False			) ->  List[Any]:


    """simple docstring"""


    for i in range(config.num_hidden_layers			):
        if base_model:
            lowerCAmelCase 						=							''''''
        else:
            lowerCAmelCase 						=							'''vit.'''
        # read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
        lowerCAmelCase 						=							state_dict.pop(F'module.blocks.{i}.attn.qkv.weight'			)
        lowerCAmelCase 						=							state_dict.pop(F'module.blocks.{i}.attn.qkv.bias'			)
        # next, add query, keys and values (in that order) to the state dict
        lowerCAmelCase 						=							in_proj_weight[
            : config.hidden_size, :
        ]
        lowerCAmelCase 						=							in_proj_bias[: config.hidden_size]
        lowerCAmelCase 						=							in_proj_weight[
            config.hidden_size : config.hidden_size * 2, :
        ]
        lowerCAmelCase 						=							in_proj_bias[
            config.hidden_size : config.hidden_size * 2
        ]
        lowerCAmelCase 						=							in_proj_weight[
            -config.hidden_size :, :
        ]
        lowerCAmelCase 						=							in_proj_bias[-config.hidden_size :]


def       snake_case   (	snake_case      : Any			) ->  List[Any]:


    """simple docstring"""


    lowerCAmelCase 						=							['''head.weight''', '''head.bias''']
    for k in ignore_keys:
        state_dict.pop(snake_case					,		snake_case			)


def       snake_case   (	snake_case      : Optional[int]			) ->  Any:


    """simple docstring"""


    lowerCAmelCase 						=							[
        '''module.fc.fc1.weight''',
        '''module.fc.fc1.bias''',
        '''module.fc.bn1.weight''',
        '''module.fc.bn1.bias''',
        '''module.fc.bn1.running_mean''',
        '''module.fc.bn1.running_var''',
        '''module.fc.bn1.num_batches_tracked''',
        '''module.fc.fc2.weight''',
        '''module.fc.fc2.bias''',
        '''module.fc.bn2.weight''',
        '''module.fc.bn2.bias''',
        '''module.fc.bn2.running_mean''',
        '''module.fc.bn2.running_var''',
        '''module.fc.bn2.num_batches_tracked''',
        '''module.fc.fc3.weight''',
        '''module.fc.fc3.bias''',
    ]
    for k in ignore_keys:
        state_dict.pop(snake_case					,		snake_case			)


def       snake_case   (	snake_case      : Optional[Any]					,		snake_case      : Dict					,		snake_case      : Optional[int]			) ->  Optional[Any]:


    """simple docstring"""


    lowerCAmelCase 						=							dct.pop(snake_case			)
    lowerCAmelCase 						=							val


def       snake_case   (	snake_case      : Optional[int]					,		snake_case      : int			) ->  Dict:


    """simple docstring"""


    lowerCAmelCase 						=							ViTMSNConfig()
    lowerCAmelCase 						=							1000

    lowerCAmelCase 						=							'''datasets/huggingface/label-files'''
    lowerCAmelCase 						=							'''imagenet-1k-id2label.json'''
    lowerCAmelCase 						=							json.load(open(hf_hub_download(snake_case					,		snake_case			)					,		'r'			)			)
    lowerCAmelCase 						=							{int(snake_case			): v for k, v in idalabel.items()}
    lowerCAmelCase 						=							idalabel
    lowerCAmelCase 						=							{v: k for k, v in idalabel.items()}

    if "s16" in checkpoint_url:
        lowerCAmelCase 						=							384
        lowerCAmelCase 						=							1536
        lowerCAmelCase 						=							6
    elif "l16" in checkpoint_url:
        lowerCAmelCase 						=							1024
        lowerCAmelCase 						=							4096
        lowerCAmelCase 						=							24
        lowerCAmelCase 						=							16
        lowerCAmelCase 						=							0.1
    elif "b4" in checkpoint_url:
        lowerCAmelCase 						=							4
    elif "l7" in checkpoint_url:
        lowerCAmelCase 						=							7
        lowerCAmelCase 						=							1024
        lowerCAmelCase 						=							4096
        lowerCAmelCase 						=							24
        lowerCAmelCase 						=							16
        lowerCAmelCase 						=							0.1

    lowerCAmelCase 						=							ViTMSNModel(snake_case			)

    lowerCAmelCase 						=							torch.hub.load_state_dict_from_url(snake_case					,		map_location='cpu'			)['''target_encoder''']

    lowerCAmelCase 						=							ViTImageProcessor(size=config.image_size			)

    remove_projection_head(snake_case			)
    lowerCAmelCase 						=							create_rename_keys(snake_case					,		base_model=snake_case			)

    for src, dest in rename_keys:
        rename_key(snake_case					,		snake_case					,		snake_case			)
    read_in_q_k_v(snake_case					,		snake_case					,		base_model=snake_case			)

    model.load_state_dict(snake_case			)
    model.eval()

    lowerCAmelCase 						=							'''http://images.cocodataset.org/val2017/000000039769.jpg'''

    lowerCAmelCase 						=							Image.open(requests.get(snake_case					,		stream=snake_case			).raw			)
    lowerCAmelCase 						=							ViTImageProcessor(
        size=config.image_size					,		image_mean=snake_case					,		image_std=snake_case			)
    lowerCAmelCase 						=							image_processor(images=snake_case					,		return_tensors='pt'			)

    # forward pass
    torch.manual_seed(2			)
    lowerCAmelCase 						=							model(**snake_case			)
    lowerCAmelCase 						=							outputs.last_hidden_state

    # The following Colab Notebook was used to generate these outputs:
    # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb
    if "s16" in checkpoint_url:
        lowerCAmelCase 						=							torch.tensor([[-1.0_915, -1.4_876, -1.1_809]]			)
    elif "b16" in checkpoint_url:
        lowerCAmelCase 						=							torch.tensor([[14.2_889, -18.9_045, 11.7_281]]			)
    elif "l16" in checkpoint_url:
        lowerCAmelCase 						=							torch.tensor([[41.5_028, -22.8_681, 45.6_475]]			)
    elif "b4" in checkpoint_url:
        lowerCAmelCase 						=							torch.tensor([[-4.3_868, 5.2_932, -0.4_137]]			)
    else:
        lowerCAmelCase 						=							torch.tensor([[-0.1_792, -0.6_465, 2.4_263]]			)

    # verify logits
    assert torch.allclose(last_hidden_state[:, 0, :3]					,		snake_case					,		atol=1e-4			)

    print(F'Saving model to {pytorch_dump_folder_path}'			)
    model.save_pretrained(snake_case			)

    print(F'Saving image processor to {pytorch_dump_folder_path}'			)
    image_processor.save_pretrained(snake_case			)


if __name__ == "__main__":
     _UpperCamelCase						:  List[Any]							       =				argparse.ArgumentParser()
     # Required parameters
     parser.add_argument(
         "--checkpoint_url",
         default="https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar",
         type=str,
         help="URL of the checkpoint you'd like to convert.",
     )
     parser.add_argument(
         "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory."
     )

     _UpperCamelCase						:  Optional[int]							       =				parser.parse_args()
     convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)













import sys

SCREAMING_SNAKE_CASE__   :      Optional[Any]				=		(
    """73167176531330624919225119674426574742355349194934"""
    """96983520312774506326239578318016984801869478851843"""
    """85861560789112949495459501737958331952853208805511"""
    """12540698747158523863050715693290963295227443043557"""
    """66896648950445244523161731856403098711121722383113"""
    """62229893423380308135336276614282806444486645238749"""
    """30358907296290491560440772390713810515859307960866"""
    """70172427121883998797908792274921901699720888093776"""
    """65727333001053367881220235421809751254540594752243"""
    """52584907711670556013604839586446706324415722155397"""
    """53697817977846174064955149290862569321978468622482"""
    """83972241375657056057490261407972968652414535100474"""
    """82166370484403199890008895243450658541227588666881"""
    """16427171479924442928230863465674813919123162824586"""
    """17866458359124566529476545682848912883142607690042"""
    """24219022671055626321111109370544217506941658960408"""
    """07198403850962455444362981230987879927244284909188"""
    """84580156166097919133875499200524063689912560717606"""
    """05886116467109405077541002256983155200055935729725"""
    """71636269561882670428252483600823257530420752963450"""
)

def   __lowercase       (			snake_case = N			):







				"""simple docstring"""






				__magic_name__		:Optional[int]   =   -sys.maxsize - 1
				for i in range(len(snake_case			) - 1_2			):
								__magic_name__		:List[Any]   =   1
								for j in range(1_3			):
												product *= int(n[i + j]			)
								if product > largest_product:
												__magic_name__		:str   =   product
				return largest_product


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






