
"""simple docstring"""







from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFSequenceClassifierOutput,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import logging
from .configuration_regnet import RegNetConfig


lowercase_         =       logging.get_logger(__name__)

# General docstring
lowercase_         =       "RegNetConfig"

# Base docstring
lowercase_         =       "facebook/regnet-y-040"
lowercase_         =       [1, 1088, 7, 7]

# Image classification docstring
lowercase_         =       "facebook/regnet-y-040"
lowercase_         =       "tabby, tabby cat"

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



class 		UpperCAmelCase_						(tf.keras.layers.Layer    ):
 """simple docstring"""




 def __init__(			self							:			Optional[int]  , a_							:			int  , a_							:			int = 3  , a_							:			int = 1  , a_							:			int = 1  , a_							:			Optional[str] = "relu"  , **a_							:			List[str]  , )->       Union[str, Any]:



      """simple docstring"""

      super().__init__(**a_			)
      # The padding and conv has been verified in
      # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb
      UpperCAmelCase_    :     Tuple            =	tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2			)
      UpperCAmelCase_    :     Optional[Any]            =	tf.keras.layers.ConvaD(
          filters=a_  , kernel_size=a_  , strides=a_  , padding="""VALID"""  , groups=a_  , use_bias=a_  , name="""convolution"""  , )
      UpperCAmelCase_    :     Tuple            =	tf.keras.layers.BatchNormalization(epsilon=1E-5  , momentum=0.9  , name="""normalization"""			)
      UpperCAmelCase_    :     int            =	ACTaFN[activation] if activation is not None else tf.identity






 def 	a   (			self							:			Optional[int]  , a_							:			Union[str, Any]			)->       Optional[int]:



      """simple docstring"""

      UpperCAmelCase_    :     Union[str, Any]            =	self.convolution(self.padding(a_			)			)
      UpperCAmelCase_    :     Optional[Any]            =	self.normalization(a_			)
      UpperCAmelCase_    :     Dict            =	self.activation(a_			)
      return hidden_state



class 		UpperCAmelCase_						(tf.keras.layers.Layer    ):
 """simple docstring"""




 def __init__(			self							:			Tuple  , a_							:			RegNetConfig  , **a_							:			List[str]			)->       str:



      """simple docstring"""

      super().__init__(**a_			)
      UpperCAmelCase_    :     Optional[int]            =	config.num_channels
      UpperCAmelCase_    :     List[Any]            =	TFRegNetConvLayer(
          out_channels=config.embedding_size  , kernel_size=3  , stride=2  , activation=config.hidden_act  , name="""embedder"""  , )






 def 	a   (			self							:			Tuple  , a_							:			int			)->       int:



      """simple docstring"""

      UpperCAmelCase_    :     str            =	shape_list(a_			)[1]
      if tf.executing_eagerly() and 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."""			)

      # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format.
      # So change the input format from `NCHW` to `NHWC`.
      # shape = (batch_size, in_height, in_width, in_channels=num_channels)
      UpperCAmelCase_    :     int            =	tf.transpose(a_  , perm=(0, 2, 3, 1)			)
      UpperCAmelCase_    :     Optional[int]            =	self.embedder(a_			)
      return hidden_state



class 		UpperCAmelCase_						(tf.keras.layers.Layer    ):
 """simple docstring"""




 def __init__(			self							:			Union[str, Any]  , a_							:			int  , a_							:			int = 2  , **a_							:			Tuple			)->       int:



      """simple docstring"""

      super().__init__(**a_			)
      UpperCAmelCase_    :     Optional[int]            =	tf.keras.layers.ConvaD(
          filters=a_  , kernel_size=1  , strides=a_  , use_bias=a_  , name="""convolution"""			)
      UpperCAmelCase_    :     Dict            =	tf.keras.layers.BatchNormalization(epsilon=1E-5  , momentum=0.9  , name="""normalization"""			)






 def 	a   (			self							:			Optional[Any]  , a_							:			tf.Tensor  , a_							:			bool = False			)->       tf.Tensor:



      """simple docstring"""

      return self.normalization(self.convolution(a_			)  , training=a_			)



class 		UpperCAmelCase_						(tf.keras.layers.Layer    ):
 """simple docstring"""




 def __init__(			self							:			Tuple  , a_							:			int  , a_							:			int  , **a_							:			List[Any]			)->       Union[str, Any]:



      """simple docstring"""

      super().__init__(**a_			)
      UpperCAmelCase_    :     Any            =	tf.keras.layers.GlobalAveragePoolingaD(keepdims=a_  , name="""pooler"""			)
      UpperCAmelCase_    :     Dict            =	[
          tf.keras.layers.ConvaD(filters=a_  , kernel_size=1  , activation="""relu"""  , name="""attention.0"""			),
          tf.keras.layers.ConvaD(filters=a_  , kernel_size=1  , activation="""sigmoid"""  , name="""attention.2"""			),
      ]






 def 	a   (			self							:			List[str]  , a_							:			List[Any]			)->       Tuple:



      """simple docstring"""

      UpperCAmelCase_    :     Union[str, Any]            =	self.pooler(a_			)
      for layer_module in self.attention:
           UpperCAmelCase_    :     Optional[Any]            =	layer_module(a_			)
      UpperCAmelCase_    :     Dict            =	hidden_state * pooled
      return hidden_state



class 		UpperCAmelCase_						(tf.keras.layers.Layer    ):
 """simple docstring"""




 def __init__(			self							:			Union[str, Any]  , a_							:			RegNetConfig  , a_							:			int  , a_							:			int  , a_							:			int = 1  , **a_							:			Any			)->       str:



      """simple docstring"""

      super().__init__(**a_			)
      UpperCAmelCase_    :     List[str]            =	in_channels != out_channels or stride != 1
      UpperCAmelCase_    :     int            =	max(1  , out_channels // config.groups_width			)
      UpperCAmelCase_    :     Optional[int]            =	(
          TFRegNetShortCut(a_  , stride=a_  , name="""shortcut"""			)
          if should_apply_shortcut
          else tf.keras.layers.Activation("""linear"""  , name="""shortcut"""			)
      )
      # `self.layers` instead of `self.layer` because that is a reserved argument.
      UpperCAmelCase_    :     List[str]            =	[
          TFRegNetConvLayer(a_  , kernel_size=1  , activation=config.hidden_act  , name="""layer.0"""			),
          TFRegNetConvLayer(
              a_  , stride=a_  , groups=a_  , activation=config.hidden_act  , name="""layer.1"""			),
          TFRegNetConvLayer(a_  , kernel_size=1  , activation=a_  , name="""layer.2"""			),
      ]
      UpperCAmelCase_    :     Optional[int]            =	ACTaFN[config.hidden_act]






 def 	a   (			self							:			str  , a_							:			Tuple			)->       Any:



      """simple docstring"""

      UpperCAmelCase_    :     Any            =	hidden_state
      for layer_module in self.layers:
           UpperCAmelCase_    :     Dict            =	layer_module(a_			)
      UpperCAmelCase_    :     Dict            =	self.shortcut(a_			)
      hidden_state += residual
      UpperCAmelCase_    :     List[Any]            =	self.activation(a_			)
      return hidden_state



class 		UpperCAmelCase_						(tf.keras.layers.Layer    ):
 """simple docstring"""




 def __init__(			self							:			Dict  , a_							:			RegNetConfig  , a_							:			int  , a_							:			int  , a_							:			int = 1  , **a_							:			Optional[int]			)->       int:



      """simple docstring"""

      super().__init__(**a_			)
      UpperCAmelCase_    :     Tuple            =	in_channels != out_channels or stride != 1
      UpperCAmelCase_    :     Optional[int]            =	max(1  , out_channels // config.groups_width			)
      UpperCAmelCase_    :     Any            =	(
          TFRegNetShortCut(a_  , stride=a_  , name="""shortcut"""			)
          if should_apply_shortcut
          else tf.keras.layers.Activation("""linear"""  , name="""shortcut"""			)
      )
      UpperCAmelCase_    :     Union[str, Any]            =	[
          TFRegNetConvLayer(a_  , kernel_size=1  , activation=config.hidden_act  , name="""layer.0"""			),
          TFRegNetConvLayer(
              a_  , stride=a_  , groups=a_  , activation=config.hidden_act  , name="""layer.1"""			),
          TFRegNetSELayer(a_  , reduced_channels=int(round(in_channels / 4			)			)  , name="""layer.2"""			),
          TFRegNetConvLayer(a_  , kernel_size=1  , activation=a_  , name="""layer.3"""			),
      ]
      UpperCAmelCase_    :     Optional[int]            =	ACTaFN[config.hidden_act]






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



      """simple docstring"""

      UpperCAmelCase_    :     Optional[Any]            =	hidden_state
      for layer_module in self.layers:
           UpperCAmelCase_    :     Dict            =	layer_module(a_			)
      UpperCAmelCase_    :     Optional[Any]            =	self.shortcut(a_			)
      hidden_state += residual
      UpperCAmelCase_    :     str            =	self.activation(a_			)
      return hidden_state



class 		UpperCAmelCase_						(tf.keras.layers.Layer    ):
 """simple docstring"""




 def __init__(			self							:			Optional[Any]  , a_							:			RegNetConfig  , a_							:			int  , a_							:			int  , a_							:			int = 2  , a_							:			int = 2  , **a_							:			Tuple			)->       List[str]:



      """simple docstring"""

      super().__init__(**a_			)

      UpperCAmelCase_    :     List[str]            =	TFRegNetXLayer if config.layer_type == """x""" else TFRegNetYLayer
      UpperCAmelCase_    :     List[Any]            =	[
          # downsampling is done in the first layer with stride of 2
          layer(a_  , a_  , a_  , stride=a_  , name="""layers.0"""			),
          *[layer(a_  , a_  , a_  , name=f'''layers.{i+1}'''			) for i in range(depth - 1			)],
      ]






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



      """simple docstring"""

      for layer_module in self.layers:
           UpperCAmelCase_    :     List[str]            =	layer_module(a_			)
      return hidden_state



class 		UpperCAmelCase_						(tf.keras.layers.Layer    ):
 """simple docstring"""




 def __init__(			self							:			List[str]  , a_							:			RegNetConfig  , **a_							:			str			)->       Optional[Any]:



      """simple docstring"""

      super().__init__(**a_			)
      UpperCAmelCase_    :     Union[str, Any]            =	[]
      # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
      self.stages.append(
          TFRegNetStage(
              a_  , config.embedding_size  , config.hidden_sizes[0]  , stride=2 if config.downsample_in_first_stage else 1  , depth=config.depths[0]  , name="""stages.0"""  , )			)
      UpperCAmelCase_    :     Optional[int]            =	zip(config.hidden_sizes  , config.hidden_sizes[1:]			)
      for i, ((in_channels, out_channels), depth) in enumerate(zip(a_  , config.depths[1:]			)			):
           self.stages.append(TFRegNetStage(a_  , a_  , a_  , depth=a_  , name=f'''stages.{i+1}'''			)			)






 def 	a   (			self							:			Dict  , a_							:			tf.Tensor  , a_							:			bool = False  , a_							:			bool = True			)->       TFBaseModelOutputWithNoAttention:



      """simple docstring"""

      UpperCAmelCase_    :     int            =	() if output_hidden_states else None

      for stage_module in self.stages:
           if output_hidden_states:
                UpperCAmelCase_    :     Optional[Any]            =	hidden_states + (hidden_state,)

           UpperCAmelCase_    :     Optional[Any]            =	stage_module(a_			)

      if output_hidden_states:
           UpperCAmelCase_    :     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 TFBaseModelOutputWithNoAttention(last_hidden_state=a_  , hidden_states=a_			)



@keras_serializable
class 		UpperCAmelCase_						(tf.keras.layers.Layer    ):
 """simple docstring"""




 UpperCamelCase_    :  Dict					 =     RegNetConfig




 def __init__(			self							:			Optional[Any]  , a_							:			Union[str, Any]  , **a_							:			List[str]			)->       List[str]:



      """simple docstring"""

      super().__init__(**a_			)
      UpperCAmelCase_    :     List[str]            =	config
      UpperCAmelCase_    :     List[str]            =	TFRegNetEmbeddings(a_  , name="""embedder"""			)
      UpperCAmelCase_    :     Optional[Any]            =	TFRegNetEncoder(a_  , name="""encoder"""			)
      UpperCAmelCase_    :     Tuple            =	tf.keras.layers.GlobalAveragePoolingaD(keepdims=a_  , name="""pooler"""			)






 @unpack_inputs
 def 	a   (			self							:			Optional[Any]  , a_							:			tf.Tensor  , a_							:			Optional[bool] = None  , a_							:			Optional[bool] = None  , a_							:			bool = False  , )->       TFBaseModelOutputWithPoolingAndNoAttention:



      """simple docstring"""

      UpperCAmelCase_    :     Optional[Any]            =	(
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      UpperCAmelCase_    :     int            =	return_dict if return_dict is not None else self.config.use_return_dict

      UpperCAmelCase_    :     str            =	self.embedder(a_  , training=a_			)

      UpperCAmelCase_    :     Dict            =	self.encoder(
          a_  , output_hidden_states=a_  , return_dict=a_  , training=a_			)

      UpperCAmelCase_    :     Tuple            =	encoder_outputs[0]
      UpperCAmelCase_    :     str            =	self.pooler(a_			)

      # Change to NCHW output format have uniformity in the modules
      UpperCAmelCase_    :     str            =	tf.transpose(a_  , perm=(0, 3, 1, 2)			)
      UpperCAmelCase_    :     Optional[int]            =	tf.transpose(a_  , perm=(0, 3, 1, 2)			)

      # Change the other hidden state outputs to NCHW as well
      if output_hidden_states:
           UpperCAmelCase_    :     Optional[int]            =	tuple([tf.transpose(a_  , perm=(0, 3, 1, 2)			) for h in encoder_outputs[1]]			)

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

      return TFBaseModelOutputWithPoolingAndNoAttention(
          last_hidden_state=a_  , pooler_output=a_  , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states  , )



class 		UpperCAmelCase_						(lowerCamelCase_    ):
 """simple docstring"""




 UpperCamelCase_    :  List[str]					 =     RegNetConfig
 UpperCamelCase_    :  Union[str, Any]					 =     """regnet"""
 UpperCamelCase_    :  Optional[int]					 =     """pixel_values"""




 @property
 def 	a   (			self							:			List[str]			)->       Dict:



      """simple docstring"""

      return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_24, 2_24)  , dtype=tf.floataa			)}

lowercase_         =       r"\n    Parameters:\n    This model is a Tensorflow\n    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n    regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n    behavior.\n        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n"

lowercase_         =       r"\n    Args:\n        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConveNextImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n"



@add_start_docstrings(
    """The bare RegNet model outputting raw features without any specific head on top."""	,			lowerCamelCase_	,			)
class 		UpperCAmelCase_						(lowerCamelCase_    ):
 """simple docstring"""




 def __init__(			self							:			Optional[int]  , a_							:			RegNetConfig  , *a_							:			Optional[int]  , **a_							:			List[Any]			)->       Any:



      """simple docstring"""

      super().__init__(a_  , *a_  , **a_			)
      UpperCAmelCase_    :     Optional[Any]            =	TFRegNetMainLayer(a_  , name="""regnet"""			)






 @unpack_inputs
 @add_start_docstrings_to_model_forward(a_			)
 @add_code_sample_docstrings(
     checkpoint=_CHECKPOINT_FOR_DOC  , output_type=a_  , config_class=_CONFIG_FOR_DOC  , modality="""vision"""  , expected_output=_EXPECTED_OUTPUT_SHAPE  , )
 def 	a   (			self							:			Union[str, Any]  , a_							:			tf.Tensor  , a_							:			Optional[bool] = None  , a_							:			Optional[bool] = None  , a_							:			int=False  , )->       Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]:



      """simple docstring"""

      UpperCAmelCase_    :     List[Any]            =	(
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      UpperCAmelCase_    :     List[Any]            =	return_dict if return_dict is not None else self.config.use_return_dict

      UpperCAmelCase_    :     Tuple            =	self.regnet(
          pixel_values=a_  , output_hidden_states=a_  , return_dict=a_  , training=a_  , )
      if not return_dict:
           return (outputs[0],) + outputs[1:]

      return TFBaseModelOutputWithPoolingAndNoAttention(
          last_hidden_state=outputs.last_hidden_state  , pooler_output=outputs.pooler_output  , hidden_states=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.
    """	,			lowerCamelCase_	,			)
class 		UpperCAmelCase_						(lowerCamelCase_	,			lowerCamelCase_    ):
 """simple docstring"""




 def __init__(			self							:			Optional[Any]  , a_							:			RegNetConfig  , *a_							:			Any  , **a_							:			Tuple			)->       Any:



      """simple docstring"""

      super().__init__(a_  , *a_  , **a_			)
      UpperCAmelCase_    :     Tuple            =	config.num_labels
      UpperCAmelCase_    :     List[str]            =	TFRegNetMainLayer(a_  , name="""regnet"""			)
      # classification head
      UpperCAmelCase_    :     List[str]            =	[
          tf.keras.layers.Flatten(),
          tf.keras.layers.Dense(config.num_labels  , name="""classifier.1"""			) if config.num_labels > 0 else tf.identity,
      ]






 @unpack_inputs
 @add_start_docstrings_to_model_forward(a_			)
 @add_code_sample_docstrings(
     checkpoint=_IMAGE_CLASS_CHECKPOINT  , output_type=a_  , config_class=_CONFIG_FOR_DOC  , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT  , )
 def 	a   (			self							:			Any  , a_							:			tf.Tensor = None  , a_							:			tf.Tensor = None  , a_							:			bool = None  , a_							:			bool = None  , a_							:			List[Any]=False  , )->       Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]:



      """simple docstring"""

      UpperCAmelCase_    :     int            =	(
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      UpperCAmelCase_    :     Union[str, Any]            =	return_dict if return_dict is not None else self.config.use_return_dict

      UpperCAmelCase_    :     Dict            =	self.regnet(
          a_  , output_hidden_states=a_  , return_dict=a_  , training=a_			)

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

      UpperCAmelCase_    :     Union[str, Any]            =	self.classifier[0](a_			)
      UpperCAmelCase_    :     str            =	self.classifier[1](a_			)

      UpperCAmelCase_    :     List[str]            =	None if labels is None else self.hf_compute_loss(labels=a_  , logits=a_			)

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

      return TFSequenceClassifierOutput(loss=a_  , logits=a_  , hidden_states=outputs.hidden_states			)



"""simple docstring"""







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 		UpperCAmelCase_						(unittest.TestCase    ):
 """simple docstring"""




 def 	a   (			self							:			str  , a_							:			int  , a_							:			int			)->       Optional[Any]:



      """simple docstring"""

      UpperCAmelCase_    :     List[Any]            =	jnp.ones((batch_size, length)			) / length
      return scores




 def 	a   (			self							:			str			)->       int:



      """simple docstring"""

      UpperCAmelCase_    :     List[Any]            =	None
      UpperCAmelCase_    :     int            =	20

      UpperCAmelCase_    :     Tuple            =	self._get_uniform_logits(batch_size=2  , length=a_			)

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

      # compute softmax
      UpperCAmelCase_    :     str            =	jax.nn.softmax(a_  , axis=-1			)

      UpperCAmelCase_    :     Union[str, Any]            =	FlaxTemperatureLogitsWarper(temperature=0.5			)
      UpperCAmelCase_    :     Any            =	FlaxTemperatureLogitsWarper(temperature=1.3			)

      UpperCAmelCase_    :     Optional[Any]            =	jax.nn.softmax(temp_dist_warper_sharper(a_  , scores.copy()  , cur_len=a_			)  , axis=-1			)
      UpperCAmelCase_    :     List[str]            =	jax.nn.softmax(temp_dist_warper_smoother(a_  , scores.copy()  , cur_len=a_			)  , 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 	a   (			self							:			Union[str, Any]			)->       Tuple:



      """simple docstring"""

      UpperCAmelCase_    :     Dict            =	None
      UpperCAmelCase_    :     str            =	10
      UpperCAmelCase_    :     List[Any]            =	2

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

      UpperCAmelCase_    :     List[Any]            =	FlaxTopKLogitsWarper(3			)

      UpperCAmelCase_    :     Tuple            =	top_k_warp(a_  , a_  , cur_len=a_			)

      # 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
      UpperCAmelCase_    :     Any            =	5
      UpperCAmelCase_    :     Optional[int]            =	FlaxTopKLogitsWarper(top_k=1  , filter_value=0.0  , min_tokens_to_keep=3			)

      UpperCAmelCase_    :     Any            =	np.broadcast_to(np.arange(a_			)[None, :]  , (batch_size, length)			).copy()
      UpperCAmelCase_    :     Tuple            =	top_k_warp_safety_check(a_  , a_  , cur_len=a_			)

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




 def 	a   (			self							:			Dict			)->       List[str]:



      """simple docstring"""

      UpperCAmelCase_    :     Tuple            =	None
      UpperCAmelCase_    :     List[str]            =	10
      UpperCAmelCase_    :     List[Any]            =	2

      # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper)
      UpperCAmelCase_    :     int            =	np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]]			)			)

      UpperCAmelCase_    :     str            =	FlaxTopPLogitsWarper(0.8			)
      UpperCAmelCase_    :     Union[str, Any]            =	np.exp(top_p_warp(a_  , a_  , cur_len=a_			)			)

      # dist should be filtered to keep min num values so that sum is >= top_p
      # exp (-inf) => 0
      UpperCAmelCase_    :     List[Any]            =	np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]]			)
      self.assertTrue(np.allclose(a_  , a_  , atol=1E-3			)			)

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

      # make ramp_logits more extreme
      UpperCAmelCase_    :     Tuple            =	ramp_logits[1] * 100.0

      # make sure at least 2 tokens are kept
      UpperCAmelCase_    :     Any            =	FlaxTopPLogitsWarper(0.9  , min_tokens_to_keep=2  , filter_value=0.0			)
      UpperCAmelCase_    :     Dict            =	top_p_warp(a_  , a_  , cur_len=a_			)

      # 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 	a   (			self							:			List[str]			)->       Any:



      """simple docstring"""

      UpperCAmelCase_    :     Dict            =	20
      UpperCAmelCase_    :     List[str]            =	4
      UpperCAmelCase_    :     Dict            =	0

      UpperCAmelCase_    :     Union[str, Any]            =	FlaxMinLengthLogitsProcessor(min_length=10  , eos_token_id=a_			)

      # check that min length is applied at length 5
      UpperCAmelCase_    :     Union[str, Any]            =	ids_tensor((batch_size, 20)  , vocab_size=20			)
      UpperCAmelCase_    :     str            =	5
      UpperCAmelCase_    :     Tuple            =	self._get_uniform_logits(a_  , a_			)
      UpperCAmelCase_    :     List[Any]            =	min_dist_processor(a_  , a_  , cur_len=a_			)
      self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist()  , 4 * [-float("""inf"""			)]			)

      # check that min length is not applied anymore at length 15
      UpperCAmelCase_    :     str            =	self._get_uniform_logits(a_  , a_			)
      UpperCAmelCase_    :     Optional[Any]            =	15
      UpperCAmelCase_    :     str            =	min_dist_processor(a_  , a_  , cur_len=a_			)
      self.assertFalse(jnp.isinf(a_			).any()			)




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



      """simple docstring"""

      UpperCAmelCase_    :     Optional[int]            =	20
      UpperCAmelCase_    :     int            =	4
      UpperCAmelCase_    :     str            =	0

      UpperCAmelCase_    :     Optional[int]            =	FlaxForcedBOSTokenLogitsProcessor(bos_token_id=a_			)

      # check that all scores are -inf except the bos_token_id score
      UpperCAmelCase_    :     List[str]            =	ids_tensor((batch_size, 1)  , vocab_size=20			)
      UpperCAmelCase_    :     List[Any]            =	1
      UpperCAmelCase_    :     int            =	self._get_uniform_logits(a_  , a_			)
      UpperCAmelCase_    :     int            =	logits_processor(a_  , a_  , cur_len=a_			)
      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
      UpperCAmelCase_    :     int            =	3
      UpperCAmelCase_    :     Tuple            =	self._get_uniform_logits(a_  , a_			)
      UpperCAmelCase_    :     Union[str, Any]            =	logits_processor(a_  , a_  , cur_len=a_			)
      self.assertFalse(jnp.isinf(a_			).any()			)




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



      """simple docstring"""

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

      UpperCAmelCase_    :     Optional[Any]            =	FlaxForcedEOSTokenLogitsProcessor(max_length=a_  , eos_token_id=a_			)

      # check that all scores are -inf except the eos_token_id when max_length is reached
      UpperCAmelCase_    :     str            =	ids_tensor((batch_size, 4)  , vocab_size=20			)
      UpperCAmelCase_    :     int            =	4
      UpperCAmelCase_    :     Optional[Any]            =	self._get_uniform_logits(a_  , a_			)
      UpperCAmelCase_    :     Union[str, Any]            =	logits_processor(a_  , a_  , cur_len=a_			)
      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
      UpperCAmelCase_    :     Any            =	3
      UpperCAmelCase_    :     Tuple            =	self._get_uniform_logits(a_  , a_			)
      UpperCAmelCase_    :     int            =	logits_processor(a_  , a_  , cur_len=a_			)
      self.assertFalse(jnp.isinf(a_			).any()			)




 def 	a   (			self							:			int			)->       List[Any]:



      """simple docstring"""

      UpperCAmelCase_    :     Dict            =	4
      UpperCAmelCase_    :     List[Any]            =	10
      UpperCAmelCase_    :     Tuple            =	15
      UpperCAmelCase_    :     Optional[Any]            =	2
      UpperCAmelCase_    :     List[Any]            =	1
      UpperCAmelCase_    :     Dict            =	15

      # dummy input_ids and scores
      UpperCAmelCase_    :     Dict            =	ids_tensor((batch_size, sequence_length)  , a_			)
      UpperCAmelCase_    :     Tuple            =	input_ids.copy()

      UpperCAmelCase_    :     int            =	self._get_uniform_logits(a_  , a_			)
      UpperCAmelCase_    :     Optional[Any]            =	scores.copy()

      # instantiate all dist processors
      UpperCAmelCase_    :     Tuple            =	FlaxTemperatureLogitsWarper(temperature=0.5			)
      UpperCAmelCase_    :     str            =	FlaxTopKLogitsWarper(3			)
      UpperCAmelCase_    :     Tuple            =	FlaxTopPLogitsWarper(0.8			)

      # instantiate all logits processors
      UpperCAmelCase_    :     Tuple            =	FlaxMinLengthLogitsProcessor(min_length=10  , eos_token_id=a_			)
      UpperCAmelCase_    :     Optional[Any]            =	FlaxForcedBOSTokenLogitsProcessor(bos_token_id=a_			)
      UpperCAmelCase_    :     Tuple            =	FlaxForcedEOSTokenLogitsProcessor(max_length=a_  , eos_token_id=a_			)

      UpperCAmelCase_    :     Tuple            =	10

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

      # with processor list
      UpperCAmelCase_    :     Tuple            =	FlaxLogitsProcessorList(
          [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]			)
      UpperCAmelCase_    :     Union[str, Any]            =	processor(a_  , a_  , cur_len=a_			)

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

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






 def 	a   (			self							:			Optional[int]			)->       Any:



      """simple docstring"""

      UpperCAmelCase_    :     Any            =	4
      UpperCAmelCase_    :     List[Any]            =	10
      UpperCAmelCase_    :     Optional[int]            =	15
      UpperCAmelCase_    :     Optional[Any]            =	2
      UpperCAmelCase_    :     Dict            =	1
      UpperCAmelCase_    :     int            =	15

      # dummy input_ids and scores
      UpperCAmelCase_    :     List[str]            =	ids_tensor((batch_size, sequence_length)  , a_			)
      UpperCAmelCase_    :     Dict            =	input_ids.copy()

      UpperCAmelCase_    :     Dict            =	self._get_uniform_logits(a_  , a_			)
      UpperCAmelCase_    :     Tuple            =	scores.copy()

      # instantiate all dist processors
      UpperCAmelCase_    :     Dict            =	FlaxTemperatureLogitsWarper(temperature=0.5			)
      UpperCAmelCase_    :     int            =	FlaxTopKLogitsWarper(3			)
      UpperCAmelCase_    :     int            =	FlaxTopPLogitsWarper(0.8			)

      # instantiate all logits processors
      UpperCAmelCase_    :     str            =	FlaxMinLengthLogitsProcessor(min_length=10  , eos_token_id=a_			)
      UpperCAmelCase_    :     Dict            =	FlaxForcedBOSTokenLogitsProcessor(bos_token_id=a_			)
      UpperCAmelCase_    :     Union[str, Any]            =	FlaxForcedEOSTokenLogitsProcessor(max_length=a_  , eos_token_id=a_			)

      UpperCAmelCase_    :     Dict            =	10

      # no processor list
      def run_no_processor_list(a_							:			Any  , a_							:			List[str]  , a_							:			Tuple			):
           UpperCAmelCase_    :     List[Any]            =	temp_dist_warp(a_  , a_  , cur_len=a_			)
           UpperCAmelCase_    :     Tuple            =	top_k_warp(a_  , a_  , cur_len=a_			)
           UpperCAmelCase_    :     int            =	top_p_warp(a_  , a_  , cur_len=a_			)
           UpperCAmelCase_    :     Tuple            =	min_dist_proc(a_  , a_  , cur_len=a_			)
           UpperCAmelCase_    :     Optional[int]            =	bos_dist_proc(a_  , a_  , cur_len=a_			)
           UpperCAmelCase_    :     List[Any]            =	eos_dist_proc(a_  , a_  , cur_len=a_			)
           return scores

      # with processor list
      def run_processor_list(a_							:			List[str]  , a_							:			Optional[int]  , a_							:			Optional[int]			):
           UpperCAmelCase_    :     List[str]            =	FlaxLogitsProcessorList(
               [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]			)
           UpperCAmelCase_    :     Optional[Any]            =	processor(a_  , a_  , cur_len=a_			)
           return scores

      UpperCAmelCase_    :     str            =	jax.jit(a_			)
      UpperCAmelCase_    :     Tuple            =	jax.jit(a_			)

      UpperCAmelCase_    :     Optional[int]            =	jitted_run_no_processor_list(a_  , a_  , a_			)
      UpperCAmelCase_    :     List[Any]            =	jitted_run_processor_list(a_  , a_  , a_			)

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

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

