


import collections.abc
from typing import Optional, Tuple, Union

import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_poolformer import PoolFormerConfig


SCREAMING_SNAKE_CASE__					:     Optional[int]       =     logging.get_logger(__name__)

# General docstring
SCREAMING_SNAKE_CASE__					:     Optional[int]       =     'PoolFormerConfig'

# Base docstring
SCREAMING_SNAKE_CASE__					:     int       =     'sail/poolformer_s12'
SCREAMING_SNAKE_CASE__					:     Any       =     [1, 512, 7, 7]

# Image classification docstring
SCREAMING_SNAKE_CASE__					:     Union[str, Any]       =     'sail/poolformer_s12'
SCREAMING_SNAKE_CASE__					:     Dict       =     'tabby, tabby cat'

SCREAMING_SNAKE_CASE__					:     Tuple       =     [
    'sail/poolformer_s12',
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]




def    a__    (		snake_case__					:							Union[str, Any]						,   snake_case__					:							Union[str, Any] = 0.0						,   snake_case__					:							str = False     ):
  if drop_prob == 0.0 or not training:
    return input
  _UpperCAmelCase		:							Tuple							=     1 - drop_prob
  _UpperCAmelCase		:							Any							=     (input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
  _UpperCAmelCase		:							Dict							=     keep_prob + torch.rand(__UpperCamelCase						,   dtype=input.dtype						,   device=input.device     )
  random_tensor.floor_()  # binarize
  _UpperCAmelCase		:							Optional[int]							=     input.div(__UpperCamelCase     ) * random_tensor
  return output


class 		_SCREAMING_SNAKE_CASE     (						nn.Module     ):


     def __init__(  self							,					A_ = None    ):
       super().__init__()
       _UpperCAmelCase		:							Optional[Any]							=     drop_prob


     def        __snake_case(  self							,					A_    ):
       return drop_path(__UpperCAmelCase							,					self.drop_prob							,					self.training    )




     def        __snake_case(  self    ):
       return "p={}".format(self.drop_prob    )


class 		_SCREAMING_SNAKE_CASE     (						nn.Module     ):


     def __init__(  self							,					A_							,					A_							,					A_							,					A_							,					A_							,					A_=None    ):
       super().__init__()
       _UpperCAmelCase		:							Optional[Any]							=     patch_size if isinstance(__UpperCAmelCase							,					collections.abc.Iterable    ) else (patch_size, patch_size)
       _UpperCAmelCase		:							List[str]							=     stride if isinstance(__UpperCAmelCase							,					collections.abc.Iterable    ) else (stride, stride)
       _UpperCAmelCase		:							Union[str, Any]							=     padding if isinstance(__UpperCAmelCase							,					collections.abc.Iterable    ) else (padding, padding)

       _UpperCAmelCase		:							int							=     nn.Convad(__UpperCAmelCase							,					__UpperCAmelCase							,					kernel_size=__UpperCAmelCase							,					stride=__UpperCAmelCase							,					padding=__UpperCAmelCase    )
       _UpperCAmelCase		:							Optional[Any]							=     norm_layer(__UpperCAmelCase    ) if norm_layer else nn.Identity()




     def        __snake_case(  self							,					A_    ):
       _UpperCAmelCase		:							Dict							=     self.projection(__UpperCAmelCase    )
       _UpperCAmelCase		:							Union[str, Any]							=     self.norm(__UpperCAmelCase    )
       return embeddings


class 		_SCREAMING_SNAKE_CASE     (						nn.GroupNorm     ):


     def __init__(  self							,					A_							,					**A_    ):
       super().__init__(1							,					__UpperCAmelCase							,					**__UpperCAmelCase    )


class 		_SCREAMING_SNAKE_CASE     (						nn.Module     ):


     def __init__(  self							,					A_    ):
       super().__init__()
       _UpperCAmelCase		:							List[str]							=     nn.AvgPoolad(__UpperCAmelCase							,					stride=1							,					padding=pool_size // 2							,					count_include_pad=__UpperCAmelCase    )




     def        __snake_case(  self							,					A_    ):
       return self.pool(__UpperCAmelCase    ) - hidden_states


class 		_SCREAMING_SNAKE_CASE     (						nn.Module     ):


     def __init__(  self							,					A_							,					A_							,					A_							,					A_    ):
       super().__init__()
       _UpperCAmelCase		:							str							=     nn.Convad(__UpperCAmelCase							,					__UpperCAmelCase							,					1    )
       _UpperCAmelCase		:							int							=     nn.Convad(__UpperCAmelCase							,					__UpperCAmelCase							,					1    )
       _UpperCAmelCase		:							Any							=     PoolFormerDropPath(__UpperCAmelCase    )
       if isinstance(config.hidden_act							,					__UpperCAmelCase    ):
         _UpperCAmelCase		:							List[Any]							=     ACTaFN[config.hidden_act]
       else:
         _UpperCAmelCase		:							Union[str, Any]							=     config.hidden_act




     def        __snake_case(  self							,					A_    ):
       _UpperCAmelCase		:							Union[str, Any]							=     self.conva(__UpperCAmelCase    )
       _UpperCAmelCase		:							Union[str, Any]							=     self.act_fn(__UpperCAmelCase    )
       _UpperCAmelCase		:							Dict							=     self.drop(__UpperCAmelCase    )
       _UpperCAmelCase		:							Union[str, Any]							=     self.conva(__UpperCAmelCase    )
       _UpperCAmelCase		:							int							=     self.drop(__UpperCAmelCase    )

       return hidden_states


class 		_SCREAMING_SNAKE_CASE     (						nn.Module     ):


     def __init__(  self							,					A_							,					A_							,					A_							,					A_							,					A_							,					A_    ):
       super().__init__()
       _UpperCAmelCase		:							Any							=     PoolFormerPooling(__UpperCAmelCase    )
       _UpperCAmelCase		:							Any							=     PoolFormerOutput(__UpperCAmelCase							,					__UpperCAmelCase							,					__UpperCAmelCase							,					__UpperCAmelCase    )
       _UpperCAmelCase		:							Any							=     PoolFormerGroupNorm(__UpperCAmelCase    )
       _UpperCAmelCase		:							Dict							=     PoolFormerGroupNorm(__UpperCAmelCase    )

       # Useful for training neural nets
       _UpperCAmelCase		:							List[Any]							=     PoolFormerDropPath(__UpperCAmelCase    ) if drop_path > 0.0 else nn.Identity()
       _UpperCAmelCase		:							List[str]							=     config.use_layer_scale
       if config.use_layer_scale:
         _UpperCAmelCase		:							str							=     nn.Parameter(
             config.layer_scale_init_value * torch.ones((__UpperCAmelCase)    )							,					requires_grad=__UpperCAmelCase    )
         _UpperCAmelCase		:							int							=     nn.Parameter(
             config.layer_scale_init_value * torch.ones((__UpperCAmelCase)    )							,					requires_grad=__UpperCAmelCase    )




     def        __snake_case(  self							,					A_    ):



       if self.use_layer_scale:
         _UpperCAmelCase		:							Any							=     self.pooling(self.before_norm(__UpperCAmelCase    )    )
         _UpperCAmelCase		:							int							=     self.layer_scale_a.unsqueeze(-1    ).unsqueeze(-1    ) * pooling_output
         # First residual connection
         _UpperCAmelCase		:							Optional[int]							=     hidden_states + self.drop_path(__UpperCAmelCase    )
         _UpperCAmelCase		:							Dict							=     ()

         _UpperCAmelCase		:							List[Any]							=     self.output(self.after_norm(__UpperCAmelCase    )    )
         _UpperCAmelCase		:							Union[str, Any]							=     self.layer_scale_a.unsqueeze(-1    ).unsqueeze(-1    ) * layer_output
         # Second residual connection
         _UpperCAmelCase		:							Optional[Any]							=     hidden_states + self.drop_path(__UpperCAmelCase    )

         _UpperCAmelCase		:							Optional[int]							=     (output,) + outputs
         return outputs

       else:
         _UpperCAmelCase		:							Optional[int]							=     self.drop_path(self.pooling(self.before_norm(__UpperCAmelCase    )    )    )
         # First residual connection
         _UpperCAmelCase		:							Tuple							=     pooling_output + hidden_states
         _UpperCAmelCase		:							Tuple							=     ()

         # Second residual connection inside the PoolFormerOutput block
         _UpperCAmelCase		:							Optional[int]							=     self.drop_path(self.output(self.after_norm(__UpperCAmelCase    )    )    )
         _UpperCAmelCase		:							str							=     hidden_states + layer_output

         _UpperCAmelCase		:							Optional[Any]							=     (output,) + outputs
         return outputs


class 		_SCREAMING_SNAKE_CASE     (						nn.Module     ):


     def __init__(  self							,					A_    ):
       super().__init__()
       _UpperCAmelCase		:							Any							=     config
       # stochastic depth decay rule
       _UpperCAmelCase		:							List[str]							=     [x.item() for x in torch.linspace(0							,					config.drop_path_rate							,					sum(config.depths    )    )]

       # patch embeddings
       _UpperCAmelCase		:							List[Any]							=     []
       for i in range(config.num_encoder_blocks    ):
         embeddings.append(
             PoolFormerEmbeddings(
                 patch_size=config.patch_sizes[i]							,					stride=config.strides[i]							,					padding=config.padding[i]							,					num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1]							,					hidden_size=config.hidden_sizes[i]							,					)    )
       _UpperCAmelCase		:							Any							=     nn.ModuleList(__UpperCAmelCase    )

       # Transformer blocks
       _UpperCAmelCase		:							Optional[int]							=     []
       _UpperCAmelCase		:							List[Any]							=     0
       for i in range(config.num_encoder_blocks    ):
         # each block consists of layers
         _UpperCAmelCase		:							str							=     []
         if i != 0:
           cur += config.depths[i - 1]
         for j in range(config.depths[i]    ):
           layers.append(
               PoolFormerLayer(
                   __UpperCAmelCase							,					num_channels=config.hidden_sizes[i]							,					pool_size=config.pool_size							,					hidden_size=config.hidden_sizes[i]							,					intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio    )							,					drop_path=dpr[cur + j]							,					)    )
         blocks.append(nn.ModuleList(__UpperCAmelCase    )    )

       _UpperCAmelCase		:							Any							=     nn.ModuleList(__UpperCAmelCase    )




     def        __snake_case(  self							,					A_							,					A_=False							,					A_=True    ):
       _UpperCAmelCase		:							int							=     () if output_hidden_states else None

       _UpperCAmelCase		:							str							=     pixel_values
       for idx, layers in enumerate(zip(self.patch_embeddings							,					self.block    )    ):
         _UpperCAmelCase		:							Optional[Any]							=     layers
         # Get patch embeddings from hidden_states
         _UpperCAmelCase		:							Any							=     embedding_layer(__UpperCAmelCase    )
         # Send the embeddings through the blocks
         for _, blk in enumerate(__UpperCAmelCase    ):
           _UpperCAmelCase		:							List[Any]							=     blk(__UpperCAmelCase    )
           _UpperCAmelCase		:							Tuple							=     layer_outputs[0]

         if output_hidden_states:
           _UpperCAmelCase		:							Optional[Any]							=     all_hidden_states + (hidden_states,)

       if not return_dict:
         return tuple(v for v in [hidden_states, all_hidden_states] if v is not None    )

       return BaseModelOutputWithNoAttention(last_hidden_state=__UpperCAmelCase							,					hidden_states=__UpperCAmelCase    )


class 		_SCREAMING_SNAKE_CASE     (						A     ):
     __SCREAMING_SNAKE_CASE     					=    PoolFormerConfig
     __SCREAMING_SNAKE_CASE     					=    "poolformer"
     __SCREAMING_SNAKE_CASE     					=    "pixel_values"
     __SCREAMING_SNAKE_CASE     					=    True


     def        __snake_case(  self							,					A_    ):



       if isinstance(__UpperCAmelCase							,					(nn.Linear, nn.Convad)    ):
         module.weight.data.normal_(mean=0.0							,					std=self.config.initializer_range    )
         if module.bias is not None:
           module.bias.data.zero_()
       elif isinstance(__UpperCAmelCase							,					nn.LayerNorm    ):
         module.bias.data.zero_()
         module.weight.data.fill_(1.0    )




     def        __snake_case(  self							,					A_							,					A_=False    ):



       if isinstance(__UpperCAmelCase							,					__UpperCAmelCase    ):
         _UpperCAmelCase		:							Dict							=     value


SCREAMING_SNAKE_CASE__					:     int       =     R'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

SCREAMING_SNAKE_CASE__					:     List[Any]       =     R'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`PoolFormerImageProcessor.__call__`] for details.\n'


@add_start_docstrings(
    '''The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.'''							,	A							,	)
class 		_SCREAMING_SNAKE_CASE     (						A     ):


     def __init__(  self							,					A_    ):
       super().__init__(__UpperCAmelCase    )
       _UpperCAmelCase		:							Optional[Any]							=     config

       _UpperCAmelCase		:							int							=     PoolFormerEncoder(__UpperCAmelCase    )

       # Initialize weights and apply final processing
       self.post_init()


     def        __snake_case(  self    ):
       return self.embeddings.patch_embeddings




     @add_start_docstrings_to_model_forward(__UpperCAmelCase    )
     @add_code_sample_docstrings(
         checkpoint=_CHECKPOINT_FOR_DOC							,					output_type=__UpperCAmelCase							,					config_class=_CONFIG_FOR_DOC							,					modality="""vision"""							,					expected_output=_EXPECTED_OUTPUT_SHAPE							,					)
     def        __snake_case(  self							,					A_ = None							,					A_ = None							,					A_ = None							,					):
       _UpperCAmelCase		:							int							=     (
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       _UpperCAmelCase		:							str							=     return_dict if return_dict is not None else self.config.use_return_dict

       if pixel_values is None:
         raise ValueError("""You have to specify pixel_values"""    )

       _UpperCAmelCase		:							List[Any]							=     self.encoder(
           __UpperCAmelCase							,					output_hidden_states=__UpperCAmelCase							,					return_dict=__UpperCAmelCase							,					)
       _UpperCAmelCase		:							Any							=     encoder_outputs[0]

       if not return_dict:
         return (sequence_output, None) + encoder_outputs[1:]

       return BaseModelOutputWithNoAttention(
           last_hidden_state=__UpperCAmelCase							,					hidden_states=encoder_outputs.hidden_states							,					)


class 		_SCREAMING_SNAKE_CASE     (						nn.Module     ):


     def __init__(  self							,					A_    ):
       super().__init__()
       _UpperCAmelCase		:							Any							=     nn.Linear(config.hidden_size							,					config.hidden_size    )




     def        __snake_case(  self							,					A_    ):
       _UpperCAmelCase		:							Any							=     self.dense(__UpperCAmelCase    )
       return output


@add_start_docstrings(
    '''\n    PoolFormer Model transformer with an image classification head on top\n    '''							,	A							,	)
class 		_SCREAMING_SNAKE_CASE     (						A     ):


     def __init__(  self							,					A_    ):
       super().__init__(__UpperCAmelCase    )
       _UpperCAmelCase		:							Optional[Any]							=     config.num_labels
       _UpperCAmelCase		:							int							=     PoolFormerModel(__UpperCAmelCase    )

       # Final norm
       _UpperCAmelCase		:							Any							=     PoolFormerGroupNorm(config.hidden_sizes[-1]    )
       # Classifier head
       _UpperCAmelCase		:							Dict							=     (
           nn.Linear(config.hidden_sizes[-1]							,					config.num_labels    ) if config.num_labels > 0 else nn.Identity()
       )

       # Initialize weights and apply final processing
       self.post_init()




     @add_start_docstrings_to_model_forward(__UpperCAmelCase    )
     @add_code_sample_docstrings(
         checkpoint=_IMAGE_CLASS_CHECKPOINT							,					output_type=__UpperCAmelCase							,					config_class=_CONFIG_FOR_DOC							,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT							,					)
     def        __snake_case(  self							,					A_ = None							,					A_ = None							,					A_ = None							,					A_ = None							,					):
       _UpperCAmelCase		:							str							=     return_dict if return_dict is not None else self.config.use_return_dict

       _UpperCAmelCase		:							str							=     self.poolformer(
           __UpperCAmelCase							,					output_hidden_states=__UpperCAmelCase							,					return_dict=__UpperCAmelCase							,					)

       _UpperCAmelCase		:							Optional[Any]							=     outputs[0]

       _UpperCAmelCase		:							Union[str, Any]							=     self.classifier(self.norm(__UpperCAmelCase    ).mean([-2, -1]    )    )

       _UpperCAmelCase		:							List[str]							=     None
       if labels is not None:
         if self.config.problem_type is None:
           if self.num_labels == 1:
             _UpperCAmelCase		:							str							=     "regression"
           elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
             _UpperCAmelCase		:							List[str]							=     "single_label_classification"
           else:
             _UpperCAmelCase		:							str							=     "multi_label_classification"

         if self.config.problem_type == "regression":
           _UpperCAmelCase		:							int							=     MSELoss()
           if self.num_labels == 1:
             _UpperCAmelCase		:							str							=     loss_fct(logits.squeeze()							,					labels.squeeze()    )
           else:
             _UpperCAmelCase		:							Union[str, Any]							=     loss_fct(__UpperCAmelCase							,					__UpperCAmelCase    )
         elif self.config.problem_type == "single_label_classification":
           _UpperCAmelCase		:							Any							=     CrossEntropyLoss()
           _UpperCAmelCase		:							Optional[Any]							=     loss_fct(logits.view(-1							,					self.num_labels    )							,					labels.view(-1    )    )
         elif self.config.problem_type == "multi_label_classification":
           _UpperCAmelCase		:							Union[str, Any]							=     BCEWithLogitsLoss()
           _UpperCAmelCase		:							Dict							=     loss_fct(__UpperCAmelCase							,					__UpperCAmelCase    )

       if not return_dict:
         _UpperCAmelCase		:							List[Any]							=     (logits,) + outputs[2:]
         return ((loss,) + output) if loss is not None else output

       return ImageClassifierOutputWithNoAttention(loss=__UpperCAmelCase							,					logits=__UpperCAmelCase							,					hidden_states=outputs.hidden_states    )










import argparse
import json
from pathlib import Path

import requests
import timm
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
from timm.data import resolve_data_config
from timm.data.transforms_factory import create_transform

from transformers import (
    BitConfig,
    ViTHybridConfig,
    ViTHybridForImageClassification,
    ViTHybridImageProcessor,
    ViTHybridModel,
)
from transformers.image_utils import PILImageResampling
from transformers.utils import logging


logging.set_verbosity_info()
lowerCamelCase		       =      logging.get_logger(__name__)






def     SCREAMING_SNAKE_CASE(      __UpperCamelCase	,				__UpperCamelCase=False )	->						Tuple:
				a__	:							int      						= []

				# fmt: off
				# stem:
				rename_keys.append(("cls_token", "vit.embeddings.cls_token") )
				rename_keys.append(("pos_embed", "vit.embeddings.position_embeddings") )

				rename_keys.append(("patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight") )
				rename_keys.append(("patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias") )

				# backbone
				rename_keys.append(("patch_embed.backbone.stem.conv.weight", "vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight") )
				rename_keys.append(("patch_embed.backbone.stem.norm.weight", "vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight") )
				rename_keys.append(("patch_embed.backbone.stem.norm.bias", "vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias") )

				for stage_idx in range(len(config.backbone_config.depths ) ):
								for layer_idx in range(config.backbone_config.depths[stage_idx] ):
												rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight') )
												rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight') )
												rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias') )
												rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight') )
												rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight') )
												rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias') )
												rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight') )
												rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight') )
												rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias') )

								rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight') )
								rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight') )
								rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias') )

				# transformer encoder
				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') )

				if base_model:
								# layernorm + pooler
								rename_keys.extend(
								    [
								        ("norm.weight", "layernorm.weight"),
								        ("norm.bias", "layernorm.bias"),
								        ("pre_logits.fc.weight", "pooler.dense.weight"),
								        ("pre_logits.fc.bias", "pooler.dense.bias"),
								    ] )

								# if just the base model, we should remove "vit" from all keys that start with "vit"
								a__	:							Dict      						= [(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"),
								    ] )
				# fmt: on

				return rename_keys






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






def     SCREAMING_SNAKE_CASE(      __UpperCamelCase )	->						Tuple:
				a__	:							Optional[int]      						= ["head.weight", "head.bias"]
				for k in ignore_keys:
								state_dict.pop(__UpperCamelCase	,				__UpperCamelCase )






def     SCREAMING_SNAKE_CASE(      __UpperCamelCase	,				__UpperCamelCase	,				__UpperCamelCase )	->						Optional[Any]:
				a__	:							Optional[Any]      						= dct.pop(__UpperCamelCase )
				a__	:							str      						= val






def     SCREAMING_SNAKE_CASE(      )	->						str:
				a__	:							Optional[int]      						= "http://images.cocodataset.org/val2017/000000039769.jpg"
				a__	:							Any      						= Image.open(requests.get(__UpperCamelCase	,				stream=__UpperCamelCase ).raw )
				return im






@torch.no_grad()
def     SCREAMING_SNAKE_CASE(      __UpperCamelCase	,				__UpperCamelCase	,				__UpperCamelCase=False )	->						Tuple:
				a__	:							Dict      						= BitConfig(
				    global_padding="same"	,				layer_type="bottleneck"	,				depths=(3, 4, 9)	,				out_features=["stage3"]	,				embedding_dynamic_padding=__UpperCamelCase	,				)
				a__	:							List[Any]      						= ViTHybridConfig(backbone_config=__UpperCamelCase	,				image_size=3_84	,				num_labels=10_00 )
				a__	:							Union[str, Any]      						= False

				# load original model from timm
				a__	:							List[Any]      						= timm.create_model(__UpperCamelCase	,				pretrained=__UpperCamelCase )
				timm_model.eval()

				# load state_dict of original model, remove and rename some keys
				a__	:							Optional[Any]      						= timm_model.state_dict()
				if base_model:
								remove_classification_head_(__UpperCamelCase )
				a__	:							Union[str, Any]      						= create_rename_keys(__UpperCamelCase	,				__UpperCamelCase )
				for src, dest in rename_keys:
								rename_key(__UpperCamelCase	,				__UpperCamelCase	,				__UpperCamelCase )
				read_in_q_k_v(__UpperCamelCase	,				__UpperCamelCase	,				__UpperCamelCase )

				a__	:							str      						= "huggingface/label-files"
				a__	:							Optional[Any]      						= "imagenet-1k-id2label.json"
				a__	:							int      						= json.load(open(hf_hub_download(__UpperCamelCase	,				__UpperCamelCase	,				repo_type="dataset" )	,				"r" ) )
				a__	:							Any      						= {int(__UpperCamelCase ): v for k, v in idalabel.items()}
				a__	:							Dict      						= idalabel
				a__	:							List[str]      						= {v: k for k, v in idalabel.items()}

				# load HuggingFace model
				if vit_name[-5:] == "in21k":
								a__	:							str      						= ViTHybridModel(__UpperCamelCase ).eval()
				else:
								a__	:							Dict      						= ViTHybridForImageClassification(__UpperCamelCase ).eval()
				model.load_state_dict(__UpperCamelCase )

				# create image processor
				a__	:							Union[str, Any]      						= create_transform(**resolve_data_config({}	,				model=__UpperCamelCase ) )
				a__	:							str      						= transform.transforms

				a__	:							List[Any]      						= {
				    "bilinear": PILImageResampling.BILINEAR,
				    "bicubic": PILImageResampling.BICUBIC,
				    "nearest": PILImageResampling.NEAREST,
				}

				a__	:							List[Any]      						= ViTHybridImageProcessor(
				    do_resize=__UpperCamelCase	,				size={"shortest_edge": timm_transforms[0].size}	,				resample=pillow_resamplings[timm_transforms[0].interpolation.value]	,				do_center_crop=__UpperCamelCase	,				crop_size={"height": timm_transforms[1].size[0], "width": timm_transforms[1].size[1]}	,				do_normalize=__UpperCamelCase	,				image_mean=timm_transforms[-1].mean.tolist()	,				image_std=timm_transforms[-1].std.tolist()	,				)

				a__	:							Optional[Any]      						= prepare_img()
				a__	:							List[str]      						= transform(__UpperCamelCase ).unsqueeze(0 )
				a__	:							Optional[Any]      						= processor(__UpperCamelCase	,				return_tensors="pt" ).pixel_values

				# verify pixel values
				assert torch.allclose(__UpperCamelCase	,				__UpperCamelCase )

				# verify logits
				with torch.no_grad():
								a__	:							Dict      						= model(__UpperCamelCase )
								a__	:							Optional[Any]      						= outputs.logits

				print("Predicted class:"	,				logits.argmax(-1 ).item() )
				if base_model:
								a__	:							Optional[int]      						= timm_model.forward_features(__UpperCamelCase )
								assert timm_pooled_output.shape == outputs.pooler_output.shape
								assert torch.allclose(__UpperCamelCase	,				outputs.pooler_output	,				atol=1e-3 )
				else:
								a__	:							List[Any]      						= timm_model(__UpperCamelCase )
								assert timm_logits.shape == outputs.logits.shape
								assert torch.allclose(__UpperCamelCase	,				outputs.logits	,				atol=1e-3 )
				print("Looks ok!" )

				if pytorch_dump_folder_path is not None:
								Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase )
								print(F'Saving model {vit_name} to {pytorch_dump_folder_path}' )
								model.save_pretrained(__UpperCamelCase )
								print(F'Saving processor to {pytorch_dump_folder_path}' )
								processor.save_pretrained(__UpperCamelCase )

				if push_to_hub:
								print(F'Pushing model and processor to the hub {vit_name}' )
								model.push_to_hub(F'ybelkada/{vit_name}' )
								processor.push_to_hub(F'ybelkada/{vit_name}' )


if __name__ == "__main__":
						lowerCamelCase		       =      argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    """--vit_name""",
						    default="""vit_base_r50_s16_384""",
						    type=str,
						    help="""Name of the hybrid ViT 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."""
						)
						parser.add_argument(
						    """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub."""
						)

						lowerCamelCase		       =      parser.parse_args()
						convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)


