"""simple docstring"""


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

from ...utils import logging


__UpperCamelCase									=			logging.get_logger(__name__)







def 					UpperCAmelCase    (		UpperCAmelCase						,					UpperCAmelCase					)				->      int:
     snake_case_					       =					nn.functional.normalize(UpperCAmelCase					)
     snake_case_					       =					nn.functional.normalize(UpperCAmelCase					)
     return torch.mm(UpperCAmelCase						,					normalized_text_embeds.t()					)




class  UpperCamelCase					(				lowerCAmelCase__   ):
       SCREAMING_SNAKE_CASE_			=      CLIPConfig

       SCREAMING_SNAKE_CASE_			=      ["CLIPEncoderLayer"]







       def __init__( self,							lowerCAmelCase__)      ->      Optional[int]:
            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__)      ->      Tuple:
            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.01

                 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__)      ->      Optional[int]:
            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.01
            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









"""simple docstring"""





def      lowerCamelCase__       (							_lowerCamelCase       :		int		,				_lowerCamelCase       :		int )      ->				int:
  lowerCamelCase_         =						1  # To kept the Calculated Value
  # Since C(n, k) = C(n, n-k)
  if k > (n - k):
    lowerCamelCase_         =						n - k
  # Calculate C(n,k)
  for i in range(_lowerCamelCase ):
    result *= n - i
    result //= i + 1
  return result







def      lowerCamelCase__       (							_lowerCamelCase       :		int )      ->				int:
  return binomial_coefficient(2 * node_count		,				_lowerCamelCase ) // (node_count + 1)







def      lowerCamelCase__       (							_lowerCamelCase       :		int )      ->				int:
  if n < 0:
    raise ValueError('factorial() not defined for negative values' )
  lowerCamelCase_         =						1
  for i in range(1		,				n + 1 ):
    result *= i
  return result







def      lowerCamelCase__       (							_lowerCamelCase       :		int )      ->				int:
  return catalan_number(_lowerCamelCase ) * factorial(_lowerCamelCase )


if __name__ == "__main__":
  _SCREAMING_SNAKE_CASE					:				int					    =						int(input('''Enter the number of nodes: ''').strip() or 0)
  if node_count <= 0:
    raise ValueError('''We need some nodes to work with.''')
  print(
      F'''Given {node_count} nodes, there are {binary_tree_count(node_count)} '''
      F'''binary trees and {catalan_number(node_count)} binary search trees.'''
  )




