



"""simple docstring"""





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

from ...utils import logging


__lowerCAmelCase							:       str          =       logging.get_logger(__name__)



class  _lowerCAmelCase     (					UpperCamelCase__		):





		"""simple docstring"""






		_lowerCamelCase      	=							CLIPConfig

		_lowerCamelCase      	=							['''CLIPEncoderLayer''']





		def __init__(       self				,     _lowercase )			->    List[str]:







								'''simple docstring'''

								super().__init__(__lowerCamelCase )

								snake_case_ :			Union[str, Any]            =      CLIPVisionModelWithProjection(config.vision_config )

								snake_case_ :			Dict            =      nn.Linear(config.vision_config.projection_dim				,     1 )
								snake_case_ :			Union[str, Any]            =      nn.Linear(config.vision_config.projection_dim				,     1 )





		@torch.no_grad()
		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase=0.5				,     _lowercase=0.5 )			->    List[Any]:







								'''simple docstring'''

								snake_case_ :			Optional[int]            =      self.vision_model(__lowerCamelCase )[0]

								snake_case_ :			List[str]            =      self.p_head(__lowerCamelCase )
								snake_case_ :			Optional[Any]            =      nsfw_detected.flatten()
								snake_case_ :			int            =      nsfw_detected > p_threshold
								snake_case_ :			str            =      nsfw_detected.tolist()

								if any(__lowerCamelCase ):
														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(__lowerCamelCase ):
														if nsfw_detected_:
																				snake_case_ :			Tuple            =      np.zeros(images[idx].shape )

								snake_case_ :			List[str]            =      self.w_head(__lowerCamelCase )
								snake_case_ :			Tuple            =      watermark_detected.flatten()
								snake_case_ :			Any            =      watermark_detected > w_threshold
								snake_case_ :			str            =      watermark_detected.tolist()

								if any(__lowerCamelCase ):
														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(__lowerCamelCase ):
														if watermark_detected_:
																				snake_case_ :			Optional[int]            =      np.zeros(images[idx].shape )

								return images, nsfw_detected, watermark_detected








a__: str							   =						'0.21.0'

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
       from .utils import rich





