'''simple docstring'''






from typing import List, Optional, Tuple, Union

import torch
from torch import nn
from torch.nn import CrossEntropyLoss

from ... import AutoBackbone
from ...modeling_outputs import SemanticSegmenterOutput
from ...modeling_utils import PreTrainedModel
from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings
from ...utils.backbone_utils import BackboneMixin
from .configuration_upernet import UperNetConfig


SCREAMING_SNAKE_CASE_	   =			[
    'openmmlab/upernet-convnext-tiny',
    # See all UperNet models at https://huggingface.co/models?filter=upernet
]

# General docstring
SCREAMING_SNAKE_CASE_	   =			'UperNetConfig'




class    a (       nn.Module				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_   ,    snake_case_ = 0   ,    snake_case_ = False   ,    snake_case_ = 1   ,    ):

    '''simple docstring'''







    super().__init__()
    __UpperCAmelCase:					str		= nn.Convad(
        in_channels=snake_case_   ,    out_channels=snake_case_   ,    kernel_size=snake_case_   ,    padding=snake_case_   ,    bias=snake_case_   ,    dilation=snake_case_   ,    )
    __UpperCAmelCase:					int		= nn.BatchNormad(snake_case_					)
    __UpperCAmelCase:					Optional[int]		= nn.ReLU()







  def    lowercase_  (							self   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					str		= self.conv(snake_case_					)
    __UpperCAmelCase:					Any		= self.batch_norm(snake_case_					)
    __UpperCAmelCase:					Tuple		= self.activation(snake_case_					)

    return output




class    a (       nn.Module				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    super().__init__()
    __UpperCAmelCase:					List[str]		= [
        nn.AdaptiveAvgPoolad(snake_case_					),
        UperNetConvModule(snake_case_   ,    snake_case_   ,    kernel_size=1					),
    ]
    for i, layer in enumerate(self.layers					):
      self.add_module(str(snake_case_					)   ,    snake_case_					)







  def    lowercase_  (							self   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					List[Any]		= input
    for layer in self.layers:
      __UpperCAmelCase:					Optional[int]		= layer(snake_case_					)
    return hidden_state




class    a (       nn.Module				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    super().__init__()
    __UpperCAmelCase:					Dict		= pool_scales
    __UpperCAmelCase:					Dict		= align_corners
    __UpperCAmelCase:					Tuple		= in_channels
    __UpperCAmelCase:					Union[str, Any]		= channels
    __UpperCAmelCase:					str		= []
    for i, pool_scale in enumerate(snake_case_					):
      __UpperCAmelCase:					Union[str, Any]		= UperNetPyramidPoolingBlock(pool_scale=snake_case_   ,    in_channels=snake_case_   ,    channels=snake_case_					)
      self.blocks.append(snake_case_					)
      self.add_module(str(snake_case_					)   ,    snake_case_					)







  def    lowercase_  (							self   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					List[Any]		= []
    for ppm in self.blocks:
      __UpperCAmelCase:					Optional[Any]		= ppm(snake_case_					)
      __UpperCAmelCase:					Any		= nn.functional.interpolate(
          snake_case_   ,    size=x.size()[2:]   ,    mode="""bilinear"""   ,    align_corners=self.align_corners					)
      ppm_outs.append(snake_case_					)
    return ppm_outs




class    a (       nn.Module				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    super().__init__()

    __UpperCAmelCase:					Tuple		= config
    __UpperCAmelCase:					Optional[Any]		= config.pool_scales  # e.g. (1, 2, 3, 6)
    __UpperCAmelCase:					int		= in_channels
    __UpperCAmelCase:					Any		= config.hidden_size
    __UpperCAmelCase:					Dict		= False
    __UpperCAmelCase:					List[Any]		= nn.Convad(self.channels   ,    config.num_labels   ,    kernel_size=1					)

    # PSP Module
    __UpperCAmelCase:					str		= UperNetPyramidPoolingModule(
        self.pool_scales   ,    self.in_channels[-1]   ,    self.channels   ,    align_corners=self.align_corners   ,    )
    __UpperCAmelCase:					Optional[Any]		= UperNetConvModule(
        self.in_channels[-1] + len(self.pool_scales					) * self.channels   ,    self.channels   ,    kernel_size=3   ,    padding=1   ,    )
    # FPN Module
    __UpperCAmelCase:					Tuple		= nn.ModuleList()
    __UpperCAmelCase:					Tuple		= nn.ModuleList()
    for in_channels in self.in_channels[:-1]:  # skip the top layer
      __UpperCAmelCase:					Any		= UperNetConvModule(snake_case_   ,    self.channels   ,    kernel_size=1					)
      __UpperCAmelCase:					List[Any]		= UperNetConvModule(self.channels   ,    self.channels   ,    kernel_size=3   ,    padding=1					)
      self.lateral_convs.append(snake_case_					)
      self.fpn_convs.append(snake_case_					)

    __UpperCAmelCase:					int		= UperNetConvModule(
        len(self.in_channels					) * self.channels   ,    self.channels   ,    kernel_size=3   ,    padding=1   ,    )







  def    lowercase_  (							self					):

    '''simple docstring'''







    self.apply(self._init_weights					)







  def    lowercase_  (							self   ,    snake_case_					):

    '''simple docstring'''







    if isinstance(snake_case_   ,    nn.Convad					):
      module.weight.data.normal_(mean=0.0   ,    std=self.config.initializer_range					)
      if module.bias is not None:
        module.bias.data.zero_()







  def    lowercase_  (							self   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[Any]		= inputs[-1]
    __UpperCAmelCase:					Union[str, Any]		= [x]
    psp_outs.extend(self.psp_modules(snake_case_					)					)
    __UpperCAmelCase:					List[Any]		= torch.cat(snake_case_   ,    dim=1					)
    __UpperCAmelCase:					Union[str, Any]		= self.bottleneck(snake_case_					)

    return output







  def    lowercase_  (							self   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[Any]		= [lateral_conv(encoder_hidden_states[i]					) for i, lateral_conv in enumerate(self.lateral_convs					)]

    laterals.append(self.psp_forward(snake_case_					)					)

    # build top-down path
    __UpperCAmelCase:					List[str]		= len(snake_case_					)
    for i in range(used_backbone_levels - 1   ,    0   ,    -1					):
      __UpperCAmelCase:					Optional[Any]		= laterals[i - 1].shape[2:]
      __UpperCAmelCase:					Tuple		= laterals[i - 1] + nn.functional.interpolate(
          laterals[i]   ,    size=snake_case_   ,    mode="""bilinear"""   ,    align_corners=self.align_corners					)

    # build outputs
    __UpperCAmelCase:					Optional[Any]		= [self.fpn_convs[i](laterals[i]					) for i in range(used_backbone_levels - 1					)]
    # append psp feature
    fpn_outs.append(laterals[-1]					)

    for i in range(used_backbone_levels - 1   ,    0   ,    -1					):
      __UpperCAmelCase:					str		= nn.functional.interpolate(
          fpn_outs[i]   ,    size=fpn_outs[0].shape[2:]   ,    mode="""bilinear"""   ,    align_corners=self.align_corners					)
    __UpperCAmelCase:					List[str]		= torch.cat(snake_case_   ,    dim=1					)
    __UpperCAmelCase:					Optional[Any]		= self.fpn_bottleneck(snake_case_					)
    __UpperCAmelCase:					Optional[Any]		= self.classifier(snake_case_					)

    return output




class    a (       nn.Module				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_ = 2   ,    snake_case_ = 3   ,    snake_case_ = 1					):

    '''simple docstring'''







    super().__init__()

    __UpperCAmelCase:					int		= config
    __UpperCAmelCase:					Union[str, Any]		= config.auxiliary_in_channels
    __UpperCAmelCase:					Dict		= config.auxiliary_channels
    __UpperCAmelCase:					Tuple		= config.auxiliary_num_convs
    __UpperCAmelCase:					Union[str, Any]		= config.auxiliary_concat_input
    __UpperCAmelCase:					List[str]		= in_index

    __UpperCAmelCase:					Union[str, Any]		= (kernel_size // 2) * dilation
    __UpperCAmelCase:					str		= []
    convs.append(
        UperNetConvModule(
            self.in_channels   ,    self.channels   ,    kernel_size=snake_case_   ,    padding=snake_case_   ,    dilation=snake_case_					)					)
    for i in range(self.num_convs - 1					):
      convs.append(
          UperNetConvModule(
              self.channels   ,    self.channels   ,    kernel_size=snake_case_   ,    padding=snake_case_   ,    dilation=snake_case_					)					)
    if self.num_convs == 0:
      __UpperCAmelCase:					str		= nn.Identity()
    else:
      __UpperCAmelCase:					Union[str, Any]		= nn.Sequential(*snake_case_					)
    if self.concat_input:
      __UpperCAmelCase:					List[Any]		= UperNetConvModule(
          self.in_channels + self.channels   ,    self.channels   ,    kernel_size=snake_case_   ,    padding=kernel_size // 2					)

    __UpperCAmelCase:					List[Any]		= nn.Convad(self.channels   ,    config.num_labels   ,    kernel_size=1					)







  def    lowercase_  (							self					):

    '''simple docstring'''







    self.apply(self._init_weights					)







  def    lowercase_  (							self   ,    snake_case_					):

    '''simple docstring'''







    if isinstance(snake_case_   ,    nn.Convad					):
      module.weight.data.normal_(mean=0.0   ,    std=self.config.initializer_range					)
      if module.bias is not None:
        module.bias.data.zero_()







  def    lowercase_  (							self   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Union[str, Any]		= encoder_hidden_states[self.in_index]
    __UpperCAmelCase:					List[str]		= self.convs(snake_case_					)
    if self.concat_input:
      __UpperCAmelCase:					int		= self.conv_cat(torch.cat([hidden_states, output]   ,    dim=1					)					)
    __UpperCAmelCase:					Optional[Any]		= self.classifier(snake_case_					)
    return output




class    a (       _UpperCamelCase				):



  """simple docstring"""
  __lowerCAmelCase				      =					UperNetConfig
  __lowerCAmelCase				      =					"""pixel_values"""
  __lowerCAmelCase				      =					True







  def    lowercase_  (							self   ,    snake_case_					):

    '''simple docstring'''







    if isinstance(snake_case_   ,    snake_case_					):
      module.backbone.init_weights()
      module.decode_head.init_weights()
      module.auxiliary_head.init_weights()







  def    lowercase_  (							self					):

    '''simple docstring'''







    self.backbone.init_weights()
    self.decode_head.init_weights()
    self.auxiliary_head.init_weights()







  def    lowercase_  (							self   ,    snake_case_   ,    snake_case_=False					):

    '''simple docstring'''







    if isinstance(snake_case_   ,    snake_case_					):
      __UpperCAmelCase:					Any		= value



SCREAMING_SNAKE_CASE_	   =			R'\n    Parameters:\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n        config ([`UperNetConfig`]): 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 [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

SCREAMING_SNAKE_CASE_	   =			R'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n            [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n        output_attentions (`bool`, *optional*):\n            Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n            `attentions` under returned tensors for more detail.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n            returned tensors for 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(
    """UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes."""						,  _UpperCamelCase						,  )
class    a (       _UpperCamelCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_					):

    '''simple docstring'''







    super().__init__(snake_case_					)

    __UpperCAmelCase:					Optional[Any]		= AutoBackbone.from_config(config.backbone_config					)

    # Semantic segmentation head(s)
    __UpperCAmelCase:					List[str]		= UperNetHead(snake_case_   ,    in_channels=self.backbone.channels					)
    __UpperCAmelCase:					List[str]		= UperNetFCNHead(snake_case_					) if config.use_auxiliary_head else None

    # Initialize weights and apply final processing
    self.post_init()







  @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format("""batch_size, sequence_length"""					)					)
  @replace_return_docstrings(output_type=snake_case_   ,    config_class=_CONFIG_FOR_DOC					)
  def    lowercase_  (							self   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    ):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[Any]		= return_dict if return_dict is not None else self.config.use_return_dict
    __UpperCAmelCase:					List[Any]		= (
        output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    )
    __UpperCAmelCase:					str		= output_attentions if output_attentions is not None else self.config.output_attentions

    __UpperCAmelCase:					Any		= self.backbone.forward_with_filtered_kwargs(
        snake_case_   ,    output_hidden_states=snake_case_   ,    output_attentions=snake_case_					)
    __UpperCAmelCase:					List[str]		= outputs.feature_maps

    __UpperCAmelCase:					Optional[int]		= self.decode_head(snake_case_					)
    __UpperCAmelCase:					List[Any]		= nn.functional.interpolate(snake_case_   ,    size=pixel_values.shape[2:]   ,    mode="""bilinear"""   ,    align_corners=snake_case_					)

    __UpperCAmelCase:					str		= None
    if self.auxiliary_head is not None:
      __UpperCAmelCase:					Optional[int]		= self.auxiliary_head(snake_case_					)
      __UpperCAmelCase:					List[Any]		= nn.functional.interpolate(
          snake_case_   ,    size=pixel_values.shape[2:]   ,    mode="""bilinear"""   ,    align_corners=snake_case_					)

    __UpperCAmelCase:					int		= None
    if labels is not None:
      if self.config.num_labels == 1:
        raise ValueError("""The number of labels should be greater than one"""					)
      else:
        # compute weighted loss
        __UpperCAmelCase:					str		= CrossEntropyLoss(ignore_index=self.config.loss_ignore_index					)
        __UpperCAmelCase:					Union[str, Any]		= loss_fct(snake_case_   ,    snake_case_					)
        __UpperCAmelCase:					Any		= loss_fct(snake_case_   ,    snake_case_					)
        __UpperCAmelCase:					str		= main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

    if not return_dict:
      if output_hidden_states:
        __UpperCAmelCase:					List[Any]		= (logits,) + outputs[1:]
      else:
        __UpperCAmelCase:					Optional[Any]		= (logits,) + outputs[2:]
      return ((loss,) + output) if loss is not None else output

    return SemanticSegmenterOutput(
        loss=snake_case_   ,    logits=snake_case_   ,    hidden_states=outputs.hidden_states   ,    attentions=outputs.attentions   ,    )





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


SCREAMING_SNAKE_CASE_	   =			get_logger(__name__)


SCREAMING_SNAKE_CASE_	   =			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(snake_case_					)
  def __call__(							self   ,    snake_case_   ,    snake_case_					):

    '''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(snake_case_					)
  def __call__(							self   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







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




class    a (       __lowerCAmelCase				):



  """simple docstring"""







  @add_start_docstrings(snake_case_					)
  def __call__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_   ,    **snake_case_					):

    '''simple docstring'''







    for processor in self:
      __UpperCAmelCase:					str		= inspect.signature(processor.__call__					).parameters
      if len(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.'''					)
        __UpperCAmelCase:					List[Any]		= processor(snake_case_   ,    snake_case_   ,    snake_case_   ,    **snake_case_					)
      else:
        __UpperCAmelCase:					Optional[Any]		= processor(snake_case_   ,    snake_case_   ,    snake_case_					)
    return scores




class    a (       __lowerCAmelCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_					):

    '''simple docstring'''







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

    __UpperCAmelCase:					Any		= temperature







  def __call__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Tuple		= scores / self.temperature
    return scores




class    a (       __lowerCAmelCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_ = -float("""Inf"""					)   ,    snake_case_ = 1					):

    '''simple docstring'''







    if not isinstance(snake_case_   ,    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(snake_case_   ,    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}'''					)

    __UpperCAmelCase:					int		= top_p
    __UpperCAmelCase:					str		= filter_value
    __UpperCAmelCase:					List[Any]		= min_tokens_to_keep







  def __call__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase, __UpperCAmelCase:					List[Any]		= lax.top_k(snake_case_   ,    scores.shape[-1]					)

    __UpperCAmelCase:					Tuple		= jnp.full_like(snake_case_   ,    self.filter_value					)
    __UpperCAmelCase:					Optional[int]		= jax.nn.softmax(snake_case_   ,    axis=-1					).cumsum(axis=-1					)
    __UpperCAmelCase:					List[str]		= cumulative_probs < self.top_p

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

    # min tokens to keep
    __UpperCAmelCase:					str		= score_mask.at[:, : self.min_tokens_to_keep].set(snake_case_					)

    __UpperCAmelCase:					List[Any]		= jnp.where(snake_case_   ,    snake_case_   ,    snake_case_					)
    __UpperCAmelCase:					Optional[Any]		= jax.lax.sort_key_val(snake_case_   ,    snake_case_					)[-1]

    return next_scores




class    a (       __lowerCAmelCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_ = -float("""Inf"""					)   ,    snake_case_ = 1					):

    '''simple docstring'''







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

    __UpperCAmelCase:					Optional[Any]		= max(snake_case_   ,    snake_case_					)
    __UpperCAmelCase:					List[str]		= filter_value







  def __call__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase, __UpperCAmelCase:					List[Any]		= scores.shape
    __UpperCAmelCase:					List[Any]		= jnp.full(batch_size * vocab_size   ,    self.filter_value					)

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

    __UpperCAmelCase:					List[Any]		= next_scores_flat.at[topk_indices_flat].set(snake_case_					)
    __UpperCAmelCase:					Any		= next_scores_flat.reshape(snake_case_   ,    snake_case_					)
    return next_scores




class    a (       __lowerCAmelCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					List[Any]		= bos_token_id







  def __call__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					List[Any]		= jnp.full(scores.shape   ,    -float("""inf"""					)					)

    __UpperCAmelCase:					Optional[Any]		= 1 - jnp.bool_(cur_len - 1					)

    __UpperCAmelCase:					Any		= jnp.where(snake_case_   ,    new_scores.at[:, self.bos_token_id].set(0					)   ,    snake_case_					)

    return scores




class    a (       __lowerCAmelCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Union[str, Any]		= max_length
    __UpperCAmelCase:					List[str]		= eos_token_id







  def __call__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







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

    __UpperCAmelCase:					List[str]		= 1 - jnp.bool_(cur_len - self.max_length + 1					)

    __UpperCAmelCase:					Any		= jnp.where(snake_case_   ,    new_scores.at[:, self.eos_token_id].set(0					)   ,    snake_case_					)

    return scores




class    a (       __lowerCAmelCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







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

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

    __UpperCAmelCase:					List[Any]		= min_length
    __UpperCAmelCase:					Optional[int]		= eos_token_id







  def __call__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Union[str, Any]		= 1 - jnp.clip(cur_len - self.min_length   ,    0   ,    1					)

    __UpperCAmelCase:					int		= jnp.where(snake_case_   ,    scores.at[:, self.eos_token_id].set(-float("""inf"""					)					)   ,    snake_case_					)

    return scores




class    a (       __lowerCAmelCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Any		= list(snake_case_					)
    __UpperCAmelCase:					Optional[Any]		= begin_index







  def __call__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Union[str, Any]		= 1 - jnp.bool_(cur_len - self.begin_index					)

    __UpperCAmelCase:					str		= jnp.where(snake_case_   ,    scores.at[:, self.begin_suppress_tokens].set(-float("""inf"""					)					)   ,    snake_case_					)

    return scores




class    a (       __lowerCAmelCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[Any]		= list(snake_case_					)







  def __call__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					int		= scores.at[..., self.suppress_tokens].set(-float("""inf"""					)					)

    return scores




class    a (       __lowerCAmelCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[int]		= dict(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.
    __UpperCAmelCase:					Optional[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:
        __UpperCAmelCase:					Union[str, Any]		= force_token_array.at[index].set(snake_case_					)
    __UpperCAmelCase:					Union[str, Any]		= jnp.intaa(snake_case_					)







  def __call__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    def _force_token(snake_case_					):
      __UpperCAmelCase:					List[Any]		= scores.shape[0]
      __UpperCAmelCase:					int		= self.force_token_array[generation_idx]

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

    __UpperCAmelCase:					Optional[int]		= 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(snake_case_					)   ,    lambda: scores   ,    )   ,    )
    return scores




class    a (       __lowerCAmelCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					int		= generate_config.eos_token_id
    __UpperCAmelCase:					Union[str, Any]		= generate_config.no_timestamps_token_id
    __UpperCAmelCase:					Tuple		= generate_config.no_timestamps_token_id + 1

    __UpperCAmelCase:					Any		= decoder_input_length + 1

    if generate_config.is_multilingual:
      # room for language token and task token
      self.begin_index += 2
    if hasattr(snake_case_   ,    """max_initial_timestamp_index"""					):
      __UpperCAmelCase:					Optional[int]		= generate_config.max_initial_timestamp_index
    else:
      __UpperCAmelCase:					List[str]		= model_config.vocab_size
    if self.max_initial_timestamp_index is None:
      __UpperCAmelCase:					Any		= model_config.vocab_size







  def __call__(							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







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

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

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

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

    __UpperCAmelCase:					Dict		= jax.vmap(snake_case_					)(snake_case_   ,    snake_case_					)

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

    __UpperCAmelCase:					List[str]		= self.timestamp_begin + self.max_initial_timestamp_index

    __UpperCAmelCase:					List[str]		= jnp.where(
        snake_case_   ,    scores.at[:, last_allowed + 1 :].set(-float("""inf"""					)					)   ,    snake_case_   ,    )

    # if sum of probability over timestamps is above any other token, sample timestamp
    __UpperCAmelCase:					Union[str, Any]		= jax.nn.log_softmax(snake_case_   ,    axis=-1					)

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

    __UpperCAmelCase:					Tuple		= jax.vmap(snake_case_					)(snake_case_   ,    snake_case_					)

    return scores