





import inspect

import jax
import jax.lax as lax
import jax.numpy as jnp

from ..utils import add_start_docstrings
from ..utils.logging import get_logger


__SCREAMING_SNAKE_CASE							:    List[Any]          =get_logger(__name__)


__SCREAMING_SNAKE_CASE							:    List[Any]          =R'''
    Args:
        input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`):
            Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam
            search or log softmax for each vocabulary token when using beam search
        kwargs (`Dict[str, Any]`, *optional*):
            Additional logits processor specific kwargs.

    Return:
        `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores.

'''

class     A_		:







			@add_start_docstrings(snake_case_      )
			def __call__(				self  :							List[Any]   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							jnp.ndarray      ):
							raise NotImplementedError(
							    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""      )

class     A_		:







			@add_start_docstrings(snake_case_      )
			def __call__(				self  :							Optional[int]   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							jnp.ndarray      ):
							raise NotImplementedError(
							    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""      )

class     A_		(			__a	):







			@add_start_docstrings(snake_case_      )
			def __call__(				self  :							int   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							int   ,      **snake_case__  :							str      ):
							for processor in self:
											lowercase							=     inspect.signature(processor.__call__      ).parameters
											if len(snake_case_      ) > 3:
															if not all(arg in kwargs for arg in list(function_args.keys()      )[2:]      ):
																			raise ValueError(
																			    F"""Make sure that all the required parameters: {list(function_args.keys()      )} for """
																			    F"""{processor.__class__} are passed to the logits processor."""      )
															lowercase							=     processor(snake_case_   ,      snake_case_   ,      snake_case_   ,      **snake_case_      )
											else:
															lowercase							=     processor(snake_case_   ,      snake_case_   ,      snake_case_      )
							return scores

class     A_		(			__a	):







			def __init__(				self  :							Optional[Any]   ,      snake_case__  :							float      ):
							if not isinstance(snake_case_   ,      snake_case_      ) or not (temperature > 0):
											raise ValueError(F"""`temperature` has to be a strictly positive float, but is {temperature}"""      )

							lowercase							=     temperature







			def __call__(				self  :							Dict   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							int      ):
							lowercase							=     scores / self.temperature
							return scores

class     A_		(			__a	):







			def __init__(				self  :							Optional[int]   ,      snake_case__  :							float   ,      snake_case__  :							float = -float("""Inf"""      )   ,      snake_case__  :							int = 1      ):
							if not isinstance(snake_case_   ,      snake_case_      ) or (top_p < 0 or top_p > 1.0):
											raise ValueError(F"""`top_p` has to be a float > 0 and < 1, but is {top_p}"""      )
							if not isinstance(snake_case_   ,      snake_case_      ) or (min_tokens_to_keep < 1):
											raise ValueError(F"""`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}"""      )

							lowercase							=     top_p
							lowercase							=     filter_value
							lowercase							=     min_tokens_to_keep







			def __call__(				self  :							int   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							int      ):
							lowercase					,    lowercase							=     lax.top_k(snake_case_   ,      scores.shape[-1]      )

							lowercase							=     jnp.full_like(snake_case_   ,      self.filter_value      )
							lowercase							=     jax.nn.softmax(snake_case_   ,      axis=-1      ).cumsum(axis=-1      )
							lowercase							=     cumulative_probs < self.top_p

							# include the token that is higher than top_p as well
							lowercase							=     jnp.roll(snake_case_   ,      1      )
							score_mask |= score_mask.at[:, 0].set(snake_case_      )

							# min tokens to keep
							lowercase							=     score_mask.at[:, : self.min_tokens_to_keep].set(snake_case_      )

							lowercase							=     jnp.where(snake_case_   ,      snake_case_   ,      snake_case_      )
							lowercase							=     jax.lax.sort_key_val(snake_case_   ,      snake_case_      )[-1]

							return next_scores

class     A_		(			__a	):







			def __init__(				self  :							Any   ,      snake_case__  :							int   ,      snake_case__  :							float = -float("""Inf"""      )   ,      snake_case__  :							int = 1      ):
							if not isinstance(snake_case_   ,      snake_case_      ) or top_k <= 0:
											raise ValueError(F"""`top_k` has to be a strictly positive integer, but is {top_k}"""      )

							lowercase							=     max(snake_case_   ,      snake_case_      )
							lowercase							=     filter_value







			def __call__(				self  :							Any   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							int      ):
							lowercase					,    lowercase							=     scores.shape
							lowercase							=     jnp.full(batch_size * vocab_size   ,      self.filter_value      )

							lowercase							=     min(self.top_k   ,      scores.shape[-1]      )  # Safety check
							lowercase					,    lowercase							=     lax.top_k(snake_case_   ,      snake_case_      )
							lowercase							=     jnp.broadcast_to((jnp.arange(snake_case_      ) * vocab_size)[:, None]   ,      (batch_size, topk)      ).flatten()
							lowercase							=     topk_scores.flatten()
							lowercase							=     topk_indices.flatten() + shift

							lowercase							=     next_scores_flat.at[topk_indices_flat].set(snake_case_      )
							lowercase							=     next_scores_flat.reshape(snake_case_   ,      snake_case_      )
							return next_scores

class     A_		(			__a	):







			def __init__(				self  :							int   ,      snake_case__  :							int      ):
							lowercase							=     bos_token_id







			def __call__(				self  :							List[str]   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							int      ):
							lowercase							=     jnp.full(scores.shape   ,      -float("""inf"""      )      )

							lowercase							=     1 - jnp.bool_(cur_len - 1      )

							lowercase							=     jnp.where(snake_case_   ,      new_scores.at[:, self.bos_token_id].set(0      )   ,      snake_case_      )

							return scores

class     A_		(			__a	):







			def __init__(				self  :							Optional[Any]   ,      snake_case__  :							int   ,      snake_case__  :							int      ):
							lowercase							=     max_length
							lowercase							=     eos_token_id







			def __call__(				self  :							str   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							int      ):
							lowercase							=     jnp.full(scores.shape   ,      -float("""inf"""      )      )

							lowercase							=     1 - jnp.bool_(cur_len - self.max_length + 1      )

							lowercase							=     jnp.where(snake_case_   ,      new_scores.at[:, self.eos_token_id].set(0      )   ,      snake_case_      )

							return scores

class     A_		(			__a	):







			def __init__(				self  :							Union[str, Any]   ,      snake_case__  :							int   ,      snake_case__  :							int      ):
							if not isinstance(snake_case_   ,      snake_case_      ) or min_length < 0:
											raise ValueError(F"""`min_length` has to be a positive integer, but is {min_length}"""      )

							if not isinstance(snake_case_   ,      snake_case_      ) or eos_token_id < 0:
											raise ValueError(F"""`eos_token_id` has to be a positive integer, but is {eos_token_id}"""      )

							lowercase							=     min_length
							lowercase							=     eos_token_id







			def __call__(				self  :							Any   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							int      ):
							# create boolean flag to decide if min length penalty should be applied
							lowercase							=     1 - jnp.clip(cur_len - self.min_length   ,      0   ,      1      )

							lowercase							=     jnp.where(snake_case_   ,      scores.at[:, self.eos_token_id].set(-float("""inf"""      )      )   ,      snake_case_      )

							return scores

class     A_		(			__a	):







			def __init__(				self  :							Optional[int]   ,      snake_case__  :							Dict   ,      snake_case__  :							str      ):
							lowercase							=     list(snake_case_      )
							lowercase							=     begin_index







			def __call__(				self  :							List[Any]   ,      snake_case__  :							Any   ,      snake_case__  :							List[Any]   ,      snake_case__  :							int      ):
							lowercase							=     1 - jnp.bool_(cur_len - self.begin_index      )

							lowercase							=     jnp.where(snake_case_   ,      scores.at[:, self.begin_suppress_tokens].set(-float("""inf"""      )      )   ,      snake_case_      )

							return scores

class     A_		(			__a	):







			def __init__(				self  :							Optional[Any]   ,      snake_case__  :							list      ):
							lowercase							=     list(snake_case_      )







			def __call__(				self  :							Optional[Any]   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							int      ):
							lowercase							=     scores.at[..., self.suppress_tokens].set(-float("""inf"""      )      )

							return scores

class     A_		(			__a	):







			def __init__(				self  :							Any   ,      snake_case__  :							str      ):
							lowercase							=     dict(snake_case_      )
							# Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the
							# index of the array corresponds to the index of the token to be forced, for XLA compatibility.
							# Indexes without forced tokens will have a negative value.
							lowercase							=     jnp.ones((max(force_token_map.keys()      ) + 1)   ,      dtype=jnp.intaa      ) * -1
							for index, token in force_token_map.items():
											if token is not None:
															lowercase							=     force_token_array.at[index].set(snake_case_      )
							lowercase							=     jnp.intaa(snake_case_      )







			def __call__(				self  :							Tuple   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							jnp.ndarray   ,      snake_case__  :							int      ):
							def _force_token(snake_case__  :							Optional[Any]      ):
											lowercase							=     scores.shape[0]
											lowercase							=     self.force_token_array[generation_idx]

											lowercase							=     jnp.ones_like(snake_case_   ,      dtype=scores.dtype      ) * -float("""inf"""      )
											lowercase							=     jnp.zeros((batch_size, 1)   ,      dtype=scores.dtype      )
											lowercase							=     lax.dynamic_update_slice(snake_case_   ,      snake_case_   ,      (0, current_token)      )
											return new_scores

							lowercase							=     lax.cond(
							    cur_len >= self.force_token_array.shape[0]   ,      lambda: scores   ,      lambda: lax.cond(
							        self.force_token_array[cur_len] >= 0   ,      lambda: _force_token(snake_case_      )   ,      lambda: scores   ,      )   ,      )
							return scores

class     A_		(			__a	):







			def __init__(				self  :							Optional[int]   ,      snake_case__  :							Any   ,      snake_case__  :							Dict   ,      snake_case__  :							Union[str, Any]      ):
							lowercase							=     generate_config.eos_token_id
							lowercase							=     generate_config.no_timestamps_token_id
							lowercase							=     generate_config.no_timestamps_token_id + 1

							lowercase							=     decoder_input_length + 1

							if generate_config.is_multilingual:
											# room for language token and task token
											self.begin_index += 2
							if hasattr(snake_case_   ,      """max_initial_timestamp_index"""      ):
											lowercase							=     generate_config.max_initial_timestamp_index
							else:
											lowercase							=     model_config.vocab_size
							if self.max_initial_timestamp_index is None:
											lowercase							=     model_config.vocab_size







			def __call__(				self  :							List[str]   ,      snake_case__  :							int   ,      snake_case__  :							int   ,      snake_case__  :							Union[str, Any]      ):
							# suppress <|notimestamps|> which is handled by without_timestamps
							lowercase							=     scores.at[:, self.no_timestamps_token_id].set(-float("""inf"""      )      )

							def handle_pairs(snake_case__  :							Union[str, Any]   ,      snake_case__  :							str      ):
											lowercase							=     jnp.where((cur_len - self.begin_index) >= 1   ,      snake_case_   ,      snake_case_      )
											lowercase							=     jnp.where(
											    input_ids_k[cur_len - 1] >= self.timestamp_begin   ,      True and last_was_timestamp   ,      snake_case_   ,      )

											lowercase							=     jnp.where((cur_len - self.begin_index) < 2   ,      snake_case_   ,      snake_case_      )
											lowercase							=     jnp.where(
											    input_ids_k[cur_len - 2] >= self.timestamp_begin   ,      snake_case_   ,      snake_case_   ,      )

											return jnp.where(
											    snake_case_   ,      jnp.where(
											        penultimate_was_timestamp > 0   ,      scores_k.at[self.timestamp_begin :].set(-float("""inf"""      )      )   ,      scores_k.at[: self.eos_token_id].set(-float("""inf"""      )      )   ,      )   ,      snake_case_   ,      )

							lowercase							=     jax.vmap(snake_case_      )(snake_case_   ,      snake_case_      )

							lowercase							=     jnp.where(cur_len == self.begin_index   ,      snake_case_   ,      snake_case_      )
							lowercase							=     jnp.where(
							    self.max_initial_timestamp_index is not None   ,      True and apply_max_initial_timestamp   ,      snake_case_   ,      )

							lowercase							=     self.timestamp_begin + self.max_initial_timestamp_index

							lowercase							=     jnp.where(
							    snake_case_   ,      scores.at[:, last_allowed + 1 :].set(-float("""inf"""      )      )   ,      snake_case_   ,      )

							# if sum of probability over timestamps is above any other token, sample timestamp
							lowercase							=     jax.nn.log_softmax(snake_case_   ,      axis=-1      )

							def handle_cumulative_probs(snake_case__  :							Any   ,      snake_case__  :							Tuple      ):
											lowercase							=     jax.nn.logsumexp(logprobs_k[self.timestamp_begin :]   ,      axis=-1      )
											lowercase							=     jnp.max(logprobs_k[: self.timestamp_begin]      )
											return jnp.where(
											    timestamp_logprob > max_text_token_logprob   ,      scores_k.at[: self.timestamp_begin].set(-float("""inf"""      )      )   ,      snake_case_   ,      )

							lowercase							=     jax.vmap(snake_case_      )(snake_case_   ,      snake_case_      )

							return scores







from collections.abc import Sequence
from queue import Queue






class     __a			:



	def __init__(    self				:     str					,						snake_case_				:     List[str]					,						snake_case_				:     Tuple					,						snake_case_				:     Tuple					,						snake_case_				:     Optional[Any]=None					,						snake_case_				:     Dict=None)->				Optional[int]:
						__lowerCAmelCase						 =start
						__lowerCAmelCase						 =end
						__lowerCAmelCase						 =val
						__lowerCAmelCase						 =(start + end) // 2
						__lowerCAmelCase						 =left
						__lowerCAmelCase						 =right



	def __repr__(    self				:     List[Any])->				Dict:
						return F"""SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})"""






class     __a			:



	def __init__(    self				:     List[str]					,						snake_case_				:     Sequence					,						snake_case_				:     int)->				str:
						__lowerCAmelCase						 =collection
						__lowerCAmelCase						 =function
						if self.collection:
											__lowerCAmelCase						 =self._build_tree(0					,						len(snake_case_) - 1)



	def 		UpperCamelCase				(    self				:     Dict					,						snake_case_				:     Tuple					,						snake_case_				:     Optional[int])->				Optional[int]:
						self._update_tree(self.root					,						snake_case_					,						snake_case_)



	def 		UpperCamelCase				(    self				:     Optional[Any]					,						snake_case_				:     Any					,						snake_case_				:     Union[str, Any])->				Union[str, Any]:
						return self._query_range(self.root					,						snake_case_					,						snake_case_)



	def 		UpperCamelCase				(    self				:     List[str]					,						snake_case_				:     List[str]					,						snake_case_				:     List[str])->				List[Any]:
						if start == end:
											return SegmentTreeNode(snake_case_					,						snake_case_					,						self.collection[start])
						__lowerCAmelCase						 =(start + end) // 2
						__lowerCAmelCase						 =self._build_tree(snake_case_					,						snake_case_)
						__lowerCAmelCase						 =self._build_tree(mid + 1					,						snake_case_)
						return SegmentTreeNode(snake_case_					,						snake_case_					,						self.fn(left.val					,						right.val)					,						snake_case_					,						snake_case_)



	def 		UpperCamelCase				(    self				:     Dict					,						snake_case_				:     Tuple					,						snake_case_				:     int					,						snake_case_				:     int)->				List[Any]:
						if node.start == i and node.end == i:
											__lowerCAmelCase						 =val
											return
						if i <= node.mid:
											self._update_tree(node.left					,						snake_case_					,						snake_case_)
						else:
											self._update_tree(node.right					,						snake_case_					,						snake_case_)
						__lowerCAmelCase						 =self.fn(node.left.val					,						node.right.val)



	def 		UpperCamelCase				(    self				:     Tuple					,						snake_case_				:     str					,						snake_case_				:     str					,						snake_case_				:     List[Any])->				Optional[int]:
						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					,						snake_case_					,						snake_case_)
											else:
																# range in left child tree and right child tree
																return self.fn(
																    self._query_range(node.left					,						snake_case_					,						node.mid)					,						self._query_range(node.right					,						node.mid + 1					,						snake_case_)					,						)
						else:
											# range in right child tree
											return self._query_range(node.right					,						snake_case_					,						snake_case_)



	def 		UpperCamelCase				(    self				:     List[str])->				Tuple:
						if self.root is not None:
											__lowerCAmelCase						 =Queue()
											queue.put(self.root)
											while not queue.empty():
																__lowerCAmelCase						 =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)
								lowercase_													=      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()




