





"""simple docstring"""





import inspect
import unittest

from transformers import BitConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch
							from torch import nn

							from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel
							from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image
class 		SCREAMING_SNAKE_CASE_				:
				"""simple docstring"""




				def __init__(  self	:List[Any]    ,     __lowercase	:Any    ,     __lowercase	:str=3    ,     __lowercase	:List[str]=32    ,     __lowercase	:Union[str, Any]=3    ,     __lowercase	:Dict=10    ,     __lowercase	:str=[8, 16, 32, 64]    ,     __lowercase	:int=[1, 1, 2, 1]    ,     __lowercase	:Optional[int]=True    ,     __lowercase	:List[str]=True    ,     __lowercase	:str="relu"    ,     __lowercase	:str=3    ,     __lowercase	:str=None    ,     __lowercase	:List[str]=["stage2", "stage3", "stage4"]    ,     __lowercase	:Union[str, Any]=[2, 3, 4]    ,     __lowercase	:Dict=1    ,     ):
											__lowerCamelCase   :   Dict       =parent
											__lowerCamelCase   :   str       =batch_size
											__lowerCamelCase   :   Union[str, Any]       =image_size
											__lowerCamelCase   :   int       =num_channels
											__lowerCamelCase   :   int       =embeddings_size
											__lowerCamelCase   :   Optional[Any]       =hidden_sizes
											__lowerCamelCase   :   str       =depths
											__lowerCamelCase   :   int       =is_training
											__lowerCamelCase   :   str       =use_labels
											__lowerCamelCase   :   List[Any]       =hidden_act
											__lowerCamelCase   :   List[str]       =num_labels
											__lowerCamelCase   :   Tuple       =scope
											__lowerCamelCase   :   Dict       =len(_SCREAMING_SNAKE_CASE      )
											__lowerCamelCase   :   Any       =out_features
											__lowerCamelCase   :   Any       =out_indices
											__lowerCamelCase   :   Dict       =num_groups




				def        __lowercase	(  self	:str      ):
											__lowerCamelCase   :   Optional[int]       =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

											__lowerCamelCase   :   Tuple       =None
											if self.use_labels:
																		__lowerCamelCase   :   Optional[Any]       =ids_tensor([self.batch_size]    ,     self.num_labels      )

											__lowerCamelCase   :   List[str]       =self.get_config()

											return config, pixel_values, labels




				def        __lowercase	(  self	:str      ):
											return BitConfig(
											    num_channels=self.num_channels    ,     embeddings_size=self.embeddings_size    ,     hidden_sizes=self.hidden_sizes    ,     depths=self.depths    ,     hidden_act=self.hidden_act    ,     num_labels=self.num_labels    ,     out_features=self.out_features    ,     out_indices=self.out_indices    ,     num_groups=self.num_groups    ,     )




				def        __lowercase	(  self	:Tuple    ,     __lowercase	:List[str]    ,     __lowercase	:int    ,     __lowercase	:str      ):
											__lowerCamelCase   :   str       =BitModel(config=_SCREAMING_SNAKE_CASE      )
											model.to(_SCREAMING_SNAKE_CASE      )
											model.eval()
											__lowerCamelCase   :   Dict       =model(_SCREAMING_SNAKE_CASE      )
											self.parent.assertEqual(
											    result.last_hidden_state.shape    ,     (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)    ,     )




				def        __lowercase	(  self	:str    ,     __lowercase	:Tuple    ,     __lowercase	:Any    ,     __lowercase	:List[str]      ):
											__lowerCamelCase   :   Any       =self.num_labels
											__lowerCamelCase   :   Optional[Any]       =BitForImageClassification(_SCREAMING_SNAKE_CASE      )
											model.to(_SCREAMING_SNAKE_CASE      )
											model.eval()
											__lowerCamelCase   :   Tuple       =model(_SCREAMING_SNAKE_CASE    ,     labels=_SCREAMING_SNAKE_CASE      )
											self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.num_labels)      )




				def        __lowercase	(  self	:List[Any]    ,     __lowercase	:Tuple    ,     __lowercase	:int    ,     __lowercase	:str      ):
											__lowerCamelCase   :   List[Any]       =BitBackbone(config=_SCREAMING_SNAKE_CASE      )
											model.to(_SCREAMING_SNAKE_CASE      )
											model.eval()
											__lowerCamelCase   :   Optional[int]       =model(_SCREAMING_SNAKE_CASE      )

											# verify feature maps
											self.parent.assertEqual(len(result.feature_maps      )    ,     len(config.out_features      )      )
											self.parent.assertListEqual(list(result.feature_maps[0].shape      )    ,     [self.batch_size, self.hidden_sizes[1], 4, 4]      )

											# verify channels
											self.parent.assertEqual(len(model.channels      )    ,     len(config.out_features      )      )
											self.parent.assertListEqual(model.channels    ,     config.hidden_sizes[1:]      )

											# verify backbone works with out_features=None
											__lowerCamelCase   :   Tuple       =None
											__lowerCamelCase   :   Tuple       =BitBackbone(config=_SCREAMING_SNAKE_CASE      )
											model.to(_SCREAMING_SNAKE_CASE      )
											model.eval()
											__lowerCamelCase   :   str       =model(_SCREAMING_SNAKE_CASE      )

											# verify feature maps
											self.parent.assertEqual(len(result.feature_maps      )    ,     1      )
											self.parent.assertListEqual(list(result.feature_maps[0].shape      )    ,     [self.batch_size, self.hidden_sizes[-1], 1, 1]      )

											# verify channels
											self.parent.assertEqual(len(model.channels      )    ,     1      )
											self.parent.assertListEqual(model.channels    ,     [config.hidden_sizes[-1]]      )




				def        __lowercase	(  self	:List[Any]      ):
											__lowerCamelCase   :   Optional[int]       =self.prepare_config_and_inputs()
											__lowerCamelCase   :   int       =config_and_inputs
											__lowerCamelCase   :   List[str]       ={"pixel_values": pixel_values}
											return config, inputs_dict

@require_torch
class 		SCREAMING_SNAKE_CASE_				(     __SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	unittest.TestCase ):
				"""simple docstring"""
				__snake_case :							List[str]									=		(BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else ()
				__snake_case :							int									=		(
				    {"""feature-extraction""": BitModel, """image-classification""": BitForImageClassification}
				    if is_torch_available()
				    else {}
				)

				__snake_case :							Optional[int]									=		False
				__snake_case :							Tuple									=		False
				__snake_case :							Tuple									=		False
				__snake_case :							Optional[int]									=		False
				__snake_case :							str									=		False




				def        __lowercase	(  self	:Optional[Any]      ):
											__lowerCamelCase   :   List[Any]       =BitModelTester(self      )
											__lowerCamelCase   :   Union[str, Any]       =ConfigTester(self    ,     config_class=_SCREAMING_SNAKE_CASE    ,     has_text_modality=_SCREAMING_SNAKE_CASE      )




				def        __lowercase	(  self	:Any      ):
											self.create_and_test_config_common_properties()
											self.config_tester.create_and_test_config_to_json_string()
											self.config_tester.create_and_test_config_to_json_file()
											self.config_tester.create_and_test_config_from_and_save_pretrained()
											self.config_tester.create_and_test_config_with_num_labels()
											self.config_tester.check_config_can_be_init_without_params()
											self.config_tester.check_config_arguments_init()




				def        __lowercase	(  self	:List[Any]      ):
											return




				@unittest.skip(reason='''Bit does not output attentions'''      )
				def        __lowercase	(  self	:int      ):
											pass




				@unittest.skip(reason='''Bit does not use inputs_embeds'''      )
				def        __lowercase	(  self	:Union[str, Any]      ):
											pass




				@unittest.skip(reason='''Bit does not support input and output embeddings'''      )
				def        __lowercase	(  self	:int      ):
											pass




				def        __lowercase	(  self	:Any      ):
											__lowerCamelCase   :   Tuple       =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowerCamelCase   :   Tuple       =model_class(_SCREAMING_SNAKE_CASE      )
																		__lowerCamelCase   :   Dict       =inspect.signature(model.forward      )
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		__lowerCamelCase   :   int       =[*signature.parameters.keys()]

																		__lowerCamelCase   :   List[Any]       =["pixel_values"]
																		self.assertListEqual(arg_names[:1]    ,     _SCREAMING_SNAKE_CASE      )




				def        __lowercase	(  self	:str      ):
											__lowerCamelCase   :   List[Any]       =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE      )




				def        __lowercase	(  self	:Dict      ):
											__lowerCamelCase   :   Dict       =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_backbone(*_SCREAMING_SNAKE_CASE      )




				def        __lowercase	(  self	:str      ):
											__lowerCamelCase   :   Any       =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowerCamelCase   :   List[str]       =model_class(config=_SCREAMING_SNAKE_CASE      )
																		for name, module in model.named_modules():
																									if isinstance(_SCREAMING_SNAKE_CASE    ,     (nn.BatchNormad, nn.GroupNorm)      ):
																																self.assertTrue(
																																    torch.all(module.weight == 1      )    ,     msg=f'Parameter {name} of model {model_class} seems not properly initialized'    ,     )
																																self.assertTrue(
																																    torch.all(module.bias == 0      )    ,     msg=f'Parameter {name} of model {model_class} seems not properly initialized'    ,     )




				def        __lowercase	(  self	:List[Any]      ):

											def check_hidden_states_output(__lowercase	:Optional[int]    ,     __lowercase	:int    ,     __lowercase	:Tuple      ):
																		__lowerCamelCase   :   Optional[int]       =model_class(_SCREAMING_SNAKE_CASE      )
																		model.to(_SCREAMING_SNAKE_CASE      )
																		model.eval()

																		with torch.no_grad():
																									__lowerCamelCase   :   Dict       =model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE      )      )

																		__lowerCamelCase   :   Any       =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																		__lowerCamelCase   :   str       =self.model_tester.num_stages
																		self.assertEqual(len(_SCREAMING_SNAKE_CASE      )    ,     expected_num_stages + 1      )

																		# Bit's feature maps are of shape (batch_size, num_channels, height, width)
																		self.assertListEqual(
																		    list(hidden_states[0].shape[-2:]      )    ,     [self.model_tester.image_size // 4, self.model_tester.image_size // 4]    ,     )

											__lowerCamelCase   :   Optional[Any]       =self.model_tester.prepare_config_and_inputs_for_common()
											__lowerCamelCase   :   Dict       =["preactivation", "bottleneck"]
											for model_class in self.all_model_classes:
																		for layer_type in layers_type:
																									__lowerCamelCase   :   Optional[Any]       =layer_type
																									__lowerCamelCase   :   str       =True
																									check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE      )

																									# check that output_hidden_states also work using config
																									del inputs_dict["output_hidden_states"]
																									__lowerCamelCase   :   Union[str, Any]       =True

																									check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE      )




				@unittest.skip(reason='''Bit does not use feedforward chunking'''      )
				def        __lowercase	(  self	:Any      ):
											pass




				def        __lowercase	(  self	:Union[str, Any]      ):
											__lowerCamelCase   :   Optional[int]       =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE      )




				@slow
				def        __lowercase	(  self	:str      ):

											for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowerCamelCase   :   Tuple       =BitModel.from_pretrained(_SCREAMING_SNAKE_CASE      )
																		self.assertIsNotNone(_SCREAMING_SNAKE_CASE      )



def 					lowerCAmelCase_      (   ):


							'''simple docstring'''







							__lowerCamelCase   :   Optional[int]       =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
							return image
@require_torch
@require_vision
class 		SCREAMING_SNAKE_CASE_				(     unittest.TestCase ):
				"""simple docstring"""




				@cached_property
				def        __lowercase	(  self	:List[str]      ):
											return (
											    BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      ) if is_vision_available() else None
											)




				@slow
				def        __lowercase	(  self	:int      ):
											__lowerCamelCase   :   Any       =BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      ).to(_SCREAMING_SNAKE_CASE      )

											__lowerCamelCase   :   int       =self.default_image_processor
											__lowerCamelCase   :   str       =prepare_img()
											__lowerCamelCase   :   Any       =image_processor(images=_SCREAMING_SNAKE_CASE    ,     return_tensors='''pt'''      ).to(_SCREAMING_SNAKE_CASE      )

											# forward pass
											with torch.no_grad():
																		__lowerCamelCase   :   str       =model(**_SCREAMING_SNAKE_CASE      )

											# verify the logits
											__lowerCamelCase   :   Dict       =torch.Size((1, 1000)      )
											self.assertEqual(outputs.logits.shape    ,     _SCREAMING_SNAKE_CASE      )

											__lowerCamelCase   :   int       =torch.tensor([[-0.6526, -0.5263, -1.4398]]      ).to(_SCREAMING_SNAKE_CASE      )

											self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,     _SCREAMING_SNAKE_CASE    ,     atol=1e-4      )      )

@require_torch
class 		SCREAMING_SNAKE_CASE_				(     __SCREAMING_SNAKE_CASE							,	unittest.TestCase ):
				"""simple docstring"""
				__snake_case :							Dict									=		(BitBackbone,) if is_torch_available() else ()
				__snake_case :							List[str]									=		BitConfig

				__snake_case :							Union[str, Any]									=		False




				def        __lowercase	(  self	:Any      ):
											__lowerCamelCase   :   Dict       =BitModelTester(self      )









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