




"""simple docstring"""




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

from ...utils import logging


lowerCAmelCase							:			List[Any]     							=							logging.get_logger(__name__)






class     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''
				__UpperCamelCase			  =						CLIPConfig

				__UpperCamelCase			  =						["CLIPEncoderLayer"]





				def __init__(       self		, _a ):





							"""simple docstring"""
							super().__init__(_a )

							lowerCamelCase			      =					CLIPVisionModelWithProjection(config.vision_config )

							lowerCamelCase			      =					nn.Linear(config.vision_config.projection_dim		, 1 )
							lowerCamelCase			      =					nn.Linear(config.vision_config.projection_dim		, 1 )





				@torch.no_grad()
				def    _lowerCAmelCase				(       self		, _a		, _a		, _a=0.5		, _a=0.5 ):





							"""simple docstring"""
							lowerCamelCase			      =					self.vision_model(_a )[0]

							lowerCamelCase			      =					self.p_head(_a )
							lowerCamelCase			      =					nsfw_detected.flatten()
							lowerCamelCase			      =					nsfw_detected > p_threshold
							lowerCamelCase			      =					nsfw_detected.tolist()

							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.""" )

							for idx, nsfw_detected_ in enumerate(_a ):
										if nsfw_detected_:
													lowerCamelCase			      =					np.zeros(images[idx].shape )

							lowerCamelCase			      =					self.w_head(_a )
							lowerCamelCase			      =					watermark_detected.flatten()
							lowerCamelCase			      =					watermark_detected > w_threshold
							lowerCamelCase			      =					watermark_detected.tolist()

							if any(_a ):
										logger.warning(
										    """Potential watermarked content was detected in one or more images. A black image will be returned instead."""
										    """ Try again with a different prompt and/or seed.""" )

							for idx, watermark_detected_ in enumerate(_a ):
										if watermark_detected_:
													lowerCamelCase			      =					np.zeros(images[idx].shape )

							return images, nsfw_detected, watermark_detected









"""simple docstring"""




from math import ceil



def     a__ (				snake_case__       , snake_case__				)						->					Optional[int]:
			lowerCamelCase			      =					list(range(0       , snake_case__				)				)

			lowerCamelCase			      =					[item for sublist in list(device_map.values()				) for item in sublist]

			# Duplicate check
			lowerCamelCase			      =					[]
			for i in device_map_blocks:
						if device_map_blocks.count(snake_case__				) > 1 and i not in duplicate_blocks:
									duplicate_blocks.append(snake_case__				)
    # Missing blocks
			lowerCamelCase			      =					[i for i in blocks if i not in device_map_blocks]
			lowerCamelCase			      =					[i for i in device_map_blocks if i not in blocks]

			if len(snake_case__				) != 0:
						raise ValueError(
						    """Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device."""
						    """ These attention blocks were specified more than once: """ + str(snake_case__				)				)
			if len(snake_case__				) != 0:
						raise ValueError(
						    """There are attention blocks for this model that are not specified in the device_map. Add these attention """
						    """blocks to a device on the device_map: """ + str(snake_case__				)				)
			if len(snake_case__				) != 0:
						raise ValueError(
						    """The device_map contains more attention blocks than this model has. Remove these from the device_map:"""
						    + str(snake_case__				)				)



def     a__ (				snake_case__       , snake_case__				)						->					List[Any]:
			lowerCamelCase			      =					list(range(snake_case__				)				)
			lowerCamelCase			      =					int(ceil(n_layers / len(snake_case__				)				)				)
			lowerCamelCase			      =					[layers[i : i + n_blocks] for i in range(0       , snake_case__       , snake_case__				)]

			return dict(zip(snake_case__       , snake_case__				)				)



