



"""simple docstring"""







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

from ...utils import logging


__snake_case	:				Any  					=			logging.get_logger(__name__)



def   a_	(			__a		,							__a      ):
     A__				=   nn.functional.normalize(__a      )
     A__				=   nn.functional.normalize(__a      )
     return torch.mm(__a		,							normalized_text_embeds.t()      )

class       UpperCamelCase  (					a				):





     """simple docstring"""






     _lowerCamelCase					: List[str]				   =CLIPConfig

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





     def __init__(					self :							List[Any]	,      _lowerCamelCase :							CLIPConfig ):
          super().__init__(_lowerCamelCase )

          A__				=   CLIPVisionModel(config.vision_config )
          A__				=   nn.Linear(config.vision_config.hidden_size	,      config.projection_dim	,      bias=_lowerCamelCase )

          A__				=   nn.Parameter(torch.ones(1_7	,      config.projection_dim )	,      requires_grad=_lowerCamelCase )
          A__				=   nn.Parameter(torch.ones(3	,      config.projection_dim )	,      requires_grad=_lowerCamelCase )

          A__				=   nn.Parameter(torch.ones(1_7 )	,      requires_grad=_lowerCamelCase )
          A__				=   nn.Parameter(torch.ones(3 )	,      requires_grad=_lowerCamelCase )





     @torch.no_grad()
     def 						A__  (					self :							Union[str, Any]	,      _lowerCamelCase :							str	,      _lowerCamelCase :							List[str] ):
          A__				=   self.vision_model(_lowerCamelCase )[1]  # pooled_output
          A__				=   self.visual_projection(_lowerCamelCase )

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

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

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

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

               result.append(_lowerCamelCase )

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

          for idx, has_nsfw_concept in enumerate(_lowerCamelCase ):
               if has_nsfw_concept:
                    if torch.is_tensor(_lowerCamelCase ) or torch.is_tensor(images[0] ):
                         A__				=   torch.zeros_like(images[idx] )  # black image
                    else:
                         A__				=   np.zeros(images[idx].shape )  # black image

          if any(_lowerCamelCase ):
               logger.warning(
                   '''Potential NSFW content was detected in one or more images. A black image will be returned instead.'''
                   ''' Try again with a different prompt and/or seed.''' )

          return images, has_nsfw_concepts





     @torch.no_grad()
     def 						A__  (					self :							Any	,      _lowerCamelCase :							torch.FloatTensor	,      _lowerCamelCase :							torch.FloatTensor ):
          A__				=   self.vision_model(_lowerCamelCase )[1]  # pooled_output
          A__				=   self.visual_projection(_lowerCamelCase )

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

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

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

          A__				=   0.0  # black image

          return images, has_nsfw_concepts








"""simple docstring"""







from math import factorial



def   a_	(			__a		,							__a      ):
     return factorial(__a      ) / (factorial(__a      ) * factorial(n - r      ))



def   a_	(			):
     A__				=   0

     for i in range(1		,							101      ):
          for j in range(1		,							i + 1      ):
               if combinations(__a		,							__a      ) > 1e6:
                    total += 1
     return total


if __name__ == "__main__":
 print(solution())



