



"""simple docstring"""
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


A:   Any								=		get_logger(__name__)


A:   Tuple								=		R"\n    Args:\n        input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`):\n            Indices of input sequence tokens in the vocabulary.\n\n            Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and\n            [`PreTrainedTokenizer.__call__`] for details.\n\n            [What are input IDs?](../glossary#input-ids)\n        scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`):\n            Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam\n            search or log softmax for each vocabulary token when using beam search\n        kwargs (`Dict[str, Any]`, *optional*):\n            Additional logits processor specific kwargs.\n\n    Return:\n        `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores.\n\n"



class       SCREAMING_SNAKE_CASE__  :

   @add_start_docstrings(_SCREAMING_SNAKE_CASE						)
   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      jnp.ndarray:





    '''simple docstring'''




    raise NotImplementedError(
        F"{self.__class__} is an abstract class. Only classes inheriting this class can be called."						)



class       SCREAMING_SNAKE_CASE__  :

   @add_start_docstrings(_SCREAMING_SNAKE_CASE						)
   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      jnp.ndarray:





    '''simple docstring'''




    raise NotImplementedError(
        F"{self.__class__} is an abstract class. Only classes inheriting this class can be called."						)



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):

   @add_start_docstrings(_SCREAMING_SNAKE_CASE						)
   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     **_SCREAMING_SNAKE_CASE						)  ->      jnp.ndarray:





    '''simple docstring'''




    for processor in self:
     UpperCAmelCase	:					List[Any]	=	inspect.signature(processor.__call__						).parameters
     if len(_SCREAMING_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."						)
      UpperCAmelCase	:					Dict	=	processor(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     **_SCREAMING_SNAKE_CASE						)
     else:
      UpperCAmelCase	:					Any	=	processor(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    return scores



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE						)  ->      Optional[Any]:





    '''simple docstring'''




    if not isinstance(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						) or not (temperature > 0):
     raise ValueError(F"`temperature` has to be a strictly positive float, but is {temperature}"						)

    UpperCAmelCase	:					str	=	temperature




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      jnp.ndarray:





    '''simple docstring'''




    UpperCAmelCase	:					List[Any]	=	scores / self.temperature
    return scores



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE = -float("""Inf"""						)      ,     _SCREAMING_SNAKE_CASE = 1						)  ->      List[str]:





    '''simple docstring'''




    if not isinstance(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_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(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_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}"						)

    UpperCAmelCase	:					int	=	top_p
    UpperCAmelCase	:					Optional[int]	=	filter_value
    UpperCAmelCase	:					Optional[int]	=	min_tokens_to_keep




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      jnp.ndarray:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					Tuple	=	lax.top_k(_SCREAMING_SNAKE_CASE      ,     scores.shape[-1]						)

    UpperCAmelCase	:					Tuple	=	jnp.full_like(_SCREAMING_SNAKE_CASE      ,     self.filter_value						)
    UpperCAmelCase	:					Tuple	=	jax.nn.softmax(_SCREAMING_SNAKE_CASE      ,     axis=-1						).cumsum(axis=-1						)
    UpperCAmelCase	:					Optional[Any]	=	cumulative_probs < self.top_p

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

    # min tokens to keep
    UpperCAmelCase	:					Tuple	=	score_mask.at[:, : self.min_tokens_to_keep].set(_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					Union[str, Any]	=	jnp.where(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					List[str]	=	jax.lax.sort_key_val(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)[-1]

    return next_scores



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE = -float("""Inf"""						)      ,     _SCREAMING_SNAKE_CASE = 1						)  ->      Any:





    '''simple docstring'''




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

    UpperCAmelCase	:					Optional[int]	=	max(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Union[str, Any]	=	filter_value




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      jnp.ndarray:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					List[Any]	=	scores.shape
    UpperCAmelCase	:					Optional[int]	=	jnp.full(batch_size * vocab_size      ,     self.filter_value						)

    UpperCAmelCase	:					int	=	min(self.top_k      ,     scores.shape[-1]						)  # Safety check
    UpperCAmelCase	,		UpperCAmelCase	:					List[str]	=	lax.top_k(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					List[str]	=	jnp.broadcast_to((jnp.arange(_SCREAMING_SNAKE_CASE						) * vocab_size)[:, None]      ,     (batch_size, topk)						).flatten()
    UpperCAmelCase	:					Union[str, Any]	=	topk_scores.flatten()
    UpperCAmelCase	:					Optional[int]	=	topk_indices.flatten() + shift

    UpperCAmelCase	:					Optional[Any]	=	next_scores_flat.at[topk_indices_flat].set(_SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Tuple	=	next_scores_flat.reshape(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    return next_scores



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE						)  ->      List[str]:





    '''simple docstring'''




    UpperCAmelCase	:					str	=	bos_token_id




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      jnp.ndarray:





    '''simple docstring'''




    UpperCAmelCase	:					Optional[int]	=	jnp.full(scores.shape      ,     -float("""inf"""						)						)

    UpperCAmelCase	:					Optional[int]	=	1 - jnp.bool_(cur_len - 1						)

    UpperCAmelCase	:					Optional[int]	=	jnp.where(_SCREAMING_SNAKE_CASE      ,     new_scores.at[:, self.bos_token_id].set(0						)      ,     _SCREAMING_SNAKE_CASE						)

    return scores



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      Any:





    '''simple docstring'''




    UpperCAmelCase	:					Union[str, Any]	=	max_length
    UpperCAmelCase	:					Optional[int]	=	eos_token_id




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      jnp.ndarray:





    '''simple docstring'''




    UpperCAmelCase	:					Any	=	jnp.full(scores.shape      ,     -float("""inf"""						)						)

    UpperCAmelCase	:					Dict	=	1 - jnp.bool_(cur_len - self.max_length + 1						)

    UpperCAmelCase	:					int	=	jnp.where(_SCREAMING_SNAKE_CASE      ,     new_scores.at[:, self.eos_token_id].set(0						)      ,     _SCREAMING_SNAKE_CASE						)

    return scores



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      int:





    '''simple docstring'''




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

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

    UpperCAmelCase	:					List[Any]	=	min_length
    UpperCAmelCase	:					Any	=	eos_token_id




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      jnp.ndarray:





    '''simple docstring'''




    UpperCAmelCase	:					Union[str, Any]	=	1 - jnp.clip(cur_len - self.min_length      ,     0      ,     1						)

    UpperCAmelCase	:					List[Any]	=	jnp.where(_SCREAMING_SNAKE_CASE      ,     scores.at[:, self.eos_token_id].set(-float("""inf"""						)						)      ,     _SCREAMING_SNAKE_CASE						)

    return scores



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      Tuple:





    '''simple docstring'''




    UpperCAmelCase	:					str	=	list(_SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Optional[Any]	=	begin_index




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      Optional[Any]:





    '''simple docstring'''




    UpperCAmelCase	:					Any	=	1 - jnp.bool_(cur_len - self.begin_index						)

    UpperCAmelCase	:					Optional[Any]	=	jnp.where(_SCREAMING_SNAKE_CASE      ,     scores.at[:, self.begin_suppress_tokens].set(-float("""inf"""						)						)      ,     _SCREAMING_SNAKE_CASE						)

    return scores



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE						)  ->      int:





    '''simple docstring'''




    UpperCAmelCase	:					str	=	list(_SCREAMING_SNAKE_CASE						)




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      jnp.ndarray:





    '''simple docstring'''




    UpperCAmelCase	:					List[Any]	=	scores.at[..., self.suppress_tokens].set(-float("""inf"""						)						)

    return scores



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE						)  ->      Tuple:





    '''simple docstring'''




    UpperCAmelCase	:					Union[str, Any]	=	dict(_SCREAMING_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.
    UpperCAmelCase	:					List[Any]	=	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:
      UpperCAmelCase	:					List[Any]	=	force_token_array.at[index].set(_SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					str	=	jnp.intaa(_SCREAMING_SNAKE_CASE						)




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      jnp.ndarray:





    '''simple docstring'''




    def _force_token(_SCREAMING_SNAKE_CASE						):
     UpperCAmelCase	:					int	=	scores.shape[0]
     UpperCAmelCase	:					List[Any]	=	self.force_token_array[generation_idx]

     UpperCAmelCase	:					int	=	jnp.ones_like(_SCREAMING_SNAKE_CASE      ,     dtype=scores.dtype						) * -float("""inf"""						)
     UpperCAmelCase	:					List[str]	=	jnp.zeros((batch_size, 1)      ,     dtype=scores.dtype						)
     UpperCAmelCase	:					List[Any]	=	lax.dynamic_update_slice(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     (0, current_token)						)
     return new_scores

    UpperCAmelCase	:					Dict	=	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(_SCREAMING_SNAKE_CASE						)      ,     lambda: scores      ,     )      ,     )
    return scores



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      Union[str, Any]:





    '''simple docstring'''




    UpperCAmelCase	:					Tuple	=	generate_config.eos_token_id
    UpperCAmelCase	:					List[Any]	=	generate_config.no_timestamps_token_id
    UpperCAmelCase	:					str	=	generate_config.no_timestamps_token_id + 1

    UpperCAmelCase	:					int	=	decoder_input_length + 1

    if generate_config.is_multilingual:
     # room for language token and task token
     self.begin_index += 2
    if hasattr(_SCREAMING_SNAKE_CASE      ,     """max_initial_timestamp_index"""						):
     UpperCAmelCase	:					Union[str, Any]	=	generate_config.max_initial_timestamp_index
    else:
     UpperCAmelCase	:					Dict	=	model_config.vocab_size
    if self.max_initial_timestamp_index is None:
     UpperCAmelCase	:					Union[str, Any]	=	model_config.vocab_size




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      Dict:





    '''simple docstring'''




    UpperCAmelCase	:					Optional[int]	=	scores.at[:, self.no_timestamps_token_id].set(-float("""inf"""						)						)

    def handle_pairs(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						):
     UpperCAmelCase	:					Optional[int]	=	jnp.where((cur_len - self.begin_index) >= 1      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
     UpperCAmelCase	:					str	=	jnp.where(
         input_ids_k[cur_len - 1] >= self.timestamp_begin      ,     True and last_was_timestamp      ,     _SCREAMING_SNAKE_CASE      ,     )

     UpperCAmelCase	:					Dict	=	jnp.where((cur_len - self.begin_index) < 2      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
     UpperCAmelCase	:					List[str]	=	jnp.where(
         input_ids_k[cur_len - 2] >= self.timestamp_begin      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     )

     return jnp.where(
         _SCREAMING_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"""						)						)      ,     )      ,     _SCREAMING_SNAKE_CASE      ,     )

    UpperCAmelCase	:					Optional[int]	=	jax.vmap(_SCREAMING_SNAKE_CASE						)(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					Union[str, Any]	=	jnp.where(cur_len == self.begin_index      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Tuple	=	jnp.where(
        self.max_initial_timestamp_index is not None      ,     True and apply_max_initial_timestamp      ,     _SCREAMING_SNAKE_CASE      ,     )

    UpperCAmelCase	:					Union[str, Any]	=	self.timestamp_begin + self.max_initial_timestamp_index

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

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

    def handle_cumulative_probs(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						):
     UpperCAmelCase	:					Any	=	jax.nn.logsumexp(logprobs_k[self.timestamp_begin :]      ,     axis=-1						)
     UpperCAmelCase	:					Union[str, Any]	=	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"""						)						)      ,     _SCREAMING_SNAKE_CASE      ,     )

    UpperCAmelCase	:					str	=	jax.vmap(_SCREAMING_SNAKE_CASE						)(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)

    return scores





"""simple docstring"""
A:   Union[str, Any]								=		{
    0: "0",
    1: "1",
    2: "2",
    3: "3",
    4: "4",
    5: "5",
    6: "6",
    7: "7",
    8: "8",
    9: "9",
    1_0: "a",
    1_1: "b",
    1_2: "c",
    1_3: "d",
    1_4: "e",
    1_5: "f",
}

def       _snake_case						(						UpperCamelCase						:		float						):
 assert type(UpperCamelCase						) in (int, float) and decimal == int(UpperCamelCase						)
 UpperCAmelCase	:					str	=	int(UpperCamelCase						)
 UpperCAmelCase	:					Optional[int]	=	""""""
 UpperCAmelCase	:					List[str]	=	False
 if decimal < 0:
  UpperCAmelCase	:					Any	=	True
  decimal *= -1
 while decimal > 0:
  UpperCAmelCase	,		UpperCAmelCase	:					Dict	=	divmod(UpperCamelCase			,		16						)
  UpperCAmelCase	:					Union[str, Any]	=	values[remainder] + hexadecimal
 UpperCAmelCase	:					int	=	"""0x""" + hexadecimal
 if negative:
  UpperCAmelCase	:					Optional[int]	=	"""-""" + hexadecimal
 return hexadecimal


if __name__ == "__main__":
      import doctest

      doctest.testmod()
