


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


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

lowerCAmelCase	=							'\\nMAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.\n\nMAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.\n\nFor details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).\n\nThis metrics is a wrapper around the official implementation of MAUVE:\nhttps://github.com/krishnap25/mauve\n'

lowerCAmelCase	=							'\nCalculates MAUVE scores between two lists of generated text and reference text.\nArgs:\n    predictions: list of generated text to score. Each predictions\n        should be a string with tokens separated by spaces.\n    references: list of reference for each prediction. Each\n        reference should be a string with tokens separated by spaces.\nOptional Args:\n    num_buckets: the size of the histogram to quantize P and Q. Options: \'auto\' (default) or an integer\n    pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1\n    kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9\n    kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5\n    kmeans_max_iter: maximum number of k-means iterations. Default 500\n    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\'].\n    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\n    max_text_length: maximum number of tokens to consider. Default 1024\n    divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25\n    mauve_scaling_factor: "c" from the paper. Default 5.\n    verbose: If True (default), print running time updates\n    seed: random seed to initialize k-means cluster assignments.\nReturns:\n    mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,\n    frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,\n    divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,\n    p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,\n    q_hist: same as above, but with q_text.\nExamples:\n\n    >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest\n    >>> import datasets\n    >>> mauve = datasets.load_metric(\'mauve\')\n    >>> predictions = ["hello there", "general kenobi"]\n    >>> references = ["hello there", "general kenobi"]\n    >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP\n    >>> print(out.mauve) # doctest: +SKIP\n    1.0\n'


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

							def 				lowerCamelCase_			(							self:     Union[str, Any]							)      ->       Any:





									"""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 				lowerCamelCase_			(							self:     int   ,						UpperCamelCase_:     Union[str, Any]   ,						UpperCamelCase_:     Union[str, Any]   ,						UpperCamelCase_:     Tuple=None   ,						UpperCamelCase_:     Dict=None   ,						UpperCamelCase_:     int=None   ,						UpperCamelCase_:     Optional[int]=None   ,						UpperCamelCase_:     Dict="auto"   ,						UpperCamelCase_:     Optional[int]=-1   ,						UpperCamelCase_:     Union[str, Any]=0.9   ,						UpperCamelCase_:     Any=5   ,						UpperCamelCase_:     Optional[Any]=500   ,						UpperCamelCase_:     Any="gpt2-large"   ,						UpperCamelCase_:     List[Any]=-1   ,						UpperCamelCase_:     Dict=1_024   ,						UpperCamelCase_:     str=25   ,						UpperCamelCase_:     int=5   ,						UpperCamelCase_:     int=True   ,						UpperCamelCase_:     Any=25   ,						)      ->       List[Any]:





									"""simple docstring"""







									lowercase__						=				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 gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer

from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


class 	_a							(      UpperCamelCase__					,     UpperCamelCase__					,     UpperCamelCase__					,     unittest.TestCase      ):
							_lowercase     :      int  				=			AltDiffusionPipeline
							_lowercase     :      Tuple  				=			TEXT_TO_IMAGE_PARAMS
							_lowercase     :      List[str]  				=			TEXT_TO_IMAGE_BATCH_PARAMS
							_lowercase     :      Union[str, Any]  				=			TEXT_TO_IMAGE_IMAGE_PARAMS
							_lowercase     :      int  				=			TEXT_TO_IMAGE_IMAGE_PARAMS

							def 				lowerCamelCase_			(							self:     Optional[int]							)      ->       Union[str, Any]:





									"""simple docstring"""







									torch.manual_seed(0							)
									lowercase__						=				UNetaDConditionModel(
									    block_out_channels=(32, 64)   ,						layers_per_block=2   ,						sample_size=32   ,						in_channels=4   ,						out_channels=4   ,						down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')   ,						up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')   ,						cross_attention_dim=32   ,						)
									lowercase__						=				DDIMScheduler(
									    beta_start=0.00085   ,						beta_end=0.012   ,						beta_schedule='''scaled_linear'''   ,						clip_sample=UpperCamelCase_   ,						set_alpha_to_one=UpperCamelCase_   ,						)
									torch.manual_seed(0							)
									lowercase__						=				AutoencoderKL(
									    block_out_channels=[32, 64]   ,						in_channels=3   ,						out_channels=3   ,						down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']   ,						up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']   ,						latent_channels=4   ,						)

									# TODO: address the non-deterministic text encoder (fails for save-load tests)
									# torch.manual_seed(0)
									# text_encoder_config = RobertaSeriesConfig(
									#     hidden_size=32,
									#     project_dim=32,
									#     intermediate_size=37,
									#     layer_norm_eps=1e-05,
									#     num_attention_heads=4,
									#     num_hidden_layers=5,
									#     vocab_size=5002,
									# )
									# text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config)

									torch.manual_seed(0							)
									lowercase__						=				CLIPTextConfig(
									    bos_token_id=0   ,						eos_token_id=2   ,						hidden_size=32   ,						projection_dim=32   ,						intermediate_size=37   ,						layer_norm_eps=1E-0_5   ,						num_attention_heads=4   ,						num_hidden_layers=5   ,						pad_token_id=1   ,						vocab_size=5_002   ,						)
									lowercase__						=				CLIPTextModel(UpperCamelCase_							)

									lowercase__						=				XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''							)
									lowercase__						=				77

									lowercase__						=				{
									    '''unet''': unet,
									    '''scheduler''': scheduler,
									    '''vae''': vae,
									    '''text_encoder''': text_encoder,
									    '''tokenizer''': tokenizer,
									    '''safety_checker''': None,
									    '''feature_extractor''': None,
									}
									return components

							def 				lowerCamelCase_			(							self:     List[str]   ,						UpperCamelCase_:     Optional[int]   ,						UpperCamelCase_:     str=0							)      ->       Union[str, Any]:





									"""simple docstring"""







									if str(UpperCamelCase_							).startswith('''mps'''							):
											lowercase__						=				torch.manual_seed(UpperCamelCase_							)
									else:
											lowercase__						=				torch.Generator(device=UpperCamelCase_							).manual_seed(UpperCamelCase_							)
									lowercase__						=				{
									    '''prompt''': '''A painting of a squirrel eating a burger''',
									    '''generator''': generator,
									    '''num_inference_steps''': 2,
									    '''guidance_scale''': 6.0,
									    '''output_type''': '''numpy''',
									}
									return inputs

							def 				lowerCamelCase_			(							self:     Optional[Any]							)      ->       Dict:





									"""simple docstring"""







									super().test_attention_slicing_forward_pass(expected_max_diff=3E-3							)

							def 				lowerCamelCase_			(							self:     Dict							)      ->       str:





									"""simple docstring"""







									super().test_inference_batch_single_identical(expected_max_diff=3E-3							)

							def 				lowerCamelCase_			(							self:     Optional[int]							)      ->       Any:





									"""simple docstring"""







									lowercase__						=				'''cpu'''  # ensure determinism for the device-dependent torch.Generator

									lowercase__						=				self.get_dummy_components()
									torch.manual_seed(0							)
									lowercase__						=				RobertaSeriesConfig(
									    hidden_size=32   ,						project_dim=32   ,						intermediate_size=37   ,						layer_norm_eps=1E-0_5   ,						num_attention_heads=4   ,						num_hidden_layers=5   ,						vocab_size=5_002   ,						)
									# TODO: remove after fixing the non-deterministic text encoder
									lowercase__						=				RobertaSeriesModelWithTransformation(UpperCamelCase_							)
									lowercase__						=				text_encoder

									lowercase__						=				AltDiffusionPipeline(**UpperCamelCase_							)
									lowercase__						=				alt_pipe.to(UpperCamelCase_							)
									alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

									lowercase__						=				self.get_dummy_inputs(UpperCamelCase_							)
									lowercase__						=				'''A photo of an astronaut'''
									lowercase__						=				alt_pipe(**UpperCamelCase_							)
									lowercase__						=				output.images
									lowercase__						=				image[0, -3:, -3:, -1]

									assert image.shape == (1, 64, 64, 3)
									lowercase__						=				np.array(
									    [0.5748162, 0.60447145, 0.48821217, 0.50100636, 0.5431185, 0.45763683, 0.49657696, 0.48132733, 0.47573093]							)

									assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2


							def 				lowerCamelCase_			(							self:     Optional[Any]							)      ->       Tuple:





									"""simple docstring"""







									lowercase__						=				'''cpu'''  # ensure determinism for the device-dependent torch.Generator

									lowercase__						=				self.get_dummy_components()
									lowercase__						=				PNDMScheduler(skip_prk_steps=UpperCamelCase_							)
									torch.manual_seed(0							)
									lowercase__						=				RobertaSeriesConfig(
									    hidden_size=32   ,						project_dim=32   ,						intermediate_size=37   ,						layer_norm_eps=1E-0_5   ,						num_attention_heads=4   ,						num_hidden_layers=5   ,						vocab_size=5_002   ,						)
									# TODO: remove after fixing the non-deterministic text encoder
									lowercase__						=				RobertaSeriesModelWithTransformation(UpperCamelCase_							)
									lowercase__						=				text_encoder
									lowercase__						=				AltDiffusionPipeline(**UpperCamelCase_							)
									lowercase__						=				alt_pipe.to(UpperCamelCase_							)
									alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

									lowercase__						=				self.get_dummy_inputs(UpperCamelCase_							)
									lowercase__						=				alt_pipe(**UpperCamelCase_							)
									lowercase__						=				output.images
									lowercase__						=				image[0, -3:, -3:, -1]

									assert image.shape == (1, 64, 64, 3)
									lowercase__						=				np.array(
									    [0.51605093, 0.5707241, 0.47365507, 0.50578886, 0.5633877, 0.4642503, 0.5182081, 0.48763484, 0.49084237]							)

									assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2


@slow
@require_torch_gpu
class 	_a							(      unittest.TestCase      ):

							def 				lowerCamelCase_			(							self:     Dict							)      ->       Optional[int]:





									"""simple docstring"""







									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()

							def 				lowerCamelCase_			(							self:     Union[str, Any]							)      ->       Union[str, Any]:





									"""simple docstring"""







									lowercase__						=				AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''   ,						safety_checker=UpperCamelCase_							)
									lowercase__						=				alt_pipe.to(UpperCamelCase_							)
									alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

									lowercase__						=				'''A painting of a squirrel eating a burger'''
									lowercase__						=				torch.manual_seed(0							)
									lowercase__						=				alt_pipe([prompt]   ,						generator=UpperCamelCase_   ,						guidance_scale=6.0   ,						num_inference_steps=20   ,						output_type='''np'''							)

									lowercase__						=				output.images

									lowercase__						=				image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									lowercase__						=				np.array([0.1010, 0.0800, 0.0794, 0.0885, 0.0843, 0.0762, 0.0769, 0.0729, 0.0586]							)

									assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2


							def 				lowerCamelCase_			(							self:     Union[str, Any]							)      ->       Dict:





									"""simple docstring"""







									lowercase__						=				DDIMScheduler.from_pretrained('''BAAI/AltDiffusion'''   ,						subfolder='''scheduler'''							)

									lowercase__						=				AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''   ,						scheduler=UpperCamelCase_   ,						safety_checker=UpperCamelCase_							)
									lowercase__						=				alt_pipe.to(UpperCamelCase_							)
									alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

									lowercase__						=				'''A painting of a squirrel eating a burger'''
									lowercase__						=				torch.manual_seed(0							)

									lowercase__						=				alt_pipe([prompt]   ,						generator=UpperCamelCase_   ,						num_inference_steps=2   ,						output_type='''numpy'''							)
									lowercase__						=				output.images

									lowercase__						=				image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									lowercase__						=				np.array([0.4019, 0.4052, 0.3810, 0.4119, 0.3916, 0.3982, 0.4651, 0.4195, 0.5323]							)

									assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2


