



from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...utils import logging


if TYPE_CHECKING:
		from ...feature_extraction_utils import FeatureExtractionMixin
		from ...tokenization_utils_base import PreTrainedTokenizerBase
		from ...utils import TensorType

__lowerCamelCase  :				Optional[Any]					=					logging.get_logger(__name__)

__lowerCamelCase  :				int					=					{
    "openai/whisper-base": "https://huggingface.co/openai/whisper-base/resolve/main/config.json",
}

# fmt: off
__lowerCamelCase  :				str					=					[
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 357, 366, 438, 532, 685,
    705, 796, 930, 1_058, 1_220, 1_267, 1_279, 1_303, 1_343, 1_377,
    1_391, 1_635, 1_782, 1_875, 2_162, 2_361, 2_488, 3_467, 4_008, 4_211,
    4_600, 4_808, 5_299, 5_855, 6_329, 7_203, 9_609, 9_959, 10_563, 10_786,
    11_420, 11_709, 11_907, 13_163, 13_697, 13_700, 14_808, 15_306, 16_410, 16_791,
    17_992, 19_203, 19_510, 20_724, 22_305, 22_935, 27_007, 30_109, 30_420, 33_409,
    34_949, 40_283, 40_493, 40_549, 47_282, 49_146, 50_257, 50_359, 50_360, 50_361
]
__lowerCamelCase  :				Optional[int]					=					[
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 359, 503, 522, 542, 873,
    893, 902, 918, 922, 931, 1_350, 1_853, 1_982, 2_460, 2_627,
    3_246, 3_253, 3_268, 3_536, 3_846, 3_961, 4_183, 4_667, 6_585, 6_647,
    7_273, 9_061, 9_383, 10_428, 10_929, 11_938, 12_033, 12_331, 12_562, 13_793,
    14_157, 14_635, 15_265, 15_618, 16_553, 16_604, 18_362, 18_956, 20_075, 21_675,
    22_520, 26_130, 26_161, 26_435, 28_279, 29_464, 31_650, 32_302, 32_470, 36_865,
    42_863, 47_425, 49_870, 50_254, 50_258, 50_360, 50_361, 50_362
]





class 			__magic_name__ (				snake_case_    ):
		lowercase   :							str        ='''whisper'''
		lowercase   :							int        =['''past_key_values''']
		lowercase   :							Tuple        ={'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}






		def __init__(    self   :  Optional[Any]					,		UpperCamelCase__   :  Optional[int]=5_18_65					,		UpperCamelCase__   :  Dict=80					,		UpperCamelCase__   :  Optional[int]=6					,		UpperCamelCase__   :  Union[str, Any]=4					,		UpperCamelCase__   :  List[Any]=6					,		UpperCamelCase__   :  List[Any]=4					,		UpperCamelCase__   :  List[Any]=15_36					,		UpperCamelCase__   :  Dict=15_36					,		UpperCamelCase__   :  int=0.0					,		UpperCamelCase__   :  List[Any]=0.0					,		UpperCamelCase__   :  Any=5_02_57					,		UpperCamelCase__   :  str=True					,		UpperCamelCase__   :  int=True					,		UpperCamelCase__   :  List[Any]="gelu"					,		UpperCamelCase__   :  Any=2_56					,		UpperCamelCase__   :  int=0.0					,		UpperCamelCase__   :  Optional[Any]=0.0					,		UpperCamelCase__   :  Tuple=0.0					,		UpperCamelCase__   :  Dict=0.02					,		UpperCamelCase__   :  int=False					,		UpperCamelCase__   :  Union[str, Any]=15_00					,		UpperCamelCase__   :  Any=4_48					,		UpperCamelCase__   :  List[str]=5_02_56					,		UpperCamelCase__   :  Any=5_02_56					,		UpperCamelCase__   :  Tuple=5_02_56					,		UpperCamelCase__   :  Tuple=None					,		UpperCamelCase__   :  Optional[Any]=[2_20, 5_02_56]					,		UpperCamelCase__   :  Any=False					,		UpperCamelCase__   :  int=2_56					,		UpperCamelCase__   :  Any=False					,		UpperCamelCase__   :  List[Any]=0.05					,		UpperCamelCase__   :  Optional[int]=10					,		UpperCamelCase__   :  Optional[Any]=2					,		UpperCamelCase__   :  str=0.0					,		UpperCamelCase__   :  Tuple=10					,		UpperCamelCase__   :  str=0					,		UpperCamelCase__   :  List[Any]=7					,		**UpperCamelCase__   :  List[str]					,		)					->       Any:
								'''simple docstring'''







								UpperCAmelCase								=  vocab_size
								UpperCAmelCase								=  num_mel_bins
								UpperCAmelCase								=  d_model
								UpperCAmelCase								=  encoder_layers
								UpperCAmelCase								=  encoder_attention_heads
								UpperCAmelCase								=  decoder_layers
								UpperCAmelCase								=  decoder_attention_heads
								UpperCAmelCase								=  decoder_ffn_dim
								UpperCAmelCase								=  encoder_ffn_dim
								UpperCAmelCase								=  dropout
								UpperCAmelCase								=  attention_dropout
								UpperCAmelCase								=  activation_dropout
								UpperCAmelCase								=  activation_function
								UpperCAmelCase								=  init_std
								UpperCAmelCase								=  encoder_layerdrop
								UpperCAmelCase								=  decoder_layerdrop
								UpperCAmelCase								=  use_cache
								UpperCAmelCase								=  encoder_layers
								UpperCAmelCase								=  scale_embedding  # scale factor will be sqrt(d_model) if True
								UpperCAmelCase								=  max_source_positions
								UpperCAmelCase								=  max_target_positions

								# Audio Classification-specific parameters. Feel free to ignore for other classes.
								UpperCAmelCase								=  classifier_proj_size
								UpperCAmelCase								=  use_weighted_layer_sum

								# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
								UpperCAmelCase								=  apply_spec_augment
								UpperCAmelCase								=  mask_time_prob
								UpperCAmelCase								=  mask_time_length
								UpperCAmelCase								=  mask_time_min_masks
								UpperCAmelCase								=  mask_feature_prob
								UpperCAmelCase								=  mask_feature_length
								UpperCAmelCase								=  mask_feature_min_masks

								UpperCAmelCase								=  median_filter_width

								super().__init__(
								    pad_token_id=UpperCamelCase__					,		bos_token_id=UpperCamelCase__					,		eos_token_id=UpperCamelCase__					,		is_encoder_decoder=UpperCamelCase__					,		decoder_start_token_id=UpperCamelCase__					,		suppress_tokens=UpperCamelCase__					,		begin_suppress_tokens=UpperCamelCase__					,		**UpperCamelCase__					,		)





class 			__magic_name__ (				snake_case_    ):






		@property
		def SCREAMING_SNAKE_CASE_      (    self   :  Union[str, Any]   )					->       Mapping[str, Mapping[int, str]]:
								'''simple docstring'''







								UpperCAmelCase								=  OrderedDict(
								    [
								        ("input_features", {0: "batch", 1: "feature_size", 2: "encoder_sequence"}),
								    ]   )
								if self.use_past:
														UpperCAmelCase								=  {0: "batch"}
								else:
														UpperCAmelCase								=  {0: "batch", 1: "decoder_sequence"}

								if self.use_past:
														self.fill_with_past_key_values_(UpperCamelCase__					,		direction="inputs"   )

								return common_inputs






		def SCREAMING_SNAKE_CASE_      (    self   :  Optional[Any]					,		UpperCamelCase__   :  Optional[Any]					,		UpperCamelCase__   :  Optional[Any] = -1					,		UpperCamelCase__   :  Optional[int] = -1					,		UpperCamelCase__   :  Optional[Any] = False					,		UpperCamelCase__   :  List[str] = None					,		UpperCamelCase__   :  Optional[int] = 2_20_50					,		UpperCamelCase__   :  Union[str, Any] = 5.0					,		UpperCamelCase__   :  Any = 2_20					,		)					->       Mapping[str, Any]:
								'''simple docstring'''







								UpperCAmelCase								=  OrderedDict()
								UpperCAmelCase								=  OnnxConfig.generate_dummy_inputs(
								    self					,		preprocessor=preprocessor.feature_extractor					,		batch_size=UpperCamelCase__					,		framework=UpperCamelCase__					,		sampling_rate=UpperCamelCase__					,		time_duration=UpperCamelCase__					,		frequency=UpperCamelCase__					,		)
								UpperCAmelCase								=  encoder_inputs["input_features"].shape[2]
								UpperCAmelCase								=  encoder_sequence_length // 2 if self.use_past else seq_length

								UpperCAmelCase								=  super().generate_dummy_inputs(
								    preprocessor.tokenizer					,		UpperCamelCase__					,		UpperCamelCase__					,		UpperCamelCase__					,		UpperCamelCase__   )

								UpperCAmelCase								=  encoder_inputs.pop("input_features"   )
								UpperCAmelCase								=  decoder_inputs.pop("decoder_input_ids"   )

								if "past_key_values" in decoder_inputs:
														UpperCAmelCase								=  decoder_inputs.pop("past_key_values"   )

								return dummy_inputs







		@property
		def SCREAMING_SNAKE_CASE_      (    self   :  int   )					->       float:
								'''simple docstring'''







								return 1e-3











import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import cached_download, hf_hub_url
from PIL import Image

from transformers import DPTConfig, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTImageProcessor
from transformers.utils import logging


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





def 		__UpperCAmelCase     (	a_):
							snake_case_           =				DPTConfig()

							if "large" in checkpoint_url:
														snake_case_           =				10_24
														snake_case_           =				40_96
														snake_case_           =				24
														snake_case_           =				16
														snake_case_           =				[5, 11, 17, 23]
														snake_case_           =				[2_56, 5_12, 10_24, 10_24]
														snake_case_           =				(1, 3_84, 3_84)

							if "ade" in checkpoint_url:
														snake_case_           =				True

														snake_case_           =				1_50
														snake_case_           =				'huggingface/label-files'
														snake_case_           =				'ade20k-id2label.json'
														snake_case_           =				json.load(open(cached_download(hf_hub_url(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_           =				[1, 1_50, 4_80, 4_80]

							return config, expected_shape





def 		__UpperCAmelCase     (	a_):
							snake_case_           =				['pretrained.model.head.weight', 'pretrained.model.head.bias']
							for k in ignore_keys:
														state_dict.pop(a_				,	a_)





def 		__UpperCAmelCase     (	a_):
							if (
							    "pretrained.model" in name
							    and "cls_token" not in name
							    and "pos_embed" not in name
							    and "patch_embed" not in name
							):
														snake_case_           =				name.replace('pretrained.model'				,	'dpt.encoder')
							if "pretrained.model" in name:
														snake_case_           =				name.replace('pretrained.model'				,	'dpt.embeddings')
							if "patch_embed" in name:
														snake_case_           =				name.replace('patch_embed'				,	'patch_embeddings')
							if "pos_embed" in name:
														snake_case_           =				name.replace('pos_embed'				,	'position_embeddings')
							if "attn.proj" in name:
														snake_case_           =				name.replace('attn.proj'				,	'attention.output.dense')
							if "proj" in name and "project" not in name:
														snake_case_           =				name.replace('proj'				,	'projection')
							if "blocks" in name:
														snake_case_           =				name.replace('blocks'				,	'layer')
							if "mlp.fc1" in name:
														snake_case_           =				name.replace('mlp.fc1'				,	'intermediate.dense')
							if "mlp.fc2" in name:
														snake_case_           =				name.replace('mlp.fc2'				,	'output.dense')
							if "norm1" in name:
														snake_case_           =				name.replace('norm1'				,	'layernorm_before')
							if "norm2" in name:
														snake_case_           =				name.replace('norm2'				,	'layernorm_after')
							if "scratch.output_conv" in name:
														snake_case_           =				name.replace('scratch.output_conv'				,	'head')
							if "scratch" in name:
														snake_case_           =				name.replace('scratch'				,	'neck')
							if "layer1_rn" in name:
														snake_case_           =				name.replace('layer1_rn'				,	'convs.0')
							if "layer2_rn" in name:
														snake_case_           =				name.replace('layer2_rn'				,	'convs.1')
							if "layer3_rn" in name:
														snake_case_           =				name.replace('layer3_rn'				,	'convs.2')
							if "layer4_rn" in name:
														snake_case_           =				name.replace('layer4_rn'				,	'convs.3')
							if "refinenet" in name:
														snake_case_           =				int(name[len('neck.refinenet') : len('neck.refinenet') + 1])
														# tricky here: we need to map 4 to 0, 3 to 1, 2 to 2 and 1 to 3
														snake_case_           =				name.replace(f'''refinenet{layer_idx}'''				,	f'''fusion_stage.layers.{abs(layer_idx-4)}''')
							if "out_conv" in name:
														snake_case_           =				name.replace('out_conv'				,	'projection')
							if "resConfUnit1" in name:
														snake_case_           =				name.replace('resConfUnit1'				,	'residual_layer1')
							if "resConfUnit2" in name:
														snake_case_           =				name.replace('resConfUnit2'				,	'residual_layer2')
							if "conv1" in name:
														snake_case_           =				name.replace('conv1'				,	'convolution1')
							if "conv2" in name:
														snake_case_           =				name.replace('conv2'				,	'convolution2')
							# readout blocks
							if "pretrained.act_postprocess1.0.project.0" in name:
														snake_case_           =				name.replace('pretrained.act_postprocess1.0.project.0'				,	'neck.reassemble_stage.readout_projects.0.0')
							if "pretrained.act_postprocess2.0.project.0" in name:
														snake_case_           =				name.replace('pretrained.act_postprocess2.0.project.0'				,	'neck.reassemble_stage.readout_projects.1.0')
							if "pretrained.act_postprocess3.0.project.0" in name:
														snake_case_           =				name.replace('pretrained.act_postprocess3.0.project.0'				,	'neck.reassemble_stage.readout_projects.2.0')
							if "pretrained.act_postprocess4.0.project.0" in name:
														snake_case_           =				name.replace('pretrained.act_postprocess4.0.project.0'				,	'neck.reassemble_stage.readout_projects.3.0')
							# resize blocks
							if "pretrained.act_postprocess1.3" in name:
														snake_case_           =				name.replace('pretrained.act_postprocess1.3'				,	'neck.reassemble_stage.layers.0.projection')
							if "pretrained.act_postprocess1.4" in name:
														snake_case_           =				name.replace('pretrained.act_postprocess1.4'				,	'neck.reassemble_stage.layers.0.resize')
							if "pretrained.act_postprocess2.3" in name:
														snake_case_           =				name.replace('pretrained.act_postprocess2.3'				,	'neck.reassemble_stage.layers.1.projection')
							if "pretrained.act_postprocess2.4" in name:
														snake_case_           =				name.replace('pretrained.act_postprocess2.4'				,	'neck.reassemble_stage.layers.1.resize')
							if "pretrained.act_postprocess3.3" in name:
														snake_case_           =				name.replace('pretrained.act_postprocess3.3'				,	'neck.reassemble_stage.layers.2.projection')
							if "pretrained.act_postprocess4.3" in name:
														snake_case_           =				name.replace('pretrained.act_postprocess4.3'				,	'neck.reassemble_stage.layers.3.projection')
							if "pretrained.act_postprocess4.4" in name:
														snake_case_           =				name.replace('pretrained.act_postprocess4.4'				,	'neck.reassemble_stage.layers.3.resize')
							if "pretrained" in name:
														snake_case_           =				name.replace('pretrained'				,	'dpt')
							if "bn" in name:
														snake_case_           =				name.replace('bn'				,	'batch_norm')
							if "head" in name:
														snake_case_           =				name.replace('head'				,	'head.head')
							if "encoder.norm" in name:
														snake_case_           =				name.replace('encoder.norm'				,	'layernorm')
							if "auxlayer" in name:
														snake_case_           =				name.replace('auxlayer'				,	'auxiliary_head.head')

							return name





def 		__UpperCAmelCase     (	a_				,	a_):
							for i in range(config.num_hidden_layers):
														# read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
														snake_case_           =				state_dict.pop(f'''dpt.encoder.layer.{i}.attn.qkv.weight''')
														snake_case_           =				state_dict.pop(f'''dpt.encoder.layer.{i}.attn.qkv.bias''')
														# next, add query, keys and values (in that order) to the state dict
														snake_case_           =				in_proj_weight[: config.hidden_size, :]
														snake_case_           =				in_proj_bias[: config.hidden_size]
														snake_case_           =				in_proj_weight[
														    config.hidden_size : config.hidden_size * 2, :
														]
														snake_case_           =				in_proj_bias[
														    config.hidden_size : config.hidden_size * 2
														]
														snake_case_           =				in_proj_weight[
														    -config.hidden_size :, :
														]
														snake_case_           =				in_proj_bias[-config.hidden_size :]





def 		__UpperCAmelCase     (	):
							snake_case_           =				'http://images.cocodataset.org/val2017/000000039769.jpg'
							snake_case_           =				Image.open(requests.get(a_				,	stream=a_).raw)
							return im





@torch.no_grad()
def 		__UpperCAmelCase     (	a_				,	a_				,	a_				,	a_):
							snake_case_     ,						snake_case_           =				get_dpt_config(a_)
							# load original state_dict from URL
							snake_case_           =				torch.hub.load_state_dict_from_url(a_				,	map_location='cpu')
							# remove certain keys
							remove_ignore_keys_(a_)
							# rename keys
							for key in state_dict.copy().keys():
														snake_case_           =				state_dict.pop(a_)
														snake_case_           =				val
							# read in qkv matrices
							read_in_q_k_v(a_				,	a_)

							# load HuggingFace model
							snake_case_           =				DPTForSemanticSegmentation(a_) if 'ade' in checkpoint_url else DPTForDepthEstimation(a_)
							model.load_state_dict(a_)
							model.eval()

							# Check outputs on an image
							snake_case_           =				4_80 if 'ade' in checkpoint_url else 3_84
							snake_case_           =				DPTImageProcessor(size=a_)

							snake_case_           =				prepare_img()
							snake_case_           =				image_processor(a_				,	return_tensors='pt')

							# forward pass
							snake_case_           =				model(**a_).logits if 'ade' in checkpoint_url else model(**a_).predicted_depth

							# Assert logits
							snake_case_           =				torch.tensor([[6.31_99, 6.36_29, 6.41_48], [6.38_50, 6.36_15, 6.41_66], [6.35_19, 6.31_76, 6.35_75]])
							if "ade" in checkpoint_url:
														snake_case_           =				torch.tensor([[4.04_80, 4.24_20, 4.43_60], [4.31_24, 4.56_93, 4.82_61], [4.57_68, 4.89_65, 5.21_63]])
							assert outputs.shape == torch.Size(a_)
							assert (
							    torch.allclose(outputs[0, 0, :3, :3]				,	a_				,	atol=1E-4)
							    if "ade" in checkpoint_url
							    else torch.allclose(outputs[0, :3, :3]				,	a_)
							)

							Path(a_).mkdir(exist_ok=a_)
							print(f'''Saving model to {pytorch_dump_folder_path}''')
							model.save_pretrained(a_)
							print(f'''Saving image processor to {pytorch_dump_folder_path}''')
							image_processor.save_pretrained(a_)

							if push_to_hub:
														print('Pushing model to hub...')
														model.push_to_hub(
														    repo_path_or_name=Path(a_				,	a_)				,	organization='nielsr'				,	commit_message='Add model'				,	use_temp_dir=a_				,	)
														image_processor.push_to_hub(
														    repo_path_or_name=Path(a_				,	a_)				,	organization='nielsr'				,	commit_message='Add image processor'				,	use_temp_dir=a_				,	)


if __name__ == "__main__":
	lowercase    				=   argparse.ArgumentParser()
	# Required parameters
	parser.add_argument(
	    "--checkpoint_url",
	    default="https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt",
	    type=str,
	    help="URL of the original DPT checkpoint you'd like to convert.",
	)
	parser.add_argument(
	    "--pytorch_dump_folder_path",
	    default=None,
	    type=str,
	    required=True,
	    help="Path to the output PyTorch model directory.",
	)
	parser.add_argument(
	    "--push_to_hub",
	    action="store_true",
	)
	parser.add_argument(
	    "--model_name",
	    default="dpt-large",
	    type=str,
	    help="Name of the model, in case you're pushing to the hub.",
	)

	lowercase    				=   parser.parse_args()
	convert_dpt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)





