




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       a__	( __lowercase				):







     def __init__(     self  ,		*A  ,		A=None  ,		A=None  ,		**A			)  ->   List[Any]:



          '''simple docstring'''



          super().__init__(*UpperCAmelCase__  ,		**UpperCAmelCase__			)
          a     =				eval_examples
          a     =				post_process_function







     def       lowerCAmelCase_    (     self  ,		A=None  ,		A=None  ,		A=None  ,		A = "eval"			)  ->   Tuple:



          '''simple docstring'''



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

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

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







     def       lowerCAmelCase_    (     self  ,		A  ,		A  ,		A=None  ,		A = "test"			)  ->   Optional[Any]:



          '''simple docstring'''



          a     =				self.get_test_dataloader(UpperCAmelCase__			)

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

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










lowercase__				:      Optional[int]	   = [
    "DownloadConfig",
    "DownloadManager",
    "DownloadMode",
    "StreamingDownloadManager",
]

from .download_config import DownloadConfig
from .download_manager import DownloadManager, DownloadMode
from .streaming_download_manager import StreamingDownloadManager

