

"""simple docstring"""





import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
   import torch_xla.core.xla_model as xm
   import torch_xla.debug.metrics as met




class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 def __init__(      self							:      Optional[Any], *UpperCAmelCase__							:      List[str], UpperCAmelCase__							:      Dict=None, UpperCAmelCase__							:      Optional[Any]=None, **UpperCAmelCase__							:      int     ):
    super().__init__(*UpperCAmelCase__, **UpperCAmelCase__     )
    __lowercase								=						eval_examples
    __lowercase								=						post_process_function


 def  _lowercase			(      self							:      List[str], UpperCAmelCase__							:      Optional[Dataset] = None, UpperCAmelCase__							:      str=None, UpperCAmelCase__							:      Optional[List[str]] = None, UpperCAmelCase__							:      str = "eval", **UpperCAmelCase__							:      List[Any], ):
    __lowercase								=						gen_kwargs.copy()
    __lowercase								=						(
        gen_kwargs["max_length"] if gen_kwargs.get("max_length"     ) is not None else self.args.generation_max_length
    )
    __lowercase								=						(
        gen_kwargs["num_beams"] if gen_kwargs.get("num_beams"     ) is not None else self.args.generation_num_beams
    )
    __lowercase								=						gen_kwargs

    __lowercase								=						self.eval_dataset if eval_dataset is None else eval_dataset
    __lowercase								=						self.get_eval_dataloader(UpperCAmelCase__     )
    __lowercase								=						self.eval_examples if eval_examples is None else eval_examples

    # Temporarily disable metric computation, we will do it in the loop here.
    __lowercase								=						self.compute_metrics
    __lowercase								=						None
    __lowercase								=						time.time()
    __lowercase								=						self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
    try:
       __lowercase								=						eval_loop(
           UpperCAmelCase__, description="Evaluation", prediction_loss_only=True if compute_metrics is None else None, ignore_keys=UpperCAmelCase__, metric_key_prefix=UpperCAmelCase__, )
    finally:
       __lowercase								=						compute_metrics
    __lowercase								=						self.args.eval_batch_size * self.args.world_size
    if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics:
       start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""]
    output.metrics.update(
        speed_metrics(
            UpperCAmelCase__, UpperCAmelCase__, num_samples=output.num_samples, num_steps=math.ceil(output.num_samples / total_batch_size     ), )     )

    if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
       # Only the main node write the results by default
       __lowercase								=						self.post_process_function(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__     )
       __lowercase								=						self.compute_metrics(UpperCAmelCase__     )

       # Prefix all keys with metric_key_prefix + '_'
       for key in list(metrics.keys()     ):
          if not key.startswith(F"""{metric_key_prefix}_"""     ):
             __lowercase								=						metrics.pop(UpperCAmelCase__     )

       metrics.update(output.metrics     )
    else:
       __lowercase								=						output.metrics

    if self.args.should_log:
       # Only the main node log the results by default
       self.log(UpperCAmelCase__     )

    if self.args.tpu_metrics_debug or self.args.debug:
       # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
       xm.master_print(met.metrics_report()     )

    __lowercase								=						self.callback_handler.on_evaluate(self.args, self.state, self.control, UpperCAmelCase__     )
    return metrics



 def  _lowercase			(      self							:      str, UpperCAmelCase__							:      int, UpperCAmelCase__							:      Tuple, UpperCAmelCase__							:      Optional[int]=None, UpperCAmelCase__							:      str = "test", **UpperCAmelCase__							:      str     ):
    __lowercase								=						gen_kwargs.copy()

    __lowercase								=						self.get_test_dataloader(UpperCAmelCase__     )

    # Temporarily disable metric computation, we will do it in the loop here.
    __lowercase								=						self.compute_metrics
    __lowercase								=						None
    __lowercase								=						time.time()
    __lowercase								=						self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
    try:
       __lowercase								=						eval_loop(
           UpperCAmelCase__, description="Prediction", prediction_loss_only=True if compute_metrics is None else None, ignore_keys=UpperCAmelCase__, metric_key_prefix=UpperCAmelCase__, )
    finally:
       __lowercase								=						compute_metrics

    __lowercase								=						self.args.eval_batch_size * self.args.world_size
    if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics:
       start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""]
    output.metrics.update(
        speed_metrics(
            UpperCAmelCase__, UpperCAmelCase__, num_samples=output.num_samples, num_steps=math.ceil(output.num_samples / total_batch_size     ), )     )
    if self.post_process_function is None or self.compute_metrics is None:
       return output

    __lowercase								=						self.post_process_function(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, "predict"     )
    __lowercase								=						self.compute_metrics(UpperCAmelCase__     )

    # Prefix all keys with metric_key_prefix + '_'
    for key in list(metrics.keys()     ):
       if not key.startswith(F"""{metric_key_prefix}_"""     ):
          __lowercase								=						metrics.pop(UpperCAmelCase__     )
    metrics.update(output.metrics     )
    return PredictionOutput(predictions=predictions.predictions, label_ids=predictions.label_ids, metrics=UpperCAmelCase__     )







"""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							=							'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   _A  (  )     ->						Tuple:
   '''simple docstring'''


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




def   _A  (  UpperCamelCase_					:	Union[str, Any]=None)     ->						Union[str, Any]:
   '''simple docstring'''


   if subparsers is not None:
      __lowercase								=						subparsers.add_parser("config",      description=UpperCamelCase_)
   else:
      __lowercase								=						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   _A  (  UpperCamelCase_					:	Dict)     ->						str:
   '''simple docstring'''


   __lowercase								=						get_user_input()
   if args.config_file is not None:
      __lowercase								=						args.config_file
   else:
      if not os.path.isdir(UpperCamelCase_):
         os.makedirs(UpperCamelCase_)
      __lowercase								=						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   _A  (  )     ->						Optional[Any]:
   '''simple docstring'''


   __lowercase								=						config_command_parser()
   __lowercase								=						parser.parse_args()
   config_command(UpperCamelCase_)


if __name__ == "__main__":
   main()




