


'''simple docstring'''





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

from ...utils import logging


__lowerCamelCase												=						logging.get_logger(__name__)




class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					CLIPConfig

 UpperCAmelCase				=					["CLIPEncoderLayer"]


 def __init__(      self							:	Union[str, Any]			,       __lowerCamelCase							:	CLIPConfig ):
    super().__init__(__lowerCamelCase )

    UpperCAmelCase__	:Optional[int]      =      CLIPVisionModelWithProjection(config.vision_config )

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





 @torch.no_grad()
 def        __SCREAMING_SNAKE_CASE					(      self							:	List[Any]			,       __lowerCamelCase							:	Optional[Any]			,       __lowerCamelCase							:	Optional[int]			,       __lowerCamelCase							:	List[Any]=0.5			,       __lowerCamelCase							:	int=0.5 ):
    UpperCAmelCase__	:List[Any]      =      self.vision_model(__lowerCamelCase )[0]

    UpperCAmelCase__	:Optional[Any]      =      self.p_head(__lowerCamelCase )
    UpperCAmelCase__	:List[Any]      =      nsfw_detected.flatten()
    UpperCAmelCase__	:Tuple      =      nsfw_detected > p_threshold
    UpperCAmelCase__	:Any      =      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_:
          UpperCAmelCase__	:List[Any]      =      np.zeros(images[idx].shape )

    UpperCAmelCase__	:Tuple      =      self.w_head(__lowerCamelCase )
    UpperCAmelCase__	:List[Any]      =      watermark_detected.flatten()
    UpperCAmelCase__	:List[Any]      =      watermark_detected > w_threshold
    UpperCAmelCase__	: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_:
          UpperCAmelCase__	:List[str]      =      np.zeros(images[idx].shape )

    return images, nsfw_detected, watermark_detected







'''simple docstring'''





from multiprocessing import Lock, Pipe, Process

# lock used to ensure that two processes do not access a pipe at the same time
__lowerCamelCase												=						Lock()


def        a__     (					UpperCamelCase_						:      str,   UpperCamelCase_						:      Any,   UpperCamelCase_						:      Any,   UpperCamelCase_						:      int,   UpperCamelCase_						:      Union[str, Any],   UpperCamelCase_						:      List[str],   UpperCamelCase_						:      Optional[int]						):
   global process_lock

   # we perform n swaps since after n swaps we know we are sorted
   # we *could* stop early if we are sorted already, but it takes as long to
   # find out we are sorted as it does to sort the list with this algorithm
   for i in range(0,   10						):
      if (i + position) % 2 == 0 and r_send is not None:
         # send your value to your right neighbor
         process_lock.acquire()
         r_send[1].send(UpperCamelCase_						)
         process_lock.release()

         # receive your right neighbor's value
         process_lock.acquire()
         UpperCAmelCase__	:Union[str, Any]      =      rr_cv[0].recv()
         process_lock.release()

         # take the lower value since you are on the left
         UpperCAmelCase__	:List[Any]      =      min(UpperCamelCase_,   UpperCamelCase_						)
      elif (i + position) % 2 != 0 and l_send is not None:
         # send your value to your left neighbor
         process_lock.acquire()
         l_send[1].send(UpperCamelCase_						)
         process_lock.release()

         # receive your left neighbor's value
         process_lock.acquire()
         UpperCAmelCase__	:List[str]      =      lr_cv[0].recv()
         process_lock.release()

         # take the higher value since you are on the right
         UpperCAmelCase__	:List[Any]      =      max(UpperCamelCase_,   UpperCamelCase_						)
    # after all swaps are performed, send the values back to main
   result_pipe[1].send(UpperCamelCase_						)


def        a__     (					UpperCamelCase_						:      int						):
   UpperCAmelCase__	:List[str]      =      []
   UpperCAmelCase__	:Any      =      []
   # initialize the list of pipes where the values will be retrieved
   for _ in arr:
      result_pipe.append(Pipe()						)
   # creates the processes
   # the first and last process only have one neighbor so they are made outside
   # of the loop
   UpperCAmelCase__	:str      =      Pipe()
   UpperCAmelCase__	:Dict      =      Pipe()
   process_array_.append(
       Process(
           target=UpperCamelCase_,   args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]),   )						)
   UpperCAmelCase__	:Optional[Any]      =      temp_rs
   UpperCAmelCase__	:Any      =      temp_rr

   for i in range(1,   len(UpperCamelCase_						) - 1						):
      UpperCAmelCase__	:Optional[int]      =      Pipe()
      UpperCAmelCase__	:List[str]      =      Pipe()
      process_array_.append(
          Process(
              target=UpperCamelCase_,   args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]),   )						)
      UpperCAmelCase__	:Optional[Any]      =      temp_rs
      UpperCAmelCase__	:str      =      temp_rr

   process_array_.append(
       Process(
           target=UpperCamelCase_,   args=(
               len(UpperCamelCase_						) - 1,
               arr[len(UpperCamelCase_						) - 1],
               temp_ls,
               None,
               temp_lr,
               None,
               result_pipe[len(UpperCamelCase_						) - 1],
           ),   )						)

   # start the processes
   for p in process_array_:
      p.start()

   # wait for the processes to end and write their values to the list
   for p in range(0,   len(UpperCamelCase_						)						):
      UpperCAmelCase__	:List[str]      =      result_pipe[p][0].recv()
      process_array_[p].join()
   return arr


def        a__     (					):
   UpperCAmelCase__	:Optional[Any]      =      list(range(10,   0,   -1						)						)
   print('''Initial List'''						)
   print(*UpperCamelCase_						)
   UpperCAmelCase__	:str      =      odd_even_transposition(UpperCamelCase_						)
   print('''Sorted List\n'''						)
   print(*UpperCamelCase_						)


if __name__ == "__main__":
    main()



