





import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
     import torch_xla.core.xla_model as xm
     import torch_xla.debug.metrics as met

class   A							(						__lowercase    ):



      def __init__(						self:    int		,	*_lowerCAmelCase:    List[Any]		,	_lowerCAmelCase:    Any=None		,	_lowerCAmelCase:    Union[str, Any]=None		,	**_lowerCAmelCase:    Any							)    ->     List[str]:
         '''simple docstring'''

         super().__init__(*_lowerCAmelCase		,	**_lowerCAmelCase							)
         UpperCAmelCase_           =eval_examples
         UpperCAmelCase_           =post_process_function



      def 						lowerCAmelCase__     (						self:    str		,	_lowerCAmelCase:    Optional[Dataset] = None		,	_lowerCAmelCase:    Dict=None		,	_lowerCAmelCase:    Optional[List[str]] = None		,	_lowerCAmelCase:    str = "eval"		,	**_lowerCAmelCase:    Any		,	)    ->     Dict[str, float]:
         '''simple docstring'''

         UpperCAmelCase_           =gen_kwargs.copy()
         UpperCAmelCase_           =(
             gen_kwargs["max_length"] if gen_kwargs.get("max_length"							) is not None else self.args.generation_max_length
         )
         UpperCAmelCase_           =(
             gen_kwargs["num_beams"] if gen_kwargs.get("num_beams"							) is not None else self.args.generation_num_beams
         )
         UpperCAmelCase_           =gen_kwargs

         UpperCAmelCase_           =self.eval_dataset if eval_dataset is None else eval_dataset
         UpperCAmelCase_           =self.get_eval_dataloader(_lowerCAmelCase							)
         UpperCAmelCase_           =self.eval_examples if eval_examples is None else eval_examples

         # Temporarily disable metric computation, we will do it in the loop here.
         UpperCAmelCase_           =self.compute_metrics
         UpperCAmelCase_           =None
         UpperCAmelCase_           =time.time()
         UpperCAmelCase_           =self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
         try:
            UpperCAmelCase_           =eval_loop(
                _lowerCAmelCase		,	description="Evaluation"		,	prediction_loss_only=True if compute_metrics is None else None		,	ignore_keys=_lowerCAmelCase		,	metric_key_prefix=_lowerCAmelCase		,	)
         finally:
            UpperCAmelCase_           =compute_metrics
         UpperCAmelCase_           =self.args.eval_batch_size * self.args.world_size
         if F'{metric_key_prefix}_jit_compilation_time' in output.metrics:
            start_time += output.metrics[F'{metric_key_prefix}_jit_compilation_time']
         output.metrics.update(
             speed_metrics(
                 _lowerCAmelCase		,	_lowerCAmelCase		,	num_samples=output.num_samples		,	num_steps=math.ceil(output.num_samples / total_batch_size							)		,	)							)

         if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
            # Only the main node write the results by default
            UpperCAmelCase_           =self.post_process_function(_lowerCAmelCase		,	_lowerCAmelCase		,	_lowerCAmelCase							)
            UpperCAmelCase_           =self.compute_metrics(_lowerCAmelCase							)

            # Prefix all keys with metric_key_prefix + '_'
            for key in list(metrics.keys()							):
               if not key.startswith(F'{metric_key_prefix}_'							):
                  UpperCAmelCase_           =metrics.pop(_lowerCAmelCase							)

            metrics.update(output.metrics							)
         else:
            UpperCAmelCase_           =output.metrics

         if self.args.should_log:
            # Only the main node log the results by default
            self.log(_lowerCAmelCase							)

         if self.args.tpu_metrics_debug or self.args.debug:
            # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
            xm.master_print(met.metrics_report()							)

         UpperCAmelCase_           =self.callback_handler.on_evaluate(self.args		,	self.state		,	self.control		,	_lowerCAmelCase							)
         return metrics






      def 						lowerCAmelCase__     (						self:    Union[str, Any]		,	_lowerCAmelCase:    Tuple		,	_lowerCAmelCase:    Optional[int]		,	_lowerCAmelCase:    Tuple=None		,	_lowerCAmelCase:    str = "test"		,	**_lowerCAmelCase:    Tuple							)    ->     Any:
         '''simple docstring'''

         UpperCAmelCase_           =gen_kwargs.copy()

         UpperCAmelCase_           =self.get_test_dataloader(_lowerCAmelCase							)

         # Temporarily disable metric computation, we will do it in the loop here.
         UpperCAmelCase_           =self.compute_metrics
         UpperCAmelCase_           =None
         UpperCAmelCase_           =time.time()
         UpperCAmelCase_           =self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
         try:
            UpperCAmelCase_           =eval_loop(
                _lowerCAmelCase		,	description="Prediction"		,	prediction_loss_only=True if compute_metrics is None else None		,	ignore_keys=_lowerCAmelCase		,	metric_key_prefix=_lowerCAmelCase		,	)
         finally:
            UpperCAmelCase_           =compute_metrics

         UpperCAmelCase_           =self.args.eval_batch_size * self.args.world_size
         if F'{metric_key_prefix}_jit_compilation_time' in output.metrics:
            start_time += output.metrics[F'{metric_key_prefix}_jit_compilation_time']
         output.metrics.update(
             speed_metrics(
                 _lowerCAmelCase		,	_lowerCAmelCase		,	num_samples=output.num_samples		,	num_steps=math.ceil(output.num_samples / total_batch_size							)		,	)							)
         if self.post_process_function is None or self.compute_metrics is None:
            return output

         UpperCAmelCase_           =self.post_process_function(_lowerCAmelCase		,	_lowerCAmelCase		,	_lowerCAmelCase		,	"predict"							)
         UpperCAmelCase_           =self.compute_metrics(_lowerCAmelCase							)

         # Prefix all keys with metric_key_prefix + '_'
         for key in list(metrics.keys()							):
            if not key.startswith(F'{metric_key_prefix}_'							):
               UpperCAmelCase_           =metrics.pop(_lowerCAmelCase							)
         metrics.update(output.metrics							)
         return PredictionOutput(predictions=predictions.predictions		,	label_ids=predictions.label_ids		,	metrics=_lowerCAmelCase							)






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

from ...utils import logging


UpperCamelCase_						:     int	       =							logging.get_logger(__name__)



def 		UpperCamelCase     (       _UpperCAmelCase  :   Optional[int] ,			_UpperCAmelCase  :   Dict  )   ->			Any:






						'''simple docstring'''

						_lowercase       :	List[Any]           =   nn.functional.normalize(_UpperCAmelCase  )
						_lowercase       :	List[str]           =   nn.functional.normalize(_UpperCAmelCase  )
						return torch.mm(_UpperCAmelCase ,			normalized_text_embeds.t()  )




class   __lowercase						(						__snake_case       ):
	_A       =   CLIPConfig

	_A       =   ["CLIPEncoderLayer"]






	def __init__(self				: List[str]      ,	snake_case				: CLIPConfig    ) ->			Optional[int]:
							super().__init__(snake_case    )

							_lowercase       :	Tuple           =   CLIPVisionModel(config.vision_config    )
							_lowercase       :	Any           =   nn.Linear(config.vision_config.hidden_size      ,	config.projection_dim      ,	bias=snake_case    )

							_lowercase       :	int           =   nn.Parameter(torch.ones(17      ,	config.projection_dim    )      ,	requires_grad=snake_case    )
							_lowercase       :	str           =   nn.Parameter(torch.ones(3      ,	config.projection_dim    )      ,	requires_grad=snake_case    )

							_lowercase       :	Any           =   nn.Parameter(torch.ones(17    )      ,	requires_grad=snake_case    )
							_lowercase       :	Dict           =   nn.Parameter(torch.ones(3    )      ,	requires_grad=snake_case    )






	@torch.no_grad()
	def       _a(self				: int      ,	snake_case				: Tuple      ,	snake_case				: Optional[Any]    ) ->			Union[str, Any]:
							_lowercase       :	str           =   self.vision_model(snake_case    )[1]  # pooled_output
							_lowercase       :	List[Any]           =   self.visual_projection(snake_case    )

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

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

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

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

							_lowercase       :	Dict           =   [len(res["bad_concepts"]    ) > 0 for res in result]

							return images, has_nsfw_concepts






	@torch.no_grad()
	def       _a(self				: str      ,	snake_case				: torch.FloatTensor      ,	snake_case				: torch.FloatTensor    ) ->			List[str]:
							_lowercase       :	Dict           =   self.vision_model(snake_case    )[1]  # pooled_output
							_lowercase       :	Any           =   self.visual_projection(snake_case    )

							_lowercase       :	Optional[Any]           =   cosine_distance(snake_case      ,	self.special_care_embeds    )
							_lowercase       :	Optional[Any]           =   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
							_lowercase       :	Optional[int]           =   0.0

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

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

							return images, has_nsfw_concepts




