


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 					UpperCamelCase   (      __magic_name__	:				List[Any]       ,       __magic_name__	:				str       ,       __magic_name__	:				List[str]=1E-1_2	)  ->    Any:
		"""simple docstring"""




		lowercase__	     =						jnp.divide(emb_a.T       ,       jnp.clip(jnp.linalg.norm(lowerCamelCase_       ,       axis=1	)       ,       a_min=lowerCamelCase_	)	).T
		lowercase__	     =						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 				A       (			nn.Module				):
					'''simple docstring'''






					A__     =					42
					A__     =					jnp.floataa

					def     lowerCamelCase__	(self       :		List[Any]						)		->  Union[str, Any]:




							"""simple docstring"""


							lowercase__	     =						FlaxCLIPVisionModule(self.config.vision_config						)
							lowercase__	     =						nn.Dense(self.config.projection_dim ,   use_bias=a__ ,   dtype=self.dtype						)

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

							lowercase__	     =						self.param("""concept_embeds_weights""" ,   jax.nn.initializers.ones ,   (17,)						)
							lowercase__	     =						self.param("""special_care_embeds_weights""" ,   jax.nn.initializers.ones ,   (3,)						)


					def __call__(self       :		Optional[int] ,   _UpperCAmelCase       :		Optional[Any]						)		->  Optional[int]:




							"""simple docstring"""


							lowercase__	     =						self.vision_model(a__						)[1]
							lowercase__	     =						self.visual_projection(a__						)

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

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

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

							return has_nsfw_concepts






class 				A       (			UpperCamelCase_				):
					'''simple docstring'''






					A__     =					CLIPConfig
					A__     =					"""clip_input"""
					A__     =					FlaxStableDiffusionSafetyCheckerModule

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




							"""simple docstring"""


							if input_shape is None:
									lowercase__	     =						(1, 224, 224, 3)
							lowercase__	     =						self.module_class(config=a__ ,   dtype=a__ ,   **a__						)
							super().__init__(a__ ,   a__ ,   input_shape=a__ ,   seed=a__ ,   dtype=a__ ,   _do_init=_do_init						)

					def     lowerCamelCase__	(self       :		Optional[int] ,   _UpperCAmelCase       :		jax.random.KeyArray ,   _UpperCAmelCase       :		Tuple ,   _UpperCAmelCase       :		FrozenDict = None						)		->  Optional[int]:




							"""simple docstring"""


							lowercase__	     =						jax.random.normal(a__ ,   a__						)

							lowercase__	     =						jax.random.split(a__						)
							lowercase__	     =						{"params": params_rng, "dropout": dropout_rng}

							lowercase__	     =						self.module.init(a__ ,   a__						)["params"]

							return random_params


					def __call__(self       :		Dict ,   _UpperCAmelCase       :		str ,   _UpperCAmelCase       :		dict = None ,   )		->  Tuple:




							"""simple docstring"""


							lowercase__	     =						jnp.transpose(a__ ,   (0, 2, 3, 1)						)

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



'''simple docstring'''

from math import factorial

snake_case									=    {str(digit): factorial(digit) for digit in range(10)}


def        UpperCAmelCase_ (  lowerCamelCase_			):







 """simple docstring"""


 if not isinstance(lowerCamelCase_      ,			lowerCamelCase_			):
  raise TypeError("Parameter number must be int"			)

 if number < 0:
  raise ValueError("Parameter number must be greater than or equal to 0"			)

 # Converts number in string to iterate on its digits and adds its factorial.
 return sum(DIGIT_FACTORIAL[digit] for digit in str(lowerCamelCase_			)			)


def        UpperCAmelCase_ (  lowerCamelCase_ = 6_0      ,			lowerCamelCase_ = 1_0_0_0_0_0_0			):







 """simple docstring"""


 if not isinstance(lowerCamelCase_      ,			lowerCamelCase_			) or not isinstance(lowerCamelCase_      ,			lowerCamelCase_			):
  raise TypeError("Parameters chain_length and number_limit must be int"			)

 if chain_length <= 0 or number_limit <= 0:
  raise ValueError(
      "Parameters chain_length and number_limit must be greater than 0"			)

 # the counter for the chains with the exact desired length
 lowerCAmelCase__     :   str     		=	0
 # the cached sizes of the previous chains
 lowerCAmelCase__     :   dict[int, int]     		=	{}

 for start_chain_element in range(1      ,			lowerCamelCase_			):
  # The temporary set will contain the elements of the chain
  lowerCAmelCase__     :   Any     		=	set()
  lowerCAmelCase__     :   int     		=	0

  # Stop computing the chain when you find a cached size, a repeating item or the
  # length is greater then the desired one.
  lowerCAmelCase__     :   Dict     		=	start_chain_element
  while (
      chain_element not in chain_sets_lengths
      and chain_element not in chain_set
      and chain_set_length <= chain_length
  ):
   chain_set.add(lowerCamelCase_			)
   chain_set_length += 1
   lowerCAmelCase__     :   Dict     		=	digit_factorial_sum(lowerCamelCase_			)

  if chain_element in chain_sets_lengths:
   chain_set_length += chain_sets_lengths[chain_element]

  lowerCAmelCase__     :   Optional[Any]     		=	chain_set_length

  # If chain contains the exact amount of elements increase the counter
  if chain_set_length == chain_length:
   chains_counter += 1

 return chains_counter


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     print(f'{solution()}')





