






"""simple docstring"""





import multiprocessing
import os
from typing import BinaryIO, Optional, Union

import fsspec

from .. import Dataset, Features, NamedSplit, config
from ..formatting import query_table
from ..packaged_modules.json.json import Json
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader


class      _lowercase (     lowerCAmelCase						):


      def __init__(    self				:	Optional[Any]							,   a				:	NestedDataStructureLike[PathLike]							,   a				:	Optional[NamedSplit] = None							,   a				:	Optional[Features] = None							,   a				:	str = None							,   a				:	bool = False							,   a				:	bool = False							,   a				:	Optional[str] = None							,   a				:	Optional[int] = None							,   **a				:	str							,   ):





            """simple docstring"""







            super().__init__(
                a							,   split=a							,   features=a							,   cache_dir=a							,   keep_in_memory=a							,   streaming=a							,   num_proc=a							,   **a							,   )
            __snake_case      :    Any  			=field
            __snake_case      :    Optional[int]  			=path_or_paths if isinstance(a							,   a						) else {self.split: path_or_paths}
            __snake_case      :    int  			=Json(
                cache_dir=a							,   data_files=a							,   features=a							,   field=a							,   **a							,   )







      def _UpperCamelCase						(    self				:	str						):





            """simple docstring"""







            if self.streaming:
                  __snake_case      :    List[str]  			=self.builder.as_streaming_dataset(split=self.split						)
            # Build regular (map-style) dataset
            else:
                  __snake_case      :    Any  			=None
                  __snake_case      :    Union[str, Any]  			=None
                  __snake_case      :    List[Any]  			=None
                  __snake_case      :    List[Any]  			=None

                  self.builder.download_and_prepare(
                      download_config=a							,   download_mode=a							,   verification_mode=a							,   base_path=a							,   num_proc=self.num_proc							,   )
                  __snake_case      :    Optional[int]  			=self.builder.as_dataset(
                      split=self.split							,   verification_mode=a							,   in_memory=self.keep_in_memory						)
            return dataset



class      _lowercase :


      def __init__(    self				:	Any							,   a				:	Dataset							,   a				:	Union[PathLike, BinaryIO]							,   a				:	Optional[int] = None							,   a				:	Optional[int] = None							,   **a				:	Optional[Any]							,   ):





            """simple docstring"""







            if num_proc is not None and num_proc <= 0:
                  raise ValueError(f'''num_proc {num_proc} must be an integer > 0.'''						)

            __snake_case      :    List[str]  			=dataset
            __snake_case      :    Dict  			=path_or_buf
            __snake_case      :    Optional[Any]  			=batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
            __snake_case      :    List[str]  			=num_proc
            __snake_case      :    Union[str, Any]  			='''utf-8'''
            __snake_case      :    List[str]  			=to_json_kwargs


      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            __snake_case      :    Union[str, Any]  			=self.to_json_kwargs.pop('''path_or_buf'''							,   a						)
            __snake_case      :    Tuple  			=self.to_json_kwargs.pop('''orient'''							,   '''records'''						)
            __snake_case      :    Optional[int]  			=self.to_json_kwargs.pop('''lines'''							,   True if orient == '''records''' else False						)
            __snake_case      :    str  			=self.to_json_kwargs.pop('''index'''							,   False if orient in ['''split''', '''table'''] else True						)
            __snake_case      :    Any  			=self.to_json_kwargs.pop('''compression'''							,   a						)

            if compression not in [None, "infer", "gzip", "bz2", "xz"]:
                  raise NotImplementedError(f'''`datasets` currently does not support {compression} compression'''						)

            if isinstance(self.path_or_buf							,   (str, bytes, os.PathLike)						):
                  with fsspec.open(self.path_or_buf							,   '''wb'''							,   compression=a						) as buffer:
                        __snake_case      :    Optional[int]  			=self._write(file_obj=a							,   orient=a							,   lines=a							,   index=a							,   **self.to_json_kwargs						)
            else:
                  if compression:
                        raise NotImplementedError(
                            f'''The compression parameter is not supported when writing to a buffer, but compression={compression}'''
                            ''' was passed. Please provide a local path instead.'''						)
                  __snake_case      :    List[str]  			=self._write(
                      file_obj=self.path_or_buf							,   orient=a							,   lines=a							,   index=a							,   **self.to_json_kwargs						)
            return written


      def _UpperCamelCase						(    self				:	str							,   a				:	Union[str, Any]						):





            """simple docstring"""







            __snake_case      :    List[str]  			=args

            __snake_case      :    List[str]  			=query_table(
                table=self.dataset.data							,   key=slice(a							,   offset + self.batch_size						)							,   indices=self.dataset._indices							,   )
            __snake_case      :    Dict  			=batch.to_pandas().to_json(
                path_or_buf=a							,   orient=a							,   lines=a							,   index=a							,   **a						)
            if not json_str.endswith('''\n'''						):
                  json_str += "\n"
            return json_str.encode(self.encoding						)







      def _UpperCamelCase						(    self				:	Optional[int]							,   a				:	BinaryIO							,   a				:	int							,   a				:	Dict							,   a				:	Tuple							,   **a				:	Any							,   ):





            """simple docstring"""







            __snake_case      :    Optional[Any]  			=0

            if self.num_proc is None or self.num_proc == 1:
                  for offset in logging.tqdm(
                      range(0							,   len(self.dataset						)							,   self.batch_size						)							,   unit='''ba'''							,   disable=not logging.is_progress_bar_enabled()							,   desc='''Creating json from Arrow format'''							,   ):
                        __snake_case      :    Tuple  			=self._batch_json((offset, orient, lines, index, to_json_kwargs)						)
                        written += file_obj.write(a						)
            else:
                  __snake_case      :    Optional[Any]  			=len(self.dataset						), self.batch_size
                  with multiprocessing.Pool(self.num_proc						) as pool:
                        for json_str in logging.tqdm(
                            pool.imap(
                                self._batch_json							,   [(offset, orient, lines, index, to_json_kwargs) for offset in range(0							,   a							,   a						)]							,   )							,   total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size							,   unit='''ba'''							,   disable=not logging.is_progress_bar_enabled()							,   desc='''Creating json from Arrow format'''							,   ):
                              written += file_obj.write(a						)

            return written








"""simple docstring"""





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     __lowercase					(    a :				Any       ,		a :				List[Any] )					->					Dict:
      __snake_case      :    Any  			=checkpoint

      __snake_case      :    Dict  			={}

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

      __snake_case      :    Tuple  			=vae_state_dict['''decoder.conv_in.weight''']
      __snake_case      :    str  			=vae_state_dict['''decoder.conv_in.bias''']
      __snake_case      :    List[str]  			=vae_state_dict['''decoder.conv_out.weight''']
      __snake_case      :    Tuple  			=vae_state_dict['''decoder.conv_out.bias''']
      __snake_case      :    Union[str, Any]  			=vae_state_dict['''decoder.norm_out.weight''']
      __snake_case      :    List[str]  			=vae_state_dict['''decoder.norm_out.bias''']

      __snake_case      :    Tuple  			=vae_state_dict['''quant_conv.weight''']
      __snake_case      :    List[str]  			=vae_state_dict['''quant_conv.bias''']
      __snake_case      :    Optional[int]  			=vae_state_dict['''post_quant_conv.weight''']
      __snake_case      :    Tuple  			=vae_state_dict['''post_quant_conv.bias''']

      # Retrieves the keys for the encoder down blocks only
      __snake_case      :    Union[str, Any]  			=len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''encoder.down''' in layer} )
      __snake_case      :    Union[str, Any]  			={
          layer_id: [key for key in vae_state_dict if f'''down.{layer_id}''' in key] for layer_id in range(a )
      }

      # Retrieves the keys for the decoder up blocks only
      __snake_case      :    List[Any]  			=len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''decoder.up''' in layer} )
      __snake_case      :    List[Any]  			={
          layer_id: [key for key in vae_state_dict if f'''up.{layer_id}''' in key] for layer_id in range(a )
      }

      for i in range(a ):
            __snake_case      :    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:
                  __snake_case      :    Optional[Any]  			=vae_state_dict.pop(
                      f'''encoder.down.{i}.downsample.conv.weight''' )
                  __snake_case      :    Optional[int]  			=vae_state_dict.pop(
                      f'''encoder.down.{i}.downsample.conv.bias''' )

            __snake_case      :    Optional[Any]  			=renew_vae_resnet_paths(a )
            __snake_case      :    Union[str, Any]  			={'''old''': f'''down.{i}.block''', '''new''': f'''down_blocks.{i}.resnets'''}
            assign_to_checkpoint(a       ,		a       ,		a       ,		additional_replacements=[meta_path]       ,		config=a )

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

            __snake_case      :    List[str]  			=renew_vae_resnet_paths(a )
            __snake_case      :    List[str]  			={'''old''': f'''mid.block_{i}''', '''new''': f'''mid_block.resnets.{i - 1}'''}
            assign_to_checkpoint(a       ,		a       ,		a       ,		additional_replacements=[meta_path]       ,		config=a )

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

      for i in range(a ):
            __snake_case      :    List[Any]  			=num_up_blocks - 1 - i
            __snake_case      :    Dict  			=[
                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:
                  __snake_case      :    int  			=vae_state_dict[
                      f'''decoder.up.{block_id}.upsample.conv.weight'''
                  ]
                  __snake_case      :    Optional[Any]  			=vae_state_dict[
                      f'''decoder.up.{block_id}.upsample.conv.bias'''
                  ]

            __snake_case      :    List[str]  			=renew_vae_resnet_paths(a )
            __snake_case      :    int  			={'''old''': f'''up.{block_id}.block''', '''new''': f'''up_blocks.{i}.resnets'''}
            assign_to_checkpoint(a       ,		a       ,		a       ,		additional_replacements=[meta_path]       ,		config=a )

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

            __snake_case      :    Tuple  			=renew_vae_resnet_paths(a )
            __snake_case      :    Tuple  			={'''old''': f'''mid.block_{i}''', '''new''': f'''mid_block.resnets.{i - 1}'''}
            assign_to_checkpoint(a       ,		a       ,		a       ,		additional_replacements=[meta_path]       ,		config=a )

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



def     __lowercase					(    a :				str       ,		a :				str       ,		)					->					Optional[int]:
      # Only support V1
      __snake_case      :    Union[str, Any]  			=requests.get(
          ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml''' )
      __snake_case      :    Union[str, Any]  			=io.BytesIO(r.content )

      __snake_case      :    Any  			=OmegaConf.load(a )
      __snake_case      :    Union[str, Any]  			=512
      __snake_case      :    List[str]  			='''cuda''' if torch.cuda.is_available() else '''cpu'''
      if checkpoint_path.endswith('''safetensors''' ):
            from safetensors import safe_open

            __snake_case      :    Any  			={}
            with safe_open(a       ,		framework='''pt'''       ,		device='''cpu''' ) as f:
                  for key in f.keys():
                        __snake_case      :    Optional[int]  			=f.get_tensor(a )
      else:
            __snake_case      :    Optional[int]  			=torch.load(a       ,		map_location=a )['''state_dict''']

      # Convert the VAE model.
      __snake_case      :    Dict  			=create_vae_diffusers_config(a       ,		image_size=a )
      __snake_case      :    Union[str, Any]  			=custom_convert_ldm_vae_checkpoint(a       ,		a )

      __snake_case      :    str  			=AutoencoderKL(**a )
      vae.load_state_dict(a )
      vae.save_pretrained(a )


if __name__ == "__main__":
    UpperCamelCase_	:				Optional[Any]										= 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.""")

    UpperCamelCase_	:				Optional[Any]										= parser.parse_args()

    vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)


