



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__   (					UpperCAmelCase_  ,  UpperCAmelCase_  ,  UpperCAmelCase_=1E-12							):
						_UpperCamelCase :		Union[str, Any]			    =       jnp.divide(emb_a.T  ,  jnp.clip(jnp.linalg.norm(UpperCAmelCase_  ,  axis=1							)  ,  a_min=UpperCAmelCase_							)							).T
						_UpperCamelCase :		str			    =       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 							lowercase__     (							nn.Module       ):
	lowercase__      =      42
	lowercase__      =      jnp.floataa





	def      UpperCamelCase_  (			self :    Optional[int]			):







							'''simple docstring'''
							_UpperCamelCase :		int			    =       FlaxCLIPVisionModule(self.config.vision_config			)
							_UpperCamelCase :		Dict			    =       nn.Dense(self.config.projection_dim			,use_bias=lowerCamelCase__			,dtype=self.dtype			)

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

							_UpperCamelCase :		List[str]			    =       self.param('concept_embeds_weights'			,jax.nn.initializers.ones			,(17,)			)
							_UpperCamelCase :		List[Any]			    =       self.param('special_care_embeds_weights'			,jax.nn.initializers.ones			,(3,)			)





	def __call__(			self :    int			,lowerCamelCase__ :    Optional[int]			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       self.vision_model(lowerCamelCase__			)[1]
							_UpperCamelCase :		List[str]			    =       self.visual_projection(lowerCamelCase__			)

							_UpperCamelCase :		Dict			    =       jax_cosine_distance(lowerCamelCase__			,self.special_care_embeds			)
							_UpperCamelCase :		Union[str, 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 :		int			    =       0.0

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

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

							return has_nsfw_concepts






class 							lowercase__     (							lowercase       ):
	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__ :    int			,):







							'''simple docstring'''
							if input_shape is None:
													_UpperCamelCase :		List[str]			    =       (1, 224, 224, 3)
							_UpperCamelCase :		Union[str, 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 :    str			,lowerCamelCase__ :    jax.random.KeyArray			,lowerCamelCase__ :    Tuple			,lowerCamelCase__ :    FrozenDict = None			):







							'''simple docstring'''
							_UpperCamelCase :		List[str]			    =       jax.random.normal(lowerCamelCase__			,lowerCamelCase__			)

							_UpperCamelCase :		Any			    =       jax.random.split(lowerCamelCase__			)
							_UpperCamelCase :		Tuple			    =       {'params': params_rng, 'dropout': dropout_rng}

							_UpperCamelCase :		Dict			    =       self.module.init(lowerCamelCase__			,lowerCamelCase__			)['params']

							return random_params





	def __call__(			self :    Optional[Any]			,lowerCamelCase__ :    List[Any]			,lowerCamelCase__ :    dict = None			,):







							'''simple docstring'''
							_UpperCamelCase :		Union[str, Any]			    =       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'''
from __future__ import annotations

import time
from collections.abc import Sequence
from random import randint

from matplotlib import pyplot as plt






def A__   (					UpperCAmelCase_  ,  UpperCAmelCase_  ,  UpperCAmelCase_							):
						if not arr:
												return None, None, 0
						if low == high:
												return low, high, arr[low]

						_UpperCamelCase :		str			    =       (low + high) // 2
						_UpperCamelCase    ,      _UpperCamelCase    ,      _UpperCamelCase :		Dict			    =       max_subarray(UpperCAmelCase_  ,  UpperCAmelCase_  ,  UpperCAmelCase_							)
						_UpperCamelCase    ,      _UpperCamelCase    ,      _UpperCamelCase :		List[Any]			    =       max_subarray(UpperCAmelCase_  ,  mid + 1  ,  UpperCAmelCase_							)
						_UpperCamelCase    ,      _UpperCamelCase    ,      _UpperCamelCase :		Tuple			    =       max_cross_sum(UpperCAmelCase_  ,  UpperCAmelCase_  ,  UpperCAmelCase_  ,  UpperCAmelCase_							)
						if left_sum >= right_sum and left_sum >= cross_sum:
												return left_low, left_high, left_sum
						elif right_sum >= left_sum and right_sum >= cross_sum:
												return right_low, right_high, right_sum
						return cross_left, cross_right, cross_sum






def A__   (					UpperCAmelCase_  ,  UpperCAmelCase_  ,  UpperCAmelCase_  ,  UpperCAmelCase_							):
						_UpperCamelCase    ,      _UpperCamelCase :		Optional[Any]			    =       float('-inf'							), -1
						_UpperCamelCase    ,      _UpperCamelCase :		int			    =       float('-inf'							), -1

						_UpperCamelCase :		int | float			    =       0
						for i in range(UpperCAmelCase_  ,  low - 1  ,  -1							):
												summ += arr[i]
												if summ > left_sum:
																		_UpperCamelCase :		Optional[int]			    =       summ
																		_UpperCamelCase :		Union[str, Any]			    =       i

						_UpperCamelCase :		List[Any]			    =       0
						for i in range(mid + 1  ,  high + 1							):
												summ += arr[i]
												if summ > right_sum:
																		_UpperCamelCase :		List[Any]			    =       summ
																		_UpperCamelCase :		List[str]			    =       i

						return max_left, max_right, (left_sum + right_sum)






def A__   (					UpperCAmelCase_							):
						_UpperCamelCase :		List[str]			    =       [randint(1  ,  UpperCAmelCase_							) for _ in range(UpperCAmelCase_							)]
						_UpperCamelCase :		Optional[Any]			    =       time.time()
						max_subarray(UpperCAmelCase_  ,  0  ,  input_size - 1							)
						_UpperCamelCase :		str			    =       time.time()
						return end - start






def A__   (					):
						_UpperCamelCase :		Any			    =       [1_0, 1_0_0, 1_0_0_0, 1_0_0_0_0, 5_0_0_0_0, 1_0_0_0_0_0, 2_0_0_0_0_0, 3_0_0_0_0_0, 4_0_0_0_0_0, 5_0_0_0_0_0]
						_UpperCamelCase :		Dict			    =       [time_max_subarray(UpperCAmelCase_							) for input_size in input_sizes]
						print('No of Inputs\t\tTime Taken'							)
						for input_size, runtime in zip(UpperCAmelCase_  ,  UpperCAmelCase_							):
												print(UpperCAmelCase_  ,  '\t\t'  ,  UpperCAmelCase_							)
						plt.plot(UpperCAmelCase_  ,  UpperCAmelCase_							)
						plt.xlabel('Number of Inputs'							)
						plt.ylabel('Time taken in seconds'							)
						plt.show()


if __name__ == "__main__":
					from doctest import testmod

					testmod()


