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


lowercase__					       =	get_logger(__name__)


lowercase__					       =	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    __snake_case       :



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





        '''simple docstring'''





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







class    __snake_case       :



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





        '''simple docstring'''





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







class    __snake_case       (     __lowerCAmelCase				):



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





        '''simple docstring'''





        for processor in self:
          a__:     Union[str, Any]										=	inspect.signature(processor.__call__).parameters
          if len(lowercase) > 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.')
            a__:     List[str]										=	processor(lowercase    ,			lowercase    ,			lowercase    ,			**lowercase)
          else:
            a__:     int										=	processor(lowercase    ,			lowercase    ,			lowercase)
        return scores







class    __snake_case       (     __lowerCAmelCase				):



      def __init__(						self    ,			lowercase)	->			Dict:





        '''simple docstring'''





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

        a__:     Any										=	temperature



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





        '''simple docstring'''





        a__:     Tuple										=	scores / self.temperature
        return scores







class    __snake_case       (     __lowerCAmelCase				):



      def __init__(						self    ,			lowercase    ,			lowercase = -float('Inf')    ,			lowercase = 1)	->			int:





        '''simple docstring'''





        if not isinstance(lowercase    ,			lowercase) 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(lowercase    ,			lowercase) 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}')

        a__:     List[str]										=	top_p
        a__:     Dict										=	filter_value
        a__:     int										=	min_tokens_to_keep



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





        '''simple docstring'''





        a__							,    a__:     Tuple										=	lax.top_k(lowercase    ,			scores.shape[-1])

        a__:     Optional[int]										=	jnp.full_like(lowercase    ,			self.filter_value)
        a__:     List[Any]										=	jax.nn.softmax(lowercase    ,			axis=-1).cumsum(axis=-1)
        a__:     Union[str, Any]										=	cumulative_probs < self.top_p

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

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

        a__:     Any										=	jnp.where(lowercase    ,			lowercase    ,			lowercase)
        a__:     List[Any]										=	jax.lax.sort_key_val(lowercase    ,			lowercase)[-1]

        return next_scores







class    __snake_case       (     __lowerCAmelCase				):



      def __init__(						self    ,			lowercase    ,			lowercase = -float('Inf')    ,			lowercase = 1)	->			Tuple:





        '''simple docstring'''





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

        a__:     Dict										=	max(lowercase    ,			lowercase)
        a__:     Any										=	filter_value



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





        '''simple docstring'''





        a__							,    a__:     str										=	scores.shape
        a__:     Any										=	jnp.full(batch_size * vocab_size    ,			self.filter_value)

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

        a__:     Union[str, Any]										=	next_scores_flat.at[topk_indices_flat].set(lowercase)
        a__:     Optional[Any]										=	next_scores_flat.reshape(lowercase    ,			lowercase)
        return next_scores







class    __snake_case       (     __lowerCAmelCase				):



      def __init__(						self    ,			lowercase)	->			Any:





        '''simple docstring'''





        a__:     int										=	bos_token_id



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





        '''simple docstring'''





        a__:     str										=	jnp.full(scores.shape    ,			-float('inf'))

        a__:     Tuple										=	1 - jnp.bool_(cur_len - 1)

        a__:     Union[str, Any]										=	jnp.where(lowercase    ,			new_scores.at[:, self.bos_token_id].set(0)    ,			lowercase)

        return scores







class    __snake_case       (     __lowerCAmelCase				):



      def __init__(						self    ,			lowercase    ,			lowercase)	->			Dict:





        '''simple docstring'''





        a__:     Dict										=	max_length
        a__:     List[Any]										=	eos_token_id



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





        '''simple docstring'''





        a__:     List[str]										=	jnp.full(scores.shape    ,			-float('inf'))

        a__:     List[str]										=	1 - jnp.bool_(cur_len - self.max_length + 1)

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

        return scores







class    __snake_case       (     __lowerCAmelCase				):



      def __init__(						self    ,			lowercase    ,			lowercase)	->			Optional[int]:





        '''simple docstring'''





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

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

        a__:     List[Any]										=	min_length
        a__:     Union[str, Any]										=	eos_token_id



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





        '''simple docstring'''





        a__:     Dict										=	1 - jnp.clip(cur_len - self.min_length    ,			0    ,			1)

        a__:     Any										=	jnp.where(lowercase    ,			scores.at[:, self.eos_token_id].set(-float('inf'))    ,			lowercase)

        return scores







class    __snake_case       (     __lowerCAmelCase				):



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





        '''simple docstring'''





        a__:     Union[str, Any]										=	list(lowercase)
        a__:     Optional[Any]										=	begin_index



      def __call__(						self    ,			lowercase    ,			lowercase    ,			lowercase)	->			Tuple:





        '''simple docstring'''





        a__:     Dict										=	1 - jnp.bool_(cur_len - self.begin_index)

        a__:     Union[str, Any]										=	jnp.where(lowercase    ,			scores.at[:, self.begin_suppress_tokens].set(-float('inf'))    ,			lowercase)

        return scores







class    __snake_case       (     __lowerCAmelCase				):



      def __init__(						self    ,			lowercase)	->			List[Any]:





        '''simple docstring'''





        a__:     Dict										=	list(lowercase)



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





        '''simple docstring'''





        a__:     Dict										=	scores.at[..., self.suppress_tokens].set(-float('inf'))

        return scores







class    __snake_case       (     __lowerCAmelCase				):



      def __init__(						self    ,			lowercase)	->			str:





        '''simple docstring'''





        a__:     int										=	dict(lowercase)
        # 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.
        a__:     Dict										=	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:
            a__:     List[str]										=	force_token_array.at[index].set(lowercase)
        a__:     Union[str, Any]										=	jnp.intaa(lowercase)



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





        '''simple docstring'''





        def _force_token(lowercase):
          a__:     str										=	scores.shape[0]
          a__:     Union[str, Any]										=	self.force_token_array[generation_idx]

          a__:     Any										=	jnp.ones_like(lowercase    ,			dtype=scores.dtype) * -float('inf')
          a__:     str										=	jnp.zeros((batch_size, 1)    ,			dtype=scores.dtype)
          a__:     Any										=	lax.dynamic_update_slice(lowercase    ,			lowercase    ,			(0, current_token))
          return new_scores

        a__:     List[str]										=	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(lowercase)    ,			lambda: scores    ,			)    ,			)
        return scores







class    __snake_case       (     __lowerCAmelCase				):



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





        '''simple docstring'''





        a__:     List[str]										=	generate_config.eos_token_id
        a__:     int										=	generate_config.no_timestamps_token_id
        a__:     List[str]										=	generate_config.no_timestamps_token_id + 1

        a__:     Any										=	decoder_input_length + 1

        if generate_config.is_multilingual:
          # room for language token and task token
          self.begin_index += 2
        if hasattr(lowercase    ,			'max_initial_timestamp_index'):
          a__:     Tuple										=	generate_config.max_initial_timestamp_index
        else:
          a__:     Optional[Any]										=	model_config.vocab_size
        if self.max_initial_timestamp_index is None:
          a__:     str										=	model_config.vocab_size



      def __call__(						self    ,			lowercase    ,			lowercase    ,			lowercase)	->			Any:





        '''simple docstring'''





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

        def handle_pairs(lowercase    ,			lowercase):
          a__:     List[str]										=	jnp.where((cur_len - self.begin_index) >= 1    ,			lowercase    ,			lowercase)
          a__:     Dict										=	jnp.where(
              input_ids_k[cur_len - 1] >= self.timestamp_begin    ,			True and last_was_timestamp    ,			lowercase    ,			)

          a__:     Dict										=	jnp.where((cur_len - self.begin_index) < 2    ,			lowercase    ,			lowercase)
          a__:     Optional[int]										=	jnp.where(
              input_ids_k[cur_len - 2] >= self.timestamp_begin    ,			lowercase    ,			lowercase    ,			)

          return jnp.where(
              lowercase    ,			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'))    ,			)    ,			lowercase    ,			)

        a__:     Tuple										=	jax.vmap(lowercase)(lowercase    ,			lowercase)

        a__:     Optional[int]										=	jnp.where(cur_len == self.begin_index    ,			lowercase    ,			lowercase)
        a__:     Dict										=	jnp.where(
            self.max_initial_timestamp_index is not None    ,			True and apply_max_initial_timestamp    ,			lowercase    ,			)

        a__:     str										=	self.timestamp_begin + self.max_initial_timestamp_index

        a__:     Union[str, Any]										=	jnp.where(
            lowercase    ,			scores.at[:, last_allowed + 1 :].set(-float('inf'))    ,			lowercase    ,			)

        # if sum of probability over timestamps is above any other token, sample timestamp
        a__:     List[str]										=	jax.nn.log_softmax(lowercase    ,			axis=-1)

        def handle_cumulative_probs(lowercase    ,			lowercase):
          a__:     Any										=	jax.nn.logsumexp(logprobs_k[self.timestamp_begin :]    ,			axis=-1)
          a__:     int										=	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'))    ,			lowercase    ,			)

        a__:     List[Any]										=	jax.vmap(lowercase)(lowercase    ,			lowercase)

        return scores



"""simple docstring"""
from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowercase__					       =	logging.get_logger(__name__)

lowercase__					       =	{
    'andreasmadsen/efficient_mlm_m0.40': (
        'https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json'
    ),
}




class    __snake_case       (     __lowerCAmelCase				):
      a__        =		"""roberta-prelayernorm"""



      def __init__(						self    ,			lowercase=5_02_65    ,			lowercase=7_68    ,			lowercase=12    ,			lowercase=12    ,			lowercase=30_72    ,			lowercase="gelu"    ,			lowercase=0.1    ,			lowercase=0.1    ,			lowercase=5_12    ,			lowercase=2    ,			lowercase=0.02    ,			lowercase=1e-12    ,			lowercase=1    ,			lowercase=0    ,			lowercase=2    ,			lowercase="absolute"    ,			lowercase=True    ,			lowercase=None    ,			**lowercase    ,			)	->			Any:





        '''simple docstring'''





        super().__init__(pad_token_id=lowercase    ,			bos_token_id=lowercase    ,			eos_token_id=lowercase    ,			**lowercase)

        a__:     Union[str, Any]										=	vocab_size
        a__:     str										=	hidden_size
        a__:     Tuple										=	num_hidden_layers
        a__:     List[str]										=	num_attention_heads
        a__:     Dict										=	hidden_act
        a__:     int										=	intermediate_size
        a__:     Tuple										=	hidden_dropout_prob
        a__:     str										=	attention_probs_dropout_prob
        a__:     Tuple										=	max_position_embeddings
        a__:     Tuple										=	type_vocab_size
        a__:     Optional[Any]										=	initializer_range
        a__:     Tuple										=	layer_norm_eps
        a__:     Optional[int]										=	position_embedding_type
        a__:     Any										=	use_cache
        a__:     Dict										=	classifier_dropout







class    __snake_case       (     __lowerCAmelCase				):



      @property
      def   lowerCamelCase_   (						self)	->			Mapping[str, Mapping[int, str]]:





        '''simple docstring'''





        if self.task == "multiple-choice":
          a__:     str										=	{0: 'batch', 1: 'choice', 2: 'sequence'}
        else:
          a__:     Union[str, Any]										=	{0: 'batch', 1: 'sequence'}
        return OrderedDict(
            [
                ('input_ids', dynamic_axis),
                ('attention_mask', dynamic_axis),
            ])


