


"""simple docstring"""







import copy
import inspect

from transformers.testing_utils import require_torch, torch_device
from transformers.utils.backbone_utils import BackboneType






@require_torch
class  __A							:






 """simple docstring"""




 __lowerCAmelCase					   =    ()
 __lowerCAmelCase					   =    True


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	Optional[int]:
     a											=self.config_class

     # test default config
     a											=config_class()
     self.assertIsNotNone(__A		)
     a											=['''stem'''] + [f'''stage{idx}''' for idx in range(1					,       len(config.depths		) + 1		)]
     self.assertEqual(config.stage_names					,       __A		)
     self.assertTrue(set(config.out_features		).issubset(set(config.stage_names		)		)		)

     # Test out_features and out_indices are correctly set
     # out_features and out_indices both None
     a											=config_class(out_features=__A					,       out_indices=__A		)
     self.assertEqual(config.out_features					,       [config.stage_names[-1]]		)
     self.assertEqual(config.out_indices					,       [len(config.stage_names		) - 1]		)

     # out_features and out_indices both set
     a											=config_class(out_features=['''stem''', '''stage1''']					,       out_indices=[0, 1]		)
     self.assertEqual(config.out_features					,       ['''stem''', '''stage1''']		)
     self.assertEqual(config.out_indices					,       [0, 1]		)

     # Only out_features set
     a											=config_class(out_features=['''stage1''', '''stage3''']		)
     self.assertEqual(config.out_features					,       ['''stage1''', '''stage3''']		)
     self.assertEqual(config.out_indices					,       [1, 3]		)

     # Only out_indices set
     a											=config_class(out_indices=[0, 2]		)
     self.assertEqual(config.out_features					,       [config.stage_names[0], config.stage_names[2]]		)
     self.assertEqual(config.out_indices					,       [0, 2]		)

     # Error raised when out_indices do not correspond to out_features
     with self.assertRaises(__A		):
         a											=config_class(out_features=['''stage1''', '''stage2''']					,       out_indices=[0, 2]		)


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	Dict:
     a						, a											=self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         a											=model_class(__A		)
         a											=inspect.signature(model.forward		)
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         a											=[*signature.parameters.keys()]
         a											=['''pixel_values''']
         self.assertListEqual(arg_names[:1]					,       __A		)


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	List[Any]:
     a						, a											=self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         a											=model_class(__A		)
         self.assertEqual(len(model.channels		)					,       len(config.out_features		)		)
         a											=model.num_features
         a											=[config.stage_names.index(__A		) for feat in config.out_features]
         a											=[num_features[idx] for idx in out_indices]
         self.assertListEqual(model.channels					,       __A		)

         a											=copy.deepcopy(__A		)
         a											=None
         a											=model_class(__A		)
         self.assertEqual(len(model.channels		)					,       1		)
         self.assertListEqual(model.channels					,       [num_features[-1]]		)

         a											=copy.deepcopy(__A		)
         a											=None
         a											=model_class(__A		)
         self.assertEqual(len(model.channels		)					,       1		)
         self.assertListEqual(model.channels					,       [num_features[-1]]		)


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	Optional[Any]:
     a						, a											=self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         a											=model_class(__A		)
         model.to(__A		)
         model.eval()
         a											=model(**__A		)

         self.assertEqual(len(result.feature_maps		)					,       len(config.out_features		)		)
         self.assertEqual(len(model.channels		)					,       len(config.out_features		)		)
         self.assertEqual(len(result.feature_maps		)					,       len(config.out_indices		)		)
         self.assertEqual(len(model.channels		)					,       len(config.out_indices		)		)

         # Check output of last stage is taken if out_features=None, out_indices=None
         a											=copy.deepcopy(__A		)
         a											=None
         a											=model_class(__A		)
         model.to(__A		)
         model.eval()
         a											=model(**__A		)

         self.assertEqual(len(result.feature_maps		)					,       1		)
         self.assertEqual(len(model.channels		)					,       1		)

         a											=copy.deepcopy(__A		)
         a											=None
         a											=model_class(__A		)
         model.to(__A		)
         model.eval()
         a											=model(**__A		)

         self.assertEqual(len(result.feature_maps		)					,       1		)
         self.assertEqual(len(model.channels		)					,       1		)

         # Check backbone can be initialized with fresh weights
         a											=copy.deepcopy(__A		)
         a											=False
         a											=model_class(__A		)
         model.to(__A		)
         model.eval()
         a											=model(**__A		)


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	str:
     a						, a											=self.model_tester.prepare_config_and_inputs_for_common()

     for backbone_class in self.all_model_classes:
         a											=backbone_class(__A		)

         self.assertTrue(hasattr(__A					,       '''backbone_type'''		)		)
         self.assertTrue(hasattr(__A					,       '''stage_names'''		)		)
         self.assertTrue(hasattr(__A					,       '''num_features'''		)		)
         self.assertTrue(hasattr(__A					,       '''out_indices'''		)		)
         self.assertTrue(hasattr(__A					,       '''out_features'''		)		)
         self.assertTrue(hasattr(__A					,       '''out_feature_channels'''		)		)
         self.assertTrue(hasattr(__A					,       '''channels'''		)		)

         self.assertIsInstance(backbone.backbone_type					,       __A		)
         # Verify num_features has been initialized in the backbone init
         self.assertIsNotNone(backbone.num_features		)
         self.assertTrue(len(backbone.channels		) == len(backbone.out_indices		)		)
         self.assertTrue(len(backbone.stage_names		) == len(backbone.num_features		)		)
         self.assertTrue(len(backbone.channels		) <= len(backbone.num_features		)		)
         self.assertTrue(len(backbone.out_feature_channels		) == len(backbone.stage_names		)		)





 def  SCREAMING_SNAKE_CASE				(						self		)   ->	str:
     a						, a											=self.model_tester.prepare_config_and_inputs_for_common()
     a											=inputs_dict['''pixel_values'''].shape[0]

     for backbone_class in self.all_model_classes:
         a											=backbone_class(__A		)
         backbone.to(__A		)
         backbone.eval()

         a											=backbone(**__A		)

         # Test default outputs and verify feature maps
         self.assertIsInstance(outputs.feature_maps					,       __A		)
         self.assertTrue(len(outputs.feature_maps		) == len(backbone.channels		)		)
         for feature_map, n_channels in zip(outputs.feature_maps					,       backbone.channels		):
             self.assertTrue(feature_map.shape[:2]					,       (batch_size, n_channels)		)
         self.assertIsNone(outputs.hidden_states		)
         self.assertIsNone(outputs.attentions		)

         # Test output_hidden_states=True
         a											=backbone(**__A					,       output_hidden_states=__A		)
         self.assertIsNotNone(outputs.hidden_states		)
         self.assertTrue(len(outputs.hidden_states		)					,       len(backbone.stage_names		)		)
         for hidden_state, n_channels in zip(outputs.hidden_states					,       backbone.channels		):
             self.assertTrue(hidden_state.shape[:2]					,       (batch_size, n_channels)		)

         # Test output_attentions=True
         if self.has_attentions:
             a											=backbone(**__A					,       output_attentions=__A		)
             self.assertIsNotNone(outputs.attentions		)



"""simple docstring"""







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

from ...utils import logging


lowerCamelCase_  :  Tuple     		=    logging.get_logger(__name__)
def _A		(		lowercase			,       lowercase						):







    """simple docstring"""
    a											=nn.functional.normalize(lowercase						)
    a											=nn.functional.normalize(lowercase						)
    return torch.mm(lowercase			,       normalized_text_embeds.t()						)






class  __A							(			_SCREAMING_SNAKE_CASE     ):






 """simple docstring"""




 __lowerCAmelCase					   =    CLIPConfig

 __lowerCAmelCase					   =    ["CLIPEncoderLayer"]


 def __init__(						self					,       __A		)   ->	int:
     super().__init__(__A		)

     a											=CLIPVisionModel(config.vision_config		)
     a											=nn.Linear(config.vision_config.hidden_size					,       config.projection_dim					,       bias=__A		)

     a											=nn.Parameter(torch.ones(17					,       config.projection_dim		)					,       requires_grad=__A		)
     a											=nn.Parameter(torch.ones(3					,       config.projection_dim		)					,       requires_grad=__A		)

     a											=nn.Parameter(torch.ones(17		)					,       requires_grad=__A		)
     a											=nn.Parameter(torch.ones(3		)					,       requires_grad=__A		)


 @torch.no_grad()
 def  SCREAMING_SNAKE_CASE				(						self					,       __A					,       __A		)   ->	Union[str, Any]:
     a											=self.vision_model(__A		)[1]  # pooled_output
     a											=self.visual_projection(__A		)

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

     a											=[]
     a											=image_embeds.shape[0]
     for i in range(__A		):
         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(__A		)

         result.append(__A		)

     a											=[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											=torch.zeros_like(images[idx]		)  # black image
             else:
                 a											=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  SCREAMING_SNAKE_CASE				(						self					,       __A					,       __A		)   ->	str:
     a											=self.vision_model(__A		)[1]  # pooled_output
     a											=self.visual_projection(__A		)

     a											=cosine_distance(__A					,       self.special_care_embeds		)
     a											=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											=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		)

     a											=0.0  # black image

     return images, has_nsfw_concepts