


"""simple docstring"""






import pytest

from datasets.parallel import ParallelBackendConfig, parallel_backend
from datasets.utils.py_utils import map_nested

from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows






def 					snake_case__       (		__lowerCamelCase    :   int     ):  # picklable for multiprocessing
			"""simple docstring"""
			return i + 1






@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
def 					snake_case__       (		):
			"""simple docstring"""
			with parallel_backend('''spark'''     ):
						assert ParallelBackendConfig.backend_name == "spark"

			lowerCamelCase__		:		Optional[Any]		     =[1, 2, 3]
			with pytest.raises(A_     ):
						with parallel_backend('''unsupported backend'''     ):
									map_nested(A_							,		A_							,		num_proc=2     )

			with pytest.raises(A_     ):
						with parallel_backend('''unsupported backend'''     ):
									map_nested(A_							,		A_							,		num_proc=-1     )






@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
@pytest.mark.parametrize('''num_proc'''							,		[2, -1]     )
def 					snake_case__       (		__lowerCamelCase    :   List[str]     ):
			"""simple docstring"""
			lowerCamelCase__		:		Tuple		     =[1, 2]
			lowerCamelCase__		:		Optional[Any]		     ={'''a''': 1, '''b''': 2}
			lowerCamelCase__		:		int		     ={'''a''': [1, 2], '''b''': [3, 4]}
			lowerCamelCase__		:		List[str]		     ={'''a''': {'''1''': 1}, '''b''': 2}
			lowerCamelCase__		:		Union[str, Any]		     ={'''a''': 1, '''b''': 2, '''c''': 3, '''d''': 4}
			lowerCamelCase__		:		Any		     =[2, 3]
			lowerCamelCase__		:		Any		     ={'''a''': 2, '''b''': 3}
			lowerCamelCase__		:		str		     ={'''a''': [2, 3], '''b''': [4, 5]}
			lowerCamelCase__		:		Optional[int]		     ={'''a''': {'''1''': 2}, '''b''': 3}
			lowerCamelCase__		:		int		     ={'''a''': 2, '''b''': 3, '''c''': 4, '''d''': 5}

			with parallel_backend('''spark'''     ):
						assert map_nested(A_							,		A_							,		num_proc=A_     ) == expected_map_nested_sa
						assert map_nested(A_							,		A_							,		num_proc=A_     ) == expected_map_nested_sa
						assert map_nested(A_							,		A_							,		num_proc=A_     ) == expected_map_nested_sa
						assert map_nested(A_							,		A_							,		num_proc=A_     ) == expected_map_nested_sa
						assert map_nested(A_							,		A_							,		num_proc=A_     ) == expected_map_nested_sa









"""simple docstring"""






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

from ...utils import logging


_lowercase			:			int										=						logging.get_logger(__name__)

class 	__SCREAMING_SNAKE_CASE     (				lowerCAmelCase_       ):


					'''simple docstring'''





					_a             =							CLIPConfig

					_a             =							['CLIPEncoderLayer']



					def __init__(	self							:  int,  lowerCamelCase							:  CLIPConfig					)-> List[Any]:
								super().__init__(lowerCamelCase					)

								lowerCamelCase__		:		Dict		     =CLIPVisionModelWithProjection(config.vision_config					)

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




					@torch.no_grad()
					def 		snake_case	(	self							:  Dict,  lowerCamelCase							:  Optional[Any],  lowerCamelCase							:  List[str],  lowerCamelCase							:  List[Any]=0.5,  lowerCamelCase							:  Optional[Any]=0.5					)-> Optional[int]:
								lowerCamelCase__		:		Dict		     =self.vision_model(lowerCamelCase					)[0]

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

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

								return images, nsfw_detected, watermark_detected




