




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

from ...utils import logging


__A								=       logging.get_logger(__name__)







def      lowerCamelCase_						(			UpperCamelCase__							: str					,	UpperCamelCase__							: List[str] )	->			List[Any]:





				"""simple docstring"""


				__lowerCamelCase									=       nn.functional.normalize(UpperCamelCase__ )
				__lowerCamelCase									=       nn.functional.normalize(UpperCamelCase__ )
				return torch.mm(UpperCamelCase__					,	normalized_text_embeds.t() )







class        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







			snake_case_  						=			CLIPConfig

			snake_case_  						=			['''CLIPEncoderLayer''']


			def __init__( self     ,		lowerCamelCase__				)       -> Dict:



							'''simple docstring'''
							super().__init__(lowerCamelCase__				)

							__lowerCamelCase									=       CLIPVisionModel(config.vision_config				)
							__lowerCamelCase									=       nn.Linear(config.vision_config.hidden_size     ,		config.projection_dim     ,		bias=lowerCamelCase__				)

							__lowerCamelCase									=       nn.Parameter(torch.ones(17     ,		config.projection_dim				)     ,		requires_grad=lowerCamelCase__				)
							__lowerCamelCase									=       nn.Parameter(torch.ones(3     ,		config.projection_dim				)     ,		requires_grad=lowerCamelCase__				)

							__lowerCamelCase									=       nn.Parameter(torch.ones(17				)     ,		requires_grad=lowerCamelCase__				)
							__lowerCamelCase									=       nn.Parameter(torch.ones(3				)     ,		requires_grad=lowerCamelCase__				)


			@torch.no_grad()
			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> Union[str, Any]:



							'''simple docstring'''
							__lowerCamelCase									=       self.vision_model(lowerCamelCase__				)[1]  # pooled_output
							__lowerCamelCase									=       self.visual_projection(lowerCamelCase__				)

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

							__lowerCamelCase									=       []
							__lowerCamelCase									=       image_embeds.shape[0]
							for i in range(lowerCamelCase__				):
											__lowerCamelCase									=       {'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									=       0.0

											for concept_idx in range(len(special_cos_dist[0]				)				):
															__lowerCamelCase									=       special_cos_dist[i][concept_idx]
															__lowerCamelCase									=       self.special_care_embeds_weights[concept_idx].item()
															__lowerCamelCase									=       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									=       0.01

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

											result.append(lowerCamelCase__				)

							__lowerCamelCase									=       [len(res['bad_concepts']				) > 0 for res in result]

							return images, has_nsfw_concepts






			@torch.no_grad()
			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> Tuple:



							'''simple docstring'''
							__lowerCamelCase									=       self.vision_model(lowerCamelCase__				)[1]  # pooled_output
							__lowerCamelCase									=       self.visual_projection(lowerCamelCase__				)

							__lowerCamelCase									=       cosine_distance(lowerCamelCase__     ,		self.special_care_embeds				)
							__lowerCamelCase									=       cosine_distance(lowerCamelCase__     ,		self.concept_embeds				)

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

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

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

							return images, has_nsfw_concepts








# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from argparse import ArgumentParser

from accelerate.commands.config import get_config_parser
from accelerate.commands.env import env_command_parser
from accelerate.commands.launch import launch_command_parser
from accelerate.commands.test import test_command_parser
from accelerate.commands.tpu import tpu_command_parser







def      lowerCamelCase_						(			)	->			Union[str, Any]:





				"""simple docstring"""


				__lowerCamelCase									=       ArgumentParser('Accelerate CLI tool'					,	usage='accelerate <command> [<args>]'					,	allow_abbrev=UpperCamelCase__ )
				__lowerCamelCase									=       parser.add_subparsers(help='accelerate command helpers' )

				# Register commands
				get_config_parser(subparsers=UpperCamelCase__ )
				env_command_parser(subparsers=UpperCamelCase__ )
				launch_command_parser(subparsers=UpperCamelCase__ )
				tpu_command_parser(subparsers=UpperCamelCase__ )
				test_command_parser(subparsers=UpperCamelCase__ )

				# Let's go
				__lowerCamelCase									=       parser.parse_args()

				if not hasattr(UpperCamelCase__					,	'func' ):
								parser.print_help()
								exit(1 )

				# Run
				args.func(UpperCamelCase__ )


if __name__ == "__main__":
					main()


