


import argparse
import io

import requests
import torch
from omegaconf import OmegaConf

from diffusers import AutoencoderKL
from diffusers.pipelines.stable_diffusion.convert_from_ckpt import (
    assign_to_checkpoint,
    conv_attn_to_linear,
    create_vae_diffusers_config,
    renew_vae_attention_paths,
    renew_vae_resnet_paths,
)




def       a_ (       SCREAMING_SNAKE_CASE__   : Any						,     SCREAMING_SNAKE_CASE__   : Optional[Any]	):
       '''simple docstring'''





       _lowerCamelCase			:						List[Any]            =checkpoint

       _lowerCamelCase			:						Union[str, Any]            ={}

       _lowerCamelCase			:						str            =vae_state_dict['''encoder.conv_in.weight''']
       _lowerCamelCase			:						Optional[Any]            =vae_state_dict['''encoder.conv_in.bias''']
       _lowerCamelCase			:						Dict            =vae_state_dict['''encoder.conv_out.weight''']
       _lowerCamelCase			:						List[str]            =vae_state_dict['''encoder.conv_out.bias''']
       _lowerCamelCase			:						Union[str, Any]            =vae_state_dict['''encoder.norm_out.weight''']
       _lowerCamelCase			:						str            =vae_state_dict['''encoder.norm_out.bias''']

       _lowerCamelCase			:						Optional[Any]            =vae_state_dict['''decoder.conv_in.weight''']
       _lowerCamelCase			:						Optional[int]            =vae_state_dict['''decoder.conv_in.bias''']
       _lowerCamelCase			:						Optional[Any]            =vae_state_dict['''decoder.conv_out.weight''']
       _lowerCamelCase			:						str            =vae_state_dict['''decoder.conv_out.bias''']
       _lowerCamelCase			:						List[Any]            =vae_state_dict['''decoder.norm_out.weight''']
       _lowerCamelCase			:						Dict            =vae_state_dict['''decoder.norm_out.bias''']

       _lowerCamelCase			:						Optional[int]            =vae_state_dict['''quant_conv.weight''']
       _lowerCamelCase			:						Union[str, Any]            =vae_state_dict['''quant_conv.bias''']
       _lowerCamelCase			:						Union[str, Any]            =vae_state_dict['''post_quant_conv.weight''']
       _lowerCamelCase			:						Dict            =vae_state_dict['''post_quant_conv.bias''']

       # Retrieves the keys for the encoder down blocks only
       _lowerCamelCase			:						Dict            =len({'.'.join(layer.split('.'	)[:3]	) for layer in vae_state_dict if 'encoder.down' in layer}	)
       _lowerCamelCase			:						Dict            ={
           layer_id: [key for key in vae_state_dict if F'''down.{layer_id}''' in key] for layer_id in range(UpperCAmelCase_	)
       }

       # Retrieves the keys for the decoder up blocks only
       _lowerCamelCase			:						Optional[Any]            =len({'.'.join(layer.split('.'	)[:3]	) for layer in vae_state_dict if 'decoder.up' in layer}	)
       _lowerCamelCase			:						Dict            ={
           layer_id: [key for key in vae_state_dict if F'''up.{layer_id}''' in key] for layer_id in range(UpperCAmelCase_	)
       }

       for i in range(UpperCAmelCase_	):
              _lowerCamelCase			:						Any            =[key for key in down_blocks[i] if F'''down.{i}''' in key and F'''down.{i}.downsample''' not in key]

              if F'''encoder.down.{i}.downsample.conv.weight''' in vae_state_dict:
                     _lowerCamelCase			:						Dict            =vae_state_dict.pop(
                         F'''encoder.down.{i}.downsample.conv.weight'''	)
                     _lowerCamelCase			:						Optional[Any]            =vae_state_dict.pop(
                         F'''encoder.down.{i}.downsample.conv.bias'''	)

              _lowerCamelCase			:						List[str]            =renew_vae_resnet_paths(UpperCAmelCase_	)
              _lowerCamelCase			:						Optional[Any]            ={'''old''': F'''down.{i}.block''', '''new''': F'''down_blocks.{i}.resnets'''}
              assign_to_checkpoint(UpperCAmelCase_						,     UpperCAmelCase_						,     UpperCAmelCase_						,     additional_replacements=[meta_path]						,     config=UpperCAmelCase_	)

       _lowerCamelCase			:						Any            =[key for key in vae_state_dict if '''encoder.mid.block''' in key]
       _lowerCamelCase			:						Dict            =2
       for i in range(1						,     num_mid_res_blocks + 1	):
              _lowerCamelCase			:						int            =[key for key in mid_resnets if F'''encoder.mid.block_{i}''' in key]

              _lowerCamelCase			:						Dict            =renew_vae_resnet_paths(UpperCAmelCase_	)
              _lowerCamelCase			:						Any            ={'''old''': F'''mid.block_{i}''', '''new''': F'''mid_block.resnets.{i - 1}'''}
              assign_to_checkpoint(UpperCAmelCase_						,     UpperCAmelCase_						,     UpperCAmelCase_						,     additional_replacements=[meta_path]						,     config=UpperCAmelCase_	)

       _lowerCamelCase			:						Optional[Any]            =[key for key in vae_state_dict if '''encoder.mid.attn''' in key]
       _lowerCamelCase			:						str            =renew_vae_attention_paths(UpperCAmelCase_	)
       _lowerCamelCase			:						List[Any]            ={'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''}
       assign_to_checkpoint(UpperCAmelCase_						,     UpperCAmelCase_						,     UpperCAmelCase_						,     additional_replacements=[meta_path]						,     config=UpperCAmelCase_	)
       conv_attn_to_linear(UpperCAmelCase_	)

       for i in range(UpperCAmelCase_	):
              _lowerCamelCase			:						Dict            =num_up_blocks - 1 - i
              _lowerCamelCase			:						Union[str, Any]            =[
                  key for key in up_blocks[block_id] if F'''up.{block_id}''' in key and F'''up.{block_id}.upsample''' not in key
              ]

              if F'''decoder.up.{block_id}.upsample.conv.weight''' in vae_state_dict:
                     _lowerCamelCase			:						Dict            =vae_state_dict[
                         F'''decoder.up.{block_id}.upsample.conv.weight'''
                     ]
                     _lowerCamelCase			:						str            =vae_state_dict[
                         F'''decoder.up.{block_id}.upsample.conv.bias'''
                     ]

              _lowerCamelCase			:						int            =renew_vae_resnet_paths(UpperCAmelCase_	)
              _lowerCamelCase			:						Tuple            ={'''old''': F'''up.{block_id}.block''', '''new''': F'''up_blocks.{i}.resnets'''}
              assign_to_checkpoint(UpperCAmelCase_						,     UpperCAmelCase_						,     UpperCAmelCase_						,     additional_replacements=[meta_path]						,     config=UpperCAmelCase_	)

       _lowerCamelCase			:						str            =[key for key in vae_state_dict if '''decoder.mid.block''' in key]
       _lowerCamelCase			:						Any            =2
       for i in range(1						,     num_mid_res_blocks + 1	):
              _lowerCamelCase			:						Union[str, Any]            =[key for key in mid_resnets if F'''decoder.mid.block_{i}''' in key]

              _lowerCamelCase			:						int            =renew_vae_resnet_paths(UpperCAmelCase_	)
              _lowerCamelCase			:						int            ={'''old''': F'''mid.block_{i}''', '''new''': F'''mid_block.resnets.{i - 1}'''}
              assign_to_checkpoint(UpperCAmelCase_						,     UpperCAmelCase_						,     UpperCAmelCase_						,     additional_replacements=[meta_path]						,     config=UpperCAmelCase_	)

       _lowerCamelCase			:						Optional[int]            =[key for key in vae_state_dict if '''decoder.mid.attn''' in key]
       _lowerCamelCase			:						str            =renew_vae_attention_paths(UpperCAmelCase_	)
       _lowerCamelCase			:						Dict            ={'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''}
       assign_to_checkpoint(UpperCAmelCase_						,     UpperCAmelCase_						,     UpperCAmelCase_						,     additional_replacements=[meta_path]						,     config=UpperCAmelCase_	)
       conv_attn_to_linear(UpperCAmelCase_	)
       return new_checkpoint




def       a_ (       SCREAMING_SNAKE_CASE__   : str						,     SCREAMING_SNAKE_CASE__   : str						,     ):
       '''simple docstring'''





       _lowerCamelCase			:						int            =requests.get(
           ' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml'	)
       _lowerCamelCase			:						Tuple            =io.BytesIO(r.content	)

       _lowerCamelCase			:						Optional[Any]            =OmegaConf.load(UpperCAmelCase_	)
       _lowerCamelCase			:						Any            =512
       _lowerCamelCase			:						int            ='''cuda''' if torch.cuda.is_available() else '''cpu'''
       if checkpoint_path.endswith('safetensors'	):
              from safetensors import safe_open

              _lowerCamelCase			:						List[str]            ={}
              with safe_open(UpperCAmelCase_						,     framework='pt'						,     device='cpu'	) as f:
                     for key in f.keys():
                            _lowerCamelCase			:						Dict            =f.get_tensor(UpperCAmelCase_	)
       else:
              _lowerCamelCase			:						List[str]            =torch.load(UpperCAmelCase_						,     map_location=UpperCAmelCase_	)['''state_dict''']

       # Convert the VAE model.
       _lowerCamelCase			:						Dict            =create_vae_diffusers_config(UpperCAmelCase_						,     image_size=UpperCAmelCase_	)
       _lowerCamelCase			:						str            =custom_convert_ldm_vae_checkpoint(UpperCAmelCase_						,     UpperCAmelCase_	)

       _lowerCamelCase			:						Any            =AutoencoderKL(**UpperCAmelCase_	)
       vae.load_state_dict(UpperCAmelCase_	)
       vae.save_pretrained(UpperCAmelCase_	)


if __name__ == "__main__":
 lowerCamelCase		   =			argparse.ArgumentParser()

 parser.add_argument('--vae_pt_path', default=None, type=str, required=True, help='Path to the VAE.pt to convert.')
 parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the VAE.pt to convert.')

 lowerCamelCase		   =			parser.parse_args()

 vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)




from collections.abc import Sequence
def 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     Sequence[int] | None = None				)			->						int:
       if nums is None or not nums:
              raise ValueError('''Input sequence should not be empty'''				)

       SCREAMING_SNAKE_CASE_							:				Tuple		=nums[0]
       for i in range(1			,							len(UpperCAmelCase_				)				):
              SCREAMING_SNAKE_CASE_							:				Any		=nums[i]
              SCREAMING_SNAKE_CASE_							:				Optional[int]		=max(UpperCAmelCase_			,							ans + num			,							UpperCAmelCase_				)

       return ans


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 # Try on a sample input from the user
 _lowercase											=    int(input("""Enter number of elements : """).strip())
 _lowercase											=    list(map(int, input("""\nEnter the numbers : """).strip().split()))[:n]
 print(max_subsequence_sum(array))
