


"""simple docstring"""



import argparse
from collections import defaultdict

import yaml


lowercase_							=							'docs/source/en/_toctree.yml'
def      UpperCAmelCase     (       _lowercase :   Any	)						-> List[Any]:


	"""simple docstring"""




	lowerCAmelCase_      							=			defaultdict(_lowercase	)
	lowerCAmelCase_      							=			[]
	lowerCAmelCase_      							=			[]
	for doc in doc_list:
		if "local" in doc:
			counts[doc["local"]] += 1

		if doc["title"].lower() == "overview":
			overview_doc.append({'''local''': doc['''local'''], '''title''': doc['''title''']}	)
		else:
			new_doc_list.append(_lowercase	)

	lowerCAmelCase_      							=			new_doc_list
	lowerCAmelCase_      							=			[key for key, value in counts.items() if value > 1]

	lowerCAmelCase_      							=			[]
	for duplicate_key in duplicates:
		lowerCAmelCase_      							=			list({doc['''title'''] for doc in doc_list if doc['''local'''] == duplicate_key}	)
		if len(_lowercase	) > 1:
			raise ValueError(
			    F"""{duplicate_key} is present several times in the documentation table of content at """
			    '''`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the '''
			    '''others.'''	)
		# Only add this once
		new_doc.append({'''local''': duplicate_key, '''title''': titles[0]}	)

	# Add none duplicate-keys
	new_doc.extend([doc for doc in doc_list if '''local''' not in counts or counts[doc['''local''']] == 1]	)
	lowerCAmelCase_      							=			sorted(_lowercase			,   key=lambda _lowercase	: s["title"].lower()	)

	# "overview" gets special treatment and is always first
	if len(_lowercase	) > 1:
		raise ValueError('''{doc_list} has two \'overview\' docs which is not allowed.'''	)

	overview_doc.extend(_lowercase	)

	# Sort
	return overview_doc
def      UpperCAmelCase     (       _lowercase :   Union[str, Any]=False	)						-> Optional[int]:


	"""simple docstring"""




	with open(_lowercase			,   encoding='''utf-8'''	) as f:
		lowerCAmelCase_      							=			yaml.safe_load(f.read()	)

	# Get to the API doc
	lowerCAmelCase_      							=			0
	while content[api_idx]["title"] != "API":
		api_idx += 1
	lowerCAmelCase_      							=			content[api_idx]['''sections''']

	# Then to the model doc
	lowerCAmelCase_      							=			0
	while api_doc[scheduler_idx]["title"] != "Schedulers":
		scheduler_idx += 1

	lowerCAmelCase_      							=			api_doc[scheduler_idx]['''sections''']
	lowerCAmelCase_      							=			clean_doc_toc(_lowercase	)

	lowerCAmelCase_      							=			False
	if new_scheduler_doc != scheduler_doc:
		lowerCAmelCase_      							=			True
		if overwrite:
			lowerCAmelCase_      							=			new_scheduler_doc

	if diff:
		if overwrite:
			lowerCAmelCase_      							=			api_doc
			with open(_lowercase			,   '''w'''			,   encoding='''utf-8'''	) as f:
				f.write(yaml.dump(_lowercase			,   allow_unicode=_lowercase	)	)
		else:
			raise ValueError(
			    '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.'''	)
def      UpperCAmelCase     (       _lowercase :   Optional[int]=False	)						-> List[Any]:


	"""simple docstring"""




	with open(_lowercase			,   encoding='''utf-8'''	) as f:
		lowerCAmelCase_      							=			yaml.safe_load(f.read()	)

	# Get to the API doc
	lowerCAmelCase_      							=			0
	while content[api_idx]["title"] != "API":
		api_idx += 1
	lowerCAmelCase_      							=			content[api_idx]['''sections''']

	# Then to the model doc
	lowerCAmelCase_      							=			0
	while api_doc[pipeline_idx]["title"] != "Pipelines":
		pipeline_idx += 1

	lowerCAmelCase_      							=			False
	lowerCAmelCase_      							=			api_doc[pipeline_idx]['''sections''']
	lowerCAmelCase_      							=			[]

	# sort sub pipeline docs
	for pipeline_doc in pipeline_docs:
		if "section" in pipeline_doc:
			lowerCAmelCase_      							=			pipeline_doc['''section''']
			lowerCAmelCase_      							=			clean_doc_toc(_lowercase	)
			if overwrite:
				lowerCAmelCase_      							=			new_sub_pipeline_doc
		new_pipeline_docs.append(_lowercase	)

	# sort overall pipeline doc
	lowerCAmelCase_      							=			clean_doc_toc(_lowercase	)

	if new_pipeline_docs != pipeline_docs:
		lowerCAmelCase_      							=			True
		if overwrite:
			lowerCAmelCase_      							=			new_pipeline_docs

	if diff:
		if overwrite:
			lowerCAmelCase_      							=			api_doc
			with open(_lowercase			,   '''w'''			,   encoding='''utf-8'''	) as f:
				f.write(yaml.dump(_lowercase			,   allow_unicode=_lowercase	)	)
		else:
			raise ValueError(
			    '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.'''	)


if __name__ == "__main__":
		lowercase_							=							argparse.ArgumentParser()
		parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
		lowercase_							=							parser.parse_args()

		check_scheduler_doc(args.fix_and_overwrite)
		check_pipeline_doc(args.fix_and_overwrite)



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


	"""simple docstring"""




	lowerCAmelCase_      							=			jnp.divide(emb_a.T			,   jnp.clip(jnp.linalg.norm(_lowercase			,   axis=1	)			,   a_min=_lowercase	)	).T
	lowerCAmelCase_      							=			jnp.divide(emb_a.T			,   jnp.clip(jnp.linalg.norm(_lowercase			,   axis=1	)			,   a_min=_lowercase	)	).T
	return jnp.matmul(_lowercase			,   norm_emb_a.T	)







class    __a						( nn.Module							):
				lowerCamelCase     :       CLIPConfig
				lowerCamelCase     :       jnp.dtype			       =jnp.floataa







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			FlaxCLIPVisionModule(self.config.vision_config   )
					lowerCAmelCase_      							=			nn.Dense(self.config.projection_dim      ,	use_bias=UpperCAmelCase      ,	dtype=self.dtype   )

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

					lowerCAmelCase_      							=			self.param('''concept_embeds_weights'''      ,	jax.nn.initializers.ones      ,	(17,)   )
					lowerCAmelCase_      							=			self.param('''special_care_embeds_weights'''      ,	jax.nn.initializers.ones      ,	(3,)   )







				def __call__( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.vision_model(UpperCAmelCase   )[1]
					lowerCAmelCase_      							=			self.visual_projection(UpperCAmelCase   )

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

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

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

					return has_nsfw_concepts







class    __a						( __snake_case							):
				lowerCamelCase     :       Optional[Any]			       =CLIPConfig
				lowerCamelCase     :       List[str]			       ='clip_input'
				lowerCamelCase     :       int			       =FlaxStableDiffusionSafetyCheckerModule







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

					'''simple docstring'''






					if input_shape is None:
						lowerCAmelCase_      							=			(1, 224, 224, 3)
					lowerCAmelCase_      							=			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'''






					lowerCAmelCase_      							=			jax.random.normal(UpperCAmelCase      ,	UpperCAmelCase   )

					lowerCAmelCase_							,	lowerCAmelCase_      							=			jax.random.split(UpperCAmelCase   )
					lowerCAmelCase_      							=			{'''params''': params_rng, '''dropout''': dropout_rng}

					lowerCAmelCase_      							=			self.module.init(UpperCAmelCase      ,	UpperCAmelCase   )['''params''']

					return random_params







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

					'''simple docstring'''






					lowerCAmelCase_      							=			jnp.transpose(UpperCAmelCase      ,	(0, 2, 3, 1)   )

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