import argparse
import json

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

from transformers import ConvNextConfig, SegformerImageProcessor, UperNetConfig, UperNetForSemanticSegmentation





def  _lowerCamelCase					(   a_				: Union[str, Any]):
			lowerCamelCase						:Dict						  =     3_84
			if "tiny" in model_name:
						lowerCamelCase						:Optional[int]						  =     [3, 3, 9, 3]
						lowerCamelCase						:int						  =     [96, 1_92, 3_84, 7_68]
			if "small" in model_name:
						lowerCamelCase						:Any						  =     [3, 3, 27, 3]
						lowerCamelCase						:Any						  =     [96, 1_92, 3_84, 7_68]
			if "base" in model_name:
						lowerCamelCase						:List[str]						  =     [3, 3, 27, 3]
						lowerCamelCase						:Optional[Any]						  =     [1_28, 2_56, 5_12, 10_24]
						lowerCamelCase						:Any						  =     5_12
			if "large" in model_name:
						lowerCamelCase						:int						  =     [3, 3, 27, 3]
						lowerCamelCase						:Any						  =     [1_92, 3_84, 7_68, 15_36]
						lowerCamelCase						:int						  =     7_68
			if "xlarge" in model_name:
						lowerCamelCase						:int						  =     [3, 3, 27, 3]
						lowerCamelCase						:Union[str, Any]						  =     [2_56, 5_12, 10_24, 20_48]
						lowerCamelCase						:Any						  =     10_24

			# set label information
			lowerCamelCase						:Optional[Any]						  =     1_50
			lowerCamelCase						:str						  =     '''huggingface/label-files'''
			lowerCamelCase						:List[str]						  =     '''ade20k-id2label.json'''
			lowerCamelCase						:Any						  =     json.load(open(hf_hub_download(a_						,			a_						,			repo_type='''dataset''')						,			'''r'''))
			lowerCamelCase						:Optional[Any]						  =     {int(a_): v for k, v in idalabel.items()}
			lowerCamelCase						:Union[str, Any]						  =     {v: k for k, v in idalabel.items()}

			lowerCamelCase						:List[Any]						  =     ConvNextConfig(
			    depths=a_						,			hidden_sizes=a_						,			out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''])
			lowerCamelCase						:List[Any]						  =     UperNetConfig(
			    backbone_config=a_						,			auxiliary_in_channels=a_						,			num_labels=a_						,			idalabel=a_						,			labelaid=a_						,			)

			return config





def  _lowerCamelCase					(   a_				: List[Any]):
			lowerCamelCase						:str						  =     []

			# fmt: off
			# stem
			rename_keys.append(('''backbone.downsample_layers.0.0.weight''', '''backbone.embeddings.patch_embeddings.weight'''))
			rename_keys.append(('''backbone.downsample_layers.0.0.bias''', '''backbone.embeddings.patch_embeddings.bias'''))
			rename_keys.append(('''backbone.downsample_layers.0.1.weight''', '''backbone.embeddings.layernorm.weight'''))
			rename_keys.append(('''backbone.downsample_layers.0.1.bias''', '''backbone.embeddings.layernorm.bias'''))
			# stages
			for i in range(len(config.backbone_config.depths)):
						for j in range(config.backbone_config.depths[i]):
									rename_keys.append((F"backbone.stages.{i}.{j}.gamma", F"backbone.encoder.stages.{i}.layers.{j}.layer_scale_parameter"))
									rename_keys.append((F"backbone.stages.{i}.{j}.depthwise_conv.weight", F"backbone.encoder.stages.{i}.layers.{j}.dwconv.weight"))
									rename_keys.append((F"backbone.stages.{i}.{j}.depthwise_conv.bias", F"backbone.encoder.stages.{i}.layers.{j}.dwconv.bias"))
									rename_keys.append((F"backbone.stages.{i}.{j}.norm.weight", F"backbone.encoder.stages.{i}.layers.{j}.layernorm.weight"))
									rename_keys.append((F"backbone.stages.{i}.{j}.norm.bias", F"backbone.encoder.stages.{i}.layers.{j}.layernorm.bias"))
									rename_keys.append((F"backbone.stages.{i}.{j}.pointwise_conv1.weight", F"backbone.encoder.stages.{i}.layers.{j}.pwconv1.weight"))
									rename_keys.append((F"backbone.stages.{i}.{j}.pointwise_conv1.bias", F"backbone.encoder.stages.{i}.layers.{j}.pwconv1.bias"))
									rename_keys.append((F"backbone.stages.{i}.{j}.pointwise_conv2.weight", F"backbone.encoder.stages.{i}.layers.{j}.pwconv2.weight"))
									rename_keys.append((F"backbone.stages.{i}.{j}.pointwise_conv2.bias", F"backbone.encoder.stages.{i}.layers.{j}.pwconv2.bias"))
						if i > 0:
									rename_keys.append((F"backbone.downsample_layers.{i}.0.weight", F"backbone.encoder.stages.{i}.downsampling_layer.0.weight"))
									rename_keys.append((F"backbone.downsample_layers.{i}.0.bias", F"backbone.encoder.stages.{i}.downsampling_layer.0.bias"))
									rename_keys.append((F"backbone.downsample_layers.{i}.1.weight", F"backbone.encoder.stages.{i}.downsampling_layer.1.weight"))
									rename_keys.append((F"backbone.downsample_layers.{i}.1.bias", F"backbone.encoder.stages.{i}.downsampling_layer.1.bias"))

						rename_keys.append((F"backbone.norm{i}.weight", F"backbone.hidden_states_norms.stage{i+1}.weight"))
						rename_keys.append((F"backbone.norm{i}.bias", F"backbone.hidden_states_norms.stage{i+1}.bias"))

			# decode head
			rename_keys.extend(
			    [
			        ('''decode_head.conv_seg.weight''', '''decode_head.classifier.weight'''),
			        ('''decode_head.conv_seg.bias''', '''decode_head.classifier.bias'''),
			        ('''auxiliary_head.conv_seg.weight''', '''auxiliary_head.classifier.weight'''),
			        ('''auxiliary_head.conv_seg.bias''', '''auxiliary_head.classifier.bias'''),
			    ])
			# fmt: on

			return rename_keys





def  _lowerCamelCase					(   a_				: int						,			a_				: List[str]						,			a_				: int):
			lowerCamelCase						:List[Any]						  =     dct.pop(a_)
			lowerCamelCase						:List[str]						  =     val





def  _lowerCamelCase					(   a_				: Union[str, Any]						,			a_				: Optional[Any]						,			a_				: int):
			lowerCamelCase						:Optional[int]						  =     {
			    '''upernet-convnext-tiny''': '''https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_tiny_fp16_512x512_160k_ade20k/upernet_convnext_tiny_fp16_512x512_160k_ade20k_20220227_124553-cad485de.pth''',
			    '''upernet-convnext-small''': '''https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_small_fp16_512x512_160k_ade20k/upernet_convnext_small_fp16_512x512_160k_ade20k_20220227_131208-1b1e394f.pth''',
			    '''upernet-convnext-base''': '''https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth''',
			    '''upernet-convnext-large''': '''https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_large_fp16_640x640_160k_ade20k/upernet_convnext_large_fp16_640x640_160k_ade20k_20220226_040532-e57aa54d.pth''',
			    '''upernet-convnext-xlarge''': '''https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_xlarge_fp16_640x640_160k_ade20k/upernet_convnext_xlarge_fp16_640x640_160k_ade20k_20220226_080344-95fc38c2.pth''',
			}
			lowerCamelCase						:List[Any]						  =     model_name_to_url[model_name]
			lowerCamelCase						:Any						  =     torch.hub.load_state_dict_from_url(a_						,			map_location='''cpu''')['''state_dict''']

			lowerCamelCase						:Tuple						  =     get_upernet_config(a_)
			lowerCamelCase						:Optional[int]						  =     UperNetForSemanticSegmentation(a_)
			model.eval()

			# replace "bn" => "batch_norm"
			for key in state_dict.copy().keys():
						lowerCamelCase						:List[Any]						  =     state_dict.pop(a_)
						if "bn" in key:
									lowerCamelCase						:List[Any]						  =     key.replace('''bn'''						,			'''batch_norm''')
						lowerCamelCase						:Optional[int]						  =     val

			# rename keys
			lowerCamelCase						:str						  =     create_rename_keys(a_)
			for src, dest in rename_keys:
						rename_key(a_						,			a_						,			a_)

			model.load_state_dict(a_)

			# verify on image
			lowerCamelCase						:Tuple						  =     '''https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg'''
			lowerCamelCase						:Tuple						  =     Image.open(requests.get(a_						,			stream=a_).raw).convert('''RGB''')

			lowerCamelCase						:List[str]						  =     SegformerImageProcessor()
			lowerCamelCase						:Any						  =     processor(a_						,			return_tensors='''pt''').pixel_values

			with torch.no_grad():
						lowerCamelCase						:Any						  =     model(a_)

			if model_name == "upernet-convnext-tiny":
						lowerCamelCase						:Optional[Any]						  =     torch.tensor(
						    [[-8.8_110, -8.8_110, -8.6_521], [-8.8_110, -8.8_110, -8.6_521], [-8.7_746, -8.7_746, -8.6_130]])
			elif model_name == "upernet-convnext-small":
						lowerCamelCase						:Any						  =     torch.tensor(
						    [[-8.8_236, -8.8_236, -8.6_771], [-8.8_236, -8.8_236, -8.6_771], [-8.7_638, -8.7_638, -8.6_240]])
			elif model_name == "upernet-convnext-base":
						lowerCamelCase						:Any						  =     torch.tensor(
						    [[-8.8_558, -8.8_558, -8.6_905], [-8.8_558, -8.8_558, -8.6_905], [-8.7_669, -8.7_669, -8.6_021]])
			elif model_name == "upernet-convnext-large":
						lowerCamelCase						:List[Any]						  =     torch.tensor(
						    [[-8.6_660, -8.6_660, -8.6_210], [-8.6_660, -8.6_660, -8.6_210], [-8.6_310, -8.6_310, -8.5_964]])
			elif model_name == "upernet-convnext-xlarge":
						lowerCamelCase						:Dict						  =     torch.tensor(
						    [[-8.4_980, -8.4_980, -8.3_977], [-8.4_980, -8.4_980, -8.3_977], [-8.4_379, -8.4_379, -8.3_412]])
			print('''Logits:'''						,			outputs.logits[0, 0, :3, :3])
			assert torch.allclose(outputs.logits[0, 0, :3, :3]						,			a_						,			atol=1e-4)
			print('''Looks ok!''')

			if pytorch_dump_folder_path is not None:
						print(F"Saving model {model_name} to {pytorch_dump_folder_path}")
						model.save_pretrained(a_)
						print(F"Saving processor to {pytorch_dump_folder_path}")
						processor.save_pretrained(a_)

			if push_to_hub:
						print(F"Pushing model and processor for {model_name} to hub")
						model.push_to_hub(F"openmmlab/{model_name}")
						processor.push_to_hub(F"openmmlab/{model_name}")


if __name__ == "__main__":
			A__           =							argparse.ArgumentParser()
			# Required parameters
			parser.add_argument(
			    """--model_name""",
			    default="""upernet-convnext-tiny""",
			    type=str,
			    choices=[F'upernet-convnext-{size}' for size in ["""tiny""", """small""", """base""", """large""", """xlarge"""]],
			    help="""Name of the ConvNext UperNet 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 or not to push the converted model to the 🤗 hub."""
			)

			A__           =							parser.parse_args()
			convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)




import ast
import os
import re
import shutil
import tempfile
import unittest
from unittest import mock

import torch

from accelerate.test_utils.examples import compare_against_test
from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow
from accelerate.utils import write_basic_config


# DataLoaders built from `test_samples/MRPC` for quick testing
# Should mock `{script_name}.get_dataloaders` via:
# @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders)

A__           =							[
    """cross_validation.py""",
    """gradient_accumulation.py""",
    """local_sgd.py""",
    """multi_process_metrics.py""",
    """memory.py""",
    """automatic_gradient_accumulation.py""",
    """fsdp_with_peak_mem_tracking.py""",
    """deepspeed_with_config_support.py""",
    """megatron_lm_gpt_pretraining.py""",
]
class   _lowerCAmelCase		(     unittest.TestCase				):







							def 				snake_case  (				self     :    Any	,					__snake_case     :    str	,					__snake_case     :    bool	,					__snake_case     :    str = None	,					__snake_case     :    list = None		):
										lowerCamelCase						:Tuple						  =     None
										lowerCamelCase						:Tuple						  =     os.path.abspath(os.path.join('''examples'''	,					'''by_feature'''		)		)
										lowerCamelCase						:Optional[int]						  =     os.path.abspath('''examples'''		)
										for item in os.listdir(__snake_case		):
													if item not in EXCLUDE_EXAMPLES:
																lowerCamelCase						:Optional[int]						  =     os.path.join(__snake_case	,					__snake_case		)
																if os.path.isfile(__snake_case		) and ".py" in item_path:
																			with self.subTest(
																			    tested_script=__snake_case	,					feature_script=__snake_case	,					tested_section='''main()''' if parser_only else '''training_function()'''	,					):
																						lowerCamelCase						:Union[str, Any]						  =     compare_against_test(
																						    os.path.join(__snake_case	,					__snake_case		)	,					__snake_case	,					__snake_case	,					__snake_case		)
																						lowerCamelCase						:int						  =     '''\n'''.join(__snake_case		)
																						if special_strings is not None:
																									for string in special_strings:
																												lowerCamelCase						:int						  =     diff.replace(__snake_case	,					''''''		)
																						self.assertEqual(__snake_case	,					''''''		)







							def 				snake_case  (				self     :    Dict		):
										self.one_complete_example('''complete_nlp_example.py'''	,					__snake_case		)
										self.one_complete_example('''complete_nlp_example.py'''	,					__snake_case		)







							def 				snake_case  (				self     :    Optional[Any]		):
										lowerCamelCase						:Union[str, Any]						  =     os.path.abspath(os.path.join('''examples'''	,					'''cv_example.py'''		)		)
										lowerCamelCase						:Optional[int]						  =     [
										    ''' ''' * 16 + '''{\n\n''',
										    ''' ''' * 20 + '''"accuracy": eval_metric["accuracy"],\n\n''',
										    ''' ''' * 20 + '''"f1": eval_metric["f1"],\n\n''',
										    ''' ''' * 20 + '''"train_loss": total_loss.item() / len(train_dataloader),\n\n''',
										    ''' ''' * 20 + '''"epoch": epoch,\n\n''',
										    ''' ''' * 16 + '''},\n\n''',
										    ''' ''' * 16 + '''step=epoch,\n''',
										    ''' ''' * 12,
										    ''' ''' * 8 + '''for step, batch in enumerate(active_dataloader):\n''',
										]
										self.one_complete_example('''complete_cv_example.py'''	,					__snake_case	,					__snake_case	,					__snake_case		)
										self.one_complete_example('''complete_cv_example.py'''	,					__snake_case	,					__snake_case	,					__snake_case		)


@mock.patch.dict(os.environ      ,   {'TESTING_MOCKED_DATALOADERS': '1'}				)
class   _lowerCAmelCase		(     __SCREAMING_SNAKE_CASE				):
							_UpperCAmelCase						    =     False







							@classmethod
							def 				snake_case  (				cls     :    Optional[Any]		):
										super().setUpClass()
										lowerCamelCase						:Any						  =     tempfile.mkdtemp()
										lowerCamelCase						:Optional[int]						  =     os.path.join(cls._tmpdir	,					'''default_config.yml'''		)

										write_basic_config(save_location=cls.configPath		)
										lowerCamelCase						:List[str]						  =     ['''accelerate''', '''launch''', '''--config_file''', cls.configPath]







							@classmethod
							def 				snake_case  (				cls     :    Dict		):
										super().tearDownClass()
										shutil.rmtree(cls._tmpdir		)







							def 				snake_case  (				self     :    int		):
										lowerCamelCase						:Any						  =     F"\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps epoch\n        --output_dir {self.tmpdir}\n        ".split()
										run_command(self._launch_args + testargs		)
										self.assertTrue(os.path.exists(os.path.join(self.tmpdir	,					'''epoch_0'''		)		)		)







							def 				snake_case  (				self     :    List[Any]		):
										lowerCamelCase						:Tuple						  =     F"\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        ".split()
										lowerCamelCase						:List[Any]						  =     run_command(self._launch_args + testargs		)
										self.assertTrue(os.path.exists(os.path.join(self.tmpdir	,					'''step_2'''		)		)		)







							def 				snake_case  (				self     :    List[str]		):
										lowerCamelCase						:Dict						  =     F"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir	,					'epoch_0'		)}\n        ".split()
										lowerCamelCase						:Dict						  =     run_command(self._launch_args + testargs	,					return_stdout=__snake_case		)
										self.assertNotIn('''epoch 0:'''	,					__snake_case		)
										self.assertIn('''epoch 1:'''	,					__snake_case		)







							def 				snake_case  (				self     :    str		):
										lowerCamelCase						:List[Any]						  =     F"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir	,					'step_2'		)}\n        ".split()
										lowerCamelCase						:Optional[int]						  =     run_command(self._launch_args + testargs	,					return_stdout=__snake_case		)
										if torch.cuda.is_available():
													lowerCamelCase						:Union[str, Any]						  =     torch.cuda.device_count()
										else:
													lowerCamelCase						:Dict						  =     1
										if num_processes > 1:
													self.assertNotIn('''epoch 0:'''	,					__snake_case		)
													self.assertIn('''epoch 1:'''	,					__snake_case		)
										else:
													self.assertIn('''epoch 0:'''	,					__snake_case		)
													self.assertIn('''epoch 1:'''	,					__snake_case		)







							@slow
							def 				snake_case  (				self     :    Any		):
										lowerCamelCase						:Tuple						  =     '''
        examples/by_feature/cross_validation.py
        --num_folds 2
        '''.split()
										with mock.patch.dict(os.environ	,					{'''TESTING_MOCKED_DATALOADERS''': '''0'''}		):
													lowerCamelCase						:Dict						  =     run_command(self._launch_args + testargs	,					return_stdout=__snake_case		)
													lowerCamelCase						:Tuple						  =     re.findall('''({.+})'''	,					__snake_case		)
													lowerCamelCase						:Optional[Any]						  =     [r for r in results if '''accuracy''' in r][-1]
													lowerCamelCase						:List[str]						  =     ast.literal_eval(__snake_case		)
													self.assertGreaterEqual(results['''accuracy''']	,					0.7_5		)







							def 				snake_case  (				self     :    int		):
										lowerCamelCase						:Dict						  =     ['''examples/by_feature/multi_process_metrics.py''']
										run_command(self._launch_args + testargs		)







							@require_trackers
							@mock.patch.dict(os.environ	,					{'''WANDB_MODE''': '''offline'''}		)
							def 				snake_case  (				self     :    Any		):
										with tempfile.TemporaryDirectory() as tmpdir:
													lowerCamelCase						:Tuple						  =     F"\n            examples/by_feature/tracking.py\n            --with_tracking\n            --project_dir {tmpdir}\n            ".split()
													run_command(self._launch_args + testargs		)
													self.assertTrue(os.path.exists(os.path.join(__snake_case	,					'''tracking'''		)		)		)







							def 				snake_case  (				self     :    Tuple		):
										lowerCamelCase						:Tuple						  =     ['''examples/by_feature/gradient_accumulation.py''']
										run_command(self._launch_args + testargs		)







							def 				snake_case  (				self     :    Optional[Any]		):
										lowerCamelCase						:int						  =     ['''examples/by_feature/local_sgd.py''']
										run_command(self._launch_args + testargs		)



