"""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 				SCREAMING_SNAKE_CASE__	(   __A = 1_000_000	)       ->	int:
	_snake_case   						=		limit + 1
	_snake_case   						=		[0] * limit
	for first_term in range(1			,				__A	):
		for n in range(__A			,				__A			,				__A	):
			_snake_case   						=		first_term + n / first_term
			if common_difference % 4:  # d must be divisble by 4
				continue
			else:
				common_difference /= 4
				if (
				    first_term > common_difference
				    and first_term < 4 * common_difference
				):  # since x,y,z are positive integers
					frequency[n] += 1  # so z>0 and a>d ,also 4d<a

	_snake_case   						=		sum(1 for x in frequency[1:limit] if x == 10	)

	return count


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

