






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


A_		:		Any		     =			get_logger(__name__)


A_		:		Dict		     =			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     _lowerCAmelCase:





       """simple docstring"""







       @add_start_docstrings(_lowerCamelCase							)
       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''							)







class     _lowerCAmelCase:





       """simple docstring"""







       @add_start_docstrings(_lowerCamelCase							)
       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''							)







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       @add_start_docstrings(_lowerCamelCase							)
       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      **_lowerCamelCase							):
         for processor in self:
           UpperCamelCase_: int   				=    inspect.signature(processor.__call__							).parameters
           if len(_lowerCamelCase							) > 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_: Any   				=    processor(_lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      **_lowerCamelCase							)
           else:
             UpperCamelCase_: Optional[Any]   				=    processor(_lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							)
         return scores







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase							):
         if not isinstance(_lowerCamelCase  ,      _lowerCamelCase							) or not (temperature > 0):
           raise ValueError(f'''`temperature` has to be a strictly positive float, but is {temperature}'''							)

         UpperCamelCase_: int   				=    temperature





       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Dict   				=    scores / self.temperature
         return scores







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase  ,      _lowerCamelCase = -float('Inf'							)  ,      _lowerCamelCase = 1							):
         if not isinstance(_lowerCamelCase  ,      _lowerCamelCase							) 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(_lowerCamelCase  ,      _lowerCamelCase							) 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_: List[str]   				=    top_p
         UpperCamelCase_: List[Any]   				=    filter_value
         UpperCamelCase_: Optional[Any]   				=    min_tokens_to_keep





       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_				,UpperCamelCase_: Optional[int]   				=    lax.top_k(_lowerCamelCase  ,      scores.shape[-1]							)

         UpperCamelCase_: Any   				=    jnp.full_like(_lowerCamelCase  ,      self.filter_value							)
         UpperCamelCase_: Optional[int]   				=    jax.nn.softmax(_lowerCamelCase  ,      axis=-1							).cumsum(axis=-1							)
         UpperCamelCase_: Union[str, Any]   				=    cumulative_probs < self.top_p

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

         # min tokens to keep
         UpperCamelCase_: List[str]   				=    score_mask.at[:, : self.min_tokens_to_keep].set(_lowerCamelCase							)

         UpperCamelCase_: Union[str, Any]   				=    jnp.where(_lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							)
         UpperCamelCase_: Optional[Any]   				=    jax.lax.sort_key_val(_lowerCamelCase  ,      _lowerCamelCase							)[-1]

         return next_scores







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase  ,      _lowerCamelCase = -float('Inf'							)  ,      _lowerCamelCase = 1							):
         if not isinstance(_lowerCamelCase  ,      _lowerCamelCase							) or top_k <= 0:
           raise ValueError(f'''`top_k` has to be a strictly positive integer, but is {top_k}'''							)

         UpperCamelCase_: List[str]   				=    max(_lowerCamelCase  ,      _lowerCamelCase							)
         UpperCamelCase_: int   				=    filter_value





       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_				,UpperCamelCase_: int   				=    scores.shape
         UpperCamelCase_: Optional[int]   				=    jnp.full(batch_size * vocab_size  ,      self.filter_value							)

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

         UpperCamelCase_: str   				=    next_scores_flat.at[topk_indices_flat].set(_lowerCamelCase							)
         UpperCamelCase_: List[str]   				=    next_scores_flat.reshape(_lowerCamelCase  ,      _lowerCamelCase							)
         return next_scores







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase							):
         UpperCamelCase_: Dict   				=    bos_token_id





       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Union[str, Any]   				=    jnp.full(scores.shape  ,      -float('inf'							)							)

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

         UpperCamelCase_: List[str]   				=    jnp.where(_lowerCamelCase  ,      new_scores.at[:, self.bos_token_id].set(0							)  ,      _lowerCamelCase							)

         return scores







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Tuple   				=    max_length
         UpperCamelCase_: Union[str, Any]   				=    eos_token_id





       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Any   				=    jnp.full(scores.shape  ,      -float('inf'							)							)

         UpperCamelCase_: Dict   				=    1 - jnp.bool_(cur_len - self.max_length + 1							)

         UpperCamelCase_: Optional[Any]   				=    jnp.where(_lowerCamelCase  ,      new_scores.at[:, self.eos_token_id].set(0							)  ,      _lowerCamelCase							)

         return scores







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         if not isinstance(_lowerCamelCase  ,      _lowerCamelCase							) or min_length < 0:
           raise ValueError(f'''`min_length` has to be a positive integer, but is {min_length}'''							)

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

         UpperCamelCase_: Any   				=    min_length
         UpperCamelCase_: List[Any]   				=    eos_token_id





       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         # create boolean flag to decide if min length penalty should be applied
         UpperCamelCase_: Dict   				=    1 - jnp.clip(cur_len - self.min_length  ,      0  ,      1							)

         UpperCamelCase_: Union[str, Any]   				=    jnp.where(_lowerCamelCase  ,      scores.at[:, self.eos_token_id].set(-float('inf'							)							)  ,      _lowerCamelCase							)

         return scores







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Union[str, Any]   				=    list(_lowerCamelCase							)
         UpperCamelCase_: Dict   				=    begin_index





       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Dict   				=    1 - jnp.bool_(cur_len - self.begin_index							)

         UpperCamelCase_: Dict   				=    jnp.where(_lowerCamelCase  ,      scores.at[:, self.begin_suppress_tokens].set(-float('inf'							)							)  ,      _lowerCamelCase							)

         return scores







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase							):
         UpperCamelCase_: Optional[int]   				=    list(_lowerCamelCase							)





       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Union[str, Any]   				=    scores.at[..., self.suppress_tokens].set(-float('inf'							)							)

         return scores







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase							):
         UpperCamelCase_: Union[str, Any]   				=    dict(_lowerCamelCase							)
         # 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_: Union[str, 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_: Dict   				=    force_token_array.at[index].set(_lowerCamelCase							)
         UpperCamelCase_: Optional[Any]   				=    jnp.intaa(_lowerCamelCase							)





       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         def _force_token(_lowerCamelCase							):
           UpperCamelCase_: List[str]   				=    scores.shape[0]
           UpperCamelCase_: Tuple   				=    self.force_token_array[generation_idx]

           UpperCamelCase_: Optional[Any]   				=    jnp.ones_like(_lowerCamelCase  ,      dtype=scores.dtype							) * -float('inf'							)
           UpperCamelCase_: Optional[Any]   				=    jnp.zeros((batch_size, 1)  ,      dtype=scores.dtype							)
           UpperCamelCase_: List[Any]   				=    lax.dynamic_update_slice(_lowerCamelCase  ,      _lowerCamelCase  ,      (0, current_token)							)
           return new_scores

         UpperCamelCase_: List[str]   				=    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(_lowerCamelCase							)  ,      lambda: scores  ,      )  ,      )
         return scores







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Union[str, Any]   				=    generate_config.eos_token_id
         UpperCamelCase_: List[str]   				=    generate_config.no_timestamps_token_id
         UpperCamelCase_: Any   				=    generate_config.no_timestamps_token_id + 1

         UpperCamelCase_: int   				=    decoder_input_length + 1

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





       def __call__( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         # suppress <|notimestamps|> which is handled by without_timestamps
         UpperCamelCase_: Optional[int]   				=    scores.at[:, self.no_timestamps_token_id].set(-float('inf'							)							)

         def handle_pairs(_lowerCamelCase  ,      _lowerCamelCase							):
           UpperCamelCase_: List[Any]   				=    jnp.where((cur_len - self.begin_index) >= 1  ,      _lowerCamelCase  ,      _lowerCamelCase							)
           UpperCamelCase_: Any   				=    jnp.where(
               input_ids_k[cur_len - 1] >= self.timestamp_begin  ,      True and last_was_timestamp  ,      _lowerCamelCase  ,      )

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

           return jnp.where(
               _lowerCamelCase  ,      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'							)							)  ,      )  ,      _lowerCamelCase  ,      )

         UpperCamelCase_: Dict   				=    jax.vmap(_lowerCamelCase							)(_lowerCamelCase  ,      _lowerCamelCase							)

         UpperCamelCase_: List[Any]   				=    jnp.where(cur_len == self.begin_index  ,      _lowerCamelCase  ,      _lowerCamelCase							)
         UpperCamelCase_: Tuple   				=    jnp.where(
             self.max_initial_timestamp_index is not None  ,      True and apply_max_initial_timestamp  ,      _lowerCamelCase  ,      )

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

         UpperCamelCase_: Union[str, Any]   				=    jnp.where(
             _lowerCamelCase  ,      scores.at[:, last_allowed + 1 :].set(-float('inf'							)							)  ,      _lowerCamelCase  ,      )

         # if sum of probability over timestamps is above any other token, sample timestamp
         UpperCamelCase_: Dict   				=    jax.nn.log_softmax(_lowerCamelCase  ,      axis=-1							)

         def handle_cumulative_probs(_lowerCamelCase  ,      _lowerCamelCase							):
           UpperCamelCase_: List[str]   				=    jax.nn.logsumexp(logprobs_k[self.timestamp_begin :]  ,      axis=-1							)
           UpperCamelCase_: int   				=    jnp.max(logprobs_k[: self.timestamp_begin]							)
           return jnp.where(
               timestamp_logprob > max_text_token_logprob  ,      scores_k.at[: self.timestamp_begin].set(-float('inf'							)							)  ,      _lowerCamelCase  ,      )

         UpperCamelCase_: Tuple   				=    jax.vmap(_lowerCamelCase							)(_lowerCamelCase  ,      _lowerCamelCase							)

         return scores







# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from accelerate.utils import ComputeEnvironment

from .cluster import get_cluster_input
from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file  # noqa: F401
from .config_utils import _ask_field, _ask_options, _convert_compute_environment  # noqa: F401
from .sagemaker import get_sagemaker_input


A_		:		Any		     =			'Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine'

def   snake_case ()	->    Union[str, Any]:
  UpperCamelCase_: Tuple   				=    _ask_options(
      'In which compute environment are you running?'     ,   ['This machine', 'AWS (Amazon SageMaker)']     ,   _convert_compute_environment     ,   )
  if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
    UpperCamelCase_: List[str]   				=    get_sagemaker_input()
  else:
    UpperCamelCase_: List[str]   				=    get_cluster_input()
  return config

def   snake_case (UpperCAmelCase__=None  )	->    Union[str, Any]:
  if subparsers is not None:
    UpperCamelCase_: List[Any]   				=    subparsers.add_parser('config'     ,   description=UpperCAmelCase__  )
  else:
    UpperCamelCase_: List[Any]   				=    argparse.ArgumentParser('Accelerate config command'     ,   description=UpperCAmelCase__  )

  parser.add_argument(
      '--config_file'     ,   default=UpperCAmelCase__     ,   help=(
          'The path to use to store the config file. Will default to a file named default_config.yaml in the cache '
          'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '
          'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '
          'with \'huggingface\'.'
      )     ,   )

  if subparsers is not None:
    parser.set_defaults(func=UpperCAmelCase__  )
  return parser

def   snake_case (UpperCAmelCase__  )	->    List[Any]:
  UpperCamelCase_: Union[str, Any]   				=    get_user_input()
  if args.config_file is not None:
    UpperCamelCase_: Tuple   				=    args.config_file
  else:
    if not os.path.isdir(UpperCAmelCase__  ):
      os.makedirs(UpperCAmelCase__  )
    UpperCamelCase_: Dict   				=    default_yaml_config_file

  if config_file.endswith('.json'  ):
    config.to_json_file(UpperCAmelCase__  )
  else:
    config.to_yaml_file(UpperCAmelCase__  )
  print(F'''accelerate configuration saved at {config_file}'''  )

def   snake_case ()	->    str:
  UpperCamelCase_: Tuple   				=    config_command_parser()
  UpperCamelCase_: int   				=    parser.parse_args()
  config_command(UpperCAmelCase__  )


if __name__ == "__main__":
   main()