




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

from ...utils import logging


UpperCAmelCase_		:					List[str]          =  logging.get_logger(__name__)




def 		lowerCAmelCase_ (						lowerCamelCase						,  lowerCamelCase ):
				__magic_name__						:						Tuple										=nn.functional.normalize(lowerCamelCase )
				__magic_name__						:						Optional[int]										=nn.functional.normalize(lowerCamelCase )
				return torch.mm(lowerCamelCase						,  normalized_text_embeds.t() )




class 		__A   (      UpperCamelCase__      ):
						UpperCamelCase      =       CLIPConfig

						UpperCamelCase      =       ["""CLIPEncoderLayer"""]

						def __init__(		self :Any     ,			__snake_case :CLIPConfig			):
										'''simple docstring'''

										super().__init__(__snake_case			)

										__magic_name__						:						Optional[Any]										=CLIPVisionModel(config.vision_config			)
										__magic_name__						:						Any										=nn.Linear(config.vision_config.hidden_size     ,			config.projection_dim     ,			bias=__snake_case			)

										__magic_name__						:						List[Any]										=nn.Parameter(torch.ones(17     ,			config.projection_dim			)     ,			requires_grad=__snake_case			)
										__magic_name__						:						int										=nn.Parameter(torch.ones(3     ,			config.projection_dim			)     ,			requires_grad=__snake_case			)

										__magic_name__						:						List[str]										=nn.Parameter(torch.ones(17			)     ,			requires_grad=__snake_case			)
										__magic_name__						:						Tuple										=nn.Parameter(torch.ones(3			)     ,			requires_grad=__snake_case			)

						@torch.no_grad()
						def      A__    (		self :Optional[Any]     ,			__snake_case :List[Any]     ,			__snake_case :str			):
										'''simple docstring'''

										__magic_name__						:						Union[str, Any]										=self.vision_model(__snake_case			)[1]  # pooled_output
										__magic_name__						:						Tuple										=self.visual_projection(__snake_case			)

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

										__magic_name__						:						Dict										=[]
										__magic_name__						:						Any										=image_embeds.shape[0]
										for i in range(__snake_case			):
														__magic_name__						:						Optional[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
														__magic_name__						:						Dict										=0.0

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

														for concept_idx in range(len(cos_dist[0]			)			):
																		__magic_name__						:						Any										=cos_dist[i][concept_idx]
																		__magic_name__						:						Union[str, Any]										=self.concept_embeds_weights[concept_idx].item()
																		__magic_name__						:						Optional[int]										=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			)

										__magic_name__						:						Optional[int]										=[len(res["""bad_concepts"""]			) > 0 for res in result]

										return images, has_nsfw_concepts


						@torch.no_grad()
						def      A__    (		self :Tuple     ,			__snake_case :torch.FloatTensor     ,			__snake_case :torch.FloatTensor			):
										'''simple docstring'''

										__magic_name__						:						Dict										=self.vision_model(__snake_case			)[1]  # pooled_output
										__magic_name__						:						Union[str, Any]										=self.visual_projection(__snake_case			)

										__magic_name__						:						Optional[Any]										=cosine_distance(__snake_case     ,			self.special_care_embeds			)
										__magic_name__						:						str										=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
										__magic_name__						:						str										=0.0

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

										__magic_name__						:						Optional[int]										=(cos_dist - self.concept_embeds_weights) + special_adjustment
										# concept_scores = concept_scores.round(decimals=3)
										__magic_name__						:						int										=torch.any(concept_scores > 0     ,			dim=1			)

										return images, has_nsfw_concepts










import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase_		:					Any          =  logging.get_logger(__name__)

UpperCAmelCase_		:					List[str]          =  {
    "nvidia/segformer-b0-finetuned-ade-512-512": (
        "https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json"
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}




class 		__A   (      UpperCamelCase__      ):
						UpperCamelCase      =       """segformer"""

						def __init__(		self :List[str]     ,			__snake_case :str=3     ,			__snake_case :Optional[Any]=4     ,			__snake_case :List[Any]=[2, 2, 2, 2]     ,			__snake_case :Dict=[8, 4, 2, 1]     ,			__snake_case :Optional[int]=[32, 64, 1_60, 2_56]     ,			__snake_case :Union[str, Any]=[7, 3, 3, 3]     ,			__snake_case :Optional[Any]=[4, 2, 2, 2]     ,			__snake_case :Tuple=[1, 2, 5, 8]     ,			__snake_case :List[Any]=[4, 4, 4, 4]     ,			__snake_case :Optional[Any]="gelu"     ,			__snake_case :Tuple=0.0     ,			__snake_case :Dict=0.0     ,			__snake_case :Optional[int]=0.1     ,			__snake_case :Optional[int]=0.02     ,			__snake_case :Tuple=0.1     ,			__snake_case :Union[str, Any]=1E-6     ,			__snake_case :int=2_56     ,			__snake_case :Optional[int]=2_55     ,			**__snake_case :Dict     ,			):
										'''simple docstring'''

										super().__init__(**__snake_case			)

										if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
														warnings.warn(
														    """Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be"""
														    """ removed, as the behaviour will default to that of reshape_last_stage = True."""     ,			__snake_case     ,			)

										__magic_name__						:						Dict										=num_channels
										__magic_name__						:						str										=num_encoder_blocks
										__magic_name__						:						List[Any]										=depths
										__magic_name__						:						Optional[Any]										=sr_ratios
										__magic_name__						:						List[str]										=hidden_sizes
										__magic_name__						:						List[str]										=patch_sizes
										__magic_name__						:						Any										=strides
										__magic_name__						:						Optional[Any]										=mlp_ratios
										__magic_name__						:						str										=num_attention_heads
										__magic_name__						:						int										=hidden_act
										__magic_name__						:						List[Any]										=hidden_dropout_prob
										__magic_name__						:						Optional[Any]										=attention_probs_dropout_prob
										__magic_name__						:						Optional[Any]										=classifier_dropout_prob
										__magic_name__						:						List[str]										=initializer_range
										__magic_name__						:						List[str]										=drop_path_rate
										__magic_name__						:						List[Any]										=layer_norm_eps
										__magic_name__						:						List[str]										=decoder_hidden_size
										__magic_name__						:						Union[str, Any]										=kwargs.get("""reshape_last_stage"""     ,			__snake_case			)
										__magic_name__						:						Dict										=semantic_loss_ignore_index




class 		__A   (      UpperCamelCase__      ):
						UpperCamelCase      =       version.parse("""1.11"""      )

						@property
						def      A__    (		self :List[str]			):
										'''simple docstring'''

										return OrderedDict(
										    [
										        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
										    ]			)

						@property
						def      A__    (		self :Any			):
										'''simple docstring'''

										return 1E-4


						@property
						def      A__    (		self :int			):
										'''simple docstring'''

										return 12




