code: 
  """simple docstring"""
  
  
  
  
  
  import numpy as np
  import torch
  import torch.nn as nn
  from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel
  
  from ...utils import logging
  
  
  _a	 =							logging.get_logger(__name__)
  
  
  
  
  
  
  
  def 			__a  (	__lowerCamelCase,	__lowerCamelCase						):
  			UpperCAmelCase_      :      List[str]      =					nn.functional.normalize(snake_case_						)
  			UpperCAmelCase_      :      str      =					nn.functional.normalize(snake_case_						)
  			return torch.mm(snake_case_,	normalized_text_embeds.t()						)
  
  
  class A_			(UpperCamelCase_   ):
  
  
  
  
  		'''simple docstring'''
  		SCREAMING_SNAKE_CASE__    :		int						  =	CLIPConfig
  
  		SCREAMING_SNAKE_CASE__    :		str						  =	["""CLIPEncoderLayer"""]
  
  
  
  
  
  
  
  		def __init__(	self       ,     lowercase_						):
  
  					"""simple docstring"""
  					super().__init__(__UpperCamelCase						)
  
  					UpperCAmelCase_      :      Optional[int]      =					CLIPVisionModel(config.vision_config						)
  					UpperCAmelCase_      :      Union[str, Any]      =					nn.Linear(config.vision_config.hidden_size       ,     config.projection_dim       ,     bias=__UpperCamelCase						)
  
  					UpperCAmelCase_      :      int      =					nn.Parameter(torch.ones(17       ,     config.projection_dim						)       ,     requires_grad=__UpperCamelCase						)
  					UpperCAmelCase_      :      Dict      =					nn.Parameter(torch.ones(3       ,     config.projection_dim						)       ,     requires_grad=__UpperCamelCase						)
  
  					UpperCAmelCase_      :      Optional[Any]      =					nn.Parameter(torch.ones(17						)       ,     requires_grad=__UpperCamelCase						)
  					UpperCAmelCase_      :      Any      =					nn.Parameter(torch.ones(3						)       ,     requires_grad=__UpperCamelCase						)
  
  
  
  
  
  
  
  		@torch.no_grad()
  		def  UpperCamelCase__      (	self       ,     lowercase_       ,     lowercase_						):
  
  					"""simple docstring"""
  					UpperCAmelCase_      :      Union[str, Any]      =					self.vision_model(__UpperCamelCase						)[1]  # pooled_output
  					UpperCAmelCase_      :      Any      =					self.visual_projection(__UpperCamelCase						)
  
  					# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
  					UpperCAmelCase_      :      Optional[Any]      =					cosine_distance(__UpperCamelCase       ,     self.special_care_embeds						).cpu().float().numpy()
  					UpperCAmelCase_      :      Any      =					cosine_distance(__UpperCamelCase       ,     self.concept_embeds						).cpu().float().numpy()
  
  					UpperCAmelCase_      :      Tuple      =					[]
  					UpperCAmelCase_      :      List[str]      =					image_embeds.shape[0]
  					for i in range(__UpperCamelCase						):
  								UpperCAmelCase_      :      List[Any]      =					{'''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
  								UpperCAmelCase_      :      Union[str, Any]      =					0.0
  
  								for concept_idx in range(len(special_cos_dist[0]						)						):
  											UpperCAmelCase_      :      int      =					special_cos_dist[i][concept_idx]
  											UpperCAmelCase_      :      int      =					self.special_care_embeds_weights[concept_idx].item()
  											UpperCAmelCase_      :      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]}						)
  														UpperCAmelCase_      :      Dict      =					0.01
  
  								for concept_idx in range(len(cos_dist[0]						)						):
  											UpperCAmelCase_      :      Any      =					cos_dist[i][concept_idx]
  											UpperCAmelCase_      :      Any      =					self.concept_embeds_weights[concept_idx].item()
  											UpperCAmelCase_      :      Optional[int]      =					round(concept_cos - concept_threshold + adjustment       ,     3						)
  											if result_img["concept_scores"][concept_idx] > 0:
  														result_img["bad_concepts"].append(__UpperCamelCase						)
  
  								result.append(__UpperCamelCase						)
  
  					UpperCAmelCase_      :      Optional[int]      =					[len(res["bad_concepts"]						) > 0 for res in result]
  
  					for idx, has_nsfw_concept in enumerate(__UpperCamelCase						):
  								if has_nsfw_concept:
  											if torch.is_tensor(__UpperCamelCase						) or torch.is_tensor(images[0]						):
  														UpperCAmelCase_      :      Optional[int]      =					torch.zeros_like(images[idx]						)  # black image
  											else:
  														UpperCAmelCase_      :      Optional[int]      =					np.zeros(images[idx].shape						)  # black image
  
  					if any(__UpperCamelCase						):
  								logger.warning(
  								    "Potential NSFW content was detected in one or more images. A black image will be returned instead."
  								    " Try again with a different prompt and/or seed."						)
  
  					return images, has_nsfw_concepts
  
  
  
  
  
  
  
  		@torch.no_grad()
  		def  UpperCamelCase__      (	self       ,     lowercase_       ,     lowercase_						):
  
  					"""simple docstring"""
  					UpperCAmelCase_      :      Tuple      =					self.vision_model(__UpperCamelCase						)[1]  # pooled_output
  					UpperCAmelCase_      :      int      =					self.visual_projection(__UpperCamelCase						)
  
  					UpperCAmelCase_      :      List[str]      =					cosine_distance(__UpperCamelCase       ,     self.special_care_embeds						)
  					UpperCAmelCase_      :      Dict      =					cosine_distance(__UpperCamelCase       ,     self.concept_embeds						)
  
  					# increase this value to create a stronger `nsfw` filter
  					# at the cost of increasing the possibility of filtering benign images
  					UpperCAmelCase_      :      Union[str, Any]      =					0.0
  
  					UpperCAmelCase_      :      List[str]      =					special_cos_dist - self.special_care_embeds_weights + adjustment
  					# special_scores = special_scores.round(decimals=3)
  					UpperCAmelCase_      :      Dict      =					torch.any(special_scores > 0       ,     dim=1						)
  					UpperCAmelCase_      :      Optional[int]      =					special_care * 0.01
  					UpperCAmelCase_      :      List[Any]      =					special_adjustment.unsqueeze(1						).expand(-1       ,     cos_dist.shape[1]						)
  
  					UpperCAmelCase_      :      Optional[int]      =					(cos_dist - self.concept_embeds_weights) + special_adjustment
  					# concept_scores = concept_scores.round(decimals=3)
  					UpperCAmelCase_      :      Any      =					torch.any(concept_scores > 0       ,     dim=1						)
  
  					UpperCAmelCase_      :      List[Any]      =					0.0  # black image
  
  					return images, has_nsfw_concepts
  
  
  
style_context: 
  """simple docstring"""
  
  
  
  
  
  from .hash_table import HashTable
  
  
  class A_			(lowercase__   ):
  
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
  
    def __init__(	self       ,     *lowercase_       ,     **lowercase_						):
  
       """simple docstring"""
       super().__init__(*lowercase_       ,     **lowercase_						)
  
  
  
  
  
  
  
    def  UpperCamelCase__      (	self       ,     lowercase_       ,     lowercase_=None						):
  
       """simple docstring"""
       UpperCAmelCase_      :      Tuple      =					1
       UpperCAmelCase_      :      List[str]      =					self.hash_function(key + i * i						)
  
       while self.values[new_key] is not None and self.values[new_key] != key:
          i += 1
          UpperCAmelCase_      :      List[Any]      =					(
              self.hash_function(key + i * i						)
              if not self.balanced_factor() >= self.lim_charge
              else None
          )
  
          if new_key is None:
             break
  
       return new_key
  
  
  