





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

from ...utils import logging


snake_case_							=			logging.get_logger(__name__)


def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)   ->     Optional[int]:
		lowerCamelCase   : Optional[int]								=nn.functional.normalize(SCREAMING_SNAKE_CASE_		)
		lowerCamelCase   : int								=nn.functional.normalize(SCREAMING_SNAKE_CASE_		)
		return torch.mm(SCREAMING_SNAKE_CASE_							,       normalized_text_embeds.t()		)



class        snake_case_		(			_A):
		lowerCamelCase    :Dict						=     CLIPConfig

		lowerCamelCase    :Tuple						=     ["CLIPEncoderLayer"]







		def __init__(		self			,     __lowercase   )				->   Any:
				super().__init__(__lowercase   )

				lowerCamelCase   : int								=CLIPVisionModel(config.vision_config   )
				lowerCamelCase   : Optional[int]								=nn.Linear(config.vision_config.hidden_size			,     config.projection_dim			,     bias=__lowercase   )

				lowerCamelCase   : str								=nn.Parameter(torch.ones(1_7			,     config.projection_dim   )			,     requires_grad=__lowercase   )
				lowerCamelCase   : Optional[int]								=nn.Parameter(torch.ones(3			,     config.projection_dim   )			,     requires_grad=__lowercase   )

				lowerCamelCase   : Optional[int]								=nn.Parameter(torch.ones(1_7   )			,     requires_grad=__lowercase   )
				lowerCamelCase   : str								=nn.Parameter(torch.ones(3   )			,     requires_grad=__lowercase   )







		@torch.no_grad()
		def 			__lowercase   (		self			,     __lowercase			,     __lowercase   )				->   List[Any]:
				lowerCamelCase   : Union[str, Any]								=self.vision_model(__lowercase   )[1]  # pooled_output
				lowerCamelCase   : Optional[Any]								=self.visual_projection(__lowercase   )

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

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

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

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

						result.append(__lowercase   )

				lowerCamelCase   : Any								=[len(res['''bad_concepts''']   ) > 0 for res in result]

				return images, has_nsfw_concepts







		@torch.no_grad()
		def 			__lowercase   (		self			,     __lowercase			,     __lowercase   )				->   List[str]:
				lowerCamelCase   : Optional[Any]								=self.vision_model(__lowercase   )[1]  # pooled_output
				lowerCamelCase   : List[str]								=self.visual_projection(__lowercase   )

				lowerCamelCase   : Any								=cosine_distance(__lowercase			,     self.special_care_embeds   )
				lowerCamelCase   : Any								=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
				lowerCamelCase   : List[str]								=0.0

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

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

				return images, has_nsfw_concepts








import math
import random
from typing import Any

from .hill_climbing import SearchProblem


def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_ = True							,       SCREAMING_SNAKE_CASE_ = math.inf							,       SCREAMING_SNAKE_CASE_ = -math.inf							,       SCREAMING_SNAKE_CASE_ = math.inf							,       SCREAMING_SNAKE_CASE_ = -math.inf							,       SCREAMING_SNAKE_CASE_ = False							,       SCREAMING_SNAKE_CASE_ = 1_0_0							,       SCREAMING_SNAKE_CASE_ = 0.0_1							,       SCREAMING_SNAKE_CASE_ = 1							,       )   ->     Any:
		lowerCamelCase   : Union[str, Any]								=False
		lowerCamelCase   : int								=search_prob
		lowerCamelCase   : int								=start_temperate
		lowerCamelCase   : Any								=[]
		lowerCamelCase   : Any								=0
		lowerCamelCase   : Union[str, Any]								=None

		while not search_end:
				lowerCamelCase   : Dict								=current_state.score()
				if best_state is None or current_score > best_state.score():
						lowerCamelCase   : Tuple								=current_state
				scores.append(SCREAMING_SNAKE_CASE_		)
				iterations += 1
				lowerCamelCase   : Union[str, Any]								=None
				lowerCamelCase   : int								=current_state.get_neighbors()
				while (
				    next_state is None and neighbors
				):  # till we do not find a neighbor that we can move to
						lowerCamelCase   : Union[str, Any]								=random.randint(0							,       len(SCREAMING_SNAKE_CASE_		) - 1		)  # picking a random neighbor
						lowerCamelCase   : List[str]								=neighbors.pop(SCREAMING_SNAKE_CASE_		)
						lowerCamelCase   : Any								=picked_neighbor.score() - current_score

						if (
						    picked_neighbor.x > max_x
						    or picked_neighbor.x < min_x
						    or picked_neighbor.y > max_y
						    or picked_neighbor.y < min_y
						):
								continue  # neighbor outside our bounds

						if not find_max:
								lowerCamelCase   : Dict								=change * -1  # in case we are finding minimum
						if change > 0:  # improves the solution
								lowerCamelCase   : Optional[int]								=picked_neighbor
						else:
								lowerCamelCase   : Any								=(math.e) ** (
								    change / current_temp
								)  # probability generation function
								if random.random() < probability:  # random number within probability
										lowerCamelCase   : Union[str, Any]								=picked_neighbor
				lowerCamelCase   : Union[str, Any]								=current_temp - (current_temp * rate_of_decrease)

				if current_temp < threshold_temp or next_state is None:
						# temperature below threshold, or could not find a suitable neighbor
						lowerCamelCase   : Optional[int]								=True
				else:
						lowerCamelCase   : List[str]								=next_state

		if visualization:
				from matplotlib import pyplot as plt

				plt.plot(range(SCREAMING_SNAKE_CASE_		)							,       SCREAMING_SNAKE_CASE_		)
				plt.xlabel('''Iterations'''		)
				plt.ylabel('''Function values'''		)
				plt.show()
		return best_state


if __name__ == "__main__":


				def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)   ->     List[str]:
						return (x**2) + (y**2)

				# starting the problem with initial coordinates (12, 47)
				snake_case_							=			SearchProblem(x=1_2, y=4_7, step_size=1, function_to_optimize=test_fa)
				snake_case_							=			simulated_annealing(
				    prob, find_max=False, max_x=1_0_0, min_x=5, max_y=5_0, min_y=-5, visualization=True
				)
				print(
				    '''The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 '''
				    F"""and 50 > y > - 5 found via hill climbing: {local_min.score()}"""
				)

				# starting the problem with initial coordinates (12, 47)
				snake_case_							=			SearchProblem(x=1_2, y=4_7, step_size=1, function_to_optimize=test_fa)
				snake_case_							=			simulated_annealing(
				    prob, find_max=True, max_x=1_0_0, min_x=5, max_y=5_0, min_y=-5, visualization=True
				)
				print(
				    '''The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 '''
				    F"""and 50 > y > - 5 found via hill climbing: {local_min.score()}"""
				)


				def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)   ->     Tuple:
						return (3 * x**2) - (6 * y)

				snake_case_							=			SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
				snake_case_							=			simulated_annealing(prob, find_max=False, visualization=True)
				print(
				    '''The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: '''
				    F"""{local_min.score()}"""
				)

				snake_case_							=			SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
				snake_case_							=			simulated_annealing(prob, find_max=True, visualization=True)



				print(
				    '''The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: '''
				    F"""{local_min.score()}"""
				)

