


'''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     _UpperCamelCase      (		UpperCamelCase__ ,      UpperCamelCase__ ,      UpperCamelCase__=1e-12					):
			UpperCAmelCase__  :					Any        =     jnp.divide(emb_a.T ,      jnp.clip(jnp.linalg.norm(a_ ,      axis=1					) ,      a_min=a_					)					).T
			UpperCAmelCase__  :					str        =     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 		_snake_case					(     nn.Module	):
					lowerCAmelCase      :Any   =					42
					lowerCAmelCase      :Optional[Any]   =					jnp.floataa




					def 				snake_case__						(      self):
								UpperCAmelCase__  :					Any        =     FlaxCLIPVisionModule(self.config.vision_config)
								UpperCAmelCase__  :					int        =     nn.Dense(self.config.projection_dim							,							use_bias=_UpperCAmelCase							,							dtype=self.dtype)

								UpperCAmelCase__  :					Any        =     self.param("""concept_embeds"""							,							jax.nn.initializers.ones							,							(17, self.config.projection_dim))
								UpperCAmelCase__  :					Dict        =     self.param(
								    """special_care_embeds"""							,							jax.nn.initializers.ones							,							(3, self.config.projection_dim))

								UpperCAmelCase__  :					Tuple        =     self.param("""concept_embeds_weights"""							,							jax.nn.initializers.ones							,							(17,))
								UpperCAmelCase__  :					Union[str, Any]        =     self.param("""special_care_embeds_weights"""							,							jax.nn.initializers.ones							,							(3,))




					def __call__(      self							,							_lowerCamelCase):
								UpperCAmelCase__  :					Optional[Any]        =     self.vision_model(_UpperCAmelCase)[1]
								UpperCAmelCase__  :					Dict        =     self.visual_projection(_UpperCAmelCase)

								UpperCAmelCase__  :					List[str]        =     jax_cosine_distance(_UpperCAmelCase							,							self.special_care_embeds)
								UpperCAmelCase__  :					Any        =     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__  :					List[str]        =     0.0

								UpperCAmelCase__  :					str        =     special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
								UpperCAmelCase__  :					Tuple        =     jnp.round(_UpperCAmelCase							,							3)
								UpperCAmelCase__  :					Tuple        =     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__  :					Union[str, Any]        =     cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
								UpperCAmelCase__  :					Dict        =     jnp.round(_UpperCAmelCase							,							3)
								UpperCAmelCase__  :					str        =     jnp.any(concept_scores > 0							,							axis=1)

								return has_nsfw_concepts


class 		_snake_case					(     _UpperCamelCase	):
					lowerCAmelCase      :List[str]   =					CLIPConfig
					lowerCAmelCase      :Optional[int]   =					'''clip_input'''
					lowerCAmelCase      :Tuple   =					FlaxStableDiffusionSafetyCheckerModule




					def __init__(      self							,							_lowerCamelCase							,							_lowerCamelCase = None							,							_lowerCamelCase = 0							,							_lowerCamelCase = jnp.floataa							,							_lowerCamelCase = True							,							**_lowerCamelCase							,							):
								if input_shape is None:
											UpperCAmelCase__  :					str        =     (1, 224, 224, 3)
								UpperCAmelCase__  :					Optional[int]        =     self.module_class(config=_UpperCAmelCase							,							dtype=_UpperCAmelCase							,							**_UpperCAmelCase)
								super().__init__(_UpperCAmelCase							,							_UpperCAmelCase							,							input_shape=_UpperCAmelCase							,							seed=_UpperCAmelCase							,							dtype=_UpperCAmelCase							,							_do_init=_do_init)




					def 				snake_case__						(      self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase = None):
								# init input tensor
								UpperCAmelCase__  :					Any        =     jax.random.normal(_UpperCAmelCase							,							_UpperCAmelCase)

								UpperCAmelCase__  :					List[str]        =     jax.random.split(_UpperCAmelCase)
								UpperCAmelCase__  :					Tuple        =     {'''params''': params_rng, '''dropout''': dropout_rng}

								UpperCAmelCase__  :					str        =     self.module.init(_UpperCAmelCase							,							_UpperCAmelCase)['''params''']

								return random_params




					def __call__(      self							,							_lowerCamelCase							,							_lowerCamelCase = None							,							):
								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'''



def     _UpperCamelCase      (		UpperCamelCase__					):
			# if the collection is empty, returns empty
			if collection == []:
						return []

			# get some information about the collection
			UpperCAmelCase__  :					List[str]        =     len(UpperCamelCase__					)
			UpperCAmelCase__  :					Union[str, Any]        =     max(UpperCamelCase__					)
			UpperCAmelCase__  :					Optional[Any]        =     min(UpperCamelCase__					)

			# create the counting array
			UpperCAmelCase__  :					Dict        =     coll_max + 1 - coll_min
			UpperCAmelCase__  :					Optional[int]        =     [0] * counting_arr_length

			# count how much a number appears in the collection
			for number in collection:
						counting_arr[number - coll_min] += 1

			# sum each position with it's predecessors. now, counting_arr[i] tells
			# us how many elements <= i has in the collection
			for i in range(1 ,      UpperCamelCase__					):
						UpperCAmelCase__  :					List[str]        =     counting_arr[i] + counting_arr[i - 1]

			# create the output collection
			UpperCAmelCase__  :					Tuple        =     [0] * coll_len

			# place the elements in the output, respecting the original order (stable
			# sort) from end to begin, updating counting_arr
			for i in reversed(range(0 ,      UpperCamelCase__					)					):
						UpperCAmelCase__  :					Any        =     collection[i]
						counting_arr[collection[i] - coll_min] -= 1

			return ordered







def     _UpperCamelCase      (		UpperCamelCase__					):
			return "".join([chr(UpperCamelCase__					) for i in counting_sort([ord(UpperCamelCase__					) for c in string]					)]					)


if __name__ == "__main__":
		# Test string sort
		assert counting_sort_string('thisisthestring') == "eghhiiinrsssttt"

		__A	=input('Enter numbers separated by a comma:\n').strip()
		__A	=[int(item) for item in user_input.split(',')]
		print(counting_sort(unsorted))