




'''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 						snake_case_       (UpperCamelCase			:    Dict					,		UpperCamelCase			:    Dict					,		UpperCamelCase			:    str=1e-12 ):





							'''simple docstring'''


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

class      A       (						nn.Module       ):
							lowercase_							     =							42
							lowercase_							     =							jnp.floataa







							def   __lowerCAmelCase     (    self			:    Tuple   )     ->				Optional[int]:


														"""simple docstring"""


														_a      	=							FlaxCLIPVisionModule(self.config.vision_config   )
														_a      	=							nn.Dense(self.config.projection_dim				,     use_bias=lowerCAmelCase_				,     dtype=self.dtype   )

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

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







							def __call__(    self			:    Any				,     lowerCAmelCase_			:    int   )     ->				List[str]:


														"""simple docstring"""


														_a      	=							self.vision_model(lowerCAmelCase_   )[1]
														_a      	=							self.visual_projection(lowerCAmelCase_   )

														_a      	=							jax_cosine_distance(lowerCAmelCase_				,     self.special_care_embeds   )
														_a      	=							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
														_a      	=							0.0

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

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

														return has_nsfw_concepts



class      A       (						_a       ):
							lowercase_							     =							CLIPConfig
							lowercase_							     =							'clip_input'
							lowercase_							     =							FlaxStableDiffusionSafetyCheckerModule







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


														"""simple docstring"""


														if input_shape is None:
																					_a      	=							(1, 2_24, 2_24, 3)
														_a      	=							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			:    Union[str, Any]				,     lowerCAmelCase_			:    jax.random.KeyArray				,     lowerCAmelCase_			:    Tuple				,     lowerCAmelCase_			:    FrozenDict = None   )     ->				FrozenDict:


														"""simple docstring"""


														_a      	=							jax.random.normal(lowerCAmelCase_				,     lowerCAmelCase_   )

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

														_a      	=							self.module.init(lowerCAmelCase_				,     lowerCAmelCase_   )['''params''']

														return random_params







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


														"""simple docstring"""


														_a      	=							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 copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..bit import BitConfig


UpperCAmelCase__			=    logging.get_logger(__name__)

UpperCAmelCase__			=    {
    """Intel/dpt-large""": """https://huggingface.co/Intel/dpt-large/resolve/main/config.json""",
    # See all DPT models at https://huggingface.co/models?filter=dpt
}


class 							a					(		lowerCAmelCase_ ):
    _snake_case  :	int  				=				'dpt'


    def __init__(       self						:	Any ,					__lowerCAmelCase						:	Optional[Any]=768 ,					__lowerCAmelCase						:	str=12 ,					__lowerCAmelCase						:	Optional[int]=12 ,					__lowerCAmelCase						:	Dict=3072 ,					__lowerCAmelCase						:	List[Any]="gelu" ,					__lowerCAmelCase						:	Tuple=0.0 ,					__lowerCAmelCase						:	str=0.0 ,					__lowerCAmelCase						:	List[Any]=0.02 ,					__lowerCAmelCase						:	Optional[Any]=1e-1_2 ,					__lowerCAmelCase						:	List[str]=384 ,					__lowerCAmelCase						:	Tuple=16 ,					__lowerCAmelCase						:	List[str]=3 ,					__lowerCAmelCase						:	Optional[Any]=False ,					__lowerCAmelCase						:	Optional[Any]=True ,					__lowerCAmelCase						:	Optional[Any]=[2, 5, 8, 11] ,					__lowerCAmelCase						:	Any="project" ,					__lowerCAmelCase						:	Dict=[4, 2, 1, 0.5] ,					__lowerCAmelCase						:	int=[96, 192, 384, 768] ,					__lowerCAmelCase						:	Dict=256 ,					__lowerCAmelCase						:	Optional[Any]=-1 ,					__lowerCAmelCase						:	Optional[Any]=False ,					__lowerCAmelCase						:	Union[str, Any]=True ,					__lowerCAmelCase						:	List[Any]=0.4 ,					__lowerCAmelCase						:	List[str]=255 ,					__lowerCAmelCase						:	List[Any]=0.1 ,					__lowerCAmelCase						:	Optional[int]=[1, 1024, 24, 24] ,					__lowerCAmelCase						:	List[Any]=[0, 1] ,					__lowerCAmelCase						:	Dict=None ,					**__lowerCAmelCase						:	str ,					):
      super().__init__(**__lowerCAmelCase       )

      _UpperCAmelCase    			=   hidden_size
      _UpperCAmelCase    			=   is_hybrid

      if self.is_hybrid:
        if backbone_config is None:
          logger.info("""Initializing the config with a `BiT` backbone."""       )
          _UpperCAmelCase    			=   {
              """global_padding""": """same""",
              """layer_type""": """bottleneck""",
              """depths""": [3, 4, 9],
              """out_features""": ["""stage1""", """stage2""", """stage3"""],
              """embedding_dynamic_padding""": True,
          }
          _UpperCAmelCase    			=   BitConfig(**__lowerCAmelCase       )
        elif isinstance(__lowerCAmelCase ,					__lowerCAmelCase       ):
          logger.info("""Initializing the config with a `BiT` backbone."""       )
          _UpperCAmelCase    			=   BitConfig(**__lowerCAmelCase       )
        elif isinstance(__lowerCAmelCase ,					__lowerCAmelCase       ):
          _UpperCAmelCase    			=   backbone_config
        else:
          raise ValueError(
              f'''backbone_config must be a dictionary or a `PretrainedConfig`, got {backbone_config.__class__}.'''       )

        _UpperCAmelCase    			=   backbone_featmap_shape
        _UpperCAmelCase    			=   neck_ignore_stages

        if readout_type != "project":
          raise ValueError("""Readout type must be 'project' when using `DPT-hybrid` mode."""       )
      else:
        _UpperCAmelCase    			=   None
        _UpperCAmelCase    			=   None
        _UpperCAmelCase    			=   []

      _UpperCAmelCase    			=   num_hidden_layers
      _UpperCAmelCase    			=   num_attention_heads
      _UpperCAmelCase    			=   intermediate_size
      _UpperCAmelCase    			=   hidden_act
      _UpperCAmelCase    			=   hidden_dropout_prob
      _UpperCAmelCase    			=   attention_probs_dropout_prob
      _UpperCAmelCase    			=   initializer_range
      _UpperCAmelCase    			=   layer_norm_eps
      _UpperCAmelCase    			=   image_size
      _UpperCAmelCase    			=   patch_size
      _UpperCAmelCase    			=   num_channels
      _UpperCAmelCase    			=   qkv_bias
      _UpperCAmelCase    			=   backbone_out_indices
      if readout_type not in ["ignore", "add", "project"]:
        raise ValueError("""Readout_type must be one of ['ignore', 'add', 'project']"""       )
      _UpperCAmelCase    			=   readout_type
      _UpperCAmelCase    			=   reassemble_factors
      _UpperCAmelCase    			=   neck_hidden_sizes
      _UpperCAmelCase    			=   fusion_hidden_size
      _UpperCAmelCase    			=   head_in_index
      _UpperCAmelCase    			=   use_batch_norm_in_fusion_residual
      # auxiliary head attributes (semantic segmentation)
      _UpperCAmelCase    			=   use_auxiliary_head
      _UpperCAmelCase    			=   auxiliary_loss_weight
      _UpperCAmelCase    			=   semantic_loss_ignore_index
      _UpperCAmelCase    			=   semantic_classifier_dropout


    def   lowerCAmelCase_	(       self						:	List[str]       ):
      _UpperCAmelCase    			=   copy.deepcopy(self.__dict__       )

      if output["backbone_config"] is not None:
        _UpperCAmelCase    			=   self.backbone_config.to_dict()

      _UpperCAmelCase    			=   self.__class__.model_type
      return output
