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

from ...utils import logging


A__ :		Union[str, Any]					=logging.get_logger(__name__)







def 							A_  (    __SCREAMING_SNAKE_CASE    :	Any      ,  __SCREAMING_SNAKE_CASE    :	List[str]			)					->							Dict:





  """simple docstring"""

  __A		:  Any       				=		nn.functional.normalize(__SCREAMING_SNAKE_CASE			)
  __A		:  Any       				=		nn.functional.normalize(__SCREAMING_SNAKE_CASE			)
  return torch.mm(__SCREAMING_SNAKE_CASE      ,  normalized_text_embeds.t()			)







class 			__A					(			_SCREAMING_SNAKE_CASE      ):
 lowerCamelCase												=CLIPConfig

 lowerCamelCase												=['''CLIPEncoderLayer''']
 def __init__( self      :     int					, lowerCamelCase      :     CLIPConfig							):



   """simple docstring"""





   super().__init__(lowerCamelCase							)

   __A		:  Any       				=		CLIPVisionModel(config.vision_config							)
   __A		:  Optional[Any]       				=		nn.Linear(config.vision_config.hidden_size					, config.projection_dim					, bias=lowerCamelCase							)

   __A		:  Optional[int]       				=		nn.Parameter(torch.ones(17					, config.projection_dim							)					, requires_grad=lowerCamelCase							)
   __A		:  Union[str, Any]       				=		nn.Parameter(torch.ones(3					, config.projection_dim							)					, requires_grad=lowerCamelCase							)

   __A		:  Tuple       				=		nn.Parameter(torch.ones(17							)					, requires_grad=lowerCamelCase							)
   __A		:  List[Any]       				=		nn.Parameter(torch.ones(3							)					, requires_grad=lowerCamelCase							)
 @torch.no_grad()
 def 		lowercase_( self      :     Optional[Any]					, lowerCamelCase      :     Optional[int]					, lowerCamelCase      :     Dict							):



   """simple docstring"""





   __A		:  Optional[int]       				=		self.vision_model(lowerCamelCase							)[1]  # pooled_output
   __A		:  str       				=		self.visual_projection(lowerCamelCase							)

   # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
   __A		:  Union[str, Any]       				=		cosine_distance(lowerCamelCase					, self.special_care_embeds							).cpu().float().numpy()
   __A		:  Dict       				=		cosine_distance(lowerCamelCase					, self.concept_embeds							).cpu().float().numpy()

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

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

     for concept_idx in range(len(cos_dist[0]							)							):
       __A		:  Union[str, Any]       				=		cos_dist[i][concept_idx]
       __A		:  List[Any]       				=		self.concept_embeds_weights[concept_idx].item()
       __A		:  str       				=		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		:  List[str]       				=		[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		:  Tuple       				=		torch.zeros_like(images[idx]							)  # black image
       else:
         __A		:  List[Any]       				=		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 		lowercase_( self      :     int					, lowerCamelCase      :     torch.FloatTensor					, lowerCamelCase      :     torch.FloatTensor							):



   """simple docstring"""





   __A		:  Union[str, Any]       				=		self.vision_model(lowerCamelCase							)[1]  # pooled_output
   __A		:  str       				=		self.visual_projection(lowerCamelCase							)

   __A		:  str       				=		cosine_distance(lowerCamelCase					, self.special_care_embeds							)
   __A		:  List[Any]       				=		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		:  Tuple       				=		0.0

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

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

   __A		:  Optional[Any]       				=		0.0  # black image

   return images, has_nsfw_concepts





'''simple docstring'''
from __future__ import annotations







def 							A_  (    __SCREAMING_SNAKE_CASE    :	float | int      ,  __SCREAMING_SNAKE_CASE    :	float | int      ,  __SCREAMING_SNAKE_CASE    :	float | int      ,  )					->							list[float | int]:





  """simple docstring"""

  if not all((nth_term, start_term_a, common_ratio_r)			):
    return []
  __A		:  list[float | int]       				=		[]
  __A		:  Union[str, Any]       				=		1
  __A		:  List[str]       				=		common_ratio_r
  for _ in range(int(__SCREAMING_SNAKE_CASE			)			):
    if not series:
      series.append(__SCREAMING_SNAKE_CASE			)
    else:
      power += 1
      series.append(float(start_term_a * multiple			)			)
      __A		:  List[str]       				=		pow(float(__SCREAMING_SNAKE_CASE			)      ,  __SCREAMING_SNAKE_CASE			)
  return series


if __name__ == "__main__":
     import doctest

     doctest.testmod()

     A__ :		Optional[int]					=float(input('Enter the last number (n term) of the Geometric Series'))
     A__ :		List[Any]					=float(input('Enter the starting term (a) of the Geometric Series'))
     A__ :		List[Any]					=float(
         input('Enter the common ratio between two terms (r) of the Geometric Series')
     )
     print('Formula of Geometric Series => a + ar + ar^2 ... +ar^n')
     print(geometric_series(nth_term, start_term_a, common_ratio_r))



