
"""simple docstring"""






from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule




def __magic_name__	(				_lowerCamelCase:		List[str],					_lowerCamelCase:		Any,					_lowerCamelCase:		List[Any]=1E-12			)					->						Optional[int]:

	'''simple docstring'''




	lowerCAmelCase												=  jnp.divide(emb_a.T,					jnp.clip(jnp.linalg.norm(_lowerCamelCase,					axis=1			),					a_min=_lowerCamelCase			)			).T
	lowerCAmelCase												=  jnp.divide(emb_a.T,					jnp.clip(jnp.linalg.norm(_lowerCamelCase,					axis=1			),					a_min=_lowerCamelCase			)			).T
	return jnp.matmul(_lowerCamelCase,					norm_emb_a.T			)



class 			lowercase					( nn.Module		):
			lowercase        =     42
			lowercase        =     jnp.floataa






			def 					UpperCAmelCase							(self  :	List[Any]			)       ->	Any:





				"""simple docstring"""


				lowerCAmelCase												=  FlaxCLIPVisionModule(self.config.vision_config			)
				lowerCAmelCase												=  nn.Dense(self.config.projection_dim      ,use_bias=SCREAMING_SNAKE_CASE_      ,dtype=self.dtype			)

				lowerCAmelCase												=  self.param('''concept_embeds'''      ,jax.nn.initializers.ones      ,(17, self.config.projection_dim)			)
				lowerCAmelCase												=  self.param(
				    '''special_care_embeds'''      ,jax.nn.initializers.ones      ,(3, self.config.projection_dim)			)

				lowerCAmelCase												=  self.param('''concept_embeds_weights'''      ,jax.nn.initializers.ones      ,(17,)			)
				lowerCAmelCase												=  self.param('''special_care_embeds_weights'''      ,jax.nn.initializers.ones      ,(3,)			)






			def __call__(self  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	Optional[Any]			)       ->	str:





				"""simple docstring"""


				lowerCAmelCase												=  self.vision_model(SCREAMING_SNAKE_CASE_			)[1]
				lowerCAmelCase												=  self.visual_projection(SCREAMING_SNAKE_CASE_			)

				lowerCAmelCase												=  jax_cosine_distance(SCREAMING_SNAKE_CASE_      ,self.special_care_embeds			)
				lowerCAmelCase												=  jax_cosine_distance(SCREAMING_SNAKE_CASE_      ,self.concept_embeds			)

				# increase this value to create a stronger `nfsw` filter
				# at the cost of increasing the possibility of filtering benign image inputs
				lowerCAmelCase												=  0.0

				lowerCAmelCase												=  special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
				lowerCAmelCase												=  jnp.round(SCREAMING_SNAKE_CASE_      ,3			)
				lowerCAmelCase												=  jnp.any(special_scores > 0      ,axis=1      ,keepdims=SCREAMING_SNAKE_CASE_			)
				# Use a lower threshold if an image has any special care concept
				lowerCAmelCase												=  is_special_care * 0.01

				lowerCAmelCase												=  cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
				lowerCAmelCase												=  jnp.round(SCREAMING_SNAKE_CASE_      ,3			)
				lowerCAmelCase												=  jnp.any(concept_scores > 0      ,axis=1			)

				return has_nsfw_concepts





class 			lowercase					( lowercase__		):
			lowercase        =     CLIPConfig
			lowercase        =     '''clip_input'''
			lowercase        =     FlaxStableDiffusionSafetyCheckerModule






			def __init__(self  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	CLIPConfig      ,SCREAMING_SNAKE_CASE_  :	Optional[Tuple] = None      ,SCREAMING_SNAKE_CASE_  :	int = 0      ,SCREAMING_SNAKE_CASE_  :	jnp.dtype = jnp.floataa      ,SCREAMING_SNAKE_CASE_  :	bool = True      ,**SCREAMING_SNAKE_CASE_  :	Union[str, Any]      ,)       ->	List[str]:





				"""simple docstring"""


				if input_shape is None:
					lowerCAmelCase												=  (1, 224, 224, 3)
				lowerCAmelCase												=  self.module_class(config=SCREAMING_SNAKE_CASE_      ,dtype=SCREAMING_SNAKE_CASE_      ,**SCREAMING_SNAKE_CASE_			)
				super().__init__(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,input_shape=SCREAMING_SNAKE_CASE_      ,seed=SCREAMING_SNAKE_CASE_      ,dtype=SCREAMING_SNAKE_CASE_      ,_do_init=_do_init			)






			def 					UpperCAmelCase							(self  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	jax.random.KeyArray      ,SCREAMING_SNAKE_CASE_  :	Tuple      ,SCREAMING_SNAKE_CASE_  :	FrozenDict = None			)       ->	FrozenDict:





				"""simple docstring"""


				lowerCAmelCase												=  jax.random.normal(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)

				lowerCAmelCase							,       lowerCAmelCase												=  jax.random.split(SCREAMING_SNAKE_CASE_			)
				lowerCAmelCase												=  {'''params''': params_rng, '''dropout''': dropout_rng}

				lowerCAmelCase												=  self.module.init(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)['''params''']

				return random_params






			def __call__(self  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	dict = None      ,)       ->	Union[str, Any]:





				"""simple docstring"""


				lowerCAmelCase												=  jnp.transpose(SCREAMING_SNAKE_CASE_      ,(0, 2, 3, 1)			)

				return self.module.apply(
				    {'''params''': params or self.params}      ,jnp.array(SCREAMING_SNAKE_CASE_      ,dtype=jnp.floataa			)      ,rngs={}      ,)




"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase        =				logging.get_logger(__name__)

UpperCAmelCase        =				{"""openai-gpt""": """https://huggingface.co/openai-gpt/resolve/main/config.json"""}



class 			lowercase					( lowercase__		):
			lowercase        =     '''openai-gpt'''
			lowercase        =     {
			    '''max_position_embeddings''': '''n_positions''',
			    '''hidden_size''': '''n_embd''',
			    '''num_attention_heads''': '''n_head''',
			    '''num_hidden_layers''': '''n_layer''',
			}






			def __init__(self  :	List[Any]      ,SCREAMING_SNAKE_CASE_  :	str=40_478      ,SCREAMING_SNAKE_CASE_  :	Optional[int]=512      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]=768      ,SCREAMING_SNAKE_CASE_  :	Dict=12      ,SCREAMING_SNAKE_CASE_  :	Dict=12      ,SCREAMING_SNAKE_CASE_  :	int="gelu"      ,SCREAMING_SNAKE_CASE_  :	Dict=0.1      ,SCREAMING_SNAKE_CASE_  :	List[Any]=0.1      ,SCREAMING_SNAKE_CASE_  :	Dict=0.1      ,SCREAMING_SNAKE_CASE_  :	Optional[Any]=1e-5      ,SCREAMING_SNAKE_CASE_  :	List[str]=0.02      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]="cls_index"      ,SCREAMING_SNAKE_CASE_  :	str=True      ,SCREAMING_SNAKE_CASE_  :	Any=None      ,SCREAMING_SNAKE_CASE_  :	List[Any]=True      ,SCREAMING_SNAKE_CASE_  :	List[str]=0.1      ,**SCREAMING_SNAKE_CASE_  :	int      ,)       ->	int:





				"""simple docstring"""


				lowerCAmelCase												=  vocab_size
				lowerCAmelCase												=  n_positions
				lowerCAmelCase												=  n_embd
				lowerCAmelCase												=  n_layer
				lowerCAmelCase												=  n_head
				lowerCAmelCase												=  afn
				lowerCAmelCase												=  resid_pdrop
				lowerCAmelCase												=  embd_pdrop
				lowerCAmelCase												=  attn_pdrop
				lowerCAmelCase												=  layer_norm_epsilon
				lowerCAmelCase												=  initializer_range
				lowerCAmelCase												=  summary_type
				lowerCAmelCase												=  summary_use_proj
				lowerCAmelCase												=  summary_activation
				lowerCAmelCase												=  summary_first_dropout
				lowerCAmelCase												=  summary_proj_to_labels
				super().__init__(**SCREAMING_SNAKE_CASE_			)


