


"""simple docstring"""






import collections
import inspect
import unittest

from transformers import SwinConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch
   from torch import nn

   from transformers import SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel
   from transformers.models.swin.modeling_swin import SWIN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor


class      __lowerCamelCase	:

     '''simple docstring'''


     def __init__(   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=13     ,					__UpperCAmelCase=32     ,					__UpperCAmelCase=2     ,					__UpperCAmelCase=3     ,					__UpperCAmelCase=16     ,					__UpperCAmelCase=[1, 2, 1]     ,					__UpperCAmelCase=[2, 2, 4]     ,					__UpperCAmelCase=2     ,					__UpperCAmelCase=2.0     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=0.0     ,					__UpperCAmelCase=0.0     ,					__UpperCAmelCase=0.1     ,					__UpperCAmelCase="gelu"     ,					__UpperCAmelCase=False     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=0.02     ,					__UpperCAmelCase=1e-5     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=None     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=10     ,					__UpperCAmelCase=8     ,					__UpperCAmelCase=["stage1", "stage2"]     ,					__UpperCAmelCase=[1, 2]     ,					)							->				Optional[Any]:
          _a     						=					parent
          _a     						=					batch_size
          _a     						=					image_size
          _a     						=					patch_size
          _a     						=					num_channels
          _a     						=					embed_dim
          _a     						=					depths
          _a     						=					num_heads
          _a     						=					window_size
          _a     						=					mlp_ratio
          _a     						=					qkv_bias
          _a     						=					hidden_dropout_prob
          _a     						=					attention_probs_dropout_prob
          _a     						=					drop_path_rate
          _a     						=					hidden_act
          _a     						=					use_absolute_embeddings
          _a     						=					patch_norm
          _a     						=					layer_norm_eps
          _a     						=					initializer_range
          _a     						=					is_training
          _a     						=					scope
          _a     						=					use_labels
          _a     						=					type_sequence_label_size
          _a     						=					encoder_stride
          _a     						=					out_features
          _a     						=					out_indices


     def 				_UpperCAmelCase   (   self				)							->				Tuple:
          _a     						=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

          _a     						=					None
          if self.use_labels:
               _a     						=					ids_tensor([self.batch_size]     ,					self.type_sequence_label_size				)

          _a     						=					self.get_config()

          return config, pixel_values, labels


     def 				_UpperCAmelCase   (   self				)							->				List[str]:
          return SwinConfig(
              image_size=self.image_size     ,					patch_size=self.patch_size     ,					num_channels=self.num_channels     ,					embed_dim=self.embed_dim     ,					depths=self.depths     ,					num_heads=self.num_heads     ,					window_size=self.window_size     ,					mlp_ratio=self.mlp_ratio     ,					qkv_bias=self.qkv_bias     ,					hidden_dropout_prob=self.hidden_dropout_prob     ,					attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,					drop_path_rate=self.drop_path_rate     ,					hidden_act=self.hidden_act     ,					use_absolute_embeddings=self.use_absolute_embeddings     ,					path_norm=self.patch_norm     ,					layer_norm_eps=self.layer_norm_eps     ,					initializer_range=self.initializer_range     ,					encoder_stride=self.encoder_stride     ,					out_features=self.out_features     ,					out_indices=self.out_indices     ,					)


     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)							->				int:
          _a     						=					SwinModel(config=__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()
          _a     						=					model(__UpperCAmelCase				)

          _a     						=					((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths				) - 1))
          _a     						=					int(config.embed_dim * 2 ** (len(config.depths				) - 1)				)

          self.parent.assertEqual(result.last_hidden_state.shape     ,					(self.batch_size, expected_seq_len, expected_dim)				)


     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)							->				Dict:
          _a     						=					SwinBackbone(config=__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()
          _a     						=					model(__UpperCAmelCase				)

          # verify hidden states
          self.parent.assertEqual(len(result.feature_maps				)     ,					len(config.out_features				)				)
          self.parent.assertListEqual(list(result.feature_maps[0].shape				)     ,					[self.batch_size, model.channels[0], 16, 16]				)

          # verify channels
          self.parent.assertEqual(len(model.channels				)     ,					len(config.out_features				)				)

          # verify backbone works with out_features=None
          _a     						=					None
          _a     						=					SwinBackbone(config=__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()
          _a     						=					model(__UpperCAmelCase				)

          # verify feature maps
          self.parent.assertEqual(len(result.feature_maps				)     ,					1				)
          self.parent.assertListEqual(list(result.feature_maps[0].shape				)     ,					[self.batch_size, model.channels[-1], 4, 4]				)

          # verify channels
          self.parent.assertEqual(len(model.channels				)     ,					1				)


     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)							->				Any:
          _a     						=					SwinForMaskedImageModeling(config=__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()
          _a     						=					model(__UpperCAmelCase				)
          self.parent.assertEqual(
              result.logits.shape     ,					(self.batch_size, self.num_channels, self.image_size, self.image_size)				)

          # test greyscale images
          _a     						=					1
          _a     						=					SwinForMaskedImageModeling(__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()

          _a     						=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
          _a     						=					model(__UpperCAmelCase				)
          self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, 1, self.image_size, self.image_size)				)


     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)							->				Any:
          _a     						=					self.type_sequence_label_size
          _a     						=					SwinForImageClassification(__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()
          _a     						=					model(__UpperCAmelCase     ,					labels=__UpperCAmelCase				)
          self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.type_sequence_label_size)				)

          # test greyscale images
          _a     						=					1
          _a     						=					SwinForImageClassification(__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()

          _a     						=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
          _a     						=					model(__UpperCAmelCase				)
          self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.type_sequence_label_size)				)





     def 				_UpperCAmelCase   (   self				)							->				Dict:
          _a     						=					self.prepare_config_and_inputs()
          (
              (
              _a
          )						,   (
              _a
          )						,   (
              _a
          )						,   
          )     						=					config_and_inputs
          _a     						=					{'''pixel_values''': pixel_values}
          return config, inputs_dict







@require_torch
class      __lowerCamelCase	(       a__	,       a__	,       unittest.TestCase					):

     '''simple docstring'''
     A_							:						List[Any]     =		(
         (
             SwinModel,
             SwinBackbone,
             SwinForImageClassification,
             SwinForMaskedImageModeling,
         )
         if is_torch_available()
         else ()
     )
     A_							:						Dict     =		(
         {'feature-extraction': SwinModel, 'image-classification': SwinForImageClassification}
         if is_torch_available()
         else {}
     )
     A_							:						Tuple     =		True

     A_							:						Dict     =		False
     A_							:						List[Any]     =		False
     A_							:						Optional[int]     =		False


     def 				_UpperCAmelCase   (   self				)							->				List[Any]:
          _a     						=					SwinModelTester(self				)
          _a     						=					ConfigTester(self     ,					config_class=__UpperCAmelCase     ,					embed_dim=37				)


     def 				_UpperCAmelCase   (   self				)							->				int:
          self.create_and_test_config_common_properties()
          self.config_tester.create_and_test_config_to_json_string()
          self.config_tester.create_and_test_config_to_json_file()
          self.config_tester.create_and_test_config_from_and_save_pretrained()
          self.config_tester.create_and_test_config_with_num_labels()
          self.config_tester.check_config_can_be_init_without_params()
          self.config_tester.check_config_arguments_init()


     def 				_UpperCAmelCase   (   self				)							->				Dict:
          return


     def 				_UpperCAmelCase   (   self				)							->				Tuple:
          _a     						=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__UpperCAmelCase				)


     @unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.'''				)
     def 				_UpperCAmelCase   (   self				)							->				Dict:
          pass


     def 				_UpperCAmelCase   (   self				)							->				str:
          super().test_training_gradient_checkpointing()


     def 				_UpperCAmelCase   (   self				)							->				Optional[int]:
          _a     						=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_backbone(*__UpperCAmelCase				)


     def 				_UpperCAmelCase   (   self				)							->				Tuple:
          _a     						=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCAmelCase				)


     def 				_UpperCAmelCase   (   self				)							->				Optional[int]:
          _a     						=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase				)


     @unittest.skip(reason='''Swin does not use inputs_embeds'''				)
     def 				_UpperCAmelCase   (   self				)							->				Any:
          pass


     @unittest.skip(reason='''Swin Transformer does not use feedforward chunking'''				)
     def 				_UpperCAmelCase   (   self				)							->				Optional[int]:
          pass


     def 				_UpperCAmelCase   (   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(__UpperCAmelCase				)
               self.assertIsInstance(model.get_input_embeddings()     ,					(nn.Module)				)
               _a     						=					model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(__UpperCAmelCase     ,					nn.Linear				)				)


     def 				_UpperCAmelCase   (   self				)							->				List[str]:
          _a						,   _a     						=					self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               _a     						=					model_class(__UpperCAmelCase				)
               _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]     ,					__UpperCAmelCase				)


     def 				_UpperCAmelCase   (   self				)							->				Union[str, Any]:
          _a						,   _a     						=					self.model_tester.prepare_config_and_inputs_for_common()
          _a     						=					True

          for model_class in self.all_model_classes:
               _a     						=					True
               _a     						=					False
               _a     						=					True
               _a     						=					model_class(__UpperCAmelCase				)
               model.to(__UpperCAmelCase				)
               model.eval()
               with torch.no_grad():
                    _a     						=					model(**self._prepare_for_class(__UpperCAmelCase     ,					__UpperCAmelCase				)				)
               _a     						=					outputs.attentions
               _a     						=					len(self.model_tester.depths				)
               self.assertEqual(len(__UpperCAmelCase				)     ,					__UpperCAmelCase				)

               # check that output_attentions also work using config
               del inputs_dict["output_attentions"]
               _a     						=					True
               _a     						=					config.window_size**2
               _a     						=					model_class(__UpperCAmelCase				)
               model.to(__UpperCAmelCase				)
               model.eval()
               with torch.no_grad():
                    _a     						=					model(**self._prepare_for_class(__UpperCAmelCase     ,					__UpperCAmelCase				)				)
               _a     						=					outputs.attentions
               self.assertEqual(len(__UpperCAmelCase				)     ,					__UpperCAmelCase				)

               self.assertListEqual(
                   list(attentions[0].shape[-3:]				)     ,					[self.model_tester.num_heads[0], window_size_squared, window_size_squared]     ,					)
               _a     						=					len(__UpperCAmelCase				)

               # Check attention is always last and order is fine
               _a     						=					True
               _a     						=					True
               _a     						=					model_class(__UpperCAmelCase				)
               model.to(__UpperCAmelCase				)
               model.eval()
               with torch.no_grad():
                    _a     						=					model(**self._prepare_for_class(__UpperCAmelCase     ,					__UpperCAmelCase				)				)

               # also another +1 for reshaped_hidden_states
               _a     						=					1 if model_class.__name__ == '''SwinBackbone''' else 2
               self.assertEqual(out_len + added_hidden_states     ,					len(__UpperCAmelCase				)				)

               _a     						=					outputs.attentions

               self.assertEqual(len(__UpperCAmelCase				)     ,					__UpperCAmelCase				)

               self.assertListEqual(
                   list(self_attentions[0].shape[-3:]				)     ,					[self.model_tester.num_heads[0], window_size_squared, window_size_squared]     ,					)


     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)							->				Optional[int]:
          _a     						=					model_class(__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()

          with torch.no_grad():
               _a     						=					model(**self._prepare_for_class(__UpperCAmelCase     ,					__UpperCAmelCase				)				)

          _a     						=					outputs.hidden_states

          _a     						=					getattr(
              self.model_tester     ,					'''expected_num_hidden_layers'''     ,					len(self.model_tester.depths				) + 1				)
          self.assertEqual(len(__UpperCAmelCase				)     ,					__UpperCAmelCase				)

          # Swin has a different seq_length
          _a     						=					(
              config.patch_size
              if isinstance(config.patch_size     ,					collections.abc.Iterable				)
              else (config.patch_size, config.patch_size)
          )

          _a     						=					(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

          self.assertListEqual(
              list(hidden_states[0].shape[-2:]				)     ,					[num_patches, self.model_tester.embed_dim]     ,					)

          if not model_class.__name__ == "SwinBackbone":
               _a     						=					outputs.reshaped_hidden_states
               self.assertEqual(len(__UpperCAmelCase				)     ,					__UpperCAmelCase				)

               _a						,   _a						,   _a						,   _a     						=					reshaped_hidden_states[0].shape
               _a     						=					(
                   reshaped_hidden_states[0].view(__UpperCAmelCase     ,					__UpperCAmelCase     ,					height * width				).permute(0     ,					2     ,					1				)
               )
               self.assertListEqual(
                   list(reshaped_hidden_states.shape[-2:]				)     ,					[num_patches, self.model_tester.embed_dim]     ,					)


     def 				_UpperCAmelCase   (   self				)							->				str:
          _a						,   _a     						=					self.model_tester.prepare_config_and_inputs_for_common()

          _a     						=					(
              self.model_tester.image_size
              if isinstance(self.model_tester.image_size     ,					collections.abc.Iterable				)
              else (self.model_tester.image_size, self.model_tester.image_size)
          )

          for model_class in self.all_model_classes:
               _a     						=					True
               self.check_hidden_states_output(__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               _a     						=					True

               self.check_hidden_states_output(__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)


     def 				_UpperCAmelCase   (   self				)							->				List[str]:
          _a						,   _a     						=					self.model_tester.prepare_config_and_inputs_for_common()
          _a     						=					3

          _a     						=					(
              self.model_tester.image_size
              if isinstance(self.model_tester.image_size     ,					collections.abc.Iterable				)
              else (self.model_tester.image_size, self.model_tester.image_size)
          )
          _a     						=					(
              config.patch_size
              if isinstance(config.patch_size     ,					collections.abc.Iterable				)
              else (config.patch_size, config.patch_size)
          )

          _a     						=					image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
          _a     						=					image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

          for model_class in self.all_model_classes:
               _a     						=					True
               self.check_hidden_states_output(__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					(padded_height, padded_width)				)

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               _a     						=					True
               self.check_hidden_states_output(__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					(padded_height, padded_width)				)


     @slow
     def 				_UpperCAmelCase   (   self				)							->				str:
          for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _a     						=					SwinModel.from_pretrained(__UpperCAmelCase				)
               self.assertIsNotNone(__UpperCAmelCase				)





     def 				_UpperCAmelCase   (   self				)							->				Optional[int]:
          _a						,   _a     						=					self.model_tester.prepare_config_and_inputs_for_common()

          _a     						=					_config_zero_init(__UpperCAmelCase				)
          for model_class in self.all_model_classes:
               _a     						=					model_class(config=__UpperCAmelCase				)
               for name, param in model.named_parameters():
                    if "embeddings" not in name and param.requires_grad:
                         self.assertIn(
                             ((param.data.mean() * 1e9).round() / 1e9).item()     ,					[0.0, 1.0]     ,					msg=F'Parameter {name} of model {model_class} seems not properly initialized'     ,					)







@require_vision
@require_torch
class      __lowerCamelCase	(       unittest.TestCase					):

     '''simple docstring'''


     @cached_property
     def 				_UpperCAmelCase   (   self				)							->				Any:
          return (
              AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''				)
              if is_vision_available()
              else None
          )





     @slow
     def 				_UpperCAmelCase   (   self				)							->				List[Any]:
          _a     						=					SwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''				).to(__UpperCAmelCase				)
          _a     						=					self.default_image_processor

          _a     						=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
          _a     						=					image_processor(images=__UpperCAmelCase     ,					return_tensors='''pt'''				).to(__UpperCAmelCase				)

          # forward pass
          with torch.no_grad():
               _a     						=					model(**__UpperCAmelCase				)

          # verify the logits
          _a     						=					torch.Size((1, 1000)				)
          self.assertEqual(outputs.logits.shape     ,					__UpperCAmelCase				)
          _a     						=					torch.tensor([-0.0948, -0.6454, -0.0921]				).to(__UpperCAmelCase				)
          self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,					__UpperCAmelCase     ,					atol=1e-4				)				)







@require_torch
class      __lowerCamelCase	(       unittest.TestCase	,       a__					):

     '''simple docstring'''
     A_							:						Optional[int]     =		(SwinBackbone,) if is_torch_available() else ()
     A_							:						Optional[Any]     =		SwinConfig


     def 				_UpperCAmelCase   (   self				)							->				Union[str, Any]:
          _a     						=					SwinModelTester(self				)



"""simple docstring"""






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

from ...utils import logging


__snake_case												=							logging.get_logger(__name__)
def    A_ ( _lowerCAmelCase  :       Optional[int],  _lowerCAmelCase  :       Optional[Any]							):






     """simple docstring"""

     _a     						=					nn.functional.normalize(_lowerCAmelCase							)
     _a     						=					nn.functional.normalize(_lowerCAmelCase							)
     return torch.mm(_lowerCAmelCase,  normalized_text_embeds.t()							)


class      __lowerCamelCase	(       a__					):

     '''simple docstring'''
     A_							:						str     =		CLIPConfig

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


     def __init__(   self     ,					__UpperCAmelCase				)							->				List[Any]:
          super().__init__(__UpperCAmelCase				)

          _a     						=					CLIPVisionModel(config.vision_config				)
          _a     						=					nn.Linear(config.vision_config.hidden_size     ,					config.projection_dim     ,					bias=__UpperCAmelCase				)

          _a     						=					nn.Parameter(torch.ones(17     ,					config.projection_dim				)     ,					requires_grad=__UpperCAmelCase				)
          _a     						=					nn.Parameter(torch.ones(3     ,					config.projection_dim				)     ,					requires_grad=__UpperCAmelCase				)

          _a     						=					nn.Parameter(torch.ones(17				)     ,					requires_grad=__UpperCAmelCase				)
          _a     						=					nn.Parameter(torch.ones(3				)     ,					requires_grad=__UpperCAmelCase				)


     @torch.no_grad()
     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)							->				Optional[Any]:
          _a     						=					self.vision_model(__UpperCAmelCase				)[1]  # pooled_output
          _a     						=					self.visual_projection(__UpperCAmelCase				)

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

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

               result.append(__UpperCAmelCase				)

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

          for idx, has_nsfw_concept in enumerate(__UpperCAmelCase				):
               if has_nsfw_concept:
                    if torch.is_tensor(__UpperCAmelCase				) 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(__UpperCAmelCase				):
               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 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)							->				Any:
          _a     						=					self.vision_model(__UpperCAmelCase				)[1]  # pooled_output
          _a     						=					self.visual_projection(__UpperCAmelCase				)

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