






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

from ...utils import logging


__UpperCamelCase   :						Optional[int]							=  logging.get_logger(__name__)
def        a_	( _A ,				_A					)     ->     Tuple:







				"""simple docstring"""
				snake_case__													=       nn.functional.normalize(_A					)
				snake_case__													=       nn.functional.normalize(_A					)
				return torch.mm(_A ,				normalized_text_embeds.t()					)


class 					__SCREAMING_SNAKE_CASE(		a_		):
							_UpperCAmelCase	      =    CLIPConfig

							_UpperCAmelCase	      =    ["CLIPEncoderLayer"]
							def __init__(       self: Optional[Any]    ,		UpperCamelCase: CLIPConfig  )						->						Optional[Any]:
											super().__init__(UpperCamelCase  )

											snake_case__													=       CLIPVisionModel(config.vision_config  )
											snake_case__													=       nn.Linear(config.vision_config.hidden_size    ,		config.projection_dim    ,		bias=UpperCamelCase  )

											snake_case__													=       nn.Parameter(torch.ones(17    ,		config.projection_dim  )    ,		requires_grad=UpperCamelCase  )
											snake_case__													=       nn.Parameter(torch.ones(3    ,		config.projection_dim  )    ,		requires_grad=UpperCamelCase  )

											snake_case__													=       nn.Parameter(torch.ones(17  )    ,		requires_grad=UpperCamelCase  )
											snake_case__													=       nn.Parameter(torch.ones(3  )    ,		requires_grad=UpperCamelCase  )
							@torch.no_grad()
							def lowerCAmelCase_	(       self: Any    ,		UpperCamelCase: Optional[int]    ,		UpperCamelCase: int  )						->						Optional[int]:
											snake_case__													=       self.vision_model(UpperCamelCase  )[1]  # pooled_output
											snake_case__													=       self.visual_projection(UpperCamelCase  )

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

											snake_case__													=       []
											snake_case__													=       image_embeds.shape[0]
											for i in range(UpperCamelCase  ):
															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
															snake_case__													=       0.0

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

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

															result.append(UpperCamelCase  )

											snake_case__													=       [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]  ):
																							snake_case__													=       torch.zeros_like(images[idx]  )  # black image
																			else:
																							snake_case__													=       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 lowerCAmelCase_	(       self: int    ,		UpperCamelCase: torch.FloatTensor    ,		UpperCamelCase: torch.FloatTensor  )						->						Union[str, Any]:
											snake_case__													=       self.vision_model(UpperCamelCase  )[1]  # pooled_output
											snake_case__													=       self.visual_projection(UpperCamelCase  )

											snake_case__													=       cosine_distance(UpperCamelCase    ,		self.special_care_embeds  )
											snake_case__													=       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
											snake_case__													=       0.0

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

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

											snake_case__													=       0.0  # black image

											return images, has_nsfw_concepts












from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCamelCase   :						int							=  {
    """configuration_openai""": ["""OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """OpenAIGPTConfig"""],
    """tokenization_openai""": ["""OpenAIGPTTokenizer"""],
}

try:
							if not is_tokenizers_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase   :						Optional[Any]							=  ["""OpenAIGPTTokenizerFast"""]

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase   :						List[str]							=  [
							    """OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """OpenAIGPTDoubleHeadsModel""",
							    """OpenAIGPTForSequenceClassification""",
							    """OpenAIGPTLMHeadModel""",
							    """OpenAIGPTModel""",
							    """OpenAIGPTPreTrainedModel""",
							    """load_tf_weights_in_openai_gpt""",
							]

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase   :						Dict							=  [
							    """TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """TFOpenAIGPTDoubleHeadsModel""",
							    """TFOpenAIGPTForSequenceClassification""",
							    """TFOpenAIGPTLMHeadModel""",
							    """TFOpenAIGPTMainLayer""",
							    """TFOpenAIGPTModel""",
							    """TFOpenAIGPTPreTrainedModel""",
							]


if TYPE_CHECKING:
							from .configuration_openai import OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OpenAIGPTConfig
							from .tokenization_openai import OpenAIGPTTokenizer

							try:
														if not is_tokenizers_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_openai_fast import OpenAIGPTTokenizerFast

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_openai import (
														    OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    OpenAIGPTDoubleHeadsModel,
														    OpenAIGPTForSequenceClassification,
														    OpenAIGPTLMHeadModel,
														    OpenAIGPTModel,
														    OpenAIGPTPreTrainedModel,
														    load_tf_weights_in_openai_gpt,
														)

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_openai import (
														    TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFOpenAIGPTDoubleHeadsModel,
														    TFOpenAIGPTForSequenceClassification,
														    TFOpenAIGPTLMHeadModel,
														    TFOpenAIGPTMainLayer,
														    TFOpenAIGPTModel,
														    TFOpenAIGPTPreTrainedModel,
														)

else:
							import sys

							__UpperCamelCase   :						List[Any]							=  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




