





"""simple docstring"""




from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_outputs import (
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import logging
from .configuration_regnet import RegNetConfig


a						: Any     	=			logging.get_logger(__name__)

# General docstring
a						: Optional[Any]     	=			'''RegNetConfig'''

# Base docstring
a						: str     	=			'''facebook/regnet-y-040'''
a						: Optional[Any]     	=			[1, 1088, 7, 7]

# Image classification docstring
a						: Tuple     	=			'''facebook/regnet-y-040'''
a						: Dict     	=			'''tabby, tabby cat'''

a						: int     	=			[
    '''facebook/regnet-y-040''',
    # See all regnet models at https://huggingface.co/models?filter=regnet
]


class        __UpperCamelCase				(						nn.Module						):






							def __init__(   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = 3      ,					lowerCAmelCase__ = 1      ,					lowerCAmelCase__ = 1      ,					lowerCAmelCase__ = "relu"      ,					)	-> Optional[Any]:
								super().__init__()
								a      :		Optional[Any]	=				nn.Convad(
								    lowerCAmelCase__      ,					lowerCAmelCase__      ,					kernel_size=lowerCAmelCase__      ,					stride=lowerCAmelCase__      ,					padding=kernel_size // 2      ,					groups=lowerCAmelCase__      ,					bias=lowerCAmelCase__      ,					)
								a      :		int	=				nn.BatchNormad(lowerCAmelCase__      )
								a      :		List[str]	=				ACTaFN[activation] if activation is not None else nn.Identity()






							def     __a   (   self      ,					lowerCAmelCase__      )	-> int:
								a      :		List[str]	=				self.convolution(lowerCAmelCase__      )
								a      :		Any	=				self.normalization(lowerCAmelCase__      )
								a      :		Optional[Any]	=				self.activation(lowerCAmelCase__      )
								return hidden_state


class        __UpperCamelCase				(						nn.Module						):






							def __init__(   self      ,					lowerCAmelCase__      )	-> int:
								super().__init__()
								a      :		Union[str, Any]	=				RegNetConvLayer(
								    config.num_channels      ,					config.embedding_size      ,					kernel_size=3      ,					stride=2      ,					activation=config.hidden_act      )
								a      :		List[str]	=				config.num_channels






							def     __a   (   self      ,					lowerCAmelCase__      )	-> Tuple:
								a      :		Union[str, Any]	=				pixel_values.shape[1]
								if num_channels != self.num_channels:
									raise ValueError(
									    "Make sure that the channel dimension of the pixel values match with the one set in the configuration."      )
								a      :		Any	=				self.embedder(lowerCAmelCase__      )
								return hidden_state


class        __UpperCamelCase				(						nn.Module						):






							def __init__(   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = 2      )	-> List[Any]:
								super().__init__()
								a      :		List[Any]	=				nn.Convad(lowerCAmelCase__      ,					lowerCAmelCase__      ,					kernel_size=1      ,					stride=lowerCAmelCase__      ,					bias=lowerCAmelCase__      )
								a      :		Union[str, Any]	=				nn.BatchNormad(lowerCAmelCase__      )






							def     __a   (   self      ,					lowerCAmelCase__      )	-> Tensor:
								a      :		List[Any]	=				self.convolution(lowerCAmelCase__      )
								a      :		List[Any]	=				self.normalization(lowerCAmelCase__      )
								return hidden_state


class        __UpperCamelCase				(						nn.Module						):






							def __init__(   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      )	-> Optional[int]:
								super().__init__()

								a      :		int	=				nn.AdaptiveAvgPoolad((1, 1)      )
								a      :		Dict	=				nn.Sequential(
								    nn.Convad(lowerCAmelCase__      ,					lowerCAmelCase__      ,					kernel_size=1      )      ,					nn.ReLU()      ,					nn.Convad(lowerCAmelCase__      ,					lowerCAmelCase__      ,					kernel_size=1      )      ,					nn.Sigmoid()      ,					)






							def     __a   (   self      ,					lowerCAmelCase__      )	-> Union[str, Any]:
								# b c h w -> b c 1 1
								a      :		int	=				self.pooler(lowerCAmelCase__      )
								a      :		Optional[Any]	=				self.attention(lowerCAmelCase__      )
								a      :		Any	=				hidden_state * attention
								return hidden_state


class        __UpperCamelCase				(						nn.Module						):






							def __init__(   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = 1      )	-> List[str]:
								super().__init__()
								a      :		str	=				in_channels != out_channels or stride != 1
								a      :		str	=				max(1      ,					out_channels // config.groups_width      )
								a      :		int	=				(
								    RegNetShortCut(lowerCAmelCase__      ,					lowerCAmelCase__      ,					stride=lowerCAmelCase__      ) if should_apply_shortcut else nn.Identity()
								)
								a      :		Tuple	=				nn.Sequential(
								    RegNetConvLayer(lowerCAmelCase__      ,					lowerCAmelCase__      ,					kernel_size=1      ,					activation=config.hidden_act      )      ,					RegNetConvLayer(lowerCAmelCase__      ,					lowerCAmelCase__      ,					stride=lowerCAmelCase__      ,					groups=lowerCAmelCase__      ,					activation=config.hidden_act      )      ,					RegNetConvLayer(lowerCAmelCase__      ,					lowerCAmelCase__      ,					kernel_size=1      ,					activation=lowerCAmelCase__      )      ,					)
								a      :		List[Any]	=				ACTaFN[config.hidden_act]






							def     __a   (   self      ,					lowerCAmelCase__      )	-> Union[str, Any]:
								a      :		Any	=				hidden_state
								a      :		int	=				self.layer(lowerCAmelCase__      )
								a      :		Union[str, Any]	=				self.shortcut(lowerCAmelCase__      )
								hidden_state += residual
								a      :		Tuple	=				self.activation(lowerCAmelCase__      )
								return hidden_state


class        __UpperCamelCase				(						nn.Module						):






							def __init__(   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = 1      )	-> str:
								super().__init__()
								a      :		Optional[int]	=				in_channels != out_channels or stride != 1
								a      :		str	=				max(1      ,					out_channels // config.groups_width      )
								a      :		int	=				(
								    RegNetShortCut(lowerCAmelCase__      ,					lowerCAmelCase__      ,					stride=lowerCAmelCase__      ) if should_apply_shortcut else nn.Identity()
								)
								a      :		int	=				nn.Sequential(
								    RegNetConvLayer(lowerCAmelCase__      ,					lowerCAmelCase__      ,					kernel_size=1      ,					activation=config.hidden_act      )      ,					RegNetConvLayer(lowerCAmelCase__      ,					lowerCAmelCase__      ,					stride=lowerCAmelCase__      ,					groups=lowerCAmelCase__      ,					activation=config.hidden_act      )      ,					RegNetSELayer(lowerCAmelCase__      ,					reduced_channels=int(round(in_channels / 4      )      )      )      ,					RegNetConvLayer(lowerCAmelCase__      ,					lowerCAmelCase__      ,					kernel_size=1      ,					activation=lowerCAmelCase__      )      ,					)
								a      :		Optional[Any]	=				ACTaFN[config.hidden_act]






							def     __a   (   self      ,					lowerCAmelCase__      )	-> Optional[Any]:
								a      :		str	=				hidden_state
								a      :		int	=				self.layer(lowerCAmelCase__      )
								a      :		Any	=				self.shortcut(lowerCAmelCase__      )
								hidden_state += residual
								a      :		int	=				self.activation(lowerCAmelCase__      )
								return hidden_state


class        __UpperCamelCase				(						nn.Module						):






							def __init__(   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = 2      ,					lowerCAmelCase__ = 2      ,					)	-> int:
								super().__init__()

								a      :		Optional[int]	=				RegNetXLayer if config.layer_type == "x" else RegNetYLayer

								a      :		Optional[int]	=				nn.Sequential(
								    # downsampling is done in the first layer with stride of 2
								    layer(
								        lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					stride=lowerCAmelCase__      ,					)      ,					*[layer(lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ) for _ in range(depth - 1      )]      ,					)






							def     __a   (   self      ,					lowerCAmelCase__      )	-> Optional[Any]:
								a      :		Optional[Any]	=				self.layers(lowerCAmelCase__      )
								return hidden_state


class        __UpperCamelCase				(						nn.Module						):






							def __init__(   self      ,					lowerCAmelCase__      )	-> str:
								super().__init__()
								a      :		Any	=				nn.ModuleList([]      )
								# based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
								self.stages.append(
								    RegNetStage(
								        lowerCAmelCase__      ,					config.embedding_size      ,					config.hidden_sizes[0]      ,					stride=2 if config.downsample_in_first_stage else 1      ,					depth=config.depths[0]      ,					)      )
								a      :		Any	=				zip(config.hidden_sizes      ,					config.hidden_sizes[1:]      )
								for (in_channels, out_channels), depth in zip(lowerCAmelCase__      ,					config.depths[1:]      ):
									self.stages.append(RegNetStage(lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					depth=lowerCAmelCase__      )      )






							def     __a   (   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = False      ,					lowerCAmelCase__ = True      )	-> BaseModelOutputWithNoAttention:
								a      :		Any	=				() if output_hidden_states else None

								for stage_module in self.stages:
									if output_hidden_states:
										a      :		Any	=				hidden_states + (hidden_state,)

									a      :		Any	=				stage_module(lowerCAmelCase__      )

								if output_hidden_states:
									a      :		Dict	=				hidden_states + (hidden_state,)

								if not return_dict:
									return tuple(v for v in [hidden_state, hidden_states] if v is not None      )

								return BaseModelOutputWithNoAttention(last_hidden_state=lowerCAmelCase__      ,					hidden_states=lowerCAmelCase__      )


class        __UpperCamelCase				(						a__						):
							lowerCamelCase   :					Union[str, Any]      					=RegNetConfig
							lowerCamelCase   :					Dict      					="""regnet"""
							lowerCamelCase   :					Tuple      					="""pixel_values"""
							lowerCamelCase   :					List[Any]      					=True






							def     __a   (   self      ,					lowerCAmelCase__      )	-> List[Any]:
								if isinstance(lowerCAmelCase__      ,					nn.Convad      ):
									nn.init.kaiming_normal_(module.weight      ,					mode="fan_out"      ,					nonlinearity="relu"      )
								elif isinstance(lowerCAmelCase__      ,					(nn.BatchNormad, nn.GroupNorm)      ):
									nn.init.constant_(module.weight      ,					1      )
									nn.init.constant_(module.bias      ,					0      )






							def     __a   (   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__=False      )	-> List[Any]:
								if isinstance(lowerCAmelCase__      ,					lowerCAmelCase__      ):
									a      :		Any	=				value
a						: str     	=			R'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

a						: Optional[int]     	=			R'''
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple.
'''


@add_start_docstrings(
    """The bare RegNet model outputting raw features without any specific head on top."""		,       a__		,       )
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class        __UpperCamelCase				(						a__						):






							def __init__(   self      ,					lowerCAmelCase__      )	-> int:
								super().__init__(lowerCAmelCase__      )
								a      :		Tuple	=				config
								a      :		Optional[int]	=				RegNetEmbeddings(lowerCAmelCase__      )
								a      :		List[str]	=				RegNetEncoder(lowerCAmelCase__      )
								a      :		Tuple	=				nn.AdaptiveAvgPoolad((1, 1)      )
								# Initialize weights and apply final processing
								self.post_init()






							@add_start_docstrings_to_model_forward(lowerCAmelCase__      )
							@add_code_sample_docstrings(
							    checkpoint=_CHECKPOINT_FOR_DOC      ,					output_type=lowerCAmelCase__      ,					config_class=_CONFIG_FOR_DOC      ,					modality="vision"      ,					expected_output=_EXPECTED_OUTPUT_SHAPE      ,					)
							def     __a   (   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      )	-> BaseModelOutputWithPoolingAndNoAttention:
								a      :		Optional[Any]	=				(
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								a      :		str	=				return_dict if return_dict is not None else self.config.use_return_dict

								a      :		Tuple	=				self.embedder(lowerCAmelCase__      )

								a      :		Any	=				self.encoder(
								    lowerCAmelCase__      ,					output_hidden_states=lowerCAmelCase__      ,					return_dict=lowerCAmelCase__      )

								a      :		Any	=				encoder_outputs[0]

								a      :		Union[str, Any]	=				self.pooler(lowerCAmelCase__      )

								if not return_dict:
									return (last_hidden_state, pooled_output) + encoder_outputs[1:]

								return BaseModelOutputWithPoolingAndNoAttention(
								    last_hidden_state=lowerCAmelCase__      ,					pooler_output=lowerCAmelCase__      ,					hidden_states=encoder_outputs.hidden_states      ,					)


@add_start_docstrings(
    """
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """		,       a__		,       )
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class        __UpperCamelCase				(						a__						):






							def __init__(   self      ,					lowerCAmelCase__      )	-> str:
								super().__init__(lowerCAmelCase__      )
								a      :		List[str]	=				config.num_labels
								a      :		Dict	=				RegNetModel(lowerCAmelCase__      )
								# classification head
								a      :		Optional[Any]	=				nn.Sequential(
								    nn.Flatten()      ,					nn.Linear(config.hidden_sizes[-1]      ,					config.num_labels      ) if config.num_labels > 0 else nn.Identity()      ,					)
								# initialize weights and apply final processing
								self.post_init()






							@add_start_docstrings_to_model_forward(lowerCAmelCase__      )
							@add_code_sample_docstrings(
							    checkpoint=_IMAGE_CLASS_CHECKPOINT      ,					output_type=lowerCAmelCase__      ,					config_class=_CONFIG_FOR_DOC      ,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT      ,					)
							def     __a   (   self      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					)	-> ImageClassifierOutputWithNoAttention:
								a      :		Dict	=				return_dict if return_dict is not None else self.config.use_return_dict

								a      :		List[Any]	=				self.regnet(lowerCAmelCase__      ,					output_hidden_states=lowerCAmelCase__      ,					return_dict=lowerCAmelCase__      )

								a      :		Any	=				outputs.pooler_output if return_dict else outputs[1]

								a      :		List[str]	=				self.classifier(lowerCAmelCase__      )

								a      :		str	=				None

								if labels is not None:
									if self.config.problem_type is None:
										if self.num_labels == 1:
											a      :		Optional[int]	=				"regression"
										elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
											a      :		Optional[Any]	=				"single_label_classification"
										else:
											a      :		str	=				"multi_label_classification"
									if self.config.problem_type == "regression":
										a      :		Optional[Any]	=				MSELoss()
										if self.num_labels == 1:
											a      :		List[Any]	=				loss_fct(logits.squeeze()      ,					labels.squeeze()      )
										else:
											a      :		str	=				loss_fct(lowerCAmelCase__      ,					lowerCAmelCase__      )
									elif self.config.problem_type == "single_label_classification":
										a      :		Union[str, Any]	=				CrossEntropyLoss()
										a      :		Tuple	=				loss_fct(logits.view(-1      ,					self.num_labels      )      ,					labels.view(-1      )      )
									elif self.config.problem_type == "multi_label_classification":
										a      :		Dict	=				BCEWithLogitsLoss()
										a      :		List[Any]	=				loss_fct(lowerCAmelCase__      ,					lowerCAmelCase__      )

								if not return_dict:
									a      :		int	=				(logits,) + outputs[2:]
									return (loss,) + output if loss is not None else output

								return ImageClassifierOutputWithNoAttention(loss=lowerCAmelCase__      ,					logits=lowerCAmelCase__      ,					hidden_states=outputs.hidden_states      )






import unittest

import numpy as np

from transformers import is_flax_available
from transformers.testing_utils import require_flax

from ..test_modeling_flax_common import ids_tensor


if is_flax_available():
     import jax
     import jax.numpy as jnp

     from transformers.generation import (
         FlaxForcedBOSTokenLogitsProcessor,
         FlaxForcedEOSTokenLogitsProcessor,
         FlaxLogitsProcessorList,
         FlaxMinLengthLogitsProcessor,
         FlaxTemperatureLogitsWarper,
         FlaxTopKLogitsWarper,
         FlaxTopPLogitsWarper,
     )






@require_flax
class   _lowerCamelCase (					unittest.TestCase			):




       """simple docstring"""



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




         '''simple docstring'''





         A_      :				Optional[int]		     =					jnp.ones((batch_size, length)		) / length
         return scores



       def        _snake_case       (      self		)->Tuple:




         '''simple docstring'''





         A_      :				Union[str, Any]		     =					None
         A_      :				Any		     =					20

         A_      :				Any		     =					self._get_uniform_logits(batch_size=2				,       length=_SCREAMING_SNAKE_CASE		)

         # tweak scores to not be uniform anymore
         A_      :				Dict		     =					scores.at[1, 5].set((1 / length) + 0.1		)  # peak, 1st batch
         A_      :				Tuple		     =					scores.at[1, 10].set((1 / length) - 0.4		)  # valley, 1st batch

         # compute softmax
         A_      :				List[str]		     =					jax.nn.softmax(_SCREAMING_SNAKE_CASE				,       axis=-1		)

         A_      :				Any		     =					FlaxTemperatureLogitsWarper(temperature=0.5		)
         A_      :				Optional[int]		     =					FlaxTemperatureLogitsWarper(temperature=1.3		)

         A_      :				Optional[Any]		     =					jax.nn.softmax(temp_dist_warper_sharper(_SCREAMING_SNAKE_CASE				,       scores.copy()				,       cur_len=_SCREAMING_SNAKE_CASE		)				,       axis=-1		)
         A_      :				List[Any]		     =					jax.nn.softmax(temp_dist_warper_smoother(_SCREAMING_SNAKE_CASE				,       scores.copy()				,       cur_len=_SCREAMING_SNAKE_CASE		)				,       axis=-1		)

         # uniform distribution stays uniform
         self.assertTrue(jnp.allclose(probs[0, :]				,       warped_prob_sharp[0, :]				,       atol=1e-3		)		)
         self.assertTrue(jnp.allclose(probs[0, :]				,       warped_prob_smooth[0, :]				,       atol=1e-3		)		)

         # sharp peaks get higher, valleys get lower
         self.assertLess(probs[1, :].max()				,       warped_prob_sharp[1, :].max()		)
         self.assertGreater(probs[1, :].min()				,       warped_prob_sharp[1, :].min()		)

         # smooth peaks get lower, valleys get higher
         self.assertGreater(probs[1, :].max()				,       warped_prob_smooth[1, :].max()		)
         self.assertLess(probs[1, :].min()				,       warped_prob_smooth[1, :].min()		)



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




         '''simple docstring'''





         A_      :				Any		     =					None
         A_      :				List[Any]		     =					10
         A_      :				str		     =					2

         # create ramp distribution
         A_      :				Any		     =					np.broadcast_to(np.arange(_SCREAMING_SNAKE_CASE		)[None, :]				,       (batch_size, vocab_size)		).copy()
         A_      :				List[Any]		     =					ramp_logits[1:, : vocab_size // 2] + vocab_size

         A_      :				Any		     =					FlaxTopKLogitsWarper(3		)

         A_      :				Tuple		     =					top_k_warp(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)

         # check that correct tokens are filtered
         self.assertListEqual(jnp.isinf(scores[0]		).tolist()				,       7 * [True] + 3 * [False]		)
         self.assertListEqual(jnp.isinf(scores[1]		).tolist()				,       2 * [True] + 3 * [False] + 5 * [True]		)

         # check special case
         A_      :				Optional[int]		     =					5
         A_      :				List[Any]		     =					FlaxTopKLogitsWarper(top_k=1				,       filter_value=0.0				,       min_tokens_to_keep=3		)

         A_      :				Optional[Any]		     =					np.broadcast_to(np.arange(_SCREAMING_SNAKE_CASE		)[None, :]				,       (batch_size, length)		).copy()
         A_      :				Dict		     =					top_k_warp_safety_check(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)

         # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified
         self.assertListEqual((scores == 0.0).sum(axis=-1		).tolist()				,       [2, 2]		)



       def        _snake_case       (      self		)->Any:




         '''simple docstring'''





         A_      :				str		     =					None
         A_      :				Optional[Any]		     =					10
         A_      :				Any		     =					2

         # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper)
         A_      :				Optional[int]		     =					np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.1_5, 0.3, 0.3, 0.2_5]]		)		)

         A_      :				str		     =					FlaxTopPLogitsWarper(0.8		)
         A_      :				Optional[int]		     =					np.exp(top_p_warp(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)		)

         # dist should be filtered to keep min num values so that sum is >= top_p
         # exp (-inf) => 0
         A_      :				Tuple		     =					np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.2_5]]		)
         self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       atol=1e-3		)		)

         # check edge cases with negative and extreme logits
         A_      :				Union[str, Any]		     =					np.broadcast_to(np.arange(_SCREAMING_SNAKE_CASE		)[None, :]				,       (batch_size, vocab_size)		).copy() - (
             vocab_size // 2
         )

         # make ramp_logits more extreme
         A_      :				str		     =					ramp_logits[1] * 1_0_0.0

         # make sure at least 2 tokens are kept
         A_      :				str		     =					FlaxTopPLogitsWarper(0.9				,       min_tokens_to_keep=2				,       filter_value=0.0		)
         A_      :				str		     =					top_p_warp(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)

         # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2.
         self.assertListEqual((filtered_dist != 0.0).sum(axis=-1		).tolist()				,       [3, 2]		)



       def        _snake_case       (      self		)->Any:




         '''simple docstring'''





         A_      :				str		     =					20
         A_      :				Union[str, Any]		     =					4
         A_      :				Optional[Any]		     =					0

         A_      :				Union[str, Any]		     =					FlaxMinLengthLogitsProcessor(min_length=10				,       eos_token_id=_SCREAMING_SNAKE_CASE		)

         # check that min length is applied at length 5
         A_      :				int		     =					ids_tensor((batch_size, 20)				,       vocab_size=20		)
         A_      :				List[Any]		     =					5
         A_      :				Optional[int]		     =					self._get_uniform_logits(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)
         A_      :				Optional[int]		     =					min_dist_processor(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
         self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist()				,       4 * [-float('''inf'''		)]		)

         # check that min length is not applied anymore at length 15
         A_      :				Tuple		     =					self._get_uniform_logits(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)
         A_      :				Any		     =					15
         A_      :				int		     =					min_dist_processor(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
         self.assertFalse(jnp.isinf(_SCREAMING_SNAKE_CASE		).any()		)



       def        _snake_case       (      self		)->Optional[Any]:




         '''simple docstring'''





         A_      :				Optional[int]		     =					20
         A_      :				Optional[int]		     =					4
         A_      :				Optional[int]		     =					0

         A_      :				Optional[Any]		     =					FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_SCREAMING_SNAKE_CASE		)

         # check that all scores are -inf except the bos_token_id score
         A_      :				Optional[Any]		     =					ids_tensor((batch_size, 1)				,       vocab_size=20		)
         A_      :				str		     =					1
         A_      :				List[str]		     =					self._get_uniform_logits(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)
         A_      :				List[str]		     =					logits_processor(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
         self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :]		).all()		)
         self.assertListEqual(scores[:, bos_token_id].tolist()				,       4 * [0]		)  # score for bos_token_id shold be zero

         # check that bos_token_id is not forced if current length is greater than 1
         A_      :				Optional[int]		     =					3
         A_      :				List[Any]		     =					self._get_uniform_logits(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)
         A_      :				Tuple		     =					logits_processor(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
         self.assertFalse(jnp.isinf(_SCREAMING_SNAKE_CASE		).any()		)



       def        _snake_case       (      self		)->List[str]:




         '''simple docstring'''





         A_      :				Union[str, Any]		     =					20
         A_      :				str		     =					4
         A_      :				Dict		     =					0
         A_      :				Optional[int]		     =					5

         A_      :				Tuple		     =					FlaxForcedEOSTokenLogitsProcessor(max_length=_SCREAMING_SNAKE_CASE				,       eos_token_id=_SCREAMING_SNAKE_CASE		)

         # check that all scores are -inf except the eos_token_id when max_length is reached
         A_      :				List[Any]		     =					ids_tensor((batch_size, 4)				,       vocab_size=20		)
         A_      :				Any		     =					4
         A_      :				Optional[Any]		     =					self._get_uniform_logits(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)
         A_      :				List[Any]		     =					logits_processor(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
         self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :]		).all()		)
         self.assertListEqual(scores[:, eos_token_id].tolist()				,       4 * [0]		)  # score for eos_token_id should be zero

         # check that eos_token_id is not forced if max_length is not reached
         A_      :				int		     =					3
         A_      :				Union[str, Any]		     =					self._get_uniform_logits(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)
         A_      :				Dict		     =					logits_processor(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
         self.assertFalse(jnp.isinf(_SCREAMING_SNAKE_CASE		).any()		)



       def        _snake_case       (      self		)->str:




         '''simple docstring'''





         A_      :				str		     =					4
         A_      :				Dict		     =					10
         A_      :				Union[str, Any]		     =					15
         A_      :				str		     =					2
         A_      :				int		     =					1
         A_      :				List[str]		     =					15

         # dummy input_ids and scores
         A_      :				Tuple		     =					ids_tensor((batch_size, sequence_length)				,       _SCREAMING_SNAKE_CASE		)
         A_      :				int		     =					input_ids.copy()

         A_      :				List[Any]		     =					self._get_uniform_logits(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)
         A_      :				Union[str, Any]		     =					scores.copy()

         # instantiate all dist processors
         A_      :				Dict		     =					FlaxTemperatureLogitsWarper(temperature=0.5		)
         A_      :				Any		     =					FlaxTopKLogitsWarper(3		)
         A_      :				List[Any]		     =					FlaxTopPLogitsWarper(0.8		)

         # instantiate all logits processors
         A_      :				List[Any]		     =					FlaxMinLengthLogitsProcessor(min_length=10				,       eos_token_id=_SCREAMING_SNAKE_CASE		)
         A_      :				Optional[int]		     =					FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_SCREAMING_SNAKE_CASE		)
         A_      :				Dict		     =					FlaxForcedEOSTokenLogitsProcessor(max_length=_SCREAMING_SNAKE_CASE				,       eos_token_id=_SCREAMING_SNAKE_CASE		)

         A_      :				Union[str, Any]		     =					10

         # no processor list
         A_      :				int		     =					temp_dist_warp(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
         A_      :				List[str]		     =					top_k_warp(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
         A_      :				Any		     =					top_p_warp(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
         A_      :				Dict		     =					min_dist_proc(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
         A_      :				Optional[int]		     =					bos_dist_proc(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
         A_      :				Optional[Any]		     =					eos_dist_proc(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)

         # with processor list
         A_      :				Any		     =					FlaxLogitsProcessorList(
             [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]		)
         A_      :				List[str]		     =					processor(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)

         # scores should be equal
         self.assertTrue(jnp.allclose(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       atol=1e-3		)		)

         # input_ids should never be changed
         self.assertListEqual(input_ids.tolist()				,       input_ids_comp.tolist()		)







       def        _snake_case       (      self		)->Dict:




         '''simple docstring'''





         A_      :				str		     =					4
         A_      :				Dict		     =					10
         A_      :				Tuple		     =					15
         A_      :				List[str]		     =					2
         A_      :				List[str]		     =					1
         A_      :				Union[str, Any]		     =					15

         # dummy input_ids and scores
         A_      :				Any		     =					ids_tensor((batch_size, sequence_length)				,       _SCREAMING_SNAKE_CASE		)
         A_      :				Union[str, Any]		     =					input_ids.copy()

         A_      :				Optional[Any]		     =					self._get_uniform_logits(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)
         A_      :				Tuple		     =					scores.copy()

         # instantiate all dist processors
         A_      :				List[str]		     =					FlaxTemperatureLogitsWarper(temperature=0.5		)
         A_      :				Optional[Any]		     =					FlaxTopKLogitsWarper(3		)
         A_      :				int		     =					FlaxTopPLogitsWarper(0.8		)

         # instantiate all logits processors
         A_      :				List[Any]		     =					FlaxMinLengthLogitsProcessor(min_length=10				,       eos_token_id=_SCREAMING_SNAKE_CASE		)
         A_      :				Dict		     =					FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_SCREAMING_SNAKE_CASE		)
         A_      :				Optional[Any]		     =					FlaxForcedEOSTokenLogitsProcessor(max_length=_SCREAMING_SNAKE_CASE				,       eos_token_id=_SCREAMING_SNAKE_CASE		)

         A_      :				str		     =					10

         # no processor list
         def run_no_processor_list(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		):
           A_      :				int		     =					temp_dist_warp(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
           A_      :				Optional[Any]		     =					top_k_warp(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
           A_      :				List[Any]		     =					top_p_warp(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
           A_      :				Dict		     =					min_dist_proc(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
           A_      :				Any		     =					bos_dist_proc(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
           A_      :				Optional[Any]		     =					eos_dist_proc(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
           return scores

         # with processor list
         def run_processor_list(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		):
           A_      :				Optional[int]		     =					FlaxLogitsProcessorList(
               [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]		)
           A_      :				Optional[int]		     =					processor(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       cur_len=_SCREAMING_SNAKE_CASE		)
           return scores

         A_      :				Optional[int]		     =					jax.jit(_SCREAMING_SNAKE_CASE		)
         A_      :				Union[str, Any]		     =					jax.jit(_SCREAMING_SNAKE_CASE		)

         A_      :				Dict		     =					jitted_run_no_processor_list(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)
         A_      :				List[Any]		     =					jitted_run_processor_list(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)

         # scores should be equal
         self.assertTrue(jnp.allclose(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       atol=1e-3		)		)

         # input_ids should never be changed
         self.assertListEqual(input_ids.tolist()				,       input_ids_comp.tolist()		)




