
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			(	a_		,   a_		,   a_=1E-12						)		->	List[str]:







				"""simple docstring"""



				__A   			= jnp.divide(emb_a.T		,   jnp.clip(jnp.linalg.norm(a_		,   axis=1						)		,   a_min=a_						)						).T
				__A   			= 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 							UpperCAmelCase      (  nn.Module  ):




	'''simple docstring'''





	snake_case_					    =     42
	snake_case_					    =     jnp.floataa






	def 				UpperCamelCase_  (       self					: List[str] ):
					__A   			= FlaxCLIPVisionModule(self.config.vision_config )
					__A   			= nn.Dense(self.config.projection_dim							,use_bias=A							,dtype=self.dtype )

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

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







	def __call__(       self					: Tuple							,A					: Any ):
					__A   			= self.vision_model(A )[1]
					__A   			= self.visual_projection(A )

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

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

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

					return has_nsfw_concepts





class 							UpperCAmelCase      (  __SCREAMING_SNAKE_CASE  ):




	'''simple docstring'''





	snake_case_					    =     CLIPConfig
	snake_case_					    =     "clip_input"
	snake_case_					    =     FlaxStableDiffusionSafetyCheckerModule






	def __init__(       self					: int							,A					: CLIPConfig							,A					: Optional[Tuple] = None							,A					: int = 0							,A					: jnp.dtype = jnp.floataa							,A					: bool = True							,**A					: Tuple							,):
					if input_shape is None:
									__A   			= (1, 2_24, 2_24, 3)
					__A   			= self.module_class(config=A							,dtype=A							,**A )
					super().__init__(A							,A							,input_shape=A							,seed=A							,dtype=A							,_do_init=_do_init )






	def 				UpperCamelCase_  (       self					: int							,A					: jax.random.KeyArray							,A					: Tuple							,A					: FrozenDict = None ):
					# init input tensor
					__A   			= jax.random.normal(A							,A )

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

					__A   			= self.module.init(A							,A )["params"]

					return random_params







	def __call__(       self					: Tuple							,A					: Dict							,A					: dict = None							,):
					__A   			= 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 __future__ import annotations


def  snake_case_     (					_lowerCAmelCase  :						str   ,      _lowerCAmelCase  :						str  )      ->   bool:
							UpperCAmelCase :       str 							=		get_failure_array(_lowerCAmelCase  )

							# 2) Step through text searching for pattern
							UpperCAmelCase							,		UpperCAmelCase :       Optional[Any] 							=		0, 0  # index into text, pattern
							while i < len(_lowerCAmelCase  ):
														if pattern[j] == text[i]:
																					if j == (len(_lowerCAmelCase  ) - 1):
																												return True
																					j += 1

														# if this is a prefix in our pattern
														# just go back far enough to continue
														elif j > 0:
																					UpperCAmelCase :       Optional[Any] 							=		failure[j - 1]
																					continue
														i += 1
							return False


def  snake_case_     (					_lowerCAmelCase  :						str  )      ->   list[int]:
							UpperCAmelCase :       Optional[Any] 							=		[0]
							UpperCAmelCase :       str 							=		0
							UpperCAmelCase :       List[str] 							=		1
							while j < len(_lowerCAmelCase  ):
														if pattern[i] == pattern[j]:
																					i += 1
														elif i > 0:
																					UpperCAmelCase :       Union[str, Any] 							=		failure[i - 1]
																					continue
														j += 1
														failure.append(_lowerCAmelCase  )
							return failure


if __name__ == "__main__":
						# Test 1)
						UpperCamelCase__:   str								=			"abc1abc12"
						UpperCamelCase__:   str								=			"alskfjaldsabc1abc1abc12k23adsfabcabc"
						UpperCamelCase__:   Any								=			"alskfjaldsk23adsfabcabc"
						assert kmp(pattern, texta) and not kmp(pattern, texta)

						# Test 2)
						UpperCamelCase__:   Tuple								=			"ABABX"
						UpperCamelCase__:   Union[str, Any]								=			"ABABZABABYABABX"
						assert kmp(pattern, text)

						# Test 3)
						UpperCamelCase__:   Any								=			"AAAB"
						UpperCamelCase__:   str								=			"ABAAAAAB"
						assert kmp(pattern, text)

						# Test 4)
						UpperCamelCase__:   int								=			"abcdabcy"
						UpperCamelCase__:   Any								=			"abcxabcdabxabcdabcdabcy"
						assert kmp(pattern, text)

						# Test 5)
						UpperCamelCase__:   List[str]								=			"aabaabaaa"
						assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]





