



'''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    __lowerCamelCase						(      _lowercase	)   ->    List[str]:
							UpperCAmelCase  :	Tuple							  =	SwinConfig()
							UpperCAmelCase  :	Optional[Any]							  =	swin_name.split("""_"""	)

							UpperCAmelCase  :	Any							  =	name_split[1]
							UpperCAmelCase  :	Dict							  =	int(name_split[4]	)
							UpperCAmelCase  :	Optional[int]							  =	int(name_split[3][-1]	)

							if model_size == "tiny":
														UpperCAmelCase  :	Optional[Any]							  =	9_6
														UpperCAmelCase  :	Union[str, Any]							  =	(2, 2, 6, 2)
														UpperCAmelCase  :	Union[str, Any]							  =	(3, 6, 1_2, 2_4)
							elif model_size == "small":
														UpperCAmelCase  :	Union[str, Any]							  =	9_6
														UpperCAmelCase  :	List[str]							  =	(2, 2, 1_8, 2)
														UpperCAmelCase  :	List[Any]							  =	(3, 6, 1_2, 2_4)
							elif model_size == "base":
														UpperCAmelCase  :	Tuple							  =	1_2_8
														UpperCAmelCase  :	List[Any]							  =	(2, 2, 1_8, 2)
														UpperCAmelCase  :	Any							  =	(4, 8, 1_6, 3_2)
							else:
														UpperCAmelCase  :	Optional[Any]							  =	1_9_2
														UpperCAmelCase  :	Tuple							  =	(2, 2, 1_8, 2)
														UpperCAmelCase  :	Dict							  =	(6, 1_2, 2_4, 4_8)

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

							UpperCAmelCase  :	str							  =	img_size
							UpperCAmelCase  :	Union[str, Any]							  =	num_classes
							UpperCAmelCase  :	Union[str, Any]							  =	embed_dim
							UpperCAmelCase  :	List[str]							  =	depths
							UpperCAmelCase  :	Dict							  =	num_heads
							UpperCAmelCase  :	List[Any]							  =	window_size

							return config



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

							if name == "norm.weight":
														UpperCAmelCase  :	int							  =	"""layernorm.weight"""
							if name == "norm.bias":
														UpperCAmelCase  :	List[str]							  =	"""layernorm.bias"""

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

							return name



def    __lowerCamelCase						(      _lowercase			,       _lowercase	)   ->    Optional[int]:
							for key in orig_state_dict.copy().keys():
														UpperCAmelCase  :	List[str]							  =	orig_state_dict.pop(_lowercase	)

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

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

							return orig_state_dict



def    __lowerCamelCase						(      _lowercase			,       _lowercase	)   ->    Optional[Any]:
							UpperCAmelCase  :	Dict							  =	timm.create_model(_lowercase			,       pretrained=_lowercase	)
							timm_model.eval()

							UpperCAmelCase  :	Tuple							  =	get_swin_config(_lowercase	)
							UpperCAmelCase  :	Optional[Any]							  =	SwinForImageClassification(_lowercase	)
							model.eval()

							UpperCAmelCase  :	Optional[Any]							  =	convert_state_dict(timm_model.state_dict()			,       _lowercase	)
							model.load_state_dict(_lowercase	)

							UpperCAmelCase  :	Any							  =	"""http://images.cocodataset.org/val2017/000000039769.jpg"""

							UpperCAmelCase  :	List[Any]							  =	AutoImageProcessor.from_pretrained("""microsoft/{}""".format(swin_name.replace("""_"""			,       """-"""	)	)	)
							UpperCAmelCase  :	Optional[Any]							  =	Image.open(requests.get(_lowercase			,       stream=_lowercase	).raw	)
							UpperCAmelCase  :	List[str]							  =	image_processor(images=_lowercase			,       return_tensors="""pt"""	)

							UpperCAmelCase  :	List[Any]							  =	timm_model(inputs["""pixel_values"""]	)
							UpperCAmelCase  :	str							  =	model(**_lowercase	).logits

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

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

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


if __name__ == "__main__":
							a     :    Any      						=					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     :    List[Any]      						=					parser.parse_args()
							convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)






'''simple docstring'''




from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


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

class     UpperCamelCase_				(							__magic_name__    ):





	def 				_lowercase(						self	,	A					)    ->    Optional[int]:
								if isinstance(A	,	A					):
															UpperCAmelCase  :	Union[str, Any]							  =	[label.strip() for label in labels.split(""","""					) if label.strip()]
								return labels






	def __call__(						self	,	A	,	A	,	A					)    ->    str:
								if len(A					) == 0 or len(A					) == 0:
															raise ValueError("""You must include at least one label and at least one sequence."""					)
								if hypothesis_template.format(labels[0]					) == hypothesis_template:
															raise ValueError(
															    (
															        """The provided hypothesis_template \"{}\" was not able to be formatted with the target labels. """
															        """Make sure the passed template includes formatting syntax such as {{}} where the label should go."""
															    ).format(A					)					)

								if isinstance(A	,	A					):
															UpperCAmelCase  :	Tuple							  =	[sequences]

								UpperCAmelCase  :	Optional[Any]							  =	[]
								for sequence in sequences:
															sequence_pairs.extend([[sequence, hypothesis_template.format(A					)] for label in labels]					)

								return sequence_pairs, sequences




@add_end_docstrings(__magic_name__    )
class     UpperCamelCase_				(							__magic_name__    ):





	def __init__(						self	,	A=ZeroShotClassificationArgumentHandler()	,	*A	,	**A					)    ->    Optional[int]:
								UpperCAmelCase  :	Tuple							  =	args_parser
								super().__init__(*A	,	**A					)
								if self.entailment_id == -1:
															logger.warning(
															    """Failed to determine 'entailment' label id from the label2id mapping in the model config. Setting to """
															    """-1. Define a descriptive label2id mapping in the model config to ensure correct outputs."""					)





	@property
	def 				_lowercase(						self					)    ->    List[Any]:
								for label, ind in self.model.config.labelaid.items():
															if label.lower().startswith("""entail"""					):
																						return ind
								return -1





	def 				_lowercase(						self	,	A	,	A=True	,	A=True	,	A=TruncationStrategy.ONLY_FIRST	,	**A					)    ->    str:
								UpperCAmelCase  :	Tuple							  =	self.framework
								if self.tokenizer.pad_token is None:
															# Override for tokenizers not supporting padding
															logger.error(
															    """Tokenizer was not supporting padding necessary for zero-shot, attempting to use """
															    """ `pad_token=eos_token`"""					)
															UpperCAmelCase  :	Any							  =	self.tokenizer.eos_token
								try:
															UpperCAmelCase  :	Tuple							  =	self.tokenizer(
															    A	,	add_special_tokens=A	,	return_tensors=A	,	padding=A	,	truncation=A	,	)
								except Exception as e:
															if "too short" in str(A					):
																						# tokenizers might yell that we want to truncate
																						# to a value that is not even reached by the input.
																						# In that case we don't want to truncate.
																						# It seems there's not a really better way to catch that
																						# exception.

																						UpperCAmelCase  :	List[str]							  =	self.tokenizer(
																						    A	,	add_special_tokens=A	,	return_tensors=A	,	padding=A	,	truncation=TruncationStrategy.DO_NOT_TRUNCATE	,	)
															else:
																						raise e

								return inputs





	def 				_lowercase(						self	,	**A					)    ->    Tuple:
								if kwargs.get("""multi_class"""	,	A					) is not None:
															UpperCAmelCase  :	Any							  =	kwargs["""multi_class"""]
															logger.warning(
															    """The `multi_class` argument has been deprecated and renamed to `multi_label`. """
															    """`multi_class` will be removed in a future version of Transformers."""					)
								UpperCAmelCase  :	int							  =	{}
								if "candidate_labels" in kwargs:
															UpperCAmelCase  :	Tuple							  =	self._args_parser._parse_labels(kwargs["""candidate_labels"""]					)
								if "hypothesis_template" in kwargs:
															UpperCAmelCase  :	List[Any]							  =	kwargs["""hypothesis_template"""]

								UpperCAmelCase  :	Dict							  =	{}
								if "multi_label" in kwargs:
															UpperCAmelCase  :	Union[str, Any]							  =	kwargs["""multi_label"""]
								return preprocess_params, {}, postprocess_params





	def __call__(						self	,	A	,	*A	,	**A	,	)    ->    Tuple:
								if len(A					) == 0:
															pass
								elif len(A					) == 1 and "candidate_labels" not in kwargs:
															UpperCAmelCase  :	Optional[Any]							  =	args[0]
								else:
															raise ValueError(f'''Unable to understand extra arguments {args}'''					)

								return super().__call__(A	,	**A					)





	def 				_lowercase(						self	,	A	,	A=None	,	A="This example is {}."					)    ->    List[Any]:
								UpperCAmelCase  ,  UpperCAmelCase  :	List[Any]							  =	self._args_parser(A	,	A	,	A					)

								for i, (candidate_label, sequence_pair) in enumerate(zip(A	,	A					)					):
															UpperCAmelCase  :	Any							  =	self._parse_and_tokenize([sequence_pair]					)

															yield {
															    "candidate_label": candidate_label,
															    "sequence": sequences[0],
															    "is_last": i == len(A					) - 1,
															    **model_input,
															}





	def 				_lowercase(						self	,	A					)    ->    Optional[int]:
								UpperCAmelCase  :	Optional[Any]							  =	inputs["""candidate_label"""]
								UpperCAmelCase  :	Tuple							  =	inputs["""sequence"""]
								UpperCAmelCase  :	List[Any]							  =	{k: inputs[k] for k in self.tokenizer.model_input_names}
								UpperCAmelCase  :	Tuple							  =	self.model(**A					)

								UpperCAmelCase  :	Optional[int]							  =	{
								    """candidate_label""": candidate_label,
								    """sequence""": sequence,
								    """is_last""": inputs["""is_last"""],
								    **outputs,
								}
								return model_outputs






	def 				_lowercase(						self	,	A	,	A=False					)    ->    List[str]:
								UpperCAmelCase  :	Dict							  =	[outputs["""candidate_label"""] for outputs in model_outputs]
								UpperCAmelCase  :	List[Any]							  =	[outputs["""sequence"""] for outputs in model_outputs]
								UpperCAmelCase  :	List[Any]							  =	np.concatenate([output["""logits"""].numpy() for output in model_outputs]					)
								UpperCAmelCase  :	Optional[Any]							  =	logits.shape[0]
								UpperCAmelCase  :	int							  =	len(A					)
								UpperCAmelCase  :	List[Any]							  =	N // n
								UpperCAmelCase  :	int							  =	logits.reshape((num_sequences, n, -1)					)

								if multi_label or len(A					) == 1:
															# softmax over the entailment vs. contradiction dim for each label independently
															UpperCAmelCase  :	str							  =	self.entailment_id
															UpperCAmelCase  :	str							  =	-1 if entailment_id == 0 else 0
															UpperCAmelCase  :	Optional[Any]							  =	reshaped_outputs[..., [contradiction_id, entailment_id]]
															UpperCAmelCase  :	int							  =	np.exp(A					) / np.exp(A					).sum(-1	,	keepdims=A					)
															UpperCAmelCase  :	int							  =	scores[..., 1]
								else:
															# softmax the "entailment" logits over all candidate labels
															UpperCAmelCase  :	Dict							  =	reshaped_outputs[..., self.entailment_id]
															UpperCAmelCase  :	Optional[int]							  =	np.exp(A					) / np.exp(A					).sum(-1	,	keepdims=A					)

								UpperCAmelCase  :	int							  =	list(reversed(scores[0].argsort()					)					)
								return {
								    "sequence": sequences[0],
								    "labels": [candidate_labels[i] for i in top_inds],
								    "scores": scores[0, top_inds].tolist(),
								}

