


'''simple docstring'''

import math
import time

from transformers import Trainer, 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      snake_case    (    lowercase			):





  """simple docstring"""




  def __init__(    self			,  *UpperCamelCase			,  UpperCamelCase=None			,  UpperCamelCase=None			,  **UpperCamelCase     ):







   """simple docstring"""
   super().__init__(*UpperCamelCase			,  **UpperCamelCase     )
   lowerCamelCase_      =  eval_examples
   lowerCamelCase_      =  post_process_function






  def   snake_case		(    self			,  UpperCamelCase=None			,  UpperCamelCase=None			,  UpperCamelCase=None			,  UpperCamelCase = "eval"     ):







   """simple docstring"""
   lowerCamelCase_      =  self.eval_dataset if eval_dataset is None else eval_dataset
   lowerCamelCase_      =  self.get_eval_dataloader(UpperCamelCase     )
   lowerCamelCase_      =  self.eval_examples if eval_examples is None else eval_examples

   # Temporarily disable metric computation, we will do it in the loop here.
   lowerCamelCase_      =  self.compute_metrics
   lowerCamelCase_      =  None
   lowerCamelCase_      =  self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
   lowerCamelCase_      =  time.time()
   try:
    lowerCamelCase_      =  eval_loop(
        UpperCamelCase			,  description="Evaluation"			,  prediction_loss_only=True if compute_metrics is None else None			,  ignore_keys=UpperCamelCase			,  metric_key_prefix=UpperCamelCase			,  )
   finally:
    lowerCamelCase_      =  compute_metrics
   lowerCamelCase_      =  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
    lowerCamelCase_      =  self.post_process_function(UpperCamelCase			,  UpperCamelCase			,  output.predictions     )
    lowerCamelCase_      =  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}_'''     ):
      lowerCamelCase_      =  metrics.pop(UpperCamelCase     )
    metrics.update(output.metrics     )
   else:
    lowerCamelCase_      =  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()     )

   lowerCamelCase_      =  self.callback_handler.on_evaluate(self.args			,  self.state			,  self.control			,  UpperCamelCase     )
   return metrics







  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase=None			,  UpperCamelCase = "test"     ):







   """simple docstring"""
   lowerCamelCase_      =  self.get_test_dataloader(UpperCamelCase     )

   # Temporarily disable metric computation, we will do it in the loop here.
   lowerCamelCase_      =  self.compute_metrics
   lowerCamelCase_      =  None
   lowerCamelCase_      =  self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
   lowerCamelCase_      =  time.time()
   try:
    lowerCamelCase_      =  eval_loop(
        UpperCamelCase			,  description="Prediction"			,  prediction_loss_only=True if compute_metrics is None else None			,  ignore_keys=UpperCamelCase			,  metric_key_prefix=UpperCamelCase			,  )
   finally:
    lowerCamelCase_      =  compute_metrics
   lowerCamelCase_      =  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

   lowerCamelCase_      =  self.post_process_function(UpperCamelCase			,  UpperCamelCase			,  output.predictions			,  "predict"     )
   lowerCamelCase_      =  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}_'''     ):
     lowerCamelCase_      =  metrics.pop(UpperCamelCase     )
   metrics.update(output.metrics     )
   return PredictionOutput(predictions=predictions.predictions			,  label_ids=predictions.label_ids			,  metrics=UpperCamelCase     )






# 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.

from pathlib import Path

import torch

from ...utils import is_npu_available, is_xpu_available
from .config_args import ClusterConfig, default_json_config_file
from .config_utils import SubcommandHelpFormatter


lowercase_									=   'Create a default config file for Accelerate with only a few flags set.'
def      a      (			A__	:   Optional[Any]="no"      ,   A__	:   str = default_json_config_file      ,   A__	:   bool = False     )    ->      Optional[int]:





      """simple docstring"""



      _lowercase	       =Path(A__     )
      path.parent.mkdir(parents=A__      ,   exist_ok=A__     )
      if path.exists():
            print(
                F'''Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.'''     )
            return False
      _lowercase	       =mixed_precision.lower()
      if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
            raise ValueError(
                F'''`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}'''     )
      _lowercase	       ={
          'compute_environment': 'LOCAL_MACHINE',
          'mixed_precision': mixed_precision,
      }
      if torch.cuda.is_available():
            _lowercase	       =torch.cuda.device_count()
            _lowercase	       =num_gpus
            _lowercase	       =False
            if num_gpus > 1:
                  _lowercase	       ='MULTI_GPU'
            else:
                  _lowercase	       ='NO'
      elif is_xpu_available() and use_xpu:
            _lowercase	       =torch.xpu.device_count()
            _lowercase	       =num_xpus
            _lowercase	       =False
            if num_xpus > 1:
                  _lowercase	       ='MULTI_XPU'
            else:
                  _lowercase	       ='NO'
      elif is_npu_available():
            _lowercase	       =torch.npu.device_count()
            _lowercase	       =num_npus
            _lowercase	       =False
            if num_npus > 1:
                  _lowercase	       ='MULTI_NPU'
            else:
                  _lowercase	       ='NO'
      else:
            _lowercase	       =0
            _lowercase	       =True
            _lowercase	       =1
            _lowercase	       ='NO'
      _lowercase	       =ClusterConfig(**A__     )
      config.to_json_file(A__     )
      return path
def      a      (			A__	:   Dict      ,   A__	:   Optional[Any]     )    ->      List[Any]:





      """simple docstring"""



      _lowercase	       =parser.add_parser('default'      ,   parents=A__      ,   help=A__      ,   formatter_class=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\'.'
          )      ,   dest='save_location'      ,   )

      parser.add_argument(
          '--mixed_precision'      ,   choices=['no', 'fp16', 'bf16']      ,   type=A__      ,   help='Whether or not to use mixed precision training. '
          'Choose between FP16 and BF16 (bfloat16) training. '
          'BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.'      ,   default='no'      ,   )
      parser.set_defaults(func=A__     )
      return parser







def      a      (			A__	:   List[str]     )    ->      Any:





      """simple docstring"""



      _lowercase	       =write_basic_config(args.mixed_precision      ,   args.save_location     )
      if config_file:
            print(F'''accelerate configuration saved at {config_file}'''     )


