import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()

class        UpperCamelCase						(				__a	,	__a	,	unittest.TestCase				):
						a__		:List[str]      =			StableDiffusionXLImgaImgPipeline
						a__		:Dict      =			TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''height''', '''width'''}
						a__		:Dict      =			PipelineTesterMixin.required_optional_params - {'''latents'''}
						a__		:Optional[Any]      =			TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
						a__		:int      =			IMAGE_TO_IMAGE_IMAGE_PARAMS
						a__		:List[str]      =			IMAGE_TO_IMAGE_IMAGE_PARAMS





						def        A_							(self						)				->				Tuple:
												torch.manual_seed(0						)
												UpperCamelCase_		:							int  =		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""")			,     attention_head_dim=(2, 4)			,     use_linear_projection=__UpperCamelCase			,     addition_embed_type="""text_time"""			,     addition_time_embed_dim=8			,     transformer_layers_per_block=(1, 2)			,     projection_class_embeddings_input_dim=80			,     cross_attention_dim=64			,     )
												UpperCamelCase_		:							Union[str, Any]  =		EulerDiscreteScheduler(
												    beta_start=0.00_085			,     beta_end=0.012			,     steps_offset=1			,     beta_schedule="""scaled_linear"""			,     timestep_spacing="""leading"""			,     )
												torch.manual_seed(0						)
												UpperCamelCase_		:							Tuple  =		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			,     sample_size=128			,     )
												torch.manual_seed(0						)
												UpperCamelCase_		:							Union[str, Any]  =		CLIPTextConfig(
												    bos_token_id=0			,     eos_token_id=2			,     hidden_size=32			,     intermediate_size=37			,     layer_norm_eps=1E-0_5			,     num_attention_heads=4			,     num_hidden_layers=5			,     pad_token_id=1			,     vocab_size=1_000			,     hidden_act="""gelu"""			,     projection_dim=32			,     )
												UpperCamelCase_		:							List[str]  =		CLIPTextModel(__UpperCamelCase						)
												UpperCamelCase_		:							Optional[Any]  =		CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""			,     local_files_only=__UpperCamelCase						)

												UpperCamelCase_		:							Tuple  =		CLIPTextModelWithProjection(__UpperCamelCase						)
												UpperCamelCase_		:							List[Any]  =		CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""			,     local_files_only=__UpperCamelCase						)

												UpperCamelCase_		:							List[Any]  =		{
												    """unet""": unet,
												    """scheduler""": scheduler,
												    """vae""": vae,
												    """text_encoder""": text_encoder,
												    """tokenizer""": tokenizer,
												    """text_encoder_2""": text_encoder_a,
												    """tokenizer_2""": tokenizer_a,
												    # "safety_checker": None,
												    # "feature_extractor": None,
												}
												return components





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase=0						)				->				Dict:
												UpperCamelCase_		:							Union[str, Any]  =		floats_tensor((1, 3, 32, 32)			,     rng=random.Random(__UpperCamelCase						)						).to(__UpperCamelCase						)
												UpperCamelCase_		:							str  =		image / 2 + 0.5
												if str(__UpperCamelCase						).startswith("""mps"""						):
																		UpperCamelCase_		:							int  =		torch.manual_seed(__UpperCamelCase						)
												else:
																		UpperCamelCase_		:							List[str]  =		torch.Generator(device=__UpperCamelCase						).manual_seed(__UpperCamelCase						)
												UpperCamelCase_		:							List[Any]  =		{
												    """prompt""": """A painting of a squirrel eating a burger""",
												    """image""": image,
												    """generator""": generator,
												    """num_inference_steps""": 2,
												    """guidance_scale""": 5.0,
												    """output_type""": """numpy""",
												    """strength""": 0.75,
												}
												return inputs





						def        A_							(self						)				->				Union[str, Any]:
												UpperCamelCase_		:							Union[str, Any]  =		"""cpu"""  # ensure determinism for the device-dependent torch.Generator
												UpperCamelCase_		:							Optional[int]  =		self.get_dummy_components()
												UpperCamelCase_		:							Optional[Any]  =		StableDiffusionXLImgaImgPipeline(**__UpperCamelCase						)
												UpperCamelCase_		:							List[Any]  =		sd_pipe.to(__UpperCamelCase						)
												sd_pipe.set_progress_bar_config(disable=__UpperCamelCase						)

												UpperCamelCase_		:							int  =		self.get_dummy_inputs(__UpperCamelCase						)
												UpperCamelCase_		:							Optional[int]  =		sd_pipe(**__UpperCamelCase						).images
												UpperCamelCase_		:							Dict  =		image[0, -3:, -3:, -1]

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

												UpperCamelCase_		:							Tuple  =		np.array([0.4_656, 0.4_840, 0.4_439, 0.6_698, 0.5_574, 0.4_524, 0.5_799, 0.5_943, 0.5_165]						)

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





						def        A_							(self						)				->				Tuple:
												super().test_attention_slicing_forward_pass(expected_max_diff=3E-3						)





						def        A_							(self						)				->				Dict:
												super().test_inference_batch_single_identical(expected_max_diff=3E-3						)





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





						def        A_							(self						)				->				List[Any]:
												UpperCamelCase_		:							int  =		self.get_dummy_components()
												UpperCamelCase_		:							List[str]  =		StableDiffusionXLImgaImgPipeline(**__UpperCamelCase						)
												UpperCamelCase_		:							List[str]  =		sd_pipe.to(__UpperCamelCase						)
												UpperCamelCase_		:							Any  =		sd_pipe.to(__UpperCamelCase						)
												sd_pipe.set_progress_bar_config(disable=__UpperCamelCase						)

												# forward without prompt embeds
												UpperCamelCase_		:							Any  =		self.get_dummy_inputs(__UpperCamelCase						)
												UpperCamelCase_		:							Any  =		3 * ["""this is a negative prompt"""]
												UpperCamelCase_		:							int  =		negative_prompt
												UpperCamelCase_		:							Tuple  =		3 * [inputs["""prompt"""]]

												UpperCamelCase_		:							List[str]  =		sd_pipe(**__UpperCamelCase						)
												UpperCamelCase_		:							List[str]  =		output.images[0, -3:, -3:, -1]

												# forward with prompt embeds
												UpperCamelCase_		:							List[str]  =		self.get_dummy_inputs(__UpperCamelCase						)
												UpperCamelCase_		:							Optional[Any]  =		3 * ["""this is a negative prompt"""]
												UpperCamelCase_		:							Union[str, Any]  =		3 * [inputs.pop("""prompt"""						)]

												(
												    (
												    UpperCamelCase_
												),(
												    UpperCamelCase_
												),(
												    UpperCamelCase_
												),(
												    UpperCamelCase_
												),
												)		:							int  =		sd_pipe.encode_prompt(__UpperCamelCase			,     negative_prompt=__UpperCamelCase						)

												UpperCamelCase_		:							str  =		sd_pipe(
												    **__UpperCamelCase			,     prompt_embeds=__UpperCamelCase			,     negative_prompt_embeds=__UpperCamelCase			,     pooled_prompt_embeds=__UpperCamelCase			,     negative_pooled_prompt_embeds=__UpperCamelCase			,     )
												UpperCamelCase_		:							Optional[int]  =		output.images[0, -3:, -3:, -1]

												# make sure that it's equal
												assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()						).max() < 1E-4


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





						def        A_							(self						)				->				Tuple:
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase="cpu"			,     __UpperCamelCase=torch.floataa			,     __UpperCamelCase=0						)				->				Optional[int]:
												UpperCamelCase_		:							Any  =		torch.Generator(device=__UpperCamelCase						).manual_seed(__UpperCamelCase						)
												UpperCamelCase_		:							Any  =		np.random.RandomState(__UpperCamelCase						).standard_normal((1, 4, 64, 64)						)
												UpperCamelCase_		:							Optional[int]  =		torch.from_numpy(__UpperCamelCase						).to(device=__UpperCamelCase			,     dtype=__UpperCamelCase						)
												UpperCamelCase_		:							Tuple  =		{
												    """prompt""": """a photograph of an astronaut riding a horse""",
												    """latents""": latents,
												    """generator""": generator,
												    """num_inference_steps""": 3,
												    """guidance_scale""": 7.5,
												    """output_type""": """numpy""",
												}
												return inputs





						def        A_							(self						)				->				Optional[int]:
												UpperCamelCase_		:							Union[str, Any]  =		DiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-base"""						)
												pipe.to(__UpperCamelCase						)
												pipe.set_progress_bar_config(disable=__UpperCamelCase						)

												UpperCamelCase_		:							Optional[Any]  =		self.get_inputs(__UpperCamelCase						)
												UpperCamelCase_		:							str  =		pipe(**__UpperCamelCase						).images
												UpperCamelCase_		:							List[Any]  =		image[0, -3:, -3:, -1].flatten()

												assert image.shape == (1, 512, 512, 3)
												UpperCamelCase_		:							List[str]  =		np.array([0.49_493, 0.47_896, 0.40_798, 0.54_214, 0.53_212, 0.48_202, 0.47_656, 0.46_329, 0.48_506]						)
												assert np.abs(image_slice - expected_slice						).max() < 7E-3







from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
	from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


SCREAMING_SNAKE_CASE		:     List[str]         =	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE		:     str         =	{
    "bigscience/bloom": "https://huggingface.co/bigscience/bloom/resolve/main/config.json",
    "bigscience/bloom-560m": "https://huggingface.co/bigscience/bloom-560m/blob/main/config.json",
    "bigscience/bloom-1b1": "https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json",
    "bigscience/bloom-1b7": "https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json",
    "bigscience/bloom-3b": "https://huggingface.co/bigscience/bloom-3b/blob/main/config.json",
    "bigscience/bloom-7b1": "https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json",
}

class        UpperCamelCase						(				__a				):
						a__		:Any      =			'''bloom'''
						a__		:Optional[int]      =			['''past_key_values''']
						a__		:Tuple      =			{
						    '''num_hidden_layers''': '''n_layer''',
						    '''num_attention_heads''': '''n_head''',
						}





						def __init__(self			,     __UpperCamelCase=250_880			,     __UpperCamelCase=64			,     __UpperCamelCase=2			,     __UpperCamelCase=8			,     __UpperCamelCase=1E-5			,     __UpperCamelCase=0.02			,     __UpperCamelCase=True			,     __UpperCamelCase=1			,     __UpperCamelCase=2			,     __UpperCamelCase=False			,     __UpperCamelCase=0.0			,     __UpperCamelCase=0.0			,     __UpperCamelCase=1			,     __UpperCamelCase=False			,     **__UpperCamelCase			,     )				->				List[Any]:
												UpperCamelCase_		:							Dict  =		vocab_size
												# Backward compatibility with n_embed kwarg
												UpperCamelCase_		:							Optional[Any]  =		kwargs.pop("""n_embed"""			,     __UpperCamelCase						)
												UpperCamelCase_		:							List[Any]  =		hidden_size if n_embed is None else n_embed
												UpperCamelCase_		:							List[Any]  =		n_layer
												UpperCamelCase_		:							Optional[Any]  =		n_head
												UpperCamelCase_		:							List[str]  =		layer_norm_epsilon
												UpperCamelCase_		:							int  =		initializer_range
												UpperCamelCase_		:							List[str]  =		use_cache
												UpperCamelCase_		:							List[Any]  =		pretraining_tp
												UpperCamelCase_		:							Optional[int]  =		apply_residual_connection_post_layernorm
												UpperCamelCase_		:							Any  =		hidden_dropout
												UpperCamelCase_		:							Optional[Any]  =		attention_dropout

												UpperCamelCase_		:							List[str]  =		bos_token_id
												UpperCamelCase_		:							Optional[Any]  =		eos_token_id
												UpperCamelCase_		:							str  =		slow_but_exact

												super().__init__(bos_token_id=__UpperCamelCase			,     eos_token_id=__UpperCamelCase			,     **__UpperCamelCase						)


class        UpperCamelCase						(				__a				):
						a__		:Dict      =			version.parse('''1.12'''				)





						def __init__(self			,     __UpperCamelCase			,     __UpperCamelCase = "default"			,     __UpperCamelCase = None			,     __UpperCamelCase = False			,     )				->				List[str]:
												super().__init__(__UpperCamelCase			,     task=__UpperCamelCase			,     patching_specs=__UpperCamelCase			,     use_past=__UpperCamelCase						)
												if not getattr(self._config			,     """pad_token_id"""			,     __UpperCamelCase						):
																		# TODO: how to do that better?
																		UpperCamelCase_		:							Optional[Any]  =		0





						@property
						def        A_							(self						)				->				Mapping[str, Mapping[int, str]]:
												UpperCamelCase_		:							str  =		OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}						)
												if self.use_past:
																		# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
																		self.fill_with_past_key_values_(__UpperCamelCase			,     direction="""inputs"""			,     inverted_values_shape=__UpperCamelCase						)
																		UpperCamelCase_		:							int  =		{0: """batch""", 1: """past_sequence + sequence"""}
												else:
																		UpperCamelCase_		:							Dict  =		{0: """batch""", 1: """sequence"""}

												return common_inputs





						@property
						def        A_							(self						)				->				int:
												return self._config.n_layer





						@property
						def        A_							(self						)				->				int:
												return self._config.n_head





						@property
						def        A_							(self						)				->				float:
												return 1E-3





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase = -1			,     __UpperCamelCase = -1			,     __UpperCamelCase = False			,     __UpperCamelCase = None			,     )				->				Mapping[str, Any]:
												UpperCamelCase_		:							Union[str, Any]  =		super(__UpperCamelCase			,     self						).generate_dummy_inputs(
												    __UpperCamelCase			,     batch_size=__UpperCamelCase			,     seq_length=__UpperCamelCase			,     is_pair=__UpperCamelCase			,     framework=__UpperCamelCase						)

												# We need to order the input in the way they appears in the forward()
												UpperCamelCase_		:							List[Any]  =		OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}						)

												# Need to add the past_keys
												if self.use_past:
																		if not is_torch_available():
																								raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""						)
																		else:
																								import torch

																								UpperCamelCase_,UpperCamelCase_		:							List[str]  =		common_inputs["""input_ids"""].shape
																								# Not using the same length for past_key_values
																								UpperCamelCase_		:							List[str]  =		seqlen + 2
																								UpperCamelCase_		:							Tuple  =		self._config.hidden_size // self.num_attention_heads
																								UpperCamelCase_		:							List[Any]  =		(
																								    batch * self.num_attention_heads,
																								    head_dim,
																								    past_key_values_length,
																								)
																								UpperCamelCase_		:							str  =		(
																								    batch * self.num_attention_heads,
																								    past_key_values_length,
																								    head_dim,
																								)
																								UpperCamelCase_		:							Any  =		[
																								    (torch.zeros(__UpperCamelCase						), torch.zeros(__UpperCamelCase						)) for _ in range(self.num_layers						)
																								]

												UpperCamelCase_		:							int  =		common_inputs["""attention_mask"""]
												if self.use_past:
																		UpperCamelCase_		:							List[str]  =		ordered_inputs["""attention_mask"""].dtype
																		UpperCamelCase_		:							Any  =		torch.cat(
																		    [ordered_inputs["""attention_mask"""], torch.ones(__UpperCamelCase			,     __UpperCamelCase			,     dtype=__UpperCamelCase						)]			,     dim=1						)

												return ordered_inputs





						@property
						def        A_							(self						)				->				int:
												return 13






