


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


__A :  str    							=				get_logger(__name__)


__A :  List[Any]    							=				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        __snake_case	:







				"""simple docstring"""



				@add_start_docstrings(UpperCamelCase_		)
				def __call__(       self   :  Dict   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  jnp.ndarray		)       ->  jnp.ndarray:
						raise NotImplementedError(
						    F'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'		)







class        __snake_case	:







				"""simple docstring"""



				@add_start_docstrings(UpperCamelCase_		)
				def __call__(       self   :  Tuple   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  jnp.ndarray		)       ->  jnp.ndarray:
						raise NotImplementedError(
						    F'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'		)







class        __snake_case	(       UpperCamelCase__):







				"""simple docstring"""



				@add_start_docstrings(UpperCamelCase_		)
				def __call__(       self   :  Dict   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  int   ,						**lowerCamelCase   :  str		)       ->  jnp.ndarray:







						for processor in self:
								lowerCAmelCase_      :				int      =       inspect.signature(processor.__call__		).parameters
								if len(UpperCamelCase_		) > 3:
										if not all(arg in kwargs for arg in list(function_args.keys()		)[2:]		):
												raise ValueError(
												    F'Make sure that all the required parameters: {list(function_args.keys()		)} for '
												    F'{processor.__class__} are passed to the logits processor.'		)
										lowerCAmelCase_      :				Any      =       processor(UpperCamelCase_   ,						UpperCamelCase_   ,						UpperCamelCase_   ,						**UpperCamelCase_		)
								else:
										lowerCAmelCase_      :				Any      =       processor(UpperCamelCase_   ,						UpperCamelCase_   ,						UpperCamelCase_		)
						return scores







class        __snake_case	(       UpperCamelCase__):







				"""simple docstring"""



				def __init__(       self   :  List[Any]   ,						lowerCamelCase   :  float		)       ->  Optional[Any]:







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

						lowerCAmelCase_      :				int      =       temperature





				def __call__(       self   :  Union[str, Any]   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  int		)       ->  jnp.ndarray:
						lowerCAmelCase_      :				Optional[int]      =       scores / self.temperature
						return scores







class        __snake_case	(       UpperCamelCase__):







				"""simple docstring"""



				def __init__(       self   :  int   ,						lowerCamelCase   :  float   ,						lowerCamelCase   :  float = -float("""Inf"""		)   ,						lowerCamelCase   :  int = 1		)       ->  Optional[Any]:







						if not isinstance(UpperCamelCase_   ,						UpperCamelCase_		) 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(UpperCamelCase_   ,						UpperCamelCase_		) or (min_tokens_to_keep < 1):
								raise ValueError(F'`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}'		)

						lowerCAmelCase_      :				Dict      =       top_p
						lowerCAmelCase_      :				Any      =       filter_value
						lowerCAmelCase_      :				List[str]      =       min_tokens_to_keep





				def __call__(       self   :  List[Any]   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  int		)       ->  jnp.ndarray:
						lowerCAmelCase_,							lowerCAmelCase_      :				Optional[Any]      =       lax.top_k(UpperCamelCase_   ,						scores.shape[-1]		)

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

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

						# min tokens to keep
						lowerCAmelCase_      :				Optional[int]      =       score_mask.at[:, : self.min_tokens_to_keep].set(UpperCamelCase_		)

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

						return next_scores







class        __snake_case	(       UpperCamelCase__):







				"""simple docstring"""



				def __init__(       self   :  Dict   ,						lowerCamelCase   :  int   ,						lowerCamelCase   :  float = -float("""Inf"""		)   ,						lowerCamelCase   :  int = 1		)       ->  Dict:







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

						lowerCAmelCase_      :				Optional[Any]      =       max(UpperCamelCase_   ,						UpperCamelCase_		)
						lowerCAmelCase_      :				Union[str, Any]      =       filter_value





				def __call__(       self   :  Any   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  int		)       ->  jnp.ndarray:
						lowerCAmelCase_,							lowerCAmelCase_      :				List[str]      =       scores.shape
						lowerCAmelCase_      :				Tuple      =       jnp.full(batch_size * vocab_size   ,						self.filter_value		)

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

						lowerCAmelCase_      :				Optional[Any]      =       next_scores_flat.at[topk_indices_flat].set(UpperCamelCase_		)
						lowerCAmelCase_      :				int      =       next_scores_flat.reshape(UpperCamelCase_   ,						UpperCamelCase_		)
						return next_scores







class        __snake_case	(       UpperCamelCase__):







				"""simple docstring"""



				def __init__(       self   :  Dict   ,						lowerCamelCase   :  int		)       ->  Any:
						lowerCAmelCase_      :				List[str]      =       bos_token_id





				def __call__(       self   :  Optional[Any]   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  int		)       ->  jnp.ndarray:
						lowerCAmelCase_      :				str      =       jnp.full(scores.shape   ,						-float("""inf"""		)		)

						lowerCAmelCase_      :				int      =       1 - jnp.bool_(cur_len - 1		)

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

						return scores







class        __snake_case	(       UpperCamelCase__):







				"""simple docstring"""



				def __init__(       self   :  Dict   ,						lowerCamelCase   :  int   ,						lowerCamelCase   :  int		)       ->  Any:
						lowerCAmelCase_      :				Optional[int]      =       max_length
						lowerCAmelCase_      :				Tuple      =       eos_token_id





				def __call__(       self   :  List[str]   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  int		)       ->  jnp.ndarray:
						lowerCAmelCase_      :				Union[str, Any]      =       jnp.full(scores.shape   ,						-float("""inf"""		)		)

						lowerCAmelCase_      :				int      =       1 - jnp.bool_(cur_len - self.max_length + 1		)

						lowerCAmelCase_      :				Tuple      =       jnp.where(UpperCamelCase_   ,						new_scores.at[:, self.eos_token_id].set(0		)   ,						UpperCamelCase_		)

						return scores







class        __snake_case	(       UpperCamelCase__):







				"""simple docstring"""



				def __init__(       self   :  Union[str, Any]   ,						lowerCamelCase   :  int   ,						lowerCamelCase   :  int		)       ->  Any:







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

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

						lowerCAmelCase_      :				Optional[Any]      =       min_length
						lowerCAmelCase_      :				List[Any]      =       eos_token_id





				def __call__(       self   :  List[str]   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  int		)       ->  jnp.ndarray:
						lowerCAmelCase_      :				Optional[int]      =       1 - jnp.clip(cur_len - self.min_length   ,						0   ,						1		)

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

						return scores







class        __snake_case	(       UpperCamelCase__):







				"""simple docstring"""



				def __init__(       self   :  List[Any]   ,						lowerCamelCase   :  Optional[int]   ,						lowerCamelCase   :  Union[str, Any]		)       ->  Optional[Any]:
						lowerCAmelCase_      :				Optional[int]      =       list(UpperCamelCase_		)
						lowerCAmelCase_      :				Optional[Any]      =       begin_index





				def __call__(       self   :  Tuple   ,						lowerCamelCase   :  List[str]   ,						lowerCamelCase   :  Optional[int]   ,						lowerCamelCase   :  int		)       ->  Tuple:
						lowerCAmelCase_      :				str      =       1 - jnp.bool_(cur_len - self.begin_index		)

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

						return scores







class        __snake_case	(       UpperCamelCase__):







				"""simple docstring"""



				def __init__(       self   :  Optional[Any]   ,						lowerCamelCase   :  list		)       ->  int:
						lowerCAmelCase_      :				Tuple      =       list(UpperCamelCase_		)





				def __call__(       self   :  int   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  int		)       ->  jnp.ndarray:
						lowerCAmelCase_      :				Union[str, Any]      =       scores.at[..., self.suppress_tokens].set(-float("""inf"""		)		)

						return scores







class        __snake_case	(       UpperCamelCase__):







				"""simple docstring"""



				def __init__(       self   :  str   ,						lowerCamelCase   :  Any		)       ->  Optional[Any]:
						lowerCAmelCase_      :				Tuple      =       dict(UpperCamelCase_		)
						# Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the
						# index of the array corresponds to the index of the token to be forced, for XLA compatibility.
						# Indexes without forced tokens will have a negative value.
						lowerCAmelCase_      :				List[str]      =       jnp.ones((max(force_token_map.keys()		) + 1)   ,						dtype=jnp.intaa		) * -1
						for index, token in force_token_map.items():
								if token is not None:
										lowerCAmelCase_      :				Optional[Any]      =       force_token_array.at[index].set(UpperCamelCase_		)
						lowerCAmelCase_      :				List[str]      =       jnp.intaa(UpperCamelCase_		)





				def __call__(       self   :  str   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  jnp.ndarray   ,						lowerCamelCase   :  int		)       ->  jnp.ndarray:







						def _force_token(lowerCamelCase   :  List[str]		):
								lowerCAmelCase_      :				Union[str, Any]      =       scores.shape[0]
								lowerCAmelCase_      :				Dict      =       self.force_token_array[generation_idx]

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

						lowerCAmelCase_      :				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(UpperCamelCase_		)   ,						lambda: scores   ,						)   ,						)
						return scores







class        __snake_case	(       UpperCamelCase__):







				"""simple docstring"""



				def __init__(       self   :  Optional[int]   ,						lowerCamelCase   :  List[str]   ,						lowerCamelCase   :  int   ,						lowerCamelCase   :  int		)       ->  Union[str, Any]:
						lowerCAmelCase_      :				Tuple      =       generate_config.eos_token_id
						lowerCAmelCase_      :				List[str]      =       generate_config.no_timestamps_token_id
						lowerCAmelCase_      :				List[str]      =       generate_config.no_timestamps_token_id + 1

						lowerCAmelCase_      :				List[str]      =       decoder_input_length + 1

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





				def __call__(       self   :  Any   ,						lowerCamelCase   :  Optional[int]   ,						lowerCamelCase   :  Optional[Any]   ,						lowerCamelCase   :  str		)       ->  Union[str, Any]:
						lowerCAmelCase_      :				str      =       scores.at[:, self.no_timestamps_token_id].set(-float("""inf"""		)		)

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

								lowerCAmelCase_      :				Optional[int]      =       jnp.where((cur_len - self.begin_index) < 2   ,						UpperCamelCase_   ,						UpperCamelCase_		)
								lowerCAmelCase_      :				Tuple      =       jnp.where(
								    input_ids_k[cur_len - 2] >= self.timestamp_begin   ,						UpperCamelCase_   ,						UpperCamelCase_   ,						)

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

						lowerCAmelCase_      :				List[Any]      =       jax.vmap(UpperCamelCase_		)(UpperCamelCase_   ,						UpperCamelCase_		)

						lowerCAmelCase_      :				int      =       jnp.where(cur_len == self.begin_index   ,						UpperCamelCase_   ,						UpperCamelCase_		)
						lowerCAmelCase_      :				Optional[Any]      =       jnp.where(
						    self.max_initial_timestamp_index is not None   ,						True and apply_max_initial_timestamp   ,						UpperCamelCase_   ,						)

						lowerCAmelCase_      :				Optional[Any]      =       self.timestamp_begin + self.max_initial_timestamp_index

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

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

						def handle_cumulative_probs(lowerCamelCase   :  Optional[Any]   ,						lowerCamelCase   :  Tuple		):
								lowerCAmelCase_      :				List[Any]      =       jax.nn.logsumexp(logprobs_k[self.timestamp_begin :]   ,						axis=-1		)
								lowerCAmelCase_      :				Union[str, Any]      =       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"""		)		)   ,						UpperCamelCase_   ,						)

						lowerCAmelCase_      :				int      =       jax.vmap(UpperCamelCase_		)(UpperCamelCase_   ,						UpperCamelCase_		)

						return scores







'''simple docstring'''






# 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 :  str    							=				"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     UpperCamelCase_      (   ):
  '''simple docstring'''





  lowerCAmelCase_      :				List[Any]      =       _ask_options(
      """In which compute environment are you running?"""							,   ["""This machine""", """AWS (Amazon SageMaker)"""]							,   _convert_compute_environment							,   )
  if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
    lowerCAmelCase_      :				str      =       get_sagemaker_input()
  else:
    lowerCAmelCase_      :				Optional[int]      =       get_cluster_input()
  return config







def     UpperCamelCase_      (   A__ :       Optional[Any]=None ):
  '''simple docstring'''





  if subparsers is not None:
    lowerCAmelCase_      :				List[str]      =       subparsers.add_parser("""config"""							,   description=A__ )
  else:
    lowerCAmelCase_      :				Optional[int]      =       argparse.ArgumentParser("""Accelerate config command"""							,   description=A__ )

  parser.add_argument(
      """--config_file"""							,   default=A__							,   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=A__ )
  return parser







def     UpperCamelCase_      (   A__ :       Any ):
  '''simple docstring'''





  lowerCAmelCase_      :				Dict      =       get_user_input()
  if args.config_file is not None:
    lowerCAmelCase_      :				List[str]      =       args.config_file
  else:
    if not os.path.isdir(A__ ):
      os.makedirs(A__ )
    lowerCAmelCase_      :				List[Any]      =       default_yaml_config_file

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







def     UpperCamelCase_      (   ):
  '''simple docstring'''





  lowerCAmelCase_      :				str      =       config_command_parser()
  lowerCAmelCase_      :				Tuple      =       parser.parse_args()
  config_command(A__ )


if __name__ == "__main__":
  main()



