



import argparse

import requests
import torch
from PIL import Image
from torchvision.transforms import Compose, Normalize, Resize, ToTensor

from transformers import SwinaSRConfig, SwinaSRForImageSuperResolution, SwinaSRImageProcessor

def      lowerCAmelCase_					(  _lowercase    :       int)							->	Tuple:







			"""simple docstring"""
			a__       :						str							=  SwinaSRConfig()

			if "Swin2SR_ClassicalSR_X4_64" in checkpoint_url:
						a__       :						Optional[int]							=  4
			elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url:
						a__       :						Tuple							=  4
						a__       :						str							=  48
						a__       :						int							=  """pixelshuffle_aux"""
			elif "Swin2SR_Lightweight_X2_64" in checkpoint_url:
						a__       :						Optional[Any]							=  [6, 6, 6, 6]
						a__       :						List[str]							=  60
						a__       :						List[Any]							=  [6, 6, 6, 6]
						a__       :						Any							=  """pixelshuffledirect"""
			elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url:
						a__       :						Dict							=  4
						a__       :						Tuple							=  """nearest+conv"""
			elif "Swin2SR_Jpeg_dynamic" in checkpoint_url:
						a__       :						Dict							=  1
						a__       :						str							=  1
						a__       :						List[str]							=  126
						a__       :						Optional[Any]							=  7
						a__       :						Tuple							=  255.0
						a__       :						Dict							=  """"""

			return config

def      lowerCAmelCase_					(  _lowercase    :       str						,       _lowercase    :       Optional[int])							->	List[Any]:







			"""simple docstring"""
			if "patch_embed.proj" in name and "layers" not in name:
						a__       :						str							=  name.replace("""patch_embed.proj"""						,       """embeddings.patch_embeddings.projection""")
			if "patch_embed.norm" in name:
						a__       :						str							=  name.replace("""patch_embed.norm"""						,       """embeddings.patch_embeddings.layernorm""")
			if "layers" in name:
						a__       :						Optional[Any]							=  name.replace("""layers"""						,       """encoder.stages""")
			if "residual_group.blocks" in name:
						a__       :						Union[str, Any]							=  name.replace("""residual_group.blocks"""						,       """layers""")
			if "attn.proj" in name:
						a__       :						int							=  name.replace("""attn.proj"""						,       """attention.output.dense""")
			if "attn" in name:
						a__       :						Any							=  name.replace("""attn"""						,       """attention.self""")
			if "norm1" in name:
						a__       :						int							=  name.replace("""norm1"""						,       """layernorm_before""")
			if "norm2" in name:
						a__       :						Dict							=  name.replace("""norm2"""						,       """layernorm_after""")
			if "mlp.fc1" in name:
						a__       :						Any							=  name.replace("""mlp.fc1"""						,       """intermediate.dense""")
			if "mlp.fc2" in name:
						a__       :						Dict							=  name.replace("""mlp.fc2"""						,       """output.dense""")
			if "q_bias" in name:
						a__       :						str							=  name.replace("""q_bias"""						,       """query.bias""")
			if "k_bias" in name:
						a__       :						Optional[int]							=  name.replace("""k_bias"""						,       """key.bias""")
			if "v_bias" in name:
						a__       :						str							=  name.replace("""v_bias"""						,       """value.bias""")
			if "cpb_mlp" in name:
						a__       :						Optional[int]							=  name.replace("""cpb_mlp"""						,       """continuous_position_bias_mlp""")
			if "patch_embed.proj" in name:
						a__       :						Any							=  name.replace("""patch_embed.proj"""						,       """patch_embed.projection""")

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

			if "conv_first" in name:
						a__       :						List[Any]							=  name.replace("""conv_first"""						,       """first_convolution""")

			if (
			    "upsample" in name
			    or "conv_before_upsample" in name
			    or "conv_bicubic" in name
			    or "conv_up" in name
			    or "conv_hr" in name
			    or "conv_last" in name
			    or "aux" in name
			):
						# heads
						if "conv_last" in name:
									a__       :						Dict							=  name.replace("""conv_last"""						,       """final_convolution""")
						if config.upsampler in ["pixelshuffle", "pixelshuffle_aux", "nearest+conv"]:
									if "conv_before_upsample.0" in name:
												a__       :						Any							=  name.replace("""conv_before_upsample.0"""						,       """conv_before_upsample""")
									if "upsample.0" in name:
												a__       :						Any							=  name.replace("""upsample.0"""						,       """upsample.convolution_0""")
									if "upsample.2" in name:
												a__       :						Optional[Any]							=  name.replace("""upsample.2"""						,       """upsample.convolution_1""")
									a__       :						List[str]							=  """upsample.""" + name
						elif config.upsampler == "pixelshuffledirect":
									a__       :						List[Any]							=  name.replace("""upsample.0.weight"""						,       """upsample.conv.weight""")
									a__       :						List[Any]							=  name.replace("""upsample.0.bias"""						,       """upsample.conv.bias""")
						else:
									pass
			else:
						a__       :						Tuple							=  """swin2sr.""" + name

			return name

def      lowerCAmelCase_					(  _lowercase    :       List[Any]						,       _lowercase    :       List[Any])							->	Tuple:







			"""simple docstring"""
			for key in orig_state_dict.copy().keys():
						a__       :						int							=  orig_state_dict.pop(_lowercase)

						if "qkv" in key:
									a__       :						Tuple							=  key.split(""".""")
									a__       :						Any							=  int(key_split[1])
									a__       :						Tuple							=  int(key_split[4])
									a__       :						List[Any]							=  config.embed_dim

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

			return orig_state_dict

def      lowerCAmelCase_					(  _lowercase    :       Any						,       _lowercase    :       List[Any]						,       _lowercase    :       Union[str, Any])							->	Union[str, Any]:







			"""simple docstring"""
			a__       :						List[Any]							=  get_config(_lowercase)
			a__       :						Optional[Any]							=  SwinaSRForImageSuperResolution(_lowercase)
			model.eval()

			a__       :						List[Any]							=  torch.hub.load_state_dict_from_url(_lowercase						,       map_location="""cpu""")
			a__       :						Any							=  convert_state_dict(_lowercase						,       _lowercase)
			a__       :						Tuple							=  model.load_state_dict(_lowercase						,       strict=_lowercase)

			if len(_lowercase) > 0:
						raise ValueError("""Missing keys when converting: {}""".format(_lowercase))
			for key in unexpected_keys:
						if not ("relative_position_index" in key or "relative_coords_table" in key or "self_mask" in key):
									raise ValueError(F'''Unexpected key {key} in state_dict''')

    # verify values
			a__       :						int							=  """https://github.com/mv-lab/swin2sr/blob/main/testsets/real-inputs/shanghai.jpg?raw=true"""
			a__       :						str							=  Image.open(requests.get(_lowercase						,       stream=_lowercase).raw).convert("""RGB""")
			a__       :						Any							=  SwinaSRImageProcessor()
			# pixel_values = processor(image, return_tensors="pt").pixel_values

			a__       :						str							=  126 if """Jpeg""" in checkpoint_url else 256
			a__       :						int							=  Compose(
			    [
			        Resize((image_size, image_size)),
			        ToTensor(),
			        Normalize(mean=[0.485, 0.456, 0.406]						,       std=[0.229, 0.224, 0.225]),
			    ])
			a__       :						Optional[Any]							=  transforms(_lowercase).unsqueeze(0)

			if config.num_channels == 1:
						a__       :						Tuple							=  pixel_values[:, 0, :, :].unsqueeze(1)

			a__       :						List[str]							=  model(_lowercase)

			# assert values
			if "Swin2SR_ClassicalSR_X2_64" in checkpoint_url:
						a__       :						Optional[Any]							=  torch.Size([1, 3, 512, 512])
						a__       :						str							=  torch.tensor(
						    [[-0.7087, -0.7138, -0.6721], [-0.8340, -0.8095, -0.7298], [-0.9149, -0.8414, -0.7940]])
			elif "Swin2SR_ClassicalSR_X4_64" in checkpoint_url:
						a__       :						Tuple							=  torch.Size([1, 3, 1024, 1024])
						a__       :						Any							=  torch.tensor(
						    [[-0.7775, -0.8105, -0.8933], [-0.7764, -0.8356, -0.9225], [-0.7976, -0.8686, -0.9579]])
			elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url:
						# TODO values didn't match exactly here
						a__       :						str							=  torch.Size([1, 3, 1024, 1024])
						a__       :						str							=  torch.tensor(
						    [[-0.8035, -0.7504, -0.7491], [-0.8538, -0.8124, -0.7782], [-0.8804, -0.8651, -0.8493]])
			elif "Swin2SR_Lightweight_X2_64" in checkpoint_url:
						a__       :						List[Any]							=  torch.Size([1, 3, 512, 512])
						a__       :						Any							=  torch.tensor(
						    [[-0.7669, -0.8662, -0.8767], [-0.8810, -0.9962, -0.9820], [-0.9340, -1.0322, -1.1149]])
			elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url:
						a__       :						Any							=  torch.Size([1, 3, 1024, 1024])
						a__       :						Tuple							=  torch.tensor(
						    [[-0.5238, -0.5557, -0.6321], [-0.6016, -0.5903, -0.6391], [-0.6244, -0.6334, -0.6889]])

			assert (
			    outputs.reconstruction.shape == expected_shape
			), F'''Shape of reconstruction should be {expected_shape}, but is {outputs.reconstruction.shape}'''
			assert torch.allclose(outputs.reconstruction[0, 0, :3, :3]						,       _lowercase						,       atol=1e-3)
			print("""Looks ok!""")

			a__       :						List[Any]							=  {
			    """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth""": (
			        """swin2SR-classical-sr-x2-64"""
			    ),
			    """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X4_64.pth""": (
			        """swin2SR-classical-sr-x4-64"""
			    ),
			    """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_CompressedSR_X4_48.pth""": (
			        """swin2SR-compressed-sr-x4-48"""
			    ),
			    """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_Lightweight_X2_64.pth""": (
			        """swin2SR-lightweight-x2-64"""
			    ),
			    """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR.pth""": (
			        """swin2SR-realworld-sr-x4-64-bsrgan-psnr"""
			    ),
			}
			a__       :						Dict							=  url_to_name[checkpoint_url]

			if pytorch_dump_folder_path is not None:
						print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''')
						model.save_pretrained(_lowercase)
						print(F'''Saving image processor to {pytorch_dump_folder_path}''')
						processor.save_pretrained(_lowercase)

			if push_to_hub:
						model.push_to_hub(F'''caidas/{model_name}''')
						processor.push_to_hub(F'''caidas/{model_name}''')


if __name__ == "__main__":
	_lowercase		:				Any      		=argparse.ArgumentParser()
	# Required parameters
	parser.add_argument(
	    "--checkpoint_url",
	    default="https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth",
	    type=str,
	    help="URL of the original Swin2SR 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."
	)
	parser.add_argument("--push_to_hub", action="store_true", help="Whether to push the converted model to the hub.")

	_lowercase		:				Dict      		=parser.parse_args()
	convert_swinasr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)









import os
import unittest

from transformers import BatchEncoding
from transformers.models.bert.tokenization_bert import (
    BasicTokenizer,
    WordpieceTokenizer,
    _is_control,
    _is_punctuation,
    _is_whitespace,
)
from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer
from transformers.testing_utils import require_torch, slow

from ...test_tokenization_common import TokenizerTesterMixin

class        snake_case__    (A__ ,     unittest.TestCase		):







						"""simple docstring"""




						__lowerCAmelCase					:Union[str, Any]      =					ProphetNetTokenizer
						__lowerCAmelCase					:Any      =					False

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Union[str, Any]:
									"""simple docstring"""




									super().setUp()

									a__       :						Optional[Any]							=  [
									    """[UNK]""",
									    """[CLS]""",
									    """[SEP]""",
									    """[PAD]""",
									    """[MASK]""",
									    """want""",
									    """##want""",
									    """##ed""",
									    """wa""",
									    """un""",
									    """runn""",
									    """##ing""",
									    """,""",
									    """low""",
									    """lowest""",
									]
									a__       :						List[str]							=  os.path.join(self.tmpdirname ,       VOCAB_FILES_NAMES["""vocab_file"""]  )
									with open(self.vocab_file ,       """w""" ,       encoding="""utf-8"""  ) as vocab_writer:
												vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]  )  )

						def 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase  ) ->				str:
									"""simple docstring"""




									a__       :						Any							=  """UNwant\u00E9d,running"""
									a__       :						Dict							=  """unwanted, running"""
									return input_text, output_text

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Dict:
									"""simple docstring"""




									a__       :						Tuple							=  self.tokenizer_class(self.vocab_file  )

									a__       :						int							=  tokenizer.tokenize("""UNwant\u00E9d,running"""  )
									self.assertListEqual(__lowercase ,       ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""]  )
									self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowercase  ) ,       [9, 6, 7, 1_2, 1_0, 1_1]  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Union[str, Any]:
									"""simple docstring"""




									a__       :						str							=  BasicTokenizer()

									self.assertListEqual(tokenizer.tokenize("""ah\u535A\u63A8zz"""  ) ,       ["""ah""", """\u535A""", """\u63A8""", """zz"""]  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				int:
									"""simple docstring"""




									a__       :						int							=  BasicTokenizer(do_lower_case=__lowercase  )

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU?  """  ) ,       ["""hello""", """!""", """how""", """are""", """you""", """?"""]  )
									self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""  ) ,       ["""hello"""]  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[Any]:
									"""simple docstring"""




									a__       :						str							=  BasicTokenizer(do_lower_case=__lowercase ,       strip_accents=__lowercase  )

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """  ) ,       ["""hällo""", """!""", """how""", """are""", """you""", """?"""]  )
									self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""  ) ,       ["""h\u00E9llo"""]  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				str:
									"""simple docstring"""




									a__       :						List[str]							=  BasicTokenizer(do_lower_case=__lowercase ,       strip_accents=__lowercase  )

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """  ) ,       ["""hallo""", """!""", """how""", """are""", """you""", """?"""]  )
									self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""  ) ,       ["""hello"""]  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[Any]:
									"""simple docstring"""




									a__       :						Optional[Any]							=  BasicTokenizer(do_lower_case=__lowercase  )

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """  ) ,       ["""hallo""", """!""", """how""", """are""", """you""", """?"""]  )
									self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""  ) ,       ["""hello"""]  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Optional[int]:
									"""simple docstring"""




									a__       :						List[str]							=  BasicTokenizer(do_lower_case=__lowercase  )

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU?  """  ) ,       ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[str]:
									"""simple docstring"""




									a__       :						str							=  BasicTokenizer(do_lower_case=__lowercase ,       strip_accents=__lowercase  )

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """  ) ,       ["""HäLLo""", """!""", """how""", """Are""", """yoU""", """?"""]  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Optional[Any]:
									"""simple docstring"""




									a__       :						List[str]							=  BasicTokenizer(do_lower_case=__lowercase ,       strip_accents=__lowercase  )

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """  ) ,       ["""HaLLo""", """!""", """how""", """Are""", """yoU""", """?"""]  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[Any]:
									"""simple docstring"""




									a__       :						Union[str, Any]							=  BasicTokenizer(do_lower_case=__lowercase ,       never_split=["""[UNK]"""]  )

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU? [UNK]"""  ) ,       ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?""", """[UNK]"""]  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Optional[Any]:
									"""simple docstring"""




									a__       :						Optional[Any]							=  ["""[UNK]""", """[CLS]""", """[SEP]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing"""]

									a__       :						Dict							=  {}
									for i, token in enumerate(__lowercase  ):
												a__       :						Optional[Any]							=  i
									a__       :						str							=  WordpieceTokenizer(vocab=__lowercase ,       unk_token="""[UNK]"""  )

									self.assertListEqual(tokenizer.tokenize(""""""  ) ,       []  )

									self.assertListEqual(tokenizer.tokenize("""unwanted running"""  ) ,       ["""un""", """##want""", """##ed""", """runn""", """##ing"""]  )

									self.assertListEqual(tokenizer.tokenize("""unwantedX running"""  ) ,       ["""[UNK]""", """runn""", """##ing"""]  )

						@require_torch
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[Any]:
									"""simple docstring"""




									a__       :						List[Any]							=  self.tokenizer_class.from_pretrained("""microsoft/prophetnet-large-uncased"""  )

									a__       :						List[Any]							=  ["""A long paragraph for summarization.""", """Another paragraph for summarization."""]
									a__       :						Optional[Any]							=  [1_0_3_7, 2_1_4_6, 2_0_4_2_3, 2_0_0_5, 7_6_8_0, 7_8_4_9, 3_9_8_9, 1_0_1_2, 1_0_2]
									a__       :						List[Any]							=  tokenizer(__lowercase ,       padding=__lowercase ,       return_tensors="""pt"""  )
									self.assertIsInstance(__lowercase ,       __lowercase  )
									a__       :						Optional[int]							=  list(batch.input_ids.numpy()[0]  )
									self.assertListEqual(__lowercase ,       __lowercase  )

									self.assertEqual((2, 9) ,       batch.input_ids.shape  )
									self.assertEqual((2, 9) ,       batch.attention_mask.shape  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[str]:
									"""simple docstring"""




									self.assertTrue(_is_whitespace(""" """  )  )
									self.assertTrue(_is_whitespace("""\t"""  )  )
									self.assertTrue(_is_whitespace("""\r"""  )  )
									self.assertTrue(_is_whitespace("""\n"""  )  )
									self.assertTrue(_is_whitespace("""\u00A0"""  )  )

									self.assertFalse(_is_whitespace("""A"""  )  )
									self.assertFalse(_is_whitespace("""-"""  )  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Optional[Any]:
									"""simple docstring"""




									self.assertTrue(_is_control("""\u0005"""  )  )

									self.assertFalse(_is_control("""A"""  )  )
									self.assertFalse(_is_control(""" """  )  )
									self.assertFalse(_is_control("""\t"""  )  )
									self.assertFalse(_is_control("""\r"""  )  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Optional[Any]:
									"""simple docstring"""




									self.assertTrue(_is_punctuation("""-"""  )  )
									self.assertTrue(_is_punctuation("""$"""  )  )
									self.assertTrue(_is_punctuation("""`"""  )  )
									self.assertTrue(_is_punctuation("""."""  )  )

									self.assertFalse(_is_punctuation("""A"""  )  )
									self.assertFalse(_is_punctuation(""" """  )  )

						@slow
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Optional[int]:
									"""simple docstring"""




									a__       :						Optional[Any]							=  self.tokenizer_class.from_pretrained("""microsoft/prophetnet-large-uncased"""  )

									a__       :						Dict							=  tokenizer.encode("""sequence builders""" ,       add_special_tokens=__lowercase  )
									a__       :						str							=  tokenizer.encode("""multi-sequence build""" ,       add_special_tokens=__lowercase  )

									a__       :						Optional[Any]							=  tokenizer.build_inputs_with_special_tokens(__lowercase  )
									a__       :						Optional[int]							=  tokenizer.build_inputs_with_special_tokens(__lowercase ,       __lowercase  )

									assert encoded_sentence == text + [1_0_2]
									assert encoded_pair == text + [1_0_2] + text_a + [1_0_2]




