



"""simple docstring"""






import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import BatchEncoding, MarianTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow
from transformers.utils import is_sentencepiece_available, is_tf_available, is_torch_available


if is_sentencepiece_available():
	from transformers.models.marian.tokenization_marian import VOCAB_FILES_NAMES, save_json

from ...test_tokenization_common import TokenizerTesterMixin


__snake_case				:  List[Any] =       get_tests_dir('fixtures/test_sentencepiece.model')

__snake_case				:  Union[str, Any] =       {'target_lang': 'fi', 'source_lang': 'en'}
__snake_case				:  Union[str, Any] =       '>>zh<<'
__snake_case				:  List[str] =       'Helsinki-NLP/'

if is_torch_available():
	__snake_case				:  Optional[int] =       'pt'
elif is_tf_available():
	__snake_case				:  List[Any] =       'tf'
else:
	__snake_case				:  Union[str, Any] =       'jax'



@require_sentencepiece
class      A__  (    __SCREAMING_SNAKE_CASE    ,						unittest.TestCase				):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE        =      MarianTokenizer
	SCREAMING_SNAKE_CASE        =      False
	SCREAMING_SNAKE_CASE        =      True

	def 				_SCREAMING_SNAKE_CASE  (	self:						int)							->			Tuple:

				"""simple docstring"""

				super().setUp()
				__lowerCAmelCase :    Dict				=			["</s>", "<unk>", "▁This", "▁is", "▁a", "▁t", "est", "\u0120", "<pad>"]
				__lowerCAmelCase :    Union[str, Any]				=			dict(zip(_SCREAMING_SNAKE_CASE			,     range(len(_SCREAMING_SNAKE_CASE))))
				__lowerCAmelCase :    int				=			Path(self.tmpdirname)
				save_json(_SCREAMING_SNAKE_CASE			,     save_dir / VOCAB_FILES_NAMES["vocab"])
				save_json(_SCREAMING_SNAKE_CASE			,     save_dir / VOCAB_FILES_NAMES["tokenizer_config_file"])
				if not (save_dir / VOCAB_FILES_NAMES["source_spm"]).exists():
							copyfile(_SCREAMING_SNAKE_CASE			,     save_dir / VOCAB_FILES_NAMES["source_spm"])
							copyfile(_SCREAMING_SNAKE_CASE			,     save_dir / VOCAB_FILES_NAMES["target_spm"])

				__lowerCAmelCase :    Tuple				=			MarianTokenizer.from_pretrained(self.tmpdirname)
				tokenizer.save_pretrained(self.tmpdirname)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Dict			,     **_SCREAMING_SNAKE_CASE:						List[str])							->			MarianTokenizer:

				"""simple docstring"""

				return MarianTokenizer.from_pretrained(self.tmpdirname			,     **_SCREAMING_SNAKE_CASE)

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

				"""simple docstring"""

				return (
				    "This is a test",
				    "This is a test",
				)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[int])							->			int:

				"""simple docstring"""

				__lowerCAmelCase :    Union[str, Any]				=			"</s>"
				__lowerCAmelCase :    int				=			0

				self.assertEqual(self.get_tokenizer()._convert_token_to_id(_SCREAMING_SNAKE_CASE)			,     _SCREAMING_SNAKE_CASE)
				self.assertEqual(self.get_tokenizer()._convert_id_to_token(_SCREAMING_SNAKE_CASE)			,     _SCREAMING_SNAKE_CASE)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Tuple)							->			List[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    List[str]				=			list(self.get_tokenizer().get_vocab().keys())

				self.assertEqual(vocab_keys[0]			,     "</s>")
				self.assertEqual(vocab_keys[1]			,     "<unk>")
				self.assertEqual(vocab_keys[-1]			,     "<pad>")
				self.assertEqual(len(_SCREAMING_SNAKE_CASE)			,     9)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Tuple)							->			Dict:

				"""simple docstring"""

				self.assertEqual(self.get_tokenizer().vocab_size			,     9)

	def 				_SCREAMING_SNAKE_CASE  (	self:						List[Any])							->			Any:

				"""simple docstring"""

				__lowerCAmelCase :    Optional[Any]				=			MarianTokenizer.from_pretrained(F"""{ORG_NAME}opus-mt-en-de""")
				__lowerCAmelCase :    int				=			en_de_tokenizer(["I am a small frog"]			,     return_tensors=_SCREAMING_SNAKE_CASE)
				self.assertIsInstance(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    Optional[int]				=			[38, 121, 14, 697, 3_8848, 0]
				self.assertListEqual(_SCREAMING_SNAKE_CASE			,     batch.input_ids[0])

				__lowerCAmelCase :    Tuple				=			tempfile.mkdtemp()
				en_de_tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    List[Any]				=			[x.name for x in Path(_SCREAMING_SNAKE_CASE).glob("*")]
				self.assertIn("source.spm"			,     _SCREAMING_SNAKE_CASE)
				MarianTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE)

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

				"""simple docstring"""

				__lowerCAmelCase :    List[str]				=			self.get_tokenizer()

				__lowerCAmelCase :    int				=			tok(
				    ["I am a small frog" * 1000, "I am a small frog"]			,     padding=_SCREAMING_SNAKE_CASE			,     truncation=_SCREAMING_SNAKE_CASE			,     return_tensors=_SCREAMING_SNAKE_CASE)
				self.assertIsInstance(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
				self.assertEqual(batch.input_ids.shape			,     (2, 512))

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

				"""simple docstring"""

				__lowerCAmelCase :    Any				=			self.get_tokenizer()
				__lowerCAmelCase :    str				=			tok(["I am a tiny frog", "I am a small frog"]			,     padding=_SCREAMING_SNAKE_CASE			,     return_tensors=_SCREAMING_SNAKE_CASE)
				self.assertIsInstance(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
				self.assertEqual(batch_smaller.input_ids.shape			,     (2, 10))

	@slow
	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[Any])							->			Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    Optional[int]				=			{"input_ids": [[4_3495, 462, 20, 4_2164, 1369, 52, 464, 132, 1703, 492, 13, 7491, 3_8999, 6, 8, 464, 132, 1703, 492, 13, 4669, 3_7867, 13, 7525, 27, 1593, 988, 13, 3_3972, 7029, 6, 20, 8251, 383, 2, 270, 5866, 3788, 2, 2353, 8251, 1_2338, 2, 1_3958, 387, 2, 3629, 6953, 188, 2900, 2, 1_3958, 8011, 1_1501, 23, 8460, 4073, 3_4009, 20, 435, 1_1439, 27, 8, 8460, 4073, 6004, 20, 9988, 375, 27, 33, 266, 1945, 1076, 1350, 3_7867, 3288, 5, 577, 1076, 4374, 8, 5082, 5, 2_6453, 257, 556, 403, 2, 242, 132, 383, 316, 492, 8, 1_0767, 6, 316, 304, 4239, 3, 0], [148, 1_5722, 19, 1839, 12, 1350, 13, 2_2327, 5082, 5418, 4_7567, 3_5938, 59, 318, 1_9552, 108, 2183, 54, 1_4976, 4835, 32, 547, 1114, 8, 315, 2417, 5, 92, 1_9088, 3, 0, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100], [36, 6395, 1_2570, 3_9147, 1_1597, 6, 266, 4, 4_5405, 7296, 3, 0, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
				# fmt: on

				self.tokenizer_integration_test_util(
				    expected_encoding=_SCREAMING_SNAKE_CASE			,     model_name="Helsinki-NLP/opus-mt-en-de"			,     revision="1a8c2263da11e68e50938f97e10cd57820bd504c"			,     decode_kwargs={"use_source_tokenizer": True}			,     )







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

				"""simple docstring"""

				__lowerCAmelCase :    List[Any]				=			MarianTokenizer.from_pretrained("hf-internal-testing/test-marian-two-vocabs")

				__lowerCAmelCase :    List[str]				=			"Tämä on testi"
				__lowerCAmelCase :    int				=			"This is a test"

				__lowerCAmelCase :    Union[str, Any]				=			[76, 7, 2047, 2]
				__lowerCAmelCase :    Dict				=			[69, 12, 11, 940, 2]

				__lowerCAmelCase :    List[str]				=			tokenizer(_SCREAMING_SNAKE_CASE).input_ids
				self.assertListEqual(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)

				__lowerCAmelCase :    str				=			tokenizer(text_target=_SCREAMING_SNAKE_CASE).input_ids
				self.assertListEqual(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)

				__lowerCAmelCase :    Dict				=			tokenizer.decode(_SCREAMING_SNAKE_CASE			,     skip_special_tokens=_SCREAMING_SNAKE_CASE)
				self.assertEqual(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)




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


__snake_case				:  List[str] =       get_logger(__name__)


__snake_case				:  str =       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      A__  :




	'''simple docstring'''





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

				"""simple docstring"""

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






class      A__  :




	'''simple docstring'''





	@add_start_docstrings(_SCREAMING_SNAKE_CASE)
	def __call__(	self:						List[str]			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray)							->			jnp.ndarray:

				"""simple docstring"""

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






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





	@add_start_docstrings(_SCREAMING_SNAKE_CASE)
	def __call__(	self:						Optional[int]			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						int			,     **_SCREAMING_SNAKE_CASE:						str)							->			jnp.ndarray:

				"""simple docstring"""

				for processor in self:
							__lowerCAmelCase :    int				=			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.""")
										__lowerCAmelCase :    int				=			processor(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE			,     **_SCREAMING_SNAKE_CASE)
							else:
										__lowerCAmelCase :    Union[str, Any]				=			processor(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
				return scores






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





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

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

				__lowerCAmelCase :    List[str]				=			temperature







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

				"""simple docstring"""

				__lowerCAmelCase :    Union[str, Any]				=			scores / self.temperature
				return scores






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





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

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

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







	def __call__(	self:						Union[str, Any]			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						int)							->			jnp.ndarray:

				"""simple docstring"""

				__lowerCAmelCase		,      __lowerCAmelCase :    Optional[Any]				=			lax.top_k(_SCREAMING_SNAKE_CASE			,     scores.shape[-1])

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

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

				# min tokens to keep
				__lowerCAmelCase :    Union[str, Any]				=			score_mask.at[:, : self.min_tokens_to_keep].set(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase :    Optional[Any]				=			jnp.where(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    Dict				=			jax.lax.sort_key_val(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)[-1]

				return next_scores






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





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

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

				__lowerCAmelCase :    Any				=			max(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    Optional[int]				=			filter_value







	def __call__(	self:						List[Any]			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						int)							->			jnp.ndarray:

				"""simple docstring"""

				__lowerCAmelCase		,      __lowerCAmelCase :    Any				=			scores.shape
				__lowerCAmelCase :    Union[str, Any]				=			jnp.full(batch_size * vocab_size			,     self.filter_value)

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

				__lowerCAmelCase :    str				=			next_scores_flat.at[topk_indices_flat].set(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    Union[str, Any]				=			next_scores_flat.reshape(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
				return next_scores






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





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

				"""simple docstring"""

				__lowerCAmelCase :    Tuple				=			bos_token_id







	def __call__(	self:						Optional[int]			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						int)							->			jnp.ndarray:

				"""simple docstring"""

				__lowerCAmelCase :    int				=			jnp.full(scores.shape			,     -float("inf"))

				__lowerCAmelCase :    int				=			1 - jnp.bool_(cur_len - 1)

				__lowerCAmelCase :    Tuple				=			jnp.where(_SCREAMING_SNAKE_CASE			,     new_scores.at[:, self.bos_token_id].set(0)			,     _SCREAMING_SNAKE_CASE)

				return scores






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





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

				"""simple docstring"""

				__lowerCAmelCase :    List[str]				=			max_length
				__lowerCAmelCase :    int				=			eos_token_id







	def __call__(	self:						List[Any]			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						int)							->			jnp.ndarray:

				"""simple docstring"""

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

				__lowerCAmelCase :    Any				=			1 - jnp.bool_(cur_len - self.max_length + 1)

				__lowerCAmelCase :    Union[str, Any]				=			jnp.where(_SCREAMING_SNAKE_CASE			,     new_scores.at[:, self.eos_token_id].set(0)			,     _SCREAMING_SNAKE_CASE)

				return scores






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





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

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

				__lowerCAmelCase :    Any				=			min_length
				__lowerCAmelCase :    Tuple				=			eos_token_id







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

				"""simple docstring"""

				__lowerCAmelCase :    Optional[Any]				=			1 - jnp.clip(cur_len - self.min_length			,     0			,     1)

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

				return scores






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





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

				"""simple docstring"""

				__lowerCAmelCase :    int				=			list(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    Optional[Any]				=			begin_index







	def __call__(	self:						Optional[Any]			,     _SCREAMING_SNAKE_CASE:						List[str]			,     _SCREAMING_SNAKE_CASE:						str			,     _SCREAMING_SNAKE_CASE:						int)							->			Dict:

				"""simple docstring"""

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

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

				return scores






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





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

				"""simple docstring"""

				__lowerCAmelCase :    Optional[Any]				=			list(_SCREAMING_SNAKE_CASE)







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

				"""simple docstring"""

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

				return scores






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





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

				"""simple docstring"""

				__lowerCAmelCase :    Optional[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.
				__lowerCAmelCase :    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:
										__lowerCAmelCase :    int				=			force_token_array.at[index].set(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    Tuple				=			jnp.intaa(_SCREAMING_SNAKE_CASE)







	def __call__(	self:						List[str]			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						jnp.ndarray			,     _SCREAMING_SNAKE_CASE:						int)							->			jnp.ndarray:

				"""simple docstring"""

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

							__lowerCAmelCase :    int				=			jnp.ones_like(_SCREAMING_SNAKE_CASE			,     dtype=scores.dtype) * -float("inf")
							__lowerCAmelCase :    Optional[int]				=			jnp.zeros((batch_size, 1)			,     dtype=scores.dtype)
							__lowerCAmelCase :    Dict				=			lax.dynamic_update_slice(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE			,     (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(_SCREAMING_SNAKE_CASE)			,     lambda: scores			,     )			,     )
				return scores






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





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

				"""simple docstring"""

				__lowerCAmelCase :    List[str]				=			generate_config.eos_token_id
				__lowerCAmelCase :    Optional[int]				=			generate_config.no_timestamps_token_id
				__lowerCAmelCase :    Dict				=			generate_config.no_timestamps_token_id + 1

				__lowerCAmelCase :    Optional[Any]				=			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"):
							__lowerCAmelCase :    Optional[int]				=			generate_config.max_initial_timestamp_index
				else:
							__lowerCAmelCase :    Optional[int]				=			model_config.vocab_size
				if self.max_initial_timestamp_index is None:
							__lowerCAmelCase :    int				=			model_config.vocab_size







	def __call__(	self:						Tuple			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]			,     _SCREAMING_SNAKE_CASE:						Optional[Any]			,     _SCREAMING_SNAKE_CASE:						int)							->			Optional[int]:

				"""simple docstring"""

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

				def handle_pairs(_SCREAMING_SNAKE_CASE:						int			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]):
							__lowerCAmelCase :    Tuple				=			jnp.where((cur_len - self.begin_index) >= 1			,     _SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
							__lowerCAmelCase :    List[Any]				=			jnp.where(
							    input_ids_k[cur_len - 1] >= self.timestamp_begin			,     True and last_was_timestamp			,     _SCREAMING_SNAKE_CASE			,     )

							__lowerCAmelCase :    Tuple				=			jnp.where((cur_len - self.begin_index) < 2			,     _SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
							__lowerCAmelCase :    int				=			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			,     )

				__lowerCAmelCase :    int				=			jax.vmap(_SCREAMING_SNAKE_CASE)(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)

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

				__lowerCAmelCase :    List[Any]				=			self.timestamp_begin + self.max_initial_timestamp_index

				__lowerCAmelCase :    Tuple				=			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
				__lowerCAmelCase :    List[str]				=			jax.nn.log_softmax(_SCREAMING_SNAKE_CASE			,     axis=-1)

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

				__lowerCAmelCase :    Dict				=			jax.vmap(_SCREAMING_SNAKE_CASE)(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)

				return scores