



"""simple docstring"""







import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionAttendAndExcitePipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_numpy, skip_mps, slow
from diffusers.utils.testing_utils import 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


A		=						False

@skip_mps
class       _a  ( SCREAMING_SNAKE_CASE__     ,	SCREAMING_SNAKE_CASE__     ,	SCREAMING_SNAKE_CASE__     ,	unittest.TestCase):
			__magic_name__     					=     StableDiffusionAttendAndExcitePipeline
			__magic_name__     					=     False
			__magic_name__     					=     TEXT_TO_IMAGE_PARAMS
			__magic_name__     					=     TEXT_TO_IMAGE_BATCH_PARAMS.union({"""token_indices"""})
			__magic_name__     					=     TEXT_TO_IMAGE_IMAGE_PARAMS
			__magic_name__     					=     TEXT_TO_IMAGE_IMAGE_PARAMS



			@classmethod
			def 				__lowercase					(			cls							:				List[str]		)							->					Any:
									super().setUpClass()
									torch.use_deterministic_algorithms(_lowercase		)



			@classmethod
			def 				__lowercase					(			cls							:				int		)							->					Optional[Any]:
									super().tearDownClass()
									torch.use_deterministic_algorithms(_lowercase		)



			def 				__lowercase					(			self							:				List[Any]		)							->					List[Any]:
									torch.manual_seed(0		)
									snake_case   :			List[Any]						    = UNetaDConditionModel(
									    block_out_channels=(32, 64)   ,	layers_per_block=1   ,	sample_size=32   ,	in_channels=4   ,	out_channels=4   ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")   ,	up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")   ,	cross_attention_dim=32   ,	attention_head_dim=(2, 4)   ,	use_linear_projection=_lowercase   ,	)
									snake_case   :			str						    = DDIMScheduler(
									    beta_start=0.00085   ,	beta_end=0.012   ,	beta_schedule="scaled_linear"   ,	clip_sample=_lowercase   ,	set_alpha_to_one=_lowercase   ,	)
									torch.manual_seed(0		)
									snake_case   :			Union[str, 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   ,	sample_size=128   ,	)
									torch.manual_seed(0		)
									snake_case   :			str						    = CLIPTextConfig(
									    bos_token_id=0   ,	eos_token_id=2   ,	hidden_size=32   ,	intermediate_size=37   ,	layer_norm_eps=1E-05   ,	num_attention_heads=4   ,	num_hidden_layers=5   ,	pad_token_id=1   ,	vocab_size=1000   ,	hidden_act="gelu"   ,	projection_dim=512   ,	)
									snake_case   :			Optional[Any]						    = CLIPTextModel(_lowercase		)
									snake_case   :			Union[str, Any]						    = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"		)

									snake_case   :			Optional[int]						    = {
									    "unet": unet,
									    "scheduler": scheduler,
									    "vae": vae,
									    "text_encoder": text_encoder,
									    "tokenizer": tokenizer,
									    "safety_checker": None,
									    "feature_extractor": None,
									}

									return components



			def 				__lowercase					(			self							:				Optional[Any]   ,	_lowercase							:				Any   ,	_lowercase							:				Any=0		)							->					Optional[int]:
									if str(_lowercase		).startswith("mps"		):
															snake_case   :			Any						    = torch.manual_seed(_lowercase		)
									else:
															snake_case   :			List[str]						    = torch.Generator(device=_lowercase		).manual_seed(_lowercase		)
									snake_case   :			Tuple						    = {
									    "prompt": "a cat and a frog",
									    "token_indices": [2, 5],
									    "generator": generator,
									    "num_inference_steps": 1,
									    "guidance_scale": 6.0,
									    "output_type": "numpy",
									    "max_iter_to_alter": 2,
									    "thresholds": {0: 0.7},
									}
									return inputs



			def 				__lowercase					(			self							:				Dict		)							->					Any:
									snake_case   :			Optional[int]						    = "cpu"

									snake_case   :			str						    = self.get_dummy_components()
									snake_case   :			Dict						    = self.pipeline_class(**_lowercase		)
									pipe.to(_lowercase		)
									pipe.set_progress_bar_config(disable=_lowercase		)

									snake_case   :			List[str]						    = self.get_dummy_inputs(_lowercase		)
									snake_case   :			Optional[Any]						    = pipe(**_lowercase		).images
									snake_case   :			Optional[int]						    = image[0, -3:, -3:, -1]

									self.assertEqual(image.shape   ,	(1, 64, 64, 3)		)
									snake_case   :			Optional[int]						    = np.array(
									    [0.63905364, 0.62897307, 0.48599017, 0.5133624, 0.5550048, 0.45769516, 0.50326973, 0.5023139, 0.45384496]		)
									snake_case   :			int						    = np.abs(image_slice.flatten() - expected_slice		).max()
									self.assertLessEqual(_lowercase   ,	1E-3		)



			def 				__lowercase					(			self							:				List[Any]		)							->					Dict:
									super().test_cpu_offload_forward_pass(expected_max_diff=5E-4		)



			def 				__lowercase					(			self							:				Any		)							->					str:
									# NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
									self._test_inference_batch_consistent(batch_sizes=[1, 2]		)



			def 				__lowercase					(			self							:				List[Any]		)							->					Union[str, Any]:
									self._test_inference_batch_single_identical(batch_size=2   ,	expected_max_diff=7E-4		)



			def 				__lowercase					(			self							:				Any		)							->					Optional[int]:
									super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3		)



			def 				__lowercase					(			self							:				Dict		)							->					List[str]:
									super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5E-4		)



			def 				__lowercase					(			self							:				Tuple		)							->					str:
									super().test_save_load_local(expected_max_difference=5E-4		)



			def 				__lowercase					(			self							:				List[str]		)							->					Optional[int]:
									super().test_save_load_optional_components(expected_max_difference=4E-4		)



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



			@classmethod
			def 				__lowercase					(			cls							:				Any		)							->					Optional[Any]:
									super().setUpClass()
									torch.use_deterministic_algorithms(_lowercase		)



			@classmethod
			def 				__lowercase					(			cls							:				Optional[Any]		)							->					Dict:
									super().tearDownClass()
									torch.use_deterministic_algorithms(_lowercase		)



			def 				__lowercase					(			self							:				Optional[Any]		)							->					Dict:
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()



			def 				__lowercase					(			self							:				str		)							->					int:
									snake_case   :			Tuple						    = torch.manual_seed(51		)

									snake_case   :			Any						    = StableDiffusionAttendAndExcitePipeline.from_pretrained(
									    "CompVis/stable-diffusion-v1-4"   ,	safety_checker=_lowercase   ,	torch_dtype=torch.floataa		)
									pipe.to("cuda"		)

									snake_case   :			Any						    = "a painting of an elephant with glasses"
									snake_case   :			str						    = [5, 7]

									snake_case   :			Tuple						    = pipe(
									    prompt=_lowercase   ,	token_indices=_lowercase   ,	guidance_scale=7.5   ,	generator=_lowercase   ,	num_inference_steps=5   ,	max_iter_to_alter=5   ,	output_type="numpy"   ,	).images[0]

									snake_case   :			int						    = load_numpy(
									    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy"		)
									assert np.abs((expected_image - image).max()		) < 5E-1








"""simple docstring"""







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


A		=						logging.get_logger(__name__)

A		=						{
    '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       _a  ( SCREAMING_SNAKE_CASE__):
			__magic_name__     					=     """bloom"""
			__magic_name__     					=     ["""past_key_values"""]
			__magic_name__     					=     {
			    """num_hidden_layers""": """n_layer""",
			    """num_attention_heads""": """n_head""",
			}



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

									snake_case   :			List[Any]						    = bos_token_id
									snake_case   :			Any						    = eos_token_id
									snake_case   :			Optional[Any]						    = slow_but_exact

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



class       _a  ( SCREAMING_SNAKE_CASE__):
			__magic_name__     					=     version.parse("""1.12""")



			def __init__(			self							:				int   ,	_lowercase							:				PretrainedConfig   ,	_lowercase							:				str = "default"   ,	_lowercase							:				List[PatchingSpec] = None   ,	_lowercase							:				bool = False   ,	)							->					Dict:
									super().__init__(_lowercase   ,	task=_lowercase   ,	patching_specs=_lowercase   ,	use_past=_lowercase		)
									if not getattr(self._config   ,	"pad_token_id"   ,	_lowercase		):
															# TODO: how to do that better?
															snake_case   :			int						    = 0



			@property
			def 				__lowercase					(			self							:				str		)							->					Mapping[str, Mapping[int, str]]:
									snake_case   :			int						    = 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_(_lowercase   ,	direction="inputs"   ,	inverted_values_shape=_lowercase		)
															snake_case   :			Optional[int]						    = {0: "batch", 1: "past_sequence + sequence"}
									else:
															snake_case   :			Dict						    = {0: "batch", 1: "sequence"}

									return common_inputs



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



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



			@property
			def 				__lowercase					(			self							:				Union[str, Any]		)							->					float:
									return 1E-3



			def 				__lowercase					(			self							:				List[str]   ,	_lowercase							:				"PreTrainedTokenizer"   ,	_lowercase							:				int = -1   ,	_lowercase							:				int = -1   ,	_lowercase							:				bool = False   ,	_lowercase							:				Optional["TensorType"] = None   ,	)							->					Mapping[str, Any]:
									snake_case   :			int						    = super(_lowercase   ,	self		).generate_dummy_inputs(
									    _lowercase   ,	batch_size=_lowercase   ,	seq_length=_lowercase   ,	is_pair=_lowercase   ,	framework=_lowercase		)

									# We need to order the input in the way they appears in the forward()
									snake_case   :			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

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

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

									return ordered_inputs



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



