
'''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   (    __a				,      __a				,      __a=1e-12     )						->			List[str]:



					'''simple docstring'''






					UpperCamelCase__     :int							      =				jnp.divide(emb_a.T				,      jnp.clip(jnp.linalg.norm(__a				,      axis=1     )				,      a_min=__a     )     ).T
					UpperCamelCase__     :Optional[int]							      =				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      lowercase			(							nn.Module      ):

		"""simple docstring"""

		_a									=       42
		_a									=       jnp.floataa




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :List[str]							      =				FlaxCLIPVisionModule(self.config.vision_config							)
							UpperCamelCase__     :Union[str, Any]							      =				nn.Dense(self.config.projection_dim					,     use_bias=UpperCamelCase_					,     dtype=self.dtype							)

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

							UpperCamelCase__     :Union[str, Any]							      =				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					,     UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__     :Tuple							      =				self.vision_model(UpperCamelCase_							)[1]
							UpperCamelCase__     :List[str]							      =				self.visual_projection(UpperCamelCase_							)

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

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

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

							return has_nsfw_concepts



class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       CLIPConfig
		_a									=       'clip_input'
		_a									=       FlaxStableDiffusionSafetyCheckerModule




		def __init__(	self					,     UpperCamelCase_					,     UpperCamelCase_ = None					,     UpperCamelCase_ = 0					,     UpperCamelCase_ = jnp.floataa					,     UpperCamelCase_ = True					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


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




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_ = None							):

							'''simple docstring'''


							UpperCamelCase__     :Optional[Any]							      =				jax.random.normal(UpperCamelCase_					,     UpperCamelCase_							)

							UpperCamelCase__     :List[Any]							      =				jax.random.split(UpperCamelCase_							)
							UpperCamelCase__     :Any							      =				{'''params''': params_rng, '''dropout''': dropout_rng}

							UpperCamelCase__     :Any							      =				self.module.init(UpperCamelCase_					,     UpperCamelCase_							)['''params''']

							return random_params




		def __call__(	self					,     UpperCamelCase_					,     UpperCamelCase_ = None					,     ):

							'''simple docstring'''


							UpperCamelCase__     :List[str]							      =				jnp.transpose(UpperCamelCase_					,     (0, 2, 3, 1)							)

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



'''simple docstring'''
from .integrations import (
    is_optuna_available,
    is_ray_available,
    is_sigopt_available,
    is_wandb_available,
    run_hp_search_optuna,
    run_hp_search_ray,
    run_hp_search_sigopt,
    run_hp_search_wandb,
)
from .trainer_utils import (
    HPSearchBackend,
    default_hp_space_optuna,
    default_hp_space_ray,
    default_hp_space_sigopt,
    default_hp_space_wandb,
)
from .utils import logging


__snake_case							   =  logging.get_logger(__name__)
class      lowercase			:

		"""simple docstring"""

		_a									=       42
		_a									=       None




		@staticmethod
		def 					lowerCAmelCase__       (	):

							'''simple docstring'''


							raise NotImplementedError




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							raise NotImplementedError




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_							):

							'''simple docstring'''


							raise NotImplementedError




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							if not self.is_available():
												raise RuntimeError(
												    F'''You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}.'''							)




		@classmethod
		def 					lowerCAmelCase__       (	cls							):

							'''simple docstring'''


							return F'''`pip install {cls.pip_package or cls.name}`'''



class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       'optuna'




		@staticmethod
		def 					lowerCAmelCase__       (	):

							'''simple docstring'''


							return is_optuna_available()




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return run_hp_search_optuna(UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_							):

							'''simple docstring'''


							return default_hp_space_optuna(UpperCamelCase_							)



class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       'ray'
		_a									=       '\'ray[tune]\''




		@staticmethod
		def 					lowerCAmelCase__       (	):

							'''simple docstring'''


							return is_ray_available()




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return run_hp_search_ray(UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_							):

							'''simple docstring'''


							return default_hp_space_ray(UpperCamelCase_							)



class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       'sigopt'




		@staticmethod
		def 					lowerCAmelCase__       (	):

							'''simple docstring'''


							return is_sigopt_available()




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return run_hp_search_sigopt(UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_							):

							'''simple docstring'''


							return default_hp_space_sigopt(UpperCamelCase_							)



class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       'wandb'




		@staticmethod
		def 					lowerCAmelCase__       (	):

							'''simple docstring'''


							return is_wandb_available()




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return run_hp_search_wandb(UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_							):

							'''simple docstring'''


							return default_hp_space_wandb(UpperCamelCase_							)



__snake_case							   =  {
    HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend]
}


def     a   (    )						->			str:



					'''simple docstring'''






					UpperCamelCase__     :List[Any]							      =				[backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()]
					if len(__a     ) > 0:
										UpperCamelCase__     :Tuple							      =				available_backends[0].name
										if len(__a     ) > 1:
															logger.info(
															    f'''{len(__a     )} hyperparameter search backends available. Using {name} as the default.'''     )
										return name
					raise RuntimeError(
					    '''No hyperparameter search backend available.\n'''
					    + '''\n'''.join(
					        f''' - To install {backend.name} run {backend.pip_install()}'''
					        for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values()     )     )