"""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   a_						(					lowerCamelCase					,			lowerCamelCase					,			lowerCamelCase=1e-1_2       ):
				UpperCAmelCase__             =							jnp.divide(emb_a.T					,			jnp.clip(jnp.linalg.norm(lowerCamelCase					,			axis=1       )					,			a_min=lowerCamelCase       )       ).T
				UpperCAmelCase__             =							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 				snake_case       ( nn.Module				):




					"""simple docstring"""

					snake_case__				    = 42
					snake_case__				    = jnp.floataa







					def 		__lowerCAmelCase						(				self   :	List[Any]			):
									UpperCAmelCase__             =							FlaxCLIPVisionModule(self.config.vision_config			)
									UpperCAmelCase__             =							nn.Dense(self.config.projection_dim    ,use_bias=lowerCamelCase__    ,dtype=self.dtype			)

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

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







					def __call__(				self   :	Union[str, Any]    ,lowerCamelCase__   :	Optional[int]			):
									UpperCAmelCase__             =							self.vision_model(lowerCamelCase__			)[1]
									UpperCAmelCase__             =							self.visual_projection(lowerCamelCase__			)

									UpperCAmelCase__             =							jax_cosine_distance(lowerCamelCase__    ,self.special_care_embeds			)
									UpperCAmelCase__             =							jax_cosine_distance(lowerCamelCase__    ,self.concept_embeds			)

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

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

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

									return has_nsfw_concepts






class 				snake_case       ( __UpperCAmelCase				):




					"""simple docstring"""

					snake_case__				    = CLIPConfig
					snake_case__				    = "clip_input"
					snake_case__				    = FlaxStableDiffusionSafetyCheckerModule







					def __init__(				self   :	Optional[Any]    ,lowerCamelCase__   :	CLIPConfig    ,lowerCamelCase__   :	Optional[Tuple] = None    ,lowerCamelCase__   :	int = 0    ,lowerCamelCase__   :	jnp.dtype = jnp.floataa    ,lowerCamelCase__   :	bool = True    ,**lowerCamelCase__   :	Any    ,):
									if input_shape is None:
													UpperCAmelCase__             =							(1, 224, 224, 3)
									UpperCAmelCase__             =							self.module_class(config=lowerCamelCase__    ,dtype=lowerCamelCase__    ,**lowerCamelCase__			)
									super().__init__(lowerCamelCase__    ,lowerCamelCase__    ,input_shape=lowerCamelCase__    ,seed=lowerCamelCase__    ,dtype=lowerCamelCase__    ,_do_init=_do_init			)







					def 		__lowerCAmelCase						(				self   :	List[Any]    ,lowerCamelCase__   :	jax.random.KeyArray    ,lowerCamelCase__   :	Tuple    ,lowerCamelCase__   :	FrozenDict = None			):
									# init input tensor
									UpperCAmelCase__             =							jax.random.normal(lowerCamelCase__    ,lowerCamelCase__			)

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

									UpperCAmelCase__             =							self.module.init(lowerCamelCase__    ,lowerCamelCase__			)['params']

									return random_params







					def __call__(				self   :	Dict    ,lowerCamelCase__   :	Any    ,lowerCamelCase__   :	dict = None    ,):
									UpperCAmelCase__             =							jnp.transpose(lowerCamelCase__    ,(0, 2, 3, 1)			)

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




"""simple docstring"""






import warnings
from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


lowerCAmelCase__  :  Optional[int]						     =	logging.get_logger(__name__)
class 				snake_case       ( __UpperCAmelCase				):




					"""simple docstring"""

					snake_case__				    = ["input_ids", "attention_mask"]







					def __init__(				self   :	Optional[int]    ,lowerCamelCase__   :	int="</s>"    ,lowerCamelCase__   :	str="<unk>"    ,lowerCamelCase__   :	Union[str, Any]="<pad>"    ,lowerCamelCase__   :	int=125    ,lowerCamelCase__   :	str=None    ,**lowerCamelCase__   :	Union[str, Any]    ,):
									# Add extra_ids to the special token list
									if extra_ids > 0 and additional_special_tokens is None:
													UpperCAmelCase__             =							[f'''<extra_id_{i}>''' for i in range(lowerCamelCase__			)]
									elif extra_ids > 0 and additional_special_tokens is not None:
													# Check that we have the right number of extra_id special tokens
													UpperCAmelCase__             =							len(set(filter(lambda lowerCamelCase__			: bool('extra_id' in str(lowerCamelCase__			)			)    ,lowerCamelCase__			)			)			)
													if extra_tokens != extra_ids:
																	raise ValueError(
																	    f'''Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are'''
																	    ' provided to ByT5Tokenizer. In this case the additional_special_tokens must include the'
																	    ' extra_ids tokens'			)

									UpperCAmelCase__             =							AddedToken(lowerCamelCase__    ,lstrip=lowerCamelCase__    ,rstrip=lowerCamelCase__			) if isinstance(lowerCamelCase__    ,lowerCamelCase__			) else pad_token
									UpperCAmelCase__             =							AddedToken(lowerCamelCase__    ,lstrip=lowerCamelCase__    ,rstrip=lowerCamelCase__			) if isinstance(lowerCamelCase__    ,lowerCamelCase__			) else eos_token
									UpperCAmelCase__             =							AddedToken(lowerCamelCase__    ,lstrip=lowerCamelCase__    ,rstrip=lowerCamelCase__			) if isinstance(lowerCamelCase__    ,lowerCamelCase__			) else unk_token

									super().__init__(
									    eos_token=lowerCamelCase__    ,unk_token=lowerCamelCase__    ,pad_token=lowerCamelCase__    ,extra_ids=lowerCamelCase__    ,additional_special_tokens=lowerCamelCase__    ,**lowerCamelCase__    ,)

									UpperCAmelCase__             =							extra_ids

									UpperCAmelCase__             =							2**8  # utf is 8 bits

									# define special tokens dict
									UpperCAmelCase__             =							{
									    self.pad_token: 0,
									    self.eos_token: 1,
									    self.unk_token: 2,
									}
									UpperCAmelCase__             =							len(self.special_tokens_encoder			)
									UpperCAmelCase__             =							len(lowerCamelCase__			)
									for i, token in enumerate(lowerCamelCase__			):
													UpperCAmelCase__             =							self.vocab_size + i - n
									UpperCAmelCase__             =							{v: k for k, v in self.special_tokens_encoder.items()}







					@property
					def 		__lowerCAmelCase						(				self   :	Union[str, Any]			):
									return self._utf_vocab_size + self._num_special_tokens + self._extra_ids







					def 		__lowerCAmelCase						(				self   :	Optional[Any]    ,lowerCamelCase__   :	List[int]    ,lowerCamelCase__   :	Optional[List[int]] = None    ,lowerCamelCase__   :	bool = False			):
									if already_has_special_tokens:
													return super().get_special_tokens_mask(
													    token_ids_a=lowerCamelCase__    ,token_ids_a=lowerCamelCase__    ,already_has_special_tokens=lowerCamelCase__			)

									# normal case: some special tokens
									if token_ids_a is None:
													return ([0] * len(lowerCamelCase__			)) + [1]
									return ([0] * len(lowerCamelCase__			)) + [1] + ([0] * len(lowerCamelCase__			)) + [1]







					def 		__lowerCAmelCase						(				self   :	str    ,lowerCamelCase__   :	List[int]			):
									if len(lowerCamelCase__			) > 0 and token_ids[-1] == self.eos_token_id:
													warnings.warn(
													    f'''This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated'''
													    ' eos tokens being added.'			)
													return token_ids
									else:
													return token_ids + [self.eos_token_id]







					def 		__lowerCAmelCase						(				self   :	str    ,lowerCamelCase__   :	List[int]    ,lowerCamelCase__   :	Optional[List[int]] = None			):
									UpperCAmelCase__             =							[self.eos_token_id]

									if token_ids_a is None:
													return len(token_ids_a + eos			) * [0]
									return len(token_ids_a + eos + token_ids_a + eos			) * [0]







					def 		__lowerCAmelCase						(				self   :	Dict    ,lowerCamelCase__   :	List[int]    ,lowerCamelCase__   :	Optional[List[int]] = None			):
									UpperCAmelCase__             =							self._add_eos_if_not_present(lowerCamelCase__			)
									if token_ids_a is None:
													return token_ids_a
									else:
													UpperCAmelCase__             =							self._add_eos_if_not_present(lowerCamelCase__			)
													return token_ids_a + token_ids_a







					def 		__lowerCAmelCase						(				self   :	Optional[Any]    ,lowerCamelCase__   :	str			):
									UpperCAmelCase__             =							[chr(lowerCamelCase__			) for i in text.encode('utf-8'			)]
									return tokens







					def 		__lowerCAmelCase						(				self   :	List[Any]    ,lowerCamelCase__   :	str			):
									if token in self.special_tokens_encoder:
													UpperCAmelCase__             =							self.special_tokens_encoder[token]
									elif token in self.added_tokens_encoder:
													UpperCAmelCase__             =							self.added_tokens_encoder[token]
									elif len(lowerCamelCase__			) != 1:
													UpperCAmelCase__             =							self.unk_token_id
									else:
													UpperCAmelCase__             =							ord(lowerCamelCase__			) + self._num_special_tokens
									return token_id







					def 		__lowerCAmelCase						(				self   :	Union[str, Any]    ,lowerCamelCase__   :	Any			):
									if index in self.special_tokens_decoder:
													UpperCAmelCase__             =							self.special_tokens_decoder[index]
									else:
													UpperCAmelCase__             =							chr(index - self._num_special_tokens			)
									return token







					def 		__lowerCAmelCase						(				self   :	Optional[Any]    ,lowerCamelCase__   :	Union[str, Any]			):
									UpperCAmelCase__             =							b''
									for token in tokens:
													if token in self.special_tokens_decoder:
																	UpperCAmelCase__             =							self.special_tokens_decoder[token].encode('utf-8'			)
													elif token in self.added_tokens_decoder:
																	UpperCAmelCase__             =							self.special_tokens_decoder[token].encode('utf-8'			)
													elif token in self.special_tokens_encoder:
																	UpperCAmelCase__             =							token.encode('utf-8'			)
													elif token in self.added_tokens_encoder:
																	UpperCAmelCase__             =							token.encode('utf-8'			)
													else:
																	UpperCAmelCase__             =							bytes([ord(lowerCamelCase__			)]			)
													bstring += tok_string
									UpperCAmelCase__             =							bstring.decode('utf-8'    ,errors='ignore'			)
									return string







					def 		__lowerCAmelCase						(				self   :	str    ,lowerCamelCase__   :	str    ,lowerCamelCase__   :	Optional[str] = None			):
									return ()



