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 	__lowerCamelCase(UpperCAmelCase__     :							int      , UpperCAmelCase__     :							List[Any]      , UpperCAmelCase__     :							Optional[int]=1e-12  ):
					SCREAMING_SNAKE_CASE    			=       jnp.divide(emb_a.T      , jnp.clip(jnp.linalg.norm(lowerCamelCase__      , axis=1  )      , a_min=lowerCamelCase__  )  ).T
					SCREAMING_SNAKE_CASE    			=       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__						:       CLIPConfig
							lowercase__						:       jnp.dtype								=							jnp.floataa




							def 	__snake_case(						self   :					Any      )					->				List[str]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       FlaxCLIPVisionModule(self.config.vision_config      )
												SCREAMING_SNAKE_CASE    			=       nn.Dense(self.config.projection_dim	,							use_bias=_UpperCamelCase	,							dtype=self.dtype      )

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

												SCREAMING_SNAKE_CASE    			=       self.param("concept_embeds_weights"	,							jax.nn.initializers.ones	,							(17,)      )
												SCREAMING_SNAKE_CASE    			=       self.param("special_care_embeds_weights"	,							jax.nn.initializers.ones	,							(3,)      )





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



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       self.vision_model(_UpperCamelCase      )[1]
												SCREAMING_SNAKE_CASE    			=       self.visual_projection(_UpperCamelCase      )

												SCREAMING_SNAKE_CASE    			=       jax_cosine_distance(_UpperCamelCase	,							self.special_care_embeds      )
												SCREAMING_SNAKE_CASE    			=       jax_cosine_distance(_UpperCamelCase	,							self.concept_embeds      )

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

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

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

												return has_nsfw_concepts



class 						lowercase    (     a     ):
							lowercase__						:       Dict								=							CLIPConfig
							lowercase__						:       Dict								=							"""clip_input"""
							lowercase__						:       List[str]								=							FlaxStableDiffusionSafetyCheckerModule




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



												'''simple docstring'''

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




							def 	__snake_case(						self   :					List[Any]	,							_UpperCamelCase   :					jax.random.KeyArray	,							_UpperCamelCase   :					Tuple	,							_UpperCamelCase   :					FrozenDict = None      )					->				FrozenDict:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       jax.random.normal(_UpperCamelCase	,							_UpperCamelCase      )

												SCREAMING_SNAKE_CASE			,   SCREAMING_SNAKE_CASE    			=       jax.random.split(_UpperCamelCase      )
												SCREAMING_SNAKE_CASE    			=       {"params": params_rng, "dropout": dropout_rng}

												SCREAMING_SNAKE_CASE    			=       self.module.init(_UpperCamelCase	,							_UpperCamelCase      )["params"]

												return random_params





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



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       jnp.transpose(_UpperCamelCase	,							(0, 2, 3, 1)      )

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





import collections
import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


_lowerCamelCase					:   Dict							=		logging.get_logger(__name__)

_lowerCamelCase					:   List[Any]							=		'''▁'''

_lowerCamelCase					:   Optional[int]							=		{'''vocab_file''': '''prophetnet.tokenizer'''}

_lowerCamelCase					:   str							=		{
    '''vocab_file''': {
        '''microsoft/xprophetnet-large-wiki100-cased''': (
            '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer'''
        ),
    }
}

_lowerCamelCase					:   Optional[Any]							=		{
    '''microsoft/xprophetnet-large-wiki100-cased''': {'''do_lower_case''': False},
}

_lowerCamelCase					:   Optional[Any]							=		{
    '''microsoft/xprophetnet-large-wiki100-cased''': 5_12,
}





def 	__lowerCamelCase							(UpperCAmelCase__     :							Optional[Any]  ):
					SCREAMING_SNAKE_CASE    			=       collections.OrderedDict()
					with open(UpperCAmelCase__      , "r"      , encoding="utf-8"  ) as reader:
										SCREAMING_SNAKE_CASE    			=       reader.readlines()
					for index, token in enumerate(UpperCAmelCase__  ):
										SCREAMING_SNAKE_CASE    			=       token.rstrip("\n"  )
										SCREAMING_SNAKE_CASE    			=       index
					return vocab


class 						lowercase    (     a     ):
							lowercase__						:       Optional[int]								=							VOCAB_FILES_NAMES
							lowercase__						:       Optional[Any]								=							PRETRAINED_VOCAB_FILES_MAP
							lowercase__						:       Union[str, Any]								=							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							lowercase__						:       Any								=							["""input_ids""", """attention_mask"""]




							def __init__(						self   :					Optional[int]	,							_UpperCamelCase   :					List[str]	,							_UpperCamelCase   :					str="[SEP]"	,							_UpperCamelCase   :					str="[SEP]"	,							_UpperCamelCase   :					Dict="[SEP]"	,							_UpperCamelCase   :					Tuple="[UNK]"	,							_UpperCamelCase   :					Dict="[PAD]"	,							_UpperCamelCase   :					Any="[CLS]"	,							_UpperCamelCase   :					Optional[Any]="[MASK]"	,							_UpperCamelCase   :					Optional[Dict[str, Any]] = None	,							**_UpperCamelCase   :					Dict	,							)					->				None:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       {} if sp_model_kwargs is None else sp_model_kwargs

												super().__init__(
												    bos_token=_UpperCamelCase	,							eos_token=_UpperCamelCase	,							sep_token=_UpperCamelCase	,							unk_token=_UpperCamelCase	,							pad_token=_UpperCamelCase	,							cls_token=_UpperCamelCase	,							mask_token=_UpperCamelCase	,							sp_model_kwargs=self.sp_model_kwargs	,							**_UpperCamelCase	,							)

												try:
																	import sentencepiece as spm
												except ImportError:
																	logger.warning(
																	    "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece"
																	    " pip install sentencepiece"      )
																	raise

												SCREAMING_SNAKE_CASE    			=       spm.SentencePieceProcessor(**self.sp_model_kwargs      )
												self.sp_model.Load(str(_UpperCamelCase      )      )
												SCREAMING_SNAKE_CASE    			=       vocab_file

												# Original fairseq vocab and spm vocab must be "aligned":
												# Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
												# -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
												# fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
												# spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

												# put special tokens and [unused] tokens into the vocab
												SCREAMING_SNAKE_CASE    			=       {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4}

												for i in range(10      ):
																	SCREAMING_SNAKE_CASE    			=       F"[unused{i}]"
																	SCREAMING_SNAKE_CASE    			=       5 + i

												# The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab
												SCREAMING_SNAKE_CASE    			=       12
												SCREAMING_SNAKE_CASE    			=       {v: k for k, v in self.fairseq_tokens_to_ids.items()}
												for k in self.fairseq_tokens_to_ids.keys():
																	self.unique_no_split_tokens.append(_UpperCamelCase      )




							def __getstate__(						self   :					Dict      )					->				str:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       self.__dict__.copy()
												SCREAMING_SNAKE_CASE    			=       None
												return state




							def __setstate__(						self   :					List[Any]	,							_UpperCamelCase   :					Union[str, Any]      )					->				Dict:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       d
												try:
																	import sentencepiece as spm
												except ImportError:
																	logger.warning(
																	    "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece"
																	    " pip install sentencepiece"      )
																	raise

												# for backward compatibility
												if not hasattr(self	,							"sp_model_kwargs"      ):
																	SCREAMING_SNAKE_CASE    			=       {}

												SCREAMING_SNAKE_CASE    			=       spm.SentencePieceProcessor(**self.sp_model_kwargs      )
												self.sp_model.Load(self.vocab_file      )




							def 	__snake_case(						self   :					Dict	,							_UpperCamelCase   :					List[int]	,							_UpperCamelCase   :					Optional[List[int]] = None	,							_UpperCamelCase   :					bool = False      )					->				List[int]:



												'''simple docstring'''

												if already_has_special_tokens:
																	return super().get_special_tokens_mask(
																	    token_ids_a=_UpperCamelCase	,							token_ids_a=_UpperCamelCase	,							already_has_special_tokens=_UpperCamelCase      )

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




							def 	__snake_case(						self   :					str	,							_UpperCamelCase   :					List[int]	,							_UpperCamelCase   :					Optional[List[int]] = None      )					->				List[int]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       [self.sep_token_id]

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




							@property
							def 	__snake_case(						self   :					Dict      )					->				Optional[Any]:



												'''simple docstring'''

												return len(self.sp_model      ) + self.fairseq_offset




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



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       {self.convert_ids_to_tokens(_UpperCamelCase      ): i for i in range(self.vocab_size      )}
												vocab.update(self.added_tokens_encoder      )
												return vocab




							def 	__snake_case(						self   :					Union[str, Any]	,							_UpperCamelCase   :					str      )					->				str:



												'''simple docstring'''

												return self.sp_model.encode(_UpperCamelCase	,							out_type=_UpperCamelCase      )




							def 	__snake_case(						self   :					Optional[Any]	,							_UpperCamelCase   :					List[Any]      )					->				List[str]:



												'''simple docstring'''

												if token in self.fairseq_tokens_to_ids:
																	return self.fairseq_tokens_to_ids[token]
												SCREAMING_SNAKE_CASE    			=       self.sp_model.PieceToId(_UpperCamelCase      )

												# Need to return unknown token if the SP model returned 0
												return spm_id + self.fairseq_offset if spm_id else self.unk_token_id




							def 	__snake_case(						self   :					str	,							_UpperCamelCase   :					str      )					->				int:



												'''simple docstring'''

												if index in self.fairseq_ids_to_tokens:
																	return self.fairseq_ids_to_tokens[index]
												return self.sp_model.IdToPiece(index - self.fairseq_offset      )




							def 	__snake_case(						self   :					List[str]	,							_UpperCamelCase   :					Dict      )					->				Optional[Any]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       "".join(_UpperCamelCase      ).replace(_UpperCamelCase	,							" "      ).strip()
												return out_string




							def 	__snake_case(						self   :					Optional[Any]	,							_UpperCamelCase   :					str	,							_UpperCamelCase   :					Optional[str] = None      )					->				Tuple[str]:



												'''simple docstring'''

												if not os.path.isdir(_UpperCamelCase      ):
																	logger.error(F"Vocabulary path ({save_directory}) should be a directory"      )
																	return
												SCREAMING_SNAKE_CASE    			=       os.path.join(
												    _UpperCamelCase	,							(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]      )

												if os.path.abspath(self.vocab_file      ) != os.path.abspath(_UpperCamelCase      ) and os.path.isfile(self.vocab_file      ):
																	copyfile(self.vocab_file	,							_UpperCamelCase      )
												elif not os.path.isfile(self.vocab_file      ):
																	with open(_UpperCamelCase	,							"wb"      ) as fi:
																						SCREAMING_SNAKE_CASE    			=       self.sp_model.serialized_model_proto()
																						fi.write(_UpperCamelCase      )

												return (out_vocab_file,)





							def 	__snake_case(						self   :					Optional[Any]	,							_UpperCamelCase   :					List[int]	,							_UpperCamelCase   :					Optional[List[int]] = None      )					->				List[int]:



												'''simple docstring'''

												if token_ids_a is None:
																	return token_ids_a + [self.sep_token_id]
												SCREAMING_SNAKE_CASE    			=       [self.sep_token_id]
												return token_ids_a + sep + token_ids_a + sep




