

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

from ...utils import logging


__lowerCAmelCase			     =       logging.get_logger(__name__)



def    _lowercase			(	a__						:			Optional[Any]							,  a__						:			List[Any]					)							->					Optional[Any]:





	"""simple docstring"""


	_UpperCamelCase			=   nn.functional.normalize(UpperCamelCase_					)
	_UpperCamelCase			=   nn.functional.normalize(UpperCamelCase_					)
	return torch.mm(UpperCamelCase_							,  normalized_text_embeds.t()					)




class 			lowerCamelCase_    (  A__		):
							__lowercase				:	Optional[int]        =				CLIPConfig

							__lowercase				:	Union[str, Any]        =				["CLIPEncoderLayer"]


							def __init__( self      ,				lowerCamelCase_						)							->   List[str]:



								"""simple docstring"""

								super().__init__(a__						)

								_UpperCamelCase			=   CLIPVisionModel(config.vision_config						)
								_UpperCamelCase			=   nn.Linear(config.vision_config.hidden_size      ,				config.projection_dim      ,				bias=a__						)

								_UpperCamelCase			=   nn.Parameter(torch.ones(17      ,				config.projection_dim						)      ,				requires_grad=a__						)
								_UpperCamelCase			=   nn.Parameter(torch.ones(3      ,				config.projection_dim						)      ,				requires_grad=a__						)

								_UpperCamelCase			=   nn.Parameter(torch.ones(17						)      ,				requires_grad=a__						)
								_UpperCamelCase			=   nn.Parameter(torch.ones(3						)      ,				requires_grad=a__						)


							@torch.no_grad()
							def  lowercase   ( self      ,				lowerCamelCase_      ,				lowerCamelCase_						)							->   Dict:



								"""simple docstring"""

								_UpperCamelCase			=   self.vision_model(a__						)[1]  # pooled_output
								_UpperCamelCase			=   self.visual_projection(a__						)

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

								_UpperCamelCase			=   []
								_UpperCamelCase			=   image_embeds.shape[0]
								for i in range(a__						):
									_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(a__						)

									result.append(a__						)

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

								return images, has_nsfw_concepts







							@torch.no_grad()
							def  lowercase   ( self      ,				lowerCamelCase_      ,				lowerCamelCase_						)							->   Dict:



								"""simple docstring"""

								_UpperCamelCase			=   self.vision_model(a__						)[1]  # pooled_output
								_UpperCamelCase			=   self.visual_projection(a__						)

								_UpperCamelCase			=   cosine_distance(a__      ,				self.special_care_embeds						)
								_UpperCamelCase			=   cosine_distance(a__      ,				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









import inspect
import os
import unittest
from dataclasses import dataclass

import torch

from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs
from accelerate.state import AcceleratorState
from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu
from accelerate.utils import KwargsHandler



@dataclass
class      _UpperCAmelCase      (							A__  ):
     UpperCamelCase__		     =					0
     UpperCamelCase__		     =					False
     UpperCamelCase__		     =					3.0







class      _UpperCAmelCase      (							unittest.TestCase  ):




     def 		snake_case_		(					self):
         # If no defaults are changed, `to_kwargs` returns an empty dict.
         self.assertDictEqual(MockClass().to_kwargs()			,    {})
         self.assertDictEqual(MockClass(a=2).to_kwargs()			,    {'''a''': 2})
         self.assertDictEqual(MockClass(a=2			,    b=a__).to_kwargs()			,    {'''a''': 2, '''b''': True})
         self.assertDictEqual(MockClass(a=2			,    c=2.2_5).to_kwargs()			,    {'''a''': 2, '''c''': 2.2_5})




     @require_cuda
     def 		snake_case_		(					self):
         # If no defaults are changed, `to_kwargs` returns an empty dict.
         A__					=       GradScalerKwargs(init_scale=1_0_2_4			,    growth_factor=2)
         AcceleratorState._reset_state()
         A__					=       Accelerator(mixed_precision='''fp16'''			,    kwargs_handlers=[scaler_handler])
         print(accelerator.use_fpaa)
         A__					=       accelerator.scaler

         # Check the kwargs have been applied
         self.assertEqual(scaler._init_scale			,    1_0_2_4.0)
         self.assertEqual(scaler._growth_factor			,    2.0)

         # Check the other values are at the default
         self.assertEqual(scaler._backoff_factor			,    0.5)
         self.assertEqual(scaler._growth_interval			,    2_0_0_0)
         self.assertEqual(scaler._enabled			,    a__)




     @require_multi_gpu
     def 		snake_case_		(					self):
         A__					=       ['''torchrun''', F"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__)]
         execute_subprocess_async(a__			,    env=os.environ.copy())


if __name__ == "__main__":
   _lowercase    	=		DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True)
   _lowercase    	=		Accelerator(kwargs_handlers=[ddp_scaler])
   _lowercase    	=		torch.nn.Linear(100, 200)
   _lowercase    	=		accelerator.prepare(model)

   # Check the values changed in kwargs
   _lowercase    	=		""
   _lowercase    	=		model.bucket_bytes_cap // (1024 * 1024)
   if observed_bucket_cap_map != 15:
      error_msg += F"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n"
   if model.find_unused_parameters is not True:
      error_msg += F"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n"

   # Check the values of the defaults
   if model.dim != 0:
      error_msg += F"Default value not respected, should have `0` but found {model.dim}.\n"
   if model.broadcast_buffers is not True:
      error_msg += F"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n"
   if model.gradient_as_bucket_view is not False:
      error_msg += F"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n"

   # Raise error at the end to make sure we don't stop at the first failure.
   if len(error_msg) > 0:
      raise ValueError(error_msg)
