






import faiss  # noqa: F401 # Here to have a nice missing dependency error message early on
import numpy  # noqa: F401 # Here to have a nice missing dependency error message early on
import requests  # noqa: F401 # Here to have a nice missing dependency error message early on
import sklearn  # noqa: F401 # Here to have a nice missing dependency error message early on
import tqdm  # noqa: F401 # Here to have a nice missing dependency error message early on
from mauve import compute_mauve  # From: mauve-text

import datasets


UpperCamelCase			    =     '''\
@inproceedings{pillutla-etal:mauve:neurips2021,
  title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers},
  author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid},
  booktitle = {NeurIPS},
  year      = {2021}
}

'''

UpperCamelCase			    =     '''\
MAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.

MAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.

For details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).

This metrics is a wrapper around the official implementation of MAUVE:
https://github.com/krishnap25/mauve
'''

UpperCamelCase			    =     '''
Calculates MAUVE scores between two lists of generated text and reference text.
Args:
    predictions: list of generated text to score. Each predictions
        should be a string with tokens separated by spaces.
    references: list of reference for each prediction. Each
        reference should be a string with tokens separated by spaces.
Optional Args:
    num_buckets: the size of the histogram to quantize P and Q. Options: \'auto\' (default) or an integer
    pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1
    kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9
    kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5
    kmeans_max_iter: maximum number of k-means iterations. Default 500
    featurize_model_name: name of the model from which features are obtained. Default \'gpt2-large\' Use one of [\'gpt2\', \'gpt2-medium\', \'gpt2-large\', \'gpt2-xl\'].
    device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU
    max_text_length: maximum number of tokens to consider. Default 1024
    divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25
    mauve_scaling_factor: "c" from the paper. Default 5.
    verbose: If True (default), print running time updates
    seed: random seed to initialize k-means cluster assignments.
Returns:
    mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,
    frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,
    divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,
    p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,
    q_hist: same as above, but with q_text.
Examples:

    >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest
    >>> import datasets
    >>> mauve = datasets.load_metric(\'mauve\')
    >>> predictions = ["hello there", "general kenobi"]
    >>> references = ["hello there", "general kenobi"]
    >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP
    >>> print(out.mauve) # doctest: +SKIP
    1.0
'''



@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION	,_KWARGS_DESCRIPTION   )
class 				__UpperCAmelCase		(datasets.Metric   ):







			def 							UpperCamelCase				(     self:  Optional[int]					):







									'''simple docstring'''




									return datasets.MetricInfo(
									    description=_DESCRIPTION				,     citation=_CITATION				,     homepage="""https://github.com/krishnap25/mauve"""				,     inputs_description=_KWARGS_DESCRIPTION				,     features=datasets.Features(
									        {
									            """predictions""": datasets.Value("""string"""				,     id="""sequence"""					),
									            """references""": datasets.Value("""string"""				,     id="""sequence"""					),
									        }					)				,     codebase_urls=["""https://github.com/krishnap25/mauve"""]				,     reference_urls=[
									        """https://arxiv.org/abs/2102.01454""",
									        """https://github.com/krishnap25/mauve""",
									    ]				,     )







			def 							UpperCamelCase				(     self:  Dict				,     UpperCAmelCase_:  Optional[int]				,     UpperCAmelCase_:  Union[str, Any]				,     UpperCAmelCase_:  List[str]=None				,     UpperCAmelCase_:  Optional[int]=None				,     UpperCAmelCase_:  Any=None				,     UpperCAmelCase_:  Any=None				,     UpperCAmelCase_:  Union[str, Any]="auto"				,     UpperCAmelCase_:  List[Any]=-1				,     UpperCAmelCase_:  int=0.9				,     UpperCAmelCase_:  Optional[int]=5				,     UpperCAmelCase_:  Dict=500				,     UpperCAmelCase_:  List[Any]="gpt2-large"				,     UpperCAmelCase_:  Dict=-1				,     UpperCAmelCase_:  List[str]=1_024				,     UpperCAmelCase_:  Optional[int]=25				,     UpperCAmelCase_:  str=5				,     UpperCAmelCase_:  List[str]=True				,     UpperCAmelCase_:  List[Any]=25				,     ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  compute_mauve(
									    p_text=UpperCAmelCase_				,     q_text=UpperCAmelCase_				,     p_features=UpperCAmelCase_				,     q_features=UpperCAmelCase_				,     p_tokens=UpperCAmelCase_				,     q_tokens=UpperCAmelCase_				,     num_buckets=UpperCAmelCase_				,     pca_max_data=UpperCAmelCase_				,     kmeans_explained_var=UpperCAmelCase_				,     kmeans_num_redo=UpperCAmelCase_				,     kmeans_max_iter=UpperCAmelCase_				,     featurize_model_name=UpperCAmelCase_				,     device_id=UpperCAmelCase_				,     max_text_length=UpperCAmelCase_				,     divergence_curve_discretization_size=UpperCAmelCase_				,     mauve_scaling_factor=UpperCAmelCase_				,     verbose=UpperCAmelCase_				,     seed=UpperCAmelCase_				,     )
									return out









import argparse
import os

import torch

from diffusers import (
    CMStochasticIterativeScheduler,
    ConsistencyModelPipeline,
    UNetaDModel,
)


UpperCamelCase			    =     {
    '''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''',
}

UpperCamelCase			    =     {
    '''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''',
}

UpperCamelCase			    =     {
    '''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''',
}

UpperCamelCase			    =     {
    '''num_train_timesteps''': 40,
    '''sigma_min''': 0.002,
    '''sigma_max''': 80.0,
}

UpperCamelCase			    =     {
    '''num_train_timesteps''': 201,
    '''sigma_min''': 0.002,
    '''sigma_max''': 80.0,
}

UpperCamelCase			    =     {
    '''num_train_timesteps''': 151,
    '''sigma_min''': 0.002,
    '''sigma_max''': 80.0,
}

def 	__lowerCamelCase      (		snake_case__		)							->  Optional[Any]:






						"""simple docstring"""





						if isinstance(snake_case__	,snake_case__		):
												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 	__lowerCamelCase      (		snake_case__	,snake_case__	,snake_case__	,snake_case__	,snake_case__=False		)							->  Optional[int]:






						"""simple docstring"""





						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.in_layers.0.weight']
						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.in_layers.0.bias']
						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.in_layers.2.weight']
						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.in_layers.2.bias']
						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.emb_layers.1.weight']
						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.emb_layers.1.bias']
						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.out_layers.0.weight']
						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.out_layers.0.bias']
						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.out_layers.3.weight']
						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.out_layers.3.bias']

						if has_skip:
												_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.skip_connection.weight']
												_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.skip_connection.bias']

						return new_checkpoint

def 	__lowerCamelCase      (		snake_case__	,snake_case__	,snake_case__	,snake_case__	,snake_case__=None		)							->  Optional[int]:






						"""simple docstring"""





						_SCREAMING_SNAKE_CASE				,					_SCREAMING_SNAKE_CASE				,					_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.qkv.weight'].chunk(3	,dim=0		)
						_SCREAMING_SNAKE_CASE				,					_SCREAMING_SNAKE_CASE				,					_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.qkv.bias'].chunk(3	,dim=0		)

						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.norm.weight']
						_SCREAMING_SNAKE_CASE     				=  checkpoint[F'{old_prefix}.norm.bias']

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

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

						return new_checkpoint

def 	__lowerCamelCase      (		snake_case__	,snake_case__		)							->  int:






						"""simple docstring"""





						_SCREAMING_SNAKE_CASE     				=  torch.load(snake_case__	,map_location="""cpu"""		)
						_SCREAMING_SNAKE_CASE     				=  {}

						_SCREAMING_SNAKE_CASE     				=  checkpoint["""time_embed.0.weight"""]
						_SCREAMING_SNAKE_CASE     				=  checkpoint["""time_embed.0.bias"""]
						_SCREAMING_SNAKE_CASE     				=  checkpoint["""time_embed.2.weight"""]
						_SCREAMING_SNAKE_CASE     				=  checkpoint["""time_embed.2.bias"""]

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

						_SCREAMING_SNAKE_CASE     				=  checkpoint["""input_blocks.0.0.weight"""]
						_SCREAMING_SNAKE_CASE     				=  checkpoint["""input_blocks.0.0.bias"""]

						_SCREAMING_SNAKE_CASE     				=  unet_config["""down_block_types"""]
						_SCREAMING_SNAKE_CASE     				=  unet_config["""layers_per_block"""]
						_SCREAMING_SNAKE_CASE     				=  unet_config["""attention_head_dim"""]
						_SCREAMING_SNAKE_CASE     				=  unet_config["""block_out_channels"""]
						_SCREAMING_SNAKE_CASE     				=  1
						_SCREAMING_SNAKE_CASE     				=  channels_list[0]

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

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

												if i != len(snake_case__		) - 1:
																		_SCREAMING_SNAKE_CASE     				=  F'down_blocks.{i}.downsamplers.0'
																		_SCREAMING_SNAKE_CASE     				=  F'input_blocks.{current_layer}.0'
																		_SCREAMING_SNAKE_CASE     				=  convert_resnet(snake_case__	,snake_case__	,snake_case__	,snake_case__		)
																		current_layer += 1

												_SCREAMING_SNAKE_CASE     				=  current_channels

						# hardcoded the mid-block for now
						_SCREAMING_SNAKE_CASE     				=  """mid_block.resnets.0"""
						_SCREAMING_SNAKE_CASE     				=  """middle_block.0"""
						_SCREAMING_SNAKE_CASE     				=  convert_resnet(snake_case__	,snake_case__	,snake_case__	,snake_case__		)
						_SCREAMING_SNAKE_CASE     				=  """mid_block.attentions.0"""
						_SCREAMING_SNAKE_CASE     				=  """middle_block.1"""
						_SCREAMING_SNAKE_CASE     				=  convert_attention(snake_case__	,snake_case__	,snake_case__	,snake_case__	,snake_case__		)
						_SCREAMING_SNAKE_CASE     				=  """mid_block.resnets.1"""
						_SCREAMING_SNAKE_CASE     				=  """middle_block.2"""
						_SCREAMING_SNAKE_CASE     				=  convert_resnet(snake_case__	,snake_case__	,snake_case__	,snake_case__		)

						_SCREAMING_SNAKE_CASE     				=  0
						_SCREAMING_SNAKE_CASE     				=  unet_config["""up_block_types"""]

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

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

																		if i != len(snake_case__		) - 1:
																								_SCREAMING_SNAKE_CASE     				=  F'up_blocks.{i}.upsamplers.0'
																								_SCREAMING_SNAKE_CASE     				=  F'output_blocks.{current_layer-1}.2'
																								_SCREAMING_SNAKE_CASE     				=  convert_resnet(snake_case__	,snake_case__	,snake_case__	,snake_case__		)

						_SCREAMING_SNAKE_CASE     				=  checkpoint["""out.0.weight"""]
						_SCREAMING_SNAKE_CASE     				=  checkpoint["""out.0.bias"""]
						_SCREAMING_SNAKE_CASE     				=  checkpoint["""out.2.weight"""]
						_SCREAMING_SNAKE_CASE     				=  checkpoint["""out.2.bias"""]

						return new_checkpoint


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

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

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

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

		if not args.class_cond:
				UpperCamelCase			    =     None

		UpperCamelCase			    =     con_pt_to_diffuser(args.unet_path, unet_config)

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

		# Get scheduler config
		if "cd" in ckpt_name or "test" in ckpt_name:
				UpperCamelCase			    =     CD_SCHEDULER_CONFIG
		elif "ct" in ckpt_name and "imagenet64" in ckpt_name:
				UpperCamelCase			    =     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)):
				UpperCamelCase			    =     CT_LSUN_256_SCHEDULER_CONFIG
		else:
				raise ValueError(f"Checkpoint type {ckpt_name} is not currently supported.")

		UpperCamelCase			    =     CMStochasticIterativeScheduler(**scheduler_config)

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

