




"""simple docstring"""



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

from ...utils import logging


SCREAMING_SNAKE_CASE    =	logging.get_logger(__name__)







def   _SCREAMING_SNAKE_CASE       (							lowercase_      ,			lowercase_						)						->		str:
 A__	=    nn.functional.normalize(lowercase_						)
 A__	=    nn.functional.normalize(lowercase_						)
 return torch.mm(lowercase_      ,			normalized_text_embeds.t()						)






class      UpperCAmelCase_  (						A_							):
    lowercase__						 =				CLIPConfig

    lowercase__						 =				['''CLIPEncoderLayer''']




    def __init__(   self						:	Optional[Any]     ,			snake_case_						:	CLIPConfig				)				->							Tuple:




     '''simple docstring'''

     super().__init__(snake_case_				)

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

     A__	=    nn.Parameter(torch.ones(17     ,			config.projection_dim				)     ,			requires_grad=snake_case_				)
     A__	=    nn.Parameter(torch.ones(3     ,			config.projection_dim				)     ,			requires_grad=snake_case_				)

     A__	=    nn.Parameter(torch.ones(17				)     ,			requires_grad=snake_case_				)
     A__	=    nn.Parameter(torch.ones(3				)     ,			requires_grad=snake_case_				)




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




     '''simple docstring'''

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

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

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

      for concept_idx in range(len(special_cos_dist[0]				)				):
       A__	=    special_cos_dist[i][concept_idx]
       A__	=    self.special_care_embeds_weights[concept_idx].item()
       A__	=    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__	=    0.01

      for concept_idx in range(len(cos_dist[0]				)				):
       A__	=    cos_dist[i][concept_idx]
       A__	=    self.concept_embeds_weights[concept_idx].item()
       A__	=    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_				)

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

     return images, has_nsfw_concepts




    @torch.no_grad()
    def 							__magic_name__							(   self						:	Optional[Any]     ,			snake_case_						:	torch.FloatTensor     ,			snake_case_						:	torch.FloatTensor				)				->							Tuple:




     '''simple docstring'''

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

     A__	=    cosine_distance(snake_case_     ,			self.special_care_embeds				)
     A__	=    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
     A__	=    0.0

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

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

     return images, has_nsfw_concepts











"""simple docstring"""



import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
      from transformers import TvltImageProcessor

if is_speech_available():
      from transformers import TvltFeatureExtractor

from transformers import TvltProcessor




@require_torch
class      UpperCAmelCase_  (						unittest.TestCase							):




    def 							__magic_name__							(   self						:	str				)				->							Dict:




     '''simple docstring'''

     A__	=    "ZinengTang/tvlt-base"
     A__	=    tempfile.mkdtemp()




    def 							__magic_name__							(   self						:	int     ,			**snake_case_						:	Dict				)				->							Union[str, Any]:




     '''simple docstring'''

     return TvltImageProcessor.from_pretrained(self.checkpoint     ,			**snake_case_				)




    def 							__magic_name__							(   self						:	Optional[int]     ,			**snake_case_						:	str				)				->							List[str]:




     '''simple docstring'''

     return TvltFeatureExtractor.from_pretrained(self.checkpoint     ,			**snake_case_				)




    def 							__magic_name__							(   self						:	Optional[int]				)				->							str:




     '''simple docstring'''

     shutil.rmtree(self.tmpdirname				)




    def 							__magic_name__							(   self						:	Tuple				)				->							Any:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)
     processor.save_pretrained(self.tmpdirname				)
     A__	=    TvltProcessor.from_pretrained(self.tmpdirname				)

     self.assertIsInstance(processor.feature_extractor     ,			snake_case_				)
     self.assertIsInstance(processor.image_processor     ,			snake_case_				)




    def 							__magic_name__							(   self						:	Optional[Any]				)				->							Union[str, Any]:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     A__	=    np.ones([12_000]				)

     A__	=    feature_extractor(snake_case_     ,			return_tensors="np"				)
     A__	=    processor(audio=snake_case_     ,			return_tensors="np"				)

     for key in audio_dict.keys():
      self.assertAlmostEqual(audio_dict[key].sum()     ,			input_processor[key].sum()     ,			delta=1e-2				)




    def 							__magic_name__							(   self						:	Tuple				)				->							Tuple:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     A__	=    np.ones([3, 224, 224]				)

     A__	=    image_processor(snake_case_     ,			return_tensors="np"				)
     A__	=    processor(images=snake_case_     ,			return_tensors="np"				)

     for key in image_dict.keys():
      self.assertAlmostEqual(image_dict[key].sum()     ,			input_processor[key].sum()     ,			delta=1e-2				)




    def 							__magic_name__							(   self						:	Tuple				)				->							Optional[int]:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     A__	=    np.ones([12_000]				)
     A__	=    np.ones([3, 224, 224]				)

     A__	=    processor(audio=snake_case_     ,			images=snake_case_				)

     self.assertListEqual(list(inputs.keys()				)     ,			["audio_values", "audio_mask", "pixel_values", "pixel_mask"]				)

     # test if it raises when no input is passed
     with pytest.raises(snake_case_				):
      processor()




    def 							__magic_name__							(   self						:	Optional[Any]				)				->							Tuple:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     self.assertListEqual(
         processor.model_input_names     ,			image_processor.model_input_names + feature_extractor.model_input_names     ,			msg="`processor` and `image_processor`+`feature_extractor` model input names do not match"     ,			)





