
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 						_snake_case(  SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__=1e-1_2				) ->    Optional[Any]:
       lowercase							:     str       	=      jnp.divide(emb_a.T					,	jnp.clip(jnp.linalg.norm(SCREAMING_SNAKE_CASE__					,	axis=1				)					,	a_min=SCREAMING_SNAKE_CASE__				)				).T
       lowercase							:     List[Any]       	=      jnp.divide(emb_a.T					,	jnp.clip(jnp.linalg.norm(SCREAMING_SNAKE_CASE__					,	axis=1				)					,	a_min=SCREAMING_SNAKE_CASE__				)				).T
       return jnp.matmul(SCREAMING_SNAKE_CASE__					,	norm_emb_a.T				)

class __snake_case		(      nn.Module     ):
    _a		:						CLIPConfig
    _a		:						jnp.dtype=				jnp.floataa
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     int       	=      FlaxCLIPVisionModule(self.config.vision_config      )
           lowercase							:     int       	=      nn.Dense(self.config.projection_dim		,use_bias=snake_case		,dtype=self.dtype      )

           lowercase							:     Union[str, Any]       	=      self.param("""concept_embeds"""		,jax.nn.initializers.ones		,(17, self.config.projection_dim)      )
           lowercase							:     List[Any]       	=      self.param(
               """special_care_embeds"""		,jax.nn.initializers.ones		,(3, self.config.projection_dim)      )

           lowercase							:     int       	=      self.param("""concept_embeds_weights"""		,jax.nn.initializers.ones		,(17,)      )
           lowercase							:     str       	=      self.param("""special_care_embeds_weights"""		,jax.nn.initializers.ones		,(3,)      )
    def __call__(  self		,snake_case      ):



           '''simple docstring'''







           lowercase							:     Dict       	=      self.vision_model(snake_case      )[1]
           lowercase							:     Optional[Any]       	=      self.visual_projection(snake_case      )

           lowercase							:     List[Any]       	=      jax_cosine_distance(snake_case		,self.special_care_embeds      )
           lowercase							:     List[str]       	=      jax_cosine_distance(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
           lowercase							:     Optional[Any]       	=      0.0

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

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

           return has_nsfw_concepts






class __snake_case		(      lowerCAmelCase     ):
    _a		:						str=				CLIPConfig
    _a		:						Dict=				"clip_input"
    _a		:						Optional[Any]=				FlaxStableDiffusionSafetyCheckerModule
    def __init__(  self		,snake_case		,snake_case = None		,snake_case = 0		,snake_case = jnp.floataa		,snake_case = True		,**snake_case		,):



           '''simple docstring'''







           if input_shape is None:
                  lowercase							:     List[str]       	=      (1, 224, 224, 3)
           lowercase							:     Union[str, Any]       	=      self.module_class(config=snake_case		,dtype=snake_case		,**snake_case      )
           super().__init__(snake_case		,snake_case		,input_shape=snake_case		,seed=snake_case		,dtype=snake_case		,_do_init=_do_init      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case = None      ):



           '''simple docstring'''







           lowercase							:     str       	=      jax.random.normal(snake_case		,snake_case      )

           lowercase	,			lowercase							:     int       	=      jax.random.split(snake_case      )
           lowercase							:     List[str]       	=      {"""params""": params_rng, """dropout""": dropout_rng}

           lowercase							:     Tuple       	=      self.module.init(snake_case		,snake_case      )["""params"""]

           return random_params
    def __call__(  self		,snake_case		,snake_case = None		,):



           '''simple docstring'''







           lowercase							:     Optional[int]       	=      jnp.transpose(snake_case		,(0, 2, 3, 1)      )

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





'''simple docstring'''





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_UpperCamelCase											=						logging.get_logger(__name__)

_UpperCamelCase											=						{
    'xlm-roberta-base': 'https://huggingface.co/xlm-roberta-base/resolve/main/config.json',
    'xlm-roberta-large': 'https://huggingface.co/xlm-roberta-large/resolve/main/config.json',
    'xlm-roberta-large-finetuned-conll02-dutch': (
        'https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json'
    ),
    'xlm-roberta-large-finetuned-conll02-spanish': (
        'https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json'
    ),
    'xlm-roberta-large-finetuned-conll03-english': (
        'https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json'
    ),
    'xlm-roberta-large-finetuned-conll03-german': (
        'https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json'
    ),
}






class 					lowerCamelCase_    (				SCREAMING_SNAKE_CASE_     ):
  """simple docstring"""




  a_												="""xlm-roberta"""




  def __init__( self      :	Any			,	_a      :	Optional[Any]=3_0522			,	_a      :	Optional[Any]=768			,	_a      :	int=12			,	_a      :	Tuple=12			,	_a      :	str=3072			,	_a      :	List[Any]="gelu"			,	_a      :	int=0.1			,	_a      :	Optional[int]=0.1			,	_a      :	Optional[int]=512			,	_a      :	List[Any]=2			,	_a      :	Optional[Any]=0.02			,	_a      :	str=1e-12			,	_a      :	str=1			,	_a      :	str=0			,	_a      :	Optional[Any]=2			,	_a      :	Optional[int]="absolute"			,	_a      :	int=True			,	_a      :	Tuple=None			,	**_a      :	Any			,	)					->					Dict:
    super().__init__(pad_token_id=_a			,	bos_token_id=_a			,	eos_token_id=_a			,	**_a   )

    __lowerCamelCase       :       int           = vocab_size
    __lowerCamelCase       :       Union[str, Any]           = hidden_size
    __lowerCamelCase       :       int           = num_hidden_layers
    __lowerCamelCase       :       Optional[int]           = num_attention_heads
    __lowerCamelCase       :       List[str]           = hidden_act
    __lowerCamelCase       :       Any           = intermediate_size
    __lowerCamelCase       :       int           = hidden_dropout_prob
    __lowerCamelCase       :       List[Any]           = attention_probs_dropout_prob
    __lowerCamelCase       :       Optional[int]           = max_position_embeddings
    __lowerCamelCase       :       Tuple           = type_vocab_size
    __lowerCamelCase       :       Optional[int]           = initializer_range
    __lowerCamelCase       :       Optional[int]           = layer_norm_eps
    __lowerCamelCase       :       str           = position_embedding_type
    __lowerCamelCase       :       List[Any]           = use_cache
    __lowerCamelCase       :       int           = classifier_dropout






class 					lowerCamelCase_    (				SCREAMING_SNAKE_CASE_     ):
  """simple docstring"""




  @property
  def 						_lowercase     ( self      :	Optional[Any]   )					->					Mapping[str, Mapping[int, str]]:
    if self.task == "multiple-choice":
      __lowerCamelCase       :       int           = {0: 'batch', 1: 'choice', 2: 'sequence'}
    else:
      __lowerCamelCase       :       Any           = {0: 'batch', 1: 'sequence'}
    return OrderedDict(
        [
            ('input_ids', dynamic_axis),
            ('attention_mask', dynamic_axis),
        ]   )






