
import argparse
import json

import torch

from diffusers import AutoencoderKL, DDPMPipeline, DDPMScheduler, UNetaDModel, VQModel



def      _a     (   __UpperCamelCase       :  str	,__UpperCamelCase       :  Dict=1    ):
			if n_shave_prefix_segments >= 0:
						return ".".join(path.split('''.'''    )[n_shave_prefix_segments:]    )
			else:
						return ".".join(path.split('''.'''    )[:n_shave_prefix_segments]    )



def      _a     (   __UpperCamelCase       :  Tuple	,__UpperCamelCase       :  int=0    ):
			lowerCAmelCase__ :    Optional[int]				      =			[]
			for old_item in old_list:
						lowerCAmelCase__ :    str				      =			old_item
						lowerCAmelCase__ :    Union[str, Any]				      =			new_item.replace('''block.'''	,'''resnets.'''    )
						lowerCAmelCase__ :    Union[str, Any]				      =			new_item.replace('''conv_shorcut'''	,'''conv1'''    )
						lowerCAmelCase__ :    Optional[int]				      =			new_item.replace('''in_shortcut'''	,'''conv_shortcut'''    )
						lowerCAmelCase__ :    Tuple				      =			new_item.replace('''temb_proj'''	,'''time_emb_proj'''    )

						lowerCAmelCase__ :    Union[str, Any]				      =			shave_segments(__UpperCamelCase	,n_shave_prefix_segments=__UpperCamelCase    )

						mapping.append({'''old''': old_item, '''new''': new_item}    )

			return mapping



def      _a     (   __UpperCamelCase       :  List[Any]	,__UpperCamelCase       :  str=0	,__UpperCamelCase       :  Union[str, Any]=False    ):
			lowerCAmelCase__ :    Union[str, Any]				      =			[]
			for old_item in old_list:
						lowerCAmelCase__ :    List[str]				      =			old_item

						# In `model.mid`, the layer is called `attn`.
						if not in_mid:
									lowerCAmelCase__ :    str				      =			new_item.replace('''attn'''	,'''attentions'''    )
						lowerCAmelCase__ :    int				      =			new_item.replace('''.k.'''	,'''.key.'''    )
						lowerCAmelCase__ :    Any				      =			new_item.replace('''.v.'''	,'''.value.'''    )
						lowerCAmelCase__ :    int				      =			new_item.replace('''.q.'''	,'''.query.'''    )

						lowerCAmelCase__ :    Union[str, Any]				      =			new_item.replace('''proj_out'''	,'''proj_attn'''    )
						lowerCAmelCase__ :    Any				      =			new_item.replace('''norm'''	,'''group_norm'''    )

						lowerCAmelCase__ :    List[Any]				      =			shave_segments(__UpperCamelCase	,n_shave_prefix_segments=__UpperCamelCase    )
						mapping.append({'''old''': old_item, '''new''': new_item}    )

			return mapping



def      _a     (   __UpperCamelCase       :  Tuple	,__UpperCamelCase       :  Any	,__UpperCamelCase       :  Union[str, Any]	,__UpperCamelCase       :  Union[str, Any]=None	,__UpperCamelCase       :  List[Any]=None	,__UpperCamelCase       :  int=None    ):
			assert isinstance(__UpperCamelCase	,__UpperCamelCase    ), "Paths should be a list of dicts containing 'old' and 'new' keys."

			if attention_paths_to_split is not None:
						if config is None:
									raise ValueError('''Please specify the config if setting \'attention_paths_to_split\' to \'True\'.'''    )

						for path, path_map in attention_paths_to_split.items():
									lowerCAmelCase__ :    int				      =			old_checkpoint[path]
									lowerCAmelCase__ :    Any				      =			old_tensor.shape[0] // 3

									lowerCAmelCase__ :    Optional[Any]				      =			(-1, channels) if len(old_tensor.shape    ) == 3 else (-1)

									lowerCAmelCase__ :    List[Any]				      =			old_tensor.shape[0] // config.get('''num_head_channels'''	,1    ) // 3

									lowerCAmelCase__ :    Union[str, Any]				      =			old_tensor.reshape((num_heads, 3 * channels // num_heads) + old_tensor.shape[1:]    )
									lowerCAmelCase__ :    Dict				      =			old_tensor.split(channels // num_heads	,dim=1    )

									lowerCAmelCase__ :    Union[str, Any]				      =			query.reshape(__UpperCamelCase    ).squeeze()
									lowerCAmelCase__ :    Any				      =			key.reshape(__UpperCamelCase    ).squeeze()
									lowerCAmelCase__ :    Union[str, Any]				      =			value.reshape(__UpperCamelCase    ).squeeze()

			for path in paths:
						lowerCAmelCase__ :    Any				      =			path['''new''']

						if attention_paths_to_split is not None and new_path in attention_paths_to_split:
									continue

						lowerCAmelCase__ :    int				      =			new_path.replace('''down.'''	,'''down_blocks.'''    )
						lowerCAmelCase__ :    Dict				      =			new_path.replace('''up.'''	,'''up_blocks.'''    )

						if additional_replacements is not None:
									for replacement in additional_replacements:
												lowerCAmelCase__ :    List[str]				      =			new_path.replace(replacement['''old''']	,replacement['''new''']    )

						if "attentions" in new_path:
									lowerCAmelCase__ :    Union[str, Any]				      =			old_checkpoint[path['''old''']].squeeze()
						else:
									lowerCAmelCase__ :    Union[str, Any]				      =			old_checkpoint[path['''old''']]



def      _a     (   __UpperCamelCase       :  Dict	,__UpperCamelCase       :  int    ):
			lowerCAmelCase__ :    Union[str, Any]				      =			{}

			lowerCAmelCase__ :    Any				      =			checkpoint['''temb.dense.0.weight''']
			lowerCAmelCase__ :    Any				      =			checkpoint['''temb.dense.0.bias''']
			lowerCAmelCase__ :    int				      =			checkpoint['''temb.dense.1.weight''']
			lowerCAmelCase__ :    Optional[int]				      =			checkpoint['''temb.dense.1.bias''']

			lowerCAmelCase__ :    List[Any]				      =			checkpoint['''norm_out.weight''']
			lowerCAmelCase__ :    Optional[Any]				      =			checkpoint['''norm_out.bias''']

			lowerCAmelCase__ :    List[str]				      =			checkpoint['''conv_in.weight''']
			lowerCAmelCase__ :    Optional[int]				      =			checkpoint['''conv_in.bias''']
			lowerCAmelCase__ :    int				      =			checkpoint['''conv_out.weight''']
			lowerCAmelCase__ :    Tuple				      =			checkpoint['''conv_out.bias''']

			lowerCAmelCase__ :    str				      =			len({'''.'''.join(layer.split('''.'''    )[:2]    ) for layer in checkpoint if '''down''' in layer}    )
			lowerCAmelCase__ :    List[str]				      =			{
			    layer_id: [key for key in checkpoint if f'''down.{layer_id}''' in key] for layer_id in range(__UpperCamelCase    )
			}

			lowerCAmelCase__ :    Optional[Any]				      =			len({'''.'''.join(layer.split('''.'''    )[:2]    ) for layer in checkpoint if '''up''' in layer}    )
			lowerCAmelCase__ :    str				      =			{layer_id: [key for key in checkpoint if f'''up.{layer_id}''' in key] for layer_id in range(__UpperCamelCase    )}

			for i in range(__UpperCamelCase    ):
						lowerCAmelCase__ :    Dict				      =			(i - 1) // (config['''layers_per_block'''] + 1)

						if any('''downsample''' in layer for layer in down_blocks[i]    ):
									lowerCAmelCase__ :    Tuple				      =			checkpoint[
									    f'''down.{i}.downsample.op.weight'''
									]
									lowerCAmelCase__ :    Union[str, Any]				      =			checkpoint[f'''down.{i}.downsample.op.bias''']
						#            new_checkpoint[f'down_blocks.{i}.downsamplers.0.op.weight'] = checkpoint[f'down.{i}.downsample.conv.weight']
						#            new_checkpoint[f'down_blocks.{i}.downsamplers.0.op.bias'] = checkpoint[f'down.{i}.downsample.conv.bias']

						if any('''block''' in layer for layer in down_blocks[i]    ):
									lowerCAmelCase__ :    Optional[Any]				      =			len(
									    {'''.'''.join(shave_segments(__UpperCamelCase	,2    ).split('''.'''    )[:2]    ) for layer in down_blocks[i] if '''block''' in layer}    )
									lowerCAmelCase__ :    Any				      =			{
									    layer_id: [key for key in down_blocks[i] if f'''block.{layer_id}''' in key]
									    for layer_id in range(__UpperCamelCase    )
									}

									if num_blocks > 0:
												for j in range(config['''layers_per_block''']    ):
															lowerCAmelCase__ :    Any				      =			renew_resnet_paths(blocks[j]    )
															assign_to_checkpoint(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase    )

						if any('''attn''' in layer for layer in down_blocks[i]    ):
									lowerCAmelCase__ :    Any				      =			len(
									    {'''.'''.join(shave_segments(__UpperCamelCase	,2    ).split('''.'''    )[:2]    ) for layer in down_blocks[i] if '''attn''' in layer}    )
									lowerCAmelCase__ :    Any				      =			{
									    layer_id: [key for key in down_blocks[i] if f'''attn.{layer_id}''' in key]
									    for layer_id in range(__UpperCamelCase    )
									}

									if num_attn > 0:
												for j in range(config['''layers_per_block''']    ):
															lowerCAmelCase__ :    str				      =			renew_attention_paths(attns[j]    )
															assign_to_checkpoint(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,config=__UpperCamelCase    )

			lowerCAmelCase__ :    Optional[int]				      =			[key for key in checkpoint if '''mid.block_1''' in key]
			lowerCAmelCase__ :    Dict				      =			[key for key in checkpoint if '''mid.block_2''' in key]
			lowerCAmelCase__ :    Any				      =			[key for key in checkpoint if '''mid.attn_1''' in key]

			# Mid new 2
			lowerCAmelCase__ :    int				      =			renew_resnet_paths(__UpperCamelCase    )
			assign_to_checkpoint(
			    __UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,additional_replacements=[{'''old''': '''mid.''', '''new''': '''mid_new_2.'''}, {'''old''': '''block_1''', '''new''': '''resnets.0'''}]	,)

			lowerCAmelCase__ :    str				      =			renew_resnet_paths(__UpperCamelCase    )
			assign_to_checkpoint(
			    __UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,additional_replacements=[{'''old''': '''mid.''', '''new''': '''mid_new_2.'''}, {'''old''': '''block_2''', '''new''': '''resnets.1'''}]	,)

			lowerCAmelCase__ :    List[str]				      =			renew_attention_paths(__UpperCamelCase	,in_mid=__UpperCamelCase    )
			assign_to_checkpoint(
			    __UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,additional_replacements=[{'''old''': '''mid.''', '''new''': '''mid_new_2.'''}, {'''old''': '''attn_1''', '''new''': '''attentions.0'''}]	,)

			for i in range(__UpperCamelCase    ):
						lowerCAmelCase__ :    str				      =			num_up_blocks - 1 - i

						if any('''upsample''' in layer for layer in up_blocks[i]    ):
									lowerCAmelCase__ :    Optional[int]				      =			checkpoint[
									    f'''up.{i}.upsample.conv.weight'''
									]
									lowerCAmelCase__ :    List[str]				      =			checkpoint[f'''up.{i}.upsample.conv.bias''']

						if any('''block''' in layer for layer in up_blocks[i]    ):
									lowerCAmelCase__ :    Dict				      =			len(
									    {'''.'''.join(shave_segments(__UpperCamelCase	,2    ).split('''.'''    )[:2]    ) for layer in up_blocks[i] if '''block''' in layer}    )
									lowerCAmelCase__ :    Optional[Any]				      =			{
									    layer_id: [key for key in up_blocks[i] if f'''block.{layer_id}''' in key] for layer_id in range(__UpperCamelCase    )
									}

									if num_blocks > 0:
												for j in range(config['''layers_per_block'''] + 1    ):
															lowerCAmelCase__ :    str				      =			{'''old''': f'''up_blocks.{i}''', '''new''': f'''up_blocks.{block_id}'''}
															lowerCAmelCase__ :    List[Any]				      =			renew_resnet_paths(blocks[j]    )
															assign_to_checkpoint(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,additional_replacements=[replace_indices]    )

						if any('''attn''' in layer for layer in up_blocks[i]    ):
									lowerCAmelCase__ :    Union[str, Any]				      =			len(
									    {'''.'''.join(shave_segments(__UpperCamelCase	,2    ).split('''.'''    )[:2]    ) for layer in up_blocks[i] if '''attn''' in layer}    )
									lowerCAmelCase__ :    Tuple				      =			{
									    layer_id: [key for key in up_blocks[i] if f'''attn.{layer_id}''' in key] for layer_id in range(__UpperCamelCase    )
									}

									if num_attn > 0:
												for j in range(config['''layers_per_block'''] + 1    ):
															lowerCAmelCase__ :    int				      =			{'''old''': f'''up_blocks.{i}''', '''new''': f'''up_blocks.{block_id}'''}
															lowerCAmelCase__ :    Optional[int]				      =			renew_attention_paths(attns[j]    )
															assign_to_checkpoint(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,additional_replacements=[replace_indices]    )

			lowerCAmelCase__ :    List[Any]				      =			{k.replace('''mid_new_2'''	,'''mid_block'''    ): v for k, v in new_checkpoint.items()}
			return new_checkpoint



def      _a     (   __UpperCamelCase       :  List[Any]	,__UpperCamelCase       :  Tuple    ):
			lowerCAmelCase__ :    Tuple				      =			{}

			lowerCAmelCase__ :    Tuple				      =			checkpoint['''encoder.norm_out.weight''']
			lowerCAmelCase__ :    str				      =			checkpoint['''encoder.norm_out.bias''']

			lowerCAmelCase__ :    Optional[Any]				      =			checkpoint['''encoder.conv_in.weight''']
			lowerCAmelCase__ :    Any				      =			checkpoint['''encoder.conv_in.bias''']
			lowerCAmelCase__ :    Any				      =			checkpoint['''encoder.conv_out.weight''']
			lowerCAmelCase__ :    List[str]				      =			checkpoint['''encoder.conv_out.bias''']

			lowerCAmelCase__ :    str				      =			checkpoint['''decoder.norm_out.weight''']
			lowerCAmelCase__ :    Dict				      =			checkpoint['''decoder.norm_out.bias''']

			lowerCAmelCase__ :    Any				      =			checkpoint['''decoder.conv_in.weight''']
			lowerCAmelCase__ :    int				      =			checkpoint['''decoder.conv_in.bias''']
			lowerCAmelCase__ :    Dict				      =			checkpoint['''decoder.conv_out.weight''']
			lowerCAmelCase__ :    str				      =			checkpoint['''decoder.conv_out.bias''']

			lowerCAmelCase__ :    str				      =			len({'''.'''.join(layer.split('''.'''    )[:3]    ) for layer in checkpoint if '''down''' in layer}    )
			lowerCAmelCase__ :    List[str]				      =			{
			    layer_id: [key for key in checkpoint if f'''down.{layer_id}''' in key] for layer_id in range(__UpperCamelCase    )
			}

			lowerCAmelCase__ :    int				      =			len({'''.'''.join(layer.split('''.'''    )[:3]    ) for layer in checkpoint if '''up''' in layer}    )
			lowerCAmelCase__ :    int				      =			{layer_id: [key for key in checkpoint if f'''up.{layer_id}''' in key] for layer_id in range(__UpperCamelCase    )}

			for i in range(__UpperCamelCase    ):
						lowerCAmelCase__ :    Union[str, Any]				      =			(i - 1) // (config['''layers_per_block'''] + 1)

						if any('''downsample''' in layer for layer in down_blocks[i]    ):
									lowerCAmelCase__ :    Tuple				      =			checkpoint[
									    f'''encoder.down.{i}.downsample.conv.weight'''
									]
									lowerCAmelCase__ :    str				      =			checkpoint[
									    f'''encoder.down.{i}.downsample.conv.bias'''
									]

						if any('''block''' in layer for layer in down_blocks[i]    ):
									lowerCAmelCase__ :    int				      =			len(
									    {'''.'''.join(shave_segments(__UpperCamelCase	,3    ).split('''.'''    )[:3]    ) for layer in down_blocks[i] if '''block''' in layer}    )
									lowerCAmelCase__ :    str				      =			{
									    layer_id: [key for key in down_blocks[i] if f'''block.{layer_id}''' in key]
									    for layer_id in range(__UpperCamelCase    )
									}

									if num_blocks > 0:
												for j in range(config['''layers_per_block''']    ):
															lowerCAmelCase__ :    Optional[int]				      =			renew_resnet_paths(blocks[j]    )
															assign_to_checkpoint(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase    )

						if any('''attn''' in layer for layer in down_blocks[i]    ):
									lowerCAmelCase__ :    List[Any]				      =			len(
									    {'''.'''.join(shave_segments(__UpperCamelCase	,3    ).split('''.'''    )[:3]    ) for layer in down_blocks[i] if '''attn''' in layer}    )
									lowerCAmelCase__ :    Any				      =			{
									    layer_id: [key for key in down_blocks[i] if f'''attn.{layer_id}''' in key]
									    for layer_id in range(__UpperCamelCase    )
									}

									if num_attn > 0:
												for j in range(config['''layers_per_block''']    ):
															lowerCAmelCase__ :    List[str]				      =			renew_attention_paths(attns[j]    )
															assign_to_checkpoint(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,config=__UpperCamelCase    )

			lowerCAmelCase__ :    List[Any]				      =			[key for key in checkpoint if '''mid.block_1''' in key]
			lowerCAmelCase__ :    Any				      =			[key for key in checkpoint if '''mid.block_2''' in key]
			lowerCAmelCase__ :    Any				      =			[key for key in checkpoint if '''mid.attn_1''' in key]

			# Mid new 2
			lowerCAmelCase__ :    str				      =			renew_resnet_paths(__UpperCamelCase    )
			assign_to_checkpoint(
			    __UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,additional_replacements=[{'''old''': '''mid.''', '''new''': '''mid_new_2.'''}, {'''old''': '''block_1''', '''new''': '''resnets.0'''}]	,)

			lowerCAmelCase__ :    List[Any]				      =			renew_resnet_paths(__UpperCamelCase    )
			assign_to_checkpoint(
			    __UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,additional_replacements=[{'''old''': '''mid.''', '''new''': '''mid_new_2.'''}, {'''old''': '''block_2''', '''new''': '''resnets.1'''}]	,)

			lowerCAmelCase__ :    Union[str, Any]				      =			renew_attention_paths(__UpperCamelCase	,in_mid=__UpperCamelCase    )
			assign_to_checkpoint(
			    __UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,additional_replacements=[{'''old''': '''mid.''', '''new''': '''mid_new_2.'''}, {'''old''': '''attn_1''', '''new''': '''attentions.0'''}]	,)

			for i in range(__UpperCamelCase    ):
						lowerCAmelCase__ :    Tuple				      =			num_up_blocks - 1 - i

						if any('''upsample''' in layer for layer in up_blocks[i]    ):
									lowerCAmelCase__ :    str				      =			checkpoint[
									    f'''decoder.up.{i}.upsample.conv.weight'''
									]
									lowerCAmelCase__ :    Optional[Any]				      =			checkpoint[
									    f'''decoder.up.{i}.upsample.conv.bias'''
									]

						if any('''block''' in layer for layer in up_blocks[i]    ):
									lowerCAmelCase__ :    Tuple				      =			len(
									    {'''.'''.join(shave_segments(__UpperCamelCase	,3    ).split('''.'''    )[:3]    ) for layer in up_blocks[i] if '''block''' in layer}    )
									lowerCAmelCase__ :    Union[str, Any]				      =			{
									    layer_id: [key for key in up_blocks[i] if f'''block.{layer_id}''' in key] for layer_id in range(__UpperCamelCase    )
									}

									if num_blocks > 0:
												for j in range(config['''layers_per_block'''] + 1    ):
															lowerCAmelCase__ :    List[Any]				      =			{'''old''': f'''up_blocks.{i}''', '''new''': f'''up_blocks.{block_id}'''}
															lowerCAmelCase__ :    int				      =			renew_resnet_paths(blocks[j]    )
															assign_to_checkpoint(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,additional_replacements=[replace_indices]    )

						if any('''attn''' in layer for layer in up_blocks[i]    ):
									lowerCAmelCase__ :    List[str]				      =			len(
									    {'''.'''.join(shave_segments(__UpperCamelCase	,3    ).split('''.'''    )[:3]    ) for layer in up_blocks[i] if '''attn''' in layer}    )
									lowerCAmelCase__ :    int				      =			{
									    layer_id: [key for key in up_blocks[i] if f'''attn.{layer_id}''' in key] for layer_id in range(__UpperCamelCase    )
									}

									if num_attn > 0:
												for j in range(config['''layers_per_block'''] + 1    ):
															lowerCAmelCase__ :    Any				      =			{'''old''': f'''up_blocks.{i}''', '''new''': f'''up_blocks.{block_id}'''}
															lowerCAmelCase__ :    Any				      =			renew_attention_paths(attns[j]    )
															assign_to_checkpoint(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase	,additional_replacements=[replace_indices]    )

			lowerCAmelCase__ :    Tuple				      =			{k.replace('''mid_new_2'''	,'''mid_block'''    ): v for k, v in new_checkpoint.items()}
			lowerCAmelCase__ :    int				      =			checkpoint['''quant_conv.weight''']
			lowerCAmelCase__ :    List[Any]				      =			checkpoint['''quant_conv.bias''']
			if "quantize.embedding.weight" in checkpoint:
						lowerCAmelCase__ :    Optional[Any]				      =			checkpoint['''quantize.embedding.weight''']
			lowerCAmelCase__ :    Optional[Any]				      =			checkpoint['''post_quant_conv.weight''']
			lowerCAmelCase__ :    str				      =			checkpoint['''post_quant_conv.bias''']

			return new_checkpoint


if __name__ == "__main__":
							A__       :   List[str]   						=					argparse.ArgumentParser()

							parser.add_argument(
							    """--checkpoint_path""", default=None, type=str, required=True, help="""Path to the checkpoint to convert."""
							)

							parser.add_argument(
							    """--config_file""",
							    default=None,
							    type=str,
							    required=True,
							    help="""The config json file corresponding to the architecture.""",
							)

							parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""")

							A__       :   Optional[Any]   						=					parser.parse_args()
							A__       :   Optional[int]   						=					torch.load(args.checkpoint_path)

							with open(args.config_file) as f:
														A__       :   Union[str, Any]   						=					json.loads(f.read())

							# unet case
							A__       :   Optional[int]   						=					{key.split(""".""")[0] for key in checkpoint.keys()}
							if "encoder" in key_prefix_set and "decoder" in key_prefix_set:
														A__       :   Union[str, Any]   						=					convert_vq_autoenc_checkpoint(checkpoint, config)
							else:
														A__       :   Tuple   						=					convert_ddpm_checkpoint(checkpoint, config)

							if "ddpm" in config:
														del config["ddpm"]

							if config["_class_name"] == "VQModel":
														A__       :   int   						=					VQModel(**config)
														model.load_state_dict(converted_checkpoint)
														model.save_pretrained(args.dump_path)
							elif config["_class_name"] == "AutoencoderKL":
														A__       :   List[Any]   						=					AutoencoderKL(**config)
														model.load_state_dict(converted_checkpoint)
														model.save_pretrained(args.dump_path)
							else:
														A__       :   Tuple   						=					UNetaDModel(**config)
														model.load_state_dict(converted_checkpoint)

														A__       :   Union[str, Any]   						=					DDPMScheduler.from_config("""/""".join(args.checkpoint_path.split("""/""")[:-1]))

														A__       :   Dict   						=					DDPMPipeline(unet=model, scheduler=scheduler)
														pipe.save_pretrained(args.dump_path)





from math import radians as angle_to_radians
from math import sin

# Acceleration Constant on Earth (unit m/s^2)
A__       :   str   						=					9.8_0665



def      _a     (   __UpperCamelCase       :  float	,__UpperCamelCase       :  float    ):

			# Ensure valid instance
			if not isinstance(__UpperCamelCase	,(int, float)    ):
						raise TypeError('''Invalid velocity. Should be a positive number.'''    )

			if not isinstance(__UpperCamelCase	,(int, float)    ):
						raise TypeError('''Invalid angle. Range is 1-90 degrees.'''    )

			# Ensure valid angle
			if angle > 90 or angle < 1:
						raise ValueError('''Invalid angle. Range is 1-90 degrees.'''    )

			# Ensure valid velocity
			if init_velocity < 0:
						raise ValueError('''Invalid velocity. Should be a positive number.'''    )



def      _a     (   __UpperCamelCase       :  float	,__UpperCamelCase       :  float    ):
			check_args(__UpperCamelCase	,__UpperCamelCase    )
			lowerCAmelCase__ :    Union[str, Any]				      =			angle_to_radians(2 * angle    )
			return round(init_velocity**2 * sin(__UpperCamelCase    ) / g	,2    )



def      _a     (   __UpperCamelCase       :  float	,__UpperCamelCase       :  float    ):
			check_args(__UpperCamelCase	,__UpperCamelCase    )
			lowerCAmelCase__ :    List[str]				      =			angle_to_radians(__UpperCamelCase    )
			return round(init_velocity**2 * sin(__UpperCamelCase    ) ** 2 / (2 * g)	,2    )



def      _a     (   __UpperCamelCase       :  float	,__UpperCamelCase       :  float    ):
			check_args(__UpperCamelCase	,__UpperCamelCase    )
			lowerCAmelCase__ :    Dict				      =			angle_to_radians(__UpperCamelCase    )
			return round(2 * init_velocity * sin(__UpperCamelCase    ) / g	,2    )



def      _a     (   ):
			lowerCAmelCase__      ,				lowerCAmelCase__ :    List[Any]				      =			25, 20
			assert horizontal_distance(__UpperCamelCase	,__UpperCamelCase    ) == 40.97
			assert max_height(__UpperCamelCase	,__UpperCamelCase    ) == 3.73
			assert total_time(__UpperCamelCase	,__UpperCamelCase    ) == 1.74


if __name__ == "__main__":
							from doctest import testmod

							testmod()

							# Get input from user
							A__       :   List[Any]   						=					float(input("""Initial Velocity: """).strip())

							# Get input from user
							A__       :   Optional[Any]   						=					float(input("""angle: """).strip())

							# Print results
							print()
							print("""Results: """)
							print(f"""Horizontal Distance: {horizontal_distance(init_vel, angle)!s} [m]""")
							print(f"""Maximum Height: {max_height(init_vel, angle)!s} [m]""")
							print(f"""Total Time: {total_time(init_vel, angle)!s} [s]""")



