



import argparse
import os

import torch

from diffusers import (
    CMStochasticIterativeScheduler,
    ConsistencyModelPipeline,
    UNetaDModel,
)


a_						=					{
    """sample_size""": 32,
    """in_channels""": 3,
    """out_channels""": 3,
    """layers_per_block""": 2,
    """num_class_embeds""": 1_000,
    """block_out_channels""": [32, 64],
    """attention_head_dim""": 8,
    """down_block_types""": [
        """ResnetDownsampleBlock2D""",
        """AttnDownBlock2D""",
    ],
    """up_block_types""": [
        """AttnUpBlock2D""",
        """ResnetUpsampleBlock2D""",
    ],
    """resnet_time_scale_shift""": """scale_shift""",
    """upsample_type""": """resnet""",
    """downsample_type""": """resnet""",
}

a_						=					{
    """sample_size""": 64,
    """in_channels""": 3,
    """out_channels""": 3,
    """layers_per_block""": 3,
    """num_class_embeds""": 1_000,
    """block_out_channels""": [192, 192 * 2, 192 * 3, 192 * 4],
    """attention_head_dim""": 64,
    """down_block_types""": [
        """ResnetDownsampleBlock2D""",
        """AttnDownBlock2D""",
        """AttnDownBlock2D""",
        """AttnDownBlock2D""",
    ],
    """up_block_types""": [
        """AttnUpBlock2D""",
        """AttnUpBlock2D""",
        """AttnUpBlock2D""",
        """ResnetUpsampleBlock2D""",
    ],
    """resnet_time_scale_shift""": """scale_shift""",
    """upsample_type""": """resnet""",
    """downsample_type""": """resnet""",
}

a_						=					{
    """sample_size""": 256,
    """in_channels""": 3,
    """out_channels""": 3,
    """layers_per_block""": 2,
    """num_class_embeds""": None,
    """block_out_channels""": [256, 256, 256 * 2, 256 * 2, 256 * 4, 256 * 4],
    """attention_head_dim""": 64,
    """down_block_types""": [
        """ResnetDownsampleBlock2D""",
        """ResnetDownsampleBlock2D""",
        """ResnetDownsampleBlock2D""",
        """AttnDownBlock2D""",
        """AttnDownBlock2D""",
        """AttnDownBlock2D""",
    ],
    """up_block_types""": [
        """AttnUpBlock2D""",
        """AttnUpBlock2D""",
        """AttnUpBlock2D""",
        """ResnetUpsampleBlock2D""",
        """ResnetUpsampleBlock2D""",
        """ResnetUpsampleBlock2D""",
    ],
    """resnet_time_scale_shift""": """default""",
    """upsample_type""": """resnet""",
    """downsample_type""": """resnet""",
}

a_						=					{
    """num_train_timesteps""": 40,
    """sigma_min""": 0.0_02,
    """sigma_max""": 80.0,
}

a_						=					{
    """num_train_timesteps""": 201,
    """sigma_min""": 0.0_02,
    """sigma_max""": 80.0,
}

a_						=					{
    """num_train_timesteps""": 151,
    """sigma_min""": 0.0_02,
    """sigma_max""": 80.0,
}





def 		a__			(		_UpperCamelCase			: List[str]							):
   if isinstance(_UpperCamelCase ,_UpperCamelCase							):
      return v
   if v.lower() in ("yes", "true", "t", "y", "1"):
      return True
   elif v.lower() in ("no", "false", "f", "n", "0"):
      return False
   else:
      raise argparse.ArgumentTypeError('''boolean value expected'''							)





def 		a__			(		_UpperCamelCase			: int ,_UpperCamelCase			: Optional[int] ,_UpperCamelCase			: Any ,_UpperCamelCase			: str ,_UpperCamelCase			: List[Any]=False							):
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.in_layers.0.weight"""]
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.in_layers.0.bias"""]
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.in_layers.2.weight"""]
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.in_layers.2.bias"""]
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.emb_layers.1.weight"""]
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.emb_layers.1.bias"""]
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.out_layers.0.weight"""]
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.out_layers.0.bias"""]
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.out_layers.3.weight"""]
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.out_layers.3.bias"""]

   if has_skip:
      __lowerCamelCase													=    checkpoint[F"""{old_prefix}.skip_connection.weight"""]
      __lowerCamelCase													=    checkpoint[F"""{old_prefix}.skip_connection.bias"""]

   return new_checkpoint





def 		a__			(		_UpperCamelCase			: int ,_UpperCamelCase			: Dict ,_UpperCamelCase			: str ,_UpperCamelCase			: Dict ,_UpperCamelCase			: Tuple=None							):
   __lowerCamelCase							,__lowerCamelCase							,__lowerCamelCase													=    checkpoint[F"""{old_prefix}.qkv.weight"""].chunk(3 ,dim=0							)
   __lowerCamelCase							,__lowerCamelCase							,__lowerCamelCase													=    checkpoint[F"""{old_prefix}.qkv.bias"""].chunk(3 ,dim=0							)

   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.norm.weight"""]
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.norm.bias"""]

   __lowerCamelCase													=    weight_q.squeeze(-1							).squeeze(-1							)
   __lowerCamelCase													=    bias_q.squeeze(-1							).squeeze(-1							)
   __lowerCamelCase													=    weight_k.squeeze(-1							).squeeze(-1							)
   __lowerCamelCase													=    bias_k.squeeze(-1							).squeeze(-1							)
   __lowerCamelCase													=    weight_v.squeeze(-1							).squeeze(-1							)
   __lowerCamelCase													=    bias_v.squeeze(-1							).squeeze(-1							)

   __lowerCamelCase													=    (
       checkpoint[F"""{old_prefix}.proj_out.weight"""].squeeze(-1							).squeeze(-1							)
   )
   __lowerCamelCase													=    checkpoint[F"""{old_prefix}.proj_out.bias"""].squeeze(-1							).squeeze(-1							)

   return new_checkpoint





def 		a__			(		_UpperCamelCase			: str ,_UpperCamelCase			: Union[str, Any]							):
   __lowerCamelCase													=    torch.load(_UpperCamelCase ,map_location='''cpu'''							)
   __lowerCamelCase													=    {}

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

   if unet_config["num_class_embeds"] is not None:
      __lowerCamelCase													=    checkpoint['''label_emb.weight''']

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

   __lowerCamelCase													=    unet_config['''down_block_types''']
   __lowerCamelCase													=    unet_config['''layers_per_block''']
   __lowerCamelCase													=    unet_config['''attention_head_dim''']
   __lowerCamelCase													=    unet_config['''block_out_channels''']
   __lowerCamelCase													=    1
   __lowerCamelCase													=    channels_list[0]

   for i, layer_type in enumerate(_UpperCamelCase							):
      __lowerCamelCase													=    channels_list[i]
      __lowerCamelCase													=    current_channels != prev_channels
      if layer_type == "ResnetDownsampleBlock2D":
         for j in range(_UpperCamelCase							):
            __lowerCamelCase													=    F"""down_blocks.{i}.resnets.{j}"""
            __lowerCamelCase													=    F"""input_blocks.{current_layer}.0"""
            __lowerCamelCase													=    True if j == 0 and downsample_block_has_skip else False
            __lowerCamelCase													=    convert_resnet(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,has_skip=_UpperCamelCase							)
            current_layer += 1

      elif layer_type == "AttnDownBlock2D":
         for j in range(_UpperCamelCase							):
            __lowerCamelCase													=    F"""down_blocks.{i}.resnets.{j}"""
            __lowerCamelCase													=    F"""input_blocks.{current_layer}.0"""
            __lowerCamelCase													=    True if j == 0 and downsample_block_has_skip else False
            __lowerCamelCase													=    convert_resnet(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,has_skip=_UpperCamelCase							)
            __lowerCamelCase													=    F"""down_blocks.{i}.attentions.{j}"""
            __lowerCamelCase													=    F"""input_blocks.{current_layer}.1"""
            __lowerCamelCase													=    convert_attention(
                _UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase							)
            current_layer += 1

      if i != len(_UpperCamelCase							) - 1:
         __lowerCamelCase													=    F"""down_blocks.{i}.downsamplers.0"""
         __lowerCamelCase													=    F"""input_blocks.{current_layer}.0"""
         __lowerCamelCase													=    convert_resnet(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase							)
         current_layer += 1

      __lowerCamelCase													=    current_channels

   # hardcoded the mid-block for now
   __lowerCamelCase													=    '''mid_block.resnets.0'''
   __lowerCamelCase													=    '''middle_block.0'''
   __lowerCamelCase													=    convert_resnet(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase							)
   __lowerCamelCase													=    '''mid_block.attentions.0'''
   __lowerCamelCase													=    '''middle_block.1'''
   __lowerCamelCase													=    convert_attention(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase							)
   __lowerCamelCase													=    '''mid_block.resnets.1'''
   __lowerCamelCase													=    '''middle_block.2'''
   __lowerCamelCase													=    convert_resnet(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase							)

   __lowerCamelCase													=    0
   __lowerCamelCase													=    unet_config['''up_block_types''']

   for i, layer_type in enumerate(_UpperCamelCase							):
      if layer_type == "ResnetUpsampleBlock2D":
         for j in range(layers_per_block + 1							):
            __lowerCamelCase													=    F"""up_blocks.{i}.resnets.{j}"""
            __lowerCamelCase													=    F"""output_blocks.{current_layer}.0"""
            __lowerCamelCase													=    convert_resnet(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,has_skip=_UpperCamelCase							)
            current_layer += 1

         if i != len(_UpperCamelCase							) - 1:
            __lowerCamelCase													=    F"""up_blocks.{i}.upsamplers.0"""
            __lowerCamelCase													=    F"""output_blocks.{current_layer-1}.1"""
            __lowerCamelCase													=    convert_resnet(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase							)
      elif layer_type == "AttnUpBlock2D":
         for j in range(layers_per_block + 1							):
            __lowerCamelCase													=    F"""up_blocks.{i}.resnets.{j}"""
            __lowerCamelCase													=    F"""output_blocks.{current_layer}.0"""
            __lowerCamelCase													=    convert_resnet(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,has_skip=_UpperCamelCase							)
            __lowerCamelCase													=    F"""up_blocks.{i}.attentions.{j}"""
            __lowerCamelCase													=    F"""output_blocks.{current_layer}.1"""
            __lowerCamelCase													=    convert_attention(
                _UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase							)
            current_layer += 1

         if i != len(_UpperCamelCase							) - 1:
            __lowerCamelCase													=    F"""up_blocks.{i}.upsamplers.0"""
            __lowerCamelCase													=    F"""output_blocks.{current_layer-1}.2"""
            __lowerCamelCase													=    convert_resnet(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase							)

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

   return new_checkpoint


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

  parser.add_argument("""--unet_path""", default=None, type=str, required=True, help="""Path to the unet.pt to convert.""")
  parser.add_argument(
      """--dump_path""", default=None, type=str, required=True, help="""Path to output the converted UNet model."""
  )
  parser.add_argument("""--class_cond""", default=True, type=str, help="""Whether the model is class-conditional.""")

  a_						=					parser.parse_args()
  a_						=					strabool(args.class_cond)

  a_						=					os.path.basename(args.unet_path)
  print(f"Checkpoint: {ckpt_name}")

  # Get U-Net config
  if "imagenet64" in ckpt_name:
    a_						=					IMAGENET_64_UNET_CONFIG
  elif "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)):
    a_						=					LSUN_256_UNET_CONFIG
  elif "test" in ckpt_name:
    a_						=					TEST_UNET_CONFIG
  else:
    raise ValueError(f"Checkpoint type {ckpt_name} is not currently supported.")

  if not args.class_cond:
    a_						=					None

  a_						=					con_pt_to_diffuser(args.unet_path, unet_config)

  a_						=					UNetaDModel(**unet_config)
  image_unet.load_state_dict(converted_unet_ckpt)

  # Get scheduler config
  if "cd" in ckpt_name or "test" in ckpt_name:
    a_						=					CD_SCHEDULER_CONFIG
  elif "ct" in ckpt_name and "imagenet64" in ckpt_name:
    a_						=					CT_IMAGENET_64_SCHEDULER_CONFIG
  elif "ct" in ckpt_name and "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)):
    a_						=					CT_LSUN_256_SCHEDULER_CONFIG
  else:
    raise ValueError(f"Checkpoint type {ckpt_name} is not currently supported.")

  a_						=					CMStochasticIterativeScheduler(**scheduler_config)

  a_						=					ConsistencyModelPipeline(unet=image_unet, scheduler=cm_scheduler)
  consistency_model.save_pretrained(args.dump_path)











import inspect
import re

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
a_			=					"""src/transformers"""


# This is to make sure the transformers module imported is the one in the repo.
a_			=					direct_transformers_import(PATH_TO_TRANSFORMERS)

a_			=					transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
a_			=					re.compile(R"""\[(.+?)\]\((https://huggingface\.co/.+?)\)""")


a_			=					{
    """DecisionTransformerConfig""",
    """EncoderDecoderConfig""",
    """MusicgenConfig""",
    """RagConfig""",
    """SpeechEncoderDecoderConfig""",
    """TimmBackboneConfig""",
    """VisionEncoderDecoderConfig""",
    """VisionTextDualEncoderConfig""",
    """LlamaConfig""",
}





def 		a__			(		_UpperCamelCase			: Union[str, Any]							):
   __lowerCamelCase													=    None

   # source code of `config_class`
   __lowerCamelCase													=    inspect.getsource(_UpperCamelCase							)
   __lowerCamelCase													=    _re_checkpoint.findall(_UpperCamelCase							)

   # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
   # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
   for ckpt_name, ckpt_link in checkpoints:
      # allow the link to end with `/`
      if ckpt_link.endswith('''/'''							):
         __lowerCamelCase													=    ckpt_link[:-1]

      # verify the checkpoint name corresponds to the checkpoint link
      __lowerCamelCase													=    F"""https://huggingface.co/{ckpt_name}"""
      if ckpt_link == ckpt_link_from_name:
         __lowerCamelCase													=    ckpt_name
         break

   return checkpoint





def 		a__			(		):
   __lowerCamelCase													=    []

   for config_class in list(CONFIG_MAPPING.values()							):
      # Skip deprecated models
      if "models.deprecated" in config_class.__module__:
         continue
      __lowerCamelCase													=    get_checkpoint_from_config_class(_UpperCamelCase							)

      __lowerCamelCase													=    config_class.__name__
      if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
         configs_without_checkpoint.append(_UpperCamelCase							)

   if len(_UpperCamelCase							) > 0:
      __lowerCamelCase													=    '''\n'''.join(sorted(_UpperCamelCase							)							)
      raise ValueError(F"""The following configurations don't contain any valid checkpoint:\n{message}"""							)


if __name__ == "__main__":
  check_config_docstrings_have_checkpoints()






