


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

from ...utils import logging


a : str		=				logging.get_logger(__name__)


def 		lowercase_  (     _UpperCamelCase       ,			_UpperCamelCase  ):


			'''simple docstring'''







			__lowercase 					=   nn.functional.normalize(_UpperCamelCase  )
			__lowercase 					=   nn.functional.normalize(_UpperCamelCase  )
			return torch.mm(_UpperCamelCase       ,			normalized_text_embeds.t()  )





class 	lowerCamelCase_       (				lowerCAmelCase__				):



					'''simple docstring'''







					__UpperCAmelCase            =				CLIPConfig

					__UpperCAmelCase            =				["CLIPEncoderLayer"]






					def __init__(				self ,       snake_case_						)   ->			Optional[int]:







								'''simple docstring'''

								super().__init__(snake_case_						)

								__lowercase 					=   CLIPVisionModel(config.vision_config						)
								__lowercase 					=   nn.Linear(config.vision_config.hidden_size ,       config.projection_dim ,       bias=snake_case_						)

								__lowercase 					=   nn.Parameter(torch.ones(1_7 ,       config.projection_dim						) ,       requires_grad=snake_case_						)
								__lowercase 					=   nn.Parameter(torch.ones(3 ,       config.projection_dim						) ,       requires_grad=snake_case_						)

								__lowercase 					=   nn.Parameter(torch.ones(1_7						) ,       requires_grad=snake_case_						)
								__lowercase 					=   nn.Parameter(torch.ones(3						) ,       requires_grad=snake_case_						)






					@torch.no_grad()
					def A      (				self ,       snake_case_ ,       snake_case_						)   ->			List[str]:







								'''simple docstring'''

								__lowercase 					=   self.vision_model(snake_case_						)[1]  # pooled_output
								__lowercase 					=   self.visual_projection(snake_case_						)

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

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

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

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

											result.append(snake_case_						)

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

								return images, has_nsfw_concepts






					@torch.no_grad()
					def A      (				self ,       snake_case_ ,       snake_case_						)   ->			Optional[int]:







								'''simple docstring'''

								__lowercase 					=   self.vision_model(snake_case_						)[1]  # pooled_output
								__lowercase 					=   self.visual_projection(snake_case_						)

								__lowercase 					=   cosine_distance(snake_case_ ,       self.special_care_embeds						)
								__lowercase 					=   cosine_distance(snake_case_ ,       self.concept_embeds						)

								# increase this value to create a stronger `nsfw` filter
								# at the cost of increasing the possibility of filtering benign images
								__lowercase 					=   0.0

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

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

								return images, has_nsfw_concepts






from itertools import count


def 		lowercase_  (     _UpperCamelCase = 50  ):


			'''simple docstring'''







			__lowercase 					=   [1] * min_block_length

			for n in count(_UpperCamelCase  ):
						fill_count_functions.append(1  )

						for block_length in range(_UpperCamelCase       ,			n + 1  ):
									for block_start in range(n - block_length  ):
												fill_count_functions[n] += fill_count_functions[
												    n - block_start - block_length - 1
												]

									fill_count_functions[n] += 1

						if fill_count_functions[n] > 1_00_00_00:
									break

			return n


if __name__ == "__main__":
	print(f'''{solution() = }''')


