


"""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   __lowerCAmelCase						(						lowercase  :  str						,							lowercase  :  Union[str, Any]			)     ->						int:


    """simple docstring"""





    snake_case   :				Dict				 =   checkpoint

    snake_case   :				Optional[Any]				 =   {}

    snake_case   :				Union[str, Any]				 =   vae_state_dict["encoder.conv_in.weight"]
    snake_case   :				Tuple				 =   vae_state_dict["encoder.conv_in.bias"]
    snake_case   :				List[str]				 =   vae_state_dict["encoder.conv_out.weight"]
    snake_case   :				List[str]				 =   vae_state_dict["encoder.conv_out.bias"]
    snake_case   :				List[str]				 =   vae_state_dict["encoder.norm_out.weight"]
    snake_case   :				Union[str, Any]				 =   vae_state_dict["encoder.norm_out.bias"]

    snake_case   :				List[str]				 =   vae_state_dict["decoder.conv_in.weight"]
    snake_case   :				List[str]				 =   vae_state_dict["decoder.conv_in.bias"]
    snake_case   :				int				 =   vae_state_dict["decoder.conv_out.weight"]
    snake_case   :				List[str]				 =   vae_state_dict["decoder.conv_out.bias"]
    snake_case   :				Dict				 =   vae_state_dict["decoder.norm_out.weight"]
    snake_case   :				Optional[int]				 =   vae_state_dict["decoder.norm_out.bias"]

    snake_case   :				Tuple				 =   vae_state_dict["quant_conv.weight"]
    snake_case   :				List[Any]				 =   vae_state_dict["quant_conv.bias"]
    snake_case   :				Dict				 =   vae_state_dict["post_quant_conv.weight"]
    snake_case   :				Optional[int]				 =   vae_state_dict["post_quant_conv.bias"]

    # Retrieves the keys for the encoder down blocks only
    snake_case   :				Optional[Any]				 =   len({".".join(layer.split("."			)[:3]			) for layer in vae_state_dict if "encoder.down" in layer}			)
    snake_case   :				List[Any]				 =   {
        layer_id: [key for key in vae_state_dict if F'down.{layer_id}' in key] for layer_id in range(lowercase			)
    }

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

    for i in range(lowercase			):
        snake_case   :				int				 =   [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   :				Tuple				 =   vae_state_dict.pop(
                F'encoder.down.{i}.downsample.conv.weight'			)
            snake_case   :				Tuple				 =   vae_state_dict.pop(
                F'encoder.down.{i}.downsample.conv.bias'			)

        snake_case   :				Any				 =   renew_vae_resnet_paths(lowercase			)
        snake_case   :				Optional[int]				 =   {"old": F'down.{i}.block', "new": F'down_blocks.{i}.resnets'}
        assign_to_checkpoint(lowercase						,							lowercase						,							lowercase						,							additional_replacements=[meta_path]						,							config=lowercase			)

    snake_case   :				List[str]				 =   [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   :				str				 =   [key for key in mid_resnets if F'encoder.mid.block_{i}' in key]

        snake_case   :				Dict				 =   renew_vae_resnet_paths(lowercase			)
        snake_case   :				str				 =   {"old": F'mid.block_{i}', "new": F'mid_block.resnets.{i - 1}'}
        assign_to_checkpoint(lowercase						,							lowercase						,							lowercase						,							additional_replacements=[meta_path]						,							config=lowercase			)

    snake_case   :				int				 =   [key for key in vae_state_dict if "encoder.mid.attn" in key]
    snake_case   :				Dict				 =   renew_vae_attention_paths(lowercase			)
    snake_case   :				int				 =   {"old": "mid.attn_1", "new": "mid_block.attentions.0"}
    assign_to_checkpoint(lowercase						,							lowercase						,							lowercase						,							additional_replacements=[meta_path]						,							config=lowercase			)
    conv_attn_to_linear(lowercase			)

    for i in range(lowercase			):
        snake_case   :				int				 =   num_up_blocks - 1 - i
        snake_case   :				Optional[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:
            snake_case   :				Any				 =   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   :				Any				 =   renew_vae_resnet_paths(lowercase			)
        snake_case   :				Any				 =   {"old": F'up.{block_id}.block', "new": F'up_blocks.{i}.resnets'}
        assign_to_checkpoint(lowercase						,							lowercase						,							lowercase						,							additional_replacements=[meta_path]						,							config=lowercase			)

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

        snake_case   :				Union[str, Any]				 =   renew_vae_resnet_paths(lowercase			)
        snake_case   :				int				 =   {"old": F'mid.block_{i}', "new": F'mid_block.resnets.{i - 1}'}
        assign_to_checkpoint(lowercase						,							lowercase						,							lowercase						,							additional_replacements=[meta_path]						,							config=lowercase			)

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







def   __lowerCAmelCase						(						lowercase  :  str						,							lowercase  :  str						,							)     ->						Tuple:


    """simple docstring"""





    snake_case   :				Optional[int]				 =   requests.get(
        " https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml"			)
    snake_case   :				Dict				 =   io.BytesIO(r.content			)

    snake_case   :				List[str]				 =   OmegaConf.load(lowercase			)
    snake_case   :				List[Any]				 =   512
    snake_case   :				Optional[Any]				 =   "cuda" if torch.cuda.is_available() else "cpu"
    if checkpoint_path.endswith("safetensors"			):
        from safetensors import safe_open

        snake_case   :				Optional[int]				 =   {}
        with safe_open(lowercase						,							framework="pt"						,							device="cpu"			) as f:
            for key in f.keys():
                snake_case   :				Tuple				 =   f.get_tensor(lowercase			)
    else:
        snake_case   :				int				 =   torch.load(lowercase						,							map_location=lowercase			)["state_dict"]

    # Convert the VAE model.
    snake_case   :				Tuple				 =   create_vae_diffusers_config(lowercase						,							image_size=lowercase			)
    snake_case   :				str				 =   custom_convert_ldm_vae_checkpoint(lowercase						,							lowercase			)

    snake_case   :				List[Any]				 =   AutoencoderKL(**lowercase			)
    vae.load_state_dict(lowercase			)
    vae.save_pretrained(lowercase			)


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

  __snake_case             =   parser.parse_args()

  vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)









"""simple docstring"""



import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
  import tensorflow as tf
  from tensorflow.python.eager import context
  from tensorflow.python.framework import ops

  from transformers import GradientAccumulator, create_optimizer







@require_tf
class _lowerCAmelCase    (				unittest.TestCase     ):







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Optional[Any]:

       '''simple docstring'''




       self.assertEqual(len(UpperCamelCase__						)						,     len(UpperCamelCase__						)						)
       for a, b in zip(UpperCamelCase__						,     UpperCamelCase__						):
           self.assertAlmostEqual(UpperCamelCase__						,     UpperCamelCase__						,     delta=UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				List[Any]				 =   GradientAccumulator()
       accumulator([tf.constant([1.0, 2.0]						)]						)
       accumulator([tf.constant([-2.0, 1.0]						)]						)
       accumulator([tf.constant([-1.0, 2.0]						)]						)
       with self.assertRaises(UpperCamelCase__						):
           accumulator([tf.constant([1.0, 1.0]						), tf.constant([2.0, 2.0]						)]						)
       self.assertEqual(accumulator.step						,     3						)
       self.assertEqual(len(accumulator.gradients						)						,     1						)
       self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist()						,     [-2.0, 5.0]						,     tol=1e-2						)
       accumulator.reset()
       self.assertEqual(accumulator.step						,     0						)
       self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist()						,     [0.0, 0.0]						,     tol=1e-2						)







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   None
       ops.enable_eager_execution_internal()
       snake_case   :				str				 =   tf.config.list_physical_devices("CPU"						)
       if len(UpperCamelCase__						) == 1:
           tf.config.set_logical_device_configuration(
               physical_devices[0]						,     [tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()]						)
       snake_case   :				Optional[int]				 =   tf.config.list_logical_devices(device_type="CPU"						)
       snake_case   :				List[str]				 =   tf.distribute.MirroredStrategy(devices=devices[:2]						)

       with strategy.scope():
           snake_case   :				int				 =   GradientAccumulator()
           snake_case   :				int				 =   tf.Variable([4.0, 3.0]						)
           snake_case ,snake_case   :				Any				 =   create_optimizer(5e-5						,     10						,     5						)
           snake_case   :				Tuple				 =   tf.Variable([0.0, 0.0]						,     trainable=UpperCamelCase__						)

       def accumulate_on_replica(UpperCamelCase__						):
           accumulator([gradient]						)

       def apply_on_replica():
           optimizer.apply_gradients(list(zip(accumulator.gradients						,     [variable]						)						)						)

       @tf.function
       def accumulate(UpperCamelCase__						,     UpperCamelCase__						):
           with strategy.scope():
               snake_case   :				Union[str, Any]				 =   strategy.experimental_local_results(UpperCamelCase__						)
               local_variables[0].assign(UpperCamelCase__						)
               local_variables[1].assign(UpperCamelCase__						)
               strategy.run(UpperCamelCase__						,     args=(gradient_placeholder,)						)

       @tf.function
       def apply_grad():
           with strategy.scope():
               strategy.run(UpperCamelCase__						)

       def _check_local_values(UpperCamelCase__						,     UpperCamelCase__						):
           snake_case   :				List[Any]				 =   strategy.experimental_local_results(accumulator._gradients[0]						)
           self.assertListAlmostEqual(values[0].value()						,     UpperCamelCase__						,     tol=1e-2						)
           self.assertListAlmostEqual(values[1].value()						,     UpperCamelCase__						,     tol=1e-2						)

       accumulate([1.0, 2.0]						,     [-1.0, 1.0]						)
       accumulate([3.0, -1.0]						,     [-1.0, -1.0]						)
       accumulate([-2.0, 2.0]						,     [3.0, -2.0]						)
       self.assertEqual(accumulator.step						,     3						)
       _check_local_values([2.0, 3.0]						,     [1.0, -2.0]						)
       apply_grad()
       self.assertListAlmostEqual(variable.value()						,     [4.0, 3.0]						,     tol=1e-2						)
       accumulator.reset()
       self.assertEqual(accumulator.step						,     0						)
       _check_local_values([0.0, 0.0]						,     [0.0, 0.0]						)





