


'''simple docstring'''
import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel

from ...utils import logging


UpperCAmelCase_ :     int								=      logging.get_logger(__name__)


def 			_lowercase       (				UpperCamelCase__       :					Dict,				UpperCamelCase__       :					Optional[int]			):
   __A  :		List[str]       =			nn.functional.normalize(UpperCamelCase__			)
   __A  :		Tuple       =			nn.functional.normalize(UpperCamelCase__			)
   return torch.mm(UpperCamelCase__,				normalized_text_embeds.t()			)


class 						_lowerCamelCase					(    A_				):




 '''simple docstring'''
 __lowercase				:		Optional[Any]    							=						CLIPConfig

 __lowercase				:		Optional[Any]    							=						['''CLIPEncoderLayer''']


 def __init__(      self      ,      __lowercase						):

    """simple docstring"""







    super().__init__(__lowercase						)

    __A  :		Union[str, Any]       =			CLIPVisionModel(config.vision_config						)
    __A  :		Any       =			nn.Linear(config.vision_config.hidden_size      ,      config.projection_dim      ,      bias=__lowercase						)

    __A  :		Any       =			nn.Parameter(torch.ones(17      ,      config.projection_dim						)      ,      requires_grad=__lowercase						)
    __A  :		Union[str, Any]       =			nn.Parameter(torch.ones(3      ,      config.projection_dim						)      ,      requires_grad=__lowercase						)

    __A  :		Any       =			nn.Parameter(torch.ones(17						)      ,      requires_grad=__lowercase						)
    __A  :		Optional[Any]       =			nn.Parameter(torch.ones(3						)      ,      requires_grad=__lowercase						)


 @torch.no_grad()
 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase						):

    """simple docstring"""







    __A  :		Any       =			self.vision_model(__lowercase						)[1]  # pooled_output
    __A  :		Tuple       =			self.visual_projection(__lowercase						)

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

    __A  :		int       =			[]
    __A  :		str       =			image_embeds.shape[0]
    for i in range(__lowercase						):
       __A  :		str       =			{"""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
       __A  :		Optional[Any]       =			0.0

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

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

       result.append(__lowercase						)

    __A  :		Tuple       =			[len(res['bad_concepts']						) > 0 for res in result]

    return images, has_nsfw_concepts







 @torch.no_grad()
 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase						):

    """simple docstring"""







    __A  :		Union[str, Any]       =			self.vision_model(__lowercase						)[1]  # pooled_output
    __A  :		str       =			self.visual_projection(__lowercase						)

    __A  :		Optional[Any]       =			cosine_distance(__lowercase      ,      self.special_care_embeds						)
    __A  :		Dict       =			cosine_distance(__lowercase      ,      self.concept_embeds						)

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

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

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

    return images, has_nsfw_concepts










'''simple docstring'''
from __future__ import annotations


def 			_lowercase       (				UpperCamelCase__       :					list,				UpperCamelCase__       :					int,				UpperCamelCase__       :					int,				UpperCamelCase__       :					int			):
 __A  :		Dict       =			[]
 __A     ,__A  :		Dict       =			input_list[low:mid], input_list[mid : high + 1]
 while left and right:
  result.append((left if left[0] <= right[0] else right).pop(0			)			)
 __A  :		Optional[Any]       =			result + left + right
 return input_list


def 			_lowercase       (				UpperCamelCase__       :					list			):
 if len(UpperCamelCase__			) <= 1:
  return input_list
 __A  :		Any       =			list(UpperCamelCase__			)

 # iteration for two-way merging
 __A  :		Dict       =			2
 while p <= len(UpperCamelCase__			):
  # getting low, high and middle value for merge-sort of single list
  for i in range(0,				len(UpperCamelCase__			),				UpperCamelCase__			):
   __A  :		Union[str, Any]       =			i
   __A  :		Tuple       =			i + p - 1
   __A  :		Union[str, Any]       =			(low + high + 1) // 2
   __A  :		Tuple       =			merge(UpperCamelCase__,				UpperCamelCase__,				UpperCamelCase__,				UpperCamelCase__			)
  # final merge of last two parts
  if p * 2 >= len(UpperCamelCase__			):
   __A  :		Optional[int]       =			i
   __A  :		str       =			merge(UpperCamelCase__,				0,				UpperCamelCase__,				len(UpperCamelCase__			) - 1			)
   break
  p *= 2

 return input_list


if __name__ == "__main__":
 UpperCAmelCase_ :     Any								=      input('Enter numbers separated by a comma:\n').strip()
 if user_input == "":
  UpperCAmelCase_ :     Union[str, Any]								=      []
 else:
  UpperCAmelCase_ :     str								=      [int(item.strip()) for item in user_input.split(',')]
 print(iter_merge_sort(unsorted))






