






"""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__	:    List[str]			 = get_logger(__name__)


A__	:    Optional[int]			 = 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        lowercase__      :


			@add_start_docstrings(_lowerCamelCase )
			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        lowercase__      :


			@add_start_docstrings(_lowerCamelCase )
			def __call__(				self					:  Optional[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        lowercase__      (							a__       ):


			@add_start_docstrings(_lowerCamelCase )
			def __call__(				self					:  str       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  int       ,	**snake_case__					:  Optional[int] ):







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







class        lowercase__      (							a__       ):


			def __init__(				self					:  Any       ,	snake_case__					:  float ):







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

										lowerCamelCase_	:							int									=temperature


			def __call__(				self					:  List[Any]       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  int ):
										lowerCamelCase_	:							Optional[Any]									=scores / self.temperature
										return scores







class        lowercase__      (							a__       ):


			def __init__(				self					:  Optional[int]       ,	snake_case__					:  float       ,	snake_case__					:  float = -float("Inf" )       ,	snake_case__					:  int = 1 ):







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

										lowerCamelCase_	:							int									=top_p
										lowerCamelCase_	:							Union[str, Any]									=filter_value
										lowerCamelCase_	:							Union[str, Any]									=min_tokens_to_keep


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

										lowerCamelCase_	:							str									=jnp.full_like(_lowerCamelCase       ,	self.filter_value )
										lowerCamelCase_	:							Dict									=jax.nn.softmax(_lowerCamelCase       ,	axis=-1 ).cumsum(axis=-1 )
										lowerCamelCase_	:							Any									=cumulative_probs < self.top_p

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

										# min tokens to keep
										lowerCamelCase_	:							List[Any]									=score_mask.at[:, : self.min_tokens_to_keep].set(_lowerCamelCase )

										lowerCamelCase_	:							Dict									=jnp.where(_lowerCamelCase       ,	_lowerCamelCase       ,	_lowerCamelCase )
										lowerCamelCase_	:							Dict									=jax.lax.sort_key_val(_lowerCamelCase       ,	_lowerCamelCase )[-1]

										return next_scores







class        lowercase__      (							a__       ):


			def __init__(				self					:  Tuple       ,	snake_case__					:  int       ,	snake_case__					:  float = -float("Inf" )       ,	snake_case__					:  int = 1 ):







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

										lowerCamelCase_	:							str									=max(_lowerCamelCase       ,	_lowerCamelCase )
										lowerCamelCase_	:							Optional[int]									=filter_value


			def __call__(				self					:  Dict       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  int ):
										lowerCamelCase_	:							Dict									=scores.shape
										lowerCamelCase_	:							Optional[Any]									=jnp.full(batch_size * vocab_size       ,	self.filter_value )

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

										lowerCamelCase_	:							Any									=next_scores_flat.at[topk_indices_flat].set(_lowerCamelCase )
										lowerCamelCase_	:							Optional[Any]									=next_scores_flat.reshape(_lowerCamelCase       ,	_lowerCamelCase )
										return next_scores







class        lowercase__      (							a__       ):


			def __init__(				self					:  str       ,	snake_case__					:  int ):
										lowerCamelCase_	:							Any									=bos_token_id


			def __call__(				self					:  Union[str, Any]       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  int ):
										lowerCamelCase_	:							Optional[Any]									=jnp.full(scores.shape       ,	-float("inf" ) )

										lowerCamelCase_	:							List[Any]									=1 - jnp.bool_(cur_len - 1 )

										lowerCamelCase_	:							List[str]									=jnp.where(_lowerCamelCase       ,	new_scores.at[:, self.bos_token_id].set(0 )       ,	_lowerCamelCase )

										return scores







class        lowercase__      (							a__       ):


			def __init__(				self					:  Union[str, Any]       ,	snake_case__					:  int       ,	snake_case__					:  int ):
										lowerCamelCase_	:							int									=max_length
										lowerCamelCase_	:							Any									=eos_token_id


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

										lowerCamelCase_	:							Optional[Any]									=1 - jnp.bool_(cur_len - self.max_length + 1 )

										lowerCamelCase_	:							Optional[int]									=jnp.where(_lowerCamelCase       ,	new_scores.at[:, self.eos_token_id].set(0 )       ,	_lowerCamelCase )

										return scores







class        lowercase__      (							a__       ):


			def __init__(				self					:  Tuple       ,	snake_case__					:  int       ,	snake_case__					:  int ):







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

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

										lowerCamelCase_	:							List[str]									=min_length
										lowerCamelCase_	:							Optional[int]									=eos_token_id


			def __call__(				self					:  Any       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  int ):
										lowerCamelCase_	:							int									=1 - jnp.clip(cur_len - self.min_length       ,	0       ,	1 )

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

										return scores







class        lowercase__      (							a__       ):


			def __init__(				self					:  int       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  List[Any] ):
										lowerCamelCase_	:							int									=list(_lowerCamelCase )
										lowerCamelCase_	:							Tuple									=begin_index


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

										lowerCamelCase_	:							Dict									=jnp.where(_lowerCamelCase       ,	scores.at[:, self.begin_suppress_tokens].set(-float("inf" ) )       ,	_lowerCamelCase )

										return scores







class        lowercase__      (							a__       ):


			def __init__(				self					:  Tuple       ,	snake_case__					:  list ):
										lowerCamelCase_	:							int									=list(_lowerCamelCase )


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

										return scores







class        lowercase__      (							a__       ):


			def __init__(				self					:  List[str]       ,	snake_case__					:  Tuple ):
										lowerCamelCase_	:							List[Any]									=dict(_lowerCamelCase )
										# 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.
										lowerCamelCase_	:							int									=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:
																								lowerCamelCase_	:							List[str]									=force_token_array.at[index].set(_lowerCamelCase )
										lowerCamelCase_	:							List[Any]									=jnp.intaa(_lowerCamelCase )


			def __call__(				self					:  List[str]       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  jnp.ndarray       ,	snake_case__					:  int ):







										def _force_token(snake_case__					:  Union[str, Any] ):
																	lowerCamelCase_	:							List[str]									=scores.shape[0]
																	lowerCamelCase_	:							List[Any]									=self.force_token_array[generation_idx]

																	lowerCamelCase_	:							Optional[Any]									=jnp.ones_like(_lowerCamelCase       ,	dtype=scores.dtype ) * -float("inf" )
																	lowerCamelCase_	:							Union[str, Any]									=jnp.zeros((batch_size, 1)       ,	dtype=scores.dtype )
																	lowerCamelCase_	:							Union[str, Any]									=lax.dynamic_update_slice(_lowerCamelCase       ,	_lowerCamelCase       ,	(0, current_token) )
																	return new_scores

										lowerCamelCase_	:							Union[str, Any]									=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(_lowerCamelCase )       ,	lambda: scores       ,	)       ,	)
										return scores







class        lowercase__      (							a__       ):


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

										lowerCamelCase_	:							List[str]									=decoder_input_length + 1

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


			def __call__(				self					:  Optional[Any]       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  List[Any]       ,	snake_case__					:  Optional[Any] ):
										lowerCamelCase_	:							str									=scores.at[:, self.no_timestamps_token_id].set(-float("inf" ) )

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

																	lowerCamelCase_	:							Union[str, Any]									=jnp.where((cur_len - self.begin_index) < 2       ,	_lowerCamelCase       ,	_lowerCamelCase )
																	lowerCamelCase_	:							Any									=jnp.where(
																	    input_ids_k[cur_len - 2] >= self.timestamp_begin       ,	_lowerCamelCase       ,	_lowerCamelCase       ,	)

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

										lowerCamelCase_	:							Dict									=jax.vmap(_lowerCamelCase )(_lowerCamelCase       ,	_lowerCamelCase )

										lowerCamelCase_	:							Dict									=jnp.where(cur_len == self.begin_index       ,	_lowerCamelCase       ,	_lowerCamelCase )
										lowerCamelCase_	:							List[Any]									=jnp.where(
										    self.max_initial_timestamp_index is not None       ,	True and apply_max_initial_timestamp       ,	_lowerCamelCase       ,	)

										lowerCamelCase_	:							str									=self.timestamp_begin + self.max_initial_timestamp_index

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

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

										def handle_cumulative_probs(snake_case__					:  List[str]       ,	snake_case__					:  Any ):
																	lowerCamelCase_	:							Optional[Any]									=jax.nn.logsumexp(logprobs_k[self.timestamp_begin :]       ,	axis=-1 )
																	lowerCamelCase_	:							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" ) )       ,	_lowerCamelCase       ,	)

										lowerCamelCase_	:							Any									=jax.vmap(_lowerCamelCase )(_lowerCamelCase       ,	_lowerCamelCase )

										return scores












"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A__	:    List[Any]			 = {
    'configuration_lilt': ['LILT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LiltConfig'],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A__	:    List[str]			 = [
						    'LILT_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'LiltForQuestionAnswering',
						    'LiltForSequenceClassification',
						    'LiltForTokenClassification',
						    'LiltModel',
						    'LiltPreTrainedModel',
						]

if TYPE_CHECKING:
						from .configuration_lilt import LILT_PRETRAINED_CONFIG_ARCHIVE_MAP, LiltConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_lilt import (
												    LILT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    LiltForQuestionAnswering,
												    LiltForSequenceClassification,
												    LiltForTokenClassification,
												    LiltModel,
												    LiltPreTrainedModel,
												)

else:
						import sys

						A__	:    Optional[Any]			 = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



