




'''simple docstring'''



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

from ...utils import logging


lowerCamelCase_									=		logging.get_logger(__name__)
def 					SCREAMING_SNAKE_CASE_					(				__A							:					str			,	__A							:					int    )					->					Tuple:
   _SCREAMING_SNAKE_CASE			= nn.functional.normalize(_A    )
   _SCREAMING_SNAKE_CASE			= nn.functional.normalize(_A    )
   return torch.mm(_A			,	normalized_text_embeds.t()    )







class 						lowercase_       (					A  ):


   """simple docstring"""

   lowerCamelCase_							    =		CLIPConfig

   lowerCamelCase_							    =		['CLIPEncoderLayer']



   def __init__(       self   :      Optional[int]    , __lowerCamelCase   :      Dict       ):

      """simple docstring"""
      super().__init__(__lowerCamelCase       )

      _SCREAMING_SNAKE_CASE			= CLIPVisionModel(config.vision_config       )
      _SCREAMING_SNAKE_CASE			= nn.Linear(config.vision_config.hidden_size    , config.projection_dim    , bias=__lowerCamelCase       )

      _SCREAMING_SNAKE_CASE			= nn.Parameter(torch.ones(1_7    , config.projection_dim       )    , requires_grad=__lowerCamelCase       )
      _SCREAMING_SNAKE_CASE			= nn.Parameter(torch.ones(3    , config.projection_dim       )    , requires_grad=__lowerCamelCase       )

      _SCREAMING_SNAKE_CASE			= nn.Parameter(torch.ones(1_7       )    , requires_grad=__lowerCamelCase       )
      _SCREAMING_SNAKE_CASE			= nn.Parameter(torch.ones(3       )    , requires_grad=__lowerCamelCase       )



   @torch.no_grad()
   def 				lowerCAmelCase_     (       self   :      Union[str, Any]    , __lowerCamelCase   :      Optional[Any]    , __lowerCamelCase   :      Optional[Any]       ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE			= self.vision_model(__lowerCamelCase       )[1]  # pooled_output
      _SCREAMING_SNAKE_CASE			= self.visual_projection(__lowerCamelCase       )

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

      _SCREAMING_SNAKE_CASE			= []
      _SCREAMING_SNAKE_CASE			= image_embeds.shape[0]
      for i in range(__lowerCamelCase       ):
         _SCREAMING_SNAKE_CASE			= {"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
         _SCREAMING_SNAKE_CASE			= 0.0

         for concept_idx in range(len(special_cos_dist[0]       )       ):
            _SCREAMING_SNAKE_CASE			= special_cos_dist[i][concept_idx]
            _SCREAMING_SNAKE_CASE			= self.special_care_embeds_weights[concept_idx].item()
            _SCREAMING_SNAKE_CASE			= 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]}       )
               _SCREAMING_SNAKE_CASE			= 0.0_1

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

         result.append(__lowerCamelCase       )

      _SCREAMING_SNAKE_CASE			= [len(res["bad_concepts"]       ) > 0 for res in result]

      return images, has_nsfw_concepts



   @torch.no_grad()
   def 				lowerCAmelCase_     (       self   :      Optional[int]    , __lowerCamelCase   :      List[Any]    , __lowerCamelCase   :      Optional[Any]       ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE			= self.vision_model(__lowerCamelCase       )[1]  # pooled_output
      _SCREAMING_SNAKE_CASE			= self.visual_projection(__lowerCamelCase       )

      _SCREAMING_SNAKE_CASE			= cosine_distance(__lowerCamelCase    , self.special_care_embeds       )
      _SCREAMING_SNAKE_CASE			= cosine_distance(__lowerCamelCase    , self.concept_embeds       )

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

      _SCREAMING_SNAKE_CASE			= special_cos_dist - self.special_care_embeds_weights + adjustment
      # special_scores = special_scores.round(decimals=3)
      _SCREAMING_SNAKE_CASE			= torch.any(special_scores > 0    , dim=1       )
      _SCREAMING_SNAKE_CASE			= special_care * 0.0_1
      _SCREAMING_SNAKE_CASE			= special_adjustment.unsqueeze(1       ).expand(-1    , cos_dist.shape[1]       )

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

      return images, has_nsfw_concepts





import torch

from diffusers import DDIMParallelScheduler

from .test_schedulers import SchedulerCommonTest







class 							lowerCamelCase__						(				UpperCAmelCase   ):






			"""simple docstring"""






			_UpperCamelCase		:       int 			=		(DDIMParallelScheduler,)
			_UpperCamelCase		:       List[Any] 			=		(('eta', 0.0), ('num_inference_steps', 50))




			def 					snake_case__					(				self   ,    **snake_case  ):

						'''simple docstring'''


						UpperCamelCase__       =							{
						    "num_train_timesteps": 1000,
						    "beta_start": 0.0001,
						    "beta_end": 0.02,
						    "beta_schedule": "linear",
						    "clip_sample": True,
						}

						config.update(**snake_case  )
						return config




			def 					snake_case__					(				self   ,    **snake_case  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.scheduler_classes[0]
						UpperCamelCase__       =							self.get_scheduler_config(**snake_case  )
						UpperCamelCase__       =							scheduler_class(**snake_case  )

						UpperCamelCase__,   UpperCamelCase__       =							10, 0.0

						UpperCamelCase__       =							self.dummy_model()
						UpperCamelCase__       =							self.dummy_sample_deter

						scheduler.set_timesteps(snake_case  )

						for t in scheduler.timesteps:
									UpperCamelCase__       =							model(snake_case   ,    snake_case  )
									UpperCamelCase__       =							scheduler.step(snake_case   ,    snake_case   ,    snake_case   ,    snake_case  ).prev_sample

						return sample




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for timesteps in [100, 500, 1000]:
									self.check_over_configs(num_train_timesteps=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for steps_offset in [0, 1]:
									self.check_over_configs(steps_offset=snake_case  )

						UpperCamelCase__       =							self.scheduler_classes[0]
						UpperCamelCase__       =							self.get_scheduler_config(steps_offset=1  )
						UpperCamelCase__       =							scheduler_class(**snake_case  )
						scheduler.set_timesteps(5  )
						assert torch.equal(scheduler.timesteps   ,    torch.LongTensor([801, 601, 401, 201, 1]  )  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1]   ,    [0.002, 0.02, 0.2, 2]  ):
									self.check_over_configs(beta_start=snake_case   ,    beta_end=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for schedule in ["linear", "squaredcos_cap_v2"]:
									self.check_over_configs(beta_schedule=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


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




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for clip_sample in [True, False]:
									self.check_over_configs(clip_sample=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for timestep_spacing in ["trailing", "leading"]:
									self.check_over_configs(timestep_spacing=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for rescale_betas_zero_snr in [True, False]:
									self.check_over_configs(rescale_betas_zero_snr=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						self.check_over_configs(thresholding=snake_case  )
						for threshold in [0.5, 1.0, 2.0]:
									for prediction_type in ["epsilon", "v_prediction"]:
												self.check_over_configs(
												    thresholding=snake_case   ,    prediction_type=snake_case   ,    sample_max_value=snake_case   ,    )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for t in [1, 10, 49]:
									self.check_over_forward(time_step=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for t, num_inference_steps in zip([1, 10, 50]   ,    [10, 50, 500]  ):
									self.check_over_forward(time_step=snake_case   ,    num_inference_steps=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for t, eta in zip([1, 10, 49]   ,    [0.0, 0.5, 1.0]  ):
									self.check_over_forward(time_step=snake_case   ,    eta=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.scheduler_classes[0]
						UpperCamelCase__       =							self.get_scheduler_config()
						UpperCamelCase__       =							scheduler_class(**snake_case  )

						assert torch.sum(torch.abs(scheduler._get_variance(0   ,    0  ) - 0.0  )  ) < 1E-5
						assert torch.sum(torch.abs(scheduler._get_variance(420   ,    400  ) - 0.14771  )  ) < 1E-5
						assert torch.sum(torch.abs(scheduler._get_variance(980   ,    960  ) - 0.32460  )  ) < 1E-5
						assert torch.sum(torch.abs(scheduler._get_variance(0   ,    0  ) - 0.0  )  ) < 1E-5
						assert torch.sum(torch.abs(scheduler._get_variance(487   ,    486  ) - 0.00979  )  ) < 1E-5
						assert torch.sum(torch.abs(scheduler._get_variance(999   ,    998  ) - 0.02  )  ) < 1E-5




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.scheduler_classes[0]
						UpperCamelCase__       =							self.get_scheduler_config()
						UpperCamelCase__       =							scheduler_class(**snake_case  )

						UpperCamelCase__,   UpperCamelCase__       =							10, 0.0
						scheduler.set_timesteps(snake_case  )

						UpperCamelCase__       =							self.dummy_model()
						UpperCamelCase__       =							self.dummy_sample_deter
						UpperCamelCase__       =							self.dummy_sample_deter + 0.1
						UpperCamelCase__       =							self.dummy_sample_deter - 0.1

						UpperCamelCase__       =							samplea.shape[0]
						UpperCamelCase__       =							torch.stack([samplea, samplea, samplea]   ,    dim=0  )
						UpperCamelCase__       =							torch.arange(snake_case  )[0:3, None].repeat(1   ,    snake_case  )

						UpperCamelCase__       =							model(samples.flatten(0   ,    1  )   ,    timesteps.flatten(0   ,    1  )  )
						UpperCamelCase__       =							scheduler.batch_step_no_noise(snake_case   ,    timesteps.flatten(0   ,    1  )   ,    samples.flatten(0   ,    1  )   ,    snake_case  )

						UpperCamelCase__       =							torch.sum(torch.abs(snake_case  )  )
						UpperCamelCase__       =							torch.mean(torch.abs(snake_case  )  )

						assert abs(result_sum.item() - 1147.7904  ) < 1E-2
						assert abs(result_mean.item() - 0.4982  ) < 1E-3




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.full_loop()

						UpperCamelCase__       =							torch.sum(torch.abs(snake_case  )  )
						UpperCamelCase__       =							torch.mean(torch.abs(snake_case  )  )

						assert abs(result_sum.item() - 172.0067  ) < 1E-2
						assert abs(result_mean.item() - 0.223967  ) < 1E-3




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.full_loop(prediction_type="v_prediction"  )

						UpperCamelCase__       =							torch.sum(torch.abs(snake_case  )  )
						UpperCamelCase__       =							torch.mean(torch.abs(snake_case  )  )

						assert abs(result_sum.item() - 52.5302  ) < 1E-2
						assert abs(result_mean.item() - 0.0684  ) < 1E-3




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.full_loop(set_alpha_to_one=snake_case   ,    beta_start=0.01  )
						UpperCamelCase__       =							torch.sum(torch.abs(snake_case  )  )
						UpperCamelCase__       =							torch.mean(torch.abs(snake_case  )  )

						assert abs(result_sum.item() - 149.8295  ) < 1E-2
						assert abs(result_mean.item() - 0.1951  ) < 1E-3







			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.full_loop(set_alpha_to_one=snake_case   ,    beta_start=0.01  )
						UpperCamelCase__       =							torch.sum(torch.abs(snake_case  )  )
						UpperCamelCase__       =							torch.mean(torch.abs(snake_case  )  )

						assert abs(result_sum.item() - 149.0784  ) < 1E-2
						assert abs(result_mean.item() - 0.1941  ) < 1E-3


