
"""simple docstring"""






import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()




class 			_SCREAMING_SNAKE_CASE					(			unittest.TestCase							):






			def  __lowerCAmelCase (					self       )			->     str:
						# clean up the VRAM after each test
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()






			@property
			def  __lowerCAmelCase (					self       )			->     Union[str, Any]:
						lowerCAmelCase_    :Union[str, Any]			     =  1
						lowerCAmelCase_    :Any			     =  3
						lowerCAmelCase_    :Tuple			     =  (32, 32)

						lowerCAmelCase_    :Dict			     =  floats_tensor((batch_size, num_channels) + sizes       ,   rng=random.Random(0       )       ).to(__A       )
						return image






			@property
			def  __lowerCAmelCase (					self       )			->     Any:
						torch.manual_seed(0       )
						lowerCAmelCase_    :Dict			     =  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       ,   )
						return model






			@property
			def  __lowerCAmelCase (					self       )			->     List[Any]:
						torch.manual_seed(0       )
						lowerCAmelCase_    :Optional[Any]			     =  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       ,   )
						return model






			@property
			def  __lowerCAmelCase (					self       )			->     Tuple:
						torch.manual_seed(0       )
						lowerCAmelCase_    :Optional[Any]			     =  RobertaSeriesConfig(
						    hidden_size=32       ,   project_dim=32       ,   intermediate_size=37       ,   layer_norm_eps=1E-05       ,   num_attention_heads=4       ,   num_hidden_layers=5       ,   pad_token_id=1       ,   vocab_size=5006       ,   )
						return RobertaSeriesModelWithTransformation(__A       )






			@property
			def  __lowerCAmelCase (					self       )			->     int:
						def extract(*__A       ,   **__A       ):




									class 			_SCREAMING_SNAKE_CASE					:






												def __init__(					self       )			->     str:
															lowerCAmelCase_    :List[str]			     =  torch.ones([0]       )






												def  __lowerCAmelCase (					self       ,   __A       )			->     int:
															self.pixel_values.to(__A       )
															return self




									return Out()

						return extract






			def  __lowerCAmelCase (					self       )			->     str:
						lowerCAmelCase_    :List[str]			     =  """cpu"""  # ensure determinism for the device-dependent torch.Generator
						lowerCAmelCase_    :Dict			     =  self.dummy_cond_unet
						lowerCAmelCase_    :List[Any]			     =  PNDMScheduler(skip_prk_steps=__A       )
						lowerCAmelCase_    :int			     =  self.dummy_vae
						lowerCAmelCase_    :Union[str, Any]			     =  self.dummy_text_encoder
						lowerCAmelCase_    :Optional[int]			     =  XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta"""       )
						lowerCAmelCase_    :Dict			     =  77

						lowerCAmelCase_    :Tuple			     =  self.dummy_image.to(__A       )
						lowerCAmelCase_    :Any			     =  init_image / 2 + 0.5

						# make sure here that pndm scheduler skips prk
						lowerCAmelCase_    :Dict			     =  AltDiffusionImgaImgPipeline(
						    unet=__A       ,   scheduler=__A       ,   vae=__A       ,   text_encoder=__A       ,   tokenizer=__A       ,   safety_checker=__A       ,   feature_extractor=self.dummy_extractor       ,   )
						lowerCAmelCase_    :Dict			     =  VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor       ,   do_normalize=__A       )
						lowerCAmelCase_    :Any			     =  alt_pipe.to(__A       )
						alt_pipe.set_progress_bar_config(disable=__A       )

						lowerCAmelCase_    :Optional[Any]			     =  """A painting of a squirrel eating a burger"""
						lowerCAmelCase_    :Optional[Any]			     =  torch.Generator(device=__A       ).manual_seed(0       )
						lowerCAmelCase_    :List[Any]			     =  alt_pipe(
						    [prompt]       ,   generator=__A       ,   guidance_scale=6.0       ,   num_inference_steps=2       ,   output_type="""np"""       ,   image=__A       ,   )

						lowerCAmelCase_    :Tuple			     =  output.images

						lowerCAmelCase_    :str			     =  torch.Generator(device=__A       ).manual_seed(0       )
						lowerCAmelCase_    :str			     =  alt_pipe(
						    [prompt]       ,   generator=__A       ,   guidance_scale=6.0       ,   num_inference_steps=2       ,   output_type="""np"""       ,   image=__A       ,   return_dict=__A       ,   )[0]

						lowerCAmelCase_    :Tuple			     =  image[0, -3:, -3:, -1]
						lowerCAmelCase_    :List[str]			     =  image_from_tuple[0, -3:, -3:, -1]

						assert image.shape == (1, 32, 32, 3)
						lowerCAmelCase_    :str			     =  np.array([0.4_4_2_7, 0.3_7_3_1, 0.4_2_4_9, 0.4_9_4_1, 0.4_5_4_6, 0.4_1_4_8, 0.4_1_9_3, 0.4_6_6_6, 0.4_4_9_9]       )

						assert np.abs(image_slice.flatten() - expected_slice       ).max() < 5E-3
						assert np.abs(image_from_tuple_slice.flatten() - expected_slice       ).max() < 5E-3






			@unittest.skipIf(torch_device != """cuda"""       ,   """This test requires a GPU"""       )
			def  __lowerCAmelCase (					self       )			->     Optional[int]:
						lowerCAmelCase_    :Tuple			     =  self.dummy_cond_unet
						lowerCAmelCase_    :int			     =  PNDMScheduler(skip_prk_steps=__A       )
						lowerCAmelCase_    :int			     =  self.dummy_vae
						lowerCAmelCase_    :Dict			     =  self.dummy_text_encoder
						lowerCAmelCase_    :List[Any]			     =  XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta"""       )
						lowerCAmelCase_    :Optional[Any]			     =  77

						lowerCAmelCase_    :Optional[int]			     =  self.dummy_image.to(__A       )

						# put models in fp16
						lowerCAmelCase_    :Any			     =  unet.half()
						lowerCAmelCase_    :Union[str, Any]			     =  vae.half()
						lowerCAmelCase_    :Optional[Any]			     =  bert.half()

						# make sure here that pndm scheduler skips prk
						lowerCAmelCase_    :Tuple			     =  AltDiffusionImgaImgPipeline(
						    unet=__A       ,   scheduler=__A       ,   vae=__A       ,   text_encoder=__A       ,   tokenizer=__A       ,   safety_checker=__A       ,   feature_extractor=self.dummy_extractor       ,   )
						lowerCAmelCase_    :List[str]			     =  VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor       ,   do_normalize=__A       )
						lowerCAmelCase_    :Union[str, Any]			     =  alt_pipe.to(__A       )
						alt_pipe.set_progress_bar_config(disable=__A       )

						lowerCAmelCase_    :int			     =  """A painting of a squirrel eating a burger"""
						lowerCAmelCase_    :Dict			     =  torch.manual_seed(0       )
						lowerCAmelCase_    :Union[str, Any]			     =  alt_pipe(
						    [prompt]       ,   generator=__A       ,   num_inference_steps=2       ,   output_type="""np"""       ,   image=__A       ,   ).images

						assert image.shape == (1, 32, 32, 3)






			@unittest.skipIf(torch_device != """cuda"""       ,   """This test requires a GPU"""       )
			def  __lowerCAmelCase (					self       )			->     Optional[int]:
						lowerCAmelCase_    :List[str]			     =  load_image(
						    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
						    """/img2img/sketch-mountains-input.jpg"""       )
						# resize to resolution that is divisible by 8 but not 16 or 32
						lowerCAmelCase_    :Tuple			     =  init_image.resize((760, 504)       )

						lowerCAmelCase_    :str			     =  """BAAI/AltDiffusion"""
						lowerCAmelCase_    :str			     =  AltDiffusionImgaImgPipeline.from_pretrained(
						    __A       ,   safety_checker=__A       ,   )
						pipe.to(__A       )
						pipe.set_progress_bar_config(disable=__A       )
						pipe.enable_attention_slicing()

						lowerCAmelCase_    :Any			     =  """A fantasy landscape, trending on artstation"""

						lowerCAmelCase_    :Dict			     =  torch.manual_seed(0       )
						lowerCAmelCase_    :str			     =  pipe(
						    prompt=__A       ,   image=__A       ,   strength=0.7_5       ,   guidance_scale=7.5       ,   generator=__A       ,   output_type="""np"""       ,   )
						lowerCAmelCase_    :Dict			     =  output.images[0]

						lowerCAmelCase_    :List[str]			     =  image[255:258, 383:386, -1]

						assert image.shape == (504, 760, 3)
						lowerCAmelCase_    :int			     =  np.array([0.9_3_5_8, 0.9_3_9_7, 0.9_5_9_9, 0.9_9_0_1, 1.0_0_0_0, 1.0_0_0_0, 0.9_8_8_2, 1.0_0_0_0, 1.0_0_0_0]       )

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




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






			def  __lowerCAmelCase (					self       )			->     Tuple:
						# clean up the VRAM after each test
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()






			def  __lowerCAmelCase (					self       )			->     Any:
						lowerCAmelCase_    :Union[str, Any]			     =  load_image(
						    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
						    """/img2img/sketch-mountains-input.jpg"""       )
						lowerCAmelCase_    :Union[str, Any]			     =  init_image.resize((768, 512)       )
						lowerCAmelCase_    :str			     =  load_numpy(
						    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy"""       )

						lowerCAmelCase_    :Union[str, Any]			     =  """BAAI/AltDiffusion"""
						lowerCAmelCase_    :Any			     =  AltDiffusionImgaImgPipeline.from_pretrained(
						    __A       ,   safety_checker=__A       ,   )
						pipe.to(__A       )
						pipe.set_progress_bar_config(disable=__A       )
						pipe.enable_attention_slicing()

						lowerCAmelCase_    :Tuple			     =  """A fantasy landscape, trending on artstation"""

						lowerCAmelCase_    :int			     =  torch.manual_seed(0       )
						lowerCAmelCase_    :Tuple			     =  pipe(
						    prompt=__A       ,   image=__A       ,   strength=0.7_5       ,   guidance_scale=7.5       ,   generator=__A       ,   output_type="""np"""       ,   )
						lowerCAmelCase_    :Optional[int]			     =  output.images[0]

						assert image.shape == (512, 768, 3)
						# img2img is flaky across GPUs even in fp32, so using MAE here
						assert np.abs(expected_image - image       ).max() < 1E-2














from sklearn.metrics import fa_score, matthews_corrcoef

import datasets

from .record_evaluation import evaluate as evaluate_record


UpperCAmelCase_ :						int      =      '''\
@article{wang2019superglue,
  title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems},
  author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R},
  journal={arXiv preprint arXiv:1905.00537},
  year={2019}
}
'''

UpperCAmelCase_ :						Optional[Any]      =      '''\
SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after
GLUE with a new set of more difficult language understanding tasks, improved
resources, and a new public leaderboard.
'''

UpperCAmelCase_ :						int      =      '''
Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset.
Args:
    predictions: list of predictions to score. Depending on the SuperGlUE subset:
        - for \'record\': list of question-answer dictionaries with the following keys:
            - \'idx\': index of the question as specified by the dataset
            - \'prediction_text\': the predicted answer text
        - for \'multirc\': list of question-answer dictionaries with the following keys:
            - \'idx\': index of the question-answer pair as specified by the dataset
            - \'prediction\': the predicted answer label
        - otherwise: list of predicted labels
    references: list of reference labels. Depending on the SuperGLUE subset:
        - for \'record\': list of question-answers dictionaries with the following keys:
            - \'idx\': index of the question as specified by the dataset
            - \'answers\': list of possible answers
        - otherwise: list of reference labels
Returns: depending on the SuperGLUE subset:
    - for \'record\':
        - \'exact_match\': Exact match between answer and gold answer
        - \'f1\': F1 score
    - for \'multirc\':
        - \'exact_match\': Exact match between answer and gold answer
        - \'f1_m\': Per-question macro-F1 score
        - \'f1_a\': Average F1 score over all answers
    - for \'axb\':
        \'matthews_correlation\': Matthew Correlation
    - for \'cb\':
        - \'accuracy\': Accuracy
        - \'f1\': F1 score
    - for all others:
        - \'accuracy\': Accuracy
Examples:

    >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'copa\')  # any of ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]
    >>> predictions = [0, 1]
    >>> references = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'accuracy\': 1.0}

    >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'cb\')
    >>> predictions = [0, 1]
    >>> references = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'accuracy\': 1.0, \'f1\': 1.0}

    >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'record\')
    >>> predictions = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'prediction_text\': \'answer\'}]
    >>> references = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'answers\': [\'answer\', \'another_answer\']}]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'exact_match\': 1.0, \'f1\': 1.0}

    >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'multirc\')
    >>> predictions = [{\'idx\': {\'answer\': 0, \'paragraph\': 0, \'question\': 0}, \'prediction\': 0}, {\'idx\': {\'answer\': 1, \'paragraph\': 2, \'question\': 3}, \'prediction\': 1}]
    >>> references = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'exact_match\': 1.0, \'f1_m\': 1.0, \'f1_a\': 1.0}

    >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'axb\')
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'matthews_correlation\': 1.0}
'''

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  Optional[Any]	,			__magic_name__					:  Optional[Any]					)		->			Union[str, Any]:






	"""simple docstring"""



	return float((preds == labels).mean()					)

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  Dict	,			__magic_name__					:  int	,			__magic_name__					:  Any="binary"					)		->			Dict:






	"""simple docstring"""



	UpperCamelCase     :List[str]          =	simple_accuracy(__magic_name__	,			__magic_name__					)
	UpperCamelCase     :Dict          =	float(fa_score(y_true=__magic_name__	,			y_pred=__magic_name__	,			average=__magic_name__					)					)
	return {
	    "accuracy": acc,
	    "f1": fa,
	}

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  List[Any]	,			__magic_name__					:  Optional[Any]					)		->			Optional[Any]:






	"""simple docstring"""



	UpperCamelCase     :Optional[Any]          =	{}
	for id_pred, label in zip(__magic_name__	,			__magic_name__					):
		UpperCamelCase     :str          =	f"""{id_pred['idx']['paragraph']}-{id_pred['idx']['question']}"""
		UpperCamelCase     :Union[str, Any]          =	id_pred["""prediction"""]
		if question_id in question_map:
			question_map[question_id].append((pred, label)					)
		else:
			UpperCamelCase     :Dict          =	[(pred, label)]
	UpperCamelCase     ,      UpperCamelCase     :Optional[int]          =	[], []
	for question, preds_labels in question_map.items():
		UpperCamelCase     ,      UpperCamelCase     :Optional[Any]          =	zip(*__magic_name__					)
		UpperCamelCase     :Optional[int]          =	fa_score(y_true=__magic_name__	,			y_pred=__magic_name__	,			average="""macro"""					)
		fas.append(__magic_name__					)
		UpperCamelCase     :int          =	int(sum(pred == label for pred, label in preds_labels					) == len(__magic_name__					)					)
		ems.append(__magic_name__					)
	UpperCamelCase     :Optional[int]          =	float(sum(__magic_name__					) / len(__magic_name__					)					)
	UpperCamelCase     :str          =	sum(__magic_name__					) / len(__magic_name__					)
	UpperCamelCase     :Tuple          =	float(fa_score(y_true=__magic_name__	,			y_pred=[id_pred["""prediction"""] for id_pred in ids_preds]					)					)
	return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a}

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





	def 						_A (		self				:					str	):
		if self.config_name not in [
		    "boolq",
		    "cb",
		    "copa",
		    "multirc",
		    "record",
		    "rte",
		    "wic",
		    "wsc",
		    "wsc.fixed",
		    "axb",
		    "axg",
		]:
			raise KeyError(
			    """You should supply a configuration name selected in """
			    """[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]"""	)
		return datasets.MetricInfo(
		    description=_DESCRIPTION ,  citation=_CITATION ,  inputs_description=_KWARGS_DESCRIPTION ,  features=datasets.Features(self._get_feature_types()	) ,  codebase_urls=[] ,  reference_urls=[] ,  format="""numpy""" if not self.config_name == """record""" and not self.config_name == """multirc""" else None ,  )





	def 						_A (		self				:					Optional[Any]	):
		if self.config_name == "record":
			return {
			    "predictions": {
			        "idx": {
			            "passage": datasets.Value("""int64"""	),
			            "query": datasets.Value("""int64"""	),
			        },
			        "prediction_text": datasets.Value("""string"""	),
			    },
			    "references": {
			        "idx": {
			            "passage": datasets.Value("""int64"""	),
			            "query": datasets.Value("""int64"""	),
			        },
			        "answers": datasets.Sequence(datasets.Value("""string"""	)	),
			    },
			}
		elif self.config_name == "multirc":
			return {
			    "predictions": {
			        "idx": {
			            "answer": datasets.Value("""int64"""	),
			            "paragraph": datasets.Value("""int64"""	),
			            "question": datasets.Value("""int64"""	),
			        },
			        "prediction": datasets.Value("""int64"""	),
			    },
			    "references": datasets.Value("""int64"""	),
			}
		else:
			return {
			    "predictions": datasets.Value("""int64"""	),
			    "references": datasets.Value("""int64"""	),
			}





	def 						_A (		self				:					List[str] ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					str	):
		if self.config_name == "axb":
			return {"matthews_correlation": matthews_corrcoef(__lowerCamelCase ,  __lowerCamelCase	)}
		elif self.config_name == "cb":
			return acc_and_fa(__lowerCamelCase ,  __lowerCamelCase ,  fa_avg="""macro"""	)
		elif self.config_name == "record":
			UpperCamelCase     :Optional[Any]          =	[
			    {
			        """qas""": [
			            {"""id""": ref["""idx"""]["""query"""], """answers""": [{"""text""": ans} for ans in ref["""answers"""]]}
			            for ref in references
			        ]
			    }
			]
			UpperCamelCase     :Tuple          =	{pred["""idx"""]["""query"""]: pred["""prediction_text"""] for pred in predictions}
			return evaluate_record(__lowerCamelCase ,  __lowerCamelCase	)[0]
		elif self.config_name == "multirc":
			return evaluate_multirc(__lowerCamelCase ,  __lowerCamelCase	)
		elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]:
			return {"accuracy": simple_accuracy(__lowerCamelCase ,  __lowerCamelCase	)}
		else:
			raise KeyError(
			    """You should supply a configuration name selected in """
			    """[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]"""	)
