






import argparse
import json
import os

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

from transformers import ConvNextImageProcessor, ConvNextVaConfig, ConvNextVaForImageClassification
from transformers.image_utils import PILImageResampling
from transformers.utils import logging


logging.set_verbosity_info()
__UpperCamelCase   :						Optional[Any]							=  logging.get_logger(__name__)
def        a_	( _A					)     ->     Union[str, Any]:







				"""simple docstring"""
				snake_case__													=       ConvNextVaConfig()

				if "atto" in checkpoint_url:
								snake_case__													=       [2, 2, 6, 2]
								snake_case__													=       [40, 80, 160, 320]
				if "femto" in checkpoint_url:
								snake_case__													=       [2, 2, 6, 2]
								snake_case__													=       [48, 96, 192, 384]
				if "pico" in checkpoint_url:
								snake_case__													=       [2, 2, 6, 2]
								snake_case__													=       [64, 128, 256, 512]
				if "nano" in checkpoint_url:
								snake_case__													=       [2, 2, 8, 2]
								snake_case__													=       [80, 160, 320, 640]
				if "tiny" in checkpoint_url:
								snake_case__													=       [3, 3, 9, 3]
								snake_case__													=       [96, 192, 384, 768]
				if "base" in checkpoint_url:
								snake_case__													=       [3, 3, 27, 3]
								snake_case__													=       [128, 256, 512, 1024]
				if "large" in checkpoint_url:
								snake_case__													=       [3, 3, 27, 3]
								snake_case__													=       [192, 384, 768, 1536]
				if "huge" in checkpoint_url:
								snake_case__													=       [3, 3, 27, 3]
								snake_case__													=       [352, 704, 1408, 2816]

				snake_case__													=       1000
				snake_case__													=       'imagenet-1k-id2label.json'
				snake_case__													=       (1, 1000)

				snake_case__													=       'huggingface/label-files'
				snake_case__													=       num_labels
				snake_case__													=       json.load(open(hf_hub_download(_A ,				_A ,				repo_type='dataset'					) ,				'r'					)					)
				snake_case__													=       {int(_A					): v for k, v in idalabel.items()}

				snake_case__													=       idalabel
				snake_case__													=       {v: k for k, v in idalabel.items()}
				snake_case__													=       hidden_sizes
				snake_case__													=       depths

				return config, expected_shape
def        a_	( _A					)     ->     Union[str, Any]:







				"""simple docstring"""
				if "downsample_layers.0.0" in name:
								snake_case__													=       name.replace('downsample_layers.0.0' ,				'embeddings.patch_embeddings'					)
				if "downsample_layers.0.1" in name:
								snake_case__													=       name.replace('downsample_layers.0.1' ,				'embeddings.norm'					)  # we rename to layernorm later on
				if "downsample_layers.1.0" in name:
								snake_case__													=       name.replace('downsample_layers.1.0' ,				'stages.1.downsampling_layer.0'					)
				if "downsample_layers.1.1" in name:
								snake_case__													=       name.replace('downsample_layers.1.1' ,				'stages.1.downsampling_layer.1'					)
				if "downsample_layers.2.0" in name:
								snake_case__													=       name.replace('downsample_layers.2.0' ,				'stages.2.downsampling_layer.0'					)
				if "downsample_layers.2.1" in name:
								snake_case__													=       name.replace('downsample_layers.2.1' ,				'stages.2.downsampling_layer.1'					)
				if "downsample_layers.3.0" in name:
								snake_case__													=       name.replace('downsample_layers.3.0' ,				'stages.3.downsampling_layer.0'					)
				if "downsample_layers.3.1" in name:
								snake_case__													=       name.replace('downsample_layers.3.1' ,				'stages.3.downsampling_layer.1'					)
				if "stages" in name and "downsampling_layer" not in name:
								# stages.0.0. for instance should be renamed to stages.0.layers.0.
								snake_case__													=       name[: len('stages.0'					)] + '.layers' + name[len('stages.0'					) :]
				if "gamma" in name:
								snake_case__													=       name.replace('gamma' ,				'weight'					)
				if "beta" in name:
								snake_case__													=       name.replace('beta' ,				'bias'					)
				if "stages" in name:
								snake_case__													=       name.replace('stages' ,				'encoder.stages'					)
				if "norm" in name:
								snake_case__													=       name.replace('norm' ,				'layernorm'					)
				if "head" in name:
								snake_case__													=       name.replace('head' ,				'classifier'					)

				return name
def        a_	( )     ->     str:







				"""simple docstring"""
				snake_case__													=       'http://images.cocodataset.org/val2017/000000039769.jpg'
				snake_case__													=       Image.open(requests.get(_A ,				stream=_A					).raw					)
				return im
def        a_	( _A					)     ->     Union[str, Any]:







				"""simple docstring"""
				if "224" in checkpoint_url:
								snake_case__													=       224
								snake_case__													=       224 / 256
				elif "384" in checkpoint_url:
								snake_case__													=       384
								snake_case__													=       None
				else:
								snake_case__													=       512
								snake_case__													=       None

				return ConvNextImageProcessor(
				    size=_A ,				crop_pct=_A ,				image_mean=[0.485, 0.456, 0.406] ,				image_std=[0.229, 0.224, 0.225] ,				resample=PILImageResampling.BICUBIC ,				)
@torch.no_grad()
def        a_	( _A ,				_A ,				_A ,				_A					)     ->     Dict:







				"""simple docstring"""
				print('Downloading original model from checkpoint...'					)
				# define ConvNeXTV2 configuration based on URL
				snake_case__							,  snake_case__													=       get_convnextva_config(_A					)
				# load original state_dict from URL
				snake_case__													=       torch.hub.load_state_dict_from_url(_A					)['model']

				print('Converting model parameters...'					)
				# rename keys
				for key in state_dict.copy().keys():
								snake_case__													=       state_dict.pop(_A					)
								snake_case__													=       val
				# add prefix to all keys expect classifier head
				for key in state_dict.copy().keys():
								snake_case__													=       state_dict.pop(_A					)
								if not key.startswith('classifier'					):
												snake_case__													=       'convnextv2.' + key
								snake_case__													=       val

				# load HuggingFace model
				snake_case__													=       ConvNextVaForImageClassification(_A					)
				model.load_state_dict(_A					)
				model.eval()

				# Check outputs on an image, prepared by ConvNextImageProcessor
				snake_case__													=       convert_preprocessor(_A					)
				snake_case__													=       preprocessor(images=prepare_img() ,				return_tensors='pt'					)
				snake_case__													=       model(**_A					).logits

				# note: the logits below were obtained without center cropping
				if checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im1k/convnextv2_atto_1k_224_ema.pt":
								snake_case__													=       torch.tensor([-0.3930, 0.1747, -0.5246, 0.4177, 0.4295]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im1k/convnextv2_femto_1k_224_ema.pt":
								snake_case__													=       torch.tensor([-0.1727, -0.5341, -0.7818, -0.4745, -0.6566]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im1k/convnextv2_pico_1k_224_ema.pt":
								snake_case__													=       torch.tensor([-0.0333, 0.1563, -0.9137, 0.1054, 0.0381]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im1k/convnextv2_nano_1k_224_ema.pt":
								snake_case__													=       torch.tensor([-0.1744, -0.1555, -0.0713, 0.0950, -0.1431]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im1k/convnextv2_tiny_1k_224_ema.pt":
								snake_case__													=       torch.tensor([0.9996, 0.1966, -0.4386, -0.3472, 0.6661]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im1k/convnextv2_base_1k_224_ema.pt":
								snake_case__													=       torch.tensor([-0.2553, -0.6708, -0.1359, 0.2518, -0.2488]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im1k/convnextv2_large_1k_224_ema.pt":
								snake_case__													=       torch.tensor([-0.0673, -0.5627, -0.3753, -0.2722, 0.0178]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im1k/convnextv2_huge_1k_224_ema.pt":
								snake_case__													=       torch.tensor([-0.6377, -0.7458, -0.2150, 0.1184, -0.0597]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im22k/convnextv2_nano_22k_224_ema.pt":
								snake_case__													=       torch.tensor([1.0799, 0.2322, -0.8860, 1.0219, 0.6231]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im22k/convnextv2_nano_22k_384_ema.pt":
								snake_case__													=       torch.tensor([0.3766, 0.4917, -1.1426, 0.9942, 0.6024]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im22k/convnextv2_tiny_22k_224_ema.pt":
								snake_case__													=       torch.tensor([0.4220, -0.6919, -0.4317, -0.2881, -0.6609]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im22k/convnextv2_tiny_22k_384_ema.pt":
								snake_case__													=       torch.tensor([0.1082, -0.8286, -0.5095, 0.4681, -0.8085]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im22k/convnextv2_base_22k_224_ema.pt":
								snake_case__													=       torch.tensor([-0.2419, -0.6221, 0.2176, -0.0980, -0.7527]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im22k/convnextv2_base_22k_384_ema.pt":
								snake_case__													=       torch.tensor([0.0391, -0.4371, 0.3786, 0.1251, -0.2784]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im22k/convnextv2_large_22k_224_ema.pt":
								snake_case__													=       torch.tensor([-0.0504, 0.5636, -0.1729, -0.6507, -0.3949]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im22k/convnextv2_large_22k_384_ema.pt":
								snake_case__													=       torch.tensor([0.3560, 0.9486, 0.3149, -0.2667, -0.5138]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im22k/convnextv2_huge_22k_384_ema.pt":
								snake_case__													=       torch.tensor([-0.2469, -0.4550, -0.5853, -0.0810, 0.0309]					)
				elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnextv2/im22k/convnextv2_huge_22k_512_ema.pt":
								snake_case__													=       torch.tensor([-0.3090, 0.0802, -0.0682, -0.1979, -0.2826]					)
				else:
								raise ValueError(f'''Unknown URL: {checkpoint_url}'''					)

				assert torch.allclose(logits[0, :5] ,				_A ,				atol=1e-3					)
				assert logits.shape == expected_shape
				print('Model outputs match the original results!'					)

				if save_model:
								print('Saving model to local...'					)
								# Create folder to save model
								if not os.path.isdir(_A					):
												os.mkdir(_A					)

								model.save_pretrained(_A					)
								preprocessor.save_pretrained(_A					)

				snake_case__													=       'convnextv2'
				if "atto" in checkpoint_url:
								model_name += "-atto"
				if "femto" in checkpoint_url:
								model_name += "-femto"
				if "pico" in checkpoint_url:
								model_name += "-pico"
				if "nano" in checkpoint_url:
								model_name += "-nano"
				elif "tiny" in checkpoint_url:
								model_name += "-tiny"
				elif "base" in checkpoint_url:
								model_name += "-base"
				elif "large" in checkpoint_url:
								model_name += "-large"
				elif "huge" in checkpoint_url:
								model_name += "-huge"
				if "22k" in checkpoint_url and "1k" not in checkpoint_url:
								model_name += "-22k"
				elif "22k" in checkpoint_url and "1k" in checkpoint_url:
								model_name += "-22k-1k"
				elif "1k" in checkpoint_url:
								model_name += "-1k"
				if "224" in checkpoint_url:
								model_name += "-224"
				elif "384" in checkpoint_url:
								model_name += "-384"
				elif "512" in checkpoint_url:
								model_name += "-512"

				if push_to_hub:
								print(f'''Pushing {model_name} to the hub...'''					)
								model.push_to_hub(_A					)
								preprocessor.push_to_hub(_A					)


if __name__ == "__main__":
							__UpperCamelCase   :						Any							=  argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    """--checkpoint_url""",
							    default="""https://dl.fbaipublicfiles.com/convnext/convnextv2/im1k/convnextv2_atto_1k_224_ema.pt""",
							    type=str,
							    help="""URL of the original ConvNeXTV2 checkpoint you'd like to convert.""",
							)
							parser.add_argument(
							    """--pytorch_dump_folder_path""",
							    default="""model""",
							    type=str,
							    help="""Path to the output PyTorch model directory.""",
							)
							parser.add_argument("""--save_model""", action="""store_true""", help="""Save model to local""")
							parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Push model and image preprocessor to the hub""")

							__UpperCamelCase   :						Dict							=  parser.parse_args()
							convert_convnextva_checkpoint(
							    args.checkpoint_url, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub
							)












import inspect
from typing import Callable, List, Optional, Set, Tuple, Union

import torch
from packaging import version
from safetensors.torch import storage_ptr, storage_size
from torch import nn

from .utils import logging


__UpperCamelCase   :						int							=  [nn.LayerNorm]

__UpperCamelCase   :						List[str]							=  logging.get_logger(__name__)

__UpperCamelCase   :						str							=  version.parse(version.parse(torch.__version__).base_version)

__UpperCamelCase   :						List[str]							=  parsed_torch_version_base >= version.parse("""2.0""")
__UpperCamelCase   :						Dict							=  parsed_torch_version_base >= version.parse("""1.12""")
__UpperCamelCase   :						Tuple							=  parsed_torch_version_base >= version.parse("""1.11""")
__UpperCamelCase   :						Union[str, Any]							=  parsed_torch_version_base < version.parse("""1.11""")
def        a_	( _A ,				_A ,				_A ,				_A ,				self					)     ->     Any:







				"""simple docstring"""
				from torch import _softmax_backward_data

				if is_torch_less_than_1_11:
								return _softmax_backward_data(_A ,				_A ,				parent.dim ,				self					)
				else:
								return _softmax_backward_data(_A ,				_A ,				parent.dim ,				self.dtype					)
def        a_	( _A ,				_A ,				_A = 0					)     ->     nn.Linear:







				"""simple docstring"""
				snake_case__													=       index.to(layer.weight.device					)
				snake_case__													=       layer.weight.index_select(_A ,				_A					).clone().detach()
				if layer.bias is not None:
								if dim == 1:
												snake_case__													=       layer.bias.clone().detach()
								else:
												snake_case__													=       layer.bias[index].clone().detach()
				snake_case__													=       list(layer.weight.size()					)
				snake_case__													=       len(_A					)
				snake_case__													=       nn.Linear(new_size[1] ,				new_size[0] ,				bias=layer.bias is not None					).to(layer.weight.device					)
				snake_case__													=       False
				new_layer.weight.copy_(W.contiguous()					)
				snake_case__													=       True
				if layer.bias is not None:
								snake_case__													=       False
								new_layer.bias.copy_(b.contiguous()					)
								snake_case__													=       True
				return new_layer


class 					__SCREAMING_SNAKE_CASE(		nn.Module		):
							def __init__(       self: Union[str, Any]    ,		UpperCamelCase: Dict    ,		UpperCamelCase: List[Any]  )						->						Any:
											super().__init__()
											snake_case__													=       nf
											snake_case__													=       nn.Parameter(torch.empty(UpperCamelCase    ,		UpperCamelCase  )  )
											snake_case__													=       nn.Parameter(torch.zeros(UpperCamelCase  )  )
											nn.init.normal_(self.weight    ,		std=0.02  )


							def lowerCAmelCase_	(       self: Optional[Any]    ,		UpperCamelCase: int  )						->						List[Any]:
											snake_case__													=       x.size()[:-1] + (self.nf,)
											snake_case__													=       torch.addmm(self.bias    ,		x.view(-1    ,		x.size(-1  )  )    ,		self.weight  )
											snake_case__													=       x.view(UpperCamelCase  )
											return x


def        a_	( _A ,				_A ,				_A = 1					)     ->     ConvaD:







				"""simple docstring"""
				snake_case__													=       index.to(layer.weight.device					)
				snake_case__													=       layer.weight.index_select(_A ,				_A					).clone().detach()
				if dim == 0:
								snake_case__													=       layer.bias.clone().detach()
				else:
								snake_case__													=       layer.bias[index].clone().detach()
				snake_case__													=       list(layer.weight.size()					)
				snake_case__													=       len(_A					)
				snake_case__													=       ConvaD(new_size[1] ,				new_size[0]					).to(layer.weight.device					)
				snake_case__													=       False
				new_layer.weight.copy_(W.contiguous()					)
				snake_case__													=       True
				snake_case__													=       False
				new_layer.bias.copy_(b.contiguous()					)
				snake_case__													=       True
				return new_layer
def        a_	( _A ,				_A ,				_A = None					)     ->     Union[nn.Linear, ConvaD]:







				"""simple docstring"""
				if isinstance(_A ,				nn.Linear					):
								return prune_linear_layer(_A ,				_A ,				dim=0 if dim is None else dim					)
				elif isinstance(_A ,				_A					):
								return prune_convad_layer(_A ,				_A ,				dim=1 if dim is None else dim					)
				else:
								raise ValueError(f'''Can\'t prune layer of class {layer.__class__}'''					)
def        a_	( _A ,				_A ,				_A ,				*_A					)     ->     torch.Tensor:







				"""simple docstring"""
				assert len(_A					) > 0, f'''{input_tensors} has to be a tuple/list of tensors'''

				# inspect.signature exist since python 3.5 and is a python method -> no problem with backward compatibility
				snake_case__													=       len(inspect.signature(_A					).parameters					)
				if num_args_in_forward_chunk_fn != len(_A					):
								raise ValueError(
								    f'''forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(_A					)} input '''
								    'tensors are given'					)

				if chunk_size > 0:
								snake_case__													=       input_tensors[0].shape[chunk_dim]
								for input_tensor in input_tensors:
												if input_tensor.shape[chunk_dim] != tensor_shape:
																raise ValueError(
																    f'''All input tenors have to be of the same shape: {tensor_shape}, '''
																    f'''found shape {input_tensor.shape[chunk_dim]}'''					)

								if input_tensors[0].shape[chunk_dim] % chunk_size != 0:
												raise ValueError(
												    f'''The dimension to be chunked {input_tensors[0].shape[chunk_dim]} has to be a multiple of the chunk '''
												    f'''size {chunk_size}'''					)

								snake_case__													=       input_tensors[0].shape[chunk_dim] // chunk_size

								# chunk input tensor into tuples
								snake_case__													=       tuple(input_tensor.chunk(_A ,				dim=_A					) for input_tensor in input_tensors					)
								# apply forward fn to every tuple
								snake_case__													=       tuple(forward_fn(*_A					) for input_tensors_chunk in zip(*_A					)					)
								# concatenate output at same dimension
								return torch.cat(_A ,				dim=_A					)

				return forward_fn(*_A					)
def        a_	( _A ,				_A ,				_A ,				_A					)     ->     Tuple[Set[int], torch.LongTensor]:







				"""simple docstring"""
				snake_case__													=       torch.ones(_A ,				_A					)
				snake_case__													=       set(_A					) - already_pruned_heads  # Convert to set and remove already pruned heads
				for head in heads:
								# Compute how many pruned heads are before the head and move the index accordingly
								snake_case__													=       head - sum(1 if h < head else 0 for h in already_pruned_heads					)
								snake_case__													=       0
				snake_case__													=       mask.view(-1					).contiguous().eq(1					)
				snake_case__													=       torch.arange(len(_A					)					)[mask].long()
				return heads, index
def        a_	( *_A ,				_A = None					)     ->     Tuple[torch.Tensor, ...]:







				"""simple docstring"""
				return torch.meshgrid(*_A ,				indexing=_A					)


def        a_	( _A					)     ->     Tuple[torch.device, int, int]:







				"""simple docstring"""
				return tensor.device, storage_ptr(_A					), storage_size(_A					)




