



import tempfile

import torch

from diffusers import (
    DEISMultistepScheduler,
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    UniPCMultistepScheduler,
)

from .test_schedulers import SchedulerCommonTest
class 				__A					( A_      ):
			UpperCamelCase					:Union[str, Any]     =			(DEISMultistepScheduler,)
			UpperCamelCase					:Any     =			(('''num_inference_steps''', 25),)





			def        _snake_case    (self							,    **__magic_name__				):
						lowerCamelCase__ :      int								=		{
						    """num_train_timesteps""": 1000,
						    """beta_start""": 0.00_01,
						    """beta_end""": 0.02,
						    """beta_schedule""": """linear""",
						    """solver_order""": 2,
						}

						config.update(**__magic_name__				)
						return config





			def        _snake_case    (self							,    __magic_name__=0							,    **__magic_name__				):
						lowerCamelCase__ :      Dict								=		dict(self.forward_default_kwargs				)
						lowerCamelCase__ :      Optional[int]								=		kwargs.pop("""num_inference_steps"""							,    __magic_name__				)
						lowerCamelCase__ :      Optional[Any]								=		self.dummy_sample
						lowerCamelCase__ :      List[Any]								=		0.1 * sample
						lowerCamelCase__ :      Optional[Any]								=		[residual + 0.2, residual + 0.15, residual + 0.10]

						for scheduler_class in self.scheduler_classes:
									lowerCamelCase__ :      List[Any]								=		self.get_scheduler_config(**__magic_name__				)
									lowerCamelCase__ :      List[str]								=		scheduler_class(**__magic_name__				)
									scheduler.set_timesteps(__magic_name__				)
									# copy over dummy past residuals
									lowerCamelCase__ :      Union[str, Any]								=		dummy_past_residuals[: scheduler.config.solver_order]

									with tempfile.TemporaryDirectory() as tmpdirname:
												scheduler.save_config(__magic_name__				)
												lowerCamelCase__ :      List[str]								=		scheduler_class.from_pretrained(__magic_name__				)
												new_scheduler.set_timesteps(__magic_name__				)
												# copy over dummy past residuals
												lowerCamelCase__ :      Dict								=		dummy_past_residuals[: new_scheduler.config.solver_order]

									lowerCamelCase__							,lowerCamelCase__ :      Dict								=		sample, sample
									for t in range(__magic_name__							,    time_step + scheduler.config.solver_order + 1				):
												lowerCamelCase__ :      Optional[int]								=		scheduler.step(__magic_name__							,    __magic_name__							,    __magic_name__							,    **__magic_name__				).prev_sample
												lowerCamelCase__ :      str								=		new_scheduler.step(__magic_name__							,    __magic_name__							,    __magic_name__							,    **__magic_name__				).prev_sample

												assert torch.sum(torch.abs(output - new_output				)				) < 1E-5, "Scheduler outputs are not identical"





			def        _snake_case    (self				):
						pass





			def        _snake_case    (self							,    __magic_name__=0							,    **__magic_name__				):
						lowerCamelCase__ :      int								=		dict(self.forward_default_kwargs				)
						lowerCamelCase__ :      Any								=		kwargs.pop("""num_inference_steps"""							,    __magic_name__				)
						lowerCamelCase__ :      Tuple								=		self.dummy_sample
						lowerCamelCase__ :      Optional[Any]								=		0.1 * sample
						lowerCamelCase__ :      Dict								=		[residual + 0.2, residual + 0.15, residual + 0.10]

						for scheduler_class in self.scheduler_classes:
									lowerCamelCase__ :      Any								=		self.get_scheduler_config()
									lowerCamelCase__ :      Union[str, Any]								=		scheduler_class(**__magic_name__				)
									scheduler.set_timesteps(__magic_name__				)

									# copy over dummy past residuals (must be after setting timesteps)
									lowerCamelCase__ :      Any								=		dummy_past_residuals[: scheduler.config.solver_order]

									with tempfile.TemporaryDirectory() as tmpdirname:
												scheduler.save_config(__magic_name__				)
												lowerCamelCase__ :      str								=		scheduler_class.from_pretrained(__magic_name__				)
												# copy over dummy past residuals
												new_scheduler.set_timesteps(__magic_name__				)

												# copy over dummy past residual (must be after setting timesteps)
												lowerCamelCase__ :      Any								=		dummy_past_residuals[: new_scheduler.config.solver_order]

									lowerCamelCase__ :      Any								=		scheduler.step(__magic_name__							,    __magic_name__							,    __magic_name__							,    **__magic_name__				).prev_sample
									lowerCamelCase__ :      Union[str, Any]								=		new_scheduler.step(__magic_name__							,    __magic_name__							,    __magic_name__							,    **__magic_name__				).prev_sample

									assert torch.sum(torch.abs(output - new_output				)				) < 1E-5, "Scheduler outputs are not identical"





			def        _snake_case    (self							,    __magic_name__=None							,    **__magic_name__				):
						if scheduler is None:
									lowerCamelCase__ :      Optional[Any]								=		self.scheduler_classes[0]
									lowerCamelCase__ :      List[str]								=		self.get_scheduler_config(**__magic_name__				)
									lowerCamelCase__ :      Union[str, Any]								=		scheduler_class(**__magic_name__				)

						lowerCamelCase__ :      str								=		self.scheduler_classes[0]
						lowerCamelCase__ :      List[str]								=		self.get_scheduler_config(**__magic_name__				)
						lowerCamelCase__ :      int								=		scheduler_class(**__magic_name__				)

						lowerCamelCase__ :      Tuple								=		10
						lowerCamelCase__ :      int								=		self.dummy_model()
						lowerCamelCase__ :      Union[str, Any]								=		self.dummy_sample_deter
						scheduler.set_timesteps(__magic_name__				)

						for i, t in enumerate(scheduler.timesteps				):
									lowerCamelCase__ :      Dict								=		model(__magic_name__							,    __magic_name__				)
									lowerCamelCase__ :      Optional[int]								=		scheduler.step(__magic_name__							,    __magic_name__							,    __magic_name__				).prev_sample

						return sample





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[int]								=		dict(self.forward_default_kwargs				)

						lowerCamelCase__ :      Any								=		kwargs.pop("""num_inference_steps"""							,    __magic_name__				)

						for scheduler_class in self.scheduler_classes:
									lowerCamelCase__ :      str								=		self.get_scheduler_config()
									lowerCamelCase__ :      List[Any]								=		scheduler_class(**__magic_name__				)

									lowerCamelCase__ :      Optional[Any]								=		self.dummy_sample
									lowerCamelCase__ :      int								=		0.1 * sample

									if num_inference_steps is not None and hasattr(__magic_name__							,    """set_timesteps"""				):
												scheduler.set_timesteps(__magic_name__				)
									elif num_inference_steps is not None and not hasattr(__magic_name__							,    """set_timesteps"""				):
												lowerCamelCase__ :      Optional[int]								=		num_inference_steps

									# copy over dummy past residuals (must be done after set_timesteps)
									lowerCamelCase__ :      Tuple								=		[residual + 0.2, residual + 0.15, residual + 0.10]
									lowerCamelCase__ :      Optional[int]								=		dummy_past_residuals[: scheduler.config.solver_order]

									lowerCamelCase__ :      Any								=		scheduler.timesteps[5]
									lowerCamelCase__ :      Tuple								=		scheduler.timesteps[6]

									lowerCamelCase__ :      str								=		scheduler.step(__magic_name__							,    __magic_name__							,    __magic_name__							,    **__magic_name__				).prev_sample
									lowerCamelCase__ :      Dict								=		scheduler.step(__magic_name__							,    __magic_name__							,    __magic_name__							,    **__magic_name__				).prev_sample

									self.assertEqual(output_a.shape							,    sample.shape				)
									self.assertEqual(output_a.shape							,    output_a.shape				)





			def        _snake_case    (self				):
						# make sure that iterating over schedulers with same config names gives same results
						# for defaults
						lowerCamelCase__ :      List[str]								=		DEISMultistepScheduler(**self.get_scheduler_config()				)
						lowerCamelCase__ :      Any								=		self.full_loop(scheduler=__magic_name__				)
						lowerCamelCase__ :      Dict								=		torch.mean(torch.abs(__magic_name__				)				)

						assert abs(result_mean.item() - 0.2_39_16				) < 1E-3

						lowerCamelCase__ :      Tuple								=		DPMSolverSinglestepScheduler.from_config(scheduler.config				)
						lowerCamelCase__ :      Dict								=		DPMSolverMultistepScheduler.from_config(scheduler.config				)
						lowerCamelCase__ :      int								=		UniPCMultistepScheduler.from_config(scheduler.config				)
						lowerCamelCase__ :      Any								=		DEISMultistepScheduler.from_config(scheduler.config				)

						lowerCamelCase__ :      Tuple								=		self.full_loop(scheduler=__magic_name__				)
						lowerCamelCase__ :      Optional[int]								=		torch.mean(torch.abs(__magic_name__				)				)

						assert abs(result_mean.item() - 0.2_39_16				) < 1E-3





			def        _snake_case    (self				):
						for timesteps in [25, 50, 100, 999, 1000]:
									self.check_over_configs(num_train_timesteps=__magic_name__				)





			def        _snake_case    (self				):
						self.check_over_configs(thresholding=__magic_name__				)
						for order in [1, 2, 3]:
									for solver_type in ["logrho"]:
												for threshold in [0.5, 1.0, 2.0]:
															for prediction_type in ["epsilon", "sample"]:
																		self.check_over_configs(
																		    thresholding=__magic_name__							,    prediction_type=__magic_name__							,    sample_max_value=__magic_name__							,    algorithm_type="""deis"""							,    solver_order=__magic_name__							,    solver_type=__magic_name__							,    )





			def        _snake_case    (self				):
						for prediction_type in ["epsilon", "v_prediction"]:
									self.check_over_configs(prediction_type=__magic_name__				)





			def        _snake_case    (self				):
						for algorithm_type in ["deis"]:
									for solver_type in ["logrho"]:
												for order in [1, 2, 3]:
															for prediction_type in ["epsilon", "sample"]:
																		self.check_over_configs(
																		    solver_order=__magic_name__							,    solver_type=__magic_name__							,    prediction_type=__magic_name__							,    algorithm_type=__magic_name__							,    )
																		lowerCamelCase__ :      List[str]								=		self.full_loop(
																		    solver_order=__magic_name__							,    solver_type=__magic_name__							,    prediction_type=__magic_name__							,    algorithm_type=__magic_name__							,    )
																		assert not torch.isnan(__magic_name__				).any(), "Samples have nan numbers"





			def        _snake_case    (self				):
						self.check_over_configs(lower_order_final=__magic_name__				)
						self.check_over_configs(lower_order_final=__magic_name__				)





			def        _snake_case    (self				):
						for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]:
									self.check_over_forward(num_inference_steps=__magic_name__							,    time_step=0				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[str]								=		self.full_loop()
						lowerCamelCase__ :      Dict								=		torch.mean(torch.abs(__magic_name__				)				)

						assert abs(result_mean.item() - 0.2_39_16				) < 1E-3





			def        _snake_case    (self				):
						lowerCamelCase__ :      int								=		self.full_loop(prediction_type="""v_prediction"""				)
						lowerCamelCase__ :      List[str]								=		torch.mean(torch.abs(__magic_name__				)				)

						assert abs(result_mean.item() - 0.0_91				) < 1E-3





			def        _snake_case    (self				):
						lowerCamelCase__ :      Dict								=		self.scheduler_classes[0]
						lowerCamelCase__ :      List[Any]								=		self.get_scheduler_config(thresholding=__magic_name__							,    dynamic_thresholding_ratio=0				)
						lowerCamelCase__ :      List[str]								=		scheduler_class(**__magic_name__				)

						lowerCamelCase__ :      Optional[int]								=		10
						lowerCamelCase__ :      str								=		self.dummy_model()
						lowerCamelCase__ :      Dict								=		self.dummy_sample_deter.half()
						scheduler.set_timesteps(__magic_name__				)

						for i, t in enumerate(scheduler.timesteps				):
									lowerCamelCase__ :      Optional[int]								=		model(__magic_name__							,    __magic_name__				)
									lowerCamelCase__ :      Dict								=		scheduler.step(__magic_name__							,    __magic_name__							,    __magic_name__				).prev_sample

						assert sample.dtype == torch.floataa








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

from ...utils import logging


_lowercase     							=						logging.get_logger(__name__)






def      _A				(UpperCamelCase		:      Dict      ,			UpperCamelCase		:      Optional[int] )  ->Tuple:



			'''simple docstring'''







			lowerCamelCase__ :      List[str]								=		nn.functional.normalize(UpperCamelCase )
			lowerCamelCase__ :      Tuple								=		nn.functional.normalize(UpperCamelCase )
			return torch.mm(UpperCamelCase      ,			normalized_text_embeds.t() )
class 				__A					( A_      ):
			UpperCamelCase					:Optional[Any]     =			CLIPConfig

			UpperCamelCase					:Optional[Any]     =			['''CLIPEncoderLayer''']





			def __init__(self							,    __magic_name__				):
						super().__init__(__magic_name__				)

						lowerCamelCase__ :      Union[str, Any]								=		CLIPVisionModel(config.vision_config				)
						lowerCamelCase__ :      Any								=		nn.Linear(config.vision_config.hidden_size							,    config.projection_dim							,    bias=__magic_name__				)

						lowerCamelCase__ :      Any								=		nn.Parameter(torch.ones(17							,    config.projection_dim				)							,    requires_grad=__magic_name__				)
						lowerCamelCase__ :      Union[str, Any]								=		nn.Parameter(torch.ones(3							,    config.projection_dim				)							,    requires_grad=__magic_name__				)

						lowerCamelCase__ :      Any								=		nn.Parameter(torch.ones(17				)							,    requires_grad=__magic_name__				)
						lowerCamelCase__ :      Optional[Any]								=		nn.Parameter(torch.ones(3				)							,    requires_grad=__magic_name__				)





			@torch.no_grad()
			def        _snake_case    (self							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Any								=		self.vision_model(__magic_name__				)[1]  # pooled_output
						lowerCamelCase__ :      Tuple								=		self.visual_projection(__magic_name__				)

						# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
						lowerCamelCase__ :      Any								=		cosine_distance(__magic_name__							,    self.special_care_embeds				).cpu().float().numpy()
						lowerCamelCase__ :      str								=		cosine_distance(__magic_name__							,    self.concept_embeds				).cpu().float().numpy()

						lowerCamelCase__ :      int								=		[]
						lowerCamelCase__ :      str								=		image_embeds.shape[0]
						for i in range(__magic_name__				):
									lowerCamelCase__ :      str								=		{"""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
									lowerCamelCase__ :      Optional[Any]								=		0.0

									for concept_idx in range(len(special_cos_dist[0]				)				):
												lowerCamelCase__ :      List[str]								=		special_cos_dist[i][concept_idx]
												lowerCamelCase__ :      Tuple								=		self.special_care_embeds_weights[concept_idx].item()
												lowerCamelCase__ :      str								=		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]}				)
															lowerCamelCase__ :      List[str]								=		0.01

									for concept_idx in range(len(cos_dist[0]				)				):
												lowerCamelCase__ :      str								=		cos_dist[i][concept_idx]
												lowerCamelCase__ :      Dict								=		self.concept_embeds_weights[concept_idx].item()
												lowerCamelCase__ :      int								=		round(concept_cos - concept_threshold + adjustment							,    3				)
												if result_img["concept_scores"][concept_idx] > 0:
															result_img["bad_concepts"].append(__magic_name__				)

									result.append(__magic_name__				)

						lowerCamelCase__ :      Tuple								=		[len(res["""bad_concepts"""]				) > 0 for res in result]

						return images, has_nsfw_concepts





			@torch.no_grad()
			def        _snake_case    (self							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Union[str, Any]								=		self.vision_model(__magic_name__				)[1]  # pooled_output
						lowerCamelCase__ :      str								=		self.visual_projection(__magic_name__				)

						lowerCamelCase__ :      Optional[Any]								=		cosine_distance(__magic_name__							,    self.special_care_embeds				)
						lowerCamelCase__ :      Dict								=		cosine_distance(__magic_name__							,    self.concept_embeds				)

						# increase this value to create a stronger `nsfw` filter
						# at the cost of increasing the possibility of filtering benign images
						lowerCamelCase__ :      Any								=		0.0

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

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

						return images, has_nsfw_concepts



