



'''simple docstring'''





import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCamelCase											=		logging.get_logger(__name__)

_UpperCamelCase											=		{
    'microsoft/wavlm-base': 'https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json',
    # See all WavLM models at https://huggingface.co/models?filter=wavlm
}






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




  a_				   ="""wavlm"""




  def __init__(					self :      List[str]     ,      _a :      Tuple=32     ,      _a :      Optional[int]=768     ,      _a :      int=12     ,      _a :      Union[str, Any]=12     ,      _a :      str=3072     ,      _a :      List[Any]="gelu"     ,      _a :      Union[str, Any]=0.1     ,      _a :      str=0.1     ,      _a :      List[Any]=0.1     ,      _a :      str=0.0     ,      _a :      List[str]=0.1     ,      _a :      Optional[Any]=0.1     ,      _a :      str=0.02     ,      _a :      List[str]=1e-5     ,      _a :      Union[str, Any]="group"     ,      _a :      Optional[int]="gelu"     ,      _a :      List[str]=(512, 512, 512, 512, 512, 512, 512)     ,      _a :      Any=(5, 2, 2, 2, 2, 2, 2)     ,      _a :      int=(10, 3, 3, 3, 3, 2, 2)     ,      _a :      List[str]=False     ,      _a :      List[Any]=128     ,      _a :      str=16     ,      _a :      Any=320     ,      _a :      List[str]=800     ,      _a :      Dict=False     ,      _a :      List[Any]=True     ,      _a :      str=0.05     ,      _a :      List[str]=10     ,      _a :      Optional[int]=2     ,      _a :      Dict=0.0     ,      _a :      Any=10     ,      _a :      Union[str, Any]=320     ,      _a :      Any=2     ,      _a :      str=0.1     ,      _a :      str=100     ,      _a :      Optional[int]=256     ,      _a :      Optional[int]=256     ,      _a :      Optional[Any]=0.1     ,      _a :      int="mean"     ,      _a :      List[Any]=False     ,      _a :      List[str]=False     ,      _a :      Tuple=256     ,      _a :      Dict=(512, 512, 512, 512, 1500)     ,      _a :      str=(5, 3, 3, 1, 1)     ,      _a :      List[str]=(1, 2, 3, 1, 1)     ,      _a :      Optional[Any]=512     ,      _a :      List[str]=80     ,      _a :      Dict=0     ,      _a :      List[Any]=1     ,      _a :      Optional[int]=2     ,      _a :      Optional[int]=False     ,      _a :      Tuple=3     ,      _a :      Any=2     ,      _a :      Optional[Any]=3     ,      _a :      Any=None     ,      **_a :      Any     ,      )   ->   Optional[int]:
    super().__init__(**_a     ,      pad_token_id=_a     ,      bos_token_id=_a     ,      eos_token_id=_a  )
    __lowerCamelCase			:			Any       			=					hidden_size
    __lowerCamelCase			:			Union[str, Any]       			=					feat_extract_norm
    __lowerCamelCase			:			Any       			=					feat_extract_activation
    __lowerCamelCase			:			Dict       			=					list(_a  )
    __lowerCamelCase			:			Optional[Any]       			=					list(_a  )
    __lowerCamelCase			:			Union[str, Any]       			=					list(_a  )
    __lowerCamelCase			:			Union[str, Any]       			=					conv_bias
    __lowerCamelCase			:			List[Any]       			=					num_buckets
    __lowerCamelCase			:			List[str]       			=					max_bucket_distance
    __lowerCamelCase			:			Dict       			=					num_conv_pos_embeddings
    __lowerCamelCase			:			Optional[int]       			=					num_conv_pos_embedding_groups
    __lowerCamelCase			:			Dict       			=					len(self.conv_dim  )
    __lowerCamelCase			:			Any       			=					num_hidden_layers
    __lowerCamelCase			:			Optional[int]       			=					intermediate_size
    __lowerCamelCase			:			Optional[Any]       			=					hidden_act
    __lowerCamelCase			:			List[str]       			=					num_attention_heads
    __lowerCamelCase			:			int       			=					hidden_dropout
    __lowerCamelCase			:			Optional[int]       			=					attention_dropout
    __lowerCamelCase			:			Union[str, Any]       			=					activation_dropout
    __lowerCamelCase			:			int       			=					feat_proj_dropout
    __lowerCamelCase			:			Union[str, Any]       			=					final_dropout
    __lowerCamelCase			:			int       			=					layerdrop
    __lowerCamelCase			:			Any       			=					layer_norm_eps
    __lowerCamelCase			:			Optional[Any]       			=					initializer_range
    __lowerCamelCase			:			Union[str, Any]       			=					num_ctc_classes
    __lowerCamelCase			:			int       			=					vocab_size
    __lowerCamelCase			:			List[Any]       			=					do_stable_layer_norm
    __lowerCamelCase			:			List[str]       			=					use_weighted_layer_sum
    __lowerCamelCase			:			Tuple       			=					classifier_proj_size

    if (
        (len(self.conv_stride  ) != self.num_feat_extract_layers)
        or (len(self.conv_kernel  ) != self.num_feat_extract_layers)
        or (len(self.conv_dim  ) != self.num_feat_extract_layers)
    ):
      raise ValueError(
          'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='
          ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='
          f' {len(self.conv_dim  )}`, `len(config.conv_stride) = {len(self.conv_stride  )}`,'
          f' `len(config.conv_kernel) = {len(self.conv_kernel  )}`.'  )

    # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
    __lowerCamelCase			:			str       			=					apply_spec_augment
    __lowerCamelCase			:			str       			=					mask_time_prob
    __lowerCamelCase			:			Optional[Any]       			=					mask_time_length
    __lowerCamelCase			:			Any       			=					mask_time_min_masks
    __lowerCamelCase			:			Union[str, Any]       			=					mask_feature_prob
    __lowerCamelCase			:			List[Any]       			=					mask_feature_length

    # parameters for pretraining with codevector quantized representations
    __lowerCamelCase			:			Optional[Any]       			=					num_codevectors_per_group
    __lowerCamelCase			:			List[Any]       			=					num_codevector_groups
    __lowerCamelCase			:			Tuple       			=					contrastive_logits_temperature
    __lowerCamelCase			:			Optional[Any]       			=					num_negatives
    __lowerCamelCase			:			Any       			=					codevector_dim
    __lowerCamelCase			:			Tuple       			=					proj_codevector_dim
    __lowerCamelCase			:			Any       			=					diversity_loss_weight

    # ctc loss
    __lowerCamelCase			:			List[str]       			=					ctc_loss_reduction
    __lowerCamelCase			:			Tuple       			=					ctc_zero_infinity

    # adapter
    __lowerCamelCase			:			Any       			=					add_adapter
    __lowerCamelCase			:			Dict       			=					adapter_kernel_size
    __lowerCamelCase			:			Optional[int]       			=					adapter_stride
    __lowerCamelCase			:			List[Any]       			=					num_adapter_layers
    __lowerCamelCase			:			Any       			=					output_hidden_size or hidden_size

    # SequenceClassification-specific parameter. Feel free to ignore for other classes.
    __lowerCamelCase			:			Tuple       			=					classifier_proj_size

    # XVector-specific parameters. Feel free to ignore for other classes.
    __lowerCamelCase			:			int       			=					list(_a  )
    __lowerCamelCase			:			List[Any]       			=					list(_a  )
    __lowerCamelCase			:			Tuple       			=					list(_a  )
    __lowerCamelCase			:			List[str]       			=					xvector_output_dim




  @property
  def 		_lowercase	(					self :      List[str]  )   ->   Any:
    return functools.reduce(operator.mul     ,      self.conv_stride     ,      1  )











'''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-12			) -> List[Any]:
  __lowerCamelCase			:			int       			=					jnp.divide(emb_a.T			,jnp.clip(jnp.linalg.norm(_lowerCAmelCase			,axis=1			)			,a_min=_lowerCAmelCase			)			).T
  __lowerCamelCase			:			Tuple       			=					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    lowerCamelCase_       (							nn.Module	):
  """simple docstring"""




  a_				   =42
  a_				   =jnp.floataa




  def 		_lowercase	(					self :      Optional[Any]  )   ->   Tuple:
    __lowerCamelCase			:			Optional[int]       			=					FlaxCLIPVisionModule(self.config.vision_config  )
    __lowerCamelCase			:			Any       			=					nn.Dense(self.config.projection_dim     ,      use_bias=_a     ,      dtype=self.dtype  )

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

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




  def __call__(					self :      Tuple     ,      _a :      Any  )   ->   Optional[int]:
    __lowerCamelCase			:			int       			=					self.vision_model(_a  )[1]
    __lowerCamelCase			:			Optional[int]       			=					self.visual_projection(_a  )

    __lowerCamelCase			:			Optional[Any]       			=					jax_cosine_distance(_a     ,      self.special_care_embeds  )
    __lowerCamelCase			:			Tuple       			=					jax_cosine_distance(_a     ,      self.concept_embeds  )

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

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

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

    return has_nsfw_concepts






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




  a_				   =CLIPConfig
  a_				   ="""clip_input"""
  a_				   =FlaxStableDiffusionSafetyCheckerModule




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




  def 		_lowercase	(					self :      int     ,      _a :      jax.random.KeyArray     ,      _a :      Tuple     ,      _a :      FrozenDict = None  )   ->   FrozenDict:
    # init input tensor
    __lowerCamelCase			:			Optional[int]       			=					jax.random.normal(_a     ,      _a  )

    __lowerCamelCase			,__lowerCamelCase			:			Optional[Any]       			=					jax.random.split(_a  )
    __lowerCamelCase			:			int       			=					{'params': params_rng, 'dropout': dropout_rng}

    __lowerCamelCase			:			Tuple       			=					self.module.init(_a     ,      _a  )['params']

    return random_params




  def __call__(					self :      Optional[int]     ,      _a :      int     ,      _a :      dict = None     ,      )   ->   Dict:
    __lowerCamelCase			:			str       			=					jnp.transpose(_a     ,      (0, 2, 3, 1)  )

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






