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

from ...utils import logging


__magic_name__  							=	logging.get_logger(__name__)





def 						SCREAMING_SNAKE_CASE__     (   __lowerCAmelCase     ,		__lowerCAmelCase				):
    snake_case__    =					nn.functional.normalize(__lowerCAmelCase				)
    snake_case__    =					nn.functional.normalize(__lowerCAmelCase				)
    return torch.mm(__lowerCAmelCase     ,		normalized_text_embeds.t()				)





class        _SCREAMING_SNAKE_CASE	(   __UpperCamelCase			):
      _A    :		List[Any]     						=  CLIPConfig

      _A    :		List[Any]     						=  ['CLIPEncoderLayer']
      def __init__(      self		,						lowerCamelCase			):
          super().__init__(lowerCamelCase			)

          snake_case__    =					CLIPVisionModel(config.vision_config			)
          snake_case__    =					nn.Linear(config.vision_config.hidden_size		,						config.projection_dim		,						bias=lowerCamelCase			)

          snake_case__    =					nn.Parameter(torch.ones(17		,						config.projection_dim			)		,						requires_grad=lowerCamelCase			)
          snake_case__    =					nn.Parameter(torch.ones(3		,						config.projection_dim			)		,						requires_grad=lowerCamelCase			)

          snake_case__    =					nn.Parameter(torch.ones(17			)		,						requires_grad=lowerCamelCase			)
          snake_case__    =					nn.Parameter(torch.ones(3			)		,						requires_grad=lowerCamelCase			)
      @torch.no_grad()
      def A_      (      self		,						lowerCamelCase		,						lowerCamelCase			):
          snake_case__    =					self.vision_model(lowerCamelCase			)[1]  # pooled_output
          snake_case__    =					self.visual_projection(lowerCamelCase			)

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

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

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

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

              result.append(lowerCamelCase			)

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

          return images, has_nsfw_concepts



      @torch.no_grad()
      def A_      (      self		,						lowerCamelCase		,						lowerCamelCase			):
          snake_case__    =					self.vision_model(lowerCamelCase			)[1]  # pooled_output
          snake_case__    =					self.visual_projection(lowerCamelCase			)

          snake_case__    =					cosine_distance(lowerCamelCase		,						self.special_care_embeds			)
          snake_case__    =					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
          snake_case__    =					0.0

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

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

          return images, has_nsfw_concepts





# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# 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.

# this script dumps information about the environment

import os
import sys

import transformers


__magic_name__  							=	'''3'''

print('''Python version:''', sys.version)
print('''transformers version:''', transformers.__version__)

try:
     import torch

     print('''Torch version:''', torch.__version__)
     print('''Cuda available:''', torch.cuda.is_available())
     print('''Cuda version:''', torch.version.cuda)
     print('''CuDNN version:''', torch.backends.cudnn.version())
     print('''Number of GPUs available:''', torch.cuda.device_count())
     print('''NCCL version:''', torch.cuda.nccl.version())
except ImportError:
     print('''Torch version:''', None)

try:
     import deepspeed

     print('''DeepSpeed version:''', deepspeed.__version__)
except ImportError:
     print('''DeepSpeed version:''', None)

try:
     import tensorflow as tf

     print('''TensorFlow version:''', tf.__version__)
     print('''TF GPUs available:''', bool(tf.config.list_physical_devices('''GPU''')))
     print('''Number of TF GPUs available:''', len(tf.config.list_physical_devices('''GPU''')))
except ImportError:
     print('''TensorFlow version:''', None)



