





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


lowercase_		:				str				=						False


@skip_mps
class 			lowercase				(							a_		,			a_		,			a_		,			unittest.TestCase     ):



						"""simple docstring"""

						_UpperCamelCase  :	int								=	StableDiffusionAttendAndExcitePipeline
						_UpperCamelCase  :	List[Any]								=	False
						_UpperCamelCase  :	Any								=	TEXT_TO_IMAGE_PARAMS
						_UpperCamelCase  :	Tuple								=	TEXT_TO_IMAGE_BATCH_PARAMS.union({"token_indices"}     )
						_UpperCamelCase  :	Union[str, Any]								=	TEXT_TO_IMAGE_IMAGE_PARAMS
						_UpperCamelCase  :	str								=	TEXT_TO_IMAGE_IMAGE_PARAMS







						@classmethod
						def 		__UpperCAmelCase       (    cls			:		Dict					):





										'''simple docstring'''







										super().setUpClass()
										torch.use_deterministic_algorithms(lowerCamelCase_					)







						@classmethod
						def 		__UpperCAmelCase       (    cls			:		int					):





										'''simple docstring'''







										super().tearDownClass()
										torch.use_deterministic_algorithms(lowerCamelCase_					)







						def 		__UpperCAmelCase       (    self			:		Any					):





										'''simple docstring'''







										torch.manual_seed(0					)
										_snake_case :			List[str]  						=   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=lowerCamelCase_ ,				)
										_snake_case :			Optional[int]  						=   DDIMScheduler(
										    beta_start=0.0_0085 ,				beta_end=0.012 ,				beta_schedule='scaled_linear' ,				clip_sample=lowerCamelCase_ ,				set_alpha_to_one=lowerCamelCase_ ,				)
										torch.manual_seed(0					)
										_snake_case :			int  						=   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=1_28 ,				)
										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=10_00 ,				hidden_act='gelu' ,				projection_dim=5_12 ,				)
										_snake_case :			Optional[int]  						=   CLIPTextModel(lowerCamelCase_					)
										_snake_case :			List[str]  						=   CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'					)

										_snake_case :			Optional[Any]  						=   {
										    'unet': unet,
										    'scheduler': scheduler,
										    'vae': vae,
										    'text_encoder': text_encoder,
										    'tokenizer': tokenizer,
										    'safety_checker': None,
										    'feature_extractor': None,
										}

										return components







						def 		__UpperCAmelCase       (    self			:		List[Any] ,				lowerCamelCase_			:		List[Any] ,				lowerCamelCase_			:		Union[str, Any]=0					):





										'''simple docstring'''







										if str(lowerCamelCase_					).startswith('mps'					):
														_snake_case :			str  						=   torch.manual_seed(lowerCamelCase_					)
										else:
														_snake_case :			List[Any]  						=   torch.Generator(device=lowerCamelCase_					).manual_seed(lowerCamelCase_					)
										_snake_case :			Any  						=   {
										    '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 		__UpperCAmelCase       (    self			:		Union[str, Any]					):





										'''simple docstring'''







										_snake_case :			Any  						=   'cpu'

										_snake_case :			List[Any]  						=   self.get_dummy_components()
										_snake_case :			List[Any]  						=   self.pipeline_class(**lowerCamelCase_					)
										pipe.to(lowerCamelCase_					)
										pipe.set_progress_bar_config(disable=lowerCamelCase_					)

										_snake_case :			List[str]  						=   self.get_dummy_inputs(lowerCamelCase_					)
										_snake_case :			Tuple  						=   pipe(**lowerCamelCase_					).images
										_snake_case :			Union[str, Any]  						=   image[0, -3:, -3:, -1]

										self.assertEqual(image.shape ,				(1, 64, 64, 3)					)
										_snake_case :			Optional[int]  						=   np.array(
										    [0.6390_5364, 0.6289_7307, 0.4859_9017, 0.513_3624, 0.555_0048, 0.4576_9516, 0.5032_6973, 0.502_3139, 0.4538_4496]					)
										_snake_case :			List[str]  						=   np.abs(image_slice.flatten() - expected_slice					).max()
										self.assertLessEqual(lowerCamelCase_ ,				1e-3					)







						def 		__UpperCAmelCase       (    self			:		int					):





										'''simple docstring'''







										super().test_cpu_offload_forward_pass(expected_max_diff=5e-4					)







						def 		__UpperCAmelCase       (    self			:		Tuple					):





										'''simple docstring'''







										self._test_inference_batch_consistent(batch_sizes=[1, 2]					)







						def 		__UpperCAmelCase       (    self			:		List[str]					):





										'''simple docstring'''







										self._test_inference_batch_single_identical(batch_size=2 ,				expected_max_diff=7e-4					)







						def 		__UpperCAmelCase       (    self			:		Tuple					):





										'''simple docstring'''







										super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3					)







						def 		__UpperCAmelCase       (    self			:		Dict					):





										'''simple docstring'''







										super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4					)







						def 		__UpperCAmelCase       (    self			:		int					):





										'''simple docstring'''







										super().test_save_load_local(expected_max_difference=5e-4					)







						def 		__UpperCAmelCase       (    self			:		Optional[int]					):





										'''simple docstring'''







										super().test_save_load_optional_components(expected_max_difference=4e-4					)



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



						"""simple docstring"""

						@classmethod
						def 		__UpperCAmelCase       (    cls			:		Optional[Any]					):





										'''simple docstring'''







										super().setUpClass()
										torch.use_deterministic_algorithms(lowerCamelCase_					)







						@classmethod
						def 		__UpperCAmelCase       (    cls			:		Union[str, Any]					):





										'''simple docstring'''







										super().tearDownClass()
										torch.use_deterministic_algorithms(lowerCamelCase_					)







						def 		__UpperCAmelCase       (    self			:		Optional[Any]					):





										'''simple docstring'''







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







						def 		__UpperCAmelCase       (    self			:		str					):





										'''simple docstring'''







										_snake_case :			Dict  						=   torch.manual_seed(51					)

										_snake_case :			Any  						=   StableDiffusionAttendAndExcitePipeline.from_pretrained(
										    'CompVis/stable-diffusion-v1-4' ,				safety_checker=lowerCamelCase_ ,				torch_dtype=torch.floataa					)
										pipe.to('cuda'					)

										_snake_case :			Union[str, Any]  						=   'a painting of an elephant with glasses'
										_snake_case :			Union[str, Any]  						=   [5, 7]

										_snake_case :			int  						=   pipe(
										    prompt=lowerCamelCase_ ,				token_indices=lowerCamelCase_ ,				guidance_scale=7.5 ,				generator=lowerCamelCase_ ,				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













import torch

from transformers import AutoModel


class 			lowercase				(							torch.nn.Module     ):



						"""simple docstring"""

						def __init__(    self			:		Tuple ,				lowerCamelCase_			:		Dict="sayef/fsner-bert-base-uncased"					):





										'''simple docstring'''







										super(lowerCamelCase_ ,				self					).__init__()

										_snake_case :			Optional[Any]  						=   AutoModel.from_pretrained(lowerCamelCase_ ,				return_dict=lowerCamelCase_					)
										_snake_case :			str  						=   torch.nn.CosineSimilarity(3 ,				1e-08					)
										_snake_case :			str  						=   torch.nn.Softmax(dim=1					)







						def 		__UpperCAmelCase       (    self			:		int ,				**lowerCamelCase_			:		List[str]					):





										'''simple docstring'''







										return self.bert(**lowerCamelCase_					).last_hidden_state







						def 		__UpperCAmelCase       (    self			:		List[Any] ,				lowerCamelCase_			:		Union[str, Any]					):





										'''simple docstring'''







										return token_embeddings.sum(2 ,				keepdim=lowerCamelCase_					)







						def 		__UpperCAmelCase       (    self			:		Dict ,				lowerCamelCase_			:		Dict ,				lowerCamelCase_			:		Tuple ,				lowerCamelCase_			:		Optional[Any]=1					):





										'''simple docstring'''







										return self.softmax(T * self.cos(lowerCamelCase_ ,				lowerCamelCase_					)					)







						def 		__UpperCAmelCase       (    self			:		Optional[Any] ,				lowerCamelCase_			:		Any ,				lowerCamelCase_			:		Union[str, Any]					):





										'''simple docstring'''







										_snake_case :			int  						=   W_supports['sizes'].tolist()
										_snake_case :			int  						=   W_supports['start_token_id'].item()
										_snake_case :			List[str]  						=   W_supports['end_token_id'].item()

										del W_supports["sizes"]
										del W_supports["start_token_id"]
										del W_supports["end_token_id"]

										_snake_case :			Optional[int]  						=   self.BERT(**lowerCamelCase_					)
										_snake_case :			Optional[Any]  						=   self.BERT(**lowerCamelCase_					)

										_snake_case :			Optional[int]  						=   None
										_snake_case :			Optional[int]  						=   None

										_snake_case :			List[str]  						=   W_supports['input_ids'] == start_token_id
										_snake_case :			Union[str, Any]  						=   W_supports['input_ids'] == end_token_id

										for i, size in enumerate(lowerCamelCase_					):
														if i == 0:
																		_snake_case :			str  						=   0
														else:
																		_snake_case :			Union[str, Any]  						=   support_sizes[i - 1]

														_snake_case :			Tuple  						=   S[s : s + size][start_token_masks[s : s + size]]
														_snake_case :			Optional[int]  						=   S[s : s + size][end_token_masks[s : s + size]]

														_snake_case :			Tuple  						=   torch.matmul(q[i] ,				s_start.T					).sum(1					).softmax(0					)
														_snake_case :			Union[str, Any]  						=   torch.matmul(q[i] ,				s_end.T					).sum(1					).softmax(0					)

														if p_starts is not None:
																		_snake_case :			Optional[Any]  						=   torch.vstack((p_starts, p_start)					)
																		_snake_case :			List[str]  						=   torch.vstack((p_ends, p_end)					)
														else:
																		_snake_case :			Union[str, Any]  						=   p_start
																		_snake_case :			Any  						=   p_end

										return p_starts, p_ends






