





import argparse
import json

import torch

from diffusers import DDPMScheduler, LDMPipeline, UNetaDModel, VQModel

def 							SCREAMING_SNAKE_CASE__       (  snake_case__  :List[Any]			,							snake_case__  :Optional[Any]=1     )      ->						int:
  if n_shave_prefix_segments >= 0:
    return ".".join(path.split('.'     )[n_shave_prefix_segments:]     )
  else:
    return ".".join(path.split('.'     )[:n_shave_prefix_segments]     )

def 							SCREAMING_SNAKE_CASE__       (  snake_case__  :Optional[int]			,							snake_case__  :List[Any]=0     )      ->						Union[str, Any]:
  _lowercase							= []
  for old_item in old_list:
    _lowercase							= old_item.replace('in_layers.0'			,							'norm1'     )
    _lowercase							= new_item.replace('in_layers.2'			,							'conv1'     )

    _lowercase							= new_item.replace('out_layers.0'			,							'norm2'     )
    _lowercase							= new_item.replace('out_layers.3'			,							'conv2'     )

    _lowercase							= new_item.replace('emb_layers.1'			,							'time_emb_proj'     )
    _lowercase							= new_item.replace('skip_connection'			,							'conv_shortcut'     )

    _lowercase							= shave_segments(snake_case__			,							n_shave_prefix_segments=snake_case__     )

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

  return mapping

def 							SCREAMING_SNAKE_CASE__       (  snake_case__  :str			,							snake_case__  :int=0     )      ->						Union[str, Any]:
  _lowercase							= []
  for old_item in old_list:
    _lowercase							= old_item

    _lowercase							= new_item.replace('norm.weight'			,							'group_norm.weight'     )
    _lowercase							= new_item.replace('norm.bias'			,							'group_norm.bias'     )

    _lowercase							= new_item.replace('proj_out.weight'			,							'proj_attn.weight'     )
    _lowercase							= new_item.replace('proj_out.bias'			,							'proj_attn.bias'     )

    _lowercase							= shave_segments(snake_case__			,							n_shave_prefix_segments=snake_case__     )

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

  return mapping

def 							SCREAMING_SNAKE_CASE__       (  snake_case__  :str			,							snake_case__  :Any			,							snake_case__  :Optional[Any]			,							snake_case__  :Dict=None			,							snake_case__  :Union[str, Any]=None			,							snake_case__  :Union[str, Any]=None     )      ->						Dict:
  assert isinstance(snake_case__			,							snake_case__     ), "Paths should be a list of dicts containing 'old' and 'new' keys."

  # Splits the attention layers into three variables.
  if attention_paths_to_split is not None:
    for path, path_map in attention_paths_to_split.items():
      _lowercase							= old_checkpoint[path]
      _lowercase							= old_tensor.shape[0] // 3

      _lowercase							= (-1, channels) if len(old_tensor.shape     ) == 3 else (-1)

      _lowercase							= old_tensor.shape[0] // config['num_head_channels'] // 3

      _lowercase							= old_tensor.reshape((num_heads, 3 * channels // num_heads) + old_tensor.shape[1:]     )
      _lowercase						,		_lowercase						,		_lowercase							= old_tensor.split(channels // num_heads			,							dim=1     )

      _lowercase							= query.reshape(snake_case__     )
      _lowercase							= key.reshape(snake_case__     )
      _lowercase							= value.reshape(snake_case__     )

  for path in paths:
    _lowercase							= path['new']

    # These have already been assigned
    if attention_paths_to_split is not None and new_path in attention_paths_to_split:
      continue

    # Global renaming happens here
    _lowercase							= new_path.replace('middle_block.0'			,							'mid_block.resnets.0'     )
    _lowercase							= new_path.replace('middle_block.1'			,							'mid_block.attentions.0'     )
    _lowercase							= new_path.replace('middle_block.2'			,							'mid_block.resnets.1'     )

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

        # proj_attn.weight has to be converted from conv 1D to linear
    if "proj_attn.weight" in new_path:
      _lowercase							= old_checkpoint[path['old']][:, :, 0]
    else:
      _lowercase							= old_checkpoint[path['old']]

def 							SCREAMING_SNAKE_CASE__       (  snake_case__  :List[Any]			,							snake_case__  :Union[str, Any]     )      ->						Optional[int]:
  _lowercase							= {}

  _lowercase							= checkpoint['time_embed.0.weight']
  _lowercase							= checkpoint['time_embed.0.bias']
  _lowercase							= checkpoint['time_embed.2.weight']
  _lowercase							= checkpoint['time_embed.2.bias']

  _lowercase							= checkpoint['input_blocks.0.0.weight']
  _lowercase							= checkpoint['input_blocks.0.0.bias']

  _lowercase							= checkpoint['out.0.weight']
  _lowercase							= checkpoint['out.0.bias']
  _lowercase							= checkpoint['out.2.weight']
  _lowercase							= checkpoint['out.2.bias']

  # Retrieves the keys for the input blocks only
  _lowercase							= len({'.'.join(layer.split('.'     )[:2]     ) for layer in checkpoint if 'input_blocks' in layer}     )
  _lowercase							= {
      layer_id: [key for key in checkpoint if F"""input_blocks.{layer_id}""" in key]
      for layer_id in range(snake_case__     )
  }

  # Retrieves the keys for the middle blocks only
  _lowercase							= len({'.'.join(layer.split('.'     )[:2]     ) for layer in checkpoint if 'middle_block' in layer}     )
  _lowercase							= {
      layer_id: [key for key in checkpoint if F"""middle_block.{layer_id}""" in key]
      for layer_id in range(snake_case__     )
  }

  # Retrieves the keys for the output blocks only
  _lowercase							= len({'.'.join(layer.split('.'     )[:2]     ) for layer in checkpoint if 'output_blocks' in layer}     )
  _lowercase							= {
      layer_id: [key for key in checkpoint if F"""output_blocks.{layer_id}""" in key]
      for layer_id in range(snake_case__     )
  }

  for i in range(1			,							snake_case__     ):
    _lowercase							= (i - 1) // (config['num_res_blocks'] + 1)
    _lowercase							= (i - 1) % (config['num_res_blocks'] + 1)

    _lowercase							= [key for key in input_blocks[i] if F"""input_blocks.{i}.0""" in key]
    _lowercase							= [key for key in input_blocks[i] if F"""input_blocks.{i}.1""" in key]

    if F"""input_blocks.{i}.0.op.weight""" in checkpoint:
      _lowercase							= checkpoint[
          F"""input_blocks.{i}.0.op.weight"""
      ]
      _lowercase							= checkpoint[
          F"""input_blocks.{i}.0.op.bias"""
      ]
      continue

    _lowercase							= renew_resnet_paths(snake_case__     )
    _lowercase							= {'old': F"""input_blocks.{i}.0""", 'new': F"""down_blocks.{block_id}.resnets.{layer_in_block_id}"""}
    _lowercase							= {'old': 'resnets.2.op', 'new': 'downsamplers.0.op'}
    assign_to_checkpoint(
        snake_case__			,							snake_case__			,							snake_case__			,							additional_replacements=[meta_path, resnet_op]			,							config=snake_case__     )

    if len(snake_case__     ):
      _lowercase							= renew_attention_paths(snake_case__     )
      _lowercase							= {
          'old': F"""input_blocks.{i}.1""",
          'new': F"""down_blocks.{block_id}.attentions.{layer_in_block_id}""",
      }
      _lowercase							= {
          F"""input_blocks.{i}.1.qkv.bias""": {
              'key': F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias""",
              'query': F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias""",
              'value': F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias""",
          },
          F"""input_blocks.{i}.1.qkv.weight""": {
              'key': F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight""",
              'query': F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight""",
              'value': F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight""",
          },
      }
      assign_to_checkpoint(
          snake_case__			,							snake_case__			,							snake_case__			,							additional_replacements=[meta_path]			,							attention_paths_to_split=snake_case__			,							config=snake_case__			,							)

  _lowercase							= middle_blocks[0]
  _lowercase							= middle_blocks[1]
  _lowercase							= middle_blocks[2]

  _lowercase							= renew_resnet_paths(snake_case__     )
  assign_to_checkpoint(snake_case__			,							snake_case__			,							snake_case__			,							config=snake_case__     )

  _lowercase							= renew_resnet_paths(snake_case__     )
  assign_to_checkpoint(snake_case__			,							snake_case__			,							snake_case__			,							config=snake_case__     )

  _lowercase							= renew_attention_paths(snake_case__     )
  _lowercase							= {
      'middle_block.1.qkv.bias': {
          'key': 'mid_block.attentions.0.key.bias',
          'query': 'mid_block.attentions.0.query.bias',
          'value': 'mid_block.attentions.0.value.bias',
      },
      'middle_block.1.qkv.weight': {
          'key': 'mid_block.attentions.0.key.weight',
          'query': 'mid_block.attentions.0.query.weight',
          'value': 'mid_block.attentions.0.value.weight',
      },
  }
  assign_to_checkpoint(
      snake_case__			,							snake_case__			,							snake_case__			,							attention_paths_to_split=snake_case__			,							config=snake_case__     )

  for i in range(snake_case__     ):
    _lowercase							= i // (config['num_res_blocks'] + 1)
    _lowercase							= i % (config['num_res_blocks'] + 1)
    _lowercase							= [shave_segments(snake_case__			,							2     ) for name in output_blocks[i]]
    _lowercase							= {}

    for layer in output_block_layers:
      _lowercase						,		_lowercase							= layer.split('.'     )[0], shave_segments(snake_case__			,							1     )
      if layer_id in output_block_list:
        output_block_list[layer_id].append(snake_case__     )
      else:
        _lowercase							= [layer_name]

    if len(snake_case__     ) > 1:
      _lowercase							= [key for key in output_blocks[i] if F"""output_blocks.{i}.0""" in key]
      _lowercase							= [key for key in output_blocks[i] if F"""output_blocks.{i}.1""" in key]

      _lowercase							= renew_resnet_paths(snake_case__     )
      _lowercase							= renew_resnet_paths(snake_case__     )

      _lowercase							= {'old': F"""output_blocks.{i}.0""", 'new': F"""up_blocks.{block_id}.resnets.{layer_in_block_id}"""}
      assign_to_checkpoint(snake_case__			,							snake_case__			,							snake_case__			,							additional_replacements=[meta_path]			,							config=snake_case__     )

      if ["conv.weight", "conv.bias"] in output_block_list.values():
        _lowercase							= list(output_block_list.values()     ).index(['conv.weight', 'conv.bias']     )
        _lowercase							= checkpoint[
            F"""output_blocks.{i}.{index}.conv.weight"""
        ]
        _lowercase							= checkpoint[
            F"""output_blocks.{i}.{index}.conv.bias"""
        ]

        # Clear attentions as they have been attributed above.
        if len(snake_case__     ) == 2:
          _lowercase							= []

      if len(snake_case__     ):
        _lowercase							= renew_attention_paths(snake_case__     )
        _lowercase							= {
            'old': F"""output_blocks.{i}.1""",
            'new': F"""up_blocks.{block_id}.attentions.{layer_in_block_id}""",
        }
        _lowercase							= {
            F"""output_blocks.{i}.1.qkv.bias""": {
                'key': F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias""",
                'query': F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias""",
                'value': F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias""",
            },
            F"""output_blocks.{i}.1.qkv.weight""": {
                'key': F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight""",
                'query': F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight""",
                'value': F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight""",
            },
        }
        assign_to_checkpoint(
            snake_case__			,							snake_case__			,							snake_case__			,							additional_replacements=[meta_path]			,							attention_paths_to_split=to_split if any('qkv' in key for key in attentions     ) else None			,							config=snake_case__			,							)
    else:
      _lowercase							= renew_resnet_paths(snake_case__			,							n_shave_prefix_segments=1     )
      for path in resnet_0_paths:
        _lowercase							= '.'.join(['output_blocks', str(snake_case__     ), path['old']]     )
        _lowercase							= '.'.join(['up_blocks', str(snake_case__     ), 'resnets', str(snake_case__     ), path['new']]     )

        _lowercase							= checkpoint[old_path]

  return new_checkpoint


if __name__ == "__main__":
      snake_case 				=       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.""")

      snake_case 				=       parser.parse_args()

      snake_case 				=       torch.load(args.checkpoint_path)

      with open(args.config_file) as f:
            snake_case 				=       json.loads(f.read())

      snake_case 				=       convert_ldm_checkpoint(checkpoint, config)

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

      snake_case 				=       UNetaDModel(**config)
      model.load_state_dict(converted_checkpoint)

      try:
            snake_case 				=       DDPMScheduler.from_config("""/""".join(args.checkpoint_path.split("""/""")[:-1]))
            snake_case 				=       VQModel.from_pretrained("""/""".join(args.checkpoint_path.split("""/""")[:-1]))

            snake_case 				=       LDMPipeline(unet=model, scheduler=scheduler, vae=vqvae)
            pipe.save_pretrained(args.dump_path)
      except:  # noqa: E722
            model.save_pretrained(args.dump_path)






snake_case 				=       """0.18.2"""

from .configuration_utils import ConfigMixin
from .utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_onnx_available,
    is_scipy_available,
    is_torch_available,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    logging,
)


try:
      if not is_onnx_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_onnx_objects import *  # noqa F403
else:
      from .pipelines import OnnxRuntimeModel

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_pt_objects import *  # noqa F403
else:
      from .models import (
          AutoencoderKL,
          ControlNetModel,
          ModelMixin,
          PriorTransformer,
          TaFilmDecoder,
          TransformeraDModel,
          UNetaDModel,
          UNetaDConditionModel,
          UNetaDModel,
          UNetaDConditionModel,
          VQModel,
      )
      from .optimization import (
          get_constant_schedule,
          get_constant_schedule_with_warmup,
          get_cosine_schedule_with_warmup,
          get_cosine_with_hard_restarts_schedule_with_warmup,
          get_linear_schedule_with_warmup,
          get_polynomial_decay_schedule_with_warmup,
          get_scheduler,
      )
      from .pipelines import (
          AudioPipelineOutput,
          ConsistencyModelPipeline,
          DanceDiffusionPipeline,
          DDIMPipeline,
          DDPMPipeline,
          DiffusionPipeline,
          DiTPipeline,
          ImagePipelineOutput,
          KarrasVePipeline,
          LDMPipeline,
          LDMSuperResolutionPipeline,
          PNDMPipeline,
          RePaintPipeline,
          ScoreSdeVePipeline,
      )
      from .schedulers import (
          CMStochasticIterativeScheduler,
          DDIMInverseScheduler,
          DDIMParallelScheduler,
          DDIMScheduler,
          DDPMParallelScheduler,
          DDPMScheduler,
          DEISMultistepScheduler,
          DPMSolverMultistepInverseScheduler,
          DPMSolverMultistepScheduler,
          DPMSolverSinglestepScheduler,
          EulerAncestralDiscreteScheduler,
          EulerDiscreteScheduler,
          HeunDiscreteScheduler,
          IPNDMScheduler,
          KarrasVeScheduler,
          KDPMaAncestralDiscreteScheduler,
          KDPMaDiscreteScheduler,
          PNDMScheduler,
          RePaintScheduler,
          SchedulerMixin,
          ScoreSdeVeScheduler,
          UnCLIPScheduler,
          UniPCMultistepScheduler,
          VQDiffusionScheduler,
      )
      from .training_utils import EMAModel

try:
      if not (is_torch_available() and is_scipy_available()):
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_torch_and_scipy_objects import *  # noqa F403
else:
      from .schedulers import LMSDiscreteScheduler

try:
      if not (is_torch_available() and is_torchsde_available()):
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_torch_and_torchsde_objects import *  # noqa F403
else:
      from .schedulers import DPMSolverSDEScheduler

try:
      if not (is_torch_available() and is_transformers_available()):
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
      from .pipelines import (
          AltDiffusionImgaImgPipeline,
          AltDiffusionPipeline,
          AudioLDMPipeline,
          CycleDiffusionPipeline,
          IFImgaImgPipeline,
          IFImgaImgSuperResolutionPipeline,
          IFInpaintingPipeline,
          IFInpaintingSuperResolutionPipeline,
          IFPipeline,
          IFSuperResolutionPipeline,
          ImageTextPipelineOutput,
          KandinskyImgaImgPipeline,
          KandinskyInpaintPipeline,
          KandinskyPipeline,
          KandinskyPriorPipeline,
          KandinskyVaaControlnetImgaImgPipeline,
          KandinskyVaaControlnetPipeline,
          KandinskyVaaImgaImgPipeline,
          KandinskyVaaInpaintPipeline,
          KandinskyVaaPipeline,
          KandinskyVaaPriorEmbaEmbPipeline,
          KandinskyVaaPriorPipeline,
          LDMTextToImagePipeline,
          PaintByExamplePipeline,
          SemanticStableDiffusionPipeline,
          ShapEImgaImgPipeline,
          ShapEPipeline,
          StableDiffusionAttendAndExcitePipeline,
          StableDiffusionControlNetImgaImgPipeline,
          StableDiffusionControlNetInpaintPipeline,
          StableDiffusionControlNetPipeline,
          StableDiffusionDepthaImgPipeline,
          StableDiffusionDiffEditPipeline,
          StableDiffusionImageVariationPipeline,
          StableDiffusionImgaImgPipeline,
          StableDiffusionInpaintPipeline,
          StableDiffusionInpaintPipelineLegacy,
          StableDiffusionInstructPixaPixPipeline,
          StableDiffusionLatentUpscalePipeline,
          StableDiffusionLDMaDPipeline,
          StableDiffusionModelEditingPipeline,
          StableDiffusionPanoramaPipeline,
          StableDiffusionParadigmsPipeline,
          StableDiffusionPipeline,
          StableDiffusionPipelineSafe,
          StableDiffusionPixaPixZeroPipeline,
          StableDiffusionSAGPipeline,
          StableDiffusionUpscalePipeline,
          StableUnCLIPImgaImgPipeline,
          StableUnCLIPPipeline,
          TextToVideoSDPipeline,
          TextToVideoZeroPipeline,
          UnCLIPImageVariationPipeline,
          UnCLIPPipeline,
          UniDiffuserModel,
          UniDiffuserPipeline,
          UniDiffuserTextDecoder,
          VersatileDiffusionDualGuidedPipeline,
          VersatileDiffusionImageVariationPipeline,
          VersatileDiffusionPipeline,
          VersatileDiffusionTextToImagePipeline,
          VideoToVideoSDPipeline,
          VQDiffusionPipeline,
      )

try:
      if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()):
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import *  # noqa F403
else:
      from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline

try:
      if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()):
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
      from .pipelines import StableDiffusionKDiffusionPipeline

try:
      if not (is_torch_available() and is_transformers_available() and is_onnx_available()):
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_torch_and_transformers_and_onnx_objects import *  # noqa F403
else:
      from .pipelines import (
          OnnxStableDiffusionImgaImgPipeline,
          OnnxStableDiffusionInpaintPipeline,
          OnnxStableDiffusionInpaintPipelineLegacy,
          OnnxStableDiffusionPipeline,
          OnnxStableDiffusionUpscalePipeline,
          StableDiffusionOnnxPipeline,
      )

try:
      if not (is_torch_available() and is_librosa_available()):
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_torch_and_librosa_objects import *  # noqa F403
else:
      from .pipelines import AudioDiffusionPipeline, Mel

try:
      if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
      from .pipelines import SpectrogramDiffusionPipeline

try:
      if not is_flax_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_flax_objects import *  # noqa F403
else:
      from .models.controlnet_flax import FlaxControlNetModel
      from .models.modeling_flax_utils import FlaxModelMixin
      from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel
      from .models.vae_flax import FlaxAutoencoderKL
      from .pipelines import FlaxDiffusionPipeline
      from .schedulers import (
          FlaxDDIMScheduler,
          FlaxDDPMScheduler,
          FlaxDPMSolverMultistepScheduler,
          FlaxKarrasVeScheduler,
          FlaxLMSDiscreteScheduler,
          FlaxPNDMScheduler,
          FlaxSchedulerMixin,
          FlaxScoreSdeVeScheduler,
      )


try:
      if not (is_flax_available() and is_transformers_available()):
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_flax_and_transformers_objects import *  # noqa F403
else:
      from .pipelines import (
          FlaxStableDiffusionControlNetPipeline,
          FlaxStableDiffusionImgaImgPipeline,
          FlaxStableDiffusionInpaintPipeline,
          FlaxStableDiffusionPipeline,
      )

try:
      if not (is_note_seq_available()):
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      from .utils.dummy_note_seq_objects import *  # noqa F403
else:
      from .pipelines import MidiProcessor