
import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
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 PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




class   __lowercase						(						__snake_case   , __snake_case   , unittest.TestCase       ):
	_A       =   StableDiffusionSAGPipeline
	_A       =   TEXT_TO_IMAGE_PARAMS
	_A       =   TEXT_TO_IMAGE_BATCH_PARAMS
	_A       =   TEXT_TO_IMAGE_IMAGE_PARAMS
	_A       =   TEXT_TO_IMAGE_IMAGE_PARAMS
	_A       =   False






	def       _a(self				: int    ) ->			Union[str, Any]:
							torch.manual_seed(0    )
							_lowercase       :	Optional[Any]           =   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       :	Tuple           =   DDIMScheduler(
							    beta_start=0.0_00_85      ,	beta_end=0.0_12      ,	beta_schedule="scaled_linear"      ,	clip_sample=snake_case      ,	set_alpha_to_one=snake_case      ,	)
							torch.manual_seed(0    )
							_lowercase       :	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      ,	)
							torch.manual_seed(0    )
							_lowercase       :	Dict           =   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      ,	)
							_lowercase       :	Optional[int]           =   CLIPTextModel(snake_case    )
							_lowercase       :	str           =   CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"    )

							_lowercase       :	Any           =   {
							    "unet": unet,
							    "scheduler": scheduler,
							    "vae": vae,
							    "text_encoder": text_encoder,
							    "tokenizer": tokenizer,
							    "safety_checker": None,
							    "feature_extractor": None,
							}
							return components






	def       _a(self				: List[str]      ,	snake_case				: Union[str, Any]      ,	snake_case				: str=0    ) ->			Union[str, Any]:
							if str(snake_case    ).startswith("mps"    ):
													_lowercase       :	Optional[int]           =   torch.manual_seed(snake_case    )
							else:
													_lowercase       :	str           =   torch.Generator(device=snake_case    ).manual_seed(snake_case    )
							_lowercase       :	Optional[int]           =   {
							    "prompt": ".",
							    "generator": generator,
							    "num_inference_steps": 2,
							    "guidance_scale": 1.0,
							    "sag_scale": 1.0,
							    "output_type": "numpy",
							}
							return inputs






	def       _a(self				: Dict    ) ->			Tuple:
							super().test_inference_batch_single_identical(expected_max_diff=3e-3    )




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






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






	def       _a(self				: List[str]    ) ->			List[str]:
							_lowercase       :	List[Any]           =   StableDiffusionSAGPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"    )
							_lowercase       :	str           =   sag_pipe.to(snake_case    )
							sag_pipe.set_progress_bar_config(disable=snake_case    )

							_lowercase       :	Optional[Any]           =   "."
							_lowercase       :	List[Any]           =   torch.manual_seed(0    )
							_lowercase       :	str           =   sag_pipe(
							    [prompt]      ,	generator=snake_case      ,	guidance_scale=7.5      ,	sag_scale=1.0      ,	num_inference_steps=20      ,	output_type="np"    )

							_lowercase       :	Any           =   output.images

							_lowercase       :	List[Any]           =   image[0, -3:, -3:, -1]

							assert image.shape == (1, 512, 512, 3)
							_lowercase       :	Tuple           =   np.array([0.15_68, 0.17_38, 0.16_95, 0.16_93, 0.15_07, 0.17_05, 0.15_47, 0.17_51, 0.19_49]    )

							assert np.abs(image_slice.flatten() - expected_slice    ).max() < 5e-2






	def       _a(self				: Dict    ) ->			Any:
							_lowercase       :	List[str]           =   StableDiffusionSAGPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"    )
							_lowercase       :	List[str]           =   sag_pipe.to(snake_case    )
							sag_pipe.set_progress_bar_config(disable=snake_case    )

							_lowercase       :	Any           =   "."
							_lowercase       :	Optional[int]           =   torch.manual_seed(0    )
							_lowercase       :	Optional[Any]           =   sag_pipe(
							    [prompt]      ,	generator=snake_case      ,	guidance_scale=7.5      ,	sag_scale=1.0      ,	num_inference_steps=20      ,	output_type="np"    )

							_lowercase       :	str           =   output.images

							_lowercase       :	List[str]           =   image[0, -3:, -3:, -1]

							assert image.shape == (1, 512, 512, 3)
							_lowercase       :	Any           =   np.array([0.34_59, 0.28_76, 0.25_37, 0.30_02, 0.26_71, 0.21_60, 0.30_26, 0.22_62, 0.23_71]    )

							assert np.abs(image_slice.flatten() - expected_slice    ).max() < 5e-2






	def       _a(self				: Dict    ) ->			Dict:
							_lowercase       :	Optional[Any]           =   StableDiffusionSAGPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"    )
							_lowercase       :	int           =   sag_pipe.to(snake_case    )
							sag_pipe.set_progress_bar_config(disable=snake_case    )

							_lowercase       :	str           =   "."
							_lowercase       :	Any           =   torch.manual_seed(0    )
							_lowercase       :	int           =   sag_pipe(
							    [prompt]      ,	width=768      ,	height=512      ,	generator=snake_case      ,	guidance_scale=7.5      ,	sag_scale=1.0      ,	num_inference_steps=20      ,	output_type="np"      ,	)

							_lowercase       :	List[str]           =   output.images

							assert image.shape == (1, 512, 768, 3)






import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel

from ...utils import logging


UpperCamelCase_						:     int	       =							logging.get_logger(__name__)



def 		UpperCamelCase     (       _UpperCAmelCase  :   Optional[int] ,			_UpperCAmelCase  :   Dict  )   ->			Any:






						'''simple docstring'''

						_lowercase       :	List[Any]           =   nn.functional.normalize(_UpperCAmelCase  )
						_lowercase       :	List[str]           =   nn.functional.normalize(_UpperCAmelCase  )
						return torch.mm(_UpperCAmelCase ,			normalized_text_embeds.t()  )




class   __lowercase						(						__snake_case       ):
	_A       =   CLIPConfig

	_A       =   ["CLIPEncoderLayer"]






	def __init__(self				: List[str]      ,	snake_case				: CLIPConfig    ) ->			Optional[int]:
							super().__init__(snake_case    )

							_lowercase       :	Tuple           =   CLIPVisionModel(config.vision_config    )
							_lowercase       :	Any           =   nn.Linear(config.vision_config.hidden_size      ,	config.projection_dim      ,	bias=snake_case    )

							_lowercase       :	int           =   nn.Parameter(torch.ones(17      ,	config.projection_dim    )      ,	requires_grad=snake_case    )
							_lowercase       :	str           =   nn.Parameter(torch.ones(3      ,	config.projection_dim    )      ,	requires_grad=snake_case    )

							_lowercase       :	Any           =   nn.Parameter(torch.ones(17    )      ,	requires_grad=snake_case    )
							_lowercase       :	Dict           =   nn.Parameter(torch.ones(3    )      ,	requires_grad=snake_case    )






	@torch.no_grad()
	def       _a(self				: int      ,	snake_case				: Tuple      ,	snake_case				: Optional[Any]    ) ->			Union[str, Any]:
							_lowercase       :	str           =   self.vision_model(snake_case    )[1]  # pooled_output
							_lowercase       :	List[Any]           =   self.visual_projection(snake_case    )

							# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
							_lowercase       :	str           =   cosine_distance(snake_case      ,	self.special_care_embeds    ).cpu().float().numpy()
							_lowercase       :	Optional[int]           =   cosine_distance(snake_case      ,	self.concept_embeds    ).cpu().float().numpy()

							_lowercase       :	List[str]           =   []
							_lowercase       :	int           =   image_embeds.shape[0]
							for i in range(snake_case    ):
													_lowercase       :	int           =   {"special_scores": {}, "special_care": [], "concept_scores": {}, "bad_concepts": []}

													# increase this value to create a stronger `nfsw` filter
													# at the cost of increasing the possibility of filtering benign images
													_lowercase       :	Optional[int]           =   0.0

													for concept_idx in range(len(special_cos_dist[0]    )    ):
																			_lowercase       :	Union[str, Any]           =   special_cos_dist[i][concept_idx]
																			_lowercase       :	Optional[int]           =   self.special_care_embeds_weights[concept_idx].item()
																			_lowercase       :	Optional[int]           =   round(concept_cos - concept_threshold + adjustment      ,	3    )
																			if result_img["special_scores"][concept_idx] > 0:
																									result_img["special_care"].append({concept_idx, result_img["special_scores"][concept_idx]}    )
																									_lowercase       :	Union[str, Any]           =   0.01

													for concept_idx in range(len(cos_dist[0]    )    ):
																			_lowercase       :	Tuple           =   cos_dist[i][concept_idx]
																			_lowercase       :	Union[str, Any]           =   self.concept_embeds_weights[concept_idx].item()
																			_lowercase       :	str           =   round(concept_cos - concept_threshold + adjustment      ,	3    )
																			if result_img["concept_scores"][concept_idx] > 0:
																									result_img["bad_concepts"].append(snake_case    )

													result.append(snake_case    )

							_lowercase       :	Dict           =   [len(res["bad_concepts"]    ) > 0 for res in result]

							return images, has_nsfw_concepts






	@torch.no_grad()
	def       _a(self				: str      ,	snake_case				: torch.FloatTensor      ,	snake_case				: torch.FloatTensor    ) ->			List[str]:
							_lowercase       :	Dict           =   self.vision_model(snake_case    )[1]  # pooled_output
							_lowercase       :	Any           =   self.visual_projection(snake_case    )

							_lowercase       :	Optional[Any]           =   cosine_distance(snake_case      ,	self.special_care_embeds    )
							_lowercase       :	Optional[Any]           =   cosine_distance(snake_case      ,	self.concept_embeds    )

							# increase this value to create a stronger `nsfw` filter
							# at the cost of increasing the possibility of filtering benign images
							_lowercase       :	Optional[int]           =   0.0

							_lowercase       :	Optional[int]           =   special_cos_dist - self.special_care_embeds_weights + adjustment
							# special_scores = special_scores.round(decimals=3)
							_lowercase       :	List[str]           =   torch.any(special_scores > 0      ,	dim=1    )
							_lowercase       :	List[Any]           =   special_care * 0.01
							_lowercase       :	Optional[Any]           =   special_adjustment.unsqueeze(1    ).expand(-1      ,	cos_dist.shape[1]    )

							_lowercase       :	Any           =   (cos_dist - self.concept_embeds_weights) + special_adjustment
							# concept_scores = concept_scores.round(decimals=3)
							_lowercase       :	List[Any]           =   torch.any(concept_scores > 0      ,	dim=1    )

							return images, has_nsfw_concepts




