



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

from ...utils import logging


SCREAMING_SNAKE_CASE  					=    logging.get_logger(__name__)







def snake_case__	(      __SCREAMING_SNAKE_CASE      ,  __SCREAMING_SNAKE_CASE			)		->				List[str]:
		UpperCAmelCase_						       =					nn.functional.normalize(__SCREAMING_SNAKE_CASE			)
		UpperCAmelCase_						       =					nn.functional.normalize(__SCREAMING_SNAKE_CASE			)
		return torch.mm(__SCREAMING_SNAKE_CASE      ,  normalized_text_embeds.t()			)



class 						lowerCamelCase   (    SCREAMING_SNAKE_CASE__	):







					'''simple docstring'''







					lowerCAmelCase_					:     Optional[int]					       =	CLIPConfig

					lowerCAmelCase_					:     int					       =	['CLIPEncoderLayer']



					def __init__(      self     ,						lowerCAmelCase  ):
							super().__init__(snake_case__  )

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

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

							UpperCAmelCase_						       =					nn.Parameter(torch.ones(17  )     ,						requires_grad=snake_case__  )
							UpperCAmelCase_						       =					nn.Parameter(torch.ones(3  )     ,						requires_grad=snake_case__  )



					@torch.no_grad()
					def  A__		(      self     ,						lowerCAmelCase     ,						lowerCAmelCase  ):
							UpperCAmelCase_						       =					self.vision_model(snake_case__  )[1]  # pooled_output
							UpperCAmelCase_						       =					self.visual_projection(snake_case__  )

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

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

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

									for concept_idx in range(len(cos_dist[0]  )  ):
											UpperCAmelCase_						       =					cos_dist[i][concept_idx]
											UpperCAmelCase_						       =					self.concept_embeds_weights[concept_idx].item()
											UpperCAmelCase_						       =					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__  )

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

							return images, has_nsfw_concepts



					@torch.no_grad()
					def  A__		(      self     ,						lowerCAmelCase     ,						lowerCAmelCase  ):
							UpperCAmelCase_						       =					self.vision_model(snake_case__  )[1]  # pooled_output
							UpperCAmelCase_						       =					self.visual_projection(snake_case__  )

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

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

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

							return images, has_nsfw_concepts







from typing import TYPE_CHECKING

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


SCREAMING_SNAKE_CASE:     Optional[int]									= {
    '''configuration_roformer''': ['''ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RoFormerConfig''', '''RoFormerOnnxConfig'''],
    '''tokenization_roformer''': ['''RoFormerTokenizer'''],
}

try:
						if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						SCREAMING_SNAKE_CASE:     Tuple									= ['''RoFormerTokenizerFast''']

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						SCREAMING_SNAKE_CASE:     Any									= [
						    '''ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''RoFormerForCausalLM''',
						    '''RoFormerForMaskedLM''',
						    '''RoFormerForMultipleChoice''',
						    '''RoFormerForQuestionAnswering''',
						    '''RoFormerForSequenceClassification''',
						    '''RoFormerForTokenClassification''',
						    '''RoFormerLayer''',
						    '''RoFormerModel''',
						    '''RoFormerPreTrainedModel''',
						    '''load_tf_weights_in_roformer''',
						]


try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						SCREAMING_SNAKE_CASE:     Dict									= [
						    '''TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''TFRoFormerForCausalLM''',
						    '''TFRoFormerForMaskedLM''',
						    '''TFRoFormerForMultipleChoice''',
						    '''TFRoFormerForQuestionAnswering''',
						    '''TFRoFormerForSequenceClassification''',
						    '''TFRoFormerForTokenClassification''',
						    '''TFRoFormerLayer''',
						    '''TFRoFormerModel''',
						    '''TFRoFormerPreTrainedModel''',
						]


try:
						if not is_flax_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						SCREAMING_SNAKE_CASE:     str									= [
						    '''FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''FlaxRoFormerForMaskedLM''',
						    '''FlaxRoFormerForMultipleChoice''',
						    '''FlaxRoFormerForQuestionAnswering''',
						    '''FlaxRoFormerForSequenceClassification''',
						    '''FlaxRoFormerForTokenClassification''',
						    '''FlaxRoFormerModel''',
						    '''FlaxRoFormerPreTrainedModel''',
						]


if TYPE_CHECKING:
						from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig
						from .tokenization_roformer import RoFormerTokenizer

						try:
												if not is_tokenizers_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_roformer_fast import RoFormerTokenizerFast

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_roformer import (
												    ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    RoFormerForCausalLM,
												    RoFormerForMaskedLM,
												    RoFormerForMultipleChoice,
												    RoFormerForQuestionAnswering,
												    RoFormerForSequenceClassification,
												    RoFormerForTokenClassification,
												    RoFormerLayer,
												    RoFormerModel,
												    RoFormerPreTrainedModel,
												    load_tf_weights_in_roformer,
												)

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_roformer import (
												    TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFRoFormerForCausalLM,
												    TFRoFormerForMaskedLM,
												    TFRoFormerForMultipleChoice,
												    TFRoFormerForQuestionAnswering,
												    TFRoFormerForSequenceClassification,
												    TFRoFormerForTokenClassification,
												    TFRoFormerLayer,
												    TFRoFormerModel,
												    TFRoFormerPreTrainedModel,
												)

						try:
												if not is_flax_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_flax_roformer import (
												    FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    FlaxRoFormerForMaskedLM,
												    FlaxRoFormerForMultipleChoice,
												    FlaxRoFormerForQuestionAnswering,
												    FlaxRoFormerForSequenceClassification,
												    FlaxRoFormerForTokenClassification,
												    FlaxRoFormerModel,
												    FlaxRoFormerPreTrainedModel,
												)


else:
						import sys

						SCREAMING_SNAKE_CASE:     Any									= _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)