





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_ (							A_    ,A_    ,A_=1e-12):
    UpperCamelCase__:	Optional[Any]					=     jnp.divide(emb_a.T    ,jnp.clip(jnp.linalg.norm(A_    ,axis=1)    ,a_min=A_)).T
    UpperCamelCase__:	Any					=     jnp.divide(emb_a.T    ,jnp.clip(jnp.linalg.norm(A_    ,axis=1)    ,a_min=A_)).T
    return jnp.matmul(A_    ,norm_emb_a.T)




class        _a    (    nn.Module):

      """simple docstring"""


      UpperCamelCase__  		=       42
      UpperCamelCase__  		=       jnp.floataa






      def 			UpperCAmelCase_       (		self:			int					):
          '''simple docstring'''


          UpperCamelCase__:	str					=     FlaxCLIPVisionModule(self.config.vision_config					)
          UpperCamelCase__:	Tuple					=     nn.Dense(self.config.projection_dim	,    use_bias=__lowerCamelCase	,    dtype=self.dtype					)

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

          UpperCamelCase__:	Optional[int]					=     self.param("concept_embeds_weights"	,    jax.nn.initializers.ones	,    (17,)					)
          UpperCamelCase__:	int					=     self.param("special_care_embeds_weights"	,    jax.nn.initializers.ones	,    (3,)					)






      def __call__(		self:			Tuple	,    __lowerCamelCase:			Dict					):
          '''simple docstring'''


          UpperCamelCase__:	List[str]					=     self.vision_model(__lowerCamelCase					)[1]
          UpperCamelCase__:	Dict					=     self.visual_projection(__lowerCamelCase					)

          UpperCamelCase__:	Dict					=     jax_cosine_distance(__lowerCamelCase	,    self.special_care_embeds					)
          UpperCamelCase__:	Optional[Any]					=     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__:	Optional[int]					=     0.0

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

          UpperCamelCase__:	int					=     cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
          UpperCamelCase__:	Union[str, Any]					=     jnp.round(__lowerCamelCase	,    3					)
          UpperCamelCase__:	Tuple					=     jnp.any(concept_scores > 0	,    axis=1					)

          return has_nsfw_concepts




class        _a    (    UpperCamelCase__):

      """simple docstring"""


      UpperCamelCase__  		=       CLIPConfig
      UpperCamelCase__  		=       """clip_input"""
      UpperCamelCase__  		=       FlaxStableDiffusionSafetyCheckerModule






      def __init__(		self:			Any	,    __lowerCamelCase:			CLIPConfig	,    __lowerCamelCase:			Optional[Tuple] = None	,    __lowerCamelCase:			int = 0	,    __lowerCamelCase:			jnp.dtype = jnp.floataa	,    __lowerCamelCase:			bool = True	,    **__lowerCamelCase:			int	,    ):
          '''simple docstring'''


          if input_shape is None:
              UpperCamelCase__:	List[Any]					=     (1, 224, 224, 3)
          UpperCamelCase__:	List[Any]					=     self.module_class(config=__lowerCamelCase	,    dtype=__lowerCamelCase	,    **__lowerCamelCase					)
          super().__init__(__lowerCamelCase	,    __lowerCamelCase	,    input_shape=__lowerCamelCase	,    seed=__lowerCamelCase	,    dtype=__lowerCamelCase	,    _do_init=_do_init					)






      def 			UpperCAmelCase_       (		self:			Any	,    __lowerCamelCase:			jax.random.KeyArray	,    __lowerCamelCase:			Tuple	,    __lowerCamelCase:			FrozenDict = None					):
          '''simple docstring'''


          UpperCamelCase__:	str					=     jax.random.normal(__lowerCamelCase	,    __lowerCamelCase					)

          UpperCamelCase__				, UpperCamelCase__:	List[str]					=     jax.random.split(__lowerCamelCase					)
          UpperCamelCase__:	Tuple					=     {"params": params_rng, "dropout": dropout_rng}

          UpperCamelCase__:	Optional[Any]					=     self.module.init(__lowerCamelCase	,    __lowerCamelCase					)["params"]

          return random_params






      def __call__(		self:			int	,    __lowerCamelCase:			Any	,    __lowerCamelCase:			dict = None	,    ):
          '''simple docstring'''


          UpperCamelCase__:	Any					=     jnp.transpose(__lowerCamelCase	,    (0, 2, 3, 1)					)

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








import logging

from transformers import PretrainedConfig


A__:    Dict  	=	logging.getLogger(__name__)


A__:    List[Any]  	=	{
    '''bertabs-finetuned-cnndm''': '''https://huggingface.co/remi/bertabs-finetuned-cnndm-extractive-abstractive-summarization/resolve/main/config.json''',
}




class        _a    (    UpperCamelCase__):

      """simple docstring"""


      UpperCamelCase__  		=       """bertabs"""






      def __init__(		self:			Any	,    __lowerCamelCase:			Optional[int]=3_0522	,    __lowerCamelCase:			List[Any]=512	,    __lowerCamelCase:			Optional[Any]=6	,    __lowerCamelCase:			Optional[Any]=512	,    __lowerCamelCase:			List[str]=8	,    __lowerCamelCase:			List[Any]=512	,    __lowerCamelCase:			Optional[int]=0.2	,    __lowerCamelCase:			Optional[int]=6	,    __lowerCamelCase:			Optional[Any]=768	,    __lowerCamelCase:			int=8	,    __lowerCamelCase:			Union[str, Any]=2048	,    __lowerCamelCase:			Dict=0.2	,    **__lowerCamelCase:			Dict	,    ):
          '''simple docstring'''


          super().__init__(**__lowerCamelCase					)

          UpperCamelCase__:	str					=     vocab_size
          UpperCamelCase__:	Tuple					=     max_pos

          UpperCamelCase__:	Union[str, Any]					=     enc_layers
          UpperCamelCase__:	Optional[Any]					=     enc_hidden_size
          UpperCamelCase__:	Optional[Any]					=     enc_heads
          UpperCamelCase__:	Dict					=     enc_ff_size
          UpperCamelCase__:	Tuple					=     enc_dropout

          UpperCamelCase__:	Tuple					=     dec_layers
          UpperCamelCase__:	int					=     dec_hidden_size
          UpperCamelCase__:	int					=     dec_heads
          UpperCamelCase__:	str					=     dec_ff_size
          UpperCamelCase__:	Union[str, Any]					=     dec_dropout

