



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

from ...utils import logging


snake_case     :    Union[str, Any]						=    logging.get_logger(__name__)
def 						snake_case__							(       __lowercase				,  __lowercase							)		->		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 				lowerCAmelCase__	(						_SCREAMING_SNAKE_CASE    ):
					__A  :     List[Any]     =    CLIPConfig

					__A  :     List[str]     =    ['CLIPEncoderLayer']







					def __init__(							self   :  int      ,		_A   :  CLIPConfig):
											super().__init__(_A)

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

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

											A__						:				Tuple         =			nn.Parameter(torch.ones(17)      ,		requires_grad=_A)
											A__						:				List[Any]         =			nn.Parameter(torch.ones(3)      ,		requires_grad=_A)







					@torch.no_grad()
					def 			_lowercase      (							self   :  Optional[Any]      ,		_A   :  Optional[int]      ,		_A   :  Dict):
											A__						:				Optional[int]         =			self.vision_model(_A)[1]  # pooled_output
											A__						:				str         =			self.visual_projection(_A)

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

											A__						:				Tuple         =			[]
											A__						:				Any         =			image_embeds.shape[0]
											for i in range(_A):
																	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(_A)

																	result.append(_A)

											A__						:				List[str]         =			[len(res["bad_concepts"]) > 0 for res in result]

											for idx, has_nsfw_concept in enumerate(_A):
																	if has_nsfw_concept:
																							if torch.is_tensor(_A) 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(_A):
																	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      ,		_A   :  torch.FloatTensor      ,		_A   :  torch.FloatTensor):
											A__						:				Union[str, Any]         =			self.vision_model(_A)[1]  # pooled_output
											A__						:				str         =			self.visual_projection(_A)

											A__						:				str         =			cosine_distance(_A      ,		self.special_care_embeds)
											A__						:				List[Any]         =			cosine_distance(_A      ,		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))



