


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

from ...utils import logging


snake_case__			 =						logging.get_logger(__name__)


class 					lowerCAmelCase_ (       _a):
						lowerCamelCase_         =     CLIPConfig

						lowerCamelCase_         =     ['CLIPEncoderLayer']




						def __init__(	self						:   Optional[int]   ,     __A						:   CLIPConfig						)      ->Dict:






									"""simple docstring"""






									super().__init__(__A						)

									a__ :int						  =    CLIPVisionModelWithProjection(config.vision_config						)

									a__ :Optional[Any]						  =    nn.Linear(config.vision_config.projection_dim   ,     1						)
									a__ :List[Any]						  =    nn.Linear(config.vision_config.projection_dim   ,     1						)






						@torch.no_grad()
						def  _snake_case					(	self						:   Optional[int]   ,     __A						:   List[str]   ,     __A						:   Dict   ,     __A						:   List[str]=0.5   ,     __A						:   Optional[int]=0.5						)      ->List[Any]:






									"""simple docstring"""






									a__ :Tuple						  =    self.vision_model(__A						)[0]

									a__ :List[Any]						  =    self.p_head(__A						)
									a__ :int						  =    nsfw_detected.flatten()
									a__ :List[Any]						  =    nsfw_detected > p_threshold
									a__ :Union[str, Any]						  =    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_:
															a__ :str						  =    np.zeros(images[idx].shape						)

									a__ :str						  =    self.w_head(__A						)
									a__ :Dict						  =    watermark_detected.flatten()
									a__ :Tuple						  =    watermark_detected > w_threshold
									a__ :Union[str, Any]						  =    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_:
															a__ :str						  =    np.zeros(images[idx].shape						)

									return images, nsfw_detected, watermark_detected






import os
import time

import numpy as np
import onnxruntime as ort


snake_case__			 =						'''1'''
snake_case__			 =						'''0'''
snake_case__			 =						'''1'''

snake_case__			 =						ort.SessionOptions()
snake_case__			 =						ort.GraphOptimizationLevel.ORT_DISABLE_ALL
print('''Create inference session...''')
snake_case__			 =						['''TensorrtExecutionProvider''', '''CUDAExecutionProvider''']
snake_case__			 =						ort.InferenceSession('''model.onnx''', sess_options=sess_opt, providers=execution_provider)
snake_case__			 =						ort.RunOptions()

snake_case__			 =						128
snake_case__			 =						1
snake_case__			 =						np.ones((batch, sequence), dtype=np.intaa)
snake_case__			 =						np.ones((batch, sequence), dtype=np.intaa)
snake_case__			 =						np.ones((batch, sequence), dtype=np.intaa)

print('''Warm up phase...''')
sess.run(
    None,
    {
        sess.get_inputs()[0].name: input_ids,
        sess.get_inputs()[1].name: attention_mask,
        sess.get_inputs()[2].name: token_type_ids,
    },
    run_options=run_opt,
)

print('''Start inference...''')
snake_case__			 =						time.time()
snake_case__			 =						2000
snake_case__			 =						{}
for iter in range(max_iters):
		snake_case__			 =						sess.run(
		    None,
		    {
		        sess.get_inputs()[0].name: input_ids,
		        sess.get_inputs()[1].name: attention_mask,
		        sess.get_inputs()[2].name: token_type_ids,
		    },
		    run_options=run_opt,
		)
print('''Average Inference Time = {:.3f} ms'''.format((time.time() - start_time) * 1000 / max_iters))


