





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 ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel
from transformers.utils import logging


logging.set_verbosity_info()
__A							:			str         =			logging.get_logger(__name__)





def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE=False				)							-> int:




    """simple docstring"""




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

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

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

        # if just the base model, we should remove "vit" from all keys that start with "vit"
        _A     =							[(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 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE=False				)							-> int:




    """simple docstring"""




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





def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE				)							-> List[str]:




    """simple docstring"""




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





def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE				)							-> List[str]:




    """simple docstring"""




    _A     =							dct.pop(_SCREAMING_SNAKE_CASE				)
    _A     =							val





def 				__lowerCAmelCase(       )							-> str:




    """simple docstring"""




    _A     =							'http://images.cocodataset.org/val2017/000000039769.jpg'
    _A     =							Image.open(requests.get(_SCREAMING_SNAKE_CASE							,     stream=_SCREAMING_SNAKE_CASE				).raw				)
    return im





@torch.no_grad()
def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE=True				)							-> Optional[Any]:




    """simple docstring"""




    _A     =							ViTConfig()
    # patch_size
    if model_name[-1] == "8":
        _A     =							8
    # set labels if required
    if not base_model:
        _A     =							1_000
        _A     =							'huggingface/label-files'
        _A     =							'imagenet-1k-id2label.json'
        _A     =							json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE							,     repo_type='dataset'				)							,     'r'				)				)
        _A     =							{int(_SCREAMING_SNAKE_CASE				): v for k, v in idalabel.items()}
        _A     =							idalabel
        _A     =							{v: k for k, v in idalabel.items()}
    # size of the architecture
    if model_name in ["dino_vits8", "dino_vits16"]:
        _A     =							384
        _A     =							1_536
        _A     =							12
        _A     =							6

    # load original model from torch hub
    _A     =							torch.hub.load('facebookresearch/dino:main'							,     _SCREAMING_SNAKE_CASE				)
    original_model.eval()

    # load state_dict of original model, remove and rename some keys
    _A     =							original_model.state_dict()
    if base_model:
        remove_classification_head_(_SCREAMING_SNAKE_CASE				)
    _A     =							create_rename_keys(_SCREAMING_SNAKE_CASE							,     base_model=_SCREAMING_SNAKE_CASE				)
    for src, dest in rename_keys:
        rename_key(_SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE				)
    read_in_q_k_v(_SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE				)

    # load HuggingFace model
    if base_model:
        _A     =							ViTModel(_SCREAMING_SNAKE_CASE							,     add_pooling_layer=_SCREAMING_SNAKE_CASE				).eval()
    else:
        _A     =							ViTForImageClassification(_SCREAMING_SNAKE_CASE				).eval()
    model.load_state_dict(_SCREAMING_SNAKE_CASE				)

    # Check outputs on an image, prepared by ViTImageProcessor
    _A     =							ViTImageProcessor()
    _A     =							image_processor(images=prepare_img()							,     return_tensors='pt'				)
    _A     =							encoding['pixel_values']
    _A     =							model(_SCREAMING_SNAKE_CASE				)

    if base_model:
        _A     =							original_model(_SCREAMING_SNAKE_CASE				)
        assert torch.allclose(_SCREAMING_SNAKE_CASE							,     outputs.last_hidden_state[:, 0, :]							,     atol=1e-1				)
    else:
        _A     =							original_model(_SCREAMING_SNAKE_CASE				)
        assert logits.shape == outputs.logits.shape
        assert torch.allclose(_SCREAMING_SNAKE_CASE							,     outputs.logits							,     atol=1e-3				)

    Path(_SCREAMING_SNAKE_CASE				).mkdir(exist_ok=_SCREAMING_SNAKE_CASE				)
    print(F"Saving model {model_name} to {pytorch_dump_folder_path}"				)
    model.save_pretrained(_SCREAMING_SNAKE_CASE				)
    print(F"Saving image processor to {pytorch_dump_folder_path}"				)
    image_processor.save_pretrained(_SCREAMING_SNAKE_CASE				)


if __name__ == "__main__":
 __A							:			List[Any]         =			argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     "--model_name",
     default="dino_vitb16",
     type=str,
     help="Name of the model trained with DINO you'd like to convert.",
 )
 parser.add_argument(
     "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory."
 )
 parser.add_argument(
     "--base_model",
     action="store_true",
     help="Whether to only convert the base model (no projection head weights).",
 )

 parser.set_defaults(base_model=True)
 __A							:			List[Any]         =			parser.parse_args()
 convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)












from __future__ import annotations

import math





def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE				)							-> list[int]:




    """simple docstring"""




    if num <= 0:
        _A     =							F"{num}: Invalid input, please enter a positive integer."
        raise ValueError(_SCREAMING_SNAKE_CASE				)

    _A     =							[True] * (num + 1)
    _A     =							[]
    _A     =							2
    _A     =							int(math.sqrt(_SCREAMING_SNAKE_CASE				)				)

    while start <= end:
        # If start is a prime
        if sieve[start] is True:
            prime.append(_SCREAMING_SNAKE_CASE				)

            # Set multiples of start be False
            for i in range(start * start							,     num + 1							,     _SCREAMING_SNAKE_CASE				):
                if sieve[i] is True:
                    _A     =							False

        start += 1

    for j in range(end + 1							,     num + 1				):
        if sieve[j] is True:
            prime.append(_SCREAMING_SNAKE_CASE				)

    return prime


if __name__ == "__main__":
 print(prime_sieve(int(input("Enter a positive integer: ").strip())))





