
"""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        _lowerCamelCase ( __a,							__a,							__a=1E-12    ):
		SCREAMING_SNAKE_CASE_						 = jnp.divide(emb_a.T,							jnp.clip(jnp.linalg.norm(__a,							axis=1    ),							a_min=__a    )    ).T
		SCREAMING_SNAKE_CASE_						 = 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   ):
							UpperCAmelCase__						     =      42
							UpperCAmelCase__						     =      jnp.floataa

							def _lowercase					(self    ):

									"""simple docstring"""







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

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

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







							def __call__(self       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = self.vision_model(SCREAMING_SNAKE_CASE_    )[1]
									SCREAMING_SNAKE_CASE_						 = self.visual_projection(SCREAMING_SNAKE_CASE_    )

									SCREAMING_SNAKE_CASE_						 = jax_cosine_distance(SCREAMING_SNAKE_CASE_       ,       self.special_care_embeds    )
									SCREAMING_SNAKE_CASE_						 = jax_cosine_distance(SCREAMING_SNAKE_CASE_       ,       self.concept_embeds    )

									# increase this value to create a stronger `nfsw` filter
									# at the cost of increasing the possibility of filtering benign image inputs
									SCREAMING_SNAKE_CASE_						 = 0.0

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

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

									return has_nsfw_concepts







class  snake_case     (							__lowercase   ):
							UpperCAmelCase__						     =      CLIPConfig
							UpperCAmelCase__						     =      '''clip_input'''
							UpperCAmelCase__						     =      FlaxStableDiffusionSafetyCheckerModule

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

									"""simple docstring"""







									if input_shape is None:
											SCREAMING_SNAKE_CASE_						 = (1, 2_24, 2_24, 3)
									SCREAMING_SNAKE_CASE_						 = self.module_class(config=SCREAMING_SNAKE_CASE_       ,       dtype=SCREAMING_SNAKE_CASE_       ,       **SCREAMING_SNAKE_CASE_    )
									super().__init__(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       input_shape=SCREAMING_SNAKE_CASE_       ,       seed=SCREAMING_SNAKE_CASE_       ,       dtype=SCREAMING_SNAKE_CASE_       ,       _do_init=_do_init    )

							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_ = None    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = jax.random.normal(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )

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

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

									return random_params







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

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = jnp.transpose(SCREAMING_SNAKE_CASE_       ,       (0, 2, 3, 1)    )

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

"""simple docstring"""




from collections.abc import Sequence
from queue import Queue





class  snake_case     :

							def __init__(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_=None       ,       SCREAMING_SNAKE_CASE_=None    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = start
									SCREAMING_SNAKE_CASE_						 = end
									SCREAMING_SNAKE_CASE_						 = val
									SCREAMING_SNAKE_CASE_						 = (start + end) // 2
									SCREAMING_SNAKE_CASE_						 = left
									SCREAMING_SNAKE_CASE_						 = right







							def __repr__(self    ):

									"""simple docstring"""







									return f'SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})'







class  snake_case     :

							def __init__(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = collection
									SCREAMING_SNAKE_CASE_						 = function
									if self.collection:
											SCREAMING_SNAKE_CASE_						 = self._build_tree(0       ,       len(SCREAMING_SNAKE_CASE_    ) - 1    )

							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									self._update_tree(self.root       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )

							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									return self._query_range(self.root       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )

							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									if start == end:
											return SegmentTreeNode(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       self.collection[start]    )
									SCREAMING_SNAKE_CASE_						 = (start + end) // 2
									SCREAMING_SNAKE_CASE_						 = self._build_tree(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = self._build_tree(mid + 1       ,       SCREAMING_SNAKE_CASE_    )
									return SegmentTreeNode(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       self.fn(left.val       ,       right.val    )       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )

							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									if node.start == i and node.end == i:
											SCREAMING_SNAKE_CASE_						 = val
											return
									if i <= node.mid:
											self._update_tree(node.left       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )
									else:
											self._update_tree(node.right       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = self.fn(node.left.val       ,       node.right.val    )

							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									if node.start == i and node.end == j:
											return node.val

									if i <= node.mid:
											if j <= node.mid:
													# range in left child tree
													return self._query_range(node.left       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )
											else:
													# range in left child tree and right child tree
													return self.fn(
													    self._query_range(node.left       ,       SCREAMING_SNAKE_CASE_       ,       node.mid    )       ,       self._query_range(node.right       ,       node.mid + 1       ,       SCREAMING_SNAKE_CASE_    )       ,       )
									else:
											# range in right child tree
											return self._query_range(node.right       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )







							def _lowercase					(self    ):

									"""simple docstring"""







									if self.root is not None:
											SCREAMING_SNAKE_CASE_						 = Queue()
											queue.put(self.root    )
											while not queue.empty():
													SCREAMING_SNAKE_CASE_						 = queue.get()
													yield node

													if node.left is not None:
															queue.put(node.left    )

													if node.right is not None:
															queue.put(node.right    )


if __name__ == "__main__":
						import operator

						for fn in [operator.add, max, min]:
												print('*' * 50)
												lowerCAmelCase__							=				SegmentTree([2, 1, 5, 3, 4], fn)
												for node in arr.traverse():
																		print(node)
												print()

												arr.update(1, 5)
												for node in arr.traverse():
																		print(node)
												print()

												print(arr.query_range(3, 4))  # 7
												print(arr.query_range(2, 2))  # 5
												print(arr.query_range(1, 3))  # 13
												print()